libcamera v0.7.1+1-5701eb5f-nvm
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) override;
41 Size patternSize(PixelFormat inputFormat) override;
42 std::vector<PixelFormat> formats(PixelFormat input) override;
43 std::tuple<unsigned int, unsigned int>
44 strideAndFrameSize(const PixelFormat &outputFormat, const Size &size) override;
45 void process(uint32_t frame, FrameBuffer *input, FrameBuffer *output, const DebayerParams &params) override;
46 int start() override;
47 void stop() override;
48 SizeRange sizes(PixelFormat inputFormat, const Size &inputSize) override;
49 const SharedFD &getStatsFD() override { return stats_->getStatsFD(); }
50
51private:
52 friend class DebayerCpuThread;
53
82 using debayerFn = void (DebayerCpu::*)(uint8_t *dst, const uint8_t *src[]);
83
84 void debayer0(uint8_t *dst, const uint8_t *src[]) { (this->*debayer0_)(dst, src); }
85 void debayer1(uint8_t *dst, const uint8_t *src[]) { (this->*debayer1_)(dst, src); }
86 void debayer2(uint8_t *dst, const uint8_t *src[]) { (this->*debayer2_)(dst, src); }
87 void debayer3(uint8_t *dst, const uint8_t *src[]) { (this->*debayer3_)(dst, src); }
88
89 /* 8-bit raw bayer format */
90 template<bool addAlphaByte, bool ccmEnabled>
91 void debayer8_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
92 template<bool addAlphaByte, bool ccmEnabled>
93 void debayer8_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
94 /* unpacked 10-bit raw bayer format */
95 template<bool addAlphaByte, bool ccmEnabled>
96 void debayer10_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
97 template<bool addAlphaByte, bool ccmEnabled>
98 void debayer10_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
99 /* unpacked 12-bit raw bayer format */
100 template<bool addAlphaByte, bool ccmEnabled>
101 void debayer12_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
102 template<bool addAlphaByte, bool ccmEnabled>
103 void debayer12_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
104 /* CSI-2 packed 10-bit raw bayer format (all the 4 orders) */
105 template<bool addAlphaByte, bool ccmEnabled>
106 void debayer10P_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
107 template<bool addAlphaByte, bool ccmEnabled>
108 void debayer10P_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
109 template<bool addAlphaByte, bool ccmEnabled>
110 void debayer10P_GBGB_BGR888(uint8_t *dst, const uint8_t *src[]);
111 template<bool addAlphaByte, bool ccmEnabled>
112 void debayer10P_RGRG_BGR888(uint8_t *dst, const uint8_t *src[]);
113 /* CSI-2 packed 12-bit raw bayer format (all the 4 orders) */
114 template<bool addAlphaByte, bool ccmEnabled>
115 void debayer12P_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
116 template<bool addAlphaByte, bool ccmEnabled>
117 void debayer12P_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
118 template<bool addAlphaByte, bool ccmEnabled>
119 void debayer12P_GBGB_BGR888(uint8_t *dst, const uint8_t *src[]);
120 template<bool addAlphaByte, bool ccmEnabled>
121 void debayer12P_RGRG_BGR888(uint8_t *dst, const uint8_t *src[]);
122
123 static int getInputConfig(PixelFormat inputFormat, DebayerInputConfig &config);
124 static int getOutputConfig(PixelFormat outputFormat, DebayerOutputConfig &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:39
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:1148
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:777
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:1072
Size patternSize(PixelFormat inputFormat) override
Get the width and height at which the bayer pattern repeats.
Definition debayer_cpu.cpp:756
int start() override
Execute a start signal in the debayer object from workerthread context.
Definition debayer_cpu.cpp:1131
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:642
const SharedFD & getStatsFD() override
Get the file descriptor for the statistics.
Definition debayer_cpu.h:49
std::vector< PixelFormat > formats(PixelFormat input) override
Get the supported output formats.
Definition debayer_cpu.cpp:766
void stop() override
Stop the debayering process and perform cleanup.
Definition debayer_cpu.cpp:1139
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:20
Configuration parameters for a stream.
Definition stream.h:40