[libcamera-devel] [PATCH 2/2] utils: ipu3-pack: Provide a 10-bit bayer packing utility

Umang Jain umang.jain at ideasonboard.com
Sat Jul 9 00:28:38 CEST 2022


Provide a 10-bit bayer packing utility for the unpacked data produced
by ipu3-unpack. Single or multiple input unpacked files can be packed
and concatenated together in a single output file.

Use-case where this is useful is streaming IMGU with different
test patterns on each frame. The output file generated by
ipu3-pack can be directly fed to IMGU.

Signed-off-by: Umang Jain <umang.jain at ideasonboard.com>
---
 utils/ipu3/ipu3-pack.c | 129 +++++++++++++++++++++++++++++++++++++++++
 utils/ipu3/meson.build |   1 +
 2 files changed, 130 insertions(+)
 create mode 100644 utils/ipu3/ipu3-pack.c

diff --git a/utils/ipu3/ipu3-pack.c b/utils/ipu3/ipu3-pack.c
new file mode 100644
index 00000000..48193f74
--- /dev/null
+++ b/utils/ipu3/ipu3-pack.c
@@ -0,0 +1,129 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * ipu3-pack - Pack IPU3 raw Bayer format to 10-bit Bayer
+ *
+ * Copyright 2022 Umang Jain <umang.jain at ideasonboard.com>
+ */
+#define _GNU_SOURCE
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+static void usage(const char *argv0)
+{
+	printf("Usage: %s input-file(s) -o output-file\n", basename(argv0));
+	printf("Pack the IPU3 raw Bayer format to 10-bit Bayer\n");
+}
+
+int packInputFile(int in_fd, int out_fd)
+{
+	int ret = 0;
+
+	while (1) {
+		uint16_t in_data[25];
+		uint8_t out_data[32];
+		unsigned int i;
+
+		ret = read(in_fd, in_data, 50);
+		if (ret == -1) {
+			fprintf(stderr, "Failed to read input data: %s\n",
+				strerror(errno));
+			break;
+		}
+
+		if (ret < 50) {
+			if (ret != 0)
+				fprintf(stderr, "%u bytes of stray data at end of input\n",
+					ret);
+			break;
+		}
+
+		for (i = 0; i < 32; ++i) {
+			unsigned int index = (i * 8) / 10;
+			unsigned int msb_shift = (i * 8) % 10;
+			unsigned int lsb_shift = 10 - msb_shift;
+
+			out_data[i] = ((in_data[index] >> msb_shift) & 0xff)
+				    | ((in_data[index+1] << lsb_shift) & 0xff);
+		}
+
+		/* 6 MSB are padding on the 32nd byte. */
+		out_data[31] = out_data[31] & 0x03;
+
+		ret = write(out_fd, out_data, 32);
+		if (ret < -1) {
+			fprintf(stderr, "Failed to write output data: %s\n",
+				strerror(errno));
+			return ret;
+		}
+	}
+
+	return ret;
+}
+int main(int argc, char *argv[])
+{
+	int input_files = argc - 3;
+	int in_fds[input_files];
+	int out_fd = -1;
+	int j = 1;
+	int ret;
+	bool oFlag = false;
+
+	if (argc < 4) {
+		usage(argv[0]);
+		return 1;
+	}
+
+	while (j < argc) {
+		if (strcmp(argv[j], "-o") == 0) {
+			oFlag = true;
+			break;
+		}
+
+		int fd = open(argv[j], O_RDONLY);
+		if (fd == -1) {
+			fprintf(stderr, "Failed to open input file '%s': %s\n",
+				argv[j], strerror(errno));
+			goto done;
+		}
+
+		in_fds[j - 1] = fd;
+		j++;
+	}
+
+	if (!oFlag) {
+		fprintf(stderr, "Missing option '-o' for output file\n");
+		usage(argv[0]);
+		goto done;
+	}
+
+	out_fd = open(argv[++j], O_WRONLY | O_TRUNC | O_CREAT, 0644);
+	if (out_fd == -1) {
+		fprintf(stderr, "Failed to open output file '%s': %s\n",
+			argv[j], strerror(errno));
+		goto done;
+	}
+
+	for (j = 0; j < input_files; j++) {
+		ret = packInputFile(in_fds[j], out_fd);
+		if (ret < 0) {
+			fprintf(stderr, "Error packing input file '%s'\n",
+				argv[j + 1]);
+			goto done;
+		}
+	}
+
+done:
+	for (j = 0; j < input_files; j++)
+		close(in_fds[j]);
+	close(out_fd);
+
+	return ret ? 1 : 0;
+}
diff --git a/utils/ipu3/meson.build b/utils/ipu3/meson.build
index 88049f58..c92cc658 100644
--- a/utils/ipu3/meson.build
+++ b/utils/ipu3/meson.build
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: CC0-1.0
 
+ipu3_pack = executable('ipu3-pack', 'ipu3-pack.c')
 ipu3_unpack = executable('ipu3-unpack', 'ipu3-unpack.c')
-- 
2.31.1



More information about the libcamera-devel mailing list