libcamera  v0.5.0+37-12007759
Supporting cameras in Linux since 2019
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
21 
22 #include "debayer.h"
23 #include "swstats_cpu.h"
24 
25 namespace libcamera {
26 
27 class DebayerCpu : public Debayer, public Object
28 {
29 public:
30  DebayerCpu(std::unique_ptr<SwStatsCpu> stats);
31  ~DebayerCpu();
32 
33  int configure(const StreamConfiguration &inputCfg,
34  const std::vector<std::reference_wrapper<StreamConfiguration>> &outputCfgs,
35  bool ccmEnabled);
36  Size patternSize(PixelFormat inputFormat);
37  std::vector<PixelFormat> formats(PixelFormat input);
38  std::tuple<unsigned int, unsigned int>
39  strideAndFrameSize(const PixelFormat &outputFormat, const Size &size);
40  void process(uint32_t frame, FrameBuffer *input, FrameBuffer *output, DebayerParams params);
41  SizeRange sizes(PixelFormat inputFormat, const Size &inputSize);
42 
48  const SharedFD &getStatsFD() { return stats_->getStatsFD(); }
49 
55  unsigned int frameSize() { return outputConfig_.frameSize; }
56 
57 private:
86  using debayerFn = void (DebayerCpu::*)(uint8_t *dst, const uint8_t *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  struct DebayerInputConfig {
115  unsigned int bpp; /* Memory used per pixel, not precision */
116  unsigned int stride;
117  std::vector<PixelFormat> outputFormats;
118  };
119 
120  struct DebayerOutputConfig {
121  unsigned int bpp; /* Memory used per pixel, not precision */
122  unsigned int stride;
123  unsigned int frameSize;
124  };
125 
126  int getInputConfig(PixelFormat inputFormat, DebayerInputConfig &config);
127  int getOutputConfig(PixelFormat outputFormat, DebayerOutputConfig &config);
128  int setupStandardBayerOrder(BayerFormat::Order order);
129  int setDebayerFunctions(PixelFormat inputFormat,
130  PixelFormat outputFormat,
131  bool ccmEnabled);
132  void setupInputMemcpy(const uint8_t *linePointers[]);
133  void shiftLinePointers(const uint8_t *linePointers[], const uint8_t *src);
134  void memcpyNextLine(const uint8_t *linePointers[]);
135  void process2(const uint8_t *src, uint8_t *dst);
136  void process4(const uint8_t *src, uint8_t *dst);
137 
138  /* Max. supported Bayer pattern height is 4, debayering this requires 5 lines */
139  static constexpr unsigned int kMaxLineBuffers = 5;
140 
147  DebayerParams::LookupTable gammaLut_;
148  debayerFn debayer0_;
149  debayerFn debayer1_;
150  debayerFn debayer2_;
151  debayerFn debayer3_;
152  Rectangle window_;
153  DebayerInputConfig inputConfig_;
154  DebayerOutputConfig outputConfig_;
155  std::unique_ptr<SwStatsCpu> stats_;
156  std::vector<uint8_t> lineBuffers_[kMaxLineBuffers];
157  unsigned int lineBufferLength_;
158  unsigned int lineBufferPadding_;
159  unsigned int lineBufferIndex_;
160  unsigned int xShift_; /* Offset of 0/1 applied to window_.x */
161  bool enableInputMemcpy_;
162  bool swapRedBlueGains_;
163  unsigned int measuredFrames_;
164  int64_t frameProcessTime_;
165  /* Skip 30 frames for things to stabilize then measure 30 frames */
166  static constexpr unsigned int kFramesToSkip = 30;
167  static constexpr unsigned int kLastFrameToMeasure = 60;
168 };
169 
170 } /* namespace libcamera */
RAII-style wrapper for file descriptors.
Definition: shared_fd.h:16
Struct to hold the debayer parameters.
Definition: debayer_params.h:18
void process(uint32_t frame, FrameBuffer *input, FrameBuffer *output, DebayerParams params)
Process the bayer data into the requested format.
Definition: debayer_cpu.cpp:759
const SharedFD & getStatsFD()
Get the file descriptor for the statistics.
Definition: debayer_cpu.h:48
libcamera image pixel format
Definition: pixel_format.h:16
Size patternSize(PixelFormat inputFormat)
Get the width and height at which the bayer pattern repeats.
Definition: debayer_cpu.cpp:567
Top-level libcamera namespace.
Definition: backtrace.h:17
std::vector< PixelFormat > formats(PixelFormat input)
Get the supported output formats.
Definition: debayer_cpu.cpp:577
Frame buffer data and its associated dynamic metadata.
Definition: framebuffer.h:48
std::array< uint8_t, kRGBLookupSize > LookupTable
Type of the lookup tables for single lookup values.
Definition: debayer_params.h:27
Describe a two-dimensional size.
Definition: geometry.h:50
SizeRange sizes(PixelFormat inputFormat, const Size &inputSize)
Get the supported output sizes for the given input format and size.
Definition: debayer_cpu.cpp:846
DebayerCpu(std::unique_ptr< SwStatsCpu > stats)
Constructs a DebayerCpu object.
Definition: debayer_cpu.cpp:42
Describe a rectangle&#39;s position and dimensions.
Definition: geometry.h:240
std::tuple< unsigned int, unsigned int > strideAndFrameSize(const PixelFormat &outputFormat, const Size &size)
Get the stride and the frame size.
Definition: debayer_cpu.cpp:588
Class for debayering on the CPU.
Definition: debayer_cpu.h:27
Class to represent Bayer formats and manipulate them.
Base object to support automatic signal disconnection.
Describe a range of sizes.
Definition: geometry.h:198
Order
The order of the colour channels in the Bayer pattern.
Definition: bayer_format.h:25
unsigned int frameSize()
Get the output frame size.
Definition: debayer_cpu.h:55
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:490
Base object to support automatic signal disconnection.
Definition: object.h:26
Base debayering class.
Definition: debayer.h:30
std::array< CcmColumn, kRGBLookupSize > CcmLookupTable
Type of the CCM lookup tables for red, green, blue values.
Definition: debayer_params.h:28
Configuration parameters for a stream.
Definition: stream.h:40