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
|
/*
* This file is part of the bladeRF project:
* http://www.github.com/nuand/bladeRF
*
* Copyright (C) 2017 Nuand LLC
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string.h>
#include <libbladeRF.h>
#include "helpers/interleave.h"
size_t _interleave_calc_num_channels(bladerf_channel_layout layout)
{
switch (layout) {
case BLADERF_RX_X1:
case BLADERF_TX_X1:
return 1;
case BLADERF_RX_X2:
case BLADERF_TX_X2:
return 2;
}
return 0;
}
size_t _interleave_calc_bytes_per_sample(bladerf_format format)
{
switch (format) {
case BLADERF_FORMAT_SC8_Q7:
case BLADERF_FORMAT_SC8_Q7_META:
return 2;
case BLADERF_FORMAT_SC16_Q11:
case BLADERF_FORMAT_SC16_Q11_META:
case BLADERF_FORMAT_PACKET_META:
return 4;
}
return 0;
}
size_t _interleave_calc_metadata_bytes(bladerf_format format)
{
switch (format) {
case BLADERF_FORMAT_SC8_Q7_META:
case BLADERF_FORMAT_SC16_Q11_META:
case BLADERF_FORMAT_PACKET_META:
return 0x10;
case BLADERF_FORMAT_SC8_Q7:
case BLADERF_FORMAT_SC16_Q11:
return 0;
}
return 0;
}
int _interleave_interleave_buf(bladerf_channel_layout layout,
bladerf_format format,
unsigned int buffer_size,
void *samples)
{
void *buf;
uint8_t *srcptr, *dstptr;
size_t num_channels = _interleave_calc_num_channels(layout);
size_t samp_size, meta_size, samps_per_ch;
size_t srcidx, dstidx, samp, ch;
// Easy:
if (num_channels < 2) {
return 0;
}
// Placeholder for an actually efficient algorithm
samp_size = _interleave_calc_bytes_per_sample(format);
meta_size = _interleave_calc_metadata_bytes(format);
samps_per_ch = buffer_size / num_channels;
buf = malloc(samp_size * buffer_size);
srcptr = samples;
dstptr = buf;
if (NULL == buf) {
return BLADERF_ERR_MEM;
}
// Copy metadata if applicable
if (meta_size > 0) {
memcpy(dstptr, srcptr, meta_size);
srcptr += meta_size;
dstptr += meta_size;
samps_per_ch -= (meta_size / samp_size / num_channels);
}
// Iterate...
for (ch = 0; ch < num_channels; ++ch) {
srcidx = samps_per_ch * ch;
for (samp = 0; samp < samps_per_ch; ++samp) {
dstidx = (samp * num_channels) + ch;
memcpy(dstptr + (dstidx * samp_size),
srcptr + ((srcidx + samp) * samp_size),
samp_size);
}
}
// Copy back...
memcpy(samples, buf, buffer_size * samp_size);
// Done
free(buf);
return 0;
}
int _interleave_deinterleave_buf(bladerf_channel_layout layout,
bladerf_format format,
unsigned int buffer_size,
void *samples)
{
void *buf;
uint8_t *srcptr, *dstptr;
size_t num_channels = _interleave_calc_num_channels(layout);
size_t samp_size, meta_size, samps_per_ch;
size_t srcidx, dstidx, samp, ch;
// Easy:
if (num_channels < 2) {
return 0;
}
// Placeholder for an actually efficient algorithm
samp_size = _interleave_calc_bytes_per_sample(format);
meta_size = _interleave_calc_metadata_bytes(format);
samps_per_ch = buffer_size / num_channels;
buf = malloc(samp_size * buffer_size);
srcptr = samples;
dstptr = buf;
if (NULL == buf) {
return BLADERF_ERR_MEM;
}
// Copy metadata if applicable
if (meta_size > 0) {
memcpy(dstptr, srcptr, meta_size);
srcptr += meta_size;
dstptr += meta_size;
samps_per_ch -= (meta_size / samp_size / num_channels);
}
// Iterate...
for (samp = 0; samp < samps_per_ch; ++samp) {
srcidx = num_channels * samp;
for (ch = 0; ch < num_channels; ++ch) {
dstidx = (samps_per_ch * ch) + samp;
memcpy(dstptr + (dstidx * samp_size),
srcptr + ((srcidx + ch) * samp_size), samp_size);
}
}
// Copy back...
memcpy(samples, buf, buffer_size * samp_size);
// Done
free(buf);
return 0;
}
|