libcamera v0.7.0+1595-4b6c47bd-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#include "libcamera/internal/global_configuration.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;
31class DebayerCpu : public Debayer
32{
33public:
34 DebayerCpu(std::unique_ptr<SwStatsCpu> stats, const GlobalConfiguration &configuration);
36
37 int configure(const StreamConfiguration &inputCfg,
38 const std::vector<std::reference_wrapper<const StreamConfiguration>> &outputCfgs,
39 bool ccmEnabled);
40 Size patternSize(PixelFormat inputFormat);
41 std::vector<PixelFormat> formats(PixelFormat input);
42 std::tuple<unsigned int, unsigned int>
43 strideAndFrameSize(const PixelFormat &outputFormat, const Size &size);
44 void process(uint32_t frame, FrameBuffer *input, FrameBuffer *output, const DebayerParams &params);
45 int start();
46 void stop();
47 SizeRange sizes(PixelFormat inputFormat, const Size &inputSize);
48 const SharedFD &getStatsFD() { 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 */
Class to represent Bayer formats and manipulate them.
Order
The order of the colour channels in the Bayer pattern.
Definition bayer_format.h:25
Class representing one CPU debayering thread.
Definition debayer_cpu.cpp:38
Class for debayering on the CPU.
Definition debayer_cpu.h:32
Size patternSize(PixelFormat inputFormat)
Get the width and height at which the bayer pattern repeats.
Definition debayer_cpu.cpp:656
SizeRange sizes(PixelFormat inputFormat, const Size &inputSize)
Get the supported output sizes for the given input format and size.
Definition debayer_cpu.cpp:1048
int configure(const StreamConfiguration &inputCfg, const std::vector< std::reference_wrapper< const StreamConfiguration > > &outputCfgs, bool ccmEnabled)
Configure the debayer object according to the passed in parameters.
Definition debayer_cpu.cpp:542
void stop()
Stop the debayering process and perform cleanup.
Definition debayer_cpu.cpp:1039
void process(uint32_t frame, FrameBuffer *input, FrameBuffer *output, const DebayerParams &params)
Process the bayer data into the requested format.
Definition debayer_cpu.cpp:972
int start()
Execute a start signal in the debayer object from workerthread context.
Definition debayer_cpu.cpp:1031
std::vector< PixelFormat > formats(PixelFormat input)
Get the supported output formats.
Definition debayer_cpu.cpp:666
std::tuple< unsigned int, unsigned int > strideAndFrameSize(const PixelFormat &outputFormat, const Size &size)
Get the stride and the frame size.
Definition debayer_cpu.cpp:677
const SharedFD & getStatsFD()
Get the file descriptor for the statistics.
Definition debayer_cpu.h:48
Base debayering class.
Definition debayer.h:36
Frame buffer data and its associated dynamic metadata.
Definition framebuffer.h:50
Support for global libcamera configuration.
Definition global_configuration.h:22
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