10
#include <sys/types.h>
14
#include <ufodecode.h>
25
int ipecamera_compute_buffer_size(ipecamera_t *ctx, size_t lines) {
26
const size_t header_size = 8 * sizeof(ipecamera_payload_t);
27
const size_t footer_size = 8 * sizeof(ipecamera_payload_t);
29
size_t line_size, raw_size, padded_blocks;
32
switch (ctx->firmware) {
34
line_size = IPECAMERA_MAX_CHANNELS * (2 + IPECAMERA_PIXELS_PER_CHANNEL / 3) * sizeof(ipecamera_payload_t);
35
raw_size = header_size + lines * line_size + footer_size;
38
line_size = (1 + IPECAMERA_PIXELS_PER_CHANNEL) * 32;
39
raw_size = header_size + lines * line_size - 32 + footer_size;
40
raw_size *= 16 / ctx->cmosis_outputs;
43
padded_blocks = raw_size / IPECAMERA_DMA_PACKET_LENGTH + ((raw_size % IPECAMERA_DMA_PACKET_LENGTH)?1:0);
45
ctx->cur_raw_size = raw_size;
46
ctx->cur_full_size = padded_blocks * IPECAMERA_DMA_PACKET_LENGTH;
48
#ifdef IPECAMERA_BUG_EXTRA_DATA
49
ctx->cur_full_size += 8;
51
#endif /* IPECAMERA_BUG_EXTRA_DATA */
53
ctx->cur_padded_size = padded_blocks * IPECAMERA_DMA_PACKET_LENGTH;
58
static inline int ipecamera_new_frame(ipecamera_t *ctx) {
59
ctx->frame[ctx->buffer_pos].event.raw_size = ctx->cur_size;
61
if (ctx->cur_size < ctx->cur_raw_size) {
62
ctx->frame[ctx->buffer_pos].event.info.flags |= PCILIB_EVENT_INFO_FLAG_BROKEN;
65
ctx->buffer_pos = (++ctx->event_id) % ctx->buffer_size;
68
ctx->frame[ctx->buffer_pos].event.info.type = PCILIB_EVENT0;
69
ctx->frame[ctx->buffer_pos].event.info.flags = 0;
70
ctx->frame[ctx->buffer_pos].event.image_ready = 0;
72
if ((ctx->event_id == ctx->autostop.evid)&&(ctx->event_id)) {
77
if (pcilib_check_deadline(&ctx->autostop.timestamp, PCILIB_DMA_TIMEOUT)) {
85
static uint32_t frame_magic[5] = { 0x51111111, 0x52222222, 0x53333333, 0x54444444, 0x55555555 };
87
static int ipecamera_data_callback(void *user, pcilib_dma_flags_t flags, size_t bufsize, void *buf) {
91
#ifdef IPECAMERA_BUG_MULTIFRAME_PACKETS
93
size_t extra_data = 0;
94
#endif /* IPECAMERA_BUG_MULTIFRAME_PACKETS */
96
ipecamera_t *ctx = (ipecamera_t*)user;
98
#if defined(IPECAMERA_BUG_INCOMPLETE_PACKETS)||defined(IPECAMERA_BUG_MULTIFRAME_PACKETS)
99
static pcilib_event_id_t invalid_frame_id = (pcilib_event_id_t)-1;
102
#ifdef IPECAMERA_DEBUG_RAW_PACKETS
105
static unsigned long packet_id = 0;
106
sprintf(fname,"%s/frame%4lu", IPECAMERA_DEBUG_RAW_PACKETS, ctx->event_id);
108
sprintf(fname,"%s/frame%4lu/frame%9lu", IPECAMERA_DEBUG_RAW_PACKETS, ctx->event_id, packet_id);
109
FILE *f = fopen(fname, "w");
111
fwrite(buf, 1, bufsize, f);
114
sprintf(fname,"%s/frame%4lu/frame%9lu.invalid", IPECAMERA_DEBUG_RAW_PACKETS, ctx->event_id, packet_id++);
116
#endif /* IPECAMERA_DEBUG_RAW_PACKETS */
118
if (!ctx->cur_size) {
119
#if defined(IPECAMERA_BUG_INCOMPLETE_PACKETS)||defined(IPECAMERA_BUG_MULTIFRAME_PACKETS)
121
for (startpos = 0; (startpos + sizeof(frame_magic)) < bufsize; startpos += sizeof(uint32_t)) {
122
if (!memcmp(buf + startpos, frame_magic, sizeof(frame_magic))) break;
125
if ((startpos + sizeof(frame_magic)) >= bufsize) {
126
#ifdef IPECAMERA_DEBUG_RAW_PACKETS
127
FILE *f = fopen(fname, "w");
129
#endif /* IPECAMERA_DEBUG_RAW_PACKETS */
131
if (invalid_frame_id != ctx->event_id) {
132
pcilib_warning("No frame magic in DMA packet of %u bytes, current event %lu", bufsize, ctx->event_id);
133
invalid_frame_id = ctx->event_id;
136
return PCILIB_STREAMING_CONTINUE;
140
// pass padding to rawdata callback
141
if (ctx->event.params.rawdata.callback) {
142
res = ctx->event.params.rawdata.callback(0, NULL, PCILIB_EVENT_FLAG_RAW_DATA_ONLY, startpos, buf, ctx->event.params.rawdata.user);
144
if (res < 0) return res;
153
#endif /* IPECAMERA_BUG_INCOMPLETE_PACKETS */
155
if ((bufsize >= 8)&&(!memcmp(buf, frame_magic, sizeof(frame_magic)))) {
156
size_t n_lines = ((uint32_t*)buf)[5] & 0x7FF;
157
ipecamera_compute_buffer_size(ctx, n_lines);
159
ctx->frame[ctx->buffer_pos].event.info.seqnum = ((uint32_t*)buf)[6] & 0x1FFFFFF;
160
ctx->frame[ctx->buffer_pos].event.info.offset = (((uint32_t*)buf)[7] & 0xFFFFFF) * 80;
162
// ctx->frame[ctx->buffer_pos].event.info.seqnum = ctx->event_id + 1;
164
gettimeofday(&ctx->frame[ctx->buffer_pos].event.info.timestamp, NULL);
166
// pcilib_warning("Frame magic is not found, ignoring broken data...");
167
return PCILIB_STREAMING_CONTINUE;
171
#ifdef IPECAMERA_BUG_MULTIFRAME_PACKETS
172
// for rawdata_callback with complete padding
175
if (ctx->cur_size + bufsize > ctx->cur_raw_size) {
178
for (need = ctx->cur_raw_size - ctx->cur_size; (need + sizeof(frame_magic)) < bufsize; need += sizeof(uint32_t)) {
179
if (!memcmp(buf + need, frame_magic, sizeof(frame_magic))) break;
182
if ((need + sizeof(frame_magic)) < bufsize) {
183
extra_data = bufsize - need;
188
// just rip of padding
189
bufsize = ctx->cur_raw_size - ctx->cur_size;
191
#ifdef IPECAMERA_DEBUG_RAW_PACKETS
192
sprintf(fname + strlen(fname) - 8, ".partial");
193
FILE *f = fopen(fname, "w");
195
fwrite(buf, 1, bufsize, f);
198
#endif /* IPECAMERA_DEBUG_RAW_PACKETS */
200
#endif /* IPECAMERA_BUG_MULTIFRAME_PACKETS */
202
if (ctx->parse_data) {
203
if (ctx->cur_size + bufsize > ctx->full_size) {
204
pcilib_error("Unexpected event data, we are expecting at maximum (%zu) bytes, but (%zu) already read", ctx->full_size, ctx->cur_size + bufsize);
205
return -PCILIB_ERROR_TOOBIG;
208
memcpy(ctx->buffer + ctx->buffer_pos * ctx->padded_size + ctx->cur_size, buf, bufsize);
211
ctx->cur_size += bufsize;
212
// printf("%i: %i %i\n", ctx->buffer_pos, ctx->cur_size, bufsize);
214
if (ctx->cur_size >= ctx->full_size) eof = 1;
216
if (ctx->event.params.rawdata.callback) {
217
res = ctx->event.params.rawdata.callback(ctx->event_id, (pcilib_event_info_t*)(ctx->frame + ctx->buffer_pos), (eof?PCILIB_EVENT_FLAG_EOF:PCILIB_EVENT_FLAGS_DEFAULT), bufsize, buf, ctx->event.params.rawdata.user);
219
if (res < 0) return res;
225
if ((ipecamera_new_frame(ctx))||(!ctx->run_reader)) {
226
return PCILIB_STREAMING_STOP;
229
#ifdef IPECAMERA_BUG_MULTIFRAME_PACKETS
231
return ipecamera_data_callback(user, flags, extra_data, buf + (real_size - extra_data));
233
#endif /* IPECAMERA_BUG_MULTIFRAME_PACKETS */
236
return PCILIB_STREAMING_REQ_FRAGMENT;
239
void *ipecamera_reader_thread(void *user) {
241
ipecamera_t *ctx = (ipecamera_t*)user;
243
while (ctx->run_reader) {
244
err = pcilib_stream_dma(ctx->event.pcilib, ctx->rdma, 0, 0, PCILIB_DMA_FLAG_MULTIPACKET, PCILIB_DMA_TIMEOUT, &ipecamera_data_callback, user);
246
if (err == PCILIB_ERROR_TIMEOUT) {
247
if (ctx->cur_size >= ctx->cur_raw_size) ipecamera_new_frame(ctx);
248
#ifdef IPECAMERA_BUG_INCOMPLETE_PACKETS
249
else if (ctx->cur_size > 0) ipecamera_new_frame(ctx);
250
#endif /* IPECAMERA_BUG_INCOMPLETE_PACKETS */
251
if (pcilib_check_deadline(&ctx->autostop.timestamp, PCILIB_DMA_TIMEOUT)) {
255
usleep(IPECAMERA_NOFRAME_SLEEP);
256
} else pcilib_error("DMA error while reading IPECamera frames, error: %i", err);
257
} //else printf("no error\n");
262
ctx->run_streamer = 0;
265
pcilib_error("partialy read frame after stop signal, %zu bytes in the buffer", ctx->cur_size);