-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream_raw.c
208 lines (177 loc) · 4.89 KB
/
stream_raw.c
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
#include <sys/types.h>
#include <unistd.h>
#include <assert.h>
#include <stdlib.h>
#include "stream_raw.h"
typedef struct _raw_s _raw_t;
struct _raw_s {
// Bytes per sample component (in-phase or quadrature)
uint8_t bytes;
// Function to process arbitrary data into complex samples
void (*process)(const void *, fftw_complex *, uint64_t);
};
static int8_t
stream_raw__init(const stream_handler_t *h,
stream_t *s)
{
_raw_t *r = (_raw_t *) h->data;
s->n_samples = s->stat.st_size / (2 * r->bytes);
return 0;
}
static uint64_t
stream_raw__to_byte_offset(const stream_handler_t *h,
__attribute__((unused)) stream_t *s,
uint64_t offset)
{
_raw_t *r = (_raw_t *) h->data;
return 2 * r->bytes * offset;
}
static uint64_t
stream_raw__to_byte_length(const stream_handler_t *h,
__attribute__((unused)) stream_t *s,
uint64_t length)
{
const _raw_t *r = (const _raw_t *) h->data;
return 2 * r->bytes * length;
}
static int8_t
stream_raw__read(const stream_handler_t *h,
stream_t *s,
uint64_t offset,
uint64_t length,
fftw_complex *out)
{
const _raw_t *r = (const _raw_t *) h->data;
uint64_t byte_offset = stream_raw__to_byte_offset(h, s, offset);
uint64_t byte_length = stream_raw__to_byte_length(h, s, length);
off_t rv;
void *in;
rv = lseek(s->fd, byte_offset, SEEK_SET);
if (rv < 0) {
return rv;
}
in = malloc(byte_length);
rv = read(s->fd, in, byte_length);
if (rv < 0) {
return rv;
}
r->process(in, out, length);
free(in);
return 0;
}
static void
_raw_u8_process(const void *data,
fftw_complex *out,
uint64_t length)
{
const uint8_t *in = (const uint8_t *) data;
uint64_t i;
// See http://cgit.osmocom.org/gr-osmosdr/tree/lib/rtl/rtl_source_c.cc#n176
for (i = 0; i < length; i++) {
out[i][0] = ((float) in[i * 2 + 0] - 127.4f) / 128.0f;
out[i][1] = ((float) in[i * 2 + 1] - 127.4f) / 128.0f;
}
}
static _raw_t raw_u8 = {
.bytes = 1,
.process = &_raw_u8_process,
};
stream_handler_t stream_u8_handler = {
.init = &stream_raw__init,
.to_byte_offset = &stream_raw__to_byte_offset,
.to_byte_length = &stream_raw__to_byte_length,
.read = &stream_raw__read,
.data = &raw_u8,
};
static void
_raw_s8_process(const void *data,
fftw_complex *out,
uint64_t length)
{
const int8_t *in = (const int8_t *) data;
uint64_t i;
for (i = 0; i < length; i++) {
out[i][0] = (-(float) in[i * 2 + 0]) / INT8_MIN;
out[i][1] = (-(float) in[i * 2 + 1]) / INT8_MIN;
}
}
static _raw_t raw_s8 = {
.bytes = 1,
.process = &_raw_s8_process,
};
stream_handler_t stream_s8_handler = {
.init = &stream_raw__init,
.to_byte_offset = &stream_raw__to_byte_offset,
.to_byte_length = &stream_raw__to_byte_length,
.read = &stream_raw__read,
.data = &raw_s8,
};
static void
_raw_s16_process(const void *data,
fftw_complex *out,
uint64_t length)
{
const int16_t *in = (const int16_t *) data;
uint64_t i;
for (i = 0; i < length; i++) {
out[i][0] = (-(float) in[i * 2 + 0]) / INT16_MIN;
out[i][1] = (-(float) in[i * 2 + 1]) / INT16_MIN;
}
}
static _raw_t raw_s16 = {
.bytes = 2,
.process = &_raw_s16_process,
};
stream_handler_t stream_s16_handler = {
.init = &stream_raw__init,
.to_byte_offset = &stream_raw__to_byte_offset,
.to_byte_length = &stream_raw__to_byte_length,
.read = &stream_raw__read,
.data = &raw_s16,
};
static void
_raw_s32_process(const void *data,
fftw_complex *out,
uint64_t length)
{
const int32_t *in = (const int32_t *) data;
uint64_t i;
for (i = 0; i < length; i++) {
out[i][0] = (-(float) in[i * 2 + 0]) / INT32_MIN;
out[i][1] = (-(float) in[i * 2 + 1]) / INT32_MIN;
}
}
static _raw_t raw_s32 = {
.bytes = 4,
.process = &_raw_s32_process,
};
stream_handler_t stream_s32_handler = {
.init = &stream_raw__init,
.to_byte_offset = &stream_raw__to_byte_offset,
.to_byte_length = &stream_raw__to_byte_length,
.read = &stream_raw__read,
.data = &raw_s32,
};
static void
_raw_f32_process(const void *data,
fftw_complex *out,
uint64_t length)
{
const float *in = (const float *) data;
uint64_t i;
for (i = 0; i < length; i++) {
out[i][0] = in[i * 2 + 0];
out[i][1] = in[i * 2 + 1];
}
}
static _raw_t raw_f32 = {
.bytes = 4,
.process = &_raw_f32_process,
};
stream_handler_t stream_f32_handler = {
.init = &stream_raw__init,
.to_byte_offset = &stream_raw__to_byte_offset,
.to_byte_length = &stream_raw__to_byte_length,
.read = &stream_raw__read,
.data = &raw_f32,
};