/alps/ipecamera

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/ipecamera

« back to all changes in this revision

Viewing changes to ipecamera/reader.c

  • Committer: Suren A. Chilingaryan
  • Date: 2011-12-12 04:45:35 UTC
  • Revision ID: csa@dside.dyndns.org-20111212044535-6no1q7g230i8uvlv
multithread preprocessing of ipecamera frames and code reorganization

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#define _IPECAMERA_IMAGE_C
 
2
#define _BSD_SOURCE
 
3
#define _GNU_SOURCE
 
4
 
 
5
#include <stdio.h>
 
6
#include <stdlib.h>
 
7
#include <unistd.h>
 
8
#include <string.h>
 
9
#include <sys/time.h>
 
10
#include <pthread.h>
 
11
#include <assert.h>
 
12
 
 
13
#include <ufodecode.h>
 
14
 
 
15
#include "../tools.h"
 
16
#include "../error.h"
 
17
 
 
18
#include "pcilib.h"
 
19
#include "private.h"
 
20
#include "reader.h"
 
21
 
 
22
static inline int ipecamera_new_frame(ipecamera_t *ctx) {
 
23
    ctx->frame[ctx->buffer_pos].event.raw_size = ctx->cur_size;
 
24
 
 
25
    if (ctx->cur_size < ctx->raw_size) ctx->frame[ctx->buffer_pos].event.info.flags |= PCILIB_EVENT_INFO_FLAG_BROKEN;
 
26
    
 
27
    ctx->buffer_pos = (++ctx->event_id) % ctx->buffer_size;
 
28
    ctx->cur_size = 0;
 
29
 
 
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;
 
33
 
 
34
    if ((ctx->event_id == ctx->autostop.evid)&&(ctx->event_id)) {
 
35
        ctx->run_reader = 0;
 
36
        return 1;
 
37
    }
 
38
        
 
39
    if (pcilib_check_deadline(&ctx->autostop.timestamp, PCILIB_DMA_TIMEOUT)) {
 
40
        ctx->run_reader = 0;
 
41
        return 1;
 
42
    }
 
43
    
 
44
    return 0;
 
45
}
 
46
 
 
47
static uint32_t frame_magic[6] = { 0x51111111, 0x52222222, 0x53333333, 0x54444444, 0x55555555, 0x56666666 };
 
48
 
 
49
static int ipecamera_data_callback(void *user, pcilib_dma_flags_t flags, size_t bufsize, void *buf) {
 
50
    int res;
 
51
    int eof = 0;
 
52
 
 
53
#ifdef IPECAMERA_BUG_MULTIFRAME_PACKETS
 
54
    size_t extra_data = 0;
 
55
#endif /* IPECAMERA_BUG_MULTIFRAME_PACKETS */
 
56
 
 
57
    ipecamera_t *ctx = (ipecamera_t*)user;
 
58
 
 
59
    if (!ctx->cur_size) {
 
60
#if defined(IPECAMERA_BUG_INCOMPLETE_PACKETS)||defined(IPECAMERA_BUG_MULTIFRAME_PACKETS)
 
61
        size_t startpos;
 
62
        for (startpos = 0; (startpos + 8) < bufsize; startpos++) {
 
63
            if (!memcmp(buf + startpos, frame_magic, sizeof(frame_magic))) break;
 
64
        }
 
65
        
 
66
        if (startpos) {
 
67
            buf += startpos;
 
68
            bufsize -= startpos;
 
69
        }
 
70
#endif /* IPECAMERA_BUG_INCOMPLETE_PACKETS */
 
71
 
 
72
        if ((bufsize >= 8)&&(!memcmp(buf, frame_magic, sizeof(frame_magic)))) {
 
73
/*
 
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;
 
77
*/
 
78
            gettimeofday(&ctx->frame[ctx->buffer_pos].event.info.timestamp, NULL);
 
79
        } else {
 
80
//          pcilib_warning("Frame magic is not found, ignoring broken data...");
 
81
            return PCILIB_STREAMING_CONTINUE;
 
82
        }
 
83
    }
 
84
 
 
85
#ifdef IPECAMERA_BUG_MULTIFRAME_PACKETS
 
86
    if (ctx->cur_size + bufsize > ctx->raw_size) {
 
87
        size_t need;
 
88
        
 
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;
 
91
        }
 
92
        
 
93
        if (need < bufsize) {
 
94
            extra_data = bufsize - need;
 
95
            //bufsize = need;
 
96
            eof = 1;
 
97
        }
 
98
        
 
99
            // just rip of padding
 
100
        bufsize = ctx->raw_size - ctx->cur_size;
 
101
    }
 
102
#endif /* IPECAMERA_BUG_MULTIFRAME_PACKETS */
 
103
 
 
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;
 
108
        }
 
109
 
 
110
        memcpy(ctx->buffer + ctx->buffer_pos * ctx->padded_size +  ctx->cur_size, buf, bufsize);
 
111
    }
 
112
 
 
113
    ctx->cur_size += bufsize;
 
114
//    printf("%i: %i %i\n", ctx->buffer_pos, ctx->cur_size, bufsize);
 
115
 
 
116
    if (ctx->cur_size >= ctx->full_size) eof = 1;
 
117
 
 
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);
 
120
        if (res <= 0) {
 
121
            if (res < 0) return res;
 
122
            ctx->run_reader = 0;
 
123
        }
 
124
    }
 
125
    
 
126
    if (eof) {
 
127
        if (ipecamera_new_frame(ctx)) {
 
128
            return PCILIB_STREAMING_STOP;
 
129
        }
 
130
        
 
131
#ifdef IPECAMERA_BUG_MULTIFRAME_PACKETS
 
132
        if (extra_data) {
 
133
            return ipecamera_data_callback(user, flags, extra_data, buf + bufsize);
 
134
        }
 
135
#endif /* IPECAMERA_BUG_MULTIFRAME_PACKETS */
 
136
    }
 
137
 
 
138
    return PCILIB_STREAMING_REQ_FRAGMENT;
 
139
}
 
140
 
 
141
void *ipecamera_reader_thread(void *user) {
 
142
    int err;
 
143
    ipecamera_t *ctx = (ipecamera_t*)user;
 
144
    
 
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);
 
147
        if (err) {
 
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)) {
 
154
                    ctx->run_reader = 0;
 
155
                    break;
 
156
                }
 
157
                usleep(IPECAMERA_NOFRAME_SLEEP);
 
158
            } else pcilib_error("DMA error while reading IPECamera frames, error: %i", err);
 
159
        } else printf("no error\n");
 
160
 
 
161
        //usleep(1000);
 
162
    }
 
163
    
 
164
    ctx->run_streamer = 0;
 
165
    
 
166
    if (ctx->cur_size)
 
167
        pcilib_error("partialy read frame after stop signal, %zu bytes in the buffer", ctx->cur_size);
 
168
 
 
169
    return NULL;
 
170
}