ufodecode.c 9.8 KB

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