From patchwork Wed Jul 13 12:15:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 16622 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 0ED44BE173 for ; Wed, 13 Jul 2022 12:15:49 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8E6E66274E; Wed, 13 Jul 2022 14:15:48 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1657714548; bh=GO3Krw5uwjFk7YwRH/hdssrCPN8DqXa+6iftBKPwzt0=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=HD0V6ckZEyHrpbguRVEHKAn7xZJFL0Uc61ezJqs6MTvkufbfPVaffJluKeZ6zy8Oi IjT+YFrp+hRBrpMonbDIImLwUFd5tBDv5SDks+ZNXD39JrJrNu3lzZdzl06nLDB6kf YTwzEIR5FbLokDZtTunfeDvXrDriU3zFoEeEnqgyU3QBTvKoj9y7JXELqf0hA+uK4p iYyFDpWfOIaOY48YvuY1fmrJAjqN0uWFCnmiU5efI8hsAE43QlUVB8bLhcCvmVu8Pp wH2yjp0Rt2dj0qstaIrO1RhOQZ9E+X1PhRDnQkaK7lhKLuZPS66cBTCFvadHb/ggTq ch42KvS7a0GSw== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 1434D6274E for ; Wed, 13 Jul 2022 14:15:47 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="UjdtbxA6"; dkim-atps=neutral Received: from perceval.ideasonboard.com (unknown [IPv6:2401:4900:1f3f:dcbd:bc88:bb0e:d3d8:8610]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id B281F305; Wed, 13 Jul 2022 14:15:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1657714546; bh=GO3Krw5uwjFk7YwRH/hdssrCPN8DqXa+6iftBKPwzt0=; h=From:To:Cc:Subject:Date:From; b=UjdtbxA6jj28QSb8yEdH95iQb3pGfour3qsHhMriRI+9zmXJAVYEMOugZTIiWj+8d CiiTnqO9dXEG2xcg0oCcXYbsdCNC2QEDnWfzCwgtKYSH04iupQF+RcZIFXHKiZP2W9 cq3a2WypOAiUtg6yVx5TxybutiHFFQQT9PwC847k= To: libcamera-devel@lists.libcamera.org Date: Wed, 13 Jul 2022 17:45:34 +0530 Message-Id: <20220713121534.115492-1-umang.jain@ideasonboard.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3] utils: ipu3-pack: Provide a 10-bit bayer packing utility X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Umang Jain via libcamera-devel From: Umang Jain Reply-To: Umang Jain Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Provide a 10-bit bayer packing utility for the unpacked data produced by ipu3-unpack. Usage: ipu3-unpack input-file output-file The output-file can be "-" to pack the data to stdout. The output file generated by ipu3-pack can be directly fed to IMGU for streaming. Signed-off-by: Umang Jain Reviewed-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- Changes in v3: - Drop fflush() as stdout is per process not global - Use sizeof () directly for bytes to read - consistency of goto --- utils/ipu3/ipu3-pack.c | 101 +++++++++++++++++++++++++++++++++++++++++ utils/ipu3/meson.build | 1 + 2 files changed, 102 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..decbfc6c --- /dev/null +++ b/utils/ipu3/ipu3-pack.c @@ -0,0 +1,101 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * ipu3-pack - Convert unpacked RAW10 Bayer data to the IPU3 packed Bayer formats + * + * Copyright 2022 Umang Jain + */ +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include + +static void usage(const char *argv0) +{ + + printf("Usage: %s input-file output-file\n", basename(argv0)); + printf("Convert unpacked RAW10 Bayer data to the IPU3 packed Bayer formats\n"); + printf("If the output-file '-', output data will be written to standard output\n"); +} + +int main(int argc, char *argv[]) +{ + int in_fd; + int out_fd; + int ret; + + if (argc != 3) { + usage(argv[0]); + return 1; + } + + in_fd = open(argv[1], O_RDONLY); + if (in_fd == -1) { + fprintf(stderr, "Failed to open input file '%s': %s\n", + argv[1], strerror(errno)); + return 1; + } + + if (strcmp(argv[2], "-") == 0) { + out_fd = STDOUT_FILENO; + } else { + out_fd = open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, 0644); + if (out_fd == -1) { + fprintf(stderr, "Failed to open output file '%s': %s\n", + argv[2], strerror(errno)); + close(in_fd); + return 1; + } + } + + while (1) { + uint16_t in_data[25]; + uint8_t out_data[32]; + unsigned int i; + + ret = read(in_fd, in_data, sizeof(in_data)); + if (ret < 0) { + fprintf(stderr, "Failed to read input data: %s\n", + strerror(errno)); + goto done; + } + + if ((unsigned)ret < sizeof(in_data)) { + if (ret != 0) + fprintf(stderr, "%u bytes of stray data at end of input\n", + ret); + goto done; + } + + for (i = 0; i < 30; ++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); + } + + out_data[30] = (in_data[24] >> 0) & 0xff; + out_data[31] = (in_data[24] >> 8) & 0x03; + + ret = write(out_fd, out_data, sizeof(out_data)); + if (ret < 0) { + fprintf(stderr, "Failed to write output data: %s\n", + strerror(errno)); + goto done; + } + } + +done: + close(in_fd); + if (out_fd != STDOUT_FILENO) + 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')