libcamera v0.6.0 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-2025 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/object.h>
19
20#include "libcamera/internal/bayer_format.h"
21#include "libcamera/internal/global_configuration.h"
22
23#include "debayer.h"
24#include "swstats_cpu.h"
25
26 namespace libcamera {
27
28 class DebayerCpu : public Debayer, public Object
29{
30 public:
31 DebayerCpu(std::unique_ptr<SwStatsCpu> stats, const GlobalConfiguration &configuration);
32 ~DebayerCpu();
33
34 int configure(const StreamConfiguration &inputCfg,
35 const std::vector<std::reference_wrapper<StreamConfiguration>> &outputCfgs,
36 bool ccmEnabled);
37 Size patternSize(PixelFormat inputFormat);
38 std::vector<PixelFormat> formats(PixelFormat input);
39 std::tuple<unsigned int, unsigned int>
40 strideAndFrameSize(const PixelFormat &outputFormat, const Size &size);
41 void process(uint32_t frame, FrameBuffer *input, FrameBuffer *output, DebayerParams params);
42 SizeRange sizes(PixelFormat inputFormat, const Size &inputSize);
43
49 const SharedFD &getStatsFD() { return stats_->getStatsFD(); }
50
56 unsigned int frameSize() { return outputConfig_.frameSize; }
57
58 private:
87 using debayerFn = void (DebayerCpu::*)(uint8_t *dst, const uint8_t *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
114 struct DebayerInputConfig {
115 Size patternSize;
116 unsigned int bpp; /* Memory used per pixel, not precision */
117 unsigned int stride;
118 std::vector<PixelFormat> outputFormats;
119 };
120
121 struct DebayerOutputConfig {
122 unsigned int bpp; /* Memory used per pixel, not precision */
123 unsigned int stride;
124 unsigned int frameSize;
125 };
126
127 int getInputConfig(PixelFormat inputFormat, DebayerInputConfig &config);
128 int getOutputConfig(PixelFormat outputFormat, DebayerOutputConfig &config);
129 int setupStandardBayerOrder(BayerFormat::Order order);
130 int setDebayerFunctions(PixelFormat inputFormat,
131 PixelFormat outputFormat,
132 bool ccmEnabled);
133 void setupInputMemcpy(const uint8_t *linePointers[]);
134 void shiftLinePointers(const uint8_t *linePointers[], const uint8_t *src);
135 void memcpyNextLine(const uint8_t *linePointers[]);
136 void process2(uint32_t frame, const uint8_t *src, uint8_t *dst);
137 void process4(uint32_t frame, const uint8_t *src, uint8_t *dst);
138
139 /* Max. supported Bayer pattern height is 4, debayering this requires 5 lines */
140 static constexpr unsigned int kMaxLineBuffers = 5;
141
142 DebayerParams::LookupTable red_;
143 DebayerParams::LookupTable green_;
144 DebayerParams::LookupTable blue_;
145 DebayerParams::CcmLookupTable redCcm_;
146 DebayerParams::CcmLookupTable greenCcm_;
147 DebayerParams::CcmLookupTable blueCcm_;
148 DebayerParams::LookupTable gammaLut_;
149 debayerFn debayer0_;
150 debayerFn debayer1_;
151 debayerFn debayer2_;
152 debayerFn debayer3_;
153 Rectangle window_;
154 DebayerInputConfig inputConfig_;
155 DebayerOutputConfig outputConfig_;
156 std::unique_ptr<SwStatsCpu> stats_;
157 std::vector<uint8_t> lineBuffers_[kMaxLineBuffers];
158 unsigned int lineBufferLength_;
159 unsigned int lineBufferPadding_;
160 unsigned int lineBufferIndex_;
161 unsigned int xShift_; /* Offset of 0/1 applied to window_.x */
162 bool enableInputMemcpy_;
163 bool swapRedBlueGains_;
164 unsigned int encounteredFrames_;
165 int64_t frameProcessTime_;
166 unsigned int skipBeforeMeasure_ = 30;
167 unsigned int framesToMeasure_ = 30;
168};
169
170} /* 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::DebayerCpu
Class for debayering on the CPU.
Definition debayer_cpu.h:29
libcamera::DebayerCpu::process
void process(uint32_t frame, FrameBuffer *input, FrameBuffer *output, DebayerParams params)
Process the bayer data into the requested format.
Definition debayer_cpu.cpp:778
libcamera::DebayerCpu::patternSize
Size patternSize(PixelFormat inputFormat)
Get the width and height at which the bayer pattern repeats.
Definition debayer_cpu.cpp:584
libcamera::DebayerCpu::sizes
SizeRange sizes(PixelFormat inputFormat, const Size &inputSize)
Get the supported output sizes for the given input format and size.
Definition debayer_cpu.cpp:866
libcamera::DebayerCpu::frameSize
unsigned int frameSize()
Get the output frame size.
Definition debayer_cpu.h:56
libcamera::DebayerCpu::configure
int configure(const StreamConfiguration &inputCfg, const std::vector< std::reference_wrapper< StreamConfiguration > > &outputCfgs, bool ccmEnabled)
Configure the debayer object according to the passed in parameters.
Definition debayer_cpu.cpp:503
libcamera::DebayerCpu::DebayerCpu
DebayerCpu(std::unique_ptr< SwStatsCpu > stats, const GlobalConfiguration &configuration)
Constructs a DebayerCpu object.
Definition debayer_cpu.cpp:44
libcamera::DebayerCpu::formats
std::vector< PixelFormat > formats(PixelFormat input)
Get the supported output formats.
Definition debayer_cpu.cpp:594
libcamera::DebayerCpu::strideAndFrameSize
std::tuple< unsigned int, unsigned int > strideAndFrameSize(const PixelFormat &outputFormat, const Size &size)
Get the stride and the frame size.
Definition debayer_cpu.cpp:605
libcamera::DebayerCpu::getStatsFD
const SharedFD & getStatsFD()
Get the file descriptor for the statistics.
Definition debayer_cpu.h:49
libcamera::Debayer
Base debayering class.
Definition debayer.h:31
libcamera::FrameBuffer
Frame buffer data and its associated dynamic metadata.
Definition framebuffer.h:50
libcamera::GlobalConfiguration
Support for global libcamera configuration.
Definition global_configuration.h:22
libcamera::Object::Object
Object(Object *parent=nullptr)
Construct an Object instance.
Definition object.cpp:69
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
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:18
libcamera::DebayerParams::CcmLookupTable
std::array< CcmColumn, kRGBLookupSize > CcmLookupTable
Type of the CCM lookup tables for red, green, blue values.
Definition debayer_params.h:28
libcamera::DebayerParams::LookupTable
std::array< uint8_t, kRGBLookupSize > LookupTable
Type of the lookup tables for single lookup values.
Definition debayer_params.h:27
libcamera::StreamConfiguration
Configuration parameters for a stream.
Definition stream.h:40