reader.c 8.1 KB

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