libipe.c 8.2 KB

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