Coverage Report

Created: 2025-06-15 00:57

/src/cmp_tool/lib/decompress/decmp.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * @file   decmp.c
3
 * @author Dominik Loidolt (dominik.loidolt@univie.ac.at)
4
 * @date   2020
5
 *
6
 * @copyright GPLv2
7
 * This program is free software; you can redistribute it and/or modify it
8
 * under the terms and conditions of the GNU General Public License,
9
 * version 2, as published by the Free Software Foundation.
10
 *
11
 * This program is distributed in the hope it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14
 * more details.
15
 *
16
 * @brief software decompression library
17
 * @see Data Compression User Manual PLATO-UVIE-PL-UM-0001
18
 *
19
 * To decompress a compression entity (consisting of a compression entity header
20
 * and the compressed data) use the decompress_cmp_entiy() function.
21
 *
22
 * @warning not intended for use with the flight software
23
 */
24
25
26
#include <stdint.h>
27
#include <limits.h>
28
#include <string.h>
29
#include <assert.h>
30
31
#include "../common/byteorder.h"
32
#include "../common/compiler.h"
33
34
#include "read_bitstream.h"
35
#include "../common/cmp_data_types.h"
36
#include "../decmp.h"
37
#include "../common/cmp_debug.h"
38
#include "../common/cmp_support.h"
39
#include "../common/cmp_entity.h"
40
#include "../common/cmp_cal_up_model.h"
41
#include "../common/cmp_max_used_bits.h"
42
43
44
136
#define CORRUPTION_DETECTED (-1)
45
46
47
MAYBE_UNUSED static const char *please_check_str =
48
  "Please check that the compression parameters match those used to compress the data and that the compressed data are not corrupted.";
49
50
51
/**
52
 * @brief function pointer to a code word decoder function
53
 */
54
55
typedef uint32_t(*decoder_ptr)(struct bit_decoder *, uint32_t, uint32_t);
56
57
58
/**
59
 * @brief structure to hold all parameters to decode a value
60
 */
61
62
struct decoder_setup {
63
  int (*decode_method_f)(const struct decoder_setup *setup,
64
             uint32_t *decoded_value); /* pointer to the decoding function with escape mechanism */
65
  decoder_ptr decode_cw_f; /* pointer to the code word decoder function (Golomb/Rice/unary) */
66
  struct bit_decoder *dec; /* pointer to a bit_decoder context */
67
  uint32_t encoder_par1;   /* encoding parameter 1 */
68
  uint32_t encoder_par2;   /* encoding parameter 2 */
69
  uint32_t outlier_par;    /* outlier parameter */
70
  uint32_t lossy_par;      /* lossy compression parameter */
71
  uint32_t max_data_bits;  /* bit length of the decoded value */
72
};
73
74
75
enum decmp_type {ICU_DECOMRESSION, RDCU_DECOMPRESSION};
76
77
78
/**
79
 * @brief decode the next unary code word in the bitstream
80
 *
81
 * @param dec   a pointer to a bit_decoder context
82
 * @param m   this parameter is not used
83
 * @param log2_m  this parameter is not used
84
 * @note: Can be used to decode a code word with compression parameter m = 1 (log2_m = 0)
85
 *
86
 * @returns the decoded value
87
 */
88
89
static __inline uint32_t unary_decoder(struct bit_decoder *dec, uint32_t m UNUSED,
90
               uint32_t log2_m UNUSED)
91
96.1k
{
92
96.1k
  uint32_t const decoded_cw = bit_peek_leading_ones(dec); /* decode unary coding */
93
96.1k
  uint32_t const cw_len = decoded_cw + 1; /* Number of 1's + following 0 */
94
95
96.1k
  bit_consume_bits(dec, cw_len);
96
97
96.1k
  return decoded_cw;
98
96.1k
}
99
100
101
/**
102
 * @brief decode the next Rice code word in the bitstream
103
 *
104
 * @param dec   a pointer to a bit_decoder context
105
 * @param m   Golomb parameter, must be the same used for encoding
106
 * @param log2_m  Rice parameter, is ilog_2(m), must be larger than 0
107
 * @note the Golomb parameter (m) must be a power of 2
108
 * @warning the Rice parameter (log2_m) must be greater than 0! If you want to
109
 *  use a Rice parameter equal to 0, use the unary_decoder instead.
110
 *
111
 * @returns the decoded value
112
 */
113
114
static uint32_t rice_decoder(struct bit_decoder *dec, uint32_t m, uint32_t log2_m)
115
21.8k
{
116
21.8k
  uint32_t q;  /* quotient */
117
21.8k
  uint32_t r;  /* remainder */
118
119
21.8k
  assert(log2_m > 0 && log2_m < 32);
120
121
21.8k
  q = unary_decoder(dec, m, log2_m); /* decode quotient unary code part */
122
21.8k
  r = bit_read_bits32(dec, log2_m); /* get remainder */
123
124
21.8k
  return (q << log2_m) + r; /* calculate decoded value (q*m+r) */
125
21.8k
}
126
127
128
/**
129
 * @brief decode the next Golomb code word in the bitstream
130
 *
131
 * @param dec   a pointer to a bit_decoder context
132
 * @param m   Golomb parameter (has to be bigger than 0)
133
 * @param log2_m  is ilog_2(m) calculate outside function for better
134
 *      performance
135
 *
136
 * @returns the decoded value
137
 */
138
139
static uint32_t golomb_decoder(struct bit_decoder *dec, uint32_t m, uint32_t log2_m)
140
61.1k
{
141
61.1k
  uint32_t q;  /* quotient */
142
61.1k
  uint32_t r1; /* remainder case 1 */
143
61.1k
  uint32_t r2; /* remainder case 2 */
144
61.1k
  uint32_t r;  /* remainder */
145
61.1k
  uint32_t cutoff; /* cutoff between group 1 and 2 */
146
147
61.1k
  assert(m > 0);
148
61.1k
  assert(log2_m == ilog_2(m));
149
150
  /* decode quotient unary code part */
151
61.1k
  q = unary_decoder(dec, m, log2_m);
152
153
  /* get the remainder code for both cases */
154
61.1k
  r2 = (uint32_t)bit_peek_bits(dec, log2_m+1);
155
61.1k
  r1 = r2 >> 1;
156
157
  /* calculate cutoff between case 1 and 2 */
158
61.1k
  cutoff = (0x2U << log2_m) - m; /* = 2^(log2_m+1)-m */
159
160
61.1k
  if (r1 < cutoff) { /* remainder case 1: remainder length=log2_m */
161
48.7k
    bit_consume_bits(dec, log2_m);
162
48.7k
    r = r1;
163
48.7k
  } else { /* remainder case 2: remainder length = log2_m+1 */
164
12.4k
    bit_consume_bits(dec, log2_m+1);
165
12.4k
    r = r2 - cutoff;
166
12.4k
  }
167
168
61.1k
  return q*m + r;
169
61.1k
}
170
171
172
/**
173
 * @brief select the decoder based on the used Golomb parameter
174
 *
175
 * @param golomb_par  Golomb parameter, has to be bigger than 0
176
 *
177
 * @note if the Golomb parameter is a power of 2 we can use the faster Rice decoder
178
 * @note if the Golomb parameter is 1 we can use the even faster unary decoder
179
 *
180
 * @returns function pointer to the select code word decoder function
181
 */
182
183
static decoder_ptr select_decoder(uint32_t golomb_par)
184
23.4k
{
185
23.4k
  assert(golomb_par > 0);
186
187
23.4k
  if (golomb_par == 1)
188
2.78k
    return &unary_decoder;
189
190
20.6k
  if (is_a_pow_of_2(golomb_par))
191
4.96k
    return &rice_decoder;
192
15.7k
  else
193
15.7k
    return &golomb_decoder;
194
20.6k
}
195
196
197
/**
198
 * @brief decode the next code word with zero escape system mechanism from the bitstream
199
 *
200
 * @param setup   pointer to the decoder setup
201
 * @param decoded_value points to the location where the decoded value is stored
202
 *
203
 * @returns 0 on success; otherwise error
204
 */
205
206
static int decode_zero(const struct decoder_setup *setup, uint32_t *decoded_value)
207
78.3k
{
208
  /* Decode the next value in the bitstream with the Golomb/Rice/unary decoder */
209
78.3k
  *decoded_value = setup->decode_cw_f(setup->dec, setup->encoder_par1, setup->encoder_par2);
210
211
78.3k
  if (*decoded_value != 0) { /* no escape symbol detected */
212
47.4k
    if (*decoded_value >= setup->outlier_par) {
213
32
      debug_print("Error: Data consistency check failed. Non-outlier decoded value greater or equal than the outlier parameter. %s", please_check_str);
214
32
      return CORRUPTION_DETECTED;
215
32
    }
216
47.4k
    *decoded_value -= 1;
217
47.4k
  } else {
218
    /* the zero escape symbol mechanism was used; read unencoded value */
219
30.9k
    bit_refill(setup->dec);
220
30.9k
    *decoded_value = bit_read_bits32_sub_1(setup->dec, setup->max_data_bits);
221
222
30.9k
    if (*decoded_value < setup->outlier_par - 1) { /* -1 because we subtract -1 from the *decoded_value */
223
35
      if (bit_refill(setup->dec) != BIT_OVERFLOW)
224
24
        debug_print("Error: Data consistency check failed. Outlier small than the outlier parameter. %s", please_check_str);
225
35
      return CORRUPTION_DETECTED;
226
35
    }
227
30.9k
  }
228
78.2k
  return bit_refill(setup->dec) == BIT_OVERFLOW;
229
78.3k
}
230
231
232
/**
233
 * @brief decode the next code word with the multi escape mechanism from the bitstream
234
 *
235
 * @param setup   pointer to the decoder setup
236
 * @param decoded_value points to the location where the decoded value is stored
237
 *
238
 * @returns 0 on success; otherwise error
239
 */
240
241
static int decode_multi(const struct decoder_setup *setup, uint32_t *decoded_value)
242
17.8k
{
243
  /* Decode the next value in the bitstream with the Golomb/Rice/unary decoder */
244
17.8k
  *decoded_value = setup->decode_cw_f(setup->dec, setup->encoder_par1, setup->encoder_par2);
245
246
17.8k
  if (*decoded_value >= setup->outlier_par) { /* escape symbol mechanism detected */
247
3.03k
    uint32_t const unencoded_len = (*decoded_value - setup->outlier_par + 1) << 1;
248
249
3.03k
    if (unencoded_len > ((setup->max_data_bits+1) & -2U)) { /* round up max_data_bits to the nearest multiple of 2 */
250
55
      debug_print("Error: Data consistency check failed. Multi escape symbol higher than expected. %s", please_check_str);
251
55
      return CORRUPTION_DETECTED;
252
55
    }
253
254
    /* read unencoded value */
255
2.98k
    bit_refill(setup->dec);
256
2.98k
    *decoded_value = bit_read_bits32(setup->dec, unencoded_len);
257
258
2.98k
    if (*decoded_value >> (unencoded_len-2) == 0) { /* check if at least one bit of the two highest is set. */
259
124
      if (unencoded_len > 2) { /* Exception: if we code outlier_par as outlier, no set bit is expected */
260
8
        if (bit_refill(setup->dec) != BIT_OVERFLOW)
261
7
          debug_print("Error: Data consistency check failed. Unencoded data after multi escape symbol to small. %s", please_check_str);
262
8
        return CORRUPTION_DETECTED;
263
8
      }
264
124
    }
265
266
2.97k
    *decoded_value += setup->outlier_par;
267
268
2.97k
    if ((*decoded_value & BIT_MASK[setup->max_data_bits]) < setup->outlier_par) { /* check for overflow in addition */
269
6
      if (bit_refill(setup->dec) != BIT_OVERFLOW)
270
5
        debug_print("Error: Data consistency check failed. Outlier small than the outlier parameter. %s", please_check_str);
271
6
      return CORRUPTION_DETECTED;
272
6
    }
273
2.97k
  }
274
17.7k
  return bit_refill(setup->dec) == BIT_OVERFLOW;
275
17.8k
}
276
277
278
/**
279
 * @brief remap an unsigned value back to a signed value
280
 * @note this is the reverse function of map_to_pos()
281
 *
282
 * @param value_to_unmap  unsigned value to remap
283
 *
284
 * @returns the signed remapped value
285
 */
286
287
static __inline uint32_t re_map_to_pos(uint32_t value_to_unmap)
288
96.1k
{
289
96.1k
  if (value_to_unmap & 0x1) { /* if uneven */
290
    /* uint64_t to prevent overflow if value_to_unmap == 0xFFFFFFFF */
291
28.9k
    uint64_t const tmp64 = value_to_unmap;
292
293
28.9k
    return (uint32_t)(-((tmp64 + 1) / 2));
294
67.1k
  } else {
295
67.1k
    return value_to_unmap / 2;
296
67.1k
  }
297
96.1k
}
298
299
300
/**
301
 * @brief decompress the next code word in the bitstream and decorrelate it with
302
 *  the model
303
 *
304
 * @param setup   pointer to the decoder setup
305
 * @param decoded_value points to the location where the decoded value is stored
306
 * @param model   model of the decoded_value (0 if not used)
307
 *
308
 * @returns 0 on success; otherwise error
309
 */
310
311
static int decode_value(const struct decoder_setup *setup, uint32_t *decoded_value,
312
      uint32_t model)
313
96.1k
{
314
  /* decode the next value from the bitstream */
315
96.1k
  int const err = setup->decode_method_f(setup, decoded_value);
316
317
  /* map the unsigned decode value back to a signed value */
318
96.1k
  *decoded_value = re_map_to_pos(*decoded_value);
319
320
  /* decorrelate data the data with the model */
321
96.1k
  *decoded_value += round_fwd(model, setup->lossy_par);
322
323
  /* we mask only the used bits in case there is an overflow when adding the model */
324
96.1k
  *decoded_value &= BIT_MASK[setup->max_data_bits];
325
326
  /* inverse step of the lossy compression */
327
96.1k
  *decoded_value = round_inv(*decoded_value, setup->lossy_par);
328
329
96.1k
  return err;
330
96.1k
}
331
332
333
/**
334
 * @brief configure a decoder setup structure to have a setup to decode a value
335
 *
336
 * @param setup   pointer to the decoder setup
337
 * @param dec   pointer to a bit_decoder context
338
 * @param cmp_mode  compression mode
339
 * @param cmp_par compression parameter
340
 * @param spillover spillover_par parameter
341
 * @param lossy_par lossy compression parameter
342
 * @param max_data_bits how many bits are needed to represent the highest possible value
343
 */
344
345
static void configure_decoder_setup(struct decoder_setup *setup, struct bit_decoder *dec,
346
            enum cmp_mode cmp_mode, uint32_t cmp_par,
347
            uint32_t spillover, uint32_t lossy_par,
348
            uint32_t max_data_bits)
349
23.4k
{
350
23.4k
  assert(setup != NULL);
351
23.4k
  assert(dec != NULL);
352
23.4k
  assert(cmp_par != 0);
353
23.4k
  assert(max_data_bits > 0 && max_data_bits <= 32);
354
355
23.4k
  if (multi_escape_mech_is_used(cmp_mode))
356
4.59k
    setup->decode_method_f = &decode_multi;
357
18.8k
  else if (zero_escape_mech_is_used(cmp_mode))
358
18.8k
    setup->decode_method_f = &decode_zero;
359
0
  else {
360
0
    debug_print("Error: Compression mode not supported.");
361
0
    assert(0);
362
0
  }
363
23.4k
  setup->decode_cw_f = select_decoder(cmp_par);
364
23.4k
  setup->dec = dec;
365
23.4k
  setup->encoder_par1 = cmp_par; /* encoding parameter 1 */
366
23.4k
  setup->encoder_par2 = ilog_2(cmp_par); /* encoding parameter 2 */
367
23.4k
  setup->outlier_par = spillover; /* outlier parameter */
368
23.4k
  setup->lossy_par = lossy_par; /* lossy compression parameter */
369
23.4k
  setup->max_data_bits = max_data_bits; /* how many bits are needed to represent the highest possible value */
370
23.4k
}
371
372
373
/**
374
 * @brief return a pointer of the data of a collection
375
 *
376
 * @param col pointer to a collection header (can be NULL)
377
 *
378
 * @returns pointer to the collection data; NULL if col is NULL
379
 */
380
381
static void *get_collection_data(void *col)
382
11.5k
{
383
11.5k
  if (col)
384
10.1k
    col = (uint8_t *)col + COLLECTION_HDR_SIZE;
385
11.5k
  return col;
386
11.5k
}
387
388
389
/**
390
 * @brief return a pointer of the data of a collection
391
 *
392
 * @param col pointer to a collection header (can be NULL)
393
 *
394
 * @returns pointer to the collection data; NULL if col is NULL
395
 */
396
397
static const void *get_collection_data_const(const void *col)
398
7.05k
{
399
7.05k
  if (col)
400
4.23k
    col = (const uint8_t *)col + COLLECTION_HDR_SIZE;
401
7.05k
  return col;
402
7.05k
}
403
404
405
/**
406
 * @brief decompress imagette data
407
 *
408
 * @param cfg pointer to the compression configuration structure
409
 * @param dec a pointer to a bit_decoder context
410
 *
411
 * @returns 0 on success; otherwise error
412
 */
413
414
static int decompress_imagette(const struct cmp_cfg *cfg, struct bit_decoder *dec, enum decmp_type decmp_type)
415
936
{
416
936
  size_t i;
417
936
  int err;
418
936
  uint32_t decoded_value;
419
936
  uint32_t max_data_bits;
420
936
  struct decoder_setup setup;
421
936
  uint16_t *data_buf;
422
936
  const uint16_t *model_buf;
423
936
  uint16_t *up_model_buf;
424
936
  const uint16_t *next_model_p;
425
936
  uint16_t model;
426
427
936
  switch (decmp_type) {
428
112
  case RDCU_DECOMPRESSION: /* RDCU compresses the header like data */
429
112
    data_buf = cfg->dst;
430
112
    model_buf = cfg->model_buf;
431
112
    up_model_buf = cfg->updated_model_buf;
432
112
    break;
433
824
  case ICU_DECOMRESSION:
434
824
    data_buf = get_collection_data(cfg->dst);
435
824
    model_buf = get_collection_data_const(cfg->model_buf);
436
824
    up_model_buf = get_collection_data(cfg->updated_model_buf);
437
824
    break;
438
936
  }
439
440
936
  if (model_mode_is_used(cfg->cmp_mode)) {
441
432
    model =  get_unaligned(&model_buf[0]);
442
432
    next_model_p = &model_buf[1];
443
504
  } else {
444
504
    up_model_buf = NULL;
445
504
    memset(&model, 0, sizeof(model));
446
504
    next_model_p = data_buf;
447
504
  }
448
449
936
  switch (cfg->data_type) {
450
398
  case DATA_TYPE_IMAGETTE:
451
404
  case DATA_TYPE_IMAGETTE_ADAPTIVE:
452
404
    max_data_bits = MAX_USED_BITS.nc_imagette;
453
404
    break;
454
234
  case DATA_TYPE_SAT_IMAGETTE:
455
250
  case DATA_TYPE_SAT_IMAGETTE_ADAPTIVE:
456
250
    max_data_bits = MAX_USED_BITS.saturated_imagette;
457
250
    break;
458
0
  default:
459
281
  case DATA_TYPE_F_CAM_IMAGETTE:
460
282
  case DATA_TYPE_F_CAM_IMAGETTE_ADAPTIVE:
461
282
    max_data_bits = MAX_USED_BITS.fc_imagette;
462
282
    break;
463
936
  }
464
465
936
  configure_decoder_setup(&setup, dec, cfg->cmp_mode, cfg->cmp_par_imagette,
466
936
        cfg->spill_imagette, cfg->round, max_data_bits);
467
468
17.9k
  for (i = 0; ; i++) {
469
17.9k
    err = decode_value(&setup, &decoded_value, model);
470
17.9k
    if (err)
471
63
      break;
472
473
17.8k
    put_unaligned((uint16_t)decoded_value, &data_buf[i]);
474
475
17.8k
    if (up_model_buf) {
476
6.20k
      uint16_t up_model = cmp_up_model((uint16_t)decoded_value, model, cfg->model_value,
477
6.20k
                   setup.lossy_par);
478
6.20k
      put_unaligned(up_model, &up_model_buf[i]);
479
6.20k
    }
480
481
17.8k
    if (i >= cfg->samples-1)
482
873
      break;
483
484
16.9k
    model = get_unaligned(&next_model_p[i]);
485
16.9k
  }
486
936
  return err;
487
936
}
488
489
490
/**
491
 * @brief decompress short normal light flux (S_FX) data
492
 *
493
 * @param cfg pointer to the compression configuration structure
494
 * @param dec a pointer to a bit_decoder context
495
 *
496
 * @returns 0 on success; otherwise error
497
 */
498
499
static int decompress_s_fx(const struct cmp_cfg *cfg, struct bit_decoder *dec)
500
261
{
501
261
  size_t i;
502
261
  int err;
503
261
  uint32_t decoded_value;
504
261
  struct decoder_setup setup_exp_flags, setup_fx;
505
261
  struct s_fx *data_buf = get_collection_data(cfg->dst);
506
261
  const struct s_fx *model_buf = get_collection_data_const(cfg->model_buf);
507
261
  struct s_fx *up_model_buf;
508
261
  const struct s_fx *next_model_p;
509
261
  struct s_fx model;
510
511
261
  if (model_mode_is_used(cfg->cmp_mode)) {
512
122
    model = model_buf[0];
513
122
    next_model_p = &model_buf[1];
514
122
    up_model_buf = get_collection_data(cfg->updated_model_buf);
515
139
  } else {
516
139
    memset(&model, 0, sizeof(model));
517
139
    next_model_p = data_buf;
518
139
    up_model_buf = NULL;
519
139
  }
520
521
261
  configure_decoder_setup(&setup_exp_flags, dec, cfg->cmp_mode, cfg->cmp_par_exp_flags,
522
261
        cfg->spill_exp_flags, cfg->round, MAX_USED_BITS.s_exp_flags);
523
261
  configure_decoder_setup(&setup_fx, dec, cfg->cmp_mode, cfg->cmp_par_fx,
524
261
        cfg->spill_fx, cfg->round, MAX_USED_BITS.s_fx);
525
526
3.15k
  for (i = 0; ; i++) {
527
3.15k
    err = decode_value(&setup_exp_flags, &decoded_value, model.exp_flags);
528
3.15k
    if (err)
529
11
      break;
530
3.14k
    data_buf[i].exp_flags = (__typeof__(data_buf[i].exp_flags))decoded_value;
531
532
3.14k
    err = decode_value(&setup_fx, &decoded_value, model.fx);
533
3.14k
    if (err)
534
6
      break;
535
3.14k
    data_buf[i].fx = decoded_value;
536
537
3.14k
    if (up_model_buf) {
538
344
      up_model_buf[i].exp_flags = cmp_up_model(data_buf[i].exp_flags, model.exp_flags,
539
344
                 cfg->model_value, setup_exp_flags.lossy_par);
540
344
      up_model_buf[i].fx = cmp_up_model(data_buf[i].fx, model.fx,
541
344
                cfg->model_value, setup_fx.lossy_par);
542
344
    }
543
544
3.14k
    if (i >= cfg->samples-1)
545
244
      break;
546
547
2.89k
    model = next_model_p[i];
548
2.89k
  }
549
261
  return err;
550
261
}
551
552
553
/**
554
 * @brief decompress S_FX_EFX data
555
 *
556
 * @param cfg pointer to the compression configuration structure
557
 * @param dec a pointer to a bit_decoder context
558
 *
559
 * @returns 0 on success; otherwise error
560
 */
561
562
static int decompress_s_fx_efx(const struct cmp_cfg *cfg, struct bit_decoder *dec)
563
455
{
564
455
  size_t i;
565
455
  int err;
566
455
  uint32_t decoded_value;
567
455
  struct decoder_setup setup_exp_flags, setup_fx, setup_efx;
568
455
  struct s_fx_efx *data_buf = get_collection_data(cfg->dst);
569
455
  const struct s_fx_efx *model_buf = get_collection_data_const(cfg->model_buf);
570
455
  struct s_fx_efx *up_model_buf;
571
455
  const struct s_fx_efx *next_model_p;
572
455
  struct s_fx_efx model;
573
574
455
  if (model_mode_is_used(cfg->cmp_mode)) {
575
259
    up_model_buf = get_collection_data(cfg->updated_model_buf);
576
259
    model = model_buf[0];
577
259
    next_model_p = &model_buf[1];
578
259
  } else {
579
196
    up_model_buf = NULL;
580
196
    memset(&model, 0, sizeof(model));
581
196
    next_model_p = data_buf;
582
196
  }
583
584
455
  configure_decoder_setup(&setup_exp_flags, dec, cfg->cmp_mode, cfg->cmp_par_exp_flags,
585
455
        cfg->spill_exp_flags, cfg->round, MAX_USED_BITS.s_exp_flags);
586
455
  configure_decoder_setup(&setup_fx, dec, cfg->cmp_mode, cfg->cmp_par_fx,
587
455
        cfg->spill_fx, cfg->round, MAX_USED_BITS.s_fx);
588
455
  configure_decoder_setup(&setup_efx, dec, cfg->cmp_mode, cfg->cmp_par_efx,
589
455
        cfg->spill_efx, cfg->round, MAX_USED_BITS.s_efx);
590
591
2.39k
  for (i = 0; ; i++) {
592
2.39k
    err = decode_value(&setup_exp_flags, &decoded_value, model.exp_flags);
593
2.39k
    if (err)
594
6
      break;
595
2.38k
    data_buf[i].exp_flags = (__typeof__(data_buf[i].exp_flags)) decoded_value;
596
597
2.38k
    err = decode_value(&setup_fx, &decoded_value, model.fx);
598
2.38k
    if (err)
599
5
      break;
600
2.38k
    data_buf[i].fx = decoded_value;
601
602
2.38k
    err = decode_value(&setup_efx, &decoded_value, model.efx);
603
2.38k
    if (err)
604
4
      break;
605
2.38k
    data_buf[i].efx = decoded_value;
606
607
2.38k
    if (up_model_buf) {
608
512
      up_model_buf[i].exp_flags = cmp_up_model(data_buf[i].exp_flags, model.exp_flags,
609
512
                 cfg->model_value, setup_exp_flags.lossy_par);
610
512
      up_model_buf[i].fx = cmp_up_model(data_buf[i].fx, model.fx,
611
512
                cfg->model_value, setup_fx.lossy_par);
612
512
      up_model_buf[i].efx = cmp_up_model(data_buf[i].efx, model.efx,
613
512
                 cfg->model_value, setup_efx.lossy_par);
614
512
    }
615
616
2.38k
    if (i >= cfg->samples-1)
617
440
      break;
618
619
1.94k
    model = next_model_p[i];
620
1.94k
  }
621
455
  return err;
622
455
}
623
624
625
/**
626
 * @brief decompress short S_FX_NCOB data
627
 *
628
 * @param cfg pointer to the compression configuration structure
629
 * @param dec a pointer to a bit_decoder context
630
 *
631
 * @returns 0 on success; otherwise error
632
 */
633
634
static int decompress_s_fx_ncob(const struct cmp_cfg *cfg, struct bit_decoder *dec)
635
283
{
636
283
  size_t i;
637
283
  int err;
638
283
  uint32_t decoded_value;
639
283
  struct decoder_setup setup_exp_flags, setup_fx, setup_ncob;
640
283
  struct s_fx_ncob *data_buf = get_collection_data(cfg->dst);
641
283
  const struct s_fx_ncob *model_buf = get_collection_data_const(cfg->model_buf);
642
283
  struct s_fx_ncob *up_model_buf;
643
283
  const struct s_fx_ncob *next_model_p;
644
283
  struct s_fx_ncob model;
645
646
283
  if (model_mode_is_used(cfg->cmp_mode)) {
647
116
    up_model_buf = get_collection_data(cfg->updated_model_buf);
648
116
    model = model_buf[0];
649
116
    next_model_p = &model_buf[1];
650
167
  } else {
651
167
    up_model_buf = NULL;
652
167
    memset(&model, 0, sizeof(model));
653
167
    next_model_p = data_buf;
654
167
  }
655
656
283
  configure_decoder_setup(&setup_exp_flags, dec, cfg->cmp_mode, cfg->cmp_par_exp_flags,
657
283
        cfg->spill_exp_flags, cfg->round, MAX_USED_BITS.s_exp_flags);
658
283
  configure_decoder_setup(&setup_fx, dec, cfg->cmp_mode, cfg->cmp_par_fx,
659
283
        cfg->spill_fx, cfg->round, MAX_USED_BITS.s_fx);
660
283
  configure_decoder_setup(&setup_ncob, dec, cfg->cmp_mode, cfg->cmp_par_ncob,
661
283
        cfg->spill_ncob, cfg->round, MAX_USED_BITS.s_ncob);
662
663
1.87k
  for (i = 0; ; i++) {
664
1.87k
    err = decode_value(&setup_exp_flags, &decoded_value, model.exp_flags);
665
1.87k
    if (err)
666
10
      break;
667
1.86k
    data_buf[i].exp_flags = (__typeof__(data_buf[i].exp_flags)) decoded_value;
668
669
1.86k
    err = decode_value(&setup_fx, &decoded_value, model.fx);
670
1.86k
    if (err)
671
4
      break;
672
1.85k
    data_buf[i].fx = decoded_value;
673
674
1.85k
    err = decode_value(&setup_ncob, &decoded_value, model.ncob_x);
675
1.85k
    if (err)
676
3
      break;
677
1.85k
    data_buf[i].ncob_x = decoded_value;
678
679
1.85k
    err = decode_value(&setup_ncob, &decoded_value, model.ncob_y);
680
1.85k
    if (err)
681
1
      break;
682
1.85k
    data_buf[i].ncob_y = decoded_value;
683
684
1.85k
    if (up_model_buf) {
685
261
      up_model_buf[i].exp_flags = cmp_up_model(data_buf[i].exp_flags, model.exp_flags,
686
261
                 cfg->model_value, setup_exp_flags.lossy_par);
687
261
      up_model_buf[i].fx = cmp_up_model(data_buf[i].fx, model.fx,
688
261
                cfg->model_value, setup_fx.lossy_par);
689
261
      up_model_buf[i].ncob_x = cmp_up_model(data_buf[i].ncob_x, model.ncob_x,
690
261
                    cfg->model_value, setup_ncob.lossy_par);
691
261
      up_model_buf[i].ncob_y = cmp_up_model(data_buf[i].ncob_y, model.ncob_y,
692
261
                    cfg->model_value, setup_ncob.lossy_par);
693
261
    }
694
695
1.85k
    if (i >= cfg->samples-1)
696
265
      break;
697
698
1.59k
    model = next_model_p[i];
699
1.59k
  }
700
283
  return err;
701
283
}
702
703
704
/**
705
 * @brief decompress short S_FX_NCOB_ECOB data
706
 *
707
 * @param cfg pointer to the compression configuration structure
708
 * @param dec a pointer to a bit_decoder context
709
 *
710
 * @returns 0 on success; otherwise error
711
 */
712
713
static int decompress_s_fx_efx_ncob_ecob(const struct cmp_cfg *cfg, struct bit_decoder *dec)
714
277
{
715
277
  size_t i;
716
277
  int err;
717
277
  uint32_t decoded_value;
718
277
  struct decoder_setup setup_exp_flags, setup_fx, setup_ncob, setup_efx, setup_ecob;
719
277
  struct s_fx_efx_ncob_ecob *data_buf = get_collection_data(cfg->dst);
720
277
  const struct s_fx_efx_ncob_ecob *model_buf = get_collection_data_const(cfg->model_buf);
721
277
  struct s_fx_efx_ncob_ecob *up_model_buf;
722
277
  const struct s_fx_efx_ncob_ecob *next_model_p;
723
277
  struct s_fx_efx_ncob_ecob model;
724
725
277
  if (model_mode_is_used(cfg->cmp_mode)) {
726
237
    up_model_buf = get_collection_data(cfg->updated_model_buf);
727
237
    model = model_buf[0];
728
237
    next_model_p = &model_buf[1];
729
237
  } else {
730
40
    up_model_buf = NULL;
731
40
    memset(&model, 0, sizeof(model));
732
40
    next_model_p = data_buf;
733
40
  }
734
735
277
  configure_decoder_setup(&setup_exp_flags, dec, cfg->cmp_mode, cfg->cmp_par_exp_flags,
736
277
        cfg->spill_exp_flags, cfg->round, MAX_USED_BITS.s_exp_flags);
737
277
  configure_decoder_setup(&setup_fx, dec, cfg->cmp_mode, cfg->cmp_par_fx, cfg->spill_fx,
738
277
        cfg->round, MAX_USED_BITS.s_fx);
739
277
  configure_decoder_setup(&setup_ncob, dec, cfg->cmp_mode, cfg->cmp_par_ncob, cfg->spill_ncob,
740
277
        cfg->round, MAX_USED_BITS.s_ncob);
741
277
  configure_decoder_setup(&setup_efx, dec, cfg->cmp_mode, cfg->cmp_par_efx, cfg->spill_efx,
742
277
        cfg->round, MAX_USED_BITS.s_efx);
743
277
  configure_decoder_setup(&setup_ecob, dec, cfg->cmp_mode, cfg->cmp_par_ecob, cfg->spill_ecob,
744
277
        cfg->round, MAX_USED_BITS.s_ecob);
745
746
588
  for (i = 0; ; i++) {
747
588
    err = decode_value(&setup_exp_flags, &decoded_value, model.exp_flags);
748
588
    if (err)
749
2
      break;
750
586
    data_buf[i].exp_flags = (__typeof__(data_buf[i].exp_flags)) decoded_value;
751
752
586
    err = decode_value(&setup_fx, &decoded_value, model.fx);
753
586
    if (err)
754
1
      break;
755
585
    data_buf[i].fx = decoded_value;
756
757
585
    err = decode_value(&setup_ncob, &decoded_value, model.ncob_x);
758
585
    if (err)
759
1
      break;
760
584
    data_buf[i].ncob_x = decoded_value;
761
762
584
    err = decode_value(&setup_ncob, &decoded_value, model.ncob_y);
763
584
    if (err)
764
3
      break;
765
581
    data_buf[i].ncob_y = decoded_value;
766
767
581
    err = decode_value(&setup_efx, &decoded_value, model.efx);
768
581
    if (err)
769
5
      break;
770
576
    data_buf[i].efx = decoded_value;
771
772
576
    err = decode_value(&setup_ecob, &decoded_value, model.ecob_x);
773
576
    if (err)
774
5
      break;
775
571
    data_buf[i].ecob_x = decoded_value;
776
777
571
    err = decode_value(&setup_ecob, &decoded_value, model.ecob_y);
778
571
    if (err)
779
1
      break;
780
570
    data_buf[i].ecob_y = decoded_value;
781
782
570
    if (up_model_buf) {
783
276
      up_model_buf[i].exp_flags = cmp_up_model(data_buf[i].exp_flags, model.exp_flags,
784
276
                 cfg->model_value, setup_exp_flags.lossy_par);
785
276
      up_model_buf[i].fx = cmp_up_model(data_buf[i].fx, model.fx,
786
276
                cfg->model_value, setup_fx.lossy_par);
787
276
      up_model_buf[i].ncob_x = cmp_up_model(data_buf[i].ncob_x, model.ncob_x,
788
276
                    cfg->model_value, setup_ncob.lossy_par);
789
276
      up_model_buf[i].ncob_y = cmp_up_model(data_buf[i].ncob_y, model.ncob_y,
790
276
                    cfg->model_value, setup_ncob.lossy_par);
791
276
      up_model_buf[i].efx = cmp_up_model(data_buf[i].efx, model.efx,
792
276
                 cfg->model_value, setup_efx.lossy_par);
793
276
      up_model_buf[i].ecob_x = cmp_up_model(data_buf[i].ecob_x, model.ecob_x,
794
276
                    cfg->model_value, setup_ecob.lossy_par);
795
276
      up_model_buf[i].ecob_y = cmp_up_model(data_buf[i].ecob_y, model.ecob_y,
796
276
                    cfg->model_value, setup_ecob.lossy_par);
797
276
    }
798
799
570
    if (i >= cfg->samples-1)
800
259
      break;
801
802
311
    model = next_model_p[i];
803
311
  }
804
277
  return err;
805
277
}
806
807
808
/**
809
 * @brief decompress long normal light flux (L_FX) data
810
 *
811
 * @param cfg pointer to the compression configuration structure
812
 * @param dec a pointer to a bit_decoder context
813
 *
814
 * @returns 0 on success; otherwise error
815
 */
816
817
static int decompress_l_fx(const struct cmp_cfg *cfg, struct bit_decoder *dec)
818
1.69k
{
819
1.69k
  size_t i;
820
1.69k
  int err;
821
1.69k
  uint32_t decoded_value;
822
1.69k
  struct decoder_setup setup_exp_flags, setup_fx, setup_fx_var;
823
1.69k
  struct l_fx *data_buf = get_collection_data(cfg->dst);
824
1.69k
  const struct l_fx *model_buf = get_collection_data_const(cfg->model_buf);
825
1.69k
  struct l_fx *up_model_buf;
826
1.69k
  const struct l_fx *next_model_p;
827
1.69k
  struct l_fx model;
828
829
1.69k
  if (model_mode_is_used(cfg->cmp_mode)) {
830
1.31k
    up_model_buf = get_collection_data(cfg->updated_model_buf);
831
1.31k
    model = model_buf[0];
832
1.31k
    next_model_p = &model_buf[1];
833
1.31k
  } else {
834
380
    up_model_buf = NULL;
835
380
    memset(&model, 0, sizeof(model));
836
380
    next_model_p = data_buf;
837
380
  }
838
839
1.69k
  configure_decoder_setup(&setup_exp_flags, dec, cfg->cmp_mode, cfg->cmp_par_exp_flags, cfg->spill_exp_flags,
840
1.69k
        cfg->round, MAX_USED_BITS.l_exp_flags);
841
1.69k
  configure_decoder_setup(&setup_fx, dec, cfg->cmp_mode, cfg->cmp_par_fx, cfg->spill_fx,
842
1.69k
        cfg->round, MAX_USED_BITS.l_fx);
843
1.69k
  configure_decoder_setup(&setup_fx_var, dec, cfg->cmp_mode, cfg->cmp_par_fx_cob_variance, cfg->spill_fx_cob_variance,
844
1.69k
        cfg->round, MAX_USED_BITS.l_fx_cob_variance);
845
846
2.55k
  for (i = 0; ; i++) {
847
2.55k
    err = decode_value(&setup_exp_flags, &decoded_value, model.exp_flags);
848
2.55k
    if (err)
849
4
      break;
850
2.54k
    data_buf[i].exp_flags = decoded_value;
851
852
2.54k
    err = decode_value(&setup_fx, &decoded_value, model.fx);
853
2.54k
    if (err)
854
3
      break;
855
2.54k
    data_buf[i].fx = decoded_value;
856
857
2.54k
    err = decode_value(&setup_fx_var, &decoded_value, model.fx_variance);
858
2.54k
    if (err)
859
6
      break;
860
2.53k
    data_buf[i].fx_variance = decoded_value;
861
862
2.53k
    if (up_model_buf) {
863
1.40k
      up_model_buf[i].exp_flags = cmp_up_model32(data_buf[i].exp_flags, model.exp_flags,
864
1.40k
                   cfg->model_value, setup_exp_flags.lossy_par);
865
1.40k
      up_model_buf[i].fx = cmp_up_model(data_buf[i].fx, model.fx,
866
1.40k
                cfg->model_value, setup_fx.lossy_par);
867
1.40k
      up_model_buf[i].fx_variance = cmp_up_model(data_buf[i].fx_variance, model.fx_variance,
868
1.40k
                   cfg->model_value, setup_fx_var.lossy_par);
869
1.40k
    }
870
871
2.53k
    if (i >= cfg->samples-1)
872
1.68k
      break;
873
874
855
    model = next_model_p[i];
875
855
  }
876
1.69k
  return err;
877
1.69k
}
878
879
880
/**
881
 * @brief decompress L_FX_EFX data
882
 *
883
 * @param cfg pointer to the compression configuration structure
884
 * @param dec a pointer to a bit_decoder context
885
 *
886
 * @returns 0 on success; otherwise error
887
 */
888
889
static int decompress_l_fx_efx(const struct cmp_cfg *cfg, struct bit_decoder *dec)
890
391
{
891
391
  size_t i;
892
391
  int err;
893
391
  uint32_t decoded_value;
894
391
  struct decoder_setup setup_exp_flags, setup_fx, setup_efx, setup_fx_var;
895
391
  struct l_fx_efx *data_buf = get_collection_data(cfg->dst);
896
391
  const struct l_fx_efx *model_buf = get_collection_data_const(cfg->model_buf);
897
391
  struct l_fx_efx *up_model_buf;
898
391
  const struct l_fx_efx *next_model_p;
899
391
  struct l_fx_efx model;
900
901
391
  if (model_mode_is_used(cfg->cmp_mode)) {
902
284
    up_model_buf = get_collection_data(cfg->updated_model_buf);
903
284
    model = model_buf[0];
904
284
    next_model_p = &model_buf[1];
905
284
  } else {
906
107
    up_model_buf = NULL;
907
107
    memset(&model, 0, sizeof(model));
908
107
    next_model_p = data_buf;
909
107
  }
910
911
391
  configure_decoder_setup(&setup_exp_flags, dec, cfg->cmp_mode, cfg->cmp_par_exp_flags, cfg->spill_exp_flags,
912
391
        cfg->round, MAX_USED_BITS.l_exp_flags);
913
391
  configure_decoder_setup(&setup_fx, dec, cfg->cmp_mode, cfg->cmp_par_fx, cfg->spill_fx,
914
391
        cfg->round, MAX_USED_BITS.l_fx);
915
391
  configure_decoder_setup(&setup_efx, dec, cfg->cmp_mode, cfg->cmp_par_efx, cfg->spill_efx,
916
391
        cfg->round, MAX_USED_BITS.l_efx);
917
391
  configure_decoder_setup(&setup_fx_var, dec, cfg->cmp_mode, cfg->cmp_par_fx_cob_variance, cfg->spill_fx_cob_variance,
918
391
        cfg->round, MAX_USED_BITS.l_fx_cob_variance);
919
920
1.30k
  for (i = 0; ; i++) {
921
1.30k
    err = decode_value(&setup_exp_flags, &decoded_value, model.exp_flags);
922
1.30k
    if (err)
923
6
      break;
924
1.29k
    data_buf[i].exp_flags = decoded_value;
925
926
1.29k
    err = decode_value(&setup_fx, &decoded_value, model.fx);
927
1.29k
    if (err)
928
2
      break;
929
1.29k
    data_buf[i].fx = decoded_value;
930
931
1.29k
    err = decode_value(&setup_efx, &decoded_value, model.efx);
932
1.29k
    if (err)
933
5
      break;
934
1.29k
    data_buf[i].efx = decoded_value;
935
936
1.29k
    err = decode_value(&setup_fx_var, &decoded_value, model.fx_variance);
937
1.29k
    if (err)
938
5
      break;
939
1.28k
    data_buf[i].fx_variance = decoded_value;
940
941
1.28k
    if (up_model_buf) {
942
344
      up_model_buf[i].exp_flags = cmp_up_model32(data_buf[i].exp_flags, model.exp_flags,
943
344
                   cfg->model_value, setup_exp_flags.lossy_par);
944
344
      up_model_buf[i].fx = cmp_up_model(data_buf[i].fx, model.fx,
945
344
                cfg->model_value, setup_fx.lossy_par);
946
344
      up_model_buf[i].efx = cmp_up_model(data_buf[i].efx, model.efx,
947
344
                 cfg->model_value, setup_efx.lossy_par);
948
344
      up_model_buf[i].fx_variance = cmp_up_model(data_buf[i].fx_variance, model.fx_variance,
949
344
                   cfg->model_value, setup_fx_var.lossy_par);
950
344
    }
951
952
1.28k
    if (i >= cfg->samples-1)
953
373
      break;
954
955
913
    model = next_model_p[i];
956
913
  }
957
391
  return err;
958
391
}
959
960
961
/**
962
 * @brief decompress L_FX_NCOB data
963
 *
964
 * @param cfg pointer to the compression configuration structure
965
 * @param dec a pointer to a bit_decoder context
966
 *
967
 * @returns 0 on success; otherwise error
968
 */
969
970
static int decompress_l_fx_ncob(const struct cmp_cfg *cfg, struct bit_decoder *dec)
971
296
{
972
296
  size_t i;
973
296
  int err;
974
296
  uint32_t decoded_value;
975
296
  struct decoder_setup setup_exp_flags, setup_fx, setup_ncob,
976
296
           setup_fx_var, setup_cob_var;
977
296
  struct l_fx_ncob *data_buf = get_collection_data(cfg->dst);
978
296
  const struct l_fx_ncob *model_buf = get_collection_data_const(cfg->model_buf);
979
296
  struct l_fx_ncob *up_model_buf;
980
296
  const struct l_fx_ncob *next_model_p;
981
296
  struct l_fx_ncob model;
982
983
296
  if (model_mode_is_used(cfg->cmp_mode)) {
984
73
    up_model_buf = get_collection_data(cfg->updated_model_buf);
985
73
    model = model_buf[0];
986
73
    next_model_p = &model_buf[1];
987
223
  } else {
988
223
    up_model_buf = NULL;
989
223
    memset(&model, 0, sizeof(model));
990
223
    next_model_p = data_buf;
991
223
  }
992
993
296
  configure_decoder_setup(&setup_exp_flags, dec, cfg->cmp_mode, cfg->cmp_par_exp_flags, cfg->spill_exp_flags,
994
296
        cfg->round, MAX_USED_BITS.l_exp_flags);
995
296
  configure_decoder_setup(&setup_fx, dec, cfg->cmp_mode, cfg->cmp_par_fx, cfg->spill_fx,
996
296
        cfg->round, MAX_USED_BITS.l_fx);
997
296
  configure_decoder_setup(&setup_ncob, dec, cfg->cmp_mode, cfg->cmp_par_ncob, cfg->spill_ncob,
998
296
        cfg->round, MAX_USED_BITS.l_ncob);
999
296
  configure_decoder_setup(&setup_fx_var, dec, cfg->cmp_mode, cfg->cmp_par_fx_cob_variance, cfg->spill_fx_cob_variance,
1000
296
        cfg->round, MAX_USED_BITS.l_fx_cob_variance);
1001
296
  configure_decoder_setup(&setup_cob_var, dec, cfg->cmp_mode, cfg->cmp_par_fx_cob_variance, cfg->spill_fx_cob_variance,
1002
296
        cfg->round, MAX_USED_BITS.l_fx_cob_variance);
1003
1004
1.73k
  for (i = 0; ; i++) {
1005
1.73k
    err = decode_value(&setup_exp_flags, &decoded_value, model.exp_flags);
1006
1.73k
    if (err)
1007
3
      break;
1008
1.73k
    data_buf[i].exp_flags = decoded_value;
1009
1010
1.73k
    err = decode_value(&setup_fx, &decoded_value, model.fx);
1011
1.73k
    if (err)
1012
1
      break;
1013
1.73k
    data_buf[i].fx = decoded_value;
1014
1015
1.73k
    err = decode_value(&setup_ncob, &decoded_value, model.ncob_x);
1016
1.73k
    if (err)
1017
1
      break;
1018
1.72k
    data_buf[i].ncob_x = decoded_value;
1019
1020
1.72k
    err = decode_value(&setup_ncob, &decoded_value, model.ncob_y);
1021
1.72k
    if (err)
1022
1
      break;
1023
1.72k
    data_buf[i].ncob_y = decoded_value;
1024
1025
1.72k
    err = decode_value(&setup_fx_var, &decoded_value, model.fx_variance);
1026
1.72k
    if (err)
1027
3
      break;
1028
1.72k
    data_buf[i].fx_variance = decoded_value;
1029
1030
1.72k
    err = decode_value(&setup_cob_var, &decoded_value, model.cob_x_variance);
1031
1.72k
    if (err)
1032
2
      break;
1033
1.72k
    data_buf[i].cob_x_variance = decoded_value;
1034
1035
1.72k
    err = decode_value(&setup_cob_var, &decoded_value, model.cob_y_variance);
1036
1.72k
    if (err)
1037
8
      break;
1038
1.71k
    data_buf[i].cob_y_variance = decoded_value;
1039
1040
1.71k
    if (up_model_buf) {
1041
194
      up_model_buf[i].exp_flags = cmp_up_model32(data_buf[i].exp_flags, model.exp_flags,
1042
194
        cfg->model_value, setup_exp_flags.lossy_par);
1043
194
      up_model_buf[i].fx = cmp_up_model(data_buf[i].fx, model.fx,
1044
194
        cfg->model_value, setup_fx.lossy_par);
1045
194
      up_model_buf[i].ncob_x = cmp_up_model(data_buf[i].ncob_x, model.ncob_x,
1046
194
        cfg->model_value, setup_ncob.lossy_par);
1047
194
      up_model_buf[i].ncob_y = cmp_up_model(data_buf[i].ncob_y, model.ncob_y,
1048
194
        cfg->model_value, setup_ncob.lossy_par);
1049
194
      up_model_buf[i].fx_variance = cmp_up_model(data_buf[i].fx_variance, model.fx_variance,
1050
194
        cfg->model_value, setup_fx_var.lossy_par);
1051
194
      up_model_buf[i].cob_x_variance = cmp_up_model(data_buf[i].cob_x_variance, model.cob_x_variance,
1052
194
        cfg->model_value, setup_cob_var.lossy_par);
1053
194
      up_model_buf[i].cob_y_variance = cmp_up_model(data_buf[i].cob_y_variance, model.cob_y_variance,
1054
194
        cfg->model_value, setup_cob_var.lossy_par);
1055
194
    }
1056
1057
1.71k
    if (i >= cfg->samples-1)
1058
277
      break;
1059
1060
1.43k
    model = next_model_p[i];
1061
1.43k
  }
1062
296
  return err;
1063
296
}
1064
1065
1066
/**
1067
 * @brief decompress L_FX_EFX_NCOB_ECOB data
1068
 *
1069
 * @param cfg pointer to the compression configuration structure
1070
 * @param dec a pointer to a bit_decoder context
1071
 *
1072
 * @returns 0 on success; otherwise error
1073
 */
1074
1075
static int decompress_l_fx_efx_ncob_ecob(const struct cmp_cfg *cfg, struct bit_decoder *dec)
1076
762
{
1077
762
  size_t i;
1078
762
  int err;
1079
762
  uint32_t decoded_value;
1080
762
  struct decoder_setup setup_exp_flags, setup_fx, setup_ncob, setup_efx,
1081
762
           setup_ecob, setup_fx_var, setup_cob_var;
1082
762
  struct l_fx_efx_ncob_ecob *data_buf = get_collection_data(cfg->dst);
1083
762
  const struct l_fx_efx_ncob_ecob *model_buf = get_collection_data_const(cfg->model_buf);
1084
762
  struct l_fx_efx_ncob_ecob *up_model_buf;
1085
762
  const struct l_fx_efx_ncob_ecob *next_model_p;
1086
762
  struct l_fx_efx_ncob_ecob model;
1087
1088
762
  if (model_mode_is_used(cfg->cmp_mode)) {
1089
224
    up_model_buf = get_collection_data(cfg->updated_model_buf);
1090
224
    model = model_buf[0];
1091
224
    next_model_p = &model_buf[1];
1092
538
  } else {
1093
538
    up_model_buf = NULL;
1094
538
    memset(&model, 0, sizeof(model));
1095
538
    next_model_p = data_buf;
1096
538
  }
1097
1098
762
  configure_decoder_setup(&setup_exp_flags, dec, cfg->cmp_mode, cfg->cmp_par_exp_flags, cfg->spill_exp_flags,
1099
762
        cfg->round, MAX_USED_BITS.l_exp_flags);
1100
762
  configure_decoder_setup(&setup_fx, dec, cfg->cmp_mode, cfg->cmp_par_fx, cfg->spill_fx,
1101
762
        cfg->round, MAX_USED_BITS.l_fx);
1102
762
  configure_decoder_setup(&setup_ncob, dec, cfg->cmp_mode, cfg->cmp_par_ncob, cfg->spill_ncob,
1103
762
        cfg->round, MAX_USED_BITS.l_ncob);
1104
762
  configure_decoder_setup(&setup_efx, dec, cfg->cmp_mode, cfg->cmp_par_efx, cfg->spill_efx,
1105
762
        cfg->round, MAX_USED_BITS.l_efx);
1106
762
  configure_decoder_setup(&setup_ecob, dec, cfg->cmp_mode, cfg->cmp_par_ecob, cfg->spill_ecob,
1107
762
        cfg->round, MAX_USED_BITS.l_ecob);
1108
762
  configure_decoder_setup(&setup_fx_var, dec, cfg->cmp_mode, cfg->cmp_par_fx_cob_variance, cfg->spill_fx_cob_variance,
1109
762
        cfg->round, MAX_USED_BITS.l_fx_cob_variance);
1110
762
  configure_decoder_setup(&setup_cob_var, dec, cfg->cmp_mode, cfg->cmp_par_fx_cob_variance, cfg->spill_fx_cob_variance,
1111
762
        cfg->round, MAX_USED_BITS.l_fx_cob_variance);
1112
1113
1.31k
  for (i = 0; ; i++) {
1114
1.31k
    err = decode_value(&setup_exp_flags, &decoded_value, model.exp_flags);
1115
1.31k
    if (err)
1116
2
      break;
1117
1.31k
    data_buf[i].exp_flags = decoded_value;
1118
1119
1.31k
    err = decode_value(&setup_fx, &decoded_value, model.fx);
1120
1.31k
    if (err)
1121
1
      break;
1122
1.31k
    data_buf[i].fx = decoded_value;
1123
1124
1.31k
    err = decode_value(&setup_ncob, &decoded_value, model.ncob_x);
1125
1.31k
    if (err)
1126
1
      break;
1127
1.31k
    data_buf[i].ncob_x = decoded_value;
1128
1129
1.31k
    err = decode_value(&setup_ncob, &decoded_value, model.ncob_y);
1130
1.31k
    if (err)
1131
1
      break;
1132
1.31k
    data_buf[i].ncob_y = decoded_value;
1133
1134
1.31k
    err = decode_value(&setup_efx, &decoded_value, model.efx);
1135
1.31k
    if (err)
1136
2
      break;
1137
1.31k
    data_buf[i].efx = decoded_value;
1138
1139
1.31k
    err = decode_value(&setup_ecob, &decoded_value, model.ecob_x);
1140
1.31k
    if (err)
1141
1
      break;
1142
1.30k
    data_buf[i].ecob_x = decoded_value;
1143
1144
1.30k
    err = decode_value(&setup_ecob, &decoded_value, model.ecob_y);
1145
1.30k
    if (err)
1146
1
      break;
1147
1.30k
    data_buf[i].ecob_y = decoded_value;
1148
1149
1.30k
    err = decode_value(&setup_fx_var, &decoded_value, model.fx_variance);
1150
1.30k
    if (err)
1151
1
      break;
1152
1.30k
    data_buf[i].fx_variance = decoded_value;
1153
1154
1.30k
    err = decode_value(&setup_cob_var, &decoded_value, model.cob_x_variance);
1155
1.30k
    if (err)
1156
1
      break;
1157
1.30k
    data_buf[i].cob_x_variance = decoded_value;
1158
1159
1.30k
    err = decode_value(&setup_cob_var, &decoded_value, model.cob_y_variance);
1160
1.30k
    if (err)
1161
6
      break;
1162
1.30k
    data_buf[i].cob_y_variance = decoded_value;
1163
1164
1.30k
    if (up_model_buf) {
1165
404
      up_model_buf[i].exp_flags = cmp_up_model32(data_buf[i].exp_flags, model.exp_flags,
1166
404
        cfg->model_value, setup_exp_flags.lossy_par);
1167
404
      up_model_buf[i].fx = cmp_up_model(data_buf[i].fx, model.fx,
1168
404
        cfg->model_value, setup_fx.lossy_par);
1169
404
      up_model_buf[i].ncob_x = cmp_up_model(data_buf[i].ncob_x, model.ncob_x,
1170
404
        cfg->model_value, setup_ncob.lossy_par);
1171
404
      up_model_buf[i].ncob_y = cmp_up_model(data_buf[i].ncob_y, model.ncob_y,
1172
404
        cfg->model_value, setup_ncob.lossy_par);
1173
404
      up_model_buf[i].efx = cmp_up_model(data_buf[i].efx, model.efx,
1174
404
        cfg->model_value, setup_efx.lossy_par);
1175
404
      up_model_buf[i].ecob_x = cmp_up_model(data_buf[i].ecob_x, model.ecob_x,
1176
404
        cfg->model_value, setup_ecob.lossy_par);
1177
404
      up_model_buf[i].ecob_y = cmp_up_model(data_buf[i].ecob_y, model.ecob_y,
1178
404
        cfg->model_value, setup_ecob.lossy_par);
1179
404
      up_model_buf[i].fx_variance = cmp_up_model(data_buf[i].fx_variance, model.fx_variance,
1180
404
        cfg->model_value, setup_fx_var.lossy_par);
1181
404
      up_model_buf[i].cob_x_variance = cmp_up_model(data_buf[i].cob_x_variance, model.cob_x_variance,
1182
404
        cfg->model_value, setup_cob_var.lossy_par);
1183
404
      up_model_buf[i].cob_y_variance = cmp_up_model(data_buf[i].cob_y_variance, model.cob_y_variance,
1184
404
        cfg->model_value, setup_cob_var.lossy_par);
1185
404
    }
1186
1187
1.30k
    if (i >= cfg->samples-1)
1188
745
      break;
1189
1190
555
    model = next_model_p[i];
1191
555
  }
1192
762
  return err;
1193
762
}
1194
1195
1196
/**
1197
 * @brief decompress N-CAM and F-CAM offset data
1198
 *
1199
 * @param cfg pointer to the compression configuration structure
1200
 * @param dec a pointer to a bit_decoder context
1201
 *
1202
 * @returns 0 on success; otherwise error
1203
 */
1204
1205
static int decompress_offset(const struct cmp_cfg *cfg, struct bit_decoder *dec)
1206
509
{
1207
509
  size_t i;
1208
509
  int err;
1209
509
  uint32_t decoded_value;
1210
509
  struct decoder_setup setup_mean, setup_var;
1211
509
  struct offset *data_buf = get_collection_data(cfg->dst);
1212
509
  const struct offset *model_buf = get_collection_data_const(cfg->model_buf);
1213
509
  struct offset *up_model_buf;
1214
509
  const struct offset *next_model_p;
1215
509
  struct offset model;
1216
1217
509
  if (model_mode_is_used(cfg->cmp_mode)) {
1218
266
    up_model_buf = get_collection_data(cfg->updated_model_buf);
1219
266
    model = model_buf[0];
1220
266
    next_model_p = &model_buf[1];
1221
266
  } else {
1222
243
    up_model_buf = NULL;
1223
243
    memset(&model, 0, sizeof(model));
1224
243
    next_model_p = data_buf;
1225
243
  }
1226
1227
509
  {
1228
509
    unsigned int mean_bits_used, variance_bits_used;
1229
1230
509
    switch (cfg->data_type) {
1231
252
    case DATA_TYPE_F_CAM_OFFSET:
1232
252
      mean_bits_used = MAX_USED_BITS.fc_offset_mean;
1233
252
      variance_bits_used = MAX_USED_BITS.fc_offset_variance;
1234
252
      break;
1235
257
    case DATA_TYPE_OFFSET:
1236
257
    default:
1237
257
      mean_bits_used = MAX_USED_BITS.nc_offset_mean;
1238
257
      variance_bits_used = MAX_USED_BITS.nc_offset_variance;
1239
257
      break;
1240
509
    }
1241
509
    configure_decoder_setup(&setup_mean, dec, cfg->cmp_mode, cfg->cmp_par_offset_mean, cfg->spill_offset_mean,
1242
509
          cfg->round, mean_bits_used);
1243
1244
509
    configure_decoder_setup(&setup_var, dec, cfg->cmp_mode, cfg->cmp_par_offset_variance, cfg->spill_offset_variance,
1245
509
          cfg->round, variance_bits_used);
1246
1247
509
  }
1248
1249
2.78k
  for (i = 0; ; i++) {
1250
2.78k
    err = decode_value(&setup_mean, &decoded_value, model.mean);
1251
2.78k
    if (err)
1252
8
      break;
1253
2.77k
    data_buf[i].mean = decoded_value;
1254
1255
2.77k
    err = decode_value(&setup_var, &decoded_value, model.variance);
1256
2.77k
    if (err)
1257
5
      break;
1258
2.76k
    data_buf[i].variance = decoded_value;
1259
1260
2.76k
    if (up_model_buf) {
1261
844
      up_model_buf[i].mean = cmp_up_model(data_buf[i].mean,
1262
844
        model.mean, cfg->model_value, setup_mean.lossy_par);
1263
844
      up_model_buf[i].variance = cmp_up_model(data_buf[i].variance,
1264
844
        model.variance, cfg->model_value, setup_var.lossy_par);
1265
844
    }
1266
1267
2.76k
    if (i >= cfg->samples-1)
1268
496
      break;
1269
1270
2.27k
    model = next_model_p[i];
1271
2.27k
  }
1272
509
  return err;
1273
509
}
1274
1275
1276
/**
1277
 * @brief decompress N-CAM background data
1278
 *
1279
 * @param cfg pointer to the compression configuration structure
1280
 * @param dec a pointer to a bit_decoder context
1281
 *
1282
 * @returns 0 on success; otherwise error
1283
 */
1284
1285
static int decompress_background(const struct cmp_cfg *cfg, struct bit_decoder *dec)
1286
922
{
1287
922
  size_t i;
1288
922
  int err;
1289
922
  uint32_t decoded_value;
1290
922
  struct decoder_setup setup_mean, setup_var, setup_pix;
1291
922
  struct background *data_buf = get_collection_data(cfg->dst);
1292
922
  const struct background *model_buf = get_collection_data_const(cfg->model_buf);
1293
922
  struct background *up_model_buf;
1294
922
  const struct background *next_model_p;
1295
922
  struct background model;
1296
1297
922
  if (model_mode_is_used(cfg->cmp_mode)) {
1298
402
    up_model_buf = get_collection_data(cfg->updated_model_buf);
1299
402
    model = model_buf[0];
1300
402
    next_model_p = &model_buf[1];
1301
520
  } else {
1302
520
    up_model_buf = NULL;
1303
520
    memset(&model, 0, sizeof(model));
1304
520
    next_model_p = data_buf;
1305
520
  }
1306
922
  {
1307
922
    unsigned int mean_used_bits, variance_used_bits, outlier_pixels_used_bits;
1308
1309
922
    switch (cfg->data_type) {
1310
835
    case DATA_TYPE_F_CAM_BACKGROUND:
1311
835
      mean_used_bits = MAX_USED_BITS.fc_background_mean;
1312
835
      variance_used_bits = MAX_USED_BITS.fc_background_variance;
1313
835
      outlier_pixels_used_bits = MAX_USED_BITS.fc_background_outlier_pixels;
1314
835
      break;
1315
87
    case DATA_TYPE_BACKGROUND:
1316
87
    default:
1317
87
      mean_used_bits = MAX_USED_BITS.nc_background_mean;
1318
87
      variance_used_bits = MAX_USED_BITS.nc_background_variance;
1319
87
      outlier_pixels_used_bits = MAX_USED_BITS.nc_background_outlier_pixels;
1320
87
      break;
1321
922
    }
1322
1323
922
    configure_decoder_setup(&setup_mean, dec, cfg->cmp_mode, cfg->cmp_par_background_mean, cfg->spill_background_mean,
1324
922
          cfg->round, mean_used_bits);
1325
1326
922
    configure_decoder_setup(&setup_var, dec, cfg->cmp_mode, cfg->cmp_par_background_variance, cfg->spill_background_variance,
1327
922
          cfg->round, variance_used_bits);
1328
1329
922
    configure_decoder_setup(&setup_pix, dec, cfg->cmp_mode, cfg->cmp_par_background_pixels_error, cfg->spill_background_pixels_error,
1330
922
          cfg->round, outlier_pixels_used_bits);
1331
1332
922
  }
1333
1334
1.76k
  for (i = 0; ; i++) {
1335
1.76k
    err = decode_value(&setup_mean, &decoded_value, model.mean);
1336
1.76k
    if (err)
1337
16
      break;
1338
1.74k
    data_buf[i].mean = decoded_value;
1339
1340
1.74k
    err = decode_value(&setup_var, &decoded_value, model.variance);
1341
1.74k
    if (err)
1342
5
      break;
1343
1.73k
    data_buf[i].variance = decoded_value;
1344
1345
1.73k
    err = decode_value(&setup_pix, &decoded_value, model.outlier_pixels);
1346
1.73k
    if (err)
1347
3
      break;
1348
1.73k
    data_buf[i].outlier_pixels = (__typeof__(data_buf[i].outlier_pixels))decoded_value;
1349
1350
1.73k
    if (up_model_buf) {
1351
437
      up_model_buf[i].mean = cmp_up_model(data_buf[i].mean,
1352
437
        model.mean, cfg->model_value, setup_mean.lossy_par);
1353
437
      up_model_buf[i].variance = cmp_up_model(data_buf[i].variance,
1354
437
        model.variance, cfg->model_value, setup_var.lossy_par);
1355
437
      up_model_buf[i].outlier_pixels = cmp_up_model(data_buf[i].outlier_pixels,
1356
437
        model.outlier_pixels, cfg->model_value, setup_pix.lossy_par);
1357
437
    }
1358
1359
1.73k
    if (i >= cfg->samples-1)
1360
898
      break;
1361
1362
838
    model = next_model_p[i];
1363
838
  }
1364
922
  return err;
1365
922
}
1366
1367
1368
/**
1369
 * @brief decompress N-CAM smearing data
1370
 *
1371
 * @param cfg pointer to the compression configuration structure
1372
 * @param dec a pointer to a bit_decoder context
1373
 *
1374
 * @returns 0 on success; otherwise error
1375
 */
1376
1377
static int decompress_smearing(const struct cmp_cfg *cfg, struct bit_decoder *dec)
1378
382
{
1379
382
  size_t i;
1380
382
  int err;
1381
382
  uint32_t decoded_value;
1382
382
  struct decoder_setup setup_mean, setup_var, setup_pix;
1383
382
  struct smearing *data_buf = get_collection_data(cfg->dst);
1384
382
  const struct smearing *model_buf = get_collection_data_const(cfg->model_buf);
1385
382
  struct smearing *up_model_buf;
1386
382
  const struct smearing *next_model_p;
1387
382
  struct smearing model;
1388
1389
382
  if (model_mode_is_used(cfg->cmp_mode)) {
1390
324
    up_model_buf = get_collection_data(cfg->updated_model_buf);
1391
324
    model = model_buf[0];
1392
324
    next_model_p = &model_buf[1];
1393
324
  } else {
1394
58
    up_model_buf = NULL;
1395
58
    memset(&model, 0, sizeof(model));
1396
58
    next_model_p = data_buf;
1397
58
  }
1398
1399
382
  configure_decoder_setup(&setup_mean, dec, cfg->cmp_mode, cfg->cmp_par_smearing_mean, cfg->spill_smearing_mean,
1400
382
        cfg->round, MAX_USED_BITS.smearing_mean);
1401
382
  configure_decoder_setup(&setup_var, dec, cfg->cmp_mode, cfg->cmp_par_smearing_variance, cfg->spill_smearing_variance,
1402
382
        cfg->round, MAX_USED_BITS.smearing_variance_mean);
1403
382
  configure_decoder_setup(&setup_pix, dec, cfg->cmp_mode, cfg->cmp_par_smearing_pixels_error, cfg->spill_smearing_pixels_error,
1404
382
        cfg->round, MAX_USED_BITS.smearing_outlier_pixels);
1405
1406
1.46k
  for (i = 0; ; i++) {
1407
1.46k
    err = decode_value(&setup_mean, &decoded_value, model.mean);
1408
1.46k
    if (err)
1409
5
      break;
1410
1.46k
    data_buf[i].mean = decoded_value;
1411
1412
1.46k
    err = decode_value(&setup_var, &decoded_value, model.variance_mean);
1413
1.46k
    if (err)
1414
5
      break;
1415
1.45k
    data_buf[i].variance_mean = (__typeof__(data_buf[i].variance_mean))decoded_value;
1416
1417
1.45k
    err = decode_value(&setup_pix, &decoded_value, model.outlier_pixels);
1418
1.45k
    if (err)
1419
5
      break;
1420
1.45k
    data_buf[i].outlier_pixels = (__typeof__(data_buf[i].outlier_pixels))decoded_value;
1421
1422
1.45k
    if (up_model_buf) {
1423
623
      up_model_buf[i].mean = cmp_up_model(data_buf[i].mean,
1424
623
        model.mean, cfg->model_value, setup_mean.lossy_par);
1425
623
      up_model_buf[i].variance_mean = cmp_up_model(data_buf[i].variance_mean,
1426
623
        model.variance_mean, cfg->model_value, setup_var.lossy_par);
1427
623
      up_model_buf[i].outlier_pixels = cmp_up_model(data_buf[i].outlier_pixels,
1428
623
        model.outlier_pixels, cfg->model_value, setup_pix.lossy_par);
1429
623
    }
1430
1431
1.45k
    if (i >= cfg->samples-1)
1432
367
      break;
1433
1434
1.08k
    model = next_model_p[i];
1435
1.08k
  }
1436
382
  return err;
1437
382
}
1438
1439
1440
/**
1441
 * @brief Decompresses the collection header.
1442
 *
1443
 * @param cfg pointer to the compression configuration structure
1444
 *
1445
 * @note the collection header is not truly compressed; it is simply copied into
1446
 *  the compressed data.
1447
 *
1448
 * @return The size of the decompressed collection header on success,
1449
 *         or -1 if the buffer length is insufficient
1450
 */
1451
1452
static int decompress_collection_hdr(const struct cmp_cfg *cfg)
1453
7.05k
{
1454
7.05k
  if (cfg->stream_size < COLLECTION_HDR_SIZE)
1455
0
    return -1;
1456
1457
7.05k
  if (cfg->src) {
1458
7.05k
    if (cfg->dst)
1459
7.05k
      memcpy(cfg->dst, cfg->src, COLLECTION_HDR_SIZE);
1460
1461
7.05k
    if (model_mode_is_used(cfg->cmp_mode) && cfg->updated_model_buf)
1462
2.71k
      memcpy(cfg->updated_model_buf, cfg->src, COLLECTION_HDR_SIZE);
1463
7.05k
  }
1464
7.05k
  return COLLECTION_HDR_SIZE;
1465
7.05k
}
1466
1467
1468
/**
1469
 * @brief decompress the data based on a compression configuration
1470
 *
1471
 * @param cfg   pointer to a compression configuration
1472
 * @param decmp_type  type of decompression: ICU chunk or RDCU decompression
1473
 *
1474
 * @note cfg->buffer_length is measured in bytes
1475
 *
1476
 * @returns the size of the decompressed data on success; returns negative on failure
1477
 */
1478
1479
static int decompressed_data_internal(const struct cmp_cfg *cfg, enum decmp_type decmp_type)
1480
73.4k
{
1481
73.4k
  int err;
1482
73.4k
  uint32_t data_size;
1483
1484
73.4k
  assert(decmp_type == ICU_DECOMRESSION || decmp_type == RDCU_DECOMPRESSION);
1485
1486
73.4k
  if (!cfg)
1487
0
    return -1;
1488
1489
73.4k
  if (!cfg->src)
1490
0
    return -1;
1491
1492
73.4k
  if (cmp_cfg_gen_par_is_invalid(cfg))
1493
0
    return -1;
1494
1495
73.4k
  if (cmp_imagette_data_type_is_used(cfg->data_type)) {
1496
12.8k
    if (cmp_cfg_imagette_is_invalid(cfg))
1497
5
      return -1;
1498
60.6k
  } else if (cmp_fx_cob_data_type_is_used(cfg->data_type)) {
1499
50.0k
    if (cmp_cfg_fx_cob_is_invalid(cfg))
1500
15
      return -1;
1501
50.0k
  } else if (cmp_aux_data_type_is_used(cfg->data_type)) {
1502
10.5k
    if (cmp_cfg_aux_is_invalid(cfg))
1503
13
      return -1;
1504
10.5k
  } else {
1505
0
    return -1;
1506
0
  }
1507
1508
73.4k
  if (model_mode_is_used(cfg->cmp_mode))
1509
8.24k
    if (!cfg->model_buf) /* we need a model for model compression */
1510
2
      return -1;
1511
1512
73.4k
  data_size = cfg->samples * (uint32_t)size_of_a_sample(cfg->data_type);
1513
73.4k
  if (decmp_type == ICU_DECOMRESSION)
1514
73.2k
    data_size += COLLECTION_HDR_SIZE;
1515
1516
73.4k
  if (cfg->cmp_mode == CMP_MODE_RAW) {
1517
58.3k
    if (cfg->dst) {
1518
11.7k
      memcpy(cfg->dst, cfg->src, data_size);
1519
11.7k
      switch (decmp_type) {
1520
11.7k
      case ICU_DECOMRESSION:
1521
11.7k
        if (be_to_cpu_chunk(cfg->dst, data_size))
1522
0
          return -1;
1523
11.7k
        break;
1524
11.7k
      case RDCU_DECOMPRESSION:
1525
0
        if (be_to_cpu_data_type(cfg->dst, data_size,
1526
0
              cfg->data_type))
1527
0
          return -1;
1528
0
        break;
1529
11.7k
      }
1530
11.7k
    }
1531
58.3k
    err = 0;
1532
1533
58.3k
  } else {
1534
15.0k
    struct bit_decoder dec;
1535
15.0k
    int hdr_size = 0;
1536
1537
15.0k
    if (!cfg->dst)
1538
7.88k
      return (int)data_size;
1539
1540
7.17k
    if (decmp_type == ICU_DECOMRESSION) {
1541
7.05k
      hdr_size = decompress_collection_hdr(cfg);
1542
7.05k
      if (hdr_size < 0)
1543
0
        return -1;
1544
7.05k
    }
1545
1546
7.17k
    bit_init_decoder(&dec, (const uint8_t *)cfg->src+hdr_size,
1547
7.17k
         cfg->stream_size-(uint32_t)hdr_size);
1548
1549
7.17k
    switch (cfg->data_type) {
1550
398
    case DATA_TYPE_IMAGETTE:
1551
404
    case DATA_TYPE_IMAGETTE_ADAPTIVE:
1552
638
    case DATA_TYPE_SAT_IMAGETTE:
1553
654
    case DATA_TYPE_SAT_IMAGETTE_ADAPTIVE:
1554
935
    case DATA_TYPE_F_CAM_IMAGETTE:
1555
936
    case DATA_TYPE_F_CAM_IMAGETTE_ADAPTIVE:
1556
936
      err = decompress_imagette(cfg, &dec, decmp_type);
1557
936
      break;
1558
261
    case DATA_TYPE_S_FX:
1559
261
      err = decompress_s_fx(cfg, &dec);
1560
261
      break;
1561
455
    case DATA_TYPE_S_FX_EFX:
1562
455
      err = decompress_s_fx_efx(cfg, &dec);
1563
455
      break;
1564
283
    case DATA_TYPE_S_FX_NCOB:
1565
283
      err = decompress_s_fx_ncob(cfg, &dec);
1566
283
      break;
1567
277
    case DATA_TYPE_S_FX_EFX_NCOB_ECOB:
1568
277
      err = decompress_s_fx_efx_ncob_ecob(cfg, &dec);
1569
277
      break;
1570
1571
1.69k
    case DATA_TYPE_L_FX:
1572
1.69k
      err = decompress_l_fx(cfg, &dec);
1573
1.69k
      break;
1574
391
    case DATA_TYPE_L_FX_EFX:
1575
391
      err = decompress_l_fx_efx(cfg, &dec);
1576
391
      break;
1577
296
    case DATA_TYPE_L_FX_NCOB:
1578
296
      err = decompress_l_fx_ncob(cfg, &dec);
1579
296
      break;
1580
762
    case DATA_TYPE_L_FX_EFX_NCOB_ECOB:
1581
762
      err = decompress_l_fx_efx_ncob_ecob(cfg, &dec);
1582
762
      break;
1583
1584
257
    case DATA_TYPE_OFFSET:
1585
509
    case DATA_TYPE_F_CAM_OFFSET:
1586
509
      err = decompress_offset(cfg, &dec);
1587
509
      break;
1588
87
    case DATA_TYPE_BACKGROUND:
1589
922
    case DATA_TYPE_F_CAM_BACKGROUND:
1590
922
      err = decompress_background(cfg, &dec);
1591
922
      break;
1592
382
    case DATA_TYPE_SMEARING:
1593
382
      err = decompress_smearing(cfg, &dec);
1594
382
      break;
1595
1596
1
    case DATA_TYPE_F_FX:
1597
1
    case DATA_TYPE_F_FX_EFX:
1598
1
    case DATA_TYPE_F_FX_NCOB:
1599
1
    case DATA_TYPE_F_FX_EFX_NCOB_ECOB:
1600
1
    case DATA_TYPE_CHUNK:
1601
1
    case DATA_TYPE_UNKNOWN:
1602
1
    default:
1603
1
      err = -1;
1604
1
      debug_print("Error: Compressed data type not supported.");
1605
1
      break;
1606
7.17k
    }
1607
1608
7.17k
    switch (bit_refill(&dec)) {
1609
137
    case BIT_OVERFLOW:
1610
137
      if (dec.cursor == dec.limit_ptr)
1611
134
        debug_print("Error: The end of the compressed bit stream has been exceeded. Please check that the compression parameters match those used to compress the data and that the compressed data are not corrupted.");
1612
3
      else
1613
3
        debug_print("Error: Data consistency check failed. %s", please_check_str);
1614
137
      break;
1615
4.25k
    case BIT_END_OF_BUFFER:
1616
      /* check if non consumed bits are zero */
1617
4.25k
      { unsigned int bits_not_read = sizeof(dec.bit_container)*8 - dec.bits_consumed;
1618
1619
4.25k
        if (bits_not_read > 57) /* can not read more than 57 bits */
1620
31
          bits_not_read = 57;
1621
1622
4.25k
        if (bit_read_bits(&dec, bits_not_read ) == 0)
1623
4.07k
          break;
1624
4.25k
      } /* fall through */
1625
271
    case BIT_UNFINISHED:
1626
271
      debug_print("Warning: Not all compressed data are processed.");
1627
271
      break;
1628
7.17k
    }
1629
7.17k
  }
1630
65.5k
  if (err)
1631
251
    return -1;
1632
1633
65.3k
  return (int)data_size;
1634
65.5k
}
1635
1636
1637
/**
1638
 * @brief read in an imagette compression entity header to a
1639
 *  compression configuration
1640
 *
1641
 * @param ent pointer to a compression entity
1642
 * @param cfg pointer to a compression configuration
1643
 *
1644
 * @returns 0 on success; otherwise error
1645
 */
1646
1647
static int cmp_ent_read_header(const struct cmp_entity *ent, struct cmp_cfg *cfg)
1648
3.88k
{
1649
3.88k
  uint32_t org_size;
1650
1651
3.88k
  if (!cfg)
1652
0
    return -1;
1653
1654
3.88k
  cfg->data_type = cmp_ent_get_data_type(ent);
1655
  /* the compression entity data type field only supports imagette or chunk data types */
1656
3.88k
  if (cfg->data_type != DATA_TYPE_CHUNK && !rdcu_supported_data_type_is_used(cfg->data_type)) {
1657
4
    debug_print("Error: Compression entity data type not supported.");
1658
4
    return -1;
1659
4
  }
1660
1661
3.88k
  cfg->cmp_mode = cmp_ent_get_cmp_mode(ent);
1662
3.88k
  if (cmp_ent_get_data_type_raw_bit(ent) != (cfg->cmp_mode == CMP_MODE_RAW)) {
1663
2
    debug_print("Error: The entity's raw data bit does not match up with the compression mode.");
1664
2
    return -1;
1665
2
  }
1666
3.88k
  cfg->model_value = cmp_ent_get_model_value(ent);
1667
3.88k
  cfg->round = cmp_ent_get_lossy_cmp_par(ent);
1668
3.88k
  cfg->stream_size = cmp_ent_get_cmp_data_size(ent);
1669
1670
3.88k
  if (cmp_cfg_gen_par_is_invalid(cfg))
1671
6
    return -1;
1672
1673
3.87k
  org_size = cmp_ent_get_original_size(ent);
1674
3.87k
  if (cfg->data_type == DATA_TYPE_CHUNK) {
1675
3.60k
    cfg->samples = 0;
1676
3.60k
    if ((cfg->stream_size < (COLLECTION_HDR_SIZE + CMP_COLLECTION_FILD_SIZE) && (cfg->cmp_mode != CMP_MODE_RAW)) ||
1677
3.60k
        (cfg->stream_size < COLLECTION_HDR_SIZE && (cfg->cmp_mode == CMP_MODE_RAW))) {
1678
4
      debug_print("Error: The compressed data size in the compression header is smaller than a collection header.");
1679
4
      return -1;
1680
4
    }
1681
3.60k
    if (org_size < COLLECTION_HDR_SIZE) {
1682
1
      debug_print("Error: The original decompressed data size in the compression header is smaller than the minimum size.");
1683
1
      return -1;
1684
1
    }
1685
3.60k
  } else {
1686
269
    if (org_size % sizeof(uint16_t)) {
1687
2
      debug_print("Error: The original size of an imagette product type in the compression header must be a multiple of 2.");
1688
2
      cfg->samples = 0;
1689
2
      return -1;
1690
2
    }
1691
267
    cfg->samples = org_size/sizeof(uint16_t);
1692
267
  }
1693
1694
3.87k
  cfg->src = cmp_ent_get_data_buf_const(ent);
1695
1696
3.87k
  if (cmp_ent_get_reserved(ent))
1697
948
    debug_print("Warning: The reserved field in the compressed header should be zero.");
1698
1699
3.87k
  if (cfg->cmp_mode == CMP_MODE_RAW) {
1700
338
    if (cmp_ent_get_original_size(ent) != cmp_ent_get_cmp_data_size(ent)) {
1701
4
      debug_print("Error: The compressed data size and the decompressed original data size in the compression header should be the same in raw mode.");
1702
4
      return -1;
1703
4
    }
1704
    /* no specific header is used for raw data we are done */
1705
334
    return 0;
1706
338
  }
1707
1708
3.53k
  if (cmp_ent_cal_hdr_size(cfg->data_type, cfg->cmp_mode == CMP_MODE_RAW)
1709
3.53k
      > cmp_ent_get_size(ent)) {
1710
12
    debug_print("Error: The compression entity size is smaller than the minimum allowed size.");
1711
12
    return -1;
1712
12
  }
1713
1714
3.52k
  switch (cfg->data_type) {
1715
13
  case DATA_TYPE_IMAGETTE_ADAPTIVE:
1716
46
  case DATA_TYPE_SAT_IMAGETTE_ADAPTIVE:
1717
49
  case DATA_TYPE_F_CAM_IMAGETTE_ADAPTIVE:
1718
    /* we do not read in adaptive parameters */
1719
130
  case DATA_TYPE_IMAGETTE:
1720
141
  case DATA_TYPE_SAT_IMAGETTE:
1721
232
  case DATA_TYPE_F_CAM_IMAGETTE:
1722
232
    cfg->cmp_par_imagette = cmp_ent_get_ima_golomb_par(ent);
1723
232
    cfg->spill_imagette = cmp_ent_get_ima_spill(ent);
1724
232
    break;
1725
0
  case DATA_TYPE_OFFSET:
1726
0
  case DATA_TYPE_F_CAM_OFFSET:
1727
0
  case DATA_TYPE_BACKGROUND:
1728
0
  case DATA_TYPE_F_CAM_BACKGROUND:
1729
0
  case DATA_TYPE_SMEARING:
1730
0
  case DATA_TYPE_S_FX:
1731
0
  case DATA_TYPE_S_FX_EFX:
1732
0
  case DATA_TYPE_S_FX_NCOB:
1733
0
  case DATA_TYPE_S_FX_EFX_NCOB_ECOB:
1734
0
  case DATA_TYPE_L_FX:
1735
0
  case DATA_TYPE_L_FX_EFX:
1736
0
  case DATA_TYPE_L_FX_NCOB:
1737
0
  case DATA_TYPE_L_FX_EFX_NCOB_ECOB:
1738
0
  case DATA_TYPE_F_FX:
1739
0
  case DATA_TYPE_F_FX_EFX:
1740
0
  case DATA_TYPE_F_FX_NCOB:
1741
0
  case DATA_TYPE_F_FX_EFX_NCOB_ECOB:
1742
3.28k
  case DATA_TYPE_CHUNK:
1743
3.28k
    cfg->cmp_par_exp_flags = cmp_ent_get_non_ima_cmp_par1(ent);
1744
3.28k
    cfg->spill_exp_flags = cmp_ent_get_non_ima_spill1(ent);
1745
3.28k
    cfg->cmp_par_fx = cmp_ent_get_non_ima_cmp_par2(ent);
1746
3.28k
    cfg->spill_fx = cmp_ent_get_non_ima_spill2(ent);
1747
3.28k
    cfg->cmp_par_ncob = cmp_ent_get_non_ima_cmp_par3(ent);
1748
3.28k
    cfg->spill_ncob = cmp_ent_get_non_ima_spill3(ent);
1749
3.28k
    cfg->cmp_par_efx = cmp_ent_get_non_ima_cmp_par4(ent);
1750
3.28k
    cfg->spill_efx = cmp_ent_get_non_ima_spill4(ent);
1751
3.28k
    cfg->cmp_par_ecob = cmp_ent_get_non_ima_cmp_par5(ent);
1752
3.28k
    cfg->spill_ecob = cmp_ent_get_non_ima_spill5(ent);
1753
3.28k
    cfg->cmp_par_fx_cob_variance = cmp_ent_get_non_ima_cmp_par6(ent);
1754
3.28k
    cfg->spill_fx_cob_variance = cmp_ent_get_non_ima_spill6(ent);
1755
3.28k
    break;
1756
  /* LCOV_EXCL_START */
1757
0
  case DATA_TYPE_UNKNOWN:
1758
0
  default:
1759
0
    return -1;
1760
  /* LCOV_EXCL_STOP */
1761
3.52k
  }
1762
1763
3.52k
  return 0;
1764
3.52k
}
1765
1766
1767
/**
1768
 * @brief Get the size of the compressed collection data
1769
 *
1770
 * @param cmp_col pointer to a compressed collection
1771
 *
1772
 * @return The size of the compressed collection data in bytes
1773
 */
1774
1775
static uint16_t get_cmp_collection_data_length(const uint8_t *cmp_col)
1776
3.12M
{
1777
3.12M
  uint16_t cmp_data_size;
1778
  /* If a non-raw mode is used to compress all collections, a
1779
   * 2-byte big endian field with the size of the compressed data
1780
   * is prefixed (without the size of the file itself and without
1781
   * the size of the collection header). This is followed by a
1782
   * collection header, followed by the compressed data.
1783
   * |---------------------| - cmp_col
1784
   * |compressed collection|
1785
   * |      data size      | 2 bytes
1786
   * |---------------------|-
1787
   * |   COLLECTION HDR    |
1788
   * |                     | 12 bytes
1789
   * |---------------------|-
1790
   * |    compressed data  | (variable) data size
1791
   * |         *-*-*       |
1792
   * |         *-*-*       |
1793
   * |---------------------|- next cmp_col
1794
   * Fields not scaled correctly
1795
   */
1796
1797
3.12M
  memcpy(&cmp_data_size, cmp_col, sizeof(cmp_data_size));
1798
3.12M
  be16_to_cpus(&cmp_data_size);
1799
1800
3.12M
  return cmp_data_size;
1801
3.12M
}
1802
1803
1804
/**
1805
 * @brief get the total size of the compressed collection
1806
 * j
1807
 * This function returns the total size of the compressed collection in bytes,
1808
 * including the size of the compressed size field itself, the collection header,
1809
 * and the compressed collection data.
1810
 *
1811
 * @param cmp_col pointer to a compressed collection
1812
 *
1813
 * @return The total size of the compressed collection in bytes
1814
 */
1815
1816
static uint32_t get_cmp_collection_size(const uint8_t *cmp_col)
1817
3.05M
{
1818
3.05M
  return CMP_COLLECTION_FILD_SIZE + COLLECTION_HDR_SIZE
1819
3.05M
    + get_cmp_collection_data_length(cmp_col);
1820
3.05M
}
1821
1822
1823
/**
1824
 * @brief get the number of compressed collections in a compression entity
1825
 *
1826
 * This function returns the number of compressed collections in a compression
1827
 * entity, by iterating over the compressed collection data
1828
 *
1829
 * @param ent  pointer to the compression entity
1830
 *
1831
 * @return the number of compressed collections in the compressed entity, or -1
1832
 *  on error
1833
 */
1834
1835
static int get_num_of_chunks(const struct cmp_entity *ent)
1836
3.28k
{
1837
3.28k
  const uint8_t *cmp_data_p = cmp_ent_get_data_buf_const(ent);
1838
3.28k
  long const cmp_data_size = cmp_ent_get_cmp_data_size(ent);
1839
3.28k
  int n = 0;
1840
3.28k
  const uint8_t *p = cmp_data_p;
1841
  /* highest plausible address of compressed collection */
1842
3.28k
  const uint8_t *limit_ptr = cmp_data_p + cmp_data_size - COLLECTION_HDR_SIZE;
1843
1844
76.6k
  while (p < limit_ptr) {
1845
73.3k
    p += get_cmp_collection_size(p);
1846
73.3k
    n++;
1847
73.3k
  }
1848
1849
3.28k
  if (p-cmp_data_p != cmp_data_size) {
1850
2
    debug_print("Error: The sum of the compressed collection does not match the size of the data in the compression header.");
1851
2
    return -1;
1852
2
  }
1853
3.28k
  return n;
1854
3.28k
}
1855
1856
1857
/**
1858
 * @brief Parse n'th compressed collection and set configuration parameters
1859
 *  for decompression it
1860
 *
1861
 * @param cmp_col   pointer to a compressed collection to parse
1862
 * @param n     the number of the compressed collection to
1863
 *        parse, starting from 1
1864
 * @param cfg     pointer to the configuration structure
1865
 * @param coll_uncompressed pointer to store whether the collection is
1866
 *        uncompressed or not
1867
 * @param decmp_size    size of the original decompressed data
1868
 *
1869
 * @return the byte offset where to put the uncompressed result in the
1870
 *  decompressed data, or -1 on error.
1871
 */
1872
1873
static long parse_cmp_collection(const uint8_t *cmp_col, int n, struct cmp_cfg *cfg,
1874
         int *coll_uncompressed, int decmp_size)
1875
73.2k
{
1876
73.2k
  int i;
1877
73.2k
  long decmp_pos = 0; /* position where to put the uncompressed result */
1878
  /* pointer to the collection header */
1879
73.2k
  const struct collection_hdr *col_hdr =
1880
73.2k
    (const struct collection_hdr *)(cmp_col + CMP_COLLECTION_FILD_SIZE);
1881
73.2k
  uint32_t cmp_data_size; /* size of the compressed data in the collection (not including the header) */
1882
73.2k
  uint16_t original_col_size; /* size of the decompressed collection data (not including the header) */
1883
73.2k
  size_t sample_size;
1884
1885
  /* get to the collection we want to decompress */
1886
3.05M
  for (i = 0; i < n; i++) {
1887
2.97M
    decmp_pos += cmp_col_get_size(col_hdr);
1888
2.97M
    cmp_col += get_cmp_collection_size(cmp_col);
1889
2.97M
    col_hdr = (const struct collection_hdr *)(cmp_col + CMP_COLLECTION_FILD_SIZE);
1890
2.97M
  }
1891
1892
73.2k
  cmp_data_size = get_cmp_collection_data_length(cmp_col);
1893
73.2k
  original_col_size = cmp_col_get_data_length(col_hdr);
1894
1895
73.2k
  if (cmp_data_size > original_col_size) {
1896
1
    debug_print("Error: Collection %i, the size of the compressed collection is larger than that of the uncompressed collection.", i);
1897
1
    return -1;
1898
1
  }
1899
1900
  /* if the compressed data size == original_col_size the collection data
1901
   * was put uncompressed into the bitstream */
1902
73.2k
  if (cmp_data_size == original_col_size)
1903
58.3k
    *coll_uncompressed = 1;
1904
14.8k
  else
1905
14.8k
    *coll_uncompressed = 0;
1906
1907
73.2k
  cfg->src = col_hdr;
1908
73.2k
  cfg->stream_size = cmp_data_size + COLLECTION_HDR_SIZE;
1909
1910
73.2k
  cfg->data_type = convert_subservice_to_cmp_data_type(cmp_col_get_subservice(col_hdr));
1911
73.2k
  sample_size = size_of_a_sample(cfg->data_type);
1912
73.2k
  if (!sample_size)
1913
11
    return -1;
1914
1915
73.2k
  if (original_col_size % sample_size) {
1916
7
    debug_print("Error: The size of the collection is not a multiple of a collection entry.");
1917
7
    return -1;
1918
7
  }
1919
73.2k
  cfg->samples = original_col_size / sample_size;
1920
1921
73.2k
  if (decmp_pos + original_col_size + COLLECTION_HDR_SIZE > decmp_size) {
1922
1
    debug_print("Error: The compressed data and the original size do not match.");
1923
1
    return -1;
1924
1
  }
1925
1926
73.2k
  return decmp_pos;
1927
73.2k
}
1928
1929
1930
/**
1931
 * @brief decompress a compression entity
1932
 *
1933
 * @note this function assumes that the entity size in the ent header is correct
1934
 * @param ent     pointer to the compression entity to be decompressed
1935
 * @param model_of_data   pointer to model data buffer (can be NULL if no
1936
 *        model compression mode is used)
1937
 * @param up_model_buf    pointer to store the updated model for the next model
1938
 *        mode compression (can be the same as the model_of_data
1939
 *        buffer for an in-place update or NULL if the updated model is not needed)
1940
 * @param decompressed_data pointer to the decompressed data buffer (can be NULL)
1941
 *
1942
 * @returns the size of the decompressed data on success; returns negative on failure
1943
 */
1944
1945
int decompress_cmp_entiy(const struct cmp_entity *ent, const void *model_of_data,
1946
       void *up_model_buf, void *decompressed_data)
1947
3.90k
{
1948
3.90k
  struct cmp_cfg cfg;
1949
3.90k
  int decmp_size;
1950
3.90k
  int i, n_chunks;
1951
1952
3.90k
  memset(&cfg, 0, sizeof(struct cmp_cfg));
1953
1954
3.90k
  if (!ent)
1955
10
    return -1;
1956
1957
3.89k
  decmp_size = (int)cmp_ent_get_original_size(ent);
1958
3.89k
  if (decmp_size < 0)
1959
0
    return -1;
1960
3.89k
  if (decmp_size == 0)
1961
6
    return 0;
1962
1963
3.88k
  if (cmp_ent_read_header(ent, &cfg))
1964
35
    return -1;
1965
1966
3.85k
  if (cfg.data_type != DATA_TYPE_CHUNK) { /* perform a non-chunk decompression */
1967
251
    if (cfg.cmp_mode == CMP_MODE_RAW) {
1968
19
      uint32_t data_size = cfg.samples * sizeof(uint16_t);
1969
1970
19
      if (decompressed_data) {
1971
9
        memcpy(decompressed_data, cmp_ent_get_data_buf_const(ent), data_size);
1972
9
        if (cmp_input_big_to_cpu_endianness(decompressed_data, data_size, cfg.data_type))
1973
0
          return -1;
1974
9
      }
1975
19
      return (int)data_size;
1976
19
    }
1977
1978
232
    cfg.model_buf = model_of_data;
1979
232
    cfg.updated_model_buf = up_model_buf;
1980
232
    cfg.dst = decompressed_data;
1981
1982
232
    return decompressed_data_internal(&cfg, RDCU_DECOMPRESSION);
1983
251
  }
1984
1985
  /* perform a chunk decompression */
1986
1987
3.60k
  if (cfg.cmp_mode == CMP_MODE_RAW) {
1988
315
    if (decompressed_data) {
1989
149
      memcpy(decompressed_data, cfg.src, cfg.stream_size);
1990
149
      cpu_to_be_chunk(decompressed_data, cfg.stream_size);
1991
149
    }
1992
315
    return (int)cfg.stream_size;
1993
315
  }
1994
1995
3.28k
  n_chunks = get_num_of_chunks(ent);
1996
3.28k
  if (n_chunks <= 0)
1997
2
    return -1;
1998
1999
76.3k
  for (i = 0; i < n_chunks; i++) {
2000
73.2k
    int decmp_chunk_size;
2001
73.2k
    int col_uncompressed;
2002
73.2k
    struct cmp_cfg cmp_cpy = cfg;
2003
73.2k
    long offset = parse_cmp_collection(cmp_ent_get_data_buf_const(ent), i,
2004
73.2k
               &cmp_cpy, &col_uncompressed, decmp_size);
2005
73.2k
    if (offset < 0)
2006
20
      return -1;
2007
2008
73.2k
    if (decompressed_data)
2009
18.7k
      cmp_cpy.dst = (uint8_t *)decompressed_data + offset;
2010
73.2k
    if (model_of_data)
2011
59.9k
      cmp_cpy.model_buf = (const uint8_t *)model_of_data + offset;
2012
73.2k
    if (up_model_buf)
2013
31.6k
      cmp_cpy.updated_model_buf = (uint8_t *)up_model_buf + offset;
2014
2015
73.2k
    if (col_uncompressed) {
2016
58.3k
      if (cmp_cpy.updated_model_buf && model_mode_is_used(cmp_cpy.cmp_mode)) {
2017
24.2k
        uint32_t s = cmp_cpy.stream_size;
2018
24.2k
        memcpy(cmp_cpy.updated_model_buf, cmp_cpy.src, s);
2019
24.2k
        if (be_to_cpu_chunk(cmp_cpy.updated_model_buf, s))
2020
0
          return -1;
2021
24.2k
      }
2022
58.3k
      cmp_cpy.cmp_mode = CMP_MODE_RAW;
2023
58.3k
    }
2024
2025
73.2k
    decmp_chunk_size = decompressed_data_internal(&cmp_cpy, ICU_DECOMRESSION);
2026
73.2k
    if (decmp_chunk_size < 0)
2027
222
      return decmp_chunk_size;
2028
73.2k
  }
2029
3.04k
  return decmp_size;
2030
3.28k
}
2031
2032
2033
/**
2034
 * @brief decompress RDCU compressed data without a compression entity header
2035
 *
2036
 * @param compressed_data pointer to the RDCU compressed data (without a
2037
 *        compression entity header)
2038
 * @param info      pointer to a decompression information structure
2039
 *        consisting of the metadata of the compression
2040
 * @param model_of_data   pointer to model data buffer (can be NULL if no
2041
 *        model compression mode is used)
2042
 * @param up_model_buf    pointer to store the updated model for the next model
2043
 *        mode compression (can be the same as the model_of_data
2044
 *        buffer for an in-place update or NULL if the
2045
 *        updated model is not needed)
2046
 * @param decompressed_data pointer to the decompressed data buffer (can be NULL)
2047
 *
2048
 * @returns the size of the decompressed data on success; returns negative on failure
2049
 */
2050
2051
int decompress_rdcu_data(const uint32_t *compressed_data, const struct cmp_info *info,
2052
       const uint16_t *model_of_data, uint16_t *up_model_buf,
2053
       uint16_t *decompressed_data)
2054
2055
0
{
2056
0
  struct cmp_cfg cfg;
2057
2058
0
  if (!compressed_data)
2059
0
    return -1;
2060
2061
0
  if (!info)
2062
0
    return -1;
2063
2064
0
  if (info->cmp_err)
2065
0
    return -1;
2066
2067
0
  memset(&cfg, 0, sizeof(struct cmp_cfg));
2068
2069
0
  cfg.data_type = DATA_TYPE_IMAGETTE;
2070
0
  cfg.model_buf = model_of_data;
2071
0
  cfg.updated_model_buf = up_model_buf;
2072
0
  cfg.dst = decompressed_data;
2073
2074
0
  cfg.cmp_mode = info->cmp_mode_used;
2075
0
  cfg.model_value = info->model_value_used;
2076
0
  cfg.round = info->round_used;
2077
0
  cfg.spill_imagette = info->spill_used;
2078
0
  cfg.cmp_par_imagette = info->golomb_par_used;
2079
0
  cfg.samples = info->samples_used;
2080
0
  cfg.src = compressed_data;
2081
0
  cfg.stream_size = (info->cmp_size+7)/8;
2082
2083
0
  return decompressed_data_internal(&cfg, RDCU_DECOMPRESSION);
2084
0
}