Show a patch.

GET /api/patches/682/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 682,
    "url": "https://patchwork.libcamera.org/api/patches/682/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/682/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20190228200410.3022-5-jacopo@jmondi.org>",
    "date": "2019-02-28T20:04:04",
    "name": "[libcamera-devel,04/10] libcamera: ipu3: Propagate image format",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "5226f3dbcd17a4a2d28cd52f5a90953ebfa3e4c5",
    "submitter": {
        "id": 3,
        "url": "https://patchwork.libcamera.org/api/people/3/?format=api",
        "name": "Jacopo Mondi",
        "email": "jacopo@jmondi.org"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/682/mbox/",
    "series": [
        {
            "id": 199,
            "url": "https://patchwork.libcamera.org/api/series/199/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=199",
            "date": "2019-02-28T20:04:00",
            "name": "libcamera: ipu3: ImgU support",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/199/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/682/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/682/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<jacopo@jmondi.org>",
        "Received": [
            "from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net\n\t[217.70.183.196])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 18B79610C4\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 28 Feb 2019 21:03:49 +0100 (CET)",
            "from uno.lan (2-224-242-101.ip172.fastwebnet.it [2.224.242.101])\n\t(Authenticated sender: jacopo@jmondi.org)\n\tby relay4-d.mail.gandi.net (Postfix) with ESMTPSA id 99781E0005;\n\tThu, 28 Feb 2019 20:03:48 +0000 (UTC)"
        ],
        "X-Originating-IP": "2.224.242.101",
        "From": "Jacopo Mondi <jacopo@jmondi.org>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Thu, 28 Feb 2019 21:04:04 +0100",
        "Message-Id": "<20190228200410.3022-5-jacopo@jmondi.org>",
        "X-Mailer": "git-send-email 2.20.1",
        "In-Reply-To": "<20190228200410.3022-1-jacopo@jmondi.org>",
        "References": "<20190228200410.3022-1-jacopo@jmondi.org>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH 04/10] libcamera: ipu3: Propagate image\n\tformat",
        "X-BeenThere": "libcamera-devel@lists.libcamera.org",
        "X-Mailman-Version": "2.1.23",
        "Precedence": "list",
        "List-Id": "<libcamera-devel.lists.libcamera.org>",
        "List-Unsubscribe": "<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>",
        "List-Archive": "<https://lists.libcamera.org/pipermail/libcamera-devel/>",
        "List-Post": "<mailto:libcamera-devel@lists.libcamera.org>",
        "List-Help": "<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>",
        "List-Subscribe": "<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>",
        "X-List-Received-Date": "Thu, 28 Feb 2019 20:03:49 -0000"
    },
    "content": "Apply the requested image format to the sensor device, and apply the\nadjusted one to the CIO2 device, the ImgU subdevice and its input and\noutput video devices.\n\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\n---\n src/libcamera/pipeline/ipu3/ipu3.cpp | 257 +++++++++++++++++++++++----\n 1 file changed, 224 insertions(+), 33 deletions(-)",
    "diff": "diff --git a/src/libcamera/pipeline/ipu3/ipu3.cpp b/src/libcamera/pipeline/ipu3/ipu3.cpp\nindex 9fa59c1bc97e..1e89e57f628b 100644\n--- a/src/libcamera/pipeline/ipu3/ipu3.cpp\n+++ b/src/libcamera/pipeline/ipu3/ipu3.cpp\n@@ -8,11 +8,14 @@\n #include <memory>\n #include <vector>\n \n+#include <linux/media-bus-format.h>\n+\n #include <libcamera/camera.h>\n #include <libcamera/request.h>\n #include <libcamera/stream.h>\n \n #include \"device_enumerator.h\"\n+#include \"geometry.h\"\n #include \"log.h\"\n #include \"media_device.h\"\n #include \"pipeline_handler.h\"\n@@ -106,6 +109,29 @@ private:\n \t\t\tPipelineHandler::cameraData(camera));\n \t}\n \n+\tvoid printDevFormat(const V4L2Device *dev, const V4L2DeviceFormat &fmt)\n+\t{\n+\t\tLOG(IPU3, Info)\n+\t\t\t<< dev->deviceNode() << \": \" << fmt.width << \"x\"\n+\t\t\t<< fmt.height << \"- 0x\" << std::hex << fmt.fourcc\n+\t\t\t<< \" planes: \" << fmt.planesCount;\n+\t}\n+\n+\tvoid printSubdevFormat(const V4L2Subdevice *dev, unsigned int pad,\n+\t\t\t       const V4L2SubdeviceFormat &fmt)\n+\t{\n+\t\tLOG(IPU3, Info)\n+\t\t\t<< \"'\" << dev->deviceName() << \"':\" << pad << \" = \"\n+\t\t\t<< fmt.width << \"x\" << fmt.height << \" - 0x\"\n+\t\t\t<< std::hex << fmt.mbus_code;\n+\t}\n+\n+\tint setImguFormat(V4L2Subdevice *imgu,\n+\t\t\t  const StreamConfiguration &config,\n+\t\t\t  Rectangle *rect);\n+\tint setSensorFormat(V4L2Subdevice *sensor,\n+\t\t\t    const StreamConfiguration &config,\n+\t\t\t    V4L2SubdeviceFormat *format);\n \tint linkImgu(ImguDevice *imgu);\n \n \tV4L2Device *openDevice(MediaDevice *media, std::string &name);\n@@ -186,14 +212,29 @@ int PipelineHandlerIPU3::configureStreams(Camera *camera,\n \t\t\t\t\t  std::map<Stream *, StreamConfiguration> &config)\n {\n \tIPU3CameraData *data = cameraData(camera);\n-\tStreamConfiguration *cfg = &config[&data->stream_];\n-\tV4L2Subdevice *sensor = data->cio2.sensor;\n+\tconst StreamConfiguration &cfg = config[&data->stream_];\n \tV4L2Subdevice *csi2 = data->cio2.csi2;\n \tV4L2Device *cio2 = data->cio2.output;\n-\tV4L2SubdeviceFormat subdevFormat = {};\n-\tV4L2DeviceFormat devFormat = {};\n \tint ret;\n \n+\t/*\n+\t * Verify that the requested size respects the IPU3 alignement\n+\t * requirements: image size shall be 8-aligned in width and 4-aligned\n+\t * in height.\n+\t *\n+\t * TODO: consider the BDS scaling factor requirements: \"the downscaling\n+\t * factor must be an integer value multiple of 1/32\"\n+\t */\n+\tif (cfg.width % 8 || cfg.height % 4) {\n+\t\tLOG(IPU3, Error) << \"Stream format not support: bad alignement\";\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tLOG(IPU3, Info)\n+\t\t<< \"Camera :'\" << camera->name() << \" - Stream format: \"\n+\t\t<< cfg.width << \"x\" << cfg.height << \" - \"\n+\t\t<< std::hex << cfg.pixelFormat;\n+\n \t/*\n \t * TODO: dynamically assign ImgU devices; as of now, with a single\n \t * stream supported, always use 'imgu0'.\n@@ -203,52 +244,73 @@ int PipelineHandlerIPU3::configureStreams(Camera *camera,\n \tif (ret)\n \t\treturn ret;\n \n+\tV4L2Device *viewfinder = data->imgu->viewfinder;\n+\tV4L2Subdevice *sensor = data->cio2.sensor;\n+\tV4L2Device *output = data->imgu->output;\n+\tV4L2Subdevice *imgu = data->imgu->imgu;\n+\tV4L2Device *input = data->imgu->input;\n+\n \t/*\n-\t * FIXME: as of now, the format gets applied to the sensor and is\n-\t * propagated along the pipeline. It should instead be applied on the\n-\t * capture device and the sensor format calculated accordingly.\n+\t * Pass the requested output image size to the sensor and get back the\n+\t * adjusted one to be propagated to the CIO2 device and to the ImgU\n+\t * input.\n \t */\n+\tV4L2SubdeviceFormat sensorFormat = {};\n+\tret = setSensorFormat(sensor, cfg, &sensorFormat);\n+\tif (ret) {\n+\t\tLOG(IPU3, Error) << \"Stream format not supported: \";\n+\t\treturn ret;\n+\t}\n+\tprintSubdevFormat(sensor, 0, sensorFormat);\n \n-\tret = sensor->getFormat(0, &subdevFormat);\n+\tret = csi2->setFormat(0, &sensorFormat);\n \tif (ret)\n \t\treturn ret;\n-\n-\tsubdevFormat.width = cfg->width;\n-\tsubdevFormat.height = cfg->height;\n-\tret = sensor->setFormat(0, &subdevFormat);\n+\tprintSubdevFormat(csi2, 0, sensorFormat);\n+\n+\t/* Apply the CIO2 image format to the CIO2 output and ImgU input. */\n+\tV4L2DeviceFormat cio2Format = {};\n+\tcio2Format.width = sensorFormat.width;\n+\tcio2Format.height = sensorFormat.height;\n+\tcio2Format.fourcc = V4L2_PIX_FMT_IPU3_SGRBG10;\n+\tcio2Format.planesCount = 1;\n+\tret = cio2->setFormat(&cio2Format);\n \tif (ret)\n \t\treturn ret;\n+\tprintDevFormat(cio2, cio2Format);\n \n-\t/* Return error if the requested format cannot be applied to sensor. */\n-\tif (subdevFormat.width != cfg->width ||\n-\t    subdevFormat.height != cfg->height) {\n-\t\tLOG(IPU3, Error)\n-\t\t\t<< \"Failed to apply image format \"\n-\t\t\t<< subdevFormat.width << \"x\" << subdevFormat.height\n-\t\t\t<< \" - got: \" << cfg->width << \"x\" << cfg->height;\n-\t\treturn -EINVAL;\n-\t}\n-\n-\tret = csi2->setFormat(0, &subdevFormat);\n+\tret = input->setFormat(&cio2Format);\n \tif (ret)\n \t\treturn ret;\n-\n-\tret = cio2->getFormat(&devFormat);\n+\tprintDevFormat(input, cio2Format);\n+\n+\t/* Apply pad formats and crop/compose rectangle to the ImgU. */\n+\tRectangle rect = {\n+\t\t.x = 0,\n+\t\t.y = 0,\n+\t\t.w = cio2Format.width,\n+\t\t.h = cio2Format.height,\n+\t};\n+\tret = setImguFormat(imgu, cfg, &rect);\n \tif (ret)\n \t\treturn ret;\n \n-\tdevFormat.width = subdevFormat.width;\n-\tdevFormat.height = subdevFormat.height;\n-\tdevFormat.fourcc = cfg->pixelFormat;\n+\t/* Apply the format to the ImgU output and viewfinder devices. */\n+\tV4L2DeviceFormat outputFormat = {};\n+\toutputFormat.width = cfg.width;\n+\toutputFormat.height = cfg.height;\n+\toutputFormat.fourcc = V4L2_PIX_FMT_NV12;\n+\toutputFormat.planesCount = 2;\n \n-\tret = cio2->setFormat(&devFormat);\n+\tret = output->setFormat(&outputFormat);\n \tif (ret)\n \t\treturn ret;\n+\tprintDevFormat(output, outputFormat);\n \n-\tLOG(IPU3, Info) << cio2->driverName() << \": \"\n-\t\t\t<< devFormat.width << \"x\" << devFormat.height\n-\t\t\t<< \"- 0x\" << std::hex << devFormat.fourcc << \" planes: \"\n-\t\t\t<< devFormat.planes;\n+\tret = viewfinder->setFormat(&outputFormat);\n+\tif (ret)\n+\t\treturn ret;\n+\tprintDevFormat(viewfinder, outputFormat);\n \n \treturn 0;\n }\n@@ -409,6 +471,135 @@ error_release_mdev:\n \treturn false;\n }\n \n+int PipelineHandlerIPU3::setImguFormat(V4L2Subdevice *imgu,\n+\t\t\t\t       const StreamConfiguration &config,\n+\t\t\t\t       Rectangle *rect)\n+{\n+\tint ret;\n+\n+\t/*\n+\t * Configure the 'imgu' subdevice with the requested sizes.\n+\t *\n+\t * FIXME: the IPU3 driver implementation shall be changed to use the\n+\t * actual input sizes as 'imgu input' subdevice sizes, and use the\n+\t * desired output sizes to configure the crop/compose rectangles.  The\n+\t * current implementation uses output sizes as 'imgu input' sizes, and\n+\t * uses the input dimension to configure the crop/compose rectangles,\n+\t * which contradicts the V4L2 specifications.\n+\t */\n+\tret = imgu->setCrop(IMGU_PAD_INPUT, rect);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tLOG(IPU3, Info)\n+\t\t<< \"'\" << imgu->deviceName() << \"':\" << IMGU_PAD_INPUT\n+\t\t<< \" = crop: (0,0)/\" << rect->w << \"x\" << rect->h;\n+\n+\tret = imgu->setCompose(IMGU_PAD_INPUT, rect);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tLOG(IPU3, Info)\n+\t\t<< \"'\" << imgu->deviceName() << \"':\" << IMGU_PAD_INPUT\n+\t\t<< \" = compose: (0,0)/\" << rect->w << \"x\" << rect->h;\n+\n+\n+\tV4L2SubdeviceFormat imguFormat = {};\n+\timguFormat.width = config.width;\n+\timguFormat.height = config.height;\n+\timguFormat.mbus_code = MEDIA_BUS_FMT_FIXED;\n+\n+\tret = imgu->setFormat(IMGU_PAD_INPUT, &imguFormat);\n+\tif (ret)\n+\t\treturn ret;\n+\tprintSubdevFormat(imgu, IMGU_PAD_INPUT, imguFormat);\n+\n+\tret = imgu->setFormat(IMGU_PAD_OUTPUT, &imguFormat);\n+\tif (ret)\n+\t\treturn ret;\n+\tprintSubdevFormat(imgu, IMGU_PAD_OUTPUT, imguFormat);\n+\n+\tret = imgu->setFormat(IMGU_PAD_VF, &imguFormat);\n+\tif (ret)\n+\t\treturn ret;\n+\tprintSubdevFormat(imgu, IMGU_PAD_VF, imguFormat);\n+\n+\tret = imgu->setFormat(IMGU_PAD_STAT, &imguFormat);\n+\tif (ret)\n+\t\treturn ret;\n+\tprintSubdevFormat(imgu, IMGU_PAD_STAT, imguFormat);\n+\n+\treturn 0;\n+}\n+\n+int PipelineHandlerIPU3::setSensorFormat(V4L2Subdevice *sensor,\n+\t\t\t\t\t const StreamConfiguration &config,\n+\t\t\t\t\t V4L2SubdeviceFormat *format)\n+{\n+\tstd::map<unsigned int, std::vector<SizeRange>> formats;\n+\tunsigned int best = ~0;\n+\tbool found = false;\n+\tint ret;\n+\n+\tformats = sensor->formats(0);\n+\tif (formats.empty()) {\n+\t\t/*\n+\t\t * If the format list is empty, try with the currently\n+\t\t * applied one.\n+\t\t */\n+\t\tret = sensor->getFormat(0, format);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\n+\t\tif (format->width < config.width ||\n+\t\t    format->height < config.height)\n+\t\t\treturn -EINVAL;\n+\n+\t\treturn 0;\n+\t}\n+\n+\t/* Search for the best approximation the sensor can provide. */\n+\tauto it = formats.begin();\n+\twhile (it != formats.end()) {\n+\t\tfor (SizeRange &size : it->second) {\n+\t\t\tif (size.maxWidth < config.width ||\n+\t\t\t    size.maxHeight < config.height)\n+\t\t\t\tcontinue;\n+\n+\t\t\tunsigned int diff =\n+\t\t\t\t(abs(size.maxWidth - config.width) +\n+\t\t\t\t abs(size.maxHeight - config.height));\n+\t\t\tif (diff >= best)\n+\t\t\t\tcontinue;\n+\n+\t\t\tbest = diff;\n+\t\t\tfound = true;\n+\n+\t\t\tformat->width = size.maxWidth;\n+\t\t\tformat->height = size.maxHeight;\n+\t\t\tformat->mbus_code = it->first;\n+\t\t}\n+\n+\t\t++it;\n+\t}\n+\tif (!found)\n+\t\treturn -EINVAL;\n+\n+\tret = sensor->setFormat(0, format);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\t/*\n+\t * Make sure everything is all right and the format did not get\n+\t * adjusted.\n+\t */\n+\tif (format->width < config.width ||\n+\t    format->height < config.height)\n+\t\treturn -EINVAL;\n+\n+\treturn 0;\n+}\n+\n /* Link entities in the ImgU unit to prepare for capture operations. */\n int PipelineHandlerIPU3::linkImgu(ImguDevice *imguDevice)\n {\n",
    "prefixes": [
        "libcamera-devel",
        "04/10"
    ]
}