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
820
#  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
3.00k
{
114
3.00k
  union collection_id cid;
115
116
3.00k
  cid.collection_id = be16_to_cpu(col->collection_id);
117
3.00k
  return cid.field.subservice;
118
3.00k
}
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
8.29k
{
167
8.29k
  return be16_to_cpu(col->collection_length);
168
8.29k
}
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
5.28k
{
181
5.28k
  return COLLECTION_HDR_SIZE + cmp_col_get_data_length(col);
182
5.28k
}
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
3.00k
{
384
3.00k
  switch (subservice) {
385
61
  case SST_NCxx_S_SCIENCE_IMAGETTE:
386
61
    return DATA_TYPE_IMAGETTE;
387
57
  case SST_NCxx_S_SCIENCE_SAT_IMAGETTE:
388
57
    return DATA_TYPE_SAT_IMAGETTE;
389
72
  case SST_NCxx_S_SCIENCE_OFFSET:
390
72
    return DATA_TYPE_OFFSET;
391
327
  case SST_NCxx_S_SCIENCE_BACKGROUND:
392
327
    return DATA_TYPE_BACKGROUND;
393
126
  case SST_NCxx_S_SCIENCE_SMEARING:
394
126
    return DATA_TYPE_SMEARING;
395
169
  case SST_NCxx_S_SCIENCE_S_FX:
396
169
    return DATA_TYPE_S_FX;
397
147
  case SST_NCxx_S_SCIENCE_S_FX_EFX:
398
147
    return DATA_TYPE_S_FX_EFX;
399
213
  case SST_NCxx_S_SCIENCE_S_FX_NCOB:
400
213
    return DATA_TYPE_S_FX_NCOB;
401
236
  case SST_NCxx_S_SCIENCE_S_FX_EFX_NCOB_ECOB:
402
236
    return DATA_TYPE_S_FX_EFX_NCOB_ECOB;
403
125
  case SST_NCxx_S_SCIENCE_L_FX:
404
125
    return DATA_TYPE_L_FX;
405
260
  case SST_NCxx_S_SCIENCE_L_FX_EFX:
406
260
    return DATA_TYPE_L_FX_EFX;
407
97
  case SST_NCxx_S_SCIENCE_L_FX_NCOB:
408
97
    return DATA_TYPE_L_FX_NCOB;
409
140
  case SST_NCxx_S_SCIENCE_L_FX_EFX_NCOB_ECOB:
410
140
    return DATA_TYPE_L_FX_EFX_NCOB_ECOB;
411
302
  case SST_NCxx_S_SCIENCE_F_FX:
412
302
    return DATA_TYPE_F_FX;
413
69
  case SST_NCxx_S_SCIENCE_F_FX_EFX:
414
69
    return DATA_TYPE_F_FX_EFX;
415
49
  case SST_NCxx_S_SCIENCE_F_FX_NCOB:
416
49
    return DATA_TYPE_F_FX_NCOB;
417
84
  case SST_NCxx_S_SCIENCE_F_FX_EFX_NCOB_ECOB:
418
84
    return DATA_TYPE_F_FX_EFX_NCOB_ECOB;
419
123
  case SST_FCx_S_SCIENCE_IMAGETTE:
420
123
    return DATA_TYPE_F_CAM_IMAGETTE;
421
139
  case SST_FCx_S_SCIENCE_OFFSET_VALUES:
422
139
    return DATA_TYPE_F_CAM_OFFSET;
423
196
  case SST_FCx_S_BACKGROUND_VALUES:
424
196
    return DATA_TYPE_F_CAM_BACKGROUND;
425
13
  default:
426
13
    return DATA_TYPE_UNKNOWN;
427
3.00k
  };
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
5.38k
{
529
5.38k
  size_t sample_size = 0;
530
531
5.38k
  switch (data_type) {
532
183
  case DATA_TYPE_IMAGETTE:
533
197
  case DATA_TYPE_IMAGETTE_ADAPTIVE:
534
302
  case DATA_TYPE_SAT_IMAGETTE:
535
335
  case DATA_TYPE_SAT_IMAGETTE_ADAPTIVE:
536
625
  case DATA_TYPE_F_CAM_IMAGETTE:
537
628
  case DATA_TYPE_F_CAM_IMAGETTE_ADAPTIVE:
538
628
    sample_size = sizeof(uint16_t);
539
628
    break;
540
119
  case DATA_TYPE_OFFSET:
541
366
  case DATA_TYPE_F_CAM_OFFSET:
542
366
    sample_size = sizeof(struct offset);
543
366
    break;
544
554
  case DATA_TYPE_BACKGROUND:
545
873
  case DATA_TYPE_F_CAM_BACKGROUND:
546
873
    sample_size = sizeof(struct background);
547
873
    break;
548
224
  case DATA_TYPE_SMEARING:
549
224
    sample_size = sizeof(struct smearing);
550
224
    break;
551
306
  case DATA_TYPE_S_FX:
552
306
    sample_size = sizeof(struct s_fx);
553
306
    break;
554
273
  case DATA_TYPE_S_FX_EFX:
555
273
    sample_size = sizeof(struct s_fx_efx);
556
273
    break;
557
350
  case DATA_TYPE_S_FX_NCOB:
558
350
    sample_size = sizeof(struct s_fx_ncob);
559
350
    break;
560
428
  case DATA_TYPE_S_FX_EFX_NCOB_ECOB:
561
428
    sample_size = sizeof(struct s_fx_efx_ncob_ecob);
562
428
    break;
563
222
  case DATA_TYPE_L_FX:
564
222
    sample_size = sizeof(struct l_fx);
565
222
    break;
566
479
  case DATA_TYPE_L_FX_EFX:
567
479
    sample_size = sizeof(struct l_fx_efx);
568
479
    break;
569
176
  case DATA_TYPE_L_FX_NCOB:
570
176
    sample_size = sizeof(struct l_fx_ncob);
571
176
    break;
572
246
  case DATA_TYPE_L_FX_EFX_NCOB_ECOB:
573
246
    sample_size = sizeof(struct l_fx_efx_ncob_ecob);
574
246
    break;
575
472
  case DATA_TYPE_F_FX:
576
472
    sample_size = sizeof(struct f_fx);
577
472
    break;
578
111
  case DATA_TYPE_F_FX_EFX:
579
111
    sample_size = sizeof(struct f_fx_efx);
580
111
    break;
581
78
  case DATA_TYPE_F_FX_NCOB:
582
78
    sample_size = sizeof(struct f_fx_ncob);
583
78
    break;
584
138
  case DATA_TYPE_F_FX_EFX_NCOB_ECOB:
585
138
    sample_size = sizeof(struct f_fx_efx_ncob_ecob);
586
138
    break;
587
0
  case DATA_TYPE_CHUNK:
588
12
  case DATA_TYPE_UNKNOWN:
589
12
  default:
590
12
    debug_print("Error: Compression data type is not supported.");
591
12
    break;
592
5.38k
  }
593
5.38k
  return sample_size;
594
5.38k
}
595
596
597
static uint32_t be24_to_cpu(uint32_t a)
598
2
{
599
2
#ifdef __LITTLE_ENDIAN
600
2
  return be32_to_cpu(a) >> 8;
601
#else
602
  return a;
603
#endif /* __LITTLE_ENDIAN */
604
2
}
605
606
607
static void be_to_cpus_16(uint16_t *a, uint32_t samples)
608
97
{
609
97
  uint32_t i;
610
611
264
  for (i = 0; i < samples; i++) {
612
167
    uint16_t tmp;
613
614
167
    tmp = be16_to_cpu(get_unaligned(&a[i]));
615
167
    put_unaligned(tmp, &a[i]);
616
167
  }
617
97
}
618
619
620
static void be_to_cpus_offset(struct offset *a, uint32_t samples)
621
48
{
622
48
  uint32_t i;
623
624
55
  for (i = 0; i < samples; i++) {
625
7
    a[i].mean = be32_to_cpu(a[i].mean);
626
7
    a[i].variance = be32_to_cpu(a[i].variance);
627
7
  }
628
48
}
629
630
631
static void be_to_cpus_background(struct background *a, uint32_t samples)
632
168
{
633
168
  uint32_t i;
634
635
169
  for (i = 0; i < samples; i++) {
636
1
    a[i].mean = be32_to_cpu(a[i].mean);
637
1
    a[i].variance = be32_to_cpu(a[i].variance);
638
1
    a[i].outlier_pixels = be16_to_cpu(a[i].outlier_pixels);
639
1
  }
640
168
}
641
642
643
static void be_to_cpus_smearing(struct smearing *a, uint32_t samples)
644
23
{
645
23
  uint32_t i;
646
647
30
  for (i = 0; i < samples; i++) {
648
7
    a[i].mean = be32_to_cpu(a[i].mean);
649
7
    a[i].variance_mean = be16_to_cpu(a[i].variance_mean);
650
7
    a[i].outlier_pixels = be16_to_cpu(a[i].outlier_pixels);
651
7
  }
652
23
}
653
654
655
static void be_to_cpus_s_fx(struct s_fx *a, uint32_t samples)
656
27
{
657
27
  uint32_t i;
658
659
32
  for (i = 0; i < samples; i++)
660
27
    a[i].fx = be32_to_cpu(a[i].fx);
661
27
}
662
663
664
static void be_to_cpus_s_fx_efx(struct s_fx_efx *a, uint32_t samples)
665
21
{
666
21
  uint32_t i;
667
668
24
  for (i = 0; i < samples; i++) {
669
3
    a[i].fx = be32_to_cpu(a[i].fx);
670
3
    a[i].efx = be32_to_cpu(a[i].efx);
671
3
  }
672
21
}
673
674
675
static void be_to_cpus_s_fx_ncob(struct s_fx_ncob *a, uint32_t samples)
676
75
{
677
75
  uint32_t i;
678
679
76
  for (i = 0; i < samples; i++) {
680
1
    a[i].fx = be32_to_cpu(a[i].fx);
681
1
    a[i].ncob_x = be32_to_cpu(a[i].ncob_x);
682
1
    a[i].ncob_y = be32_to_cpu(a[i].ncob_y);
683
1
  }
684
75
}
685
686
687
static void be_to_cpus_s_fx_efx_ncob_ecob(struct s_fx_efx_ncob_ecob *a, uint32_t samples)
688
44
{
689
44
  uint32_t i;
690
691
44
  for (i = 0; i < samples; i++) {
692
0
    a[i].fx = be32_to_cpu(a[i].fx);
693
0
    a[i].ncob_x = be32_to_cpu(a[i].ncob_x);
694
0
    a[i].ncob_y = be32_to_cpu(a[i].ncob_y);
695
0
    a[i].efx = be32_to_cpu(a[i].efx);
696
0
    a[i].ecob_x = be32_to_cpu(a[i].ecob_x);
697
0
    a[i].ecob_y = be32_to_cpu(a[i].ecob_y);
698
0
  }
699
44
}
700
701
702
static void be_to_cpus_l_fx(struct l_fx *a, uint32_t samples)
703
23
{
704
23
  uint32_t i;
705
706
24
  for (i = 0; i < samples; i++) {
707
1
    a[i].exp_flags = be24_to_cpu(a[i].exp_flags);
708
1
    a[i].fx = be32_to_cpu(a[i].fx);
709
1
    a[i].fx_variance = be32_to_cpu(a[i].fx_variance);
710
1
  }
711
23
}
712
713
714
static void be_to_cpus_l_fx_efx(struct l_fx_efx *a, uint32_t samples)
715
40
{
716
40
  uint32_t i;
717
718
41
  for (i = 0; i < samples; i++) {
719
1
    a[i].exp_flags = be24_to_cpu(a[i].exp_flags);
720
1
    a[i].fx = be32_to_cpu(a[i].fx);
721
1
    a[i].efx = be32_to_cpu(a[i].efx);
722
1
    a[i].fx_variance = be32_to_cpu(a[i].fx_variance);
723
1
  }
724
40
}
725
726
727
static void be_to_cpus_l_fx_ncob(struct l_fx_ncob *a, uint32_t samples)
728
18
{
729
18
  uint32_t i;
730
731
18
  for (i = 0; i < samples; i++) {
732
0
    a[i].exp_flags = be24_to_cpu(a[i].exp_flags);
733
0
    a[i].fx = be32_to_cpu(a[i].fx);
734
0
    a[i].ncob_x = be32_to_cpu(a[i].ncob_x);
735
0
    a[i].ncob_y = be32_to_cpu(a[i].ncob_y);
736
0
    a[i].fx_variance = be32_to_cpu(a[i].fx_variance);
737
0
    a[i].cob_x_variance = be32_to_cpu(a[i].cob_x_variance);
738
0
    a[i].cob_y_variance = be32_to_cpu(a[i].cob_y_variance);
739
0
  }
740
18
}
741
742
743
static void be_to_cpus_l_fx_efx_ncob_ecob(struct l_fx_efx_ncob_ecob *a, uint32_t samples)
744
33
{
745
33
  uint32_t i;
746
747
33
  for (i = 0; i < samples; i++) {
748
0
    a[i].exp_flags = be24_to_cpu(a[i].exp_flags);
749
0
    a[i].fx = be32_to_cpu(a[i].fx);
750
0
    a[i].ncob_x = be32_to_cpu(a[i].ncob_x);
751
0
    a[i].ncob_y = be32_to_cpu(a[i].ncob_y);
752
0
    a[i].efx = be32_to_cpu(a[i].efx);
753
0
    a[i].ecob_x = be32_to_cpu(a[i].ecob_x);
754
0
    a[i].ecob_y = be32_to_cpu(a[i].ecob_y);
755
0
    a[i].fx_variance = be32_to_cpu(a[i].fx_variance);
756
0
    a[i].cob_x_variance = be32_to_cpu(a[i].cob_x_variance);
757
0
    a[i].cob_y_variance = be32_to_cpu(a[i].cob_y_variance);
758
0
  }
759
33
}
760
761
762
static void be_to_cpus_f_fx(struct f_fx *a, uint32_t samples)
763
132
{
764
132
  uint32_t i;
765
766
225
  for (i = 0; i < samples; i++)
767
132
    a[i].fx = be32_to_cpu(a[i].fx);
768
132
}
769
770
771
static void be_to_cpus_f_fx_efx(struct f_fx_efx *a, uint32_t samples)
772
26
{
773
26
  uint32_t i;
774
775
33
  for (i = 0; i < samples; i++) {
776
7
    a[i].fx = be32_to_cpu(a[i].fx);
777
7
    a[i].efx = be32_to_cpu(a[i].efx);
778
7
  }
779
26
}
780
781
782
static void be_to_cpus_f_fx_ncob(struct f_fx_ncob *a, uint32_t samples)
783
19
{
784
19
  uint32_t i;
785
786
20
  for (i = 0; i < samples; i++) {
787
1
    a[i].fx = be32_to_cpu(a[i].fx);
788
1
    a[i].ncob_x = be32_to_cpu(a[i].ncob_x);
789
1
    a[i].ncob_y = be32_to_cpu(a[i].ncob_y);
790
1
  }
791
19
}
792
793
794
static void be_to_cpus_f_fx_efx_ncob_ecob(struct f_fx_efx_ncob_ecob *a, uint32_t samples)
795
26
{
796
26
  uint32_t i;
797
798
26
  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
26
}
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
822
{
826
822
  uint32_t sample_size = (uint32_t)size_of_a_sample(data_type);
827
822
  uint32_t samples;
828
829
822
  if (!data) /* nothing to do */
830
0
    return 0;
831
832
822
  if (!sample_size)
833
1
    return -1;
834
835
821
  if (data_size_byte % sample_size) {
836
1
    debug_print("Error: Can not convert data size in samples.");
837
1
    return -1;
838
1
  }
839
820
  samples = data_size_byte / sample_size;
840
841
820
  if (CMP_IS_BIG_ENDIAN)
842
0
    return 0;
843
844
820
  switch (data_type) {
845
22
  case DATA_TYPE_IMAGETTE:
846
24
  case DATA_TYPE_IMAGETTE_ADAPTIVE:
847
47
  case DATA_TYPE_SAT_IMAGETTE:
848
48
  case DATA_TYPE_SAT_IMAGETTE_ADAPTIVE:
849
96
  case DATA_TYPE_F_CAM_IMAGETTE:
850
97
  case DATA_TYPE_F_CAM_IMAGETTE_ADAPTIVE:
851
97
    be_to_cpus_16(data, samples);
852
97
    break;
853
21
  case DATA_TYPE_OFFSET:
854
48
  case DATA_TYPE_F_CAM_OFFSET:
855
48
    be_to_cpus_offset(data, samples);
856
48
    break;
857
99
  case DATA_TYPE_BACKGROUND:
858
168
  case DATA_TYPE_F_CAM_BACKGROUND:
859
168
    be_to_cpus_background(data, samples);
860
168
    break;
861
23
  case DATA_TYPE_SMEARING:
862
23
    be_to_cpus_smearing(data, samples);
863
23
    break;
864
27
  case DATA_TYPE_S_FX:
865
27
    be_to_cpus_s_fx(data, samples);
866
27
    break;
867
21
  case DATA_TYPE_S_FX_EFX:
868
21
    be_to_cpus_s_fx_efx(data, samples);
869
21
    break;
870
75
  case DATA_TYPE_S_FX_NCOB:
871
75
    be_to_cpus_s_fx_ncob(data, samples);
872
75
    break;
873
44
  case DATA_TYPE_S_FX_EFX_NCOB_ECOB:
874
44
    be_to_cpus_s_fx_efx_ncob_ecob(data, samples);
875
44
    break;
876
23
  case DATA_TYPE_L_FX:
877
23
    be_to_cpus_l_fx(data, samples);
878
23
    break;
879
40
  case DATA_TYPE_L_FX_EFX:
880
40
    be_to_cpus_l_fx_efx(data, samples);
881
40
    break;
882
18
  case DATA_TYPE_L_FX_NCOB:
883
18
    be_to_cpus_l_fx_ncob(data, samples);
884
18
    break;
885
33
  case DATA_TYPE_L_FX_EFX_NCOB_ECOB:
886
33
    be_to_cpus_l_fx_efx_ncob_ecob(data, samples);
887
33
    break;
888
132
  case DATA_TYPE_F_FX:
889
132
    be_to_cpus_f_fx(data, samples);
890
132
    break;
891
26
  case DATA_TYPE_F_FX_EFX:
892
26
    be_to_cpus_f_fx_efx(data, samples);
893
26
    break;
894
19
  case DATA_TYPE_F_FX_NCOB:
895
19
    be_to_cpus_f_fx_ncob(data, samples);
896
19
    break;
897
26
  case DATA_TYPE_F_FX_EFX_NCOB_ECOB:
898
26
    be_to_cpus_f_fx_efx_ncob_ecob(data, samples);
899
26
    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
820
  }
906
907
820
  return 0;
908
820
}
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
796
{
924
796
  uint8_t *col_p = chunk;
925
926
796
  if (!chunk) /* nothing to do */
927
0
    return 0;
928
929
796
  if (chunk_size < COLLECTION_HDR_SIZE)
930
0
    return -1;
931
932
1.60k
  while (col_p <= chunk + chunk_size - COLLECTION_HDR_SIZE) {
933
814
    struct collection_hdr *col_hdr = (struct collection_hdr *)col_p;
934
814
    enum cmp_data_type data_type = convert_subservice_to_cmp_data_type(cmp_col_get_subservice(col_hdr));
935
814
    uint32_t data_size = cmp_col_get_data_length(col_hdr);
936
937
814
    col_p += cmp_col_get_size(col_hdr);
938
814
    if (col_p > chunk + chunk_size)  /* over read chunk? */
939
1
      break;
940
941
813
    if (be_to_cpu_data_type(col_hdr->entry, data_size, data_type))
942
2
      return -1;
943
813
  }
944
945
794
  if (col_p != chunk + chunk_size) {
946
51
    debug_print("Error: The chunk size does not match the sum of the collection sizes.");
947
51
    return -1;
948
51
  }
949
950
743
  return 0;
951
794
}
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
9
{
968
9
  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
9
  return be_to_cpu_data_type(data, data_size_byte, data_type);
976
9
}