[libcamera-devel,v9,4/4] cam: sdl_sink: Add MJPG support to SDL sink
diff mbox series

Message ID 20220520190106.425386-5-ecurtin@redhat.com
State Accepted
Headers show
Series
  • Add SDL Sink
Related show

Commit Message

Eric Curtin May 20, 2022, 7:01 p.m. UTC
So we have at least two supported capturing pixel formats (although
many possible output pixel formats thanks to SDL conversion). MJPG
support only built in if SDL2_image is available, provides
decompression.

Signed-off-by: Eric Curtin <ecurtin@redhat.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Tested-by: Jacopo Mondi <jacopo@jmondi.org>
---
 src/cam/meson.build          |  9 +++++++++
 src/cam/sdl_sink.cpp         |  8 ++++++++
 src/cam/sdl_texture_mjpg.cpp | 25 +++++++++++++++++++++++++
 src/cam/sdl_texture_mjpg.h   | 17 +++++++++++++++++
 4 files changed, 59 insertions(+)
 create mode 100644 src/cam/sdl_texture_mjpg.cpp
 create mode 100644 src/cam/sdl_texture_mjpg.h

Patch
diff mbox series

diff --git a/src/cam/meson.build b/src/cam/meson.build
index 7d714152..a3c776d3 100644
--- a/src/cam/meson.build
+++ b/src/cam/meson.build
@@ -33,6 +33,7 @@  if libdrm.found()
 endif
 
 libsdl2 = dependency('SDL2', required : false)
+libsdl2_image = dependency('SDL2_image', required : false)
 
 if libsdl2.found()
     cam_cpp_args += ['-DHAVE_SDL']
@@ -41,6 +42,13 @@  if libsdl2.found()
         'sdl_texture.cpp',
         'sdl_texture_yuyv.cpp'
     ])
+
+    if libsdl2_image.found()
+        cam_cpp_args += ['-DHAVE_SDL_IMAGE']
+        cam_sources += files([
+            'sdl_texture_mjpg.cpp'
+        ])
+    endif
 endif
 
 cam  = executable('cam', cam_sources,
@@ -50,6 +58,7 @@  cam  = executable('cam', cam_sources,
                       libdrm,
                       libevent,
                       libsdl2,
+                      libsdl2_image,
                   ],
                   cpp_args : cam_cpp_args,
                   install : true)
diff --git a/src/cam/sdl_sink.cpp b/src/cam/sdl_sink.cpp
index 65430efb..e0dd235a 100644
--- a/src/cam/sdl_sink.cpp
+++ b/src/cam/sdl_sink.cpp
@@ -21,6 +21,9 @@ 
 
 #include "event_loop.h"
 #include "image.h"
+#ifdef HAVE_SDL_IMAGE
+#include "sdl_texture_mjpg.h"
+#endif
 #include "sdl_texture_yuyv.h"
 
 using namespace libcamera;
@@ -59,6 +62,11 @@  int SDLSink::configure(const libcamera::CameraConfiguration &config)
 	rect_.h = cfg.size.height;
 
 	switch (cfg.pixelFormat) {
+#ifdef HAVE_SDL_IMAGE
+	case libcamera::formats::MJPEG:
+		texture_ = std::make_unique<SDLTextureMJPG>(rect_);
+		break;
+#endif
 	case libcamera::formats::YUYV:
 		texture_ = std::make_unique<SDLTextureYUYV>(rect_);
 		break;
diff --git a/src/cam/sdl_texture_mjpg.cpp b/src/cam/sdl_texture_mjpg.cpp
new file mode 100644
index 00000000..12f26c0b
--- /dev/null
+++ b/src/cam/sdl_texture_mjpg.cpp
@@ -0,0 +1,25 @@ 
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2022, Ideas on Board Oy
+ *
+ * sdl_texture_mjpg.cpp - SDL Texture MJPG
+ */
+
+#include "sdl_texture_mjpg.h"
+
+#include <SDL2/SDL_image.h>
+
+using namespace libcamera;
+
+SDLTextureMJPG::SDLTextureMJPG(const SDL_Rect &sdlRect)
+	: SDLTexture(sdlRect, SDL_PIXELFORMAT_RGB24, 0)
+{
+}
+
+void SDLTextureMJPG::update(const Span<uint8_t> &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);
+}
diff --git a/src/cam/sdl_texture_mjpg.h b/src/cam/sdl_texture_mjpg.h
new file mode 100644
index 00000000..113ffb55
--- /dev/null
+++ b/src/cam/sdl_texture_mjpg.h
@@ -0,0 +1,17 @@ 
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2022, Ideas on Board Oy
+ *
+ * sdl_texture_mjpg.h - SDL Texture MJPG
+ */
+
+#pragma once
+
+#include "sdl_texture.h"
+
+class SDLTextureMJPG : public SDLTexture
+{
+public:
+	SDLTextureMJPG(const SDL_Rect &sdlRect);
+	void update(const libcamera::Span<uint8_t> &data) override;
+};