libipe.c 9.8 KB

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