summaryrefslogtreecommitdiffstats
path: root/src/ufodecode.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ufodecode.c')
-rw-r--r--src/ufodecode.c72
1 files changed, 53 insertions, 19 deletions
diff --git a/src/ufodecode.c b/src/ufodecode.c
index d1a3984..5b8b874 100644
--- a/src/ufodecode.c
+++ b/src/ufodecode.c
@@ -1,4 +1,3 @@
-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -46,12 +45,16 @@
* \return A new decoder instance that can be used to iterate over the frames
* using ufo_decoder_get_next_frame.
*/
-ufo_decoder ufo_decoder_new(uint32_t height, uint32_t *raw, size_t num_bytes)
+ufo_decoder ufo_decoder_new(uint32_t height, uint32_t width, uint32_t *raw, size_t num_bytes)
{
+ if (width%IPECAMERA_PIXELS_PER_CHANNEL)
+ return NULL;
+
ufo_decoder decoder = malloc(sizeof(struct ufo_decoder_t));
if (decoder == NULL)
return NULL;
+ decoder->width = width;
decoder->height = height;
ufo_decoder_set_raw_data(decoder, raw, num_bytes);
return decoder;
@@ -83,16 +86,23 @@ void ufo_decoder_set_raw_data(ufo_decoder decoder, uint32_t *raw, size_t num_byt
decoder->current_pos = 0;
}
-static int ufo_decode_frame(uint16_t *pixel_buffer, uint32_t *raw, int num_rows, int *offset)
+static int ufo_decode_frame(ufo_decoder decoder, uint16_t *pixel_buffer, uint16_t *cmask, uint32_t *raw, size_t num_words, int *offset)
{
static int channel_order[IPECAMERA_NUM_CHANNELS] = { 15, 13, 14, 12, 10, 8, 11, 7, 9, 6, 5, 2, 4, 3, 0, 1 };
+ static int channel_size = (2 + IPECAMERA_PIXELS_PER_CHANNEL / 3);
int info;
+ int num_rows = decoder->height;
+ int num_cols = decoder->width;
+ size_t c;
+ const size_t cpl = (num_cols / IPECAMERA_PIXELS_PER_CHANNEL);
+ const size_t cpi = num_rows * cpl;
int row = 0;
int channel = 0;
+ int pixels = 0;
int pos = 0;
uint32_t data;
- const int bytes = 43;
+ const int bytes = channel_size - 1;
#ifdef HAVE_SSE
__m128i mask = _mm_set_epi32(0x3FF, 0x3FF, 0x3FF, 0x3FF);
@@ -101,13 +111,18 @@ static int ufo_decode_frame(uint16_t *pixel_buffer, uint32_t *raw, int num_rows,
uint32_t result[4] __attribute__ ((aligned (16))) = {0};
#endif
- do {
+ if (cpi * channel_size > num_words) {
+#ifdef DEBUG
+ fprintf(stderr, "Not enough data to decode frame, expected %lu bytes, but received %lu\n", cpi * channel_size * sizeof(uint32_t), num_words * sizeof(uint32_t));
+#endif
+ return EILSEQ;
+ }
+
+ for (c = 0; c < cpi; c++) {
info = raw[0];
row = (info >> 4) & 0x7FF;
- int pixels = (info >> 20) & 0xFF;
-
- channel = channel_order[info & 0x0F];
- int base = row * IPECAMERA_WIDTH + channel * IPECAMERA_PIXELS_PER_CHANNEL;
+ channel = info & 0x0F;
+ pixels = (info >> 20) & 0xFF;
#ifdef DEBUG
int err = 0;
@@ -118,6 +133,16 @@ static int ufo_decode_frame(uint16_t *pixel_buffer, uint32_t *raw, int num_rows,
CHECK_FLAG("channel, limited by %i output channels", channel < IPECAMERA_NUM_CHANNELS, channel, IPECAMERA_NUM_CHANNELS);
#endif
+
+ if ((row > num_rows)||(channel > cpl)||(pixels>IPECAMERA_PIXELS_PER_CHANNEL))
+ return EILSEQ;
+
+ if (cmask) cmask[row] |= (1<<channel);
+
+ channel = channel_order[channel];
+
+ int base = row * IPECAMERA_WIDTH + channel * IPECAMERA_PIXELS_PER_CHANNEL;
+
/* "Correct" missing pixel */
if ((row < 2) && (pixels == (IPECAMERA_PIXELS_PER_CHANNEL - 1))) {
pixel_buffer[base] = 0;
@@ -162,6 +187,7 @@ static int ufo_decode_frame(uint16_t *pixel_buffer, uint32_t *raw, int num_rows,
pixel_buffer[base++] = (data >> 20) & 0x3FF;
pixel_buffer[base++] = (data >> 10) & 0x3FF;
pixel_buffer[base++] = data & 0x3FF;
+
data = raw[42];
pixel_buffer[base++] = (data >> 20) & 0x3FF;
pixel_buffer[base++] = (data >> 10) & 0x3FF;
@@ -194,9 +220,9 @@ static int ufo_decode_frame(uint16_t *pixel_buffer, uint32_t *raw, int num_rows,
for (int j = 0; j < ppw; j++)
pixel_buffer[base++] = (data >> (10 * (ppw - j))) & 0x3FF;
- pos += bytes + 1;
- raw += bytes + 1;
- } while ((row < (num_rows - 1)) || (channel != 1));
+ pos += channel_size;
+ raw += channel_size;
+ }
*offset = pos;
return 0;
@@ -264,19 +290,20 @@ void ufo_deinterlace_weave(const uint16_t *in1, const uint16_t *in2, uint16_t *o
* allocated otherwise, this user-supplied buffer is used.
* \param frame_number Frame number as reported in the header
* \param time_stamp Time stamp of the frame as reported in the header
+ * \paran cmask Change-mask
*
* \return 0 in case of no error, ENOSR if end of stream was reached, ENOMEM if
* NULL was passed but no memory could be allocated, EILSEQ if data stream is
* corrupt and EFAULT if pixels is a NULL-pointer.
*/
-int ufo_decoder_get_next_frame(ufo_decoder decoder, uint16_t **pixels, uint32_t *frame_number, uint32_t *time_stamp)
+int ufo_decoder_get_next_frame(ufo_decoder decoder, uint16_t **pixels, uint32_t *frame_number, uint32_t *time_stamp, uint16_t *cmask)
{
uint32_t *raw = decoder->raw;
int err = 0;
- int pos = decoder->current_pos;
+ size_t pos = decoder->current_pos;
int advance;
- const int num_words = decoder->num_bytes / 4;
+ const size_t num_words = decoder->num_bytes / 4;
if (pixels == NULL)
return EFAULT;
@@ -312,7 +339,7 @@ int ufo_decoder_get_next_frame(ufo_decoder decoder, uint16_t **pixels, uint32_t
pos += 8;
#endif
- err = ufo_decode_frame(*pixels, raw + pos, decoder->height, &advance);
+ err = ufo_decode_frame(decoder, *pixels, cmask, raw + pos, num_words - pos - 8, &advance);
if (err)
return EILSEQ;
@@ -321,8 +348,15 @@ int ufo_decoder_get_next_frame(ufo_decoder decoder, uint16_t **pixels, uint32_t
#ifdef DEBUG
CHECK_VALUE(raw[pos++], 0x0AAAAAAA);
CHECK_VALUE(raw[pos++], 0x0BBBBBBB);
- /* TODO: what should we check? */
- pos += 3;
+ // Statuses of previous! frame is following
+ pos++; // status1 0x840dffff is expected
+ pos++; // status2 0x0f001001 is expected
+ pos++; // status3 0x28000111 explains problems if status2 is wrong
+/*
+ CHECK_VALUE(raw[pos++], 0x0CCCCCCC);
+ CHECK_VALUE(raw[pos++], 0x0DDDDDDD);
+ CHECK_VALUE(raw[pos++], 0x0EEEEEEE);
+*/
CHECK_VALUE(raw[pos++], 0x0FFFFFFF);
CHECK_VALUE(raw[pos++], 0x00000000);
CHECK_VALUE(raw[pos++], 0x01111111);
@@ -331,7 +365,7 @@ int ufo_decoder_get_next_frame(ufo_decoder decoder, uint16_t **pixels, uint32_t
#endif
/* if bytes left and we see fill bytes, skip them */
- if ((raw[pos] == 0x0) && (raw[pos+1] == 0x1111111)) {
+ if (((pos + 2) < num_words) && ((raw[pos] == 0x0) && (raw[pos+1] == 0x1111111))) {
pos += 2;
while ((pos < num_words) && ((raw[pos] == 0x89abcdef) || (raw[pos] == 0x1234567)))
pos++;