libcamera v0.7.0+1-4ceceb68
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
19
21#include "libcamera/internal/global_configuration.h"
22#include "libcamera/internal/software_isp/debayer_params.h"
23#include "libcamera/internal/software_isp/swstats_cpu.h"
24
25#include "debayer.h"
26
27namespace libcamera {
28
29class DebayerCpu : public Debayer
30{
31public:
32 DebayerCpu(std::unique_ptr<SwStatsCpu> stats, const GlobalConfiguration &configuration);
34
35 int configure(const StreamConfiguration &inputCfg,
36 const std::vector<std::reference_wrapper<StreamConfiguration>> &outputCfgs,
37 bool ccmEnabled);
38 Size patternSize(PixelFormat inputFormat);
39 std::vector<PixelFormat> formats(PixelFormat input);
40 std::tuple<unsigned int, unsigned int>
41 strideAndFrameSize(const PixelFormat &outputFormat, const Size &size);
42 void process(uint32_t frame, FrameBuffer *input, FrameBuffer *output, DebayerParams params);
43 SizeRange sizes(PixelFormat inputFormat, const Size &inputSize);
44 const SharedFD &getStatsFD() { return stats_->getStatsFD(); }
45
46private:
75 using debayerFn = void (DebayerCpu::*)(uint8_t *dst, const uint8_t *src[]);
76
77 /* 8-bit raw bayer format */
78 template<bool addAlphaByte, bool ccmEnabled>
79 void debayer8_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
80 template<bool addAlphaByte, bool ccmEnabled>
81 void debayer8_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
82 /* unpacked 10-bit raw bayer format */
83 template<bool addAlphaByte, bool ccmEnabled>
84 void debayer10_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
85 template<bool addAlphaByte, bool ccmEnabled>
86 void debayer10_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
87 /* unpacked 12-bit raw bayer format */
88 template<bool addAlphaByte, bool ccmEnabled>
89 void debayer12_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
90 template<bool addAlphaByte, bool ccmEnabled>
91 void debayer12_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
92 /* CSI-2 packed 10-bit raw bayer format (all the 4 orders) */
93 template<bool addAlphaByte, bool ccmEnabled>
94 void debayer10P_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
95 template<bool addAlphaByte, bool ccmEnabled>
96 void debayer10P_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
97 template<bool addAlphaByte, bool ccmEnabled>
98 void debayer10P_GBGB_BGR888(uint8_t *dst, const uint8_t *src[]);
99 template<bool addAlphaByte, bool ccmEnabled>
100 void debayer10P_RGRG_BGR888(uint8_t *dst, const uint8_t *src[]);
101
102 static int getInputConfig(PixelFormat inputFormat, DebayerInputConfig &config);
103 static int getOutputConfig(PixelFormat outputFormat, DebayerOutputConfig &config);
104 int setupStandardBayerOrder(BayerFormat::Order order);
105 int setDebayerFunctions(PixelFormat inputFormat,
106 PixelFormat outputFormat,
107 bool ccmEnabled);
108 void setupInputMemcpy(const uint8_t *linePointers[]);
109 void shiftLinePointers(const uint8_t *linePointers[], const uint8_t *src);
110 void memcpyNextLine(const uint8_t *linePointers[]);
111 void process2(uint32_t frame, const uint8_t *src, uint8_t *dst);
112 void process4(uint32_t frame, const uint8_t *src, uint8_t *dst);
113 void updateGammaTable(DebayerParams &params);
114 void updateLookupTables(DebayerParams &params);
115
116 /* Max. supported Bayer pattern height is 4, debayering this requires 5 lines */
117 static constexpr unsigned int kMaxLineBuffers = 5;
118
119 static constexpr unsigned int kRGBLookupSize = 256;
120 static constexpr unsigned int kGammaLookupSize = 1024;
121 struct CcmColumn {
122 int16_t r;
123 int16_t g;
124 int16_t b;
125 };
126 using LookupTable = std::array<uint8_t, kRGBLookupSize>;
127 using CcmLookupTable = std::array<CcmColumn, kRGBLookupSize>;
128 LookupTable red_;
129 LookupTable green_;
130 LookupTable blue_;
131 CcmLookupTable redCcm_;
132 CcmLookupTable greenCcm_;
133 CcmLookupTable blueCcm_;
134 std::array<double, kGammaLookupSize> gammaTable_;
135 LookupTable gammaLut_;
136 bool ccmEnabled_;
137 DebayerParams params_;
138
139 debayerFn debayer0_;
140 debayerFn debayer1_;
141 debayerFn debayer2_;
142 debayerFn debayer3_;
143 Rectangle window_;
144 std::unique_ptr<SwStatsCpu> stats_;
145 std::vector<uint8_t> lineBuffers_[kMaxLineBuffers];
146 unsigned int lineBufferLength_;
147 unsigned int lineBufferPadding_;
148 unsigned int lineBufferIndex_;
149 unsigned int xShift_; /* Offset of 0/1 applied to window_.x */
150 bool enableInputMemcpy_;
151};
152
153} /* 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 for debayering on the CPU.
Definition debayer_cpu.h:30
void process(uint32_t frame, FrameBuffer *input, FrameBuffer *output, DebayerParams params)
Process the bayer data into the requested format.
Definition debayer_cpu.cpp:845
Size patternSize(PixelFormat inputFormat)
Get the width and height at which the bayer pattern repeats.
Definition debayer_cpu.cpp:569
SizeRange sizes(PixelFormat inputFormat, const Size &inputSize)
Get the supported output sizes for the given input format and size.
Definition debayer_cpu.cpp:893
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:481
std::vector< PixelFormat > formats(PixelFormat input)
Get the supported output formats.
Definition debayer_cpu.cpp:579
std::tuple< unsigned int, unsigned int > strideAndFrameSize(const PixelFormat &outputFormat, const Size &size)
Get the stride and the frame size.
Definition debayer_cpu.cpp:590
const SharedFD & getStatsFD()
Get the file descriptor for the statistics.
Definition debayer_cpu.h:44
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
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