libcamera: src/libcamera/software_isp/debayer_cpu.h Source File

libcamera v0.7.1 Supporting cameras in Linux since 2019

Loading...
Searching...
No Matches
debayer_cpu.h
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/*
3 * Copyright (C) 2023, Linaro Ltd
4 * Copyright (C) 2023-2026 Red Hat Inc.
5 *
6 * Authors:
7 * Hans de Goede <hdegoede@redhat.com>
8 *
9 * CPU based debayering header
10 */
11
12#pragma once
13
14#include <memory>
15#include <stdint.h>
16#include <vector>
17
18#include <libcamera/base/mutex.h>
19#include <libcamera/base/object.h>
20
21#include "libcamera/internal/bayer_format.h"
22#include "libcamera/internal/camera_manager.h"
23#include "libcamera/internal/software_isp/debayer_params.h"
24#include "libcamera/internal/software_isp/swstats_cpu.h"
25
26#include "debayer.h"
27
28namespace libcamera {
29
30class DebayerCpuThread;
31 class DebayerCpu : public Debayer
32{
33public:
34 DebayerCpu(std::unique_ptr<SwStatsCpu> stats, const CameraManager &cm);
35 ~DebayerCpu();
36
37 int configure(const StreamConfiguration &inputCfg,
38 const std::vector<std::reference_wrapper<const StreamConfiguration>> &outputCfgs,
39 bool ccmEnabled) override;
40 Size patternSize(PixelFormat inputFormat) override;
41 std::vector<PixelFormat> formats(PixelFormat input) override;
42 std::tuple<unsigned int, unsigned int>
43 strideAndFrameSize(const PixelFormat &outputFormat, const Size &size) override;
44 void process(uint32_t frame, FrameBuffer *input, FrameBuffer *output, const DebayerParams &params) override;
45 int start() override;
46 void stop() override;
47 SizeRange sizes(PixelFormat inputFormat, const Size &inputSize) override;
48 const SharedFD &getStatsFD() override { return stats_->getStatsFD(); }
49
50private:
51 friend class DebayerCpuThread;
52
81 using debayerFn = void (DebayerCpu::*)(uint8_t *dst, const uint8_t *src[]);
82
83 void debayer0(uint8_t *dst, const uint8_t *src[]) { (this->*debayer0_)(dst, src); }
84 void debayer1(uint8_t *dst, const uint8_t *src[]) { (this->*debayer1_)(dst, src); }
85 void debayer2(uint8_t *dst, const uint8_t *src[]) { (this->*debayer2_)(dst, src); }
86 void debayer3(uint8_t *dst, const uint8_t *src[]) { (this->*debayer3_)(dst, src); }
87
88 /* 8-bit raw bayer format */
89 template<bool addAlphaByte, bool ccmEnabled>
90 void debayer8_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
91 template<bool addAlphaByte, bool ccmEnabled>
92 void debayer8_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
93 /* unpacked 10-bit raw bayer format */
94 template<bool addAlphaByte, bool ccmEnabled>
95 void debayer10_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
96 template<bool addAlphaByte, bool ccmEnabled>
97 void debayer10_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
98 /* unpacked 12-bit raw bayer format */
99 template<bool addAlphaByte, bool ccmEnabled>
100 void debayer12_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
101 template<bool addAlphaByte, bool ccmEnabled>
102 void debayer12_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
103 /* CSI-2 packed 10-bit raw bayer format (all the 4 orders) */
104 template<bool addAlphaByte, bool ccmEnabled>
105 void debayer10P_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
106 template<bool addAlphaByte, bool ccmEnabled>
107 void debayer10P_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
108 template<bool addAlphaByte, bool ccmEnabled>
109 void debayer10P_GBGB_BGR888(uint8_t *dst, const uint8_t *src[]);
110 template<bool addAlphaByte, bool ccmEnabled>
111 void debayer10P_RGRG_BGR888(uint8_t *dst, const uint8_t *src[]);
112
113 static int getInputConfig(PixelFormat inputFormat, DebayerInputConfig &config);
114 static int getOutputConfig(PixelFormat outputFormat, DebayerOutputConfig &config);
115 int setupStandardBayerOrder(BayerFormat::Order order);
116 int setDebayerFunctions(PixelFormat inputFormat,
117 PixelFormat outputFormat,
118 bool ccmEnabled);
119 void updateGammaTable(const DebayerParams &params);
120 void updateLookupTables(const DebayerParams &params);
121
122 static constexpr unsigned int kRGBLookupSize = 256;
123 static constexpr unsigned int kGammaLookupSize = 1024;
124 struct CcmColumn {
125 int16_t r;
126 int16_t g;
127 int16_t b;
128 };
129 using LookupTable = std::array<uint8_t, kRGBLookupSize>;
130 using CcmLookupTable = std::array<CcmColumn, kRGBLookupSize>;
131 LookupTable red_;
132 LookupTable green_;
133 LookupTable blue_;
134 CcmLookupTable redCcm_;
135 CcmLookupTable greenCcm_;
136 CcmLookupTable blueCcm_;
137 std::array<double, kGammaLookupSize> gammaTable_;
138 LookupTable gammaLut_;
139 bool ccmEnabled_;
140 DebayerParams params_;
141
142 debayerFn debayer0_;
143 debayerFn debayer1_;
144 debayerFn debayer2_;
145 debayerFn debayer3_;
146 Rectangle window_;
147 std::unique_ptr<SwStatsCpu> stats_;
148 unsigned int xShift_; /* Offset of 0/1 applied to window_.x */
149
150 static constexpr unsigned int kMinThreads = 1;
151 static constexpr unsigned int kMaxThreads = 8;
152 static constexpr unsigned int kDefaultThreads = 2;
153
154 unsigned int workPending_ LIBCAMERA_TSA_GUARDED_BY(workPendingMutex_);
155 Mutex workPendingMutex_;
156 ConditionVariable workPendingCv_;
157 std::vector<std::unique_ptr<DebayerCpuThread>> threads_;
158};
159
160} /* namespace libcamera */
bayer_format.h
Class to represent Bayer formats and manipulate them.
libcamera::BayerFormat::Order
Order
The order of the colour channels in the Bayer pattern.
Definition bayer_format.h:25
libcamera::CameraManager
Provide access and manage all cameras in the system.
Definition camera_manager.h:25
libcamera::DebayerCpuThread
Class representing one CPU debayering thread.
Definition debayer_cpu.cpp:38
libcamera::DebayerCpu
Class for debayering on the CPU.
Definition debayer_cpu.h:32
libcamera::DebayerCpu::sizes
SizeRange sizes(PixelFormat inputFormat, const Size &inputSize) override
Get the supported output sizes for the given input format and size.
Definition debayer_cpu.cpp:1049
libcamera::DebayerCpu::strideAndFrameSize
std::tuple< unsigned int, unsigned int > strideAndFrameSize(const PixelFormat &outputFormat, const Size &size) override
Get the stride and the frame size.
Definition debayer_cpu.cpp:678
libcamera::DebayerCpu::process
void process(uint32_t frame, FrameBuffer *input, FrameBuffer *output, const DebayerParams &params) override
Process the bayer data into the requested format.
Definition debayer_cpu.cpp:973
libcamera::DebayerCpu::patternSize
Size patternSize(PixelFormat inputFormat) override
Get the width and height at which the bayer pattern repeats.
Definition debayer_cpu.cpp:657
libcamera::DebayerCpu::DebayerCpu
DebayerCpu(std::unique_ptr< SwStatsCpu > stats, const CameraManager &cm)
Constructs a DebayerCpu object.
Definition debayer_cpu.cpp:94
libcamera::DebayerCpu::start
int start() override
Execute a start signal in the debayer object from workerthread context.
Definition debayer_cpu.cpp:1032
libcamera::DebayerCpu::configure
int configure(const StreamConfiguration &inputCfg, const std::vector< std::reference_wrapper< const StreamConfiguration > > &outputCfgs, bool ccmEnabled) override
Configure the debayer object according to the passed in parameters.
Definition debayer_cpu.cpp:543
libcamera::DebayerCpu::getStatsFD
const SharedFD & getStatsFD() override
Get the file descriptor for the statistics.
Definition debayer_cpu.h:48
libcamera::DebayerCpu::formats
std::vector< PixelFormat > formats(PixelFormat input) override
Get the supported output formats.
Definition debayer_cpu.cpp:667
libcamera::DebayerCpu::stop
void stop() override
Stop the debayering process and perform cleanup.
Definition debayer_cpu.cpp:1040
libcamera::Debayer::Debayer
Debayer(const CameraManager &cm)
Construct a Debayer object.
Definition debayer.cpp:59
libcamera::FrameBuffer
Frame buffer data and its associated dynamic metadata.
Definition framebuffer.h:50
libcamera::PixelFormat
libcamera image pixel format
Definition pixel_format.h:17
libcamera::SharedFD
RAII-style wrapper for file descriptors.
Definition shared_fd.h:17
libcamera::SizeRange
Describe a range of sizes.
Definition geometry.h:205
libcamera::Size
Describe a two-dimensional size.
Definition geometry.h:51
camera_manager.h
Internal camera manager support.
mutex.h
Mutex classes with clang thread safety annotation.
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
object.h
Base object to support automatic signal disconnection.
libcamera::DebayerParams
Struct to hold the debayer parameters.
Definition debayer_params.h:20
libcamera::Debayer::DebayerInputConfig
Structure describing the incoming Bayer parameters.
Definition debayer.h:63
libcamera::Debayer::DebayerOutputConfig
Structure describing the output frame configuration.
Definition debayer.h:70
libcamera::StreamConfiguration
Configuration parameters for a stream.
Definition stream.h:40