1
#define _IPECAMERA_IMAGE_C
13
#include <ufodecode.h>
22
static inline int ipecamera_new_frame(ipecamera_t *ctx) {
23
ctx->frame[ctx->buffer_pos].event.raw_size = ctx->cur_size;
25
if (ctx->cur_size < ctx->raw_size) ctx->frame[ctx->buffer_pos].event.info.flags |= PCILIB_EVENT_INFO_FLAG_BROKEN;
27
ctx->buffer_pos = (++ctx->event_id) % ctx->buffer_size;
30
ctx->frame[ctx->buffer_pos].event.info.type = PCILIB_EVENT0;
31
ctx->frame[ctx->buffer_pos].event.info.flags = 0;
32
ctx->frame[ctx->buffer_pos].event.image_ready = 0;
34
if ((ctx->event_id == ctx->autostop.evid)&&(ctx->event_id)) {
39
if (pcilib_check_deadline(&ctx->autostop.timestamp, PCILIB_DMA_TIMEOUT)) {
47
static uint32_t frame_magic[6] = { 0x51111111, 0x52222222, 0x53333333, 0x54444444, 0x55555555, 0x56666666 };
49
static int ipecamera_data_callback(void *user, pcilib_dma_flags_t flags, size_t bufsize, void *buf) {
53
#ifdef IPECAMERA_BUG_MULTIFRAME_PACKETS
54
size_t extra_data = 0;
55
#endif /* IPECAMERA_BUG_MULTIFRAME_PACKETS */
57
ipecamera_t *ctx = (ipecamera_t*)user;
60
#if defined(IPECAMERA_BUG_INCOMPLETE_PACKETS)||defined(IPECAMERA_BUG_MULTIFRAME_PACKETS)
62
for (startpos = 0; (startpos + 8) < bufsize; startpos++) {
63
if (!memcmp(buf + startpos, frame_magic, sizeof(frame_magic))) break;
70
#endif /* IPECAMERA_BUG_INCOMPLETE_PACKETS */
72
if ((bufsize >= 8)&&(!memcmp(buf, frame_magic, sizeof(frame_magic)))) {
74
// Not implemented in hardware yet
75
ctx->frame[ctx->buffer_pos].event.info.seqnum = ((uint32_t*)buf)[6] & 0xF0000000;
76
ctx->frame[ctx->buffer_pos].event.info.offset = ((uint32_t*)buf)[7] & 0xF0000000;
78
gettimeofday(&ctx->frame[ctx->buffer_pos].event.info.timestamp, NULL);
80
// pcilib_warning("Frame magic is not found, ignoring broken data...");
81
return PCILIB_STREAMING_CONTINUE;
85
#ifdef IPECAMERA_BUG_MULTIFRAME_PACKETS
86
if (ctx->cur_size + bufsize > ctx->raw_size) {
89
for (need = ctx->raw_size - ctx->cur_size; need < bufsize; need += sizeof(uint32_t)) {
90
if (*(uint32_t*)(buf + need) == frame_magic[0]) break;
94
extra_data = bufsize - need;
99
// just rip of padding
100
bufsize = ctx->raw_size - ctx->cur_size;
102
#endif /* IPECAMERA_BUG_MULTIFRAME_PACKETS */
104
if (ctx->parse_data) {
105
if (ctx->cur_size + bufsize > ctx->full_size) {
106
pcilib_error("Unexpected event data, we are expecting at maximum (%zu) bytes, but (%zu) already read", ctx->full_size, ctx->cur_size + bufsize);
107
return -PCILIB_ERROR_TOOBIG;
110
memcpy(ctx->buffer + ctx->buffer_pos * ctx->padded_size + ctx->cur_size, buf, bufsize);
113
ctx->cur_size += bufsize;
114
// printf("%i: %i %i\n", ctx->buffer_pos, ctx->cur_size, bufsize);
116
if (ctx->cur_size >= ctx->full_size) eof = 1;
118
if (ctx->event.params.rawdata.callback) {
119
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);
121
if (res < 0) return res;
127
if (ipecamera_new_frame(ctx)) {
128
return PCILIB_STREAMING_STOP;
131
#ifdef IPECAMERA_BUG_MULTIFRAME_PACKETS
133
return ipecamera_data_callback(user, flags, extra_data, buf + bufsize);
135
#endif /* IPECAMERA_BUG_MULTIFRAME_PACKETS */
138
return PCILIB_STREAMING_REQ_FRAGMENT;
141
void *ipecamera_reader_thread(void *user) {
143
ipecamera_t *ctx = (ipecamera_t*)user;
145
while (ctx->run_reader) {
146
err = pcilib_stream_dma(ctx->event.pcilib, ctx->rdma, 0, 0, PCILIB_DMA_FLAG_MULTIPACKET, PCILIB_DMA_TIMEOUT, &ipecamera_data_callback, user);
148
if (err == PCILIB_ERROR_TIMEOUT) {
149
if (ctx->cur_size > ctx->raw_size) ipecamera_new_frame(ctx);
150
#ifdef IPECAMERA_BUG_INCOMPLETE_PACKETS
151
else if (ctx->cur_size > 0) ipecamera_new_frame(ctx);
152
#endif /* IPECAMERA_BUG_INCOMPLETE_PACKETS */
153
if (pcilib_check_deadline(&ctx->autostop.timestamp, PCILIB_DMA_TIMEOUT)) {
157
usleep(IPECAMERA_NOFRAME_SLEEP);
158
} else pcilib_error("DMA error while reading IPECamera frames, error: %i", err);
159
} else printf("no error\n");
164
ctx->run_streamer = 0;
167
pcilib_error("partialy read frame after stop signal, %zu bytes in the buffer", ctx->cur_size);