File: ziq.cpp

package info (click to toggle)
satdump 1.2.2%2Bgb79af48-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,648 kB
  • sloc: cpp: 276,768; ansic: 164,598; lisp: 1,219; sh: 283; xml: 106; makefile: 7
file content (340 lines) | stat: -rw-r--r-- 10,530 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#ifdef BUILD_ZIQ
#include "ziq.h"
#include "logger.h"
#include "dsp/buffer.h"
#include <volk/volk.h>

#define ZIQ_DECOMPRESS_BUFSIZE 8192

namespace ziq
{
    // Writer
    ziq_writer::ziq_writer(ziq_cfg cfg, std::ofstream &stream) : cfg(cfg), stream(stream)
    {
        // Write header
        stream.write((char *)ZIQ_SIGNATURE, 4);
        stream.write((char *)&cfg.is_compressed, 1);
        stream.write((char *)&cfg.bits_per_sample, 1);
        stream.write((char *)&cfg.samplerate, 8);
        uint64_t string_size = cfg.annotation.size();
        stream.write((char *)&string_size, 8);
        stream.write((char *)cfg.annotation.c_str(), string_size);

        // If compresssed, init compression
        if (cfg.is_compressed)
        {
            zstd_ctx = ZSTD_createCCtx();
            ZSTD_CCtx_setParameter(zstd_ctx, ZSTD_c_compressionLevel, zst_level);
            ZSTD_CCtx_setParameter(zstd_ctx, ZSTD_c_checksumFlag, 1);
            ZSTD_CCtx_setParameter(zstd_ctx, ZSTD_c_nbWorkers, zst_workers);

            // Init buffer
            max_buffer_size = dsp::STREAM_BUFFER_SIZE; // Abolute max size. Show never be reached
            output_compressed = new uint8_t[max_buffer_size * sizeof(complex_t)];
        }

        if (cfg.bits_per_sample == 8)
            buffer_i8 = new int8_t[max_buffer_size * 2];
        else if (cfg.bits_per_sample == 16)
            buffer_i16 = new int16_t[max_buffer_size * 2];
    }

    ziq_writer::~ziq_writer()
    {
        ZSTD_freeCCtx(zstd_ctx);

        if (cfg.is_compressed)
            delete[] output_compressed;

        if (cfg.bits_per_sample == 8)
            delete[] buffer_i8;
        else if (cfg.bits_per_sample == 16)
            delete[] buffer_i16;
    }

    int ziq_writer::compress_and_write(uint8_t *input, size_t size)
    {
        zstd_input = {input, size, 0};
        zstd_output = {output_compressed, max_buffer_size, 0};

        while (zstd_input.pos < zstd_input.size)
            ZSTD_compressStream2(zstd_ctx, &zstd_output, &zstd_input, ZSTD_e_continue);

        stream.write((char *)output_compressed, zstd_output.pos);

        return zstd_output.pos;
    }

    int ziq_writer::write(complex_t *input, int size)
    {
        if (cfg.bits_per_sample == 8)
        {
            volk_32f_s32f_convert_8i(buffer_i8, (float *)input, 127, size * 2);

            if (cfg.is_compressed)
            {
                return compress_and_write((uint8_t *)buffer_i8, size * 2 * sizeof(int8_t));
            }
            else
            {
                stream.write((char *)buffer_i8, size * 2 * sizeof(int8_t));
                return size * 2 * sizeof(int8_t);
            }
        }
        else if (cfg.bits_per_sample == 16)
        {
            volk_32f_s32f_convert_16i(buffer_i16, (float *)input, 32767, size * 2);

            if (cfg.is_compressed)
            {
                return compress_and_write((uint8_t *)buffer_i16, size * 2 * sizeof(int16_t));
            }
            else
            {
                stream.write((char *)buffer_i16, size * 2 * sizeof(int16_t));
                return size * 2 * sizeof(int16_t);
            }
        }
        else if (cfg.bits_per_sample == 32)
        {
            if (cfg.is_compressed)
            {
                return compress_and_write((uint8_t *)input, size * sizeof(complex_t));
            }
            else
            {
                stream.write((char *)input, size * sizeof(complex_t));
                return size * sizeof(complex_t);
            }
        }

        return 0;
    }

    // Reader
    ziq_reader::ziq_reader(std::ifstream &stream) : stream(stream)
    {
        // Read header
        char signature[4];
        annotation_size = 0;
        stream.read((char *)signature, 4);
        stream.read((char *)&cfg.is_compressed, 1);
        stream.read((char *)&cfg.bits_per_sample, 1);
        stream.read((char *)&cfg.samplerate, 8);
        stream.read((char *)&annotation_size, 8);
        cfg.annotation.resize(annotation_size);
        stream.read((char *)cfg.annotation.c_str(), annotation_size);

        if (std::string(&signature[0], &signature[4]) != ZIQ_SIGNATURE)
        {
            logger->critical("This file is not a valid ZIQ file!");
            isValid = false;
        }

        // If compresssed, init compression
        if (cfg.is_compressed)
        {
            zstd_ctx = ZSTD_createDCtx();

            // Init buffer
            max_buffer_size = dsp::STREAM_BUFFER_SIZE; // Abolute max size. Show never be reached
            output_decompressed = new uint8_t[max_buffer_size * sizeof(complex_t)];
            compressed_buffer = new uint8_t[ZIQ_DECOMPRESS_BUFSIZE];
        }

        if (cfg.bits_per_sample == 8)
            buffer_i8 = new int8_t[max_buffer_size * 2];
        else if (cfg.bits_per_sample == 16)
            buffer_i16 = new int16_t[max_buffer_size * 2];

        decompressed_cnt = 0;

        isValid = true;
    }

    ziq_reader::~ziq_reader()
    {
        ZSTD_freeDCtx(zstd_ctx);

        if (cfg.is_compressed)
        {
            delete[] output_decompressed;
            delete[] compressed_buffer;
        }

        if (cfg.bits_per_sample == 8)
            delete[] buffer_i8;
        else if (cfg.bits_per_sample == 16)
            delete[] buffer_i16;
    }

    bool ziq_reader::seekg(size_t pos)
    {
        // Move to location in file
        if (cfg.is_compressed)
        {
            size_t err;
            decompressed_cnt = 0;
            if (pos + annotation_size + 22 < (size_t)stream.tellg())
            {
                err = ZSTD_DCtx_reset(zstd_ctx, ZSTD_reset_session_only);
                if (ZSTD_isError(err))
                    return false;
                stream.seekg(annotation_size + 22);
            }
            while ((size_t)stream.tellg() < pos + annotation_size + 22)
            {
                stream.read((char *)compressed_buffer, ZIQ_DECOMPRESS_BUFSIZE);
                zstd_input = {compressed_buffer, (unsigned long)ZIQ_DECOMPRESS_BUFSIZE, 0};
                zstd_output = {output_decompressed, max_buffer_size, 0};
                while (zstd_input.pos < zstd_input.size)
                {
                    err = ZSTD_decompressStream(zstd_ctx, &zstd_output, &zstd_input);
                    if (ZSTD_isError(err))
                        return false;
                }
            }
        }
        else
        {
            stream.seekg(pos + annotation_size + 22);
            return true;
        }

        return true;
    }

    int ziq_reader::decompress_at_least(int size)
    {
        while (decompressed_cnt <= size && !stream.eof())
        {
            stream.read((char *)compressed_buffer, ZIQ_DECOMPRESS_BUFSIZE);

            zstd_input = {compressed_buffer, (unsigned long)ZIQ_DECOMPRESS_BUFSIZE, 0};
            zstd_output = {&output_decompressed[decompressed_cnt], (unsigned long)(max_buffer_size - decompressed_cnt), 0};

            while (zstd_input.pos < zstd_input.size)
            {
                size_t err = ZSTD_decompressStream(zstd_ctx, &zstd_output, &zstd_input);
                if (ZSTD_isError(err))
                {
                    ZSTD_DCtx_reset(zstd_ctx, ZSTD_reset_session_only);
                    break;
                }
            }

            decompressed_cnt += zstd_output.pos;
        }

        if (decompressed_cnt < size)
            return 1;

        return 0;
    }

    int ziq_reader::read_decompressed(uint8_t *buffer, int size)
    {
        if (size <= decompressed_cnt)
        {
            memcpy(buffer, output_decompressed, size);

            if (size < decompressed_cnt)
            {
                memmove(output_decompressed, &output_decompressed[size], decompressed_cnt - size);
                decompressed_cnt -= size;
            }
            else
            {
                decompressed_cnt = 0;
            }
            return 0;
        }
        else
        {
            return 1;
        }
    }

    int ziq_reader::read(complex_t *output, int size)
    {
        if (!isValid)
            return 1;

        if (cfg.bits_per_sample == 8)
        {
            if (cfg.is_compressed)
            {
                decompress_at_least(size * 2 * sizeof(int8_t));
                read_decompressed((uint8_t *)buffer_i8, size * 2 * sizeof(int8_t));
            }
            else
            {
                stream.read((char *)buffer_i8, size * 2 * sizeof(int8_t));
            }

            volk_8i_s32f_convert_32f_u((float *)output, (const int8_t *)buffer_i8, 127, size * 2);
        }
        else if (cfg.bits_per_sample == 16)
        {
            if (cfg.is_compressed)
            {
                decompress_at_least(size * 2 * sizeof(int16_t));
                read_decompressed((uint8_t *)buffer_i16, size * 2 * sizeof(int16_t));
            }
            else
            {
                stream.read((char *)buffer_i16, size * 2 * sizeof(int16_t));
            }

            volk_16i_s32f_convert_32f_u((float *)output, (const int16_t *)buffer_i16, 32767, size * 2);
        }
        else if (cfg.bits_per_sample == 32)
        {
            if (cfg.is_compressed)
            {
                // return compress_and_write((uint8_t *)input, size * sizeof(complex_t));
                decompress_at_least(size * sizeof(complex_t));
                read_decompressed((uint8_t *)output, size * sizeof(complex_t));
            }
            else
            {
                stream.read((char *)output, size * sizeof(complex_t));
            }
        }

        return 0;
    }

    // Util functions
    bool isValidZIQ(std::string file)
    {
        std::ifstream stream(file, std::ios::binary);
        char signature[4];
        stream.read((char *)signature, 4);
        stream.close();

        return std::string(&signature[0], &signature[4]) == ZIQ_SIGNATURE;
    }

    ziq_cfg getCfgFromFile(std::string file)
    {
        ziq_cfg cfg;

        std::ifstream stream(file, std::ios::binary);

        char signature[4];
        stream.read((char *)signature, 4);
        stream.read((char *)&cfg.is_compressed, 1);
        stream.read((char *)&cfg.bits_per_sample, 1);
        stream.read((char *)&cfg.samplerate, 8);
        uint64_t string_size = 0;
        stream.read((char *)&string_size, 8);
        cfg.annotation.resize(string_size);
        stream.read((char *)cfg.annotation.c_str(), string_size);

        stream.close();

        return cfg;
    }
};
#endif