libcamera v0.8.0+rpt20261104
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
20
22
24#include "libcamera/internal/software_isp/debayer_params.h"
25#include "libcamera/internal/software_isp/swstats_cpu.h"
26
27#include "debayer.h"
28
29namespace libcamera {
30
31class DebayerCpuThread;
32class DebayerCpu : public Debayer
33{
34public:
35 DebayerCpu(std::unique_ptr<SwStatsCpu> stats, const CameraManager &cm);
37
38 int configure(const StreamConfiguration &inputCfg,
39 const std::vector<std::reference_wrapper<const StreamConfiguration>> &outputCfgs,
40 bool ccmEnabled,
41 bool lscEnabled) override;
42 Size patternSize(PixelFormat inputFormat) override;
43 std::vector<PixelFormat> formats(PixelFormat input) override;
44 std::tuple<unsigned int, unsigned int>
45 strideAndFrameSize(const PixelFormat &outputFormat, const Size &size) override;
46 void process(uint32_t frame, FrameBuffer *input, FrameBuffer *output, const DebayerParams &params) override;
47 int start() override;
48 void stop() override;
49 SizeRange sizes(PixelFormat inputFormat, const Size &inputSize) override;
50 const SharedFD &getStatsFD() override { return stats_->getStatsFD(); }
51
52private:
53 friend class DebayerCpuThread;
54
83 using debayerFn = void (DebayerCpu::*)(uint8_t *dst, const uint8_t *src[]);
84
85 void debayer0(uint8_t *dst, const uint8_t *src[]) { (this->*debayer0_)(dst, src); }
86 void debayer1(uint8_t *dst, const uint8_t *src[]) { (this->*debayer1_)(dst, src); }
87 void debayer2(uint8_t *dst, const uint8_t *src[]) { (this->*debayer2_)(dst, src); }
88 void debayer3(uint8_t *dst, const uint8_t *src[]) { (this->*debayer3_)(dst, src); }
89
90 /* 8-bit raw bayer format */
91 template<bool addAlphaByte, bool ccmEnabled>
92 void debayer8_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
93 template<bool addAlphaByte, bool ccmEnabled>
94 void debayer8_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
95 /* unpacked 10-bit raw bayer format */
96 template<bool addAlphaByte, bool ccmEnabled>
97 void debayer10_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
98 template<bool addAlphaByte, bool ccmEnabled>
99 void debayer10_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
100 /* unpacked 12-bit raw bayer format */
101 template<bool addAlphaByte, bool ccmEnabled>
102 void debayer12_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
103 template<bool addAlphaByte, bool ccmEnabled>
104 void debayer12_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
105 /* CSI-2 packed 10-bit raw bayer format (all the 4 orders) */
106 template<bool addAlphaByte, bool ccmEnabled>
107 void debayer10P_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
108 template<bool addAlphaByte, bool ccmEnabled>
109 void debayer10P_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
110 template<bool addAlphaByte, bool ccmEnabled>
111 void debayer10P_GBGB_BGR888(uint8_t *dst, const uint8_t *src[]);
112 template<bool addAlphaByte, bool ccmEnabled>
113 void debayer10P_RGRG_BGR888(uint8_t *dst, const uint8_t *src[]);
114 /* CSI-2 packed 12-bit raw bayer format (all the 4 orders) */
115 template<bool addAlphaByte, bool ccmEnabled>
116 void debayer12P_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
117 template<bool addAlphaByte, bool ccmEnabled>
118 void debayer12P_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
119 template<bool addAlphaByte, bool ccmEnabled>
120 void debayer12P_GBGB_BGR888(uint8_t *dst, const uint8_t *src[]);
121 template<bool addAlphaByte, bool ccmEnabled>
122 void debayer12P_RGRG_BGR888(uint8_t *dst, const uint8_t *src[]);
123
124 static int getInputConfig(PixelFormat inputFormat, DebayerInputConfig &config);
125 int setupStandardBayerOrder(BayerFormat::Order order);
126 int setDebayerFunctions(PixelFormat inputFormat,
127 PixelFormat outputFormat,
128 bool ccmEnabled);
129 void updateGammaTable(const DebayerParams &params);
130 void updateLookupTables(const DebayerParams &params);
131
132 static constexpr unsigned int kRGBLookupSize = 256;
133 static constexpr unsigned int kGammaLookupSize = 1024;
134 struct CcmColumn {
135 int16_t r;
136 int16_t g;
137 int16_t b;
138 };
139 using LookupTable = std::array<uint8_t, kRGBLookupSize>;
140 using CcmLookupTable = std::array<CcmColumn, kRGBLookupSize>;
141 LookupTable red_;
142 LookupTable green_;
143 LookupTable blue_;
144 CcmLookupTable redCcm_;
145 CcmLookupTable greenCcm_;
146 CcmLookupTable blueCcm_;
147 std::array<double, kGammaLookupSize> gammaTable_;
148 LookupTable gammaLut_;
149 bool ccmEnabled_;
150 DebayerParams params_;
151
152 debayerFn debayer0_;
153 debayerFn debayer1_;
154 debayerFn debayer2_;
155 debayerFn debayer3_;
156 Rectangle window_;
157 std::unique_ptr<SwStatsCpu> stats_;
158 unsigned int xShift_; /* Offset of 0/1 applied to window_.x */
159
160 static constexpr unsigned int kMinThreads = 1;
161 static constexpr unsigned int kMaxThreads = 8;
162 static constexpr unsigned int kDefaultThreads = 2;
163
164 unsigned int workPending_ LIBCAMERA_TSA_GUARDED_BY(workPendingMutex_);
165 Mutex workPendingMutex_;
166 ConditionVariable workPendingCv_;
167 std::vector<std::unique_ptr<DebayerCpuThread>> threads_;
168};
169
170} /* namespace libcamera */
Class to represent Bayer formats and manipulate them.
The camera manager.
Order
The order of the colour channels in the Bayer pattern.
Definition bayer_format.h:25
Provide access and manage all cameras in the system.
Definition camera_manager.h:25
Class representing one CPU debayering thread.
Definition debayer_cpu.cpp:40
Class for debayering on the CPU.
Definition debayer_cpu.h:33
SizeRange sizes(PixelFormat inputFormat, const Size &inputSize) override
Get the supported output sizes for the given input format and size.
Definition debayer_cpu.cpp:1125
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:764
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:1049
Size patternSize(PixelFormat inputFormat) override
Get the width and height at which the bayer pattern repeats.
Definition debayer_cpu.cpp:743
int start() override
Execute a start signal in the debayer object from workerthread context.
Definition debayer_cpu.cpp:1108
const SharedFD & getStatsFD() override
Get the file descriptor for the statistics.
Definition debayer_cpu.h:50
int configure(const StreamConfiguration &inputCfg, const std::vector< std::reference_wrapper< const StreamConfiguration > > &outputCfgs, bool ccmEnabled, bool lscEnabled) override
Configure the debayer object according to the passed in parameters.
Definition debayer_cpu.cpp:625
std::vector< PixelFormat > formats(PixelFormat input) override
Get the supported output formats.
Definition debayer_cpu.cpp:753
void stop() override
Stop the debayering process and perform cleanup.
Definition debayer_cpu.cpp:1116
Base debayering class.
Definition debayer.h:36
Frame buffer data and its associated dynamic metadata.
Definition framebuffer.h:50
libcamera image pixel format
Definition pixel_format.h:17
RAII-style wrapper for file descriptors.
Definition shared_fd.h:17
Describe a range of sizes.
Definition geometry.h:205
Describe a two-dimensional size.
Definition geometry.h:51
Mutex classes with clang thread safety annotation.
Top-level libcamera namespace.
Definition backtrace.h:17
Base object to support automatic signal disconnection.
Struct to hold the debayer parameters.
Definition debayer_params.h:21
Configuration parameters for a stream.
Definition stream.h:40