{"id":16683,"url":"https://patchwork.libcamera.org/api/patches/16683/?format=json","web_url":"https://patchwork.libcamera.org/patch/16683/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20220719091745.8040-1-ecurtin@redhat.com>","date":"2022-07-19T09:17:46","name":"[libcamera-devel,v4] cam: sdl_sink: Use libjpeg over SDL2_image","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"e79e8ddf2875269865d6717a93ed39c81b8f5109","submitter":{"id":101,"url":"https://patchwork.libcamera.org/api/people/101/?format=json","name":"Eric Curtin","email":"ecurtin@redhat.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/16683/mbox/","series":[{"id":3298,"url":"https://patchwork.libcamera.org/api/series/3298/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3298","date":"2022-07-19T09:17:46","name":"[libcamera-devel,v4] cam: sdl_sink: Use libjpeg over SDL2_image","version":4,"mbox":"https://patchwork.libcamera.org/series/3298/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/16683/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/16683/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 104F1BD1F1\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 19 Jul 2022 09:21:27 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 73A2663313;\n\tTue, 19 Jul 2022 11:21:26 +0200 (CEST)","from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.133.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 4674F6330E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 19 Jul 2022 11:21:24 +0200 (CEST)","from mail-wm1-f72.google.com (mail-wm1-f72.google.com\n\t[209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS\n\t(version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id\n\tus-mta-442-xmBxOoaHP_6m8oAIvBM-6A-1; Tue, 19 Jul 2022 05:21:16 -0400","by mail-wm1-f72.google.com with SMTP id\n\tbh18-20020a05600c3d1200b003a32044cc9fso1542079wmb.6\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 19 Jul 2022 02:21:15 -0700 (PDT)","from p1.Home ([2001:8a0:6724:4500:a69c:e66f:828e:b340])\n\tby smtp.gmail.com with ESMTPSA id\n\tz5-20020a5d6405000000b0021b966abc19sm12707826wru.19.2022.07.19.02.21.13\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tTue, 19 Jul 2022 02:21:14 -0700 (PDT)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1658222486;\n\tbh=RT8gB2hjeVHoCQWtYINvKoo4DrCoLSJwJPiyifZet08=;\n\th=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post:\n\tList-Help:List-Subscribe:From:Reply-To:Cc:From;\n\tb=LWJsueL5C/bjAWlnZ2xnGehNuPQKmGHjkEv9GbwAkUsLakj1ZNwnpM5NJW1R/HeRi\n\tHuHOwD5HSDylIsJQpJUu/B3UV76tXUKQWhLUsoJv8r9z8wzVLjRGUcIj1uJYO2dxg+\n\tlbenlp6Z+3BGDqVFBWvdDAv7sX60vLE+QW/1hNvuLQt1rN1ZSr5ZOCs9l/dhhnDr0r\n\t0tTyjg/W2vYKGkoGuGq7lOX9IAA5S1Te8/SCh0vuP396s+B18lSUApu4iObf7guHVE\n\tE27xzQGr9VBxi0f5xbyXX7LCeSHyMgrfDIeecHGuRYyJyrsKTSpD2bHq7eeBfdw3QG\n\tW7i6Snp8nrpSQ==","v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1658222483;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:cc:mime-version:mime-version:content-type:content-type:\n\tcontent-transfer-encoding:content-transfer-encoding;\n\tbh=2GYsqtW90C1tt3DTmE02NgzuUOLS+3qCtRBtteiIAKk=;\n\tb=ZJaGX/wZ+Fgh6VVMeYwgN/7YWSruRwZD0iQRN+lfF6s7Fxvi0iYasLTVPj8wqCcE4AD1hV\n\tnZKgqhrF86J+kcQ7ue/VEZ7JFcEjh+br2jOIMo7hvrmZRIK8r11Hi4SvmRLyy037mSolBL\n\tEvvj4AHph88xvlFR0MGX+ZPjpiP8JSA="],"Authentication-Results":["lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=redhat.com\n\theader.i=@redhat.com header.b=\"ZJaGX/wZ\"; \n\tdkim-atps=neutral","relay.mimecast.com;\n\tauth=pass smtp.auth=CUSA124A263 smtp.mailfrom=ecurtin@redhat.com"],"X-MC-Unique":"xmBxOoaHP_6m8oAIvBM-6A-1","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version\n\t:content-transfer-encoding;\n\tbh=2GYsqtW90C1tt3DTmE02NgzuUOLS+3qCtRBtteiIAKk=;\n\tb=5V4iMdidgC7Xf7UuX2EG+MgXIFaxAHr5xsSUCXAha0wxlWYU41wlJHU0wRfoMYfYyw\n\tVRCC9MsW5cnVDA3k/asMr1uISH/rUsnioth1uPXSfJt/Gcc4uWX0HTnfp398gohMSewb\n\tJjw0Msm8bMR0ZVflPiXF/SPLIRsnUIlko0S0sba9FeKmfhMBqcf7KqnlC+mZHCmJZ0QC\n\tbQMgCkcn9a6+/COYanCuyTonFkZDFO6a/4p0LeClP9piNXKnFFPjoYTwvKV2YnuFGiX5\n\t9Xgrxc18UChmVcP/+GPllKp1bAu4PAa1ghvLIark621XiHr+tuSzDRpuxB91d1IKxey6\n\t0h+Q==","X-Gm-Message-State":"AJIora+cGblJ3QxKBx1H7oYE8X+USU09asKYRepj3WxoreZYUztU596m\n\tl98B2vnZ9zDSTWSnu+46CfFJm2Er2Rn3MgFf6FgEJFw/7EdfmHX15XJMfebWcC4VgUKb9YChOFX\n\tSYYFFzHA9C+JNypnJiNrGnEAnX0zwAYRSbP2zc7WKo3QGFEvIetF9Oni8tCQD8ho03BvWpevD7g\n\tonsV7C1n6y","X-Received":["by 2002:a5d:4e04:0:b0:21d:6ec3:38a2 with SMTP id\n\tp4-20020a5d4e04000000b0021d6ec338a2mr25944909wrt.362.1658222474821; \n\tTue, 19 Jul 2022 02:21:14 -0700 (PDT)","by 2002:a5d:4e04:0:b0:21d:6ec3:38a2 with SMTP id\n\tp4-20020a5d4e04000000b0021d6ec338a2mr25944881wrt.362.1658222474462; \n\tTue, 19 Jul 2022 02:21:14 -0700 (PDT)"],"X-Google-Smtp-Source":"AGRyM1sI7crGWJrvNsuZO4Dg1y0qr7d3J54L7Xn0pofLE0Va5qgOXm64cPPUggF/V11oNpT+Hik8Mw==","To":"libcamera-devel@lists.libcamera.org","Date":"Tue, 19 Jul 2022 10:17:46 +0100","Message-Id":"<20220719091745.8040-1-ecurtin@redhat.com>","X-Mailer":"git-send-email 2.35.3","MIME-Version":"1.0","X-Mimecast-Spam-Score":"0","X-Mimecast-Originator":"redhat.com","Content-Transfer-Encoding":"8bit","Content-Type":"text/plain; charset=\"US-ASCII\"; x-default=true","Subject":"[libcamera-devel] [PATCH v4] cam: sdl_sink: Use libjpeg over\n\tSDL2_image","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>","From":"Eric Curtin via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Eric Curtin <ecurtin@redhat.com>","Cc":"Ian Mullins <imullins@redhat.com>, jozef@mlich.cz","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"We were using the libjpeg functionality of SDL2_image only, instead just\nuse libjpeg directly to reduce our dependancy count, it is a more\ncommonly available library.\n\nSigned-off-by: Eric Curtin <ecurtin@redhat.com>\n---\nChanges in v4:\n- decompress function takes a Span<const uint8_t>&\n\nChanges in v3:\n- create JpegErrorManager struct\n- change c cast to reinterpret_cast\n\nChanges in v2:\n- alphabetically sorted various orderings\n- pitch_ is const again\n- added setjmp logic for error handling in libjpeg\n- rgbdata_ to unique_ptr and renamed to rgb_\n- removed a copy from buffer to rgb_\n- removed a destructor\n---\n README.rst                     |  2 +-\n src/cam/jpeg_error_manager.cpp | 26 ++++++++++++++++++++\n src/cam/jpeg_error_manager.h   | 21 ++++++++++++++++\n src/cam/meson.build            |  9 +++----\n src/cam/sdl_sink.cpp           |  4 ++--\n src/cam/sdl_texture_mjpg.cpp   | 44 +++++++++++++++++++++++++++++-----\n src/cam/sdl_texture_mjpg.h     |  6 +++++\n 7 files changed, 99 insertions(+), 13 deletions(-)\n create mode 100644 src/cam/jpeg_error_manager.cpp\n create mode 100644 src/cam/jpeg_error_manager.h","diff":"diff --git a/README.rst b/README.rst\nindex b9e72d81..47b914f0 100644\n--- a/README.rst\n+++ b/README.rst\n@@ -92,8 +92,8 @@ for cam: [optional]\n         tool:\n \n         - libdrm-dev: Enables the KMS sink\n+        - libjpeg-dev: Enables MJPEG on the SDL sink\n         - libsdl2-dev: Enables the SDL sink\n-        - libsdl2-image-dev: Supports MJPEG on the SDL sink\n \n for qcam: [optional]\n         qtbase5-dev libqt5core5a libqt5gui5 libqt5widgets5 qttools5-dev-tools libtiff-dev\ndiff --git a/src/cam/jpeg_error_manager.cpp b/src/cam/jpeg_error_manager.cpp\nnew file mode 100644\nindex 00000000..7e45e577\n--- /dev/null\n+++ b/src/cam/jpeg_error_manager.cpp\n@@ -0,0 +1,26 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * Copyright (C) 2022, Ideas on Board Oy\n+ *\n+ * jpeg_error_manager.cpp - JPEG Error Manager\n+ */\n+\n+#include \"jpeg_error_manager.h\"\n+\n+static void errorExit(j_common_ptr cinfo)\n+{\n+\tJpegErrorManager *self =\n+\t\treinterpret_cast<JpegErrorManager *>(cinfo->err);\n+\tlongjmp(self->escape_, 1);\n+}\n+\n+static void outputMessage([[maybe_unused]] j_common_ptr cinfo)\n+{\n+}\n+\n+JpegErrorManager::JpegErrorManager(struct jpeg_decompress_struct &cinfo)\n+{\n+\tcinfo.err = jpeg_std_error(&errmgr_);\n+\terrmgr_.error_exit = errorExit;\n+\terrmgr_.output_message = outputMessage;\n+}\ndiff --git a/src/cam/jpeg_error_manager.h b/src/cam/jpeg_error_manager.h\nnew file mode 100644\nindex 00000000..0af28da1\n--- /dev/null\n+++ b/src/cam/jpeg_error_manager.h\n@@ -0,0 +1,21 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * Copyright (C) 2022, Ideas on Board Oy\n+ *\n+ * jpeg_error_manager.h - JPEG Error Manager\n+ */\n+\n+#pragma once\n+\n+#include <setjmp.h>\n+#include <stdio.h>\n+\n+#include <jpeglib.h>\n+\n+struct JpegErrorManager {\n+\tJpegErrorManager(struct jpeg_decompress_struct &cinfo);\n+\n+\t/* Order very important for reinterpret_cast */\n+\tstruct jpeg_error_mgr errmgr_;\n+\tjmp_buf escape_;\n+};\ndiff --git a/src/cam/meson.build b/src/cam/meson.build\nindex 5957ce14..86cef683 100644\n--- a/src/cam/meson.build\n+++ b/src/cam/meson.build\n@@ -24,8 +24,8 @@ cam_sources = files([\n cam_cpp_args = []\n \n libdrm = dependency('libdrm', required : false)\n+libjpeg = dependency('libjpeg', required : false)\n libsdl2 = dependency('SDL2', required : false)\n-libsdl2_image = dependency('SDL2_image', required : false)\n \n if libdrm.found()\n     cam_cpp_args += [ '-DHAVE_KMS' ]\n@@ -43,9 +43,10 @@ if libsdl2.found()\n         'sdl_texture_yuyv.cpp'\n     ])\n \n-    if libsdl2_image.found()\n-        cam_cpp_args += ['-DHAVE_SDL_IMAGE']\n+    if libjpeg.found()\n+        cam_cpp_args += ['-DHAVE_LIBJPEG']\n         cam_sources += files([\n+            'jpeg_error_manager.cpp',\n             'sdl_texture_mjpg.cpp'\n         ])\n     endif\n@@ -57,8 +58,8 @@ cam  = executable('cam', cam_sources,\n                       libcamera_public,\n                       libdrm,\n                       libevent,\n+                      libjpeg,\n                       libsdl2,\n-                      libsdl2_image,\n                       libyaml,\n                   ],\n                   cpp_args : cam_cpp_args,\ndiff --git a/src/cam/sdl_sink.cpp b/src/cam/sdl_sink.cpp\nindex f8e3e95d..19fdfd6d 100644\n--- a/src/cam/sdl_sink.cpp\n+++ b/src/cam/sdl_sink.cpp\n@@ -21,7 +21,7 @@\n \n #include \"event_loop.h\"\n #include \"image.h\"\n-#ifdef HAVE_SDL_IMAGE\n+#ifdef HAVE_LIBJPEG\n #include \"sdl_texture_mjpg.h\"\n #endif\n #include \"sdl_texture_yuyv.h\"\n@@ -62,7 +62,7 @@ int SDLSink::configure(const libcamera::CameraConfiguration &config)\n \trect_.h = cfg.size.height;\n \n \tswitch (cfg.pixelFormat) {\n-#ifdef HAVE_SDL_IMAGE\n+#ifdef HAVE_LIBJPEG\n \tcase libcamera::formats::MJPEG:\n \t\ttexture_ = std::make_unique<SDLTextureMJPG>(rect_);\n \t\tbreak;\ndiff --git a/src/cam/sdl_texture_mjpg.cpp b/src/cam/sdl_texture_mjpg.cpp\nindex 69e99ad3..22f570c6 100644\n--- a/src/cam/sdl_texture_mjpg.cpp\n+++ b/src/cam/sdl_texture_mjpg.cpp\n@@ -7,19 +7,51 @@\n \n #include \"sdl_texture_mjpg.h\"\n \n-#include <SDL2/SDL_image.h>\n+#include \"jpeg_error_manager.h\"\n+\n+#include <iostream>\n \n using namespace libcamera;\n \n SDLTextureMJPG::SDLTextureMJPG(const SDL_Rect &rect)\n-\t: SDLTexture(rect, SDL_PIXELFORMAT_RGB24, 0)\n+\t: SDLTexture(rect, SDL_PIXELFORMAT_RGB24, rect.w * 3),\n+\t  rgb_(std::make_unique<unsigned char[]>(pitch_ * rect.h))\n+{\n+}\n+\n+int SDLTextureMJPG::decompress(const Span<uint8_t> &data)\n {\n+\tstruct jpeg_decompress_struct cinfo;\n+\tJpegErrorManager jpegErrorManager(cinfo);\n+\tif (setjmp(jpegErrorManager.escape_)) {\n+\t\t/* libjpeg found an error */\n+\t\tjpeg_destroy_decompress(&cinfo);\n+\t\tstd::cerr << \"JPEG decompression error\" << std::endl;\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tjpeg_create_decompress(&cinfo);\n+\n+\tjpeg_mem_src(&cinfo, data.data(), data.size());\n+\n+\tjpeg_read_header(&cinfo, TRUE);\n+\n+\tjpeg_start_decompress(&cinfo);\n+\n+\tfor (int i = 0; cinfo.output_scanline < cinfo.output_height; ++i) {\n+\t\tJSAMPROW rowptr = rgb_.get() + i * pitch_;\n+\t\tjpeg_read_scanlines(&cinfo, &rowptr, 1);\n+\t}\n+\n+\tjpeg_finish_decompress(&cinfo);\n+\n+\tjpeg_destroy_decompress(&cinfo);\n+\n+\treturn 0;\n }\n \n void SDLTextureMJPG::update(const Span<uint8_t> &data)\n {\n-\tSDL_RWops *bufferStream = SDL_RWFromMem(data.data(), data.size());\n-\tSDL_Surface *frame = IMG_Load_RW(bufferStream, 0);\n-\tSDL_UpdateTexture(ptr_, nullptr, frame->pixels, frame->pitch);\n-\tSDL_FreeSurface(frame);\n+\tdecompress(data);\n+\tSDL_UpdateTexture(ptr_, nullptr, rgb_.get(), pitch_);\n }\ndiff --git a/src/cam/sdl_texture_mjpg.h b/src/cam/sdl_texture_mjpg.h\nindex b103f801..328c45a9 100644\n--- a/src/cam/sdl_texture_mjpg.h\n+++ b/src/cam/sdl_texture_mjpg.h\n@@ -13,5 +13,11 @@ class SDLTextureMJPG : public SDLTexture\n {\n public:\n \tSDLTextureMJPG(const SDL_Rect &rect);\n+\n \tvoid update(const libcamera::Span<uint8_t> &data) override;\n+\n+private:\n+\tint decompress(const libcamera::Span<uint8_t> &data);\n+\n+\tstd::unique_ptr<unsigned char[]> rgb_;\n };\n","prefixes":["libcamera-devel","v4"]}