Show a patch.

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

{
    "id": 8913,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/8913/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/8913/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/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": "<20200721220126.202065-5-kieran.bingham@ideasonboard.com>",
    "date": "2020-07-21T22:01:24",
    "name": "[libcamera-devel,RFC,4/6] android: camera_device: Support MJPEG stream construction",
    "commit_ref": null,
    "pull_url": null,
    "state": "rfc",
    "archived": false,
    "hash": "f446e83f61105e7865bbf152ce3d1c2ffec48ee8",
    "submitter": {
        "id": 4,
        "url": "https://patchwork.libcamera.org/api/1.1/people/4/?format=api",
        "name": "Kieran Bingham",
        "email": "kieran.bingham@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/8913/mbox/",
    "series": [
        {
            "id": 1125,
            "url": "https://patchwork.libcamera.org/api/1.1/series/1125/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=1125",
            "date": "2020-07-21T22:01:20",
            "name": "android: jpeg / software streams",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/1125/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/8913/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/8913/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<libcamera-devel-bounces@lists.libcamera.org>",
        "X-Original-To": "parsemail@patchwork.libcamera.org",
        "Delivered-To": "parsemail@patchwork.libcamera.org",
        "Received": [
            "from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 4CB11C2E68\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 21 Jul 2020 22:01:38 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 06613609A9;\n\tWed, 22 Jul 2020 00:01:38 +0200 (CEST)",
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id EB4DB60943\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 22 Jul 2020 00:01:32 +0200 (CEST)",
            "from localhost.localdomain\n\t(cpc89242-aztw30-2-0-cust488.18-1.cable.virginm.net [86.31.129.233])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 2049D51A;\n\tWed, 22 Jul 2020 00:01:32 +0200 (CEST)"
        ],
        "Authentication-Results": "lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"WWbxNmv2\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1595368892;\n\tbh=8FCCc/j7kEX8ddGZY6kMswMJEatvfKkxggXUodGUX+o=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=WWbxNmv223Tx06jMQsqLvhbhXbRIvzRGYwtSvZpLT8eFTPIhkvDxw1Iw6qac9toaS\n\tnRJ9tzt0siSW9QP2aatxxYYae/mAwhtdCYk7N9JkjVLBJDef8Mo6jxJDw8JJ0YD9Up\n\tattsMZyLPRwvnKktVwxGASO1Rt0AXAxV66lE5FRE=",
        "From": "Kieran Bingham <kieran.bingham@ideasonboard.com>",
        "To": "libcamera devel <libcamera-devel@lists.libcamera.org>",
        "Date": "Tue, 21 Jul 2020 23:01:24 +0100",
        "Message-Id": "<20200721220126.202065-5-kieran.bingham@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.25.1",
        "In-Reply-To": "<20200721220126.202065-1-kieran.bingham@ideasonboard.com>",
        "References": "<20200721220126.202065-1-kieran.bingham@ideasonboard.com>",
        "MIME-Version": "1.0",
        "Subject": "[libcamera-devel] [RFC PATCH 4/6] android: camera_device: Support\n\tMJPEG stream construction",
        "X-BeenThere": "libcamera-devel@lists.libcamera.org",
        "X-Mailman-Version": "2.1.29",
        "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>",
        "Content-Type": "text/plain; charset=\"us-ascii\"",
        "Content-Transfer-Encoding": "7bit",
        "Errors-To": "libcamera-devel-bounces@lists.libcamera.org",
        "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"
    },
    "content": "MJPEG streams must be created referencing a libcamera stream.\nThis stream may already be provided by the request configuration,\nin which case the existing stream is utilised.\n\nIf no compatible stream is available to encode, a new stream is requested\nfrom the libcamera configuration.\n\nSigned-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n---\n src/android/camera_device.cpp | 158 +++++++++++++++++++++++++++++++++-\n src/android/camera_device.h   |   8 ++\n 2 files changed, 162 insertions(+), 4 deletions(-)",
    "diff": "diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp\nindex 56652ac57676..7323d4e58f68 100644\n--- a/src/android/camera_device.cpp\n+++ b/src/android/camera_device.cpp\n@@ -8,6 +8,7 @@\n #include \"camera_device.h\"\n #include \"camera_ops.h\"\n \n+#include <sys/mman.h>\n #include <tuple>\n #include <vector>\n \n@@ -21,6 +22,8 @@\n #include \"camera_metadata.h\"\n #include \"system/graphics.h\"\n \n+#include \"jpeg/compressor_jpeg.h\"\n+\n using namespace libcamera;\n \n namespace {\n@@ -1004,6 +1007,7 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list)\n \t */\n \tunsigned int streamIndex = 0;\n \n+\t/* First handle all non-MJPEG streams */\n \tfor (unsigned int i = 0; i < stream_list->num_streams; ++i) {\n \t\tcamera3_stream_t *stream = stream_list->streams[i];\n \n@@ -1019,6 +1023,18 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list)\n \t\tif (!format.isValid())\n \t\t\treturn -EINVAL;\n \n+\t\tstream->priv = static_cast<void *>(&streams_[i]);\n+\t\tstreams_[i].format = format;\n+\t\tstreams_[i].size = Size(stream->width, stream->height);\n+\n+\t\t/* Defer handling of MJPEG streams until all others are known. */\n+\t\tif (format == formats::MJPEG) {\n+\t\t\tLOG(HAL, Info) << \"Handling MJPEG stream\";\n+\n+\t\t\tstreams_[i].index = -1;\n+\t\t\tcontinue;\n+\t\t}\n+\n \t\tStreamConfiguration streamConfiguration;\n \n \t\tstreamConfiguration.size.width = stream->width;\n@@ -1028,7 +1044,61 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list)\n \t\tconfig_->addConfiguration(streamConfiguration);\n \n \t\tstreams_[i].index = streamIndex++;\n-\t\tstream->priv = static_cast<void *>(&streams_[i]);\n+\t}\n+\n+\t/* Now handle MJPEG streams, adding a new stream if required. */\n+\tfor (unsigned int i = 0; i < stream_list->num_streams; ++i) {\n+\t\tcamera3_stream_t *stream = stream_list->streams[i];\n+\t\tCameraStream *cameraStream = &streams_[i];\n+\n+\t\t/* Only process MJPEG streams */\n+\t\tif (cameraStream->format != formats::MJPEG)\n+\t\t\tcontinue;\n+\n+\t\tbool match = false;\n+\n+\t\t/* Search for a compatible stream */\n+\t\tfor (unsigned int j = 0; j < config_->size(); j++) {\n+\t\t\tStreamConfiguration &cfg = config_->at(j);\n+\n+\t\t\t/*\n+\t\t\t * The PixelFormat must also be compatible with the\n+\t\t\t * encoder.\n+\t\t\t */\n+\t\t\tif (cfg.size == cameraStream->size) {\n+\t\t\t\t    LOG(HAL, Info)\n+\t\t\t\t\t<< \"Stream \" << i\n+\t\t\t\t\t<< \" using libcamera stream \"\n+\t\t\t\t\t<< j;\n+\n+\t\t\t\t    match = true;\n+\t\t\t\t    cameraStream->index = j;\n+\t\t\t}\n+\t\t}\n+\n+\t\t/*\n+\t\t * Without a compatible match for JPEG encoding we must\n+\t\t * introduce a new stream to satisfy the request requirements.\n+\t\t */\n+\t\tif (!match) {\n+\t\t\tStreamConfiguration streamConfiguration;\n+\n+\t\t\t/*\n+\t\t\t * \\todo: The pixelFormat should be a 'best-fit' choice\n+\t\t\t * and may require a validation cycle. This is not yet\n+\t\t\t * handled, and should be considered as part of any\n+\t\t\t * stream configuration reworks.\n+\t\t\t */\n+\t\t\tstreamConfiguration.size.width = stream->width;\n+\t\t\tstreamConfiguration.size.height = stream->height;\n+\t\t\tstreamConfiguration.pixelFormat = formats::NV12;\n+\n+\t\t\tLOG(HAL, Info) << \"Adding \" << streamConfiguration.toString()\n+\t\t\t\t       << \" for MJPEG support\";\n+\n+\t\t\tconfig_->addConfiguration(streamConfiguration);\n+\t\t\tstreams_[i].index = streamIndex++;\n+\t\t}\n \t}\n \n \tswitch (config_->validate()) {\n@@ -1059,6 +1129,18 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list)\n \n \t\t/* Use the bufferCount confirmed by the validation process. */\n \t\tstream->max_buffers = cfg.bufferCount;\n+\n+\t\t/*\n+\t\t * Construct a software compressor for MJPEG streams from the\n+\t\t * chosen libcamera source stream.\n+\t\t */\n+\t\tif (cameraStream->format == formats::MJPEG) {\n+\t\t\tcameraStream->jpeg = new CompressorJPEG();\n+\t\t\tcameraStream->jpeg->configure(cfg);\n+\t\t} else {\n+\t\t\t/* Either construct this correctly, or use a better interface */\n+\t\t\tcameraStream->jpeg = nullptr;\n+\t\t}\n \t}\n \n \t/*\n@@ -1112,6 +1194,9 @@ FrameBuffer *CameraDevice::createFrameBuffer(const buffer_handle_t camera3buffer\n \n int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Request)\n {\n+\tLOG(HAL, Error) << \"Received request \" << camera3Request->frame_number\n+\t\t\t<< \" with \" << camera3Request->num_output_buffers << \" buffers\";\n+\n \tif (!camera3Request->num_output_buffers) {\n \t\tLOG(HAL, Error) << \"No output buffers provided\";\n \t\treturn -EINVAL;\n@@ -1158,6 +1243,10 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques\n \t\tdescriptor->buffers[i].stream = camera3Buffers[i].stream;\n \t\tdescriptor->buffers[i].buffer = camera3Buffers[i].buffer;\n \n+\t\t/* Software streams are handled after hardware streams complete. */\n+\t\tif (cameraStream->format == formats::MJPEG)\n+\t\t\tcontinue;\n+\n \t\t/*\n \t\t * Create a libcamera buffer using the dmabuf descriptors of\n \t\t * the camera3Buffer for each stream. The FrameBuffer is\n@@ -1190,11 +1279,40 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques\n \treturn 0;\n }\n \n+static CompressedImage mapAndroidBlobBuffer(const buffer_handle_t camera3buffer)\n+{\n+\tCompressedImage img;\n+\n+\t/* ANDROID_JPEG_MAX_SIZE */\n+\tunsigned int length = int32_t{13 << 20};\n+\n+\t/* Take only the first plane */\n+\tvoid *memory = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_SHARED,\n+\t\t\t    camera3buffer->data[0], 0);\n+\n+\timg.length = length;\n+\timg.data = static_cast<unsigned char*>(memory);\n+\n+\treturn img;\n+}\n+\n+static void unmapAndroidBlobBuffer(CompressedImage *img)\n+{\n+\tmunmap(img->data, img->length);\n+\timg->data = nullptr;\n+\timg->length = 0;\n+}\n+\n void CameraDevice::requestComplete(Request *request)\n {\n \tconst std::map<Stream *, FrameBuffer *> &buffers = request->buffers();\n \tcamera3_buffer_status status = CAMERA3_BUFFER_STATUS_OK;\n \tstd::unique_ptr<CameraMetadata> resultMetadata;\n+\tCamera3RequestDescriptor *descriptor =\n+\t\treinterpret_cast<Camera3RequestDescriptor *>(request->cookie());\n+\n+\n+\tLOG(HAL, Error) << \"Request completed:...\";\n \n \tif (request->status() != Request::RequestComplete) {\n \t\tLOG(HAL, Error) << \"Request not succesfully completed: \"\n@@ -1202,10 +1320,41 @@ void CameraDevice::requestComplete(Request *request)\n \t\tstatus = CAMERA3_BUFFER_STATUS_ERROR;\n \t}\n \n-\t/* Prepare to call back the Android camera stack. */\n-\tCamera3RequestDescriptor *descriptor =\n-\t\treinterpret_cast<Camera3RequestDescriptor *>(request->cookie());\n+\t/* Handle any JPEG compression */\n+\tfor (unsigned int i = 0; i < descriptor->numBuffers; ++i) {\n+\t\tCameraStream *cameraStream =\n+\t\t\tstatic_cast<CameraStream *>(descriptor->buffers[i].stream->priv);\n+\t\tCompressor *compressor = cameraStream->jpeg;\n \n+\t\t/* Only handle streams with compression enabled. */\n+\t\tif (!compressor)\n+\t\t\tcontinue;\n+\n+\t\t/* \\todo: Optimise this dance routine, just to get the stream/buffer ... */\n+\t\tStreamConfiguration *streamConfiguration = &config_->at(cameraStream->index);\n+\t\tStream *stream = streamConfiguration->stream();\n+\t\tFrameBuffer *buffer = request->findBuffer(stream);\n+\t\tif (!buffer) {\n+\t\t\tLOG(HAL, Error) << \"Failed to find a source stream buffer\";\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tCompressedImage output = mapAndroidBlobBuffer(*descriptor->buffers[i].buffer);\n+\t\tif (output.data == MAP_FAILED) {\n+\t\t\tLOG(HAL, Error) << \"Failed to mmap android blob buffer of length \" << output.length;\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tint ret = compressor->compress(buffer, &output);\n+\t\tif (ret) {\n+\t\t\tLOG(HAL, Error) << \"Failed to compress stream image\";\n+\t\t\tstatus = CAMERA3_BUFFER_STATUS_ERROR;\n+\t\t}\n+\n+\t\tunmapAndroidBlobBuffer(&output);\n+\t}\n+\n+\t/* Prepare to call back the Android camera stack. */\n \tcamera3_capture_result_t captureResult = {};\n \tcaptureResult.frame_number = descriptor->frameNumber;\n \tcaptureResult.num_output_buffers = descriptor->numBuffers;\n@@ -1246,6 +1395,7 @@ void CameraDevice::requestComplete(Request *request)\n \t\t\t    descriptor->buffers[0].stream);\n \t}\n \n+\n \tcallbacks_->process_capture_result(callbacks_, &captureResult);\n \n \tdelete descriptor;\ndiff --git a/src/android/camera_device.h b/src/android/camera_device.h\nindex 5b8b9c3e26e2..1973adaa2b4b 100644\n--- a/src/android/camera_device.h\n+++ b/src/android/camera_device.h\n@@ -23,6 +23,8 @@\n #include \"libcamera/internal/log.h\"\n #include \"libcamera/internal/message.h\"\n \n+#include \"jpeg/compressor_jpeg.h\"\n+\n class CameraMetadata;\n \n struct CameraStream {\n@@ -32,6 +34,12 @@ struct CameraStream {\n \t * one or more streams to the Android framework.\n \t */\n \tunsigned int index;\n+\n+\tlibcamera::PixelFormat format;\n+\tlibcamera::Size size;\n+\n+\t/* Make sure this gets destructed correctly */\n+\tCompressorJPEG *jpeg;\n };\n \n class CameraDevice : protected libcamera::Loggable\n",
    "prefixes": [
        "libcamera-devel",
        "RFC",
        "4/6"
    ]
}