Instrument Neutral Distributed Interface INDI  1.9.5
stream.c
Go to the documentation of this file.
1 /*
2  * libDSPAU - a digital signal processing library for astronomy usage
3  * Copyright (C) 2017 Ilia Platone <info@iliaplatone.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "dsp.h"
20 
22 {
23  if(stream->buf != NULL) {
24  stream->buf = (dsp_t*)realloc(stream->buf, sizeof(dsp_t) * len);
25  } else {
26  stream->buf = (dsp_t*)malloc(sizeof(dsp_t) * len);
27  }
28 }
29 
30 void dsp_stream_set_buffer(dsp_stream_p stream, void *buffer, int len)
31 {
32  stream->buf = (dsp_t*)buffer;
33  stream->len = len;
34 }
35 
37 {
38  return stream->buf;
39 }
40 
42 {
43  if(stream->buf == NULL)
44  return;
45  free(stream->buf);
46 }
47 
49 {
50  dsp_stream_p stream = (dsp_stream_p)malloc(sizeof(dsp_stream) * 1);
51  stream->buf = (dsp_t*)malloc(sizeof(dsp_t) * 1);
52  stream->sizes = (int*)malloc(sizeof(int) * 1);
53  stream->pixel_sizes = (double*)malloc(sizeof(double) * 1);
54  stream->children = (dsp_stream_p*)malloc(sizeof(dsp_stream_p) * 1);
55  stream->ROI = (dsp_region*)malloc(sizeof(dsp_region) * 1);
56  stream->location = (double*)malloc(sizeof(double) * 3);
57  stream->target = (double*)malloc(sizeof(double) * 3);
58  stream->stars = (dsp_star*)malloc(sizeof(dsp_star) * 1);
59  stream->align_info.offset = (double*)malloc(sizeof(double)*1);
60  stream->align_info.center = (double*)malloc(sizeof(double)*1);
61  stream->align_info.radians = (double*)malloc(sizeof(double)*1);
62  stream->stars_count = 0;
63  stream->child_count = 0;
64  stream->frame_number = 0;
65  stream->parent = NULL;
66  stream->dims = 0;
67  stream->red = -1;
68  stream->len = 1;
69  stream->wavelength = 0;
70  stream->diameter = 1;
71  stream->focal_ratio = 1;
72  stream->samplerate = 0;
73  return stream;
74 }
75 
77 {
78  if(stream == NULL)
79  return;
80  free(stream->sizes);
81  free(stream->pixel_sizes);
82  free(stream->children);
83  free(stream->ROI);
84  free(stream->location);
85  free(stream->target);
86  free(stream);
87 }
88 
90 {
92  int i;
93  for(i = 0; i < stream->dims; i++)
94  dsp_stream_add_dim(dest, abs(stream->sizes[i]));
95  dsp_stream_alloc_buffer(dest, dest->len);
96  dest->wavelength = stream->wavelength;
97  dest->samplerate = stream->samplerate;
98  dest->stars_count = stream->stars_count;
99  dest->diameter = stream->diameter;
100  dest->focal_ratio = stream->focal_ratio;
101  dest->starttimeutc = stream->starttimeutc;
102  dest->align_info = stream->align_info;
103  memcpy(dest->ROI, stream->ROI, sizeof(dsp_region) * stream->dims);
104  memcpy(dest->pixel_sizes, stream->pixel_sizes, sizeof(double) * stream->dims);
105  memcpy(dest->target, stream->target, sizeof(double) * 3);
106  memcpy(dest->location, stream->location, sizeof(double) * 3);
107  memcpy(dest->buf, stream->buf, sizeof(dsp_t) * stream->len);
108  return dest;
109 }
110 
111 void dsp_stream_add_dim(dsp_stream_p stream, int size)
112 {
113  stream->sizes[stream->dims] = size;
114  stream->len *= size;
115  stream->dims ++;
116  stream->ROI = (dsp_region*)realloc(stream->ROI, sizeof(dsp_region) * (stream->dims + 1));
117  stream->sizes = (int*)realloc(stream->sizes, sizeof(int) * (stream->dims + 1));
118  stream->pixel_sizes = (double*)realloc(stream->pixel_sizes, sizeof(double) * (stream->dims + 1));
119  stream->align_info.dims = stream->dims;
120  stream->align_info.offset = (double*)realloc(stream->align_info.offset, sizeof(double)*stream->dims);
121  stream->align_info.center = (double*)realloc(stream->align_info.center, sizeof(double)*stream->dims);
122  stream->align_info.radians = (double*)realloc(stream->align_info.radians, sizeof(double)*(stream->dims-1));
123  stream->align_info.factor = 0.0;
124 }
125 
126 void dsp_stream_del_dim(dsp_stream_p stream, int index)
127 {
128  int* sizes = (int*)malloc(sizeof(int) * stream->dims);
129  int dims = stream->dims;
130  memcpy(sizes, stream->sizes, sizeof(int) * stream->dims);
131  free(stream->sizes);
132  stream->dims = 0;
133  int i;
134  for(i = 0; i < dims; i++) {
135  if(i != index) {
136  dsp_stream_add_dim(stream, abs(sizes[i]));
137  }
138  }
139 }
140 
142 {
143  child->parent = stream;
144  stream->children[stream->child_count] = child;
145  stream->child_count++;
146  stream->children = realloc(stream->children, sizeof(dsp_stream_p) * (stream->child_count + 1));
147 }
148 
149 void dsp_stream_del_child(dsp_stream_p stream, int index)
150 {
151  dsp_stream_p* children = (dsp_stream_p*)malloc(sizeof(dsp_stream_p) * stream->child_count);
152  int child_count = stream->child_count;
153  memcpy(children, stream->children, sizeof(dsp_stream_p*) * stream->child_count);
154  free(stream->children);
155  stream->child_count = 0;
156  int i;
157  for(i = 0; i < child_count; i++) {
158  if(i != index) {
159  dsp_stream_add_child(stream, children[i]);
160  }
161  }
162 }
163 
165 {
166  stream->stars = (dsp_star*)realloc(stream->stars, sizeof(dsp_star)*(stream->stars_count+1));
167  stream->stars[stream->stars_count].center.dims = star.center.dims;
168  stream->stars[stream->stars_count].center.location = star.center.location;
169  stream->stars[stream->stars_count].diameter = star.diameter;
170  stream->stars_count++;
171 }
172 
173 void dsp_stream_del_star(dsp_stream_p stream, int index)
174 {
175  dsp_star* stars = (dsp_star*)malloc(sizeof(dsp_star) * stream->stars_count);
176  int stars_count = stream->stars_count;
177  memcpy(stars, stream->stars, sizeof(dsp_star*) * stream->stars_count);
178  free(stream->stars);
179  stream->stars_count = 0;
180  int i;
181  for(i = 0; i < stars_count; i++) {
182  if(i != index) {
183  dsp_stream_add_star(stream, stars[i]);
184  }
185  }
186 }
187 
188 int* dsp_stream_get_position(dsp_stream_p stream, int index) {
189  int dim = 0;
190  int y = 0;
191  int m = 1;
192  int* pos = (int*)malloc(sizeof(int) * stream->dims);
193  for (dim = 0; dim < stream->dims; dim++) {
194  y = index / m;
195  y %= stream->sizes[dim];
196  m *= stream->sizes[dim];
197  pos[dim] = y;
198  }
199  return pos;
200 }
201 
202 int dsp_stream_set_position(dsp_stream_p stream, int* pos) {
203  int dim = 0;
204  int index = 0;
205  int m = 1;
206  for (dim = 0; dim < stream->dims; dim++) {
207  index += m * pos[dim];
208  m *= stream->sizes[dim];
209  }
210  return index;
211 }
212 
214 {
215  dsp_stream_p stream = dsp_stream_new();
216  int dim, d;
217  for(dim = 0; dim < in->dims; dim++) {
218  dsp_stream_add_dim(stream, abs(in->ROI[dim].len));
219  }
220  dsp_stream_alloc_buffer(stream, stream->len);
221  int *init = dsp_stream_get_position(in, 0);
222  int *stop = dsp_stream_get_position(in, 0);
223  for(d = 0; d < in->dims; d++) {
224  init[d] = in->ROI[d].start;
225  stop[d] = in->ROI[d].start+in->ROI[d].len;
226  }
227  int index = dsp_stream_set_position(in, init);
228  int end = dsp_stream_set_position(in, stop);
229  free(init);
230  free(stop);
231  int x = 0;
232  for (; index<end; index++)
233  {
234  int breakout = 0;
235  int *pos = dsp_stream_get_position(in, index);
236  for(dim = 0; dim < in->dims; dim++) {
237  if(pos[dim]>in->ROI[dim].start&&pos[dim]<in->ROI[dim].start+in->ROI[dim].len)break;
238  breakout++;
239  }
240  if(breakout > 0)continue;
241  stream->buf[x++] = in->buf[index];
242  free(pos);
243  }
244  dsp_buffer_copy(stream->sizes, in->sizes, stream->dims);
245  in->len = stream->len;
246  dsp_stream_alloc_buffer(in, in->len);
247  dsp_buffer_copy(stream->buf, in->buf, stream->len);
248  dsp_stream_free_buffer(stream);
249  dsp_stream_free(stream);
250 }
251 
253 {
254  dsp_stream_p stream = dsp_stream_copy(in);
255  int* offset = (int*)malloc(sizeof(int)*stream->dims);
256  dsp_buffer_copy(stream->align_info.offset, offset, stream->dims);
257  int z = dsp_stream_set_position(stream, offset);
258  free(offset);
259  int k = -z;
260  z = Max(0, z);
261  k = Max(0, k);
262  int len = stream->len-z-k;
263  dsp_t *buf = &stream->buf[k];
264  dsp_t *data = &in->buf[z];
265  memset(in->buf, 0, sizeof(dsp_t)*in->len);
266  memcpy(data, buf, sizeof(dsp_t)*len);
267  dsp_stream_free_buffer(stream);
268  dsp_stream_free(stream);
269 }
270 
271 static void* dsp_stream_scale_th(void* arg)
272 {
273  struct {
274  int cur_th;
275  dsp_stream_p stream;
276  } *arguments = arg;
277  dsp_stream_p stream = arguments->stream;
278  dsp_stream_p in = stream->parent;
279  int cur_th = arguments->cur_th;
280  int start = cur_th * stream->len / DSP_MAX_THREADS;
281  int end = start + stream->len / DSP_MAX_THREADS;
282  end = Min(stream->len, end);
283  int y, d;
284  for(y = start; y < end; y++)
285  {
286  int* pos = dsp_stream_get_position(stream, y);
287  for(d = 0; d < stream->dims; d++) {
288  pos[d] -= stream->sizes[d];
289  pos[d] /= stream->align_info.factor;
290  pos[d] += stream->sizes[d];
291  }
292  int x = dsp_stream_set_position(in, pos);
293  if(x >= 0 && x < in->len)
294  stream->buf[y] += in->buf[x]/(stream->align_info.factor*stream->dims);
295  free(pos);
296  }
297  return NULL;
298 }
299 
301 {
302  int y;
303  dsp_stream_p stream = dsp_stream_copy(in);
304  dsp_buffer_set(stream->buf, stream->len, 0);
305  stream->parent = in;
306  pthread_t *th = malloc(sizeof(pthread_t)*DSP_MAX_THREADS);
307  struct {
308  int cur_th;
309  dsp_stream_p stream;
310  } thread_arguments[DSP_MAX_THREADS];
311  for(y = 0; y < DSP_MAX_THREADS; y++)
312  {
313  thread_arguments[y].cur_th = y;
314  thread_arguments[y].stream = stream;
315  pthread_create(&th[y], NULL, dsp_stream_scale_th, &thread_arguments[y]);
316  }
317  for(y = 0; y < DSP_MAX_THREADS; y++)
318  pthread_join(th[y], NULL);
319  free(th);
320  stream->parent = NULL;
321  dsp_buffer_copy(stream->buf, in->buf, stream->len);
322  dsp_stream_free_buffer(stream);
323  dsp_stream_free(stream);
324 }
325 
326 static void* dsp_stream_rotate_th(void* arg)
327 {
328  struct {
329  int cur_th;
330  dsp_stream_p stream;
331  } *arguments = arg;
332  dsp_stream_p stream = arguments->stream;
333  dsp_stream_p in = stream->parent;
334  int cur_th = arguments->cur_th;
335  int start = cur_th * stream->len / DSP_MAX_THREADS;
336  int end = start + stream->len / DSP_MAX_THREADS;
337  end = Min(stream->len, end);
338  int y;
339  for(y = start; y < end; y++)
340  {
341  int *pos = dsp_stream_get_position(stream, y);
342  int dim;
343  for (dim = 1; dim < stream->dims; dim++) {
344  pos[dim] -= stream->align_info.center[dim];
345  pos[dim-1] -= stream->align_info.center[dim-1];
346  double r = stream->align_info.radians[dim-1];
347  double x = pos[dim-1];
348  double y = pos[dim];
349  pos[dim] = x*-cos(r-M_PI_2);
350  pos[dim-1] = x*cos(r);
351  pos[dim] += y*cos(r);
352  pos[dim] += stream->align_info.center[dim];
353  pos[dim-1] += stream->align_info.center[dim-1];
354  pos[dim-1] += y*cos(r-M_PI_2);
355  }
356  int x = dsp_stream_set_position(in, pos);
357  free(pos);
358  if(x >= 0 && x < in->len)
359  stream->buf[y] = in->buf[x];
360  }
361  return NULL;
362 }
363 
365 {
366  dsp_stream_p stream = dsp_stream_copy(in);
367  dsp_buffer_set(stream->buf, stream->len, 0);
368  stream->parent = in;
369  int y;
370  pthread_t *th = malloc(sizeof(pthread_t)*DSP_MAX_THREADS);
371  struct {
372  int cur_th;
373  dsp_stream_p stream;
374  } thread_arguments[DSP_MAX_THREADS];
375  for(y = 0; y < DSP_MAX_THREADS; y++) {
376  thread_arguments[y].cur_th = y;
377  thread_arguments[y].stream = stream;
378  pthread_create(&th[y], NULL, dsp_stream_rotate_th, &thread_arguments[y]);
379  }
380  for(y = 0; y < DSP_MAX_THREADS; y++)
381  pthread_join(th[y], NULL);
382  free(th);
383  dsp_buffer_copy(stream->buf, in->buf, stream->len);
384  dsp_stream_free_buffer(stream);
385  dsp_stream_free(stream);
386 }
th
std::thread th
Definition: activefocuser_utils.cpp:88
dsp_stream_free
void dsp_stream_free(dsp_stream_p stream)
Free the DSP stream passed as argument.
Definition: stream.c:76
dsp_stream_get_position
int * dsp_stream_get_position(dsp_stream_p stream, int index)
Return the multidimensional positional indexes of a DSP stream by specify a linear index.
Definition: stream.c:188
dsp_stream_p
struct dsp_stream_t * dsp_stream_p
dsp_stream_free_buffer
void dsp_stream_free_buffer(dsp_stream_p stream)
Free the buffer of the DSP Stream passed as argument.
Definition: stream.c:41
dsp_align_info_t::center
double * center
Center of rotation coordinates.
Definition: dsp.h:119
dsp_stream_scale
void dsp_stream_scale(dsp_stream_p in)
Scale a stream.
Definition: stream.c:300
dsp_stream_del_child
void dsp_stream_del_child(dsp_stream_p stream, int index)
Remove the child with index n to a DSP stream.
Definition: stream.c:149
dsp_star_t::diameter
double diameter
The diameter of the star.
Definition: dsp.h:160
dsp.h
dsp_stream_add_dim
void dsp_stream_add_dim(dsp_stream_p stream, int size)
Add a dimension with length len to a DSP stream.
Definition: stream.c:111
dsp_align_info_t::radians
double * radians
Rotational offset.
Definition: dsp.h:121
dsp_stream_new
dsp_stream_p dsp_stream_new()
Allocate a new DSP stream type.
Definition: stream.c:48
dsp_stream_t::wavelength
double wavelength
Wavelength observed, used as reference with signal generators or filters.
Definition: dsp.h:200
dsp_star_t::center
dsp_point center
The center of the star.
Definition: dsp.h:158
dsp_stream_traslate
void dsp_stream_traslate(dsp_stream_p in)
Traslate a stream.
Definition: stream.c:252
dsp_stream_get_buffer
dsp_t * dsp_stream_get_buffer(dsp_stream_p stream)
Return the buffer of the stream passed as argument.
Definition: stream.c:36
dsp_stream_set_position
int dsp_stream_set_position(dsp_stream_p stream, int *pos)
Obtain the position the DSP stream by parsing multidimensional indexes.
Definition: stream.c:202
dsp_stream_t::frame_number
int frame_number
Frame number (if part of a series)
Definition: dsp.h:226
dsp_stream_t::diameter
double diameter
Diameter.
Definition: dsp.h:204
dsp_align_info_t::dims
int dims
Dimensions limit.
Definition: dsp.h:125
Max
#define Max(a, b)
if max() is not present you can use this one
Definition: dsp.h:72
dsp_star_t
A star or object contained into a buffer.
Definition: dsp.h:155
dsp_point_t::location
double * location
Center of the point.
Definition: dsp.h:95
dsp_stream_set_buffer
void dsp_stream_set_buffer(dsp_stream_p stream, void *buffer, int len)
Set the buffer of the stream passed as argument to a specific memory location.
Definition: stream.c:30
dsp_stream_t
Contains a set of informations and data relative to a buffer and how to use it.
Definition: dsp.h:175
dsp_stream_del_dim
void dsp_stream_del_dim(dsp_stream_p stream, int index)
Remove the dimension with index n to a DSP stream.
Definition: stream.c:126
dsp_buffer_set
#define dsp_buffer_set(buf, len, _val)
Place the given value on each element of the buffer.
Definition: dsp.h:579
dsp_region_t::start
int start
Starting point within the buffer.
Definition: dsp.h:147
Min
#define Min(a, b)
if min() is not present you can use this one
Definition: dsp.h:65
dsp_stream_t::starttimeutc
struct timespec starttimeutc
Time at the beginning of the stream.
Definition: dsp.h:198
dsp_stream_t::align_info
dsp_align_info align_info
Align/scale/rotation settings.
Definition: dsp.h:224
dsp_stream_del_star
void dsp_stream_del_star(dsp_stream_p stream, int index)
Remove the star with index n to a DSP stream.
Definition: stream.c:173
dsp_stream_t::samplerate
double samplerate
Sample rate of the buffers.
Definition: dsp.h:212
dsp_stream_t::location
double * location
Location coordinates.
Definition: dsp.h:194
dsp_stream_crop
void dsp_stream_crop(dsp_stream_p in)
Crop the buffers of the stream passed as argument by reading the ROI field.
Definition: stream.c:213
dsp_align_info_t::offset
double * offset
Traslation offset.
Definition: dsp.h:117
dsp_stream_t::stars
dsp_star * stars
Stars or objects identified into the buffers - TODO.
Definition: dsp.h:220
dsp_stream_t::len
int len
The buffers length.
Definition: dsp.h:178
dsp_buffer_copy
#define dsp_buffer_copy(in, out, len)
Fill the output buffer with the values of the elements of the input stream by casting them to the out...
Definition: dsp.h:791
dsp_stream_alloc_buffer
void dsp_stream_alloc_buffer(dsp_stream_p stream, int len)
Allocate a buffer with length len on the stream passed as argument.
Definition: stream.c:21
dsp_stream_t::focal_ratio
double focal_ratio
Focal ratio.
Definition: dsp.h:202
end
JsonIterator end(JsonValue)
Definition: gason.h:108
dsp_stream_t::sizes
int * sizes
Sizes of each dimension.
Definition: dsp.h:182
dsp_stream_t::stars_count
int stars_count
Stars or objects quantity.
Definition: dsp.h:222
dsp_stream_t::pixel_sizes
double * pixel_sizes
Sensor size.
Definition: dsp.h:210
dsp_stream_add_child
void dsp_stream_add_child(dsp_stream_p stream, dsp_stream_p child)
Add a child to the DSP Stream passed as argument.
Definition: stream.c:141
dsp_stream_copy
dsp_stream_p dsp_stream_copy(dsp_stream_p stream)
Create a copy of the DSP stream passed as argument.
Definition: stream.c:89
dsp_stream_rotate
void dsp_stream_rotate(dsp_stream_p in)
Rotate a stream around an axis and offset.
Definition: stream.c:364
dsp_point_t::dims
int dims
Dimensions limit of the point.
Definition: dsp.h:97
dsp_stream_add_star
void dsp_stream_add_star(dsp_stream_p stream, dsp_star star)
Add a star into the stream struct.
Definition: stream.c:164
dsp_stream_t::dims
int dims
Number of dimensions of the buffers.
Definition: dsp.h:180
dsp_stream_t::ROI
dsp_region * ROI
Regions of interest for each dimension.
Definition: dsp.h:218
dsp_region_t
Delimits a region in a single dimension of a buffer.
Definition: dsp.h:144
dsp_region_t::len
int len
Length of the region.
Definition: dsp.h:149
DSP_MAX_THREADS
#define DSP_MAX_THREADS
Definition: dsp.h:61
dsp_align_info_t::factor
double factor
Scaling factor.
Definition: dsp.h:123
dsp_t
#define dsp_t
Definition: dsp.h:57
dsp_stream_t::parent
struct dsp_stream_t * parent
The stream this one is child of.
Definition: dsp.h:188
dsp_stream_t::red
int red
Red pixel (Bayer)
Definition: dsp.h:208
dsp_stream_t::child_count
int child_count
Children streams count.
Definition: dsp.h:192
dsp_stream_t::children
struct dsp_stream_t ** children
Children streams of the current one.
Definition: dsp.h:190
Aux::buffer
std::vector< uint8_t > buffer
Definition: celestronauxpacket.h:38
dsp_stream_t::buf
dsp_t * buf
buffer
Definition: dsp.h:184
dsp_stream_t::target
double * target
Target coordinates.
Definition: dsp.h:196