reader.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. int ipecamera_compute_buffer_size(ipecamera_t *ctx, size_t lines) {
  20. const size_t header_size = 8 * sizeof(ipecamera_payload_t);
  21. const size_t footer_size = 8 * sizeof(ipecamera_payload_t);
  22. size_t line_size, raw_size, padded_blocks;
  23. switch (ctx->firmware) {
  24. case 4:
  25. line_size = IPECAMERA_MAX_CHANNELS * (2 + IPECAMERA_PIXELS_PER_CHANNEL / 3) * sizeof(ipecamera_payload_t);
  26. raw_size = header_size + lines * line_size + footer_size;
  27. break;
  28. default:
  29. line_size = (1 + IPECAMERA_PIXELS_PER_CHANNEL) * 32;
  30. raw_size = lines * line_size;
  31. raw_size *= 16 / ctx->cmosis_outputs;
  32. raw_size += header_size + footer_size;
  33. }
  34. padded_blocks = raw_size / IPECAMERA_DMA_PACKET_LENGTH + ((raw_size % IPECAMERA_DMA_PACKET_LENGTH)?1:0);
  35. ctx->cur_raw_size = raw_size;
  36. ctx->cur_full_size = padded_blocks * IPECAMERA_DMA_PACKET_LENGTH;
  37. #ifdef IPECAMERA_BUG_EXTRA_DATA
  38. ctx->cur_full_size += 8;
  39. padded_blocks ++;
  40. #endif /* IPECAMERA_BUG_EXTRA_DATA */
  41. ctx->cur_padded_size = padded_blocks * IPECAMERA_DMA_PACKET_LENGTH;
  42. // printf("%lu %lu %lu\n", ctx->cur_raw_size, ctx->cur_full_size, ctx->cur_padded_size);
  43. return 0;
  44. }
  45. static inline int ipecamera_new_frame(ipecamera_t *ctx) {
  46. ctx->frame[ctx->buffer_pos].event.raw_size = ctx->cur_size;
  47. if (ctx->cur_size < ctx->cur_raw_size) {
  48. ctx->frame[ctx->buffer_pos].event.info.flags |= PCILIB_EVENT_INFO_FLAG_BROKEN;
  49. }
  50. ctx->buffer_pos = (++ctx->event_id) % ctx->buffer_size;
  51. ctx->cur_size = 0;
  52. ctx->frame[ctx->buffer_pos].event.info.type = PCILIB_EVENT0;
  53. ctx->frame[ctx->buffer_pos].event.info.flags = 0;
  54. ctx->frame[ctx->buffer_pos].event.image_ready = 0;
  55. if ((ctx->event_id == ctx->autostop.evid)&&(ctx->event_id)) {
  56. ctx->run_reader = 0;
  57. return 1;
  58. }
  59. if (pcilib_check_deadline(&ctx->autostop.timestamp, 0)) {
  60. ctx->run_reader = 0;
  61. return 1;
  62. }
  63. return 0;
  64. }
  65. static uint32_t frame_magic[5] = { 0x51111111, 0x52222222, 0x53333333, 0x54444444, 0x55555555 };
  66. static int ipecamera_data_callback(void *user, pcilib_dma_flags_t flags, size_t bufsize, void *buf) {
  67. int res;
  68. int eof = 0;
  69. #ifdef IPECAMERA_BUG_MULTIFRAME_PACKETS
  70. size_t real_size;
  71. size_t extra_data = 0;
  72. #endif /* IPECAMERA_BUG_MULTIFRAME_PACKETS */
  73. ipecamera_t *ctx = (ipecamera_t*)user;
  74. #if defined(IPECAMERA_BUG_INCOMPLETE_PACKETS)||defined(IPECAMERA_BUG_MULTIFRAME_PACKETS)
  75. static pcilib_event_id_t invalid_frame_id = (pcilib_event_id_t)-1;
  76. #endif
  77. #ifdef IPECAMERA_DEBUG_RAW_PACKETS
  78. char fname[128];
  79. {
  80. static unsigned long packet_id = 0;
  81. sprintf(fname,"%s/frame%4lu", IPECAMERA_DEBUG_RAW_PACKETS, ctx->event_id);
  82. mkdir(fname, 0755);
  83. sprintf(fname,"%s/frame%4lu/frame%9lu", IPECAMERA_DEBUG_RAW_PACKETS, ctx->event_id, packet_id);
  84. FILE *f = fopen(fname, "w");
  85. if (f) {
  86. fwrite(buf, 1, bufsize, f);
  87. fclose(f);
  88. }
  89. sprintf(fname,"%s/frame%4lu/frame%9lu.invalid", IPECAMERA_DEBUG_RAW_PACKETS, ctx->event_id, packet_id++);
  90. }
  91. #endif /* IPECAMERA_DEBUG_RAW_PACKETS */
  92. if (!ctx->cur_size) {
  93. #if defined(IPECAMERA_BUG_INCOMPLETE_PACKETS)||defined(IPECAMERA_BUG_MULTIFRAME_PACKETS)
  94. size_t startpos;
  95. for (startpos = 0; (startpos + sizeof(frame_magic)) <= bufsize; startpos += sizeof(uint32_t)) {
  96. if (!memcmp(buf + startpos, frame_magic, sizeof(frame_magic))) break;
  97. }
  98. if ((startpos + sizeof(frame_magic)) > bufsize) {
  99. #ifdef IPECAMERA_DEBUG_RAW_PACKETS
  100. FILE *f = fopen(fname, "w");
  101. if (f) fclose(f);
  102. #endif /* IPECAMERA_DEBUG_RAW_PACKETS */
  103. if (invalid_frame_id != ctx->event_id) {
  104. // pcilib_warning("No frame magic in DMA packet of %u bytes, current event %lu", bufsize, ctx->event_id);
  105. invalid_frame_id = ctx->event_id;
  106. }
  107. return PCILIB_STREAMING_CONTINUE;
  108. }
  109. if (startpos) {
  110. // pass padding to rawdata callback
  111. if (ctx->event.params.rawdata.callback) {
  112. res = ctx->event.params.rawdata.callback(0, NULL, PCILIB_EVENT_FLAG_RAW_DATA_ONLY, startpos, buf, ctx->event.params.rawdata.user);
  113. if (res <= 0) {
  114. if (res < 0) return res;
  115. ctx->run_reader = 0;
  116. }
  117. }
  118. buf += startpos;
  119. bufsize -= startpos;
  120. }
  121. #endif /* IPECAMERA_BUG_INCOMPLETE_PACKETS */
  122. if ((bufsize >= 8)&&(!memcmp(buf, frame_magic, sizeof(frame_magic)))) {
  123. size_t n_lines = ((uint32_t*)buf)[5] & 0x7FF;
  124. ipecamera_compute_buffer_size(ctx, n_lines);
  125. ctx->frame[ctx->buffer_pos].event.info.seqnum = ((uint32_t*)buf)[6] & 0x1FFFFFF;
  126. ctx->frame[ctx->buffer_pos].event.info.offset = (((uint32_t*)buf)[7] & 0xFFFFFF) * 80;
  127. gettimeofday(&ctx->frame[ctx->buffer_pos].event.info.timestamp, NULL);
  128. } else {
  129. // pcilib_warning("Frame magic is not found, ignoring broken data...");
  130. return PCILIB_STREAMING_CONTINUE;
  131. }
  132. }
  133. #ifdef IPECAMERA_BUG_MULTIFRAME_PACKETS
  134. // for rawdata_callback with complete padding
  135. real_size = bufsize;
  136. if (ctx->cur_size + bufsize > ctx->cur_raw_size) {
  137. size_t need;
  138. for (need = ctx->cur_raw_size - ctx->cur_size; (need + sizeof(frame_magic)) < bufsize; need += sizeof(uint32_t)) {
  139. if (!memcmp(buf + need, frame_magic, sizeof(frame_magic))) break;
  140. }
  141. if ((need + sizeof(frame_magic)) < bufsize) {
  142. extra_data = bufsize - need;
  143. //bufsize = need;
  144. eof = 1;
  145. }
  146. // just rip of padding
  147. bufsize = ctx->cur_raw_size - ctx->cur_size;
  148. #ifdef IPECAMERA_DEBUG_RAW_PACKETS
  149. sprintf(fname + strlen(fname) - 8, ".partial");
  150. FILE *f = fopen(fname, "w");
  151. if (f) {
  152. fwrite(buf, 1, bufsize, f);
  153. fclose(f);
  154. }
  155. #endif /* IPECAMERA_DEBUG_RAW_PACKETS */
  156. }
  157. #endif /* IPECAMERA_BUG_MULTIFRAME_PACKETS */
  158. if (ctx->parse_data) {
  159. if (ctx->cur_size + bufsize > ctx->full_size) {
  160. pcilib_error("Unexpected event data, we are expecting at maximum (%zu) bytes, but (%zu) already read", ctx->full_size, ctx->cur_size + bufsize);
  161. return -PCILIB_ERROR_TOOBIG;
  162. }
  163. if (bufsize)
  164. memcpy(ctx->buffer + ctx->buffer_pos * ctx->padded_size + ctx->cur_size, buf, bufsize);
  165. }
  166. ctx->cur_size += bufsize;
  167. // printf("%i: %i %i\n", ctx->buffer_pos, ctx->cur_size, bufsize);
  168. if (ctx->cur_size >= ctx->cur_raw_size) {
  169. eof = 1;
  170. }
  171. if (ctx->event.params.rawdata.callback) {
  172. 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);
  173. if (res <= 0) {
  174. if (res < 0) return res;
  175. ctx->run_reader = 0;
  176. }
  177. }
  178. if (eof) {
  179. if ((ipecamera_new_frame(ctx))||(!ctx->run_reader)) {
  180. return PCILIB_STREAMING_STOP;
  181. }
  182. #ifdef IPECAMERA_BUG_MULTIFRAME_PACKETS
  183. if (extra_data) {
  184. return ipecamera_data_callback(user, flags, extra_data, buf + (real_size - extra_data));
  185. }
  186. #endif /* IPECAMERA_BUG_MULTIFRAME_PACKETS */
  187. }
  188. return PCILIB_STREAMING_REQ_FRAGMENT;
  189. }
  190. void *ipecamera_reader_thread(void *user) {
  191. int err;
  192. ipecamera_t *ctx = (ipecamera_t*)user;
  193. while (ctx->run_reader) {
  194. err = pcilib_stream_dma(ctx->event.pcilib, ctx->rdma, 0, 0, PCILIB_DMA_FLAG_MULTIPACKET, IPECAMERA_DMA_TIMEOUT, &ipecamera_data_callback, user);
  195. if (err) {
  196. if (err == PCILIB_ERROR_TIMEOUT) {
  197. if (ctx->cur_size >= ctx->cur_raw_size) ipecamera_new_frame(ctx);
  198. #ifdef IPECAMERA_BUG_INCOMPLETE_PACKETS
  199. else if (ctx->cur_size > 0) ipecamera_new_frame(ctx);
  200. #endif /* IPECAMERA_BUG_INCOMPLETE_PACKETS */
  201. if (pcilib_check_deadline(&ctx->autostop.timestamp, 0)) {
  202. ctx->run_reader = 0;
  203. break;
  204. }
  205. usleep(IPECAMERA_NOFRAME_SLEEP);
  206. } else pcilib_error("DMA error while reading IPECamera frames, error: %i", err);
  207. } //else printf("no error\n");
  208. //usleep(1000);
  209. }
  210. ctx->run_streamer = 0;
  211. // if (ctx->cur_size)
  212. // pcilib_error("partialy read frame after stop signal, %zu bytes in the buffer", ctx->cur_size);
  213. return NULL;
  214. }