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

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

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

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
22
23
24
#include "libaec.h"
#include "encode.h"
#include "encode_accessors.h"
25

26
/* Marker for Remainder Of Segment condition in zero block encoding */
27
#define ROS -1
28

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
29
30
31
32
33
34
35
36
37
38
39
40
41
static int m_get_block(struct aec_stream *strm);
static int m_get_block_cautious(struct aec_stream *strm);
static int m_check_zero_block(struct aec_stream *strm);
static int m_select_code_option(struct aec_stream *strm);
static int m_flush_block(struct aec_stream *strm);
static int m_flush_block_cautious(struct aec_stream *strm);
static int m_encode_splitting(struct aec_stream *strm);
static int m_encode_uncomp(struct aec_stream *strm);
static int m_encode_se(struct aec_stream *strm);
static int m_encode_zero(struct aec_stream *strm);

static inline void emit(struct internal_state *state,
                        uint32_t data, int bits)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
42
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
43
44
45
46
47
    /**
       Emit sequence of bits.
     */

    if (bits <= state->bit_p) {
48
49
        state->bit_p -= bits;
        *state->cds_p += data << state->bit_p;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
50
    } else {
51
        bits -= state->bit_p;
52
        *state->cds_p++ += (uint64_t)data >> bits;
53

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
54
        while (bits & ~7) {
55
            bits -= 8;
56
            *state->cds_p++ = data >> bits;
57
        }
58

59
60
        state->bit_p = 8 - bits;
        *state->cds_p = data << state->bit_p;
61
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
62
63
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
64
static inline void emitfs(struct internal_state *state, int fs)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
65
{
66
67
    /**
       Emits a fundamental sequence.
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
68

69
70
       fs zero bits followed by one 1 bit.
     */
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
71

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
72
73
    for(;;) {
        if (fs < state->bit_p) {
74
75
            state->bit_p -= fs + 1;
            *state->cds_p += 1 << state->bit_p;
76
            break;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
77
        } else {
78
79
80
            fs -= state->bit_p;
            *++state->cds_p = 0;
            state->bit_p = 8;
81
82
        }
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
83
}
84

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
85
#define EMITBLOCK(ref)                                          \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
86
87
    static inline void emitblock_##ref(struct aec_stream *strm, \
                                       int k)                   \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
88
89
90
    {                                                           \
        int b;                                                  \
        uint64_t a;                                             \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
91
        struct internal_state *state = strm->state;             \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
92
93
94
95
96
97
98
99
        uint32_t *in = state->block_p + ref;                    \
        uint32_t *in_end = state->block_p + strm->block_size;   \
        uint64_t mask = (1ULL << k) - 1;                        \
        uint8_t *o = state->cds_p;                              \
        int p = state->bit_p;                                   \
                                                                \
        a = *o;                                                 \
                                                                \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
100
        while(in < in_end) {                                    \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
101
102
103
            a <<= 56;                                           \
            p = (p % 8) + 56;                                   \
                                                                \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
104
            while (p > k && in < in_end) {                      \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
                p -= k;                                         \
                a += ((uint64_t)(*in++) & mask) << p;           \
            }                                                   \
                                                                \
            for (b = 56; b > (p & ~7); b -= 8)                  \
                *o++ = a >> b;                                  \
            a >>= b;                                            \
        }                                                       \
                                                                \
        *o = a;                                                 \
        state->cds_p = o;                                       \
        state->bit_p = p % 8;                                   \
    }

EMITBLOCK(0);
EMITBLOCK(1);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
122
static void preprocess_unsigned(struct aec_stream *strm)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
123
{
124
    int64_t d;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
125
    struct internal_state *state = strm->state;
126
127
    uint32_t *x = state->block_buf;
    int64_t x1 = *x++;
128
129
    uint32_t xmax = state->xmax;
    uint32_t rsi = strm->rsi * strm->block_size - 1;
130
131

    while (rsi--) {
132
133
134
135
136
137
138
139
        if (*x >= x1) {
            d = *x - x1;
            if (d <= x1) {
                x1 = *x;
                *x = 2 * d;
            } else {
                x1 = *x;
            }
140
        } else {
141
142
143
144
145
146
147
148
            d = x1 - *x;
            if (d <= xmax - x1) {
                x1 = *x;
                *x = 2 * d - 1;
            } else {
                x1 = *x;
                *x = xmax - *x;
            }
149
        }
150
        x++;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
151
152
153
    }
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
154
static void preprocess_signed(struct aec_stream *strm)
155
{
156
    int64_t d;
157
    int64_t x;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
158
    struct internal_state *state = strm->state;
159
160
    uint32_t *buf = state->block_buf;
    uint32_t m = 1ULL << (strm->bit_per_sample - 1);
161
    int64_t x1 = (((int64_t)*buf++) ^ m) - m;
162
163
164
    int64_t xmax = state->xmax;
    int64_t xmin = state->xmin;
    uint32_t rsi = strm->rsi * strm->block_size - 1;
165
166

    while (rsi--) {
167
168
169
170
171
172
173
        x = (((int64_t)*buf) ^ m) - m;
        if (x < x1) {
            d = x1 - x;
            if (d <= xmax - x1)
                *buf = 2 * d - 1;
            else
                *buf = xmax - x;
174
        } else {
175
176
177
178
179
            d = x - x1;
            if (d <= x1 - xmin)
                *buf = 2 * d;
            else
                *buf = x - xmin;
180
        }
181
        x1 = x;
182
        buf++;
183
    }
184
}
185

186
187
188
189
190
191
/*
 *
 * FSM functions
 *
 */

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
192
static int m_get_block(struct aec_stream *strm)
193
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
194
    struct internal_state *state = strm->state;
195

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
196
197
    if (strm->avail_out > state->cds_len) {
        if (!state->direct_out) {
198
199
200
            state->direct_out = 1;
            *strm->next_out = *state->cds_p;
            state->cds_p = strm->next_out;
201
        }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
202
203
    } else {
        if (state->zero_blocks == 0 || state->direct_out) {
204
            /* copy leftover from last block */
205
206
            *state->cds_buf = *state->cds_p;
            state->cds_p = state->cds_buf;
207
        }
208
        state->direct_out = 0;
209
    }
210

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
211
    if (state->blocks_avail == 0) {
212
        state->block_p = state->data_pp;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
213

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
214
        if (strm->avail_in >= state->block_len * strm->rsi) {
215
            state->get_rsi(strm);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
216
            state->blocks_avail = strm->rsi - 1;
217

218
            if (strm->flags & AEC_DATA_PREPROCESS) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
219
                state->preprocess(strm);
220
221
                state->ref = 1;
            }
222
            return m_check_zero_block(strm);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
223
        } else {
224
225
226
            state->i = 0;
            state->mode = m_get_block_cautious;
        }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
227
    } else {
228
229
230
231
        state->ref = 0;
        state->block_p += strm->block_size;
        state->blocks_avail--;
        return m_check_zero_block(strm);
232
    }
233
    return M_CONTINUE;
234
235
}

236
static int m_get_block_cautious(struct aec_stream *strm)
237
{
238
    int j;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
239
240
    struct internal_state *state = strm->state;

241
242
243
    do {
        if (strm->avail_in > 0) {
            state->block_buf[state->i] = state->get_sample(strm);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
244
        } else {
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
            if (state->flush == AEC_FLUSH) {
                if (state->i > 0) {
                    for (j = state->i; j < strm->rsi * strm->block_size; j++)
                        state->block_buf[j] = state->block_buf[state->i - 1];
                    state->i = strm->rsi * strm->block_size;
                } else {
                    if (state->zero_blocks) {
                        state->mode = m_encode_zero;
                        return M_CONTINUE;
                    }

                    emit(state, 0, state->bit_p);
                    if (state->direct_out == 0)
                        *strm->next_out++ = *state->cds_p;
                    strm->avail_out--;
                    strm->total_out++;

                    return M_EXIT;
                }
            } else {
                return M_EXIT;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
266
            }
267
        }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
268
    } while (++state->i < strm->rsi * strm->block_size);
269

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
270
    state->blocks_avail = strm->rsi - 1;
271
    if (strm->flags & AEC_DATA_PREPROCESS) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
272
        state->preprocess(strm);
273
274
        state->ref = 1;
    }
275
276

    return m_check_zero_block(strm);
277
278
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
279
static int m_check_zero_block(struct aec_stream *strm)
280
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
281
    struct internal_state *state = strm->state;
282
283
284
    uint32_t *p = state->block_p + state->ref;
    uint32_t *end = state->block_p + strm->block_size;

285
    while(p < end && *p == 0)
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
        p++;

    if (p < end) {
        if (state->zero_blocks) {
            /* The current block isn't zero but we have to emit a
             * previous zero block first. The current block will be
             * handled later.
             */
            state->block_p -= strm->block_size;
            state->blocks_avail++;
            state->mode = m_encode_zero;
            return M_CONTINUE;
        }
        state->mode = m_select_code_option;
        return M_CONTINUE;
    } else {
        state->zero_blocks++;
        if (state->zero_blocks == 1) {
304
            state->zero_ref = state->ref;
305
            state->zero_ref_sample = state->block_p[0];
306
        }
307
308
        if (state->blocks_avail == 0
            || (strm->rsi - state->blocks_avail) % 64 == 0) {
309
310
311
312
313
314
315
            if (state->zero_blocks > 4)
                state->zero_blocks = ROS;
            state->mode = m_encode_zero;
            return M_CONTINUE;
        }
        state->mode = m_get_block;
        return M_CONTINUE;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
316
317
318
    }
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
319
static uint64_t block_fs(struct aec_stream *strm, int k)
320
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
321
322
323
324
325
326
327
328
329
330
331
332
333
    int j;
    uint64_t fs;
    struct internal_state *state = strm->state;

    fs = (uint64_t)(state->block_p[1] >> k)
        + (uint64_t)(state->block_p[2] >> k)
        + (uint64_t)(state->block_p[3] >> k)
        + (uint64_t)(state->block_p[4] >> k)
        + (uint64_t)(state->block_p[5] >> k)
        + (uint64_t)(state->block_p[6] >> k)
        + (uint64_t)(state->block_p[7] >> k);

    if (strm->block_size > 8)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
334
        for (j = 8; j < strm->block_size; j += 8)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
335
            fs +=
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
336
337
338
339
340
341
342
343
344
345
                (uint64_t)(state->block_p[j + 0] >> k)
                + (uint64_t)(state->block_p[j + 1] >> k)
                + (uint64_t)(state->block_p[j + 2] >> k)
                + (uint64_t)(state->block_p[j + 3] >> k)
                + (uint64_t)(state->block_p[j + 4] >> k)
                + (uint64_t)(state->block_p[j + 5] >> k)
                + (uint64_t)(state->block_p[j + 6] >> k)
                + (uint64_t)(state->block_p[j + 7] >> k);

    if (!state->ref)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
346
347
348
349
        fs += (uint64_t)(state->block_p[0] >> k);

    return fs;
}
350

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
351
352
static int count_splitting_option(struct aec_stream *strm)
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
    /**
       Find the best point for splitting samples in a block.

       In Rice coding each sample in a block of samples is split at
       the same position into k LSB and bit_per_sample - k MSB. The
       LSB part is left binary and the MSB part is coded as a
       fundamental sequence a.k.a. unary (see CCSDS 121.0-B-2). The
       function of the length of the Coded Data Set (CDS) depending on
       k has exactly one minimum (see A. Kiely, IPN Progress Report
       42-159).

       To find that minimum with only a few costly evaluations of the
       CDS length, we start with the k of the previous CDS. K is
       increased and the CDS length evaluated. If the CDS length gets
       smaller, then we are moving towards the minimum. If the length
368
369
370
371
372
373
374
375
       increases, then the minimum will be found with smaller k.

       For increasing k we know that we will gain block_size bits in
       length through the larger binary part. If the FS lenth is less
       than the block size then a reduced FS part can't compensate the
       larger binary part. So we know that the CDS for k+1 will be
       larger than for k without actually computing the length. An
       analogue check can be done for decreasing k.
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
376
377
378
379
     */

    int k, k_min;
    int this_bs; /* Block size of current block */
380
    int no_turn; /* 1 if we shouldn't reverse */
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
381
382
383
384
385
    int dir; /* Direction, 1 means increasing k, 0 decreasing k */
    uint64_t len; /* CDS length for current k */
    uint64_t len_min; /* CDS length minimum so far */
    uint64_t fs_len; /* Length of FS part (not including 1s) */

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
386
    struct internal_state *state = strm->state;
387

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
388
389
    this_bs = strm->block_size - state->ref;
    len_min = UINT64_MAX;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
390
    k = k_min = state->k;
391
    no_turn = (k == 0) ? 1 : 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
392
    dir = 1;
393

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
394
    for (;;) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
395
396
        fs_len = block_fs(strm, k);
        len = fs_len + this_bs * (k + 1);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
397
398

        if (len < len_min) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
399
            if (len_min < UINT64_MAX)
400
                no_turn = 1;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
401

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
402
            len_min = len;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
403
            k_min = k;
404

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
405
            if (dir) {
406
407
408
409
410
411
                if (fs_len < this_bs || k >= state->kmax) {
                    if (no_turn)
                        break;
                    k = state->k - 1;
                    dir = 0;
                    no_turn = 1;
412
413
                } else {
                    k++;
414
                }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
415
            } else {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
416
417
418
                if (fs_len >= this_bs || k == 0)
                    break;
                k--;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
419
            }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
420
        } else {
421
422
423
424
425
            if (no_turn)
                break;
            k = state->k - 1;
            dir = 0;
            no_turn = 1;
426
427
        }
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
428
    state->k = k_min;
429

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
430
431
432
433
434
435
436
437
438
439
440
441
    return len_min;
}

static int count_se_option(uint64_t limit, struct aec_stream *strm)
{
    int i;
    uint64_t d, len;
    struct internal_state *state = strm->state;

    len = 1;

    for (i = 0; i < strm->block_size; i+= 2) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
442
443
        d = (uint64_t)state->block_p[i]
            + (uint64_t)state->block_p[i + 1];
444
        /* we have to worry about overflow here */
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
445
446
        if (d > limit) {
            len = UINT64_MAX;
447
            break;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
448
449
450
        } else {
            len += d * (d + 1) / 2
                + (uint64_t)state->block_p[i + 1];
451
452
        }
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
453
454
455
456
457
458
459
    return len;
}

static int m_select_code_option(struct aec_stream *strm)
{
    uint64_t uncomp_len, split_len, se_len;
    struct internal_state *state = strm->state;
460

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
461
462
463
464
    uncomp_len = (strm->block_size - state->ref)
        * strm->bit_per_sample;
    split_len = count_splitting_option(strm);
    se_len = count_se_option(split_len, strm);
465

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
466
467
    if (split_len < uncomp_len) {
        if (split_len < se_len)
468
469
470
            return m_encode_splitting(strm);
        else
            return m_encode_se(strm);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
471
    } else {
472
473
474
475
476
477
        if (uncomp_len <= se_len)
            return m_encode_uncomp(strm);
        else
            return m_encode_se(strm);
    }
}
478

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
479
static int m_encode_splitting(struct aec_stream *strm)
480
481
{
    int i;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
482
    struct internal_state *state = strm->state;
483
    int k = state->k;
484

485
    emit(state, k + 1, state->id_len);
486

487
    if (state->ref)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
488
    {
489
        emit(state, state->block_p[0], strm->bit_per_sample);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
490
491
        for (i = 1; i < strm->block_size; i++)
            emitfs(state, state->block_p[i] >> k);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
492
493
        if (k)
            emitblock_1(strm, k);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
494
495
496
497
498
    }
    else
    {
        for (i = 0; i < strm->block_size; i++)
            emitfs(state, state->block_p[i] >> k);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
499
500
        if (k)
            emitblock_0(strm, k);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
501
    }
502

503
504
    return m_flush_block(strm);
}
505

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
506
static int m_encode_uncomp(struct aec_stream *strm)
507
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
508
    struct internal_state *state = strm->state;
509

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
510
    emit(state, (1U << state->id_len) - 1, state->id_len);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
511
    emitblock_0(strm, strm->bit_per_sample);
512

513
514
    return m_flush_block(strm);
}
515

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
516
static int m_encode_se(struct aec_stream *strm)
517
518
{
    int i;
519
    uint32_t d;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
520
    struct internal_state *state = strm->state;
521

522
523
    emit(state, 1, state->id_len + 1);
    if (state->ref)
524
        emit(state, state->block_p[0], strm->bit_per_sample);
525

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
526
    for (i = 0; i < strm->block_size; i+= 2) {
527
528
        d = state->block_p[i] + state->block_p[i + 1];
        emitfs(state, d * (d + 1) / 2 + state->block_p[i + 1]);
529
    }
530

531
532
    return m_flush_block(strm);
}
533

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
534
static int m_encode_zero(struct aec_stream *strm)
535
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
536
    struct internal_state *state = strm->state;
537

538
    emit(state, 0, state->id_len + 1);
539

540
541
    if (state->zero_ref)
        emit(state, state->zero_ref_sample, strm->bit_per_sample);
542

543
544
545
546
547
548
    if (state->zero_blocks == ROS)
        emitfs(state, 4);
    else if (state->zero_blocks >= 5)
        emitfs(state, state->zero_blocks);
    else
        emitfs(state, state->zero_blocks - 1);
549

550
551
552
    state->zero_blocks = 0;
    return m_flush_block(strm);
}
553

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
554
static int m_flush_block(struct aec_stream *strm)
555
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
556
557
558
559
560
    /**
       Flush block in direct_out mode by updating counters.

       Fall back to slow flushing if in buffered mode.
    */
561
    int n;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
562
    struct internal_state *state = strm->state;
563

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
564
    if (state->direct_out) {
565
        n = state->cds_p - strm->next_out;
566
567
568
569
570
571
        strm->next_out += n;
        strm->avail_out -= n;
        strm->total_out += n;
        state->mode = m_get_block;
        return M_CONTINUE;
    }
572

573
574
575
576
577
    state->i = 0;
    state->mode = m_flush_block_cautious;
    return M_CONTINUE;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
578
static int m_flush_block_cautious(struct aec_stream *strm)
579
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
580
581
582
583
    /**
       Slow and restartable flushing
    */
    struct internal_state *state = strm->state;
584

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
585
    while(state->cds_buf + state->i < state->cds_p) {
586
587
588
        if (strm->avail_out == 0)
            return M_EXIT;

589
        *strm->next_out++ = state->cds_buf[state->i];
590
591
592
593
594
595
596
597
598
599
600
601
602
        strm->avail_out--;
        strm->total_out++;
        state->i++;
    }
    state->mode = m_get_block;
    return M_CONTINUE;
}

/*
 *
 * API functions
 *
 */
603

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
604
int aec_encode_init(struct aec_stream *strm)
605
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
606
    struct internal_state *state;
607
608

    if (strm->bit_per_sample > 32 || strm->bit_per_sample == 0)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
609
        return AEC_CONF_ERROR;
610
611
612
613
614

    if (strm->block_size != 8
        && strm->block_size != 16
        && strm->block_size != 32
        && strm->block_size != 64)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
615
        return AEC_CONF_ERROR;
616
617

    if (strm->rsi > 4096)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
618
        return AEC_CONF_ERROR;
619

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
620
    state = (struct internal_state *)malloc(sizeof(struct internal_state));
621
    if (state == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
622
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
623
624

    memset(state, 0, sizeof(struct internal_state));
625
626
    strm->state = state;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
627
    if (strm->bit_per_sample > 16) {
628
        /* 24/32 input bit settings */
629
630
        state->id_len = 5;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
631
632
        if (strm->bit_per_sample <= 24
            && strm->flags & AEC_DATA_3BYTE) {
633
            state->block_len = 3 * strm->block_size;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
634
            if (strm->flags & AEC_DATA_MSB) {
635
                state->get_sample = get_msb_24;
636
                state->get_rsi = get_rsi_msb_24;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
637
            } else {
638
                state->get_sample = get_lsb_24;
639
                state->get_rsi = get_rsi_lsb_24;
640
            }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
641
        } else {
642
            state->block_len = 4 * strm->block_size;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
643
            if (strm->flags & AEC_DATA_MSB) {
644
                state->get_sample = get_msb_32;
645
                state->get_rsi = get_rsi_msb_32;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
646
            } else {
647
                state->get_sample = get_lsb_32;
648
                state->get_rsi = get_rsi_lsb_32;
649
650
            }
        }
651
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
652
    else if (strm->bit_per_sample > 8) {
653
654
        /* 16 bit settings */
        state->id_len = 4;
655
        state->block_len = 2 * strm->block_size;
656

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
657
        if (strm->flags & AEC_DATA_MSB) {
658
            state->get_sample = get_msb_16;
659
            state->get_rsi = get_rsi_msb_16;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
660
        } else {
661
            state->get_sample = get_lsb_16;
662
            state->get_rsi = get_rsi_lsb_16;
663
        }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
664
    } else {
665
666
        /* 8 bit settings */
        state->id_len = 3;
667
        state->block_len = strm->block_size;
668
669

        state->get_sample = get_8;
670
        state->get_rsi = get_rsi_8;
671
672
    }

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
673
    if (strm->flags & AEC_DATA_SIGNED) {
674
675
        state->xmin = -(1ULL << (strm->bit_per_sample - 1));
        state->xmax = (1ULL << (strm->bit_per_sample - 1)) - 1;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
676
        state->preprocess = preprocess_signed;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
677
    } else {
678
679
        state->xmin = 0;
        state->xmax = (1ULL << strm->bit_per_sample) - 1;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
680
        state->preprocess = preprocess_unsigned;
681
682
    }

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
683
684
    state->kmax = (1U << state->id_len) - 3;

685
686
687
    state->block_buf = (uint32_t *)malloc(strm->rsi
                                         * strm->block_size
                                         * sizeof(uint32_t));
688
    if (state->block_buf == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
689
        return AEC_MEM_ERROR;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
690

691
    state->block_p = state->block_buf;
692

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
693
    /* Largest possible CDS according to specs */
694
695
696
    state->cds_len = (5 + 64 * 32) / 8 + 3;
    state->cds_buf = (uint8_t *)malloc(state->cds_len);
    if (state->cds_buf == NULL)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
697
        return AEC_MEM_ERROR;
698

699
700
701
    strm->total_in = 0;
    strm->total_out = 0;

702
703
704
    state->cds_p = state->cds_buf;
    *state->cds_p = 0;
    state->bit_p = 8;
705
706
    state->mode = m_get_block;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
707
    return AEC_OK;
708
709
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
710
int aec_encode(struct aec_stream *strm, int flush)
711
712
713
714
715
{
    /**
       Finite-state machine implementation of the adaptive entropy
       encoder.
    */
716
    int n;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
717
    struct internal_state *state;
718
719
720
721
722
    state = strm->state;
    state->flush = flush;

    while (state->mode(strm) == M_CONTINUE);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
723
    if (state->direct_out) {
724
725
726
727
728
729
730
731
        n = state->cds_p - strm->next_out;
        strm->next_out += n;
        strm->avail_out -= n;
        strm->total_out += n;

        *state->cds_buf = *state->cds_p;
        state->cds_p = state->cds_buf;
        state->direct_out = 0;
732
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
733
    return AEC_OK;
734
735
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
736
int aec_encode_end(struct aec_stream *strm)
737
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
738
    struct internal_state *state = strm->state;
739

740
741
    free(state->block_buf);
    free(state->cds_buf);
742
    free(state);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
743
    return AEC_OK;
744
}
745

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
746
int aec_buffer_encode(struct aec_stream *strm)
747
748
749
750
751
752
753
754
755
756
757
758
759
{
    int status;

    status = aec_encode_init(strm);
    if (status != AEC_OK)
        return status;
    status = aec_encode(strm, AEC_FLUSH);
    if (strm->avail_in > 0 || strm->avail_out == 0)
        status = AEC_DATA_ERROR;

    aec_encode_end(strm);
    return status;
}