From patchwork Fri Jul 15 11:36:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Curtin X-Patchwork-Id: 16637 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 8114FBE173 for ; Fri, 15 Jul 2022 11:42:48 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 486D163312; Fri, 15 Jul 2022 13:42:48 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1657885368; bh=nbXzclAiXC2DFTKOjABoK5xci9vnQrLGw2hzLvQFs0k=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=BJpKuUHmMyVP6zDlcS+xuXLp1RsojSQjuCFMfkPotieYGpTrAeztMzUZkcD6itOQb B5sfzAVdxmLq5oLA0bkiA63RJS1twdB6HTU7N1FNHhL+H6+eKn9Cs/pwZ6fVLHg1Nv 1/MG/nocCXMv0J8EUNDh+X//L8iG+Z4GXmIjjeRlcG42l0GbGZbjMXkKn6zi1Tab6b 6BaLj3PiT3Hpl4Sd1/9JHm+PJRhGviyzN2C96PQkAppnVgLr5NCXq2yJVeaYxdF7EU c9pyQpUcT3g7f+t7m1jj+YsYhR+ytaGPq1kY+BwX3KrVC42m5fm1/G5YtWd2aAGJ4C fulNTgGSU+jxg== Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 536456330A for ; Fri, 15 Jul 2022 13:42:47 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="FugJInLW"; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1657885366; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LEZeDKetc+/u9TutSq6Yk2Y3UBYnvu1ZHom0mHkNUa0=; b=FugJInLWXJA3QFTRMPKE059quIfRe/oTMyihFzeheulIM/waDs71yRWezStL+R2nkEyKPt 4p/7teFv4ldWt8UQzhpqkOey+GbPhgSDy9ZohLJiojYZwdhTvOJsB2h5SERmfjiYHc3o1F zOi+d9+RTfN1bQG/YJGO3eYlrIoTgt0= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-465-kN2-VBg1NLK2yR9ypUappg-1; Fri, 15 Jul 2022 07:42:45 -0400 X-MC-Unique: kN2-VBg1NLK2yR9ypUappg-1 Received: by mail-wm1-f72.google.com with SMTP id v18-20020a05600c215200b003a2fea66b7cso1781630wml.4 for ; Fri, 15 Jul 2022 04:42:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=LEZeDKetc+/u9TutSq6Yk2Y3UBYnvu1ZHom0mHkNUa0=; b=Za7ufcqDVIKBkGNFrt0gYdb7KyeeY2QSPcuZDXYsunv0EdwMkWuVapEaGv/TO3s/Kw JSz+SI2MXElKRBiPlFfvhhTo7TNuICNYXfQ1vp1g5Z61euVxXRNXRtr2bmceyahBeDHv 2EaHXwyZrNXcpJlFRlaT+L3c2ajts9OvvwrI3Xa/tBk/ecniyKU5j0DkHBahNKovV1N5 xZqk3eK5JgcYWyhKgz6zZb4S6BDoW8sqf4kfDHsZHd45RhSmmR3CMWkJ1HFuL3kRc334 0f5CuBrouV9ZoyAFgz7U+OrHzlPLPRb1lP/u13lQgIdCHVLtYpeUElWgI7IGFbIYiQp1 rA+w== X-Gm-Message-State: AJIora+2mL/rzTgj9ykoFFVuwfnMM0cKqu+tJl+Z7vDCFeDnPPZD/fnF lmnUmabysZ5JisxRvcJhQAw38mH58K6IwUsoUtP6ggtkuBhK/IeXwYHvExmuysWTASVEVhCrIQm 0qzE96USsA5w8evNIXe0fOnWlfwaxQhthl+5RyhlTO0/ebCesxwfcjp43Ht+YXO2oFND1TcMH9M cj32FT0d4D X-Received: by 2002:a5d:55d0:0:b0:21d:cf5a:7617 with SMTP id i16-20020a5d55d0000000b0021dcf5a7617mr2406973wrw.368.1657885363748; Fri, 15 Jul 2022 04:42:43 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tf9v7Aq/kbYjjC3g6dV50iq8tWv+WzwBTjXAg84BDYvhbwlE9hBpk+Z9AfRFPvvP3vM3PVEg== X-Received: by 2002:a5d:55d0:0:b0:21d:cf5a:7617 with SMTP id i16-20020a5d55d0000000b0021dcf5a7617mr2406959wrw.368.1657885363517; Fri, 15 Jul 2022 04:42:43 -0700 (PDT) Received: from localhost.localdomain ([2001:8a0:6ed9:ab01:85de:1e25:70ef:ff9e]) by smtp.gmail.com with ESMTPSA id m65-20020a1ca344000000b0039c5a765388sm4923933wme.28.2022.07.15.04.42.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Jul 2022 04:42:42 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Fri, 15 Jul 2022 12:36:05 +0100 Message-Id: <20220715113604.10131-1-ecurtin@redhat.com> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=ecurtin@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [libcamera-devel] [PATCH v2] cam: sdl_sink: Use libjpeg over SDL2_image 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: Eric Curtin via libcamera-devel From: Eric Curtin Reply-To: Eric Curtin Cc: Ian Mullins , jozef@mlich.cz Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" We were using the libjpeg functionality of SDL2_image only, instead just use libjpeg directly to reduce our dependancy count, it is a more commonly available library. Signed-off-by: Eric Curtin --- Changes in v2: - alphabetically sorted various orderings - pitch_ is const again - added setjmp logic for error handling in libjpeg - rgbdata_ to unique_ptr and renamed to rgb_ - removed a copy from buffer to rgb_ - removed a destructor --- README.rst | 2 +- src/cam/meson.build | 8 ++--- src/cam/sdl_sink.cpp | 4 +-- src/cam/sdl_texture_mjpg.cpp | 63 ++++++++++++++++++++++++++++++++---- src/cam/sdl_texture_mjpg.h | 6 ++++ 5 files changed, 70 insertions(+), 13 deletions(-) diff --git a/README.rst b/README.rst index b9e72d81..47b914f0 100644 --- a/README.rst +++ b/README.rst @@ -92,8 +92,8 @@ for cam: [optional] tool: - libdrm-dev: Enables the KMS sink + - libjpeg-dev: Enables MJPEG on the SDL sink - libsdl2-dev: Enables the SDL sink - - libsdl2-image-dev: Supports MJPEG on the SDL sink for qcam: [optional] qtbase5-dev libqt5core5a libqt5gui5 libqt5widgets5 qttools5-dev-tools libtiff-dev diff --git a/src/cam/meson.build b/src/cam/meson.build index 5957ce14..4dfa7b22 100644 --- a/src/cam/meson.build +++ b/src/cam/meson.build @@ -24,8 +24,8 @@ cam_sources = files([ cam_cpp_args = [] libdrm = dependency('libdrm', required : false) +libjpeg = dependency('libjpeg', required : false) libsdl2 = dependency('SDL2', required : false) -libsdl2_image = dependency('SDL2_image', required : false) if libdrm.found() cam_cpp_args += [ '-DHAVE_KMS' ] @@ -43,8 +43,8 @@ if libsdl2.found() 'sdl_texture_yuyv.cpp' ]) - if libsdl2_image.found() - cam_cpp_args += ['-DHAVE_SDL_IMAGE'] + if libjpeg.found() + cam_cpp_args += ['-DHAVE_LIBJPEG'] cam_sources += files([ 'sdl_texture_mjpg.cpp' ]) @@ -57,8 +57,8 @@ cam = executable('cam', cam_sources, libcamera_public, libdrm, libevent, + libjpeg, libsdl2, - libsdl2_image, libyaml, ], cpp_args : cam_cpp_args, diff --git a/src/cam/sdl_sink.cpp b/src/cam/sdl_sink.cpp index f8e3e95d..19fdfd6d 100644 --- a/src/cam/sdl_sink.cpp +++ b/src/cam/sdl_sink.cpp @@ -21,7 +21,7 @@ #include "event_loop.h" #include "image.h" -#ifdef HAVE_SDL_IMAGE +#ifdef HAVE_LIBJPEG #include "sdl_texture_mjpg.h" #endif #include "sdl_texture_yuyv.h" @@ -62,7 +62,7 @@ int SDLSink::configure(const libcamera::CameraConfiguration &config) rect_.h = cfg.size.height; switch (cfg.pixelFormat) { -#ifdef HAVE_SDL_IMAGE +#ifdef HAVE_LIBJPEG case libcamera::formats::MJPEG: texture_ = std::make_unique(rect_); break; diff --git a/src/cam/sdl_texture_mjpg.cpp b/src/cam/sdl_texture_mjpg.cpp index 69e99ad3..a064e6e5 100644 --- a/src/cam/sdl_texture_mjpg.cpp +++ b/src/cam/sdl_texture_mjpg.cpp @@ -7,19 +7,70 @@ #include "sdl_texture_mjpg.h" -#include +#include +#include + +#include using namespace libcamera; SDLTextureMJPG::SDLTextureMJPG(const SDL_Rect &rect) - : SDLTexture(rect, SDL_PIXELFORMAT_RGB24, 0) + : SDLTexture(rect, SDL_PIXELFORMAT_RGB24, rect.w * 3), + rgb_(std::make_unique(pitch_ * rect.h)) +{ +} + +struct error_mgr { + struct jpeg_error_mgr errmgr; + jmp_buf escape; +}; + +static void error_exit(j_common_ptr cinfo) +{ + struct error_mgr *err = (struct error_mgr *)cinfo->err; + longjmp(err->escape, 1); +} + +static void output_no_message(j_common_ptr cinfo) +{ + (void)cinfo; +} + +void SDLTextureMJPG::decompress(const unsigned char *jpeg, + unsigned long jpeg_size) { + struct error_mgr jerr; + struct jpeg_decompress_struct cinfo; + cinfo.err = jpeg_std_error(&jerr.errmgr); + jerr.errmgr.error_exit = error_exit; + jerr.errmgr.output_message = output_no_message; + if (setjmp(jerr.escape)) { + /* libjpeg found an error */ + jpeg_destroy_decompress(&cinfo); + std::cerr << "JPEG decompression error" << std::endl; + return; + } + + jpeg_create_decompress(&cinfo); + + jpeg_mem_src(&cinfo, jpeg, jpeg_size); + + jpeg_read_header(&cinfo, TRUE); + + jpeg_start_decompress(&cinfo); + + for (int i = 0; cinfo.output_scanline < cinfo.output_height; ++i) { + JSAMPROW rowptr = rgb_.get() + i * pitch_; + jpeg_read_scanlines(&cinfo, &rowptr, 1); + } + + jpeg_finish_decompress(&cinfo); + + jpeg_destroy_decompress(&cinfo); } void SDLTextureMJPG::update(const Span &data) { - SDL_RWops *bufferStream = SDL_RWFromMem(data.data(), data.size()); - SDL_Surface *frame = IMG_Load_RW(bufferStream, 0); - SDL_UpdateTexture(ptr_, nullptr, frame->pixels, frame->pitch); - SDL_FreeSurface(frame); + decompress(data.data(), data.size()); + SDL_UpdateTexture(ptr_, nullptr, rgb_.get(), pitch_); } diff --git a/src/cam/sdl_texture_mjpg.h b/src/cam/sdl_texture_mjpg.h index b103f801..c68b03b0 100644 --- a/src/cam/sdl_texture_mjpg.h +++ b/src/cam/sdl_texture_mjpg.h @@ -13,5 +13,11 @@ class SDLTextureMJPG : public SDLTexture { public: SDLTextureMJPG(const SDL_Rect &rect); + void update(const libcamera::Span &data) override; + +private: + void decompress(const unsigned char *jpeg, unsigned long jpeg_size); + + std::unique_ptr rgb_; };