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
0
#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
90.0k
{
92
90.0k
  uint32_t const decoded_cw = bit_peek_leading_ones(dec); /* decode unary coding */
93
90.0k
  uint32_t const cw_len = decoded_cw + 1; /* Number of 1's + following 0 */
94
95
90.0k
  bit_consume_bits(dec, cw_len);
96
97
90.0k
  return decoded_cw;
98
90.0k
}
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.1k
{
116
21.1k
  uint32_t q;  /* quotient */
117
21.1k
  uint32_t r;  /* remainder */
118
119
21.1k
  assert(log2_m > 0 && log2_m < 32);
120
121
21.1k
  q = unary_decoder(dec, m, log2_m); /* decode quotient unary code part */
122
21.1k
  r = bit_read_bits32(dec, log2_m); /* get remainder */
123
124
21.1k
  return (q << log2_m) + r; /* calculate decoded value (q*m+r) */
125
21.1k
}
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
57.1k
{
141
57.1k
  uint32_t q;  /* quotient */
142
57.1k
  uint32_t r1; /* remainder case 1 */
143
57.1k
  uint32_t r2; /* remainder case 2 */
144
57.1k
  uint32_t r;  /* remainder */
145
57.1k
  uint32_t cutoff; /* cutoff between group 1 and 2 */
146
147
57.1k
  assert(m > 0);
148
57.1k
  assert(log2_m == ilog_2(m));
149
150
  /* decode quotient unary code part */
151
57.1k
  q = unary_decoder(dec, m, log2_m);
152
153
  /* get the remainder code for both cases */
154
57.1k
  r2 = (uint32_t)bit_peek_bits(dec, log2_m+1);
155
57.1k
  r1 = r2 >> 1;
156
157
  /* calculate cutoff between case 1 and 2 */
158
57.1k
  cutoff = (0x2U << log2_m) - m; /* = 2^(log2_m+1)-m */
159
160
57.1k
  if (r1 < cutoff) { /* remainder case 1: remainder length=log2_m */
161
45.9k
    bit_consume_bits(dec, log2_m);
162
45.9k
    r = r1;
163
45.9k
  } else { /* remainder case 2: remainder length = log2_m+1 */
164
11.2k
    bit_consume_bits(dec, log2_m+1);
165
11.2k
    r = r2 - cutoff;
166
11.2k
  }
167
168
57.1k
  return q*m + r;
169
57.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
21.9k
{
185
21.9k
  assert(golomb_par > 0);
186
187
21.9k
  if (golomb_par == 1)
188
2.69k
    return &unary_decoder;
189
190
19.2k
  if (is_a_pow_of_2(golomb_par))
191
4.81k
    return &rice_decoder;
192
14.4k
  else
193
14.4k
    return &golomb_decoder;
194
19.2k
}
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
76.1k
{
208
  /* Decode the next value in the bitstream with the Golomb/Rice/unary decoder */
209
76.1k
  *decoded_value = setup->decode_cw_f(setup->dec, setup->encoder_par1, setup->encoder_par2);
210
211
76.1k
  if (*decoded_value != 0) { /* no escape symbol detected */
212
46.0k
    if (*decoded_value >= setup->outlier_par) {
213
0
      debug_print("Error: Data consistency check failed. Non-outlier decoded value greater or equal than the outlier parameter. %s", please_check_str);
214
0
      return CORRUPTION_DETECTED;
215
0
    }
216
46.0k
    *decoded_value -= 1;
217
46.0k
  } else {
218
    /* the zero escape symbol mechanism was used; read unencoded value */
219
30.1k
    bit_refill(setup->dec);
220
30.1k
    *decoded_value = bit_read_bits32_sub_1(setup->dec, setup->max_data_bits);
221
222
30.1k
    if (*decoded_value < setup->outlier_par - 1) { /* -1 because we subtract -1 from the *decoded_value */
223
0
      if (bit_refill(setup->dec) != BIT_OVERFLOW)
224
0
        debug_print("Error: Data consistency check failed. Outlier small than the outlier parameter. %s", please_check_str);
225
0
      return CORRUPTION_DETECTED;
226
0
    }
227
30.1k
  }
228
76.1k
  return bit_refill(setup->dec) == BIT_OVERFLOW;
229
76.1k
}
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
13.8k
{
243
  /* Decode the next value in the bitstream with the Golomb/Rice/unary decoder */
244
13.8k
  *decoded_value = setup->decode_cw_f(setup->dec, setup->encoder_par1, setup->encoder_par2);
245
246
13.8k
  if (*decoded_value >= setup->outlier_par) { /* escape symbol mechanism detected */
247
2.72k
    uint32_t const unencoded_len = (*decoded_value - setup->outlier_par + 1) << 1;
248
249
2.72k
    if (unencoded_len > ((setup->max_data_bits+1) & -2U)) { /* round up max_data_bits to the nearest multiple of 2 */
250
0
      debug_print("Error: Data consistency check failed. Multi escape symbol higher than expected. %s", please_check_str);
251
0
      return CORRUPTION_DETECTED;
252
0
    }
253
254
    /* read unencoded value */
255
2.72k
    bit_refill(setup->dec);
256
2.72k
    *decoded_value = bit_read_bits32(setup->dec, unencoded_len);
257
258
2.72k
    if (*decoded_value >> (unencoded_len-2) == 0) { /* check if at least one bit of the two highest is set. */
259
40
      if (unencoded_len > 2) { /* Exception: if we code outlier_par as outlier, no set bit is expected */
260
0
        if (bit_refill(setup->dec) != BIT_OVERFLOW)
261
0
          debug_print("Error: Data consistency check failed. Unencoded data after multi escape symbol to small. %s", please_check_str);
262
0
        return CORRUPTION_DETECTED;
263
0
      }
264
40
    }
265
266
2.72k
    *decoded_value += setup->outlier_par;
267
268
2.72k
    if ((*decoded_value & BIT_MASK[setup->max_data_bits]) < setup->outlier_par) { /* check for overflow in addition */
269
0
      if (bit_refill(setup->dec) != BIT_OVERFLOW)
270
0
        debug_print("Error: Data consistency check failed. Outlier small than the outlier parameter. %s", please_check_str);
271
0
      return CORRUPTION_DETECTED;
272
0
    }
273
2.72k
  }
274
13.8k
  return bit_refill(setup->dec) == BIT_OVERFLOW;
275
13.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
90.0k
{
289
90.0k
  if (value_to_unmap & 0x1) { /* if uneven */
290
    /* uint64_t to prevent overflow if value_to_unmap == 0xFFFFFFFF */
291
26.2k
    uint64_t const tmp64 = value_to_unmap;
292
293
26.2k
    return (uint32_t)(-((tmp64 + 1) / 2));
294
63.7k
  } else {
295
63.7k
    return value_to_unmap / 2;
296
63.7k
  }
297
90.0k
}
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
90.0k
{
314
  /* decode the next value from the bitstream */
315
90.0k
  int const err = setup->decode_method_f(setup, decoded_value);
316
317
  /* map the unsigned decode value back to a signed value */
318
90.0k
  *decoded_value = re_map_to_pos(*decoded_value);
319
320
  /* decorrelate data the data with the model */
321
90.0k
  *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
90.0k
  *decoded_value &= BIT_MASK[setup->max_data_bits];
325
326
  /* inverse step of the lossy compression */
327
90.0k
  *decoded_value = round_inv(*decoded_value, setup->lossy_par);
328
329
90.0k
  return err;
330
90.0k
}
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
21.9k
{
350
21.9k
  assert(setup != NULL);
351
21.9k
  assert(dec != NULL);
352
21.9k
  assert(cmp_par != 0);
353
21.9k
  assert(max_data_bits > 0 && max_data_bits <= 32);
354
355
21.9k
  if (multi_escape_mech_is_used(cmp_mode))
356
3.66k
    setup->decode_method_f = &decode_multi;
357
18.3k
  else if (zero_escape_mech_is_used(cmp_mode))
358
18.3k
    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
21.9k
  setup->decode_cw_f = select_decoder(cmp_par);
364
21.9k
  setup->dec = dec;
365
21.9k
  setup->encoder_par1 = cmp_par; /* encoding parameter 1 */
366
21.9k
  setup->encoder_par2 = ilog_2(cmp_par); /* encoding parameter 2 */
367
21.9k
  setup->outlier_par = spillover; /* outlier parameter */
368
21.9k
  setup->lossy_par = lossy_par; /* lossy compression parameter */
369
21.9k
  setup->max_data_bits = max_data_bits; /* how many bits are needed to represent the highest possible value */
370
21.9k
}
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
10.8k
{
383
10.8k
  if (col)
384
9.64k
    col = (uint8_t *)col + COLLECTION_HDR_SIZE;
385
10.8k
  return col;
386
10.8k
}
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
6.66k
{
399
6.66k
  if (col)
400
3.94k
    col = (const uint8_t *)col + COLLECTION_HDR_SIZE;
401
6.66k
  return col;
402
6.66k
}
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
819
{
416
819
  size_t i;
417
819
  int err;
418
819
  uint32_t decoded_value;
419
819
  uint32_t max_data_bits;
420
819
  struct decoder_setup setup;
421
819
  uint16_t *data_buf;
422
819
  const uint16_t *model_buf;
423
819
  uint16_t *up_model_buf;
424
819
  const uint16_t *next_model_p;
425
819
  uint16_t model;
426
427
819
  switch (decmp_type) {
428
0
  case RDCU_DECOMPRESSION: /* RDCU compresses the header like data */
429
0
    data_buf = cfg->dst;
430
0
    model_buf = cfg->model_buf;
431
0
    up_model_buf = cfg->updated_model_buf;
432
0
    break;
433
819
  case ICU_DECOMRESSION:
434
819
    data_buf = get_collection_data(cfg->dst);
435
819
    model_buf = get_collection_data_const(cfg->model_buf);
436
819
    up_model_buf = get_collection_data(cfg->updated_model_buf);
437
819
    break;
438
819
  }
439
440
819
  if (model_mode_is_used(cfg->cmp_mode)) {
441
369
    model =  get_unaligned(&model_buf[0]);
442
369
    next_model_p = &model_buf[1];
443
450
  } else {
444
450
    up_model_buf = NULL;
445
450
    memset(&model, 0, sizeof(model));
446
450
    next_model_p = data_buf;
447
450
  }
448
449
819
  switch (cfg->data_type) {
450
356
  case DATA_TYPE_IMAGETTE:
451
356
  case DATA_TYPE_IMAGETTE_ADAPTIVE:
452
356
    max_data_bits = MAX_USED_BITS.nc_imagette;
453
356
    break;
454
227
  case DATA_TYPE_SAT_IMAGETTE:
455
227
  case DATA_TYPE_SAT_IMAGETTE_ADAPTIVE:
456
227
    max_data_bits = MAX_USED_BITS.saturated_imagette;
457
227
    break;
458
0
  default:
459
236
  case DATA_TYPE_F_CAM_IMAGETTE:
460
236
  case DATA_TYPE_F_CAM_IMAGETTE_ADAPTIVE:
461
236
    max_data_bits = MAX_USED_BITS.fc_imagette;
462
236
    break;
463
819
  }
464
465
819
  configure_decoder_setup(&setup, dec, cfg->cmp_mode, cfg->cmp_par_imagette,
466
819
        cfg->spill_imagette, cfg->round, max_data_bits);
467
468
15.5k
  for (i = 0; ; i++) {
469
15.5k
    err = decode_value(&setup, &decoded_value, model);
470
15.5k
    if (err)
471
0
      break;
472
473
15.5k
    put_unaligned((uint16_t)decoded_value, &data_buf[i]);
474
475
15.5k
    if (up_model_buf) {
476
5.59k
      uint16_t up_model = cmp_up_model((uint16_t)decoded_value, model, cfg->model_value,
477
5.59k
                   setup.lossy_par);
478
5.59k
      put_unaligned(up_model, &up_model_buf[i]);
479
5.59k
    }
480
481
15.5k
    if (i >= cfg->samples-1)
482
819
      break;
483
484
14.7k
    model = get_unaligned(&next_model_p[i]);
485
14.7k
  }
486
819
  return err;
487
819
}
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
212
{
501
212
  size_t i;
502
212
  int err;
503
212
  uint32_t decoded_value;
504
212
  struct decoder_setup setup_exp_flags, setup_fx;
505
212
  struct s_fx *data_buf = get_collection_data(cfg->dst);
506
212
  const struct s_fx *model_buf = get_collection_data_const(cfg->model_buf);
507
212
  struct s_fx *up_model_buf;
508
212
  const struct s_fx *next_model_p;
509
212
  struct s_fx model;
510
511
212
  if (model_mode_is_used(cfg->cmp_mode)) {
512
86
    model = model_buf[0];
513
86
    next_model_p = &model_buf[1];
514
86
    up_model_buf = get_collection_data(cfg->updated_model_buf);
515
126
  } else {
516
126
    memset(&model, 0, sizeof(model));
517
126
    next_model_p = data_buf;
518
126
    up_model_buf = NULL;
519
126
  }
520
521
212
  configure_decoder_setup(&setup_exp_flags, dec, cfg->cmp_mode, cfg->cmp_par_exp_flags,
522
212
        cfg->spill_exp_flags, cfg->round, MAX_USED_BITS.s_exp_flags);
523
212
  configure_decoder_setup(&setup_fx, dec, cfg->cmp_mode, cfg->cmp_par_fx,
524
212
        cfg->spill_fx, cfg->round, MAX_USED_BITS.s_fx);
525
526
2.95k
  for (i = 0; ; i++) {
527
2.95k
    err = decode_value(&setup_exp_flags, &decoded_value, model.exp_flags);
528
2.95k
    if (err)
529
0
      break;
530
2.95k
    data_buf[i].exp_flags = (__typeof__(data_buf[i].exp_flags))decoded_value;
531
532
2.95k
    err = decode_value(&setup_fx, &decoded_value, model.fx);
533
2.95k
    if (err)
534
0
      break;
535
2.95k
    data_buf[i].fx = decoded_value;
536
537
2.95k
    if (up_model_buf) {
538
272
      up_model_buf[i].exp_flags = cmp_up_model(data_buf[i].exp_flags, model.exp_flags,
539
272
                 cfg->model_value, setup_exp_flags.lossy_par);
540
272
      up_model_buf[i].fx = cmp_up_model(data_buf[i].fx, model.fx,
541
272
                cfg->model_value, setup_fx.lossy_par);
542
272
    }
543
544
2.95k
    if (i >= cfg->samples-1)
545
212
      break;
546
547
2.73k
    model = next_model_p[i];
548
2.73k
  }
549
212
  return err;
550
212
}
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
406
{
564
406
  size_t i;
565
406
  int err;
566
406
  uint32_t decoded_value;
567
406
  struct decoder_setup setup_exp_flags, setup_fx, setup_efx;
568
406
  struct s_fx_efx *data_buf = get_collection_data(cfg->dst);
569
406
  const struct s_fx_efx *model_buf = get_collection_data_const(cfg->model_buf);
570
406
  struct s_fx_efx *up_model_buf;
571
406
  const struct s_fx_efx *next_model_p;
572
406
  struct s_fx_efx model;
573
574
406
  if (model_mode_is_used(cfg->cmp_mode)) {
575
229
    up_model_buf = get_collection_data(cfg->updated_model_buf);
576
229
    model = model_buf[0];
577
229
    next_model_p = &model_buf[1];
578
229
  } else {
579
177
    up_model_buf = NULL;
580
177
    memset(&model, 0, sizeof(model));
581
177
    next_model_p = data_buf;
582
177
  }
583
584
406
  configure_decoder_setup(&setup_exp_flags, dec, cfg->cmp_mode, cfg->cmp_par_exp_flags,
585
406
        cfg->spill_exp_flags, cfg->round, MAX_USED_BITS.s_exp_flags);
586
406
  configure_decoder_setup(&setup_fx, dec, cfg->cmp_mode, cfg->cmp_par_fx,
587
406
        cfg->spill_fx, cfg->round, MAX_USED_BITS.s_fx);
588
406
  configure_decoder_setup(&setup_efx, dec, cfg->cmp_mode, cfg->cmp_par_efx,
589
406
        cfg->spill_efx, cfg->round, MAX_USED_BITS.s_efx);
590
591
2.27k
  for (i = 0; ; i++) {
592
2.27k
    err = decode_value(&setup_exp_flags, &decoded_value, model.exp_flags);
593
2.27k
    if (err)
594
0
      break;
595
2.27k
    data_buf[i].exp_flags = (__typeof__(data_buf[i].exp_flags)) decoded_value;
596
597
2.27k
    err = decode_value(&setup_fx, &decoded_value, model.fx);
598
2.27k
    if (err)
599
0
      break;
600
2.27k
    data_buf[i].fx = decoded_value;
601
602
2.27k
    err = decode_value(&setup_efx, &decoded_value, model.efx);
603
2.27k
    if (err)
604
0
      break;
605
2.27k
    data_buf[i].efx = decoded_value;
606
607
2.27k
    if (up_model_buf) {
608
476
      up_model_buf[i].exp_flags = cmp_up_model(data_buf[i].exp_flags, model.exp_flags,
609
476
                 cfg->model_value, setup_exp_flags.lossy_par);
610
476
      up_model_buf[i].fx = cmp_up_model(data_buf[i].fx, model.fx,
611
476
                cfg->model_value, setup_fx.lossy_par);
612
476
      up_model_buf[i].efx = cmp_up_model(data_buf[i].efx, model.efx,
613
476
                 cfg->model_value, setup_efx.lossy_par);
614
476
    }
615
616
2.27k
    if (i >= cfg->samples-1)
617
406
      break;
618
619
1.87k
    model = next_model_p[i];
620
1.87k
  }
621
406
  return err;
622
406
}
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
261
{
636
261
  size_t i;
637
261
  int err;
638
261
  uint32_t decoded_value;
639
261
  struct decoder_setup setup_exp_flags, setup_fx, setup_ncob;
640
261
  struct s_fx_ncob *data_buf = get_collection_data(cfg->dst);
641
261
  const struct s_fx_ncob *model_buf = get_collection_data_const(cfg->model_buf);
642
261
  struct s_fx_ncob *up_model_buf;
643
261
  const struct s_fx_ncob *next_model_p;
644
261
  struct s_fx_ncob model;
645
646
261
  if (model_mode_is_used(cfg->cmp_mode)) {
647
95
    up_model_buf = get_collection_data(cfg->updated_model_buf);
648
95
    model = model_buf[0];
649
95
    next_model_p = &model_buf[1];
650
166
  } else {
651
166
    up_model_buf = NULL;
652
166
    memset(&model, 0, sizeof(model));
653
166
    next_model_p = data_buf;
654
166
  }
655
656
261
  configure_decoder_setup(&setup_exp_flags, dec, cfg->cmp_mode, cfg->cmp_par_exp_flags,
657
261
        cfg->spill_exp_flags, cfg->round, MAX_USED_BITS.s_exp_flags);
658
261
  configure_decoder_setup(&setup_fx, dec, cfg->cmp_mode, cfg->cmp_par_fx,
659
261
        cfg->spill_fx, cfg->round, MAX_USED_BITS.s_fx);
660
261
  configure_decoder_setup(&setup_ncob, dec, cfg->cmp_mode, cfg->cmp_par_ncob,
661
261
        cfg->spill_ncob, cfg->round, MAX_USED_BITS.s_ncob);
662
663
1.76k
  for (i = 0; ; i++) {
664
1.76k
    err = decode_value(&setup_exp_flags, &decoded_value, model.exp_flags);
665
1.76k
    if (err)
666
0
      break;
667
1.76k
    data_buf[i].exp_flags = (__typeof__(data_buf[i].exp_flags)) decoded_value;
668
669
1.76k
    err = decode_value(&setup_fx, &decoded_value, model.fx);
670
1.76k
    if (err)
671
0
      break;
672
1.76k
    data_buf[i].fx = decoded_value;
673
674
1.76k
    err = decode_value(&setup_ncob, &decoded_value, model.ncob_x);
675
1.76k
    if (err)
676
0
      break;
677
1.76k
    data_buf[i].ncob_x = decoded_value;
678
679
1.76k
    err = decode_value(&setup_ncob, &decoded_value, model.ncob_y);
680
1.76k
    if (err)
681
0
      break;
682
1.76k
    data_buf[i].ncob_y = decoded_value;
683
684
1.76k
    if (up_model_buf) {
685
241
      up_model_buf[i].exp_flags = cmp_up_model(data_buf[i].exp_flags, model.exp_flags,
686
241
                 cfg->model_value, setup_exp_flags.lossy_par);
687
241
      up_model_buf[i].fx = cmp_up_model(data_buf[i].fx, model.fx,
688
241
                cfg->model_value, setup_fx.lossy_par);
689
241
      up_model_buf[i].ncob_x = cmp_up_model(data_buf[i].ncob_x, model.ncob_x,
690
241
                    cfg->model_value, setup_ncob.lossy_par);
691
241
      up_model_buf[i].ncob_y = cmp_up_model(data_buf[i].ncob_y, model.ncob_y,
692
241
                    cfg->model_value, setup_ncob.lossy_par);
693
241
    }
694
695
1.76k
    if (i >= cfg->samples-1)
696
261
      break;
697
698
1.50k
    model = next_model_p[i];
699
1.50k
  }
700
261
  return err;
701
261
}
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
253
{
715
253
  size_t i;
716
253
  int err;
717
253
  uint32_t decoded_value;
718
253
  struct decoder_setup setup_exp_flags, setup_fx, setup_ncob, setup_efx, setup_ecob;
719
253
  struct s_fx_efx_ncob_ecob *data_buf = get_collection_data(cfg->dst);
720
253
  const struct s_fx_efx_ncob_ecob *model_buf = get_collection_data_const(cfg->model_buf);
721
253
  struct s_fx_efx_ncob_ecob *up_model_buf;
722
253
  const struct s_fx_efx_ncob_ecob *next_model_p;
723
253
  struct s_fx_efx_ncob_ecob model;
724
725
253
  if (model_mode_is_used(cfg->cmp_mode)) {
726
220
    up_model_buf = get_collection_data(cfg->updated_model_buf);
727
220
    model = model_buf[0];
728
220
    next_model_p = &model_buf[1];
729
220
  } else {
730
33
    up_model_buf = NULL;
731
33
    memset(&model, 0, sizeof(model));
732
33
    next_model_p = data_buf;
733
33
  }
734
735
253
  configure_decoder_setup(&setup_exp_flags, dec, cfg->cmp_mode, cfg->cmp_par_exp_flags,
736
253
        cfg->spill_exp_flags, cfg->round, MAX_USED_BITS.s_exp_flags);
737
253
  configure_decoder_setup(&setup_fx, dec, cfg->cmp_mode, cfg->cmp_par_fx, cfg->spill_fx,
738
253
        cfg->round, MAX_USED_BITS.s_fx);
739
253
  configure_decoder_setup(&setup_ncob, dec, cfg->cmp_mode, cfg->cmp_par_ncob, cfg->spill_ncob,
740
253
        cfg->round, MAX_USED_BITS.s_ncob);
741
253
  configure_decoder_setup(&setup_efx, dec, cfg->cmp_mode, cfg->cmp_par_efx, cfg->spill_efx,
742
253
        cfg->round, MAX_USED_BITS.s_efx);
743
253
  configure_decoder_setup(&setup_ecob, dec, cfg->cmp_mode, cfg->cmp_par_ecob, cfg->spill_ecob,
744
253
        cfg->round, MAX_USED_BITS.s_ecob);
745
746
521
  for (i = 0; ; i++) {
747
521
    err = decode_value(&setup_exp_flags, &decoded_value, model.exp_flags);
748
521
    if (err)
749
0
      break;
750
521
    data_buf[i].exp_flags = (__typeof__(data_buf[i].exp_flags)) decoded_value;
751
752
521
    err = decode_value(&setup_fx, &decoded_value, model.fx);
753
521
    if (err)
754
0
      break;
755
521
    data_buf[i].fx = decoded_value;
756
757
521
    err = decode_value(&setup_ncob, &decoded_value, model.ncob_x);
758
521
    if (err)
759
0
      break;
760
521
    data_buf[i].ncob_x = decoded_value;
761
762
521
    err = decode_value(&setup_ncob, &decoded_value, model.ncob_y);
763
521
    if (err)
764
0
      break;
765
521
    data_buf[i].ncob_y = decoded_value;
766
767
521
    err = decode_value(&setup_efx, &decoded_value, model.efx);
768
521
    if (err)
769
0
      break;
770
521
    data_buf[i].efx = decoded_value;
771
772
521
    err = decode_value(&setup_ecob, &decoded_value, model.ecob_x);
773
521
    if (err)
774
0
      break;
775
521
    data_buf[i].ecob_x = decoded_value;
776
777
521
    err = decode_value(&setup_ecob, &decoded_value, model.ecob_y);
778
521
    if (err)
779
0
      break;
780
521
    data_buf[i].ecob_y = decoded_value;
781
782
521
    if (up_model_buf) {
783
266
      up_model_buf[i].exp_flags = cmp_up_model(data_buf[i].exp_flags, model.exp_flags,
784
266
                 cfg->model_value, setup_exp_flags.lossy_par);
785
266
      up_model_buf[i].fx = cmp_up_model(data_buf[i].fx, model.fx,
786
266
                cfg->model_value, setup_fx.lossy_par);
787
266
      up_model_buf[i].ncob_x = cmp_up_model(data_buf[i].ncob_x, model.ncob_x,
788
266
                    cfg->model_value, setup_ncob.lossy_par);
789
266
      up_model_buf[i].ncob_y = cmp_up_model(data_buf[i].ncob_y, model.ncob_y,
790
266
                    cfg->model_value, setup_ncob.lossy_par);
791
266
      up_model_buf[i].efx = cmp_up_model(data_buf[i].efx, model.efx,
792
266
                 cfg->model_value, setup_efx.lossy_par);
793
266
      up_model_buf[i].ecob_x = cmp_up_model(data_buf[i].ecob_x, model.ecob_x,
794
266
                    cfg->model_value, setup_ecob.lossy_par);
795
266
      up_model_buf[i].ecob_y = cmp_up_model(data_buf[i].ecob_y, model.ecob_y,
796
266
                    cfg->model_value, setup_ecob.lossy_par);
797
266
    }
798
799
521
    if (i >= cfg->samples-1)
800
253
      break;
801
802
268
    model = next_model_p[i];
803
268
  }
804
253
  return err;
805
253
}
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.67k
{
819
1.67k
  size_t i;
820
1.67k
  int err;
821
1.67k
  uint32_t decoded_value;
822
1.67k
  struct decoder_setup setup_exp_flags, setup_fx, setup_fx_var;
823
1.67k
  struct l_fx *data_buf = get_collection_data(cfg->dst);
824
1.67k
  const struct l_fx *model_buf = get_collection_data_const(cfg->model_buf);
825
1.67k
  struct l_fx *up_model_buf;
826
1.67k
  const struct l_fx *next_model_p;
827
1.67k
  struct l_fx model;
828
829
1.67k
  if (model_mode_is_used(cfg->cmp_mode)) {
830
1.29k
    up_model_buf = get_collection_data(cfg->updated_model_buf);
831
1.29k
    model = model_buf[0];
832
1.29k
    next_model_p = &model_buf[1];
833
1.29k
  } else {
834
371
    up_model_buf = NULL;
835
371
    memset(&model, 0, sizeof(model));
836
371
    next_model_p = data_buf;
837
371
  }
838
839
1.67k
  configure_decoder_setup(&setup_exp_flags, dec, cfg->cmp_mode, cfg->cmp_par_exp_flags, cfg->spill_exp_flags,
840
1.67k
        cfg->round, MAX_USED_BITS.l_exp_flags);
841
1.67k
  configure_decoder_setup(&setup_fx, dec, cfg->cmp_mode, cfg->cmp_par_fx, cfg->spill_fx,
842
1.67k
        cfg->round, MAX_USED_BITS.l_fx);
843
1.67k
  configure_decoder_setup(&setup_fx_var, dec, cfg->cmp_mode, cfg->cmp_par_fx_cob_variance, cfg->spill_fx_cob_variance,
844
1.67k
        cfg->round, MAX_USED_BITS.l_fx_cob_variance);
845
846
2.47k
  for (i = 0; ; i++) {
847
2.47k
    err = decode_value(&setup_exp_flags, &decoded_value, model.exp_flags);
848
2.47k
    if (err)
849
0
      break;
850
2.47k
    data_buf[i].exp_flags = decoded_value;
851
852
2.47k
    err = decode_value(&setup_fx, &decoded_value, model.fx);
853
2.47k
    if (err)
854
0
      break;
855
2.47k
    data_buf[i].fx = decoded_value;
856
857
2.47k
    err = decode_value(&setup_fx_var, &decoded_value, model.fx_variance);
858
2.47k
    if (err)
859
0
      break;
860
2.47k
    data_buf[i].fx_variance = decoded_value;
861
862
2.47k
    if (up_model_buf) {
863
1.38k
      up_model_buf[i].exp_flags = cmp_up_model32(data_buf[i].exp_flags, model.exp_flags,
864
1.38k
                   cfg->model_value, setup_exp_flags.lossy_par);
865
1.38k
      up_model_buf[i].fx = cmp_up_model(data_buf[i].fx, model.fx,
866
1.38k
                cfg->model_value, setup_fx.lossy_par);
867
1.38k
      up_model_buf[i].fx_variance = cmp_up_model(data_buf[i].fx_variance, model.fx_variance,
868
1.38k
                   cfg->model_value, setup_fx_var.lossy_par);
869
1.38k
    }
870
871
2.47k
    if (i >= cfg->samples-1)
872
1.67k
      break;
873
874
801
    model = next_model_p[i];
875
801
  }
876
1.67k
  return err;
877
1.67k
}
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
309
{
891
309
  size_t i;
892
309
  int err;
893
309
  uint32_t decoded_value;
894
309
  struct decoder_setup setup_exp_flags, setup_fx, setup_efx, setup_fx_var;
895
309
  struct l_fx_efx *data_buf = get_collection_data(cfg->dst);
896
309
  const struct l_fx_efx *model_buf = get_collection_data_const(cfg->model_buf);
897
309
  struct l_fx_efx *up_model_buf;
898
309
  const struct l_fx_efx *next_model_p;
899
309
  struct l_fx_efx model;
900
901
309
  if (model_mode_is_used(cfg->cmp_mode)) {
902
228
    up_model_buf = get_collection_data(cfg->updated_model_buf);
903
228
    model = model_buf[0];
904
228
    next_model_p = &model_buf[1];
905
228
  } else {
906
81
    up_model_buf = NULL;
907
81
    memset(&model, 0, sizeof(model));
908
81
    next_model_p = data_buf;
909
81
  }
910
911
309
  configure_decoder_setup(&setup_exp_flags, dec, cfg->cmp_mode, cfg->cmp_par_exp_flags, cfg->spill_exp_flags,
912
309
        cfg->round, MAX_USED_BITS.l_exp_flags);
913
309
  configure_decoder_setup(&setup_fx, dec, cfg->cmp_mode, cfg->cmp_par_fx, cfg->spill_fx,
914
309
        cfg->round, MAX_USED_BITS.l_fx);
915
309
  configure_decoder_setup(&setup_efx, dec, cfg->cmp_mode, cfg->cmp_par_efx, cfg->spill_efx,
916
309
        cfg->round, MAX_USED_BITS.l_efx);
917
309
  configure_decoder_setup(&setup_fx_var, dec, cfg->cmp_mode, cfg->cmp_par_fx_cob_variance, cfg->spill_fx_cob_variance,
918
309
        cfg->round, MAX_USED_BITS.l_fx_cob_variance);
919
920
1.21k
  for (i = 0; ; i++) {
921
1.21k
    err = decode_value(&setup_exp_flags, &decoded_value, model.exp_flags);
922
1.21k
    if (err)
923
0
      break;
924
1.21k
    data_buf[i].exp_flags = decoded_value;
925
926
1.21k
    err = decode_value(&setup_fx, &decoded_value, model.fx);
927
1.21k
    if (err)
928
0
      break;
929
1.21k
    data_buf[i].fx = decoded_value;
930
931
1.21k
    err = decode_value(&setup_efx, &decoded_value, model.efx);
932
1.21k
    if (err)
933
0
      break;
934
1.21k
    data_buf[i].efx = decoded_value;
935
936
1.21k
    err = decode_value(&setup_fx_var, &decoded_value, model.fx_variance);
937
1.21k
    if (err)
938
0
      break;
939
1.21k
    data_buf[i].fx_variance = decoded_value;
940
941
1.21k
    if (up_model_buf) {
942
333
      up_model_buf[i].exp_flags = cmp_up_model32(data_buf[i].exp_flags, model.exp_flags,
943
333
                   cfg->model_value, setup_exp_flags.lossy_par);
944
333
      up_model_buf[i].fx = cmp_up_model(data_buf[i].fx, model.fx,
945
333
                cfg->model_value, setup_fx.lossy_par);
946
333
      up_model_buf[i].efx = cmp_up_model(data_buf[i].efx, model.efx,
947
333
                 cfg->model_value, setup_efx.lossy_par);
948
333
      up_model_buf[i].fx_variance = cmp_up_model(data_buf[i].fx_variance, model.fx_variance,
949
333
                   cfg->model_value, setup_fx_var.lossy_par);
950
333
    }
951
952
1.21k
    if (i >= cfg->samples-1)
953
309
      break;
954
955
901
    model = next_model_p[i];
956
901
  }
957
309
  return err;
958
309
}
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
273
{
972
273
  size_t i;
973
273
  int err;
974
273
  uint32_t decoded_value;
975
273
  struct decoder_setup setup_exp_flags, setup_fx, setup_ncob,
976
273
           setup_fx_var, setup_cob_var;
977
273
  struct l_fx_ncob *data_buf = get_collection_data(cfg->dst);
978
273
  const struct l_fx_ncob *model_buf = get_collection_data_const(cfg->model_buf);
979
273
  struct l_fx_ncob *up_model_buf;
980
273
  const struct l_fx_ncob *next_model_p;
981
273
  struct l_fx_ncob model;
982
983
273
  if (model_mode_is_used(cfg->cmp_mode)) {
984
54
    up_model_buf = get_collection_data(cfg->updated_model_buf);
985
54
    model = model_buf[0];
986
54
    next_model_p = &model_buf[1];
987
219
  } else {
988
219
    up_model_buf = NULL;
989
219
    memset(&model, 0, sizeof(model));
990
219
    next_model_p = data_buf;
991
219
  }
992
993
273
  configure_decoder_setup(&setup_exp_flags, dec, cfg->cmp_mode, cfg->cmp_par_exp_flags, cfg->spill_exp_flags,
994
273
        cfg->round, MAX_USED_BITS.l_exp_flags);
995
273
  configure_decoder_setup(&setup_fx, dec, cfg->cmp_mode, cfg->cmp_par_fx, cfg->spill_fx,
996
273
        cfg->round, MAX_USED_BITS.l_fx);
997
273
  configure_decoder_setup(&setup_ncob, dec, cfg->cmp_mode, cfg->cmp_par_ncob, cfg->spill_ncob,
998
273
        cfg->round, MAX_USED_BITS.l_ncob);
999
273
  configure_decoder_setup(&setup_fx_var, dec, cfg->cmp_mode, cfg->cmp_par_fx_cob_variance, cfg->spill_fx_cob_variance,
1000
273
        cfg->round, MAX_USED_BITS.l_fx_cob_variance);
1001
273
  configure_decoder_setup(&setup_cob_var, dec, cfg->cmp_mode, cfg->cmp_par_fx_cob_variance, cfg->spill_fx_cob_variance,
1002
273
        cfg->round, MAX_USED_BITS.l_fx_cob_variance);
1003
1004
1.66k
  for (i = 0; ; i++) {
1005
1.66k
    err = decode_value(&setup_exp_flags, &decoded_value, model.exp_flags);
1006
1.66k
    if (err)
1007
0
      break;
1008
1.66k
    data_buf[i].exp_flags = decoded_value;
1009
1010
1.66k
    err = decode_value(&setup_fx, &decoded_value, model.fx);
1011
1.66k
    if (err)
1012
0
      break;
1013
1.66k
    data_buf[i].fx = decoded_value;
1014
1015
1.66k
    err = decode_value(&setup_ncob, &decoded_value, model.ncob_x);
1016
1.66k
    if (err)
1017
0
      break;
1018
1.66k
    data_buf[i].ncob_x = decoded_value;
1019
1020
1.66k
    err = decode_value(&setup_ncob, &decoded_value, model.ncob_y);
1021
1.66k
    if (err)
1022
0
      break;
1023
1.66k
    data_buf[i].ncob_y = decoded_value;
1024
1025
1.66k
    err = decode_value(&setup_fx_var, &decoded_value, model.fx_variance);
1026
1.66k
    if (err)
1027
0
      break;
1028
1.66k
    data_buf[i].fx_variance = decoded_value;
1029
1030
1.66k
    err = decode_value(&setup_cob_var, &decoded_value, model.cob_x_variance);
1031
1.66k
    if (err)
1032
0
      break;
1033
1.66k
    data_buf[i].cob_x_variance = decoded_value;
1034
1035
1.66k
    err = decode_value(&setup_cob_var, &decoded_value, model.cob_y_variance);
1036
1.66k
    if (err)
1037
0
      break;
1038
1.66k
    data_buf[i].cob_y_variance = decoded_value;
1039
1040
1.66k
    if (up_model_buf) {
1041
180
      up_model_buf[i].exp_flags = cmp_up_model32(data_buf[i].exp_flags, model.exp_flags,
1042
180
        cfg->model_value, setup_exp_flags.lossy_par);
1043
180
      up_model_buf[i].fx = cmp_up_model(data_buf[i].fx, model.fx,
1044
180
        cfg->model_value, setup_fx.lossy_par);
1045
180
      up_model_buf[i].ncob_x = cmp_up_model(data_buf[i].ncob_x, model.ncob_x,
1046
180
        cfg->model_value, setup_ncob.lossy_par);
1047
180
      up_model_buf[i].ncob_y = cmp_up_model(data_buf[i].ncob_y, model.ncob_y,
1048
180
        cfg->model_value, setup_ncob.lossy_par);
1049
180
      up_model_buf[i].fx_variance = cmp_up_model(data_buf[i].fx_variance, model.fx_variance,
1050
180
        cfg->model_value, setup_fx_var.lossy_par);
1051
180
      up_model_buf[i].cob_x_variance = cmp_up_model(data_buf[i].cob_x_variance, model.cob_x_variance,
1052
180
        cfg->model_value, setup_cob_var.lossy_par);
1053
180
      up_model_buf[i].cob_y_variance = cmp_up_model(data_buf[i].cob_y_variance, model.cob_y_variance,
1054
180
        cfg->model_value, setup_cob_var.lossy_par);
1055
180
    }
1056
1057
1.66k
    if (i >= cfg->samples-1)
1058
273
      break;
1059
1060
1.39k
    model = next_model_p[i];
1061
1.39k
  }
1062
273
  return err;
1063
273
}
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
741
{
1077
741
  size_t i;
1078
741
  int err;
1079
741
  uint32_t decoded_value;
1080
741
  struct decoder_setup setup_exp_flags, setup_fx, setup_ncob, setup_efx,
1081
741
           setup_ecob, setup_fx_var, setup_cob_var;
1082
741
  struct l_fx_efx_ncob_ecob *data_buf = get_collection_data(cfg->dst);
1083
741
  const struct l_fx_efx_ncob_ecob *model_buf = get_collection_data_const(cfg->model_buf);
1084
741
  struct l_fx_efx_ncob_ecob *up_model_buf;
1085
741
  const struct l_fx_efx_ncob_ecob *next_model_p;
1086
741
  struct l_fx_efx_ncob_ecob model;
1087
1088
741
  if (model_mode_is_used(cfg->cmp_mode)) {
1089
206
    up_model_buf = get_collection_data(cfg->updated_model_buf);
1090
206
    model = model_buf[0];
1091
206
    next_model_p = &model_buf[1];
1092
535
  } else {
1093
535
    up_model_buf = NULL;
1094
535
    memset(&model, 0, sizeof(model));
1095
535
    next_model_p = data_buf;
1096
535
  }
1097
1098
741
  configure_decoder_setup(&setup_exp_flags, dec, cfg->cmp_mode, cfg->cmp_par_exp_flags, cfg->spill_exp_flags,
1099
741
        cfg->round, MAX_USED_BITS.l_exp_flags);
1100
741
  configure_decoder_setup(&setup_fx, dec, cfg->cmp_mode, cfg->cmp_par_fx, cfg->spill_fx,
1101
741
        cfg->round, MAX_USED_BITS.l_fx);
1102
741
  configure_decoder_setup(&setup_ncob, dec, cfg->cmp_mode, cfg->cmp_par_ncob, cfg->spill_ncob,
1103
741
        cfg->round, MAX_USED_BITS.l_ncob);
1104
741
  configure_decoder_setup(&setup_efx, dec, cfg->cmp_mode, cfg->cmp_par_efx, cfg->spill_efx,
1105
741
        cfg->round, MAX_USED_BITS.l_efx);
1106
741
  configure_decoder_setup(&setup_ecob, dec, cfg->cmp_mode, cfg->cmp_par_ecob, cfg->spill_ecob,
1107
741
        cfg->round, MAX_USED_BITS.l_ecob);
1108
741
  configure_decoder_setup(&setup_fx_var, dec, cfg->cmp_mode, cfg->cmp_par_fx_cob_variance, cfg->spill_fx_cob_variance,
1109
741
        cfg->round, MAX_USED_BITS.l_fx_cob_variance);
1110
741
  configure_decoder_setup(&setup_cob_var, dec, cfg->cmp_mode, cfg->cmp_par_fx_cob_variance, cfg->spill_fx_cob_variance,
1111
741
        cfg->round, MAX_USED_BITS.l_fx_cob_variance);
1112
1113
1.26k
  for (i = 0; ; i++) {
1114
1.26k
    err = decode_value(&setup_exp_flags, &decoded_value, model.exp_flags);
1115
1.26k
    if (err)
1116
0
      break;
1117
1.26k
    data_buf[i].exp_flags = decoded_value;
1118
1119
1.26k
    err = decode_value(&setup_fx, &decoded_value, model.fx);
1120
1.26k
    if (err)
1121
0
      break;
1122
1.26k
    data_buf[i].fx = decoded_value;
1123
1124
1.26k
    err = decode_value(&setup_ncob, &decoded_value, model.ncob_x);
1125
1.26k
    if (err)
1126
0
      break;
1127
1.26k
    data_buf[i].ncob_x = decoded_value;
1128
1129
1.26k
    err = decode_value(&setup_ncob, &decoded_value, model.ncob_y);
1130
1.26k
    if (err)
1131
0
      break;
1132
1.26k
    data_buf[i].ncob_y = decoded_value;
1133
1134
1.26k
    err = decode_value(&setup_efx, &decoded_value, model.efx);
1135
1.26k
    if (err)
1136
0
      break;
1137
1.26k
    data_buf[i].efx = decoded_value;
1138
1139
1.26k
    err = decode_value(&setup_ecob, &decoded_value, model.ecob_x);
1140
1.26k
    if (err)
1141
0
      break;
1142
1.26k
    data_buf[i].ecob_x = decoded_value;
1143
1144
1.26k
    err = decode_value(&setup_ecob, &decoded_value, model.ecob_y);
1145
1.26k
    if (err)
1146
0
      break;
1147
1.26k
    data_buf[i].ecob_y = decoded_value;
1148
1149
1.26k
    err = decode_value(&setup_fx_var, &decoded_value, model.fx_variance);
1150
1.26k
    if (err)
1151
0
      break;
1152
1.26k
    data_buf[i].fx_variance = decoded_value;
1153
1154
1.26k
    err = decode_value(&setup_cob_var, &decoded_value, model.cob_x_variance);
1155
1.26k
    if (err)
1156
0
      break;
1157
1.26k
    data_buf[i].cob_x_variance = decoded_value;
1158
1159
1.26k
    err = decode_value(&setup_cob_var, &decoded_value, model.cob_y_variance);
1160
1.26k
    if (err)
1161
0
      break;
1162
1.26k
    data_buf[i].cob_y_variance = decoded_value;
1163
1164
1.26k
    if (up_model_buf) {
1165
396
      up_model_buf[i].exp_flags = cmp_up_model32(data_buf[i].exp_flags, model.exp_flags,
1166
396
        cfg->model_value, setup_exp_flags.lossy_par);
1167
396
      up_model_buf[i].fx = cmp_up_model(data_buf[i].fx, model.fx,
1168
396
        cfg->model_value, setup_fx.lossy_par);
1169
396
      up_model_buf[i].ncob_x = cmp_up_model(data_buf[i].ncob_x, model.ncob_x,
1170
396
        cfg->model_value, setup_ncob.lossy_par);
1171
396
      up_model_buf[i].ncob_y = cmp_up_model(data_buf[i].ncob_y, model.ncob_y,
1172
396
        cfg->model_value, setup_ncob.lossy_par);
1173
396
      up_model_buf[i].efx = cmp_up_model(data_buf[i].efx, model.efx,
1174
396
        cfg->model_value, setup_efx.lossy_par);
1175
396
      up_model_buf[i].ecob_x = cmp_up_model(data_buf[i].ecob_x, model.ecob_x,
1176
396
        cfg->model_value, setup_ecob.lossy_par);
1177
396
      up_model_buf[i].ecob_y = cmp_up_model(data_buf[i].ecob_y, model.ecob_y,
1178
396
        cfg->model_value, setup_ecob.lossy_par);
1179
396
      up_model_buf[i].fx_variance = cmp_up_model(data_buf[i].fx_variance, model.fx_variance,
1180
396
        cfg->model_value, setup_fx_var.lossy_par);
1181
396
      up_model_buf[i].cob_x_variance = cmp_up_model(data_buf[i].cob_x_variance, model.cob_x_variance,
1182
396
        cfg->model_value, setup_cob_var.lossy_par);
1183
396
      up_model_buf[i].cob_y_variance = cmp_up_model(data_buf[i].cob_y_variance, model.cob_y_variance,
1184
396
        cfg->model_value, setup_cob_var.lossy_par);
1185
396
    }
1186
1187
1.26k
    if (i >= cfg->samples-1)
1188
741
      break;
1189
1190
522
    model = next_model_p[i];
1191
522
  }
1192
741
  return err;
1193
741
}
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
477
{
1207
477
  size_t i;
1208
477
  int err;
1209
477
  uint32_t decoded_value;
1210
477
  struct decoder_setup setup_mean, setup_var;
1211
477
  struct offset *data_buf = get_collection_data(cfg->dst);
1212
477
  const struct offset *model_buf = get_collection_data_const(cfg->model_buf);
1213
477
  struct offset *up_model_buf;
1214
477
  const struct offset *next_model_p;
1215
477
  struct offset model;
1216
1217
477
  if (model_mode_is_used(cfg->cmp_mode)) {
1218
243
    up_model_buf = get_collection_data(cfg->updated_model_buf);
1219
243
    model = model_buf[0];
1220
243
    next_model_p = &model_buf[1];
1221
243
  } else {
1222
234
    up_model_buf = NULL;
1223
234
    memset(&model, 0, sizeof(model));
1224
234
    next_model_p = data_buf;
1225
234
  }
1226
1227
477
  {
1228
477
    unsigned int mean_bits_used, variance_bits_used;
1229
1230
477
    switch (cfg->data_type) {
1231
228
    case DATA_TYPE_F_CAM_OFFSET:
1232
228
      mean_bits_used = MAX_USED_BITS.fc_offset_mean;
1233
228
      variance_bits_used = MAX_USED_BITS.fc_offset_variance;
1234
228
      break;
1235
249
    case DATA_TYPE_OFFSET:
1236
249
    default:
1237
249
      mean_bits_used = MAX_USED_BITS.nc_offset_mean;
1238
249
      variance_bits_used = MAX_USED_BITS.nc_offset_variance;
1239
249
      break;
1240
477
    }
1241
477
    configure_decoder_setup(&setup_mean, dec, cfg->cmp_mode, cfg->cmp_par_offset_mean, cfg->spill_offset_mean,
1242
477
          cfg->round, mean_bits_used);
1243
1244
477
    configure_decoder_setup(&setup_var, dec, cfg->cmp_mode, cfg->cmp_par_offset_variance, cfg->spill_offset_variance,
1245
477
          cfg->round, variance_bits_used);
1246
1247
477
  }
1248
1249
2.68k
  for (i = 0; ; i++) {
1250
2.68k
    err = decode_value(&setup_mean, &decoded_value, model.mean);
1251
2.68k
    if (err)
1252
0
      break;
1253
2.68k
    data_buf[i].mean = decoded_value;
1254
1255
2.68k
    err = decode_value(&setup_var, &decoded_value, model.variance);
1256
2.68k
    if (err)
1257
0
      break;
1258
2.68k
    data_buf[i].variance = decoded_value;
1259
1260
2.68k
    if (up_model_buf) {
1261
822
      up_model_buf[i].mean = cmp_up_model(data_buf[i].mean,
1262
822
        model.mean, cfg->model_value, setup_mean.lossy_par);
1263
822
      up_model_buf[i].variance = cmp_up_model(data_buf[i].variance,
1264
822
        model.variance, cfg->model_value, setup_var.lossy_par);
1265
822
    }
1266
1267
2.68k
    if (i >= cfg->samples-1)
1268
477
      break;
1269
1270
2.20k
    model = next_model_p[i];
1271
2.20k
  }
1272
477
  return err;
1273
477
}
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
890
{
1287
890
  size_t i;
1288
890
  int err;
1289
890
  uint32_t decoded_value;
1290
890
  struct decoder_setup setup_mean, setup_var, setup_pix;
1291
890
  struct background *data_buf = get_collection_data(cfg->dst);
1292
890
  const struct background *model_buf = get_collection_data_const(cfg->model_buf);
1293
890
  struct background *up_model_buf;
1294
890
  const struct background *next_model_p;
1295
890
  struct background model;
1296
1297
890
  if (model_mode_is_used(cfg->cmp_mode)) {
1298
374
    up_model_buf = get_collection_data(cfg->updated_model_buf);
1299
374
    model = model_buf[0];
1300
374
    next_model_p = &model_buf[1];
1301
516
  } else {
1302
516
    up_model_buf = NULL;
1303
516
    memset(&model, 0, sizeof(model));
1304
516
    next_model_p = data_buf;
1305
516
  }
1306
890
  {
1307
890
    unsigned int mean_used_bits, variance_used_bits, outlier_pixels_used_bits;
1308
1309
890
    switch (cfg->data_type) {
1310
818
    case DATA_TYPE_F_CAM_BACKGROUND:
1311
818
      mean_used_bits = MAX_USED_BITS.fc_background_mean;
1312
818
      variance_used_bits = MAX_USED_BITS.fc_background_variance;
1313
818
      outlier_pixels_used_bits = MAX_USED_BITS.fc_background_outlier_pixels;
1314
818
      break;
1315
72
    case DATA_TYPE_BACKGROUND:
1316
72
    default:
1317
72
      mean_used_bits = MAX_USED_BITS.nc_background_mean;
1318
72
      variance_used_bits = MAX_USED_BITS.nc_background_variance;
1319
72
      outlier_pixels_used_bits = MAX_USED_BITS.nc_background_outlier_pixels;
1320
72
      break;
1321
890
    }
1322
1323
890
    configure_decoder_setup(&setup_mean, dec, cfg->cmp_mode, cfg->cmp_par_background_mean, cfg->spill_background_mean,
1324
890
          cfg->round, mean_used_bits);
1325
1326
890
    configure_decoder_setup(&setup_var, dec, cfg->cmp_mode, cfg->cmp_par_background_variance, cfg->spill_background_variance,
1327
890
          cfg->round, variance_used_bits);
1328
1329
890
    configure_decoder_setup(&setup_pix, dec, cfg->cmp_mode, cfg->cmp_par_background_pixels_error, cfg->spill_background_pixels_error,
1330
890
          cfg->round, outlier_pixels_used_bits);
1331
1332
890
  }
1333
1334
1.63k
  for (i = 0; ; i++) {
1335
1.63k
    err = decode_value(&setup_mean, &decoded_value, model.mean);
1336
1.63k
    if (err)
1337
0
      break;
1338
1.63k
    data_buf[i].mean = decoded_value;
1339
1340
1.63k
    err = decode_value(&setup_var, &decoded_value, model.variance);
1341
1.63k
    if (err)
1342
0
      break;
1343
1.63k
    data_buf[i].variance = decoded_value;
1344
1345
1.63k
    err = decode_value(&setup_pix, &decoded_value, model.outlier_pixels);
1346
1.63k
    if (err)
1347
0
      break;
1348
1.63k
    data_buf[i].outlier_pixels = (__typeof__(data_buf[i].outlier_pixels))decoded_value;
1349
1350
1.63k
    if (up_model_buf) {
1351
416
      up_model_buf[i].mean = cmp_up_model(data_buf[i].mean,
1352
416
        model.mean, cfg->model_value, setup_mean.lossy_par);
1353
416
      up_model_buf[i].variance = cmp_up_model(data_buf[i].variance,
1354
416
        model.variance, cfg->model_value, setup_var.lossy_par);
1355
416
      up_model_buf[i].outlier_pixels = cmp_up_model(data_buf[i].outlier_pixels,
1356
416
        model.outlier_pixels, cfg->model_value, setup_pix.lossy_par);
1357
416
    }
1358
1359
1.63k
    if (i >= cfg->samples-1)
1360
890
      break;
1361
1362
746
    model = next_model_p[i];
1363
746
  }
1364
890
  return err;
1365
890
}
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
353
{
1379
353
  size_t i;
1380
353
  int err;
1381
353
  uint32_t decoded_value;
1382
353
  struct decoder_setup setup_mean, setup_var, setup_pix;
1383
353
  struct smearing *data_buf = get_collection_data(cfg->dst);
1384
353
  const struct smearing *model_buf = get_collection_data_const(cfg->model_buf);
1385
353
  struct smearing *up_model_buf;
1386
353
  const struct smearing *next_model_p;
1387
353
  struct smearing model;
1388
1389
353
  if (model_mode_is_used(cfg->cmp_mode)) {
1390
309
    up_model_buf = get_collection_data(cfg->updated_model_buf);
1391
309
    model = model_buf[0];
1392
309
    next_model_p = &model_buf[1];
1393
309
  } else {
1394
44
    up_model_buf = NULL;
1395
44
    memset(&model, 0, sizeof(model));
1396
44
    next_model_p = data_buf;
1397
44
  }
1398
1399
353
  configure_decoder_setup(&setup_mean, dec, cfg->cmp_mode, cfg->cmp_par_smearing_mean, cfg->spill_smearing_mean,
1400
353
        cfg->round, MAX_USED_BITS.smearing_mean);
1401
353
  configure_decoder_setup(&setup_var, dec, cfg->cmp_mode, cfg->cmp_par_smearing_variance, cfg->spill_smearing_variance,
1402
353
        cfg->round, MAX_USED_BITS.smearing_variance_mean);
1403
353
  configure_decoder_setup(&setup_pix, dec, cfg->cmp_mode, cfg->cmp_par_smearing_pixels_error, cfg->spill_smearing_pixels_error,
1404
353
        cfg->round, MAX_USED_BITS.smearing_outlier_pixels);
1405
1406
1.39k
  for (i = 0; ; i++) {
1407
1.39k
    err = decode_value(&setup_mean, &decoded_value, model.mean);
1408
1.39k
    if (err)
1409
0
      break;
1410
1.39k
    data_buf[i].mean = decoded_value;
1411
1412
1.39k
    err = decode_value(&setup_var, &decoded_value, model.variance_mean);
1413
1.39k
    if (err)
1414
0
      break;
1415
1.39k
    data_buf[i].variance_mean = (__typeof__(data_buf[i].variance_mean))decoded_value;
1416
1417
1.39k
    err = decode_value(&setup_pix, &decoded_value, model.outlier_pixels);
1418
1.39k
    if (err)
1419
0
      break;
1420
1.39k
    data_buf[i].outlier_pixels = (__typeof__(data_buf[i].outlier_pixels))decoded_value;
1421
1422
1.39k
    if (up_model_buf) {
1423
605
      up_model_buf[i].mean = cmp_up_model(data_buf[i].mean,
1424
605
        model.mean, cfg->model_value, setup_mean.lossy_par);
1425
605
      up_model_buf[i].variance_mean = cmp_up_model(data_buf[i].variance_mean,
1426
605
        model.variance_mean, cfg->model_value, setup_var.lossy_par);
1427
605
      up_model_buf[i].outlier_pixels = cmp_up_model(data_buf[i].outlier_pixels,
1428
605
        model.outlier_pixels, cfg->model_value, setup_pix.lossy_par);
1429
605
    }
1430
1431
1.39k
    if (i >= cfg->samples-1)
1432
353
      break;
1433
1434
1.04k
    model = next_model_p[i];
1435
1.04k
  }
1436
353
  return err;
1437
353
}
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
6.66k
{
1454
6.66k
  if (cfg->stream_size < COLLECTION_HDR_SIZE)
1455
0
    return -1;
1456
1457
6.66k
  if (cfg->src) {
1458
6.66k
    if (cfg->dst)
1459
6.66k
      memcpy(cfg->dst, cfg->src, COLLECTION_HDR_SIZE);
1460
1461
6.66k
    if (model_mode_is_used(cfg->cmp_mode) && cfg->updated_model_buf)
1462
2.61k
      memcpy(cfg->updated_model_buf, cfg->src, COLLECTION_HDR_SIZE);
1463
6.66k
  }
1464
6.66k
  return COLLECTION_HDR_SIZE;
1465
6.66k
}
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
71.0k
{
1481
71.0k
  int err;
1482
71.0k
  uint32_t data_size;
1483
1484
71.0k
  assert(decmp_type == ICU_DECOMRESSION || decmp_type == RDCU_DECOMPRESSION);
1485
1486
71.0k
  if (!cfg)
1487
0
    return -1;
1488
1489
71.0k
  if (!cfg->src)
1490
0
    return -1;
1491
1492
71.0k
  if (cmp_cfg_gen_par_is_invalid(cfg))
1493
0
    return -1;
1494
1495
71.0k
  if (cmp_imagette_data_type_is_used(cfg->data_type)) {
1496
12.4k
    if (cmp_cfg_imagette_is_invalid(cfg))
1497
0
      return -1;
1498
58.5k
  } else if (cmp_fx_cob_data_type_is_used(cfg->data_type)) {
1499
48.6k
    if (cmp_cfg_fx_cob_is_invalid(cfg))
1500
0
      return -1;
1501
48.6k
  } else if (cmp_aux_data_type_is_used(cfg->data_type)) {
1502
9.95k
    if (cmp_cfg_aux_is_invalid(cfg))
1503
0
      return -1;
1504
9.95k
  } else {
1505
0
    return -1;
1506
0
  }
1507
1508
71.0k
  if (model_mode_is_used(cfg->cmp_mode))
1509
7.47k
    if (!cfg->model_buf) /* we need a model for model compression */
1510
0
      return -1;
1511
1512
71.0k
  data_size = cfg->samples * (uint32_t)size_of_a_sample(cfg->data_type);
1513
71.0k
  if (decmp_type == ICU_DECOMRESSION)
1514
71.0k
    data_size += COLLECTION_HDR_SIZE;
1515
1516
71.0k
  if (cfg->cmp_mode == CMP_MODE_RAW) {
1517
57.2k
    if (cfg->dst) {
1518
11.2k
      memcpy(cfg->dst, cfg->src, data_size);
1519
11.2k
      switch (decmp_type) {
1520
11.2k
      case ICU_DECOMRESSION:
1521
11.2k
        if (be_to_cpu_chunk(cfg->dst, data_size))
1522
0
          return -1;
1523
11.2k
        break;
1524
11.2k
      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.2k
      }
1530
11.2k
    }
1531
57.2k
    err = 0;
1532
1533
57.2k
  } else {
1534
13.8k
    struct bit_decoder dec;
1535
13.8k
    int hdr_size = 0;
1536
1537
13.8k
    if (!cfg->dst)
1538
7.18k
      return (int)data_size;
1539
1540
6.66k
    if (decmp_type == ICU_DECOMRESSION) {
1541
6.66k
      hdr_size = decompress_collection_hdr(cfg);
1542
6.66k
      if (hdr_size < 0)
1543
0
        return -1;
1544
6.66k
    }
1545
1546
6.66k
    bit_init_decoder(&dec, (const uint8_t *)cfg->src+hdr_size,
1547
6.66k
         cfg->stream_size-(uint32_t)hdr_size);
1548
1549
6.66k
    switch (cfg->data_type) {
1550
356
    case DATA_TYPE_IMAGETTE:
1551
356
    case DATA_TYPE_IMAGETTE_ADAPTIVE:
1552
583
    case DATA_TYPE_SAT_IMAGETTE:
1553
583
    case DATA_TYPE_SAT_IMAGETTE_ADAPTIVE:
1554
819
    case DATA_TYPE_F_CAM_IMAGETTE:
1555
819
    case DATA_TYPE_F_CAM_IMAGETTE_ADAPTIVE:
1556
819
      err = decompress_imagette(cfg, &dec, decmp_type);
1557
819
      break;
1558
212
    case DATA_TYPE_S_FX:
1559
212
      err = decompress_s_fx(cfg, &dec);
1560
212
      break;
1561
406
    case DATA_TYPE_S_FX_EFX:
1562
406
      err = decompress_s_fx_efx(cfg, &dec);
1563
406
      break;
1564
261
    case DATA_TYPE_S_FX_NCOB:
1565
261
      err = decompress_s_fx_ncob(cfg, &dec);
1566
261
      break;
1567
253
    case DATA_TYPE_S_FX_EFX_NCOB_ECOB:
1568
253
      err = decompress_s_fx_efx_ncob_ecob(cfg, &dec);
1569
253
      break;
1570
1571
1.67k
    case DATA_TYPE_L_FX:
1572
1.67k
      err = decompress_l_fx(cfg, &dec);
1573
1.67k
      break;
1574
309
    case DATA_TYPE_L_FX_EFX:
1575
309
      err = decompress_l_fx_efx(cfg, &dec);
1576
309
      break;
1577
273
    case DATA_TYPE_L_FX_NCOB:
1578
273
      err = decompress_l_fx_ncob(cfg, &dec);
1579
273
      break;
1580
741
    case DATA_TYPE_L_FX_EFX_NCOB_ECOB:
1581
741
      err = decompress_l_fx_efx_ncob_ecob(cfg, &dec);
1582
741
      break;
1583
1584
249
    case DATA_TYPE_OFFSET:
1585
477
    case DATA_TYPE_F_CAM_OFFSET:
1586
477
      err = decompress_offset(cfg, &dec);
1587
477
      break;
1588
72
    case DATA_TYPE_BACKGROUND:
1589
890
    case DATA_TYPE_F_CAM_BACKGROUND:
1590
890
      err = decompress_background(cfg, &dec);
1591
890
      break;
1592
353
    case DATA_TYPE_SMEARING:
1593
353
      err = decompress_smearing(cfg, &dec);
1594
353
      break;
1595
1596
0
    case DATA_TYPE_F_FX:
1597
0
    case DATA_TYPE_F_FX_EFX:
1598
0
    case DATA_TYPE_F_FX_NCOB:
1599
0
    case DATA_TYPE_F_FX_EFX_NCOB_ECOB:
1600
0
    case DATA_TYPE_CHUNK:
1601
0
    case DATA_TYPE_UNKNOWN:
1602
0
    default:
1603
0
      err = -1;
1604
0
      debug_print("Error: Compressed data type not supported.");
1605
0
      break;
1606
6.66k
    }
1607
1608
6.66k
    switch (bit_refill(&dec)) {
1609
0
    case BIT_OVERFLOW:
1610
0
      if (dec.cursor == dec.limit_ptr)
1611
0
        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
0
      else
1613
0
        debug_print("Error: Data consistency check failed. %s", please_check_str);
1614
0
      break;
1615
4.00k
    case BIT_END_OF_BUFFER:
1616
      /* check if non consumed bits are zero */
1617
4.00k
      { unsigned int bits_not_read = sizeof(dec.bit_container)*8 - dec.bits_consumed;
1618
1619
4.00k
        if (bits_not_read > 57) /* can not read more than 57 bits */
1620
0
          bits_not_read = 57;
1621
1622
4.00k
        if (bit_read_bits(&dec, bits_not_read ) == 0)
1623
4.00k
          break;
1624
4.00k
      } /* fall through */
1625
0
    case BIT_UNFINISHED:
1626
0
      debug_print("Warning: Not all compressed data are processed.");
1627
0
      break;
1628
6.66k
    }
1629
6.66k
  }
1630
63.8k
  if (err)
1631
0
    return -1;
1632
1633
63.8k
  return (int)data_size;
1634
63.8k
}
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
2.62k
{
1649
2.62k
  uint32_t org_size;
1650
1651
2.62k
  if (!cfg)
1652
0
    return -1;
1653
1654
2.62k
  cfg->data_type = cmp_ent_get_data_type(ent);
1655
  /* the compression entity data type field only supports imagette or chunk data types */
1656
2.62k
  if (cfg->data_type != DATA_TYPE_CHUNK && !rdcu_supported_data_type_is_used(cfg->data_type)) {
1657
0
    debug_print("Error: Compression entity data type not supported.");
1658
0
    return -1;
1659
0
  }
1660
1661
2.62k
  cfg->cmp_mode = cmp_ent_get_cmp_mode(ent);
1662
2.62k
  if (cmp_ent_get_data_type_raw_bit(ent) != (cfg->cmp_mode == CMP_MODE_RAW)) {
1663
0
    debug_print("Error: The entity's raw data bit does not match up with the compression mode.");
1664
0
    return -1;
1665
0
  }
1666
2.62k
  cfg->model_value = cmp_ent_get_model_value(ent);
1667
2.62k
  cfg->round = cmp_ent_get_lossy_cmp_par(ent);
1668
2.62k
  cfg->stream_size = cmp_ent_get_cmp_data_size(ent);
1669
1670
2.62k
  if (cmp_cfg_gen_par_is_invalid(cfg))
1671
0
    return -1;
1672
1673
2.62k
  org_size = cmp_ent_get_original_size(ent);
1674
2.62k
  if (cfg->data_type == DATA_TYPE_CHUNK) {
1675
2.62k
    cfg->samples = 0;
1676
2.62k
    if ((cfg->stream_size < (COLLECTION_HDR_SIZE + CMP_COLLECTION_FILD_SIZE) && (cfg->cmp_mode != CMP_MODE_RAW)) ||
1677
2.62k
        (cfg->stream_size < COLLECTION_HDR_SIZE && (cfg->cmp_mode == CMP_MODE_RAW))) {
1678
0
      debug_print("Error: The compressed data size in the compression header is smaller than a collection header.");
1679
0
      return -1;
1680
0
    }
1681
2.62k
    if (org_size < COLLECTION_HDR_SIZE) {
1682
0
      debug_print("Error: The original decompressed data size in the compression header is smaller than the minimum size.");
1683
0
      return -1;
1684
0
    }
1685
2.62k
  } else {
1686
0
    if (org_size % sizeof(uint16_t)) {
1687
0
      debug_print("Error: The original size of an imagette product type in the compression header must be a multiple of 2.");
1688
0
      cfg->samples = 0;
1689
0
      return -1;
1690
0
    }
1691
0
    cfg->samples = org_size/sizeof(uint16_t);
1692
0
  }
1693
1694
2.62k
  cfg->src = cmp_ent_get_data_buf_const(ent);
1695
1696
2.62k
  if (cmp_ent_get_reserved(ent))
1697
0
    debug_print("Warning: The reserved field in the compressed header should be zero.");
1698
1699
2.62k
  if (cfg->cmp_mode == CMP_MODE_RAW) {
1700
200
    if (cmp_ent_get_original_size(ent) != cmp_ent_get_cmp_data_size(ent)) {
1701
0
      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
0
      return -1;
1703
0
    }
1704
    /* no specific header is used for raw data we are done */
1705
200
    return 0;
1706
200
  }
1707
1708
2.42k
  if (cmp_ent_cal_hdr_size(cfg->data_type, cfg->cmp_mode == CMP_MODE_RAW)
1709
2.42k
      > cmp_ent_get_size(ent)) {
1710
0
    debug_print("Error: The compression entity size is smaller than the minimum allowed size.");
1711
0
    return -1;
1712
0
  }
1713
1714
2.42k
  switch (cfg->data_type) {
1715
0
  case DATA_TYPE_IMAGETTE_ADAPTIVE:
1716
0
  case DATA_TYPE_SAT_IMAGETTE_ADAPTIVE:
1717
0
  case DATA_TYPE_F_CAM_IMAGETTE_ADAPTIVE:
1718
    /* we do not read in adaptive parameters */
1719
0
  case DATA_TYPE_IMAGETTE:
1720
0
  case DATA_TYPE_SAT_IMAGETTE:
1721
0
  case DATA_TYPE_F_CAM_IMAGETTE:
1722
0
    cfg->cmp_par_imagette = cmp_ent_get_ima_golomb_par(ent);
1723
0
    cfg->spill_imagette = cmp_ent_get_ima_spill(ent);
1724
0
    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
2.42k
  case DATA_TYPE_CHUNK:
1743
2.42k
    cfg->cmp_par_exp_flags = cmp_ent_get_non_ima_cmp_par1(ent);
1744
2.42k
    cfg->spill_exp_flags = cmp_ent_get_non_ima_spill1(ent);
1745
2.42k
    cfg->cmp_par_fx = cmp_ent_get_non_ima_cmp_par2(ent);
1746
2.42k
    cfg->spill_fx = cmp_ent_get_non_ima_spill2(ent);
1747
2.42k
    cfg->cmp_par_ncob = cmp_ent_get_non_ima_cmp_par3(ent);
1748
2.42k
    cfg->spill_ncob = cmp_ent_get_non_ima_spill3(ent);
1749
2.42k
    cfg->cmp_par_efx = cmp_ent_get_non_ima_cmp_par4(ent);
1750
2.42k
    cfg->spill_efx = cmp_ent_get_non_ima_spill4(ent);
1751
2.42k
    cfg->cmp_par_ecob = cmp_ent_get_non_ima_cmp_par5(ent);
1752
2.42k
    cfg->spill_ecob = cmp_ent_get_non_ima_spill5(ent);
1753
2.42k
    cfg->cmp_par_fx_cob_variance = cmp_ent_get_non_ima_cmp_par6(ent);
1754
2.42k
    cfg->spill_fx_cob_variance = cmp_ent_get_non_ima_spill6(ent);
1755
2.42k
    break;
1756
  /* LCOV_EXCL_START */
1757
0
  case DATA_TYPE_UNKNOWN:
1758
0
  default:
1759
0
    return -1;
1760
  /* LCOV_EXCL_STOP */
1761
2.42k
  }
1762
1763
2.42k
  return 0;
1764
2.42k
}
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.11M
{
1777
3.11M
  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.11M
  memcpy(&cmp_data_size, cmp_col, sizeof(cmp_data_size));
1798
3.11M
  be16_to_cpus(&cmp_data_size);
1799
1800
3.11M
  return cmp_data_size;
1801
3.11M
}
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.04M
{
1818
3.04M
  return CMP_COLLECTION_FILD_SIZE + COLLECTION_HDR_SIZE
1819
3.04M
    + get_cmp_collection_data_length(cmp_col);
1820
3.04M
}
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
2.42k
{
1837
2.42k
  const uint8_t *cmp_data_p = cmp_ent_get_data_buf_const(ent);
1838
2.42k
  long const cmp_data_size = cmp_ent_get_cmp_data_size(ent);
1839
2.42k
  int n = 0;
1840
2.42k
  const uint8_t *p = cmp_data_p;
1841
  /* highest plausible address of compressed collection */
1842
2.42k
  const uint8_t *limit_ptr = cmp_data_p + cmp_data_size - COLLECTION_HDR_SIZE;
1843
1844
73.4k
  while (p < limit_ptr) {
1845
71.0k
    p += get_cmp_collection_size(p);
1846
71.0k
    n++;
1847
71.0k
  }
1848
1849
2.42k
  if (p-cmp_data_p != cmp_data_size) {
1850
0
    debug_print("Error: The sum of the compressed collection does not match the size of the data in the compression header.");
1851
0
    return -1;
1852
0
  }
1853
2.42k
  return n;
1854
2.42k
}
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
71.0k
{
1876
71.0k
  int i;
1877
71.0k
  long decmp_pos = 0; /* position where to put the uncompressed result */
1878
  /* pointer to the collection header */
1879
71.0k
  const struct collection_hdr *col_hdr =
1880
71.0k
    (const struct collection_hdr *)(cmp_col + CMP_COLLECTION_FILD_SIZE);
1881
71.0k
  uint32_t cmp_data_size; /* size of the compressed data in the collection (not including the header) */
1882
71.0k
  uint16_t original_col_size; /* size of the decompressed collection data (not including the header) */
1883
71.0k
  size_t sample_size;
1884
1885
  /* get to the collection we want to decompress */
1886
3.04M
  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
71.0k
  cmp_data_size = get_cmp_collection_data_length(cmp_col);
1893
71.0k
  original_col_size = cmp_col_get_data_length(col_hdr);
1894
1895
71.0k
  if (cmp_data_size > original_col_size) {
1896
0
    debug_print("Error: Collection %i, the size of the compressed collection is larger than that of the uncompressed collection.", i);
1897
0
    return -1;
1898
0
  }
1899
1900
  /* if the compressed data size == original_col_size the collection data
1901
   * was put uncompressed into the bitstream */
1902
71.0k
  if (cmp_data_size == original_col_size)
1903
57.2k
    *coll_uncompressed = 1;
1904
13.8k
  else
1905
13.8k
    *coll_uncompressed = 0;
1906
1907
71.0k
  cfg->src = col_hdr;
1908
71.0k
  cfg->stream_size = cmp_data_size + COLLECTION_HDR_SIZE;
1909
1910
71.0k
  cfg->data_type = convert_subservice_to_cmp_data_type(cmp_col_get_subservice(col_hdr));
1911
71.0k
  sample_size = size_of_a_sample(cfg->data_type);
1912
71.0k
  if (!sample_size)
1913
0
    return -1;
1914
1915
71.0k
  if (original_col_size % sample_size) {
1916
0
    debug_print("Error: The size of the collection is not a multiple of a collection entry.");
1917
0
    return -1;
1918
0
  }
1919
71.0k
  cfg->samples = original_col_size / sample_size;
1920
1921
71.0k
  if (decmp_pos + original_col_size + COLLECTION_HDR_SIZE > decmp_size) {
1922
0
    debug_print("Error: The compressed data and the original size do not match.");
1923
0
    return -1;
1924
0
  }
1925
1926
71.0k
  return decmp_pos;
1927
71.0k
}
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
2.62k
{
1948
2.62k
  struct cmp_cfg cfg;
1949
2.62k
  int decmp_size;
1950
2.62k
  int i, n_chunks;
1951
1952
2.62k
  memset(&cfg, 0, sizeof(struct cmp_cfg));
1953
1954
2.62k
  if (!ent)
1955
0
    return -1;
1956
1957
2.62k
  decmp_size = (int)cmp_ent_get_original_size(ent);
1958
2.62k
  if (decmp_size < 0)
1959
0
    return -1;
1960
2.62k
  if (decmp_size == 0)
1961
0
    return 0;
1962
1963
2.62k
  if (cmp_ent_read_header(ent, &cfg))
1964
0
    return -1;
1965
1966
2.62k
  if (cfg.data_type != DATA_TYPE_CHUNK) { /* perform a non-chunk decompression */
1967
0
    if (cfg.cmp_mode == CMP_MODE_RAW) {
1968
0
      uint32_t data_size = cfg.samples * sizeof(uint16_t);
1969
1970
0
      if (decompressed_data) {
1971
0
        memcpy(decompressed_data, cmp_ent_get_data_buf_const(ent), data_size);
1972
0
        if (cmp_input_big_to_cpu_endianness(decompressed_data, data_size, cfg.data_type))
1973
0
          return -1;
1974
0
      }
1975
0
      return (int)data_size;
1976
0
    }
1977
1978
0
    cfg.model_buf = model_of_data;
1979
0
    cfg.updated_model_buf = up_model_buf;
1980
0
    cfg.dst = decompressed_data;
1981
1982
0
    return decompressed_data_internal(&cfg, RDCU_DECOMPRESSION);
1983
0
  }
1984
1985
  /* perform a chunk decompression */
1986
1987
2.62k
  if (cfg.cmp_mode == CMP_MODE_RAW) {
1988
200
    if (decompressed_data) {
1989
92
      memcpy(decompressed_data, cfg.src, cfg.stream_size);
1990
92
      cpu_to_be_chunk(decompressed_data, cfg.stream_size);
1991
92
    }
1992
200
    return (int)cfg.stream_size;
1993
200
  }
1994
1995
2.42k
  n_chunks = get_num_of_chunks(ent);
1996
2.42k
  if (n_chunks <= 0)
1997
0
    return -1;
1998
1999
73.4k
  for (i = 0; i < n_chunks; i++) {
2000
71.0k
    int decmp_chunk_size;
2001
71.0k
    int col_uncompressed;
2002
71.0k
    struct cmp_cfg cmp_cpy = cfg;
2003
71.0k
    long offset = parse_cmp_collection(cmp_ent_get_data_buf_const(ent), i,
2004
71.0k
               &cmp_cpy, &col_uncompressed, decmp_size);
2005
71.0k
    if (offset < 0)
2006
0
      return -1;
2007
2008
71.0k
    if (decompressed_data)
2009
17.9k
      cmp_cpy.dst = (uint8_t *)decompressed_data + offset;
2010
71.0k
    if (model_of_data)
2011
58.6k
      cmp_cpy.model_buf = (const uint8_t *)model_of_data + offset;
2012
71.0k
    if (up_model_buf)
2013
31.2k
      cmp_cpy.updated_model_buf = (uint8_t *)up_model_buf + offset;
2014
2015
71.0k
    if (col_uncompressed) {
2016
57.2k
      if (cmp_cpy.updated_model_buf && model_mode_is_used(cmp_cpy.cmp_mode)) {
2017
23.9k
        uint32_t s = cmp_cpy.stream_size;
2018
23.9k
        memcpy(cmp_cpy.updated_model_buf, cmp_cpy.src, s);
2019
23.9k
        if (be_to_cpu_chunk(cmp_cpy.updated_model_buf, s))
2020
0
          return -1;
2021
23.9k
      }
2022
57.2k
      cmp_cpy.cmp_mode = CMP_MODE_RAW;
2023
57.2k
    }
2024
2025
71.0k
    decmp_chunk_size = decompressed_data_internal(&cmp_cpy, ICU_DECOMRESSION);
2026
71.0k
    if (decmp_chunk_size < 0)
2027
0
      return decmp_chunk_size;
2028
71.0k
  }
2029
2.42k
  return decmp_size;
2030
2.42k
}
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
}