]> sigrok.org Git - libsigrok.git/blame - filter.c
A few more random 'probe' to 'channel' changes.
[libsigrok.git] / filter.c
CommitLineData
a1bb33af 1/*
50985c20 2 * This file is part of the libsigrok project.
a1bb33af 3 *
c73d2ea4 4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
a1bb33af
UH
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <stdlib.h>
21#include <stdint.h>
22#include <string.h>
305de92e 23#include <glib.h>
45c59c8b
BV
24#include "libsigrok.h"
25#include "libsigrok-internal.h"
a1bb33af 26
3544f848 27#define LOG_PREFIX "filter"
a885ce3e 28
393fb9cb
UH
29/**
30 * @file
31 *
fca75cbb 32 * Helper functions to filter out unused channels from samples.
393fb9cb
UH
33 */
34
7b870c38 35/**
fca75cbb 36 * @defgroup grp_filter Channel filter
7b870c38 37 *
fca75cbb 38 * Helper functions to filter out unused channels from samples.
7b870c38
UH
39 *
40 * @{
41 */
42
25b4fb85 43/**
fca75cbb 44 * Remove unused channels from samples.
25b4fb85 45 *
fca75cbb 46 * Convert sample from maximum channels -- the way the hardware driver sent
a1bb33af 47 * it -- to a sample taking up only as much space as required, with
fca75cbb 48 * unused channels removed.
25b4fb85 49 *
fca75cbb 50 * The "unit size" is the number of bytes used to store channel values.
488a13b1 51 * For example, a unit size of 1 means one byte is used (which can store
fca75cbb
UH
52 * 8 channel values, each of them is 1 bit). A unit size of 2 means we can
53 * store 16 channel values, 3 means we can store 24 channel values, and so on.
488a13b1
UH
54 *
55 * If the data coming from the logic analyzer has a unit size of 4 for
fca75cbb 56 * example (as the device has 32 channels), but only 2 of them are actually
488a13b1
UH
57 * used in an acquisition, this function can convert the samples to only
58 * use up 1 byte per sample (unit size = 1) instead of 4 bytes per sample.
59 *
fca75cbb
UH
60 * The output will contain the channel values in the order specified via the
61 * channellist. For example, if in_unitsize = 4, channellist = [5, 16, 30], and
488a13b1 62 * out_unitsize = 1, then the output samples (each of them one byte in size)
fca75cbb
UH
63 * will have the following format: bit 0 = value of channel 5, bit 1 = value
64 * of channel 16, bit 2 = value of channel 30. Unused bit(s) in the output byte(s)
488a13b1
UH
65 * are zero.
66 *
67 * The caller must make sure that length_in is not bigger than the memory
68 * actually allocated for the input data (data_in), as this function does
69 * not check that.
70 *
71 * @param in_unitsize The unit size (>= 1) of the input (data_in).
72 * @param out_unitsize The unit size (>= 1) the output shall have (data_out).
7a6ec0c3
UH
73 * The requested unit size must be big enough to hold as
74 * much data as is specified by the number of enabled
fca75cbb
UH
75 * channels in 'channellist'.
76 * @param channel_array Pointer to a list of channel numbers, numbered starting
ca0938c5 77 * from 0. The list is terminated with -1.
488a13b1
UH
78 * @param data_in Pointer to the input data buffer. Must not be NULL.
79 * @param length_in The input data length (>= 1), in number of bytes.
80 * @param data_out Variable which will point to the newly allocated buffer
81 * of output data. The caller is responsible for g_free()'ing
82 * the buffer when it's no longer needed. Must not be NULL.
83 * @param length_out Pointer to the variable which will contain the output
84 * data length (in number of bytes) when the function
85 * returns SR_OK. Must not be NULL.
86 *
87 * @return SR_OK upon success, SR_ERR_MALLOC upon memory allocation errors,
88 * or SR_ERR_ARG upon invalid arguments.
89 * If something other than SR_OK is returned, the values of
90 * out_unitsize, data_out, and length_out are undefined.
9fb5f2df 91 *
53f05fa8 92 * @since 0.2.0
a1bb33af 93 */
f3ca73ed 94SR_API int sr_filter_channels(unsigned int in_unitsize, unsigned int out_unitsize,
fca75cbb 95 const GArray *channel_array, const uint8_t *data_in,
054e6709 96 uint64_t length_in, uint8_t **data_out,
1a081ca6 97 uint64_t *length_out)
a1bb33af 98{
afc8e4de 99 unsigned int in_offset, out_offset;
fca75cbb 100 int *channellist, out_bit;
305de92e 101 unsigned int i;
764e7bbf 102 uint8_t *sample_in, *sample_out;
a1bb33af 103
fca75cbb
UH
104 if (!channel_array) {
105 sr_err("%s: channel_array was NULL", __func__);
488a13b1
UH
106 return SR_ERR_ARG;
107 }
fca75cbb 108 channellist = (int *)channel_array->data;
488a13b1
UH
109
110 if (!data_in) {
a885ce3e 111 sr_err("%s: data_in was NULL", __func__);
488a13b1
UH
112 return SR_ERR_ARG;
113 }
114
115 if (!data_out) {
a885ce3e 116 sr_err("%s: data_out was NULL", __func__);
488a13b1
UH
117 return SR_ERR_ARG;
118 }
119
120 if (!length_out) {
a885ce3e 121 sr_err("%s: length_out was NULL", __func__);
488a13b1
UH
122 return SR_ERR_ARG;
123 }
124
fca75cbb
UH
125 /* Are there more channels than the target unit size supports? */
126 if (channel_array->len > out_unitsize * 8) {
127 sr_err("%s: too many channels (%d) for the target unit "
128 "size (%d)", __func__, channel_array->len, out_unitsize);
7a6ec0c3
UH
129 return SR_ERR_ARG;
130 }
131
132 if (!(*data_out = g_try_malloc(length_in))) {
a885ce3e 133 sr_err("%s: data_out malloc failed", __func__);
7a6ec0c3
UH
134 return SR_ERR_MALLOC;
135 }
136
fca75cbb
UH
137 if (channel_array->len == in_unitsize * 8) {
138 /* All channels are used -- no need to compress anything. */
a1bb33af
UH
139 memcpy(*data_out, data_in, length_in);
140 *length_out = length_in;
e46b8fb1 141 return SR_OK;
fdebec21
UH
142 }
143
fca75cbb 144 /* If we reached this point, not all channels are used, so "compress". */
fdebec21
UH
145 in_offset = out_offset = 0;
146 while (in_offset <= length_in - in_unitsize) {
bb9d6116 147 sample_in = (uint8_t *)data_in + in_offset;
764e7bbf
MC
148 sample_out = (*data_out) + out_offset;
149 memset(sample_out, 0, out_unitsize);
150 out_bit = 0;
fca75cbb
UH
151 for (i = 0; i < channel_array->len; i++) {
152 if (sample_in[channellist[i]>>3] & (1 << (channellist[i]&7)))
764e7bbf 153 sample_out[out_bit>>3] |= (1 << (out_bit&7));
fdebec21
UH
154 out_bit++;
155 }
fdebec21
UH
156 in_offset += in_unitsize;
157 out_offset += out_unitsize;
a1bb33af 158 }
fdebec21 159 *length_out = out_offset;
a1bb33af 160
e46b8fb1 161 return SR_OK;
a1bb33af 162}
7b870c38
UH
163
164/** @} */