File: ziq2.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 (162 lines) | stat: -rw-r--r-- 4,777 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
#include "ziq2.h"
#ifdef BUILD_ZIQ2
#include "logger.h"
#include "dsp/buffer.h"
#include <volk/volk.h>

namespace ziq2
{
#ifdef _WIN32
#define __attribute__(x)
#pragma pack(push, 1)
#endif
    struct ziq2_info_pkt_hdr_t
    {
        uint64_t samplerate;
    } __attribute__((packed));
#ifdef _WIN32
#pragma pack(pop)
#endif

#ifdef _WIN32
#define __attribute__(x)
#pragma pack(push, 1)
#endif
    struct ziq2_iq_pkt_hdr_t
    {
        uint8_t bit_depth;
        float scale;
    } __attribute__((packed));
#ifdef _WIN32
#pragma pack(pop)
#endif

    int ziq2_write_info_pkt(uint8_t *output, uint64_t samplerate, bool sync)
    {
        if (sync)
        {
            output[0] = 0x1a;
            output[1] = 0xcf;
            output[2] = 0xfc;
            output[3] = 0x1d;
        }

        ziq2_pkt_hdr_t *mdr = (ziq2_pkt_hdr_t *)&output[sync ? 4 : 0];
        mdr->pkt_type = ZIQ2_PKT_INFO;

        ziq2_info_pkt_hdr_t *hdr = (ziq2_info_pkt_hdr_t *)&output[(sync ? 4 : 0) + sizeof(ziq2_pkt_hdr_t)];
        hdr->samplerate = samplerate;

        mdr->pkt_size = sizeof(ziq2_info_pkt_hdr_t);

        return (sync ? 4 : 0) + sizeof(ziq2_pkt_hdr_t) + mdr->pkt_size;
    }

    int ziq2_write_iq_pkt(uint8_t *output, complex_t *input, float *mag_buf, int nsamples, int bit_depth, bool sync)
    {
        if (sync)
        {
            output[0] = 0x1a;
            output[1] = 0xcf;
            output[2] = 0xfc;
            output[3] = 0x1d;
        }

        ziq2_pkt_hdr_t *mdr = (ziq2_pkt_hdr_t *)&output[sync ? 4 : 0];
        mdr->pkt_type = ZIQ2_PKT_IQ;

        ziq2_iq_pkt_hdr_t *hdr = (ziq2_iq_pkt_hdr_t *)&output[(sync ? 4 : 0) + sizeof(ziq2_pkt_hdr_t)];

        uint32_t max_index = 0;
        volk_32fc_magnitude_32f(mag_buf, (lv_32fc_t *)input, nsamples);
        volk_32f_index_max_32u(&max_index, mag_buf, nsamples);

        float scale = 0;

        if (bit_depth == 8)
            scale = 127.0f / mag_buf[max_index];
        else if (bit_depth == 16)
            scale = 32767.0f / mag_buf[max_index];

        hdr->bit_depth = bit_depth;
        hdr->scale = scale;

        int final_size = (sync ? 4 : 0) + sizeof(ziq2_pkt_hdr_t) + sizeof(ziq2_iq_pkt_hdr_t);

        if (bit_depth == 8)
        {
            volk_32f_s32f_convert_8i((int8_t *)&output[final_size], (float *)input, scale, nsamples * 2);
            final_size = nsamples * sizeof(int8_t) * 2;
        }
        else if (bit_depth == 16)
        {
            volk_32f_s32f_convert_16i((int16_t *)&output[final_size], (float *)input, scale, nsamples * 2);
            final_size = nsamples * sizeof(int16_t) * 2;
        }

        mdr->pkt_size = sizeof(ziq2_iq_pkt_hdr_t) + final_size;

        return (sync ? 4 : 0) + sizeof(ziq2_pkt_hdr_t) + mdr->pkt_size;
    }

    int ziq2_write_file_hdr(uint8_t *output, uint64_t samplerate, bool sync)
    {
        char *sig = (char *)&output[0];
        sig[0] = 'Z';
        sig[1] = 'I';
        sig[2] = 'Q';
        sig[3] = '2';

        return 4 + ziq2_write_info_pkt(&output[4], samplerate, sync);
    }

    void ziq2_read_info_pkt(uint8_t *input, uint64_t *samplerate)
    {
        ziq2_pkt_hdr_t *mdr = (ziq2_pkt_hdr_t *)&input[0];
        ziq2_info_pkt_hdr_t *hdr = (ziq2_info_pkt_hdr_t *)&input[sizeof(ziq2_pkt_hdr_t)];
        *samplerate = hdr->samplerate;
    }

    void ziq2_read_iq_pkt(uint8_t *input, complex_t *output, int *nsamples)
    {
        ziq2_pkt_hdr_t *mdr = (ziq2_pkt_hdr_t *)&input[0];
        ziq2_iq_pkt_hdr_t *hdr = (ziq2_iq_pkt_hdr_t *)&input[sizeof(ziq2_pkt_hdr_t)];
        *nsamples = ((mdr->pkt_size - sizeof(ziq2_iq_pkt_hdr_t)) * 8) / (hdr->bit_depth * 2);

        int final_size = sizeof(ziq2_pkt_hdr_t) + sizeof(ziq2_iq_pkt_hdr_t);

        if (hdr->bit_depth == 8)
            volk_8i_s32f_convert_32f((float *)output, (int8_t *)&input[final_size], hdr->scale, *nsamples * 2);
        else if (hdr->bit_depth == 16)
            volk_16i_s32f_convert_32f((float *)output, (int16_t *)&input[final_size], hdr->scale, *nsamples * 2);
    }

    bool ziq2_is_valid_ziq2(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]) == ZIQ2_SIGNATURE;
    }

    uint64_t ziq2_try_parse_header(std::string file)
    {
        std::ifstream stream(file, std::ios::binary);
        stream.seekg(8);

        ziq2_pkt_hdr_t mdr;
        stream.read((char *)&mdr, sizeof(ziq2_pkt_hdr_t));

        if (mdr.pkt_type == ZIQ2_PKT_INFO)
        {
            ziq2_info_pkt_hdr_t hdr;
            stream.read((char *)&hdr, sizeof(ziq2_info_pkt_hdr_t));
            return hdr.samplerate;
        }

        return 0;
    }
};
#endif