Coverage Report

Created: 2025-06-15 00:57

/src/cmp_tool/lib/common/cmp_data_types.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * @file    cmp_data_types.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 collection of functions to handle the different compression data types
17
 */
18
19
20
#include <stddef.h>
21
#include <stdint.h>
22
#include <limits.h>
23
24
#include "compiler.h"
25
#include "byteorder.h"
26
#include "cmp_debug.h"
27
#include "cmp_support.h"
28
#include "cmp_data_types.h"
29
30
31
#ifdef __BIG_ENDIAN
32
#  define CMP_IS_BIG_ENDIAN 1
33
#else
34
54.2k
#  define CMP_IS_BIG_ENDIAN 0
35
#endif
36
37
/**
38
 * @brief get the collection timestamp from the collection header
39
 *
40
 * @param col pointer to a collection header
41
 *
42
 * @returns the collection timestamp
43
 */
44
45
uint64_t cmp_col_get_timestamp(const struct collection_hdr *col)
46
0
{
47
0
#ifdef __LITTLE_ENDIAN
48
0
  return be64_to_cpu(col->timestamp) >> 16;
49
#else
50
  return col->timestamp;
51
#endif /* __LITTLE_ENDIAN */
52
0
}
53
54
55
/**
56
 * @brief get the configuration identifier from the collection header
57
 *
58
 * @param col pointer to a collection header
59
 *
60
 * @returns the configuration identifier
61
 */
62
63
uint16_t cmp_col_get_configuration_id(const struct collection_hdr *col)
64
0
{
65
0
  return be16_to_cpu(col->configuration_id);
66
0
}
67
68
69
/**
70
 * @brief get the collection identifier from the collection header
71
 *
72
 * @param col pointer to a collection header
73
 *
74
 * @returns the collection identifier
75
 */
76
77
uint16_t cmp_col_get_col_id(const struct collection_hdr *col)
78
0
{
79
0
  return be16_to_cpu(col->collection_id);
80
0
}
81
82
83
/**
84
 * @brief get the packet type bit in the collection identifier field of the
85
 *  collection header
86
 *
87
 * @param col pointer to a collection header
88
 *
89
 * @returns the collection packet type a collection; 1 for science packets and
90
 *  0 for window packets
91
 *
92
 */
93
94
uint8_t cmp_col_get_pkt_type(const struct collection_hdr *col)
95
0
{
96
0
  union collection_id cid;
97
98
0
  cid.collection_id = be16_to_cpu(col->collection_id);
99
0
  return cid.field.pkt_type;
100
0
}
101
102
103
/**
104
 * @brief get the subservice field in the collection identifier field of the
105
 *  collection header
106
 *
107
 * @param col pointer to a collection header
108
 *
109
 * @returns the collection subservice type
110
 */
111
112
uint8_t cmp_col_get_subservice(const struct collection_hdr *col)
113
295k
{
114
295k
  union collection_id cid;
115
116
295k
  cid.collection_id = be16_to_cpu(col->collection_id);
117
295k
  return cid.field.subservice;
118
295k
}
119
120
121
/**
122
 * @brief get the CCD identifier field in the collection identifier field of
123
 *  the collection header
124
 *
125
 * @param col pointer to a collection header
126
 *
127
 * @returns the collection CCD identifier
128
 */
129
130
uint8_t cmp_col_get_ccd_id(const struct collection_hdr *col)
131
0
{
132
0
  union collection_id cid;
133
134
0
  cid.collection_id = be16_to_cpu(col->collection_id);
135
0
  return cid.field.ccd_id;
136
0
}
137
138
139
/**
140
 * @brief get the sequence number field in the collection identifier field of
141
 *  the collection header
142
 *
143
 * @param col pointer to a collection header
144
 *
145
 * @returns the collection sequence number
146
 */
147
148
uint8_t cmp_col_get_sequence_num(const struct collection_hdr *col)
149
0
{
150
0
  union collection_id cid;
151
152
0
  cid.collection_id = be16_to_cpu(col->collection_id);
153
0
  return cid.field.sequence_num;
154
0
}
155
156
157
/**
158
 * @brief get the collection length from the collection header
159
 *
160
 * @param col pointer to a collection header
161
 *
162
 * @returns the collection length in bytes
163
 */
164
165
uint16_t cmp_col_get_data_length(const struct collection_hdr *col)
166
3.44M
{
167
3.44M
  return be16_to_cpu(col->collection_length);
168
3.44M
}
169
170
171
/**
172
 * @brief get the entire collection size (header plus data size)
173
 *
174
 * @param col pointer to a collection header
175
 *
176
 * @returns the collection size in bytes
177
 */
178
179
uint32_t cmp_col_get_size(const struct collection_hdr *col)
180
3.24M
{
181
3.24M
  return COLLECTION_HDR_SIZE + cmp_col_get_data_length(col);
182
3.24M
}
183
184
185
/**
186
 * @brief set the timestamp in the collection header
187
 *
188
 * @param col   pointer to a collection header
189
 * @param timestamp collection timestamp (coarse and fine)
190
 *
191
 * @returns 0 on success, otherwise error
192
 */
193
194
int cmp_col_set_timestamp(struct collection_hdr *col, uint64_t timestamp)
195
0
{
196
0
  if (!col)
197
0
    return -1;
198
0
  if (timestamp >> 48)
199
0
    return -1;
200
201
0
#ifdef __LITTLE_ENDIAN
202
0
  col->timestamp = cpu_to_be64(timestamp) >> 16;
203
#else
204
  col->timestamp = timestamp;
205
#endif /* __LITTLE_ENDIAN */
206
207
0
  return 0;
208
0
}
209
210
211
/**
212
 * @brief set the configuration identifier in the collection header
213
 *
214
 * @param col     pointer to a collection header
215
 * @param configuration_id  configuration identifier
216
 *
217
 * @returns 0 on success, otherwise error
218
 */
219
220
int cmp_col_set_configuration_id(struct collection_hdr *col, uint16_t configuration_id)
221
0
{
222
0
  if (!col)
223
0
    return 1;
224
225
0
  col->configuration_id = cpu_to_be16(configuration_id);
226
0
  return 0;
227
0
}
228
229
230
/**
231
 * @brief set the collection identifier in the collection header
232
 *
233
 * @param col   pointer to a collection header
234
 * @param collection_id collection identifier
235
 *
236
 * @returns 0 on success, otherwise error
237
 */
238
239
int cmp_col_set_col_id(struct collection_hdr *col, uint16_t collection_id)
240
0
{
241
0
  if (!col)
242
0
    return -1;
243
244
0
  col->collection_id = cpu_to_be16(collection_id);
245
0
  return 0;
246
0
}
247
248
249
/**
250
 * @brief set the packet type bit in the collection identifier field of the
251
 *  collection header
252
 *
253
 * @param col   pointer to a collection header
254
 * @param pkt_type  packet type bit; 1 for science packets and 0 for window packets
255
 *
256
 * @returns 0 on success, otherwise error
257
 */
258
259
int cmp_col_set_pkt_type(struct collection_hdr *col, uint8_t pkt_type)
260
0
{
261
0
  union collection_id cid;
262
263
0
  if (!col)
264
0
    return -1;
265
0
  if (pkt_type >> 1)
266
0
    return -1;
267
268
0
  cid.collection_id = be16_to_cpu(col->collection_id);
269
0
  cid.field.pkt_type = pkt_type;
270
0
  cmp_col_set_col_id(col, cid.collection_id);
271
0
  return 0;
272
0
}
273
274
275
/**
276
 * @brief set the packet subservice field in the collection identifier field of
277
 *  the collection header
278
 *
279
 * @param col   pointer to a collection header
280
 * @param subservice  collection subservice type
281
 *
282
 * @returns 0 on success, otherwise error
283
 */
284
285
int cmp_col_set_subservice(struct collection_hdr *col, uint8_t subservice)
286
0
{
287
0
  union collection_id cid;
288
289
0
  if (!col)
290
0
    return -1;
291
0
  if (subservice >> 6)
292
0
    return -1;
293
294
0
  cid.collection_id = be16_to_cpu(col->collection_id);
295
0
  cid.field.subservice = subservice;
296
0
  cmp_col_set_col_id(col, cid.collection_id);
297
0
  return 0;
298
0
}
299
300
301
/**
302
 * @brief set the packet CCD identifier field in the collection identifier field
303
 *  of the collection header
304
 *
305
 * @param col   pointer to a collection header
306
 * @param ccd_id  collection CCD identifier
307
 *
308
 * @returns 0 on success, otherwise error
309
 */
310
311
int cmp_col_set_ccd_id(struct collection_hdr *col, uint8_t ccd_id)
312
0
{
313
0
  union collection_id cid;
314
315
0
  if (!col)
316
0
    return -1;
317
0
  if (ccd_id >> 2)
318
0
    return -1;
319
320
0
  cid.collection_id = be16_to_cpu(col->collection_id);
321
0
  cid.field.ccd_id = ccd_id;
322
0
  cmp_col_set_col_id(col, cid.collection_id);
323
0
  return 0;
324
0
}
325
326
327
/**
328
 * @brief set the collection sequence number bit field in the collection
329
 *  identifier of the collection header
330
 *
331
 * @param col   pointer to a collection header
332
 * @param sequence_num  collection sequence number
333
 *
334
 * @returns 0 on success, otherwise error
335
 */
336
337
int cmp_col_set_sequence_num(struct collection_hdr *col, uint8_t sequence_num)
338
0
{
339
0
  union collection_id cid;
340
341
0
  if (!col)
342
0
    return -1;
343
344
0
  if (sequence_num >> 7)
345
0
    return -1;
346
347
0
  cid.collection_id = be16_to_cpu(col->collection_id);
348
0
  cid.field.sequence_num = sequence_num;
349
0
  return cmp_col_set_col_id(col, cid.collection_id);
350
0
}
351
352
353
/**
354
 * @brief set the collection length in the collection header
355
 *
356
 * @param col   pointer to a collection header
357
 * @param length  length of the collection in bytes TBC: without the
358
 *      header size itself
359
 *
360
 * @returns 0 on success, otherwise error
361
 */
362
363
int cmp_col_set_data_length(struct collection_hdr *col, uint16_t length)
364
0
{
365
0
  if (!col)
366
0
    return -1;
367
368
0
  col->collection_length = cpu_to_be16(length);
369
0
  return 0;
370
0
}
371
372
373
/**
374
 * @brief converts a subservice to its associated compression data type
375
 *
376
 * @param subservice  collection subservice type
377
 *
378
 * @returns the converted compression data type; DATA_TYPE_UNKNOWN if the
379
 *  subservice is unknown
380
 */
381
382
enum cmp_data_type convert_subservice_to_cmp_data_type(uint8_t subservice)
383
199k
{
384
199k
  switch (subservice) {
385
3.62k
  case SST_NCxx_S_SCIENCE_IMAGETTE:
386
3.62k
    return DATA_TYPE_IMAGETTE;
387
35.3k
  case SST_NCxx_S_SCIENCE_SAT_IMAGETTE:
388
35.3k
    return DATA_TYPE_SAT_IMAGETTE;
389
2.03k
  case SST_NCxx_S_SCIENCE_OFFSET:
390
2.03k
    return DATA_TYPE_OFFSET;
391
795
  case SST_NCxx_S_SCIENCE_BACKGROUND:
392
795
    return DATA_TYPE_BACKGROUND;
393
5.85k
  case SST_NCxx_S_SCIENCE_SMEARING:
394
5.85k
    return DATA_TYPE_SMEARING;
395
83.0k
  case SST_NCxx_S_SCIENCE_S_FX:
396
83.0k
    return DATA_TYPE_S_FX;
397
4.41k
  case SST_NCxx_S_SCIENCE_S_FX_EFX:
398
4.41k
    return DATA_TYPE_S_FX_EFX;
399
8.79k
  case SST_NCxx_S_SCIENCE_S_FX_NCOB:
400
8.79k
    return DATA_TYPE_S_FX_NCOB;
401
6.23k
  case SST_NCxx_S_SCIENCE_S_FX_EFX_NCOB_ECOB:
402
6.23k
    return DATA_TYPE_S_FX_EFX_NCOB_ECOB;
403
14.4k
  case SST_NCxx_S_SCIENCE_L_FX:
404
14.4k
    return DATA_TYPE_L_FX;
405
2.45k
  case SST_NCxx_S_SCIENCE_L_FX_EFX:
406
2.45k
    return DATA_TYPE_L_FX_EFX;
407
2.29k
  case SST_NCxx_S_SCIENCE_L_FX_NCOB:
408
2.29k
    return DATA_TYPE_L_FX_NCOB;
409
6.24k
  case SST_NCxx_S_SCIENCE_L_FX_EFX_NCOB_ECOB:
410
6.24k
    return DATA_TYPE_L_FX_EFX_NCOB_ECOB;
411
0
  case SST_NCxx_S_SCIENCE_F_FX:
412
0
    return DATA_TYPE_F_FX;
413
0
  case SST_NCxx_S_SCIENCE_F_FX_EFX:
414
0
    return DATA_TYPE_F_FX_EFX;
415
0
  case SST_NCxx_S_SCIENCE_F_FX_NCOB:
416
0
    return DATA_TYPE_F_FX_NCOB;
417
0
  case SST_NCxx_S_SCIENCE_F_FX_EFX_NCOB_ECOB:
418
0
    return DATA_TYPE_F_FX_EFX_NCOB_ECOB;
419
1.77k
  case SST_FCx_S_SCIENCE_IMAGETTE:
420
1.77k
    return DATA_TYPE_F_CAM_IMAGETTE;
421
2.92k
  case SST_FCx_S_SCIENCE_OFFSET_VALUES:
422
2.92k
    return DATA_TYPE_F_CAM_OFFSET;
423
18.8k
  case SST_FCx_S_BACKGROUND_VALUES:
424
18.8k
    return DATA_TYPE_F_CAM_BACKGROUND;
425
0
  default:
426
0
    return DATA_TYPE_UNKNOWN;
427
199k
  };
428
0
}
429
430
431
/**
432
 * @brief converts a compression data type to its associated subservice.
433
 *
434
 * @param data_type compression data type
435
 *
436
 * @returns the converted subservice; -1 if the data type is unknown.
437
 */
438
439
uint8_t convert_cmp_data_type_to_subservice(enum cmp_data_type data_type)
440
0
{
441
0
  uint8_t sst = 0;
442
443
0
  switch (data_type) {
444
0
  case DATA_TYPE_IMAGETTE:
445
0
  case DATA_TYPE_IMAGETTE_ADAPTIVE:
446
0
    sst = SST_NCxx_S_SCIENCE_IMAGETTE;
447
0
    break;
448
0
  case DATA_TYPE_SAT_IMAGETTE:
449
0
  case DATA_TYPE_SAT_IMAGETTE_ADAPTIVE:
450
0
    sst = SST_NCxx_S_SCIENCE_SAT_IMAGETTE;
451
0
    break;
452
0
  case DATA_TYPE_OFFSET:
453
0
    sst = SST_NCxx_S_SCIENCE_OFFSET;
454
0
    break;
455
0
  case DATA_TYPE_BACKGROUND:
456
0
    sst = SST_NCxx_S_SCIENCE_BACKGROUND;
457
0
    break;
458
0
  case DATA_TYPE_SMEARING:
459
0
    sst = SST_NCxx_S_SCIENCE_SMEARING;
460
0
    break;
461
0
  case DATA_TYPE_S_FX:
462
0
    sst = SST_NCxx_S_SCIENCE_S_FX;
463
0
    break;
464
0
  case DATA_TYPE_S_FX_EFX:
465
0
    sst = SST_NCxx_S_SCIENCE_S_FX_EFX;
466
0
    break;
467
0
  case DATA_TYPE_S_FX_NCOB:
468
0
    sst = SST_NCxx_S_SCIENCE_S_FX_NCOB;
469
0
    break;
470
0
  case DATA_TYPE_S_FX_EFX_NCOB_ECOB:
471
0
    sst = SST_NCxx_S_SCIENCE_S_FX_EFX_NCOB_ECOB;
472
0
    break;
473
0
  case DATA_TYPE_L_FX:
474
0
    sst = SST_NCxx_S_SCIENCE_L_FX;
475
0
    break;
476
0
  case DATA_TYPE_L_FX_EFX:
477
0
    sst = SST_NCxx_S_SCIENCE_L_FX_EFX;
478
0
    break;
479
0
  case DATA_TYPE_L_FX_NCOB:
480
0
    sst = SST_NCxx_S_SCIENCE_L_FX_NCOB;
481
0
    break;
482
0
  case DATA_TYPE_L_FX_EFX_NCOB_ECOB:
483
0
    sst = SST_NCxx_S_SCIENCE_L_FX_EFX_NCOB_ECOB;
484
0
    break;
485
0
  case DATA_TYPE_F_FX:
486
0
    sst = SST_NCxx_S_SCIENCE_F_FX;
487
0
    break;
488
0
  case DATA_TYPE_F_FX_EFX:
489
0
    sst = SST_NCxx_S_SCIENCE_F_FX_EFX;
490
0
    break;
491
0
  case DATA_TYPE_F_FX_NCOB:
492
0
    sst = SST_NCxx_S_SCIENCE_F_FX_NCOB;
493
0
    break;
494
0
  case DATA_TYPE_F_FX_EFX_NCOB_ECOB:
495
0
    sst = SST_NCxx_S_SCIENCE_F_FX_EFX_NCOB_ECOB;
496
0
    break;
497
0
  case DATA_TYPE_F_CAM_IMAGETTE:
498
0
  case DATA_TYPE_F_CAM_IMAGETTE_ADAPTIVE:
499
0
    sst = SST_FCx_S_SCIENCE_IMAGETTE;
500
0
    break;
501
0
  case DATA_TYPE_F_CAM_OFFSET:
502
0
    sst = SST_FCx_S_SCIENCE_OFFSET_VALUES;
503
0
    break;
504
0
  case DATA_TYPE_F_CAM_BACKGROUND:
505
0
    sst = SST_FCx_S_BACKGROUND_VALUES;
506
0
    break;
507
0
  default:
508
0
  case DATA_TYPE_UNKNOWN:
509
0
  case DATA_TYPE_CHUNK:
510
0
    debug_print("Error: Unknown compression data type!");
511
0
    sst = (uint8_t)-1;
512
0
  };
513
514
0
  return sst;
515
0
}
516
517
518
/**
519
 * @brief calculate the size of a sample for the different compression data type
520
 *
521
 * @param data_type compression data_type
522
 *
523
 * @returns the size of a data sample in bytes for the selected compression
524
 *  data type; zero on unknown data type
525
 */
526
527
size_t size_of_a_sample(enum cmp_data_type data_type)
528
416k
{
529
416k
  size_t sample_size = 0;
530
531
416k
  switch (data_type) {
532
7.37k
  case DATA_TYPE_IMAGETTE:
533
7.37k
  case DATA_TYPE_IMAGETTE_ADAPTIVE:
534
95.3k
  case DATA_TYPE_SAT_IMAGETTE:
535
95.3k
  case DATA_TYPE_SAT_IMAGETTE_ADAPTIVE:
536
99.1k
  case DATA_TYPE_F_CAM_IMAGETTE:
537
99.1k
  case DATA_TYPE_F_CAM_IMAGETTE_ADAPTIVE:
538
99.1k
    sample_size = sizeof(uint16_t);
539
99.1k
    break;
540
4.83k
  case DATA_TYPE_OFFSET:
541
10.9k
  case DATA_TYPE_F_CAM_OFFSET:
542
10.9k
    sample_size = sizeof(struct offset);
543
10.9k
    break;
544
1.87k
  case DATA_TYPE_BACKGROUND:
545
39.8k
  case DATA_TYPE_F_CAM_BACKGROUND:
546
39.8k
    sample_size = sizeof(struct background);
547
39.8k
    break;
548
15.6k
  case DATA_TYPE_SMEARING:
549
15.6k
    sample_size = sizeof(struct smearing);
550
15.6k
    break;
551
151k
  case DATA_TYPE_S_FX:
552
151k
    sample_size = sizeof(struct s_fx);
553
151k
    break;
554
10.0k
  case DATA_TYPE_S_FX_EFX:
555
10.0k
    sample_size = sizeof(struct s_fx_efx);
556
10.0k
    break;
557
17.2k
  case DATA_TYPE_S_FX_NCOB:
558
17.2k
    sample_size = sizeof(struct s_fx_ncob);
559
17.2k
    break;
560
14.3k
  case DATA_TYPE_S_FX_EFX_NCOB_ECOB:
561
14.3k
    sample_size = sizeof(struct s_fx_efx_ncob_ecob);
562
14.3k
    break;
563
32.7k
  case DATA_TYPE_L_FX:
564
32.7k
    sample_size = sizeof(struct l_fx);
565
32.7k
    break;
566
5.69k
  case DATA_TYPE_L_FX_EFX:
567
5.69k
    sample_size = sizeof(struct l_fx_efx);
568
5.69k
    break;
569
5.00k
  case DATA_TYPE_L_FX_NCOB:
570
5.00k
    sample_size = sizeof(struct l_fx_ncob);
571
5.00k
    break;
572
14.4k
  case DATA_TYPE_L_FX_EFX_NCOB_ECOB:
573
14.4k
    sample_size = sizeof(struct l_fx_efx_ncob_ecob);
574
14.4k
    break;
575
0
  case DATA_TYPE_F_FX:
576
0
    sample_size = sizeof(struct f_fx);
577
0
    break;
578
0
  case DATA_TYPE_F_FX_EFX:
579
0
    sample_size = sizeof(struct f_fx_efx);
580
0
    break;
581
0
  case DATA_TYPE_F_FX_NCOB:
582
0
    sample_size = sizeof(struct f_fx_ncob);
583
0
    break;
584
0
  case DATA_TYPE_F_FX_EFX_NCOB_ECOB:
585
0
    sample_size = sizeof(struct f_fx_efx_ncob_ecob);
586
0
    break;
587
0
  case DATA_TYPE_CHUNK:
588
0
  case DATA_TYPE_UNKNOWN:
589
0
  default:
590
0
    debug_print("Error: Compression data type is not supported.");
591
0
    break;
592
416k
  }
593
416k
  return sample_size;
594
416k
}
595
596
597
static uint32_t be24_to_cpu(uint32_t a)
598
8.57k
{
599
8.57k
#ifdef __LITTLE_ENDIAN
600
8.57k
  return be32_to_cpu(a) >> 8;
601
#else
602
  return a;
603
#endif /* __LITTLE_ENDIAN */
604
8.57k
}
605
606
607
static void be_to_cpus_16(uint16_t *a, uint32_t samples)
608
19.9k
{
609
19.9k
  uint32_t i;
610
611
98.5k
  for (i = 0; i < samples; i++) {
612
78.5k
    uint16_t tmp;
613
614
78.5k
    tmp = be16_to_cpu(get_unaligned(&a[i]));
615
78.5k
    put_unaligned(tmp, &a[i]);
616
78.5k
  }
617
19.9k
}
618
619
620
static void be_to_cpus_offset(struct offset *a, uint32_t samples)
621
1.00k
{
622
1.00k
  uint32_t i;
623
624
3.29k
  for (i = 0; i < samples; i++) {
625
2.28k
    a[i].mean = be32_to_cpu(a[i].mean);
626
2.28k
    a[i].variance = be32_to_cpu(a[i].variance);
627
2.28k
  }
628
1.00k
}
629
630
631
static void be_to_cpus_background(struct background *a, uint32_t samples)
632
5.87k
{
633
5.87k
  uint32_t i;
634
635
14.6k
  for (i = 0; i < samples; i++) {
636
8.74k
    a[i].mean = be32_to_cpu(a[i].mean);
637
8.74k
    a[i].variance = be32_to_cpu(a[i].variance);
638
8.74k
    a[i].outlier_pixels = be16_to_cpu(a[i].outlier_pixels);
639
8.74k
  }
640
5.87k
}
641
642
643
static void be_to_cpus_smearing(struct smearing *a, uint32_t samples)
644
808
{
645
808
  uint32_t i;
646
647
4.35k
  for (i = 0; i < samples; i++) {
648
3.54k
    a[i].mean = be32_to_cpu(a[i].mean);
649
3.54k
    a[i].variance_mean = be16_to_cpu(a[i].variance_mean);
650
3.54k
    a[i].outlier_pixels = be16_to_cpu(a[i].outlier_pixels);
651
3.54k
  }
652
808
}
653
654
655
static void be_to_cpus_s_fx(struct s_fx *a, uint32_t samples)
656
16.8k
{
657
16.8k
  uint32_t i;
658
659
19.2k
  for (i = 0; i < samples; i++)
660
16.8k
    a[i].fx = be32_to_cpu(a[i].fx);
661
16.8k
}
662
663
664
static void be_to_cpus_s_fx_efx(struct s_fx_efx *a, uint32_t samples)
665
1.02k
{
666
1.02k
  uint32_t i;
667
668
3.87k
  for (i = 0; i < samples; i++) {
669
2.84k
    a[i].fx = be32_to_cpu(a[i].fx);
670
2.84k
    a[i].efx = be32_to_cpu(a[i].efx);
671
2.84k
  }
672
1.02k
}
673
674
675
static void be_to_cpus_s_fx_ncob(struct s_fx_ncob *a, uint32_t samples)
676
2.08k
{
677
2.08k
  uint32_t i;
678
679
5.74k
  for (i = 0; i < samples; i++) {
680
3.65k
    a[i].fx = be32_to_cpu(a[i].fx);
681
3.65k
    a[i].ncob_x = be32_to_cpu(a[i].ncob_x);
682
3.65k
    a[i].ncob_y = be32_to_cpu(a[i].ncob_y);
683
3.65k
  }
684
2.08k
}
685
686
687
static void be_to_cpus_s_fx_efx_ncob_ecob(struct s_fx_efx_ncob_ecob *a, uint32_t samples)
688
1.83k
{
689
1.83k
  uint32_t i;
690
691
4.64k
  for (i = 0; i < samples; i++) {
692
2.80k
    a[i].fx = be32_to_cpu(a[i].fx);
693
2.80k
    a[i].ncob_x = be32_to_cpu(a[i].ncob_x);
694
2.80k
    a[i].ncob_y = be32_to_cpu(a[i].ncob_y);
695
2.80k
    a[i].efx = be32_to_cpu(a[i].efx);
696
2.80k
    a[i].ecob_x = be32_to_cpu(a[i].ecob_x);
697
2.80k
    a[i].ecob_y = be32_to_cpu(a[i].ecob_y);
698
2.80k
  }
699
1.83k
}
700
701
702
static void be_to_cpus_l_fx(struct l_fx *a, uint32_t samples)
703
2.31k
{
704
2.31k
  uint32_t i;
705
706
5.37k
  for (i = 0; i < samples; i++) {
707
3.05k
    a[i].exp_flags = be24_to_cpu(a[i].exp_flags);
708
3.05k
    a[i].fx = be32_to_cpu(a[i].fx);
709
3.05k
    a[i].fx_variance = be32_to_cpu(a[i].fx_variance);
710
3.05k
  }
711
2.31k
}
712
713
714
static void be_to_cpus_l_fx_efx(struct l_fx_efx *a, uint32_t samples)
715
568
{
716
568
  uint32_t i;
717
718
2.41k
  for (i = 0; i < samples; i++) {
719
1.84k
    a[i].exp_flags = be24_to_cpu(a[i].exp_flags);
720
1.84k
    a[i].fx = be32_to_cpu(a[i].fx);
721
1.84k
    a[i].efx = be32_to_cpu(a[i].efx);
722
1.84k
    a[i].fx_variance = be32_to_cpu(a[i].fx_variance);
723
1.84k
  }
724
568
}
725
726
727
static void be_to_cpus_l_fx_ncob(struct l_fx_ncob *a, uint32_t samples)
728
457
{
729
457
  uint32_t i;
730
731
2.17k
  for (i = 0; i < samples; i++) {
732
1.71k
    a[i].exp_flags = be24_to_cpu(a[i].exp_flags);
733
1.71k
    a[i].fx = be32_to_cpu(a[i].fx);
734
1.71k
    a[i].ncob_x = be32_to_cpu(a[i].ncob_x);
735
1.71k
    a[i].ncob_y = be32_to_cpu(a[i].ncob_y);
736
1.71k
    a[i].fx_variance = be32_to_cpu(a[i].fx_variance);
737
1.71k
    a[i].cob_x_variance = be32_to_cpu(a[i].cob_x_variance);
738
1.71k
    a[i].cob_y_variance = be32_to_cpu(a[i].cob_y_variance);
739
1.71k
  }
740
457
}
741
742
743
static void be_to_cpus_l_fx_efx_ncob_ecob(struct l_fx_efx_ncob_ecob *a, uint32_t samples)
744
1.42k
{
745
1.42k
  uint32_t i;
746
747
3.36k
  for (i = 0; i < samples; i++) {
748
1.94k
    a[i].exp_flags = be24_to_cpu(a[i].exp_flags);
749
1.94k
    a[i].fx = be32_to_cpu(a[i].fx);
750
1.94k
    a[i].ncob_x = be32_to_cpu(a[i].ncob_x);
751
1.94k
    a[i].ncob_y = be32_to_cpu(a[i].ncob_y);
752
1.94k
    a[i].efx = be32_to_cpu(a[i].efx);
753
1.94k
    a[i].ecob_x = be32_to_cpu(a[i].ecob_x);
754
1.94k
    a[i].ecob_y = be32_to_cpu(a[i].ecob_y);
755
1.94k
    a[i].fx_variance = be32_to_cpu(a[i].fx_variance);
756
1.94k
    a[i].cob_x_variance = be32_to_cpu(a[i].cob_x_variance);
757
1.94k
    a[i].cob_y_variance = be32_to_cpu(a[i].cob_y_variance);
758
1.94k
  }
759
1.42k
}
760
761
762
static void be_to_cpus_f_fx(struct f_fx *a, uint32_t samples)
763
0
{
764
0
  uint32_t i;
765
766
0
  for (i = 0; i < samples; i++)
767
0
    a[i].fx = be32_to_cpu(a[i].fx);
768
0
}
769
770
771
static void be_to_cpus_f_fx_efx(struct f_fx_efx *a, uint32_t samples)
772
0
{
773
0
  uint32_t i;
774
775
0
  for (i = 0; i < samples; i++) {
776
0
    a[i].fx = be32_to_cpu(a[i].fx);
777
0
    a[i].efx = be32_to_cpu(a[i].efx);
778
0
  }
779
0
}
780
781
782
static void be_to_cpus_f_fx_ncob(struct f_fx_ncob *a, uint32_t samples)
783
0
{
784
0
  uint32_t i;
785
786
0
  for (i = 0; i < samples; i++) {
787
0
    a[i].fx = be32_to_cpu(a[i].fx);
788
0
    a[i].ncob_x = be32_to_cpu(a[i].ncob_x);
789
0
    a[i].ncob_y = be32_to_cpu(a[i].ncob_y);
790
0
  }
791
0
}
792
793
794
static void be_to_cpus_f_fx_efx_ncob_ecob(struct f_fx_efx_ncob_ecob *a, uint32_t samples)
795
0
{
796
0
  uint32_t i;
797
798
0
  for (i = 0; i < samples; i++) {
799
0
    a[i].fx = be32_to_cpu(a[i].fx);
800
0
    a[i].ncob_x = be32_to_cpu(a[i].ncob_x);
801
0
    a[i].ncob_y = be32_to_cpu(a[i].ncob_y);
802
0
    a[i].efx = be32_to_cpu(a[i].efx);
803
0
    a[i].ecob_x = be32_to_cpu(a[i].ecob_x);
804
0
    a[i].ecob_y = be32_to_cpu(a[i].ecob_y);
805
0
  }
806
0
}
807
808
809
/**
810
 * @brief swaps the endianness of (collection) data from big endian to the CPU
811
 *  endianness (or vice versa) in place.
812
 * @note if you want to swap the data of a whole collection, including a
813
 *  collection header or a chunk of collections use the be_to_cpu_chunk() or
814
 *  cpu_to_be_chunk() functions
815
 *
816
 * @param data      a pointer to the data to swap (not including a
817
 *        collection header); can be NULL
818
 * @param data_size_byte  size of the data in bytes
819
 * @param data_type   compression data type
820
 *
821
 * @returns 0 on success; -1 on failure
822
 */
823
824
int be_to_cpu_data_type(void *data, uint32_t data_size_byte, enum cmp_data_type data_type)
825
54.2k
{
826
54.2k
  uint32_t sample_size = (uint32_t)size_of_a_sample(data_type);
827
54.2k
  uint32_t samples;
828
829
54.2k
  if (!data) /* nothing to do */
830
0
    return 0;
831
832
54.2k
  if (!sample_size)
833
0
    return -1;
834
835
54.2k
  if (data_size_byte % sample_size) {
836
0
    debug_print("Error: Can not convert data size in samples.");
837
0
    return -1;
838
0
  }
839
54.2k
  samples = data_size_byte / sample_size;
840
841
54.2k
  if (CMP_IS_BIG_ENDIAN)
842
0
    return 0;
843
844
54.2k
  switch (data_type) {
845
607
  case DATA_TYPE_IMAGETTE:
846
607
  case DATA_TYPE_IMAGETTE_ADAPTIVE:
847
19.5k
  case DATA_TYPE_SAT_IMAGETTE:
848
19.5k
  case DATA_TYPE_SAT_IMAGETTE_ADAPTIVE:
849
19.9k
  case DATA_TYPE_F_CAM_IMAGETTE:
850
19.9k
  case DATA_TYPE_F_CAM_IMAGETTE_ADAPTIVE:
851
19.9k
    be_to_cpus_16(data, samples);
852
19.9k
    break;
853
373
  case DATA_TYPE_OFFSET:
854
1.00k
  case DATA_TYPE_F_CAM_OFFSET:
855
1.00k
    be_to_cpus_offset(data, samples);
856
1.00k
    break;
857
200
  case DATA_TYPE_BACKGROUND:
858
5.87k
  case DATA_TYPE_F_CAM_BACKGROUND:
859
5.87k
    be_to_cpus_background(data, samples);
860
5.87k
    break;
861
808
  case DATA_TYPE_SMEARING:
862
808
    be_to_cpus_smearing(data, samples);
863
808
    break;
864
16.8k
  case DATA_TYPE_S_FX:
865
16.8k
    be_to_cpus_s_fx(data, samples);
866
16.8k
    break;
867
1.02k
  case DATA_TYPE_S_FX_EFX:
868
1.02k
    be_to_cpus_s_fx_efx(data, samples);
869
1.02k
    break;
870
2.08k
  case DATA_TYPE_S_FX_NCOB:
871
2.08k
    be_to_cpus_s_fx_ncob(data, samples);
872
2.08k
    break;
873
1.83k
  case DATA_TYPE_S_FX_EFX_NCOB_ECOB:
874
1.83k
    be_to_cpus_s_fx_efx_ncob_ecob(data, samples);
875
1.83k
    break;
876
2.31k
  case DATA_TYPE_L_FX:
877
2.31k
    be_to_cpus_l_fx(data, samples);
878
2.31k
    break;
879
568
  case DATA_TYPE_L_FX_EFX:
880
568
    be_to_cpus_l_fx_efx(data, samples);
881
568
    break;
882
457
  case DATA_TYPE_L_FX_NCOB:
883
457
    be_to_cpus_l_fx_ncob(data, samples);
884
457
    break;
885
1.42k
  case DATA_TYPE_L_FX_EFX_NCOB_ECOB:
886
1.42k
    be_to_cpus_l_fx_efx_ncob_ecob(data, samples);
887
1.42k
    break;
888
0
  case DATA_TYPE_F_FX:
889
0
    be_to_cpus_f_fx(data, samples);
890
0
    break;
891
0
  case DATA_TYPE_F_FX_EFX:
892
0
    be_to_cpus_f_fx_efx(data, samples);
893
0
    break;
894
0
  case DATA_TYPE_F_FX_NCOB:
895
0
    be_to_cpus_f_fx_ncob(data, samples);
896
0
    break;
897
0
  case DATA_TYPE_F_FX_EFX_NCOB_ECOB:
898
0
    be_to_cpus_f_fx_efx_ncob_ecob(data, samples);
899
0
    break;
900
  /* LCOV_EXCL_START */
901
0
  default:
902
0
    debug_print("Error: Can not swap endianness for this compression data type.");
903
0
    return -1;
904
  /* LCOV_EXCL_STOP */
905
54.2k
  }
906
907
54.2k
  return 0;
908
54.2k
}
909
910
911
/**
912
 * @brief swaps the endianness of chunk data from big endian to the CPU
913
 *  endianness (or vice versa) in place
914
 * @note the endianness of the collection header is not changed!
915
 *
916
 * @param chunk   pointer to a chunk of collections (can be NULL)
917
 * @param chunk_size  size in bytes of the chunk
918
 *
919
 * @returns 0 on success; -1 on failure
920
 */
921
922
int be_to_cpu_chunk(uint8_t *chunk, size_t chunk_size)
923
35.3k
{
924
35.3k
  uint8_t *col_p = chunk;
925
926
35.3k
  if (!chunk) /* nothing to do */
927
0
    return 0;
928
929
35.3k
  if (chunk_size < COLLECTION_HDR_SIZE)
930
0
    return -1;
931
932
71.1k
  while (col_p <= chunk + chunk_size - COLLECTION_HDR_SIZE) {
933
35.8k
    struct collection_hdr *col_hdr = (struct collection_hdr *)col_p;
934
35.8k
    enum cmp_data_type data_type = convert_subservice_to_cmp_data_type(cmp_col_get_subservice(col_hdr));
935
35.8k
    uint32_t data_size = cmp_col_get_data_length(col_hdr);
936
937
35.8k
    col_p += cmp_col_get_size(col_hdr);
938
35.8k
    if (col_p > chunk + chunk_size)  /* over read chunk? */
939
0
      break;
940
941
35.8k
    if (be_to_cpu_data_type(col_hdr->entry, data_size, data_type))
942
0
      return -1;
943
35.8k
  }
944
945
35.3k
  if (col_p != chunk + chunk_size) {
946
0
    debug_print("Error: The chunk size does not match the sum of the collection sizes.");
947
0
    return -1;
948
0
  }
949
950
35.3k
  return 0;
951
35.3k
}
952
953
954
/**
955
 * @brief swap the endianness of uncompressed data from big endian to the cpu
956
 *  endianness (or the other way around) in place
957
 *
958
 * @param data      pointer to a data sample
959
 * @param data_size_byte  size of the data in bytes
960
 * @param data_type   compression data type
961
 *
962
 * @returns 0 on success; non-zero on failure
963
 */
964
965
int cmp_input_big_to_cpu_endianness(void *data, uint32_t data_size_byte,
966
            enum cmp_data_type data_type)
967
0
{
968
0
  if (data && !rdcu_supported_data_type_is_used(data_type)) {
969
    /* skip collection header for non RDCU data types */
970
0
    data = (uint8_t *)data + COLLECTION_HDR_SIZE;
971
0
    if (data_size_byte < COLLECTION_HDR_SIZE)
972
0
      return -1;
973
0
    data_size_byte -= COLLECTION_HDR_SIZE;
974
0
  }
975
0
  return be_to_cpu_data_type(data, data_size_byte, data_type);
976
0
}