reader.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #define _BSD_SOURCE
  2. #define _GNU_SOURCE
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <sys/time.h>
  8. #include <sys/stat.h>
  9. #include <sys/types.h>
  10. #include <pthread.h>
  11. #include <assert.h>
  12. #include <ufodecode.h>
  13. #include <pcilib.h>
  14. #include <pcilib/tools.h>
  15. #include <pcilib/error.h>
  16. #include "model.h"
  17. #include "private.h"
  18. #include "reader.h"
  19. //#define CHECK_FRAME_MAGIC(buf) \
  20. // memcmp(buf, ((void*)frame_magic) + 1, sizeof(frame_magic) - 1)
  21. #define CHECK_FRAME_MAGIC(buf) \
  22. memcmp(((ipecamera_payload_t*)(buf)) + 1, &frame_magic[1], sizeof(frame_magic) - sizeof(ipecamera_payload_t))
  23. static ipecamera_payload_t frame_magic[3] = { 0x51111111, 0x52222222, 0x53333333 };
  24. int ipecamera_compute_buffer_size(ipecamera_t *ctx, ipecamera_format_t format, size_t header_size, size_t lines) {
  25. // const size_t header_size = 8 * sizeof(ipecamera_payload_t);
  26. const size_t footer_size = CMOSIS_FRAME_TAIL_SIZE;
  27. size_t max_channels;
  28. size_t line_size, raw_size, padded_blocks;
  29. switch (format) {
  30. case IPECAMERA_FORMAT_CMOSIS:
  31. max_channels = CMOSIS_MAX_CHANNELS;
  32. line_size = (1 + CMOSIS_PIXELS_PER_CHANNEL) * 32;
  33. break;
  34. case IPECAMERA_FORMAT_CMOSIS20:
  35. max_channels = CMOSIS20_MAX_CHANNELS;
  36. line_size = (1 + CMOSIS20_PIXELS_PER_CHANNEL) * 32 / 2;
  37. break;
  38. default:
  39. pcilib_warning("Unsupported version (%u) of frame format...", format);
  40. return PCILIB_ERROR_NOTSUPPORTED;
  41. }
  42. raw_size = lines * line_size;
  43. raw_size *= max_channels / ctx->cmosis_outputs;
  44. raw_size += header_size + footer_size;
  45. #ifdef IPECAMERA_BUG_MISSING_PAYLOAD
  46. // As I understand, the first 32-byte packet is missing, so we need to substract 32 (both CMOSIS and CMOSIS20)
  47. raw_size -= 32;
  48. #endif /* IPECAMERA_BUG_MISSING_PAYLOAD */
  49. padded_blocks = raw_size / IPECAMERA_DMA_PACKET_LENGTH + ((raw_size % IPECAMERA_DMA_PACKET_LENGTH)?1:0);
  50. ctx->roi_raw_size = raw_size;
  51. ctx->roi_padded_size = padded_blocks * IPECAMERA_DMA_PACKET_LENGTH;
  52. return 0;
  53. }
  54. static int ipecamera_parse_header(ipecamera_t *ctx, ipecamera_payload_t *buf, size_t buf_size) {
  55. int last = buf[0] & 1;
  56. int version = (buf[0] >> 1) & 7;
  57. size_t size = 0, n_lines;
  58. ipecamera_format_t format = IPECAMERA_FORMAT_CMOSIS;
  59. switch (version) {
  60. case 0:
  61. n_lines = ((uint32_t*)buf)[5] & 0x7FF;
  62. ctx->frame[ctx->buffer_pos].event.info.seqnum = buf[6] & 0xFFFFFF;
  63. ctx->frame[ctx->buffer_pos].event.info.offset = (buf[7] & 0xFFFFFF) * 80;
  64. break;
  65. case 1:
  66. n_lines = ((uint32_t*)buf)[5] & 0xFFFF;
  67. if (!n_lines) {
  68. pcilib_error("The frame header claims 0 lines in the data");
  69. return 0;
  70. }
  71. ctx->frame[ctx->buffer_pos].event.info.seqnum = buf[6] & 0xFFFFFF;
  72. ctx->frame[ctx->buffer_pos].event.info.offset = (buf[7] & 0xFFFFFF) * 80;
  73. format = (buf[6] >> 24)&0x0F;
  74. break;
  75. default:
  76. ipecamera_debug(HARDWARE, "Incorrect version of the frame header, ignoring broken data...");
  77. return 0;
  78. }
  79. gettimeofday(&ctx->frame[ctx->buffer_pos].event.info.timestamp, NULL);
  80. ipecamera_debug(FRAME_HEADERS, "frame %lu: %x %x %x %x", ctx->frame[ctx->buffer_pos].event.info.seqnum, buf[0], buf[1], buf[2], buf[3]);
  81. ipecamera_debug(FRAME_HEADERS, "frame %lu: %x %x %x %x", ctx->frame[ctx->buffer_pos].event.info.seqnum, buf[4], buf[5], buf[6], buf[7]);
  82. while ((!last)&&((size + CMOSIS_FRAME_HEADER_SIZE) <= buf_size)) {
  83. size += CMOSIS_FRAME_HEADER_SIZE;
  84. last = buf[size] & 1;
  85. }
  86. size += CMOSIS_FRAME_HEADER_SIZE;
  87. ipecamera_compute_buffer_size(ctx, format, size, n_lines);
  88. // Returns total size of found headers or 0 on the error
  89. return size;
  90. }
  91. static inline int ipecamera_new_frame(ipecamera_t *ctx) {
  92. ctx->frame[ctx->buffer_pos].event.raw_size = ctx->cur_size;
  93. if (ctx->cur_size < ctx->roi_raw_size) {
  94. ctx->frame[ctx->buffer_pos].event.info.flags |= PCILIB_EVENT_INFO_FLAG_BROKEN;
  95. }
  96. ctx->buffer_pos = (++ctx->event_id) % ctx->buffer_size;
  97. ctx->cur_size = 0;
  98. ctx->frame[ctx->buffer_pos].event.info.type = PCILIB_EVENT0;
  99. ctx->frame[ctx->buffer_pos].event.info.flags = 0;
  100. ctx->frame[ctx->buffer_pos].event.image_ready = 0;
  101. if ((ctx->event_id == ctx->autostop.evid)&&(ctx->event_id)) {
  102. ctx->run_reader = 0;
  103. return 1;
  104. }
  105. if (pcilib_check_deadline(&ctx->autostop.timestamp, 0)) {
  106. ctx->run_reader = 0;
  107. return 1;
  108. }
  109. return 0;
  110. }
  111. static int ipecamera_data_callback(void *user, pcilib_dma_flags_t flags, size_t bufsize, void *buf) {
  112. int res;
  113. int eof = 0;
  114. static unsigned long packet_id = 0;
  115. #ifdef IPECAMERA_BUG_MULTIFRAME_PACKETS
  116. size_t real_size;
  117. size_t extra_data = 0;
  118. #endif /* IPECAMERA_BUG_MULTIFRAME_PACKETS */
  119. ipecamera_t *ctx = (ipecamera_t*)user;
  120. #if defined(IPECAMERA_BUG_INCOMPLETE_PACKETS)||defined(IPECAMERA_BUG_MULTIFRAME_PACKETS)
  121. static pcilib_event_id_t invalid_frame_id = (pcilib_event_id_t)-1;
  122. #endif
  123. packet_id++;
  124. ipecamera_debug_buffer(RAW_PACKETS, bufsize, buf, PCILIB_DEBUG_BUFFER_MKDIR, "frame%4lu/frame%9lu", ctx->event_id, packet_id);
  125. if (!ctx->cur_size) {
  126. #if defined(IPECAMERA_BUG_INCOMPLETE_PACKETS)||defined(IPECAMERA_BUG_MULTIFRAME_PACKETS)
  127. size_t startpos;
  128. for (startpos = 0; (startpos + CMOSIS_FRAME_HEADER_SIZE) <= bufsize; startpos += sizeof(ipecamera_payload_t)) {
  129. if (!CHECK_FRAME_MAGIC(buf + startpos)) break;
  130. }
  131. if ((startpos + CMOSIS_FRAME_HEADER_SIZE) > bufsize) {
  132. ipecamera_debug_buffer(RAW_PACKETS, bufsize, NULL, 0, "frame%4lu/frame%9lu.invalid", ctx->event_id, packet_id);
  133. if (invalid_frame_id != ctx->event_id) {
  134. ipecamera_debug(HARDWARE, "No frame magic in DMA packet of %u bytes, current event %lu", bufsize, ctx->event_id);
  135. invalid_frame_id = ctx->event_id;
  136. }
  137. return PCILIB_STREAMING_CONTINUE;
  138. }
  139. if (startpos) {
  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);
  143. if (res <= 0) {
  144. if (res < 0) return res;
  145. ctx->run_reader = 0;
  146. }
  147. }
  148. buf += startpos;
  149. bufsize -= startpos;
  150. }
  151. #endif /* IPECAMERA_BUG_INCOMPLETE_PACKETS */
  152. if ((bufsize >= CMOSIS_FRAME_HEADER_SIZE)&&(!CHECK_FRAME_MAGIC(buf))) {
  153. // We should handle the case when multi-header is split between multiple DMA packets
  154. if (!ipecamera_parse_header(ctx, buf, bufsize))
  155. return PCILIB_STREAMING_CONTINUE;
  156. } else {
  157. ipecamera_debug(HARDWARE, "Frame magic is not found, ignoring broken data...");
  158. return PCILIB_STREAMING_CONTINUE;
  159. }
  160. }
  161. #ifdef IPECAMERA_BUG_MULTIFRAME_PACKETS
  162. // for rawdata_callback with complete padding
  163. real_size = bufsize;
  164. if (ctx->cur_size + bufsize > ctx->roi_raw_size) {
  165. size_t need;
  166. for (need = ctx->roi_raw_size - ctx->cur_size; (need + CMOSIS_FRAME_HEADER_SIZE) <= bufsize; need += sizeof(uint32_t)) {
  167. if (!CHECK_FRAME_MAGIC(buf + need)) break;
  168. }
  169. if ((need + CMOSIS_FRAME_HEADER_SIZE) <= bufsize) {
  170. extra_data = bufsize - need;
  171. eof = 1;
  172. }
  173. // just rip of padding
  174. bufsize = ctx->roi_raw_size - ctx->cur_size;
  175. ipecamera_debug_buffer(RAW_PACKETS, bufsize, buf, 0, "frame%4lu/frame%9lu.partial", ctx->event_id, packet_id);
  176. }
  177. #endif /* IPECAMERA_BUG_MULTIFRAME_PACKETS */
  178. if (ctx->parse_data) {
  179. if (ctx->cur_size + bufsize > ctx->padded_size) {
  180. pcilib_error("Unexpected event data, we are expecting at maximum (%zu) bytes, but (%zu) already read", ctx->padded_size, ctx->cur_size + bufsize);
  181. return -PCILIB_ERROR_TOOBIG;
  182. }
  183. if (bufsize)
  184. memcpy(ctx->buffer + ctx->buffer_pos * ctx->padded_size + ctx->cur_size, buf, bufsize);
  185. }
  186. ctx->cur_size += bufsize;
  187. if (ctx->cur_size >= ctx->roi_raw_size) {
  188. eof = 1;
  189. }
  190. if (ctx->event.params.rawdata.callback) {
  191. 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);
  192. if (res <= 0) {
  193. if (res < 0) return res;
  194. ctx->run_reader = 0;
  195. }
  196. }
  197. if (eof) {
  198. if ((ipecamera_new_frame(ctx))||(!ctx->run_reader)) {
  199. return PCILIB_STREAMING_STOP;
  200. }
  201. #ifdef IPECAMERA_BUG_MULTIFRAME_PACKETS
  202. if (extra_data) {
  203. return ipecamera_data_callback(user, flags, extra_data, buf + (real_size - extra_data));
  204. }
  205. #endif /* IPECAMERA_BUG_MULTIFRAME_PACKETS */
  206. }
  207. return PCILIB_STREAMING_REQ_FRAGMENT;
  208. }
  209. void *ipecamera_reader_thread(void *user) {
  210. int err;
  211. ipecamera_t *ctx = (ipecamera_t*)user;
  212. while (ctx->run_reader) {
  213. err = pcilib_stream_dma(ctx->event.pcilib, ctx->rdma, 0, 0, PCILIB_DMA_FLAG_MULTIPACKET, IPECAMERA_DMA_TIMEOUT, &ipecamera_data_callback, user);
  214. if (err) {
  215. if (err == PCILIB_ERROR_TIMEOUT) {
  216. if (ctx->cur_size >= ctx->roi_raw_size) ipecamera_new_frame(ctx);
  217. #ifdef IPECAMERA_BUG_INCOMPLETE_PACKETS
  218. else if (ctx->cur_size > 0) ipecamera_new_frame(ctx);
  219. #endif /* IPECAMERA_BUG_INCOMPLETE_PACKETS */
  220. if (pcilib_check_deadline(&ctx->autostop.timestamp, 0)) {
  221. ctx->run_reader = 0;
  222. break;
  223. }
  224. usleep(IPECAMERA_NOFRAME_SLEEP);
  225. } else pcilib_error("DMA error while reading IPECamera frames, error: %i", err);
  226. }
  227. }
  228. ctx->run_streamer = 0;
  229. if (ctx->cur_size)
  230. pcilib_info("partialy read frame after stop signal, %zu bytes in the buffer", ctx->cur_size);
  231. return NULL;
  232. }