ufodecode.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include "ufodecode.h"
  6. #include "ufodecode-private.h"
  7. #include "config.h"
  8. #ifdef HAVE_SSE
  9. #include <xmmintrin.h>
  10. #endif
  11. #define IPECAMERA_NUM_CHANNELS 16 /**< Number of channels per row */
  12. #define IPECAMERA_PIXELS_PER_CHANNEL 128 /**< Number of pixels per channel */
  13. #define IPECAMERA_WIDTH (IPECAMERA_NUM_CHANNELS * IPECAMERA_PIXELS_PER_CHANNEL) /**< Total pixel width of row */
  14. /**
  15. * Check if value matches expected input.
  16. */
  17. #define CHECK_VALUE(value, expected) \
  18. if (value != expected) { \
  19. fprintf(stderr, "<%s:%i> 0x%x != 0x%x\n", __FILE__, __LINE__, value, expected); \
  20. err = 1; \
  21. }
  22. /**
  23. * Check that flag evaluates to non-zero.
  24. */
  25. #define CHECK_FLAG(flag, check, ...) \
  26. if (!(check)) { \
  27. fprintf(stderr, "<%s:%i> Unexpected value 0x%x of " flag "\n", __FILE__, __LINE__, __VA_ARGS__); \
  28. err = 1; \
  29. }
  30. /**
  31. * \brief Setup a new decoder instance
  32. *
  33. * \param height Number of rows that are expected in the data stream
  34. * \param raw The data stream from the camera or NULL if set later with
  35. * ufo_decoder_set_raw_data.
  36. * \param num_bytes Size of the data stream buffer in bytes
  37. *
  38. * \return A new decoder instance that can be used to iterate over the frames
  39. * using ufo_decoder_get_next_frame.
  40. */
  41. ufo_decoder ufo_decoder_new(uint32_t height, uint32_t *raw, size_t num_bytes)
  42. {
  43. ufo_decoder decoder = malloc(sizeof(struct ufo_decoder_t));
  44. if (decoder == NULL)
  45. return NULL;
  46. decoder->height = height;
  47. ufo_decoder_set_raw_data(decoder, raw, num_bytes);
  48. return decoder;
  49. }
  50. /**
  51. * \brief Release decoder instance
  52. *
  53. * \param decoder An ufo_decoder instance
  54. */
  55. void ufo_decoder_free(ufo_decoder decoder)
  56. {
  57. free(decoder);
  58. }
  59. /**
  60. * \brief Set raw data stream
  61. *
  62. * \param decoder An ufo_decoder instance
  63. * \param raw Raw data stream
  64. * \param num_bytes Size of data stream buffer in bytes
  65. */
  66. void ufo_decoder_set_raw_data(ufo_decoder decoder, uint32_t *raw, size_t num_bytes)
  67. {
  68. decoder->raw = raw;
  69. decoder->num_bytes = num_bytes;
  70. decoder->current_pos = 0;
  71. }
  72. static int ufo_decode_frame(uint16_t *pixel_buffer, uint32_t *raw, int num_rows, int *offset)
  73. {
  74. static int channel_order[IPECAMERA_NUM_CHANNELS] = { 15, 13, 14, 12, 10, 8, 11, 7, 9, 6, 5, 2, 4, 3, 0, 1 };
  75. int info;
  76. int row = 0;
  77. int channel = 0;
  78. int pos = 0;
  79. uint32_t data;
  80. const int bytes = 43;
  81. #ifdef HAVE_SSE
  82. __m128i mask = _mm_set_epi32(0x3FF, 0x3FF, 0x3FF, 0x3FF);
  83. __m128i packed;
  84. __m128i tmp1, tmp2;
  85. uint32_t result[4] __attribute__ ((aligned (16))) = {0};
  86. #endif
  87. do {
  88. info = raw[0];
  89. row = (info >> 4) & 0x7FF;
  90. int pixels = (info >> 20) & 0xFF;
  91. channel = channel_order[info & 0x0F];
  92. int base = row * IPECAMERA_WIDTH + channel * IPECAMERA_PIXELS_PER_CHANNEL;
  93. #ifdef DEBUG
  94. int err = 0;
  95. int header = (info >> 30) & 0x03; // 2 bits
  96. const int bpp = (info >> 16) & 0x0F; // 4 bits
  97. CHECK_FLAG("raw header magick", header == 2, header);
  98. CHECK_FLAG("pixel size, only 10 bits are supported", bpp == 10, bpp);
  99. CHECK_FLAG("channel, limited by %i output channels", channel < IPECAMERA_NUM_CHANNELS, channel, IPECAMERA_NUM_CHANNELS);
  100. #endif
  101. /* "Correct" missing pixel */
  102. if ((row < 2) && (pixels == (IPECAMERA_PIXELS_PER_CHANNEL - 1))) {
  103. pixel_buffer[base] = 0;
  104. /* base++; */
  105. }
  106. #ifdef DEBUG
  107. else
  108. CHECK_FLAG("number of pixels, %i is expected", pixels == IPECAMERA_PIXELS_PER_CHANNEL, pixels, IPECAMERA_PIXELS_PER_CHANNEL);
  109. #endif
  110. #ifdef HAVE_SSE
  111. for (int i = 1 ; i < bytes-4; i += 4, base += 12) {
  112. packed = _mm_set_epi32(raw[i], raw[i+1], raw[i+2], raw[i+3]);
  113. tmp1 = _mm_srli_epi32(packed, 20);
  114. tmp2 = _mm_and_si128(tmp1, mask);
  115. _mm_storeu_si128((__m128i*) result, tmp2);
  116. pixel_buffer[base] = result[0];
  117. pixel_buffer[base+3] = result[1];
  118. pixel_buffer[base+6] = result[2];
  119. pixel_buffer[base+9] = result[3];
  120. tmp1 = _mm_srli_epi32(packed, 10);
  121. tmp2 = _mm_and_si128(tmp1, mask);
  122. _mm_storeu_si128((__m128i*) result, tmp2);
  123. pixel_buffer[base+1] = result[0];
  124. pixel_buffer[base+4] = result[1];
  125. pixel_buffer[base+7] = result[2];
  126. pixel_buffer[base+10] = result[3];
  127. tmp1 = _mm_and_si128(packed, mask);
  128. _mm_storeu_si128((__m128i*) result, tmp1);
  129. pixel_buffer[base+2] = result[0];
  130. pixel_buffer[base+5] = result[1];
  131. pixel_buffer[base+8] = result[2];
  132. pixel_buffer[base+11] = result[3];
  133. }
  134. /* Compute last pixels the usual way */
  135. for (int i = bytes-4; i < bytes; i++) {
  136. data = raw[i];
  137. pixel_buffer[base++] = (data >> 20) & 0x3FF;
  138. pixel_buffer[base++] = (data >> 10) & 0x3FF;
  139. pixel_buffer[base++] = data & 0x3FF;
  140. }
  141. #else
  142. for (int i = 1 ; i < bytes; i++) {
  143. data = raw[i];
  144. #ifdef DEBUG
  145. header = (data >> 30) & 0x03;
  146. CHECK_FLAG("raw data magick", header == 3, header);
  147. if (err)
  148. return err;
  149. #endif
  150. pixel_buffer[base++] = (data >> 20) & 0x3FF;
  151. pixel_buffer[base++] = (data >> 10) & 0x3FF;
  152. pixel_buffer[base++] = data & 0x3FF;
  153. }
  154. #endif
  155. data = raw[bytes];
  156. #ifdef DEBUG
  157. header = (data >> 30) & 0x03;
  158. CHECK_FLAG("raw data magick", header == 3, header);
  159. CHECK_FLAG("raw footer magick", (data & 0x3FF) == 0x55, (data & 0x3FF));
  160. if (err)
  161. return err;
  162. #endif
  163. int ppw = pixels >> 6;
  164. for (int j = 0; j < ppw; j++)
  165. pixel_buffer[base++] = (data >> (10 * (ppw - j))) & 0x3FF;
  166. pos += bytes + 1;
  167. raw += bytes + 1;
  168. } while ((row < (num_rows - 1)) || (channel != 1));
  169. *offset = pos;
  170. return 0;
  171. }
  172. /**
  173. * \brief Deinterlace by interpolating between two rows
  174. *
  175. * \param in Input frame
  176. * \param out Destination of interpolated frame
  177. * \param width Width of frame in pixels
  178. * \param height Height of frame in pixels
  179. */
  180. void ufo_deinterlace_interpolate(const uint16_t *in, uint16_t *out, int width, int height)
  181. {
  182. const size_t row_size_bytes = width * sizeof(uint16_t);
  183. for (int row = 0; row < height; row++) {
  184. /* Copy one line */
  185. memcpy(out, in + row*width, row_size_bytes);
  186. out += width;
  187. /* Interpolate between source row and row+1 */
  188. for (int x = 0; x < width; x++) {
  189. out[x] = (int) (0.5 * in[row*width + x] + 0.5 * in[(row+1)*width + x]);
  190. }
  191. out += width;
  192. }
  193. /* Copy last row */
  194. memcpy(out, in + width * (height - 1), row_size_bytes);
  195. }
  196. /**
  197. * \brief Deinterlace by "weaving" the rows of two frames
  198. *
  199. * \param in1 First frame
  200. * \param in2 Second frame
  201. * \param out Destination of weaved frame
  202. * \param width Width of frame in pixels
  203. * \param height Height of frame in pixels
  204. */
  205. void ufo_deinterlace_weave(const uint16_t *in1, const uint16_t *in2, uint16_t *out, int width, int height)
  206. {
  207. const size_t row_size_bytes = width * sizeof(uint16_t);
  208. for (int row = 0; row < height; row++) {
  209. memcpy(out, in1 + row*width, row_size_bytes);
  210. out += width;
  211. memcpy(out, in2 + row*width, row_size_bytes);
  212. out += width;
  213. }
  214. }
  215. /**
  216. * \brief Iterate and decode next frame
  217. *
  218. * This function tries to decode the next frame in the currently set raw data
  219. * stream.
  220. *
  221. * \param decoder An ufo_decoder instance
  222. * \param pixels If pointer with NULL content is passed, a new buffer is
  223. * allocated otherwise, this user-supplied buffer is used.
  224. * \param frame_number Frame number as reported in the header
  225. * \param time_stamp Time stamp of the frame as reported in the header
  226. *
  227. * \return 0 in case of no error, ENOSR if end of stream was reached, ENOMEM if
  228. * NULL was passed but no memory could be allocated, EILSEQ if data stream is
  229. * corrupt and EFAULT if pixels is a NULL-pointer.
  230. */
  231. int ufo_decoder_get_next_frame(ufo_decoder decoder, uint16_t **pixels, uint32_t *frame_number, uint32_t *time_stamp)
  232. {
  233. uint32_t *raw = decoder->raw;
  234. int err = 0;
  235. int pos = decoder->current_pos;
  236. int advance;
  237. const int num_words = decoder->num_bytes / 4;
  238. if (pixels == NULL)
  239. return EFAULT;
  240. if (pos >= num_words)
  241. return ENOSR;
  242. if (num_words < 16)
  243. return EILSEQ;
  244. if (*pixels == NULL) {
  245. *pixels = (uint16_t *) malloc(IPECAMERA_WIDTH * decoder->height * sizeof(uint16_t));
  246. if (*pixels == NULL)
  247. return ENOMEM;
  248. }
  249. #ifdef DEBUG
  250. CHECK_VALUE(raw[pos++], 0x51111111);
  251. CHECK_VALUE(raw[pos++], 0x52222222);
  252. CHECK_VALUE(raw[pos++], 0x53333333);
  253. CHECK_VALUE(raw[pos++], 0x54444444);
  254. CHECK_VALUE(raw[pos++], 0x55555555);
  255. CHECK_VALUE(raw[pos++], 0x56666666);
  256. CHECK_VALUE(raw[pos] >> 28, 0x5);
  257. *frame_number = raw[pos++] & 0xF0000000;
  258. CHECK_VALUE(raw[pos] >> 28, 0x5);
  259. *time_stamp = raw[pos++] & 0xF0000000;
  260. if (err)
  261. return EILSEQ;
  262. #else
  263. *frame_number = raw[pos + 6] & 0xF0000000;
  264. *time_stamp = raw[pos + 7] & 0xF0000000;
  265. pos += 8;
  266. #endif
  267. err = ufo_decode_frame(*pixels, raw + pos, decoder->height, &advance);
  268. if (err)
  269. return EILSEQ;
  270. pos += advance;
  271. #ifdef DEBUG
  272. CHECK_VALUE(raw[pos++], 0x0AAAAAAA);
  273. CHECK_VALUE(raw[pos++], 0x0BBBBBBB);
  274. CHECK_VALUE(raw[pos++], 0x0CCCCCCC);
  275. CHECK_VALUE(raw[pos++], 0x0DDDDDDD);
  276. CHECK_VALUE(raw[pos++], 0x0EEEEEEE);
  277. CHECK_VALUE(raw[pos++], 0x0FFFFFFF);
  278. CHECK_VALUE(raw[pos++], 0x00000000);
  279. CHECK_VALUE(raw[pos++], 0x01111111);
  280. #else
  281. pos += 8;
  282. #endif
  283. /* if bytes left and we see fill bytes, skip them */
  284. if ((raw[pos] == 0x0) && (raw[pos+1] == 0x1111111)) {
  285. pos += 2;
  286. while ((pos < num_words) && ((raw[pos] == 0x89abcdef) || (raw[pos] == 0x1234567)))
  287. pos++;
  288. }
  289. decoder->current_pos = pos;
  290. return 0;
  291. }