reader.c 7.3 KB

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