libcamera  v0.2.0
Supporting cameras in Linux since 2019
debayer_cpu.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /*
3  * Copyright (C) 2023, Linaro Ltd
4  * Copyright (C) 2023, Red Hat Inc.
5  *
6  * Authors:
7  * Hans de Goede <hdegoede@redhat.com>
8  *
9  * debayer_cpu.h - 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  Size patternSize(PixelFormat inputFormat);
36  std::vector<PixelFormat> formats(PixelFormat input);
37  std::tuple<unsigned int, unsigned int>
38  strideAndFrameSize(const PixelFormat &outputFormat, const Size &size);
39  void process(FrameBuffer *input, FrameBuffer *output, DebayerParams params);
40  SizeRange sizes(PixelFormat inputFormat, const Size &inputSize);
41 
47  const SharedFD &getStatsFD() { return stats_->getStatsFD(); }
48 
54  unsigned int frameSize() { return outputConfig_.frameSize; }
55 
56 private:
85  using debayerFn = void (DebayerCpu::*)(uint8_t *dst, const uint8_t *src[]);
86 
87  /* 8-bit raw bayer format */
88  void debayer8_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
89  void debayer8_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
90  /* unpacked 10-bit raw bayer format */
91  void debayer10_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
92  void debayer10_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
93  /* unpacked 12-bit raw bayer format */
94  void debayer12_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
95  void debayer12_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
96  /* CSI-2 packed 10-bit raw bayer format (all the 4 orders) */
97  void debayer10P_BGBG_BGR888(uint8_t *dst, const uint8_t *src[]);
98  void debayer10P_GRGR_BGR888(uint8_t *dst, const uint8_t *src[]);
99  void debayer10P_GBGB_BGR888(uint8_t *dst, const uint8_t *src[]);
100  void debayer10P_RGRG_BGR888(uint8_t *dst, const uint8_t *src[]);
101 
102  struct DebayerInputConfig {
104  unsigned int bpp; /* Memory used per pixel, not precision */
105  unsigned int stride;
106  std::vector<PixelFormat> outputFormats;
107  };
108 
109  struct DebayerOutputConfig {
110  unsigned int bpp; /* Memory used per pixel, not precision */
111  unsigned int stride;
112  unsigned int frameSize;
113  };
114 
115  int getInputConfig(PixelFormat inputFormat, DebayerInputConfig &config);
116  int getOutputConfig(PixelFormat outputFormat, DebayerOutputConfig &config);
117  int setupStandardBayerOrder(BayerFormat::Order order);
118  int setDebayerFunctions(PixelFormat inputFormat, PixelFormat outputFormat);
119  void setupInputMemcpy(const uint8_t *linePointers[]);
120  void shiftLinePointers(const uint8_t *linePointers[], const uint8_t *src);
121  void memcpyNextLine(const uint8_t *linePointers[]);
122  void process2(const uint8_t *src, uint8_t *dst);
123  void process4(const uint8_t *src, uint8_t *dst);
124 
125  static constexpr unsigned int kGammaLookupSize = 1024;
126  static constexpr unsigned int kRGBLookupSize = 256;
127  /* Max. supported Bayer pattern height is 4, debayering this requires 5 lines */
128  static constexpr unsigned int kMaxLineBuffers = 5;
129 
130  std::array<uint8_t, kGammaLookupSize> gamma_;
131  std::array<uint8_t, kRGBLookupSize> red_;
132  std::array<uint8_t, kRGBLookupSize> green_;
133  std::array<uint8_t, kRGBLookupSize> blue_;
134  debayerFn debayer0_;
135  debayerFn debayer1_;
136  debayerFn debayer2_;
137  debayerFn debayer3_;
138  Rectangle window_;
139  DebayerInputConfig inputConfig_;
140  DebayerOutputConfig outputConfig_;
141  std::unique_ptr<SwStatsCpu> stats_;
142  uint8_t *lineBuffers_[kMaxLineBuffers];
143  unsigned int lineBufferLength_;
144  unsigned int lineBufferPadding_;
145  unsigned int lineBufferIndex_;
146  unsigned int xShift_; /* Offset of 0/1 applied to window_.x */
147  bool enableInputMemcpy_;
148  bool swapRedBlueGains_;
149  float gammaCorrection_;
150  unsigned int blackLevel_;
151  unsigned int measuredFrames_;
152  int64_t frameProcessTime_;
153  /* Skip 30 frames for things to stabilize then measure 30 frames */
154  static constexpr unsigned int kFramesToSkip = 30;
155  static constexpr unsigned int kLastFrameToMeasure = 60;
156 };
157 
158 } /* 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:28
Size patternSize(PixelFormat inputFormat)
Get the width and height at which the bayer pattern repeats.
Definition: debayer_cpu.cpp:506
int configure(const StreamConfiguration &inputCfg, const std::vector< std::reference_wrapper< StreamConfiguration >> &outputCfgs)
Configure the debayer object according to the passed in parameters.
Definition: debayer_cpu.cpp:430
SizeRange sizes(PixelFormat inputFormat, const Size &inputSize)
Get the supported output sizes for the given input format and size.
Definition: debayer_cpu.cpp:778
DebayerCpu(std::unique_ptr< SwStatsCpu > stats)
Constructs a DebayerCpu object.
Definition: debayer_cpu.cpp:37
const SharedFD & getStatsFD()
Get the file descriptor for the statistics.
Definition: debayer_cpu.h:47
unsigned int frameSize()
Get the output frame size.
Definition: debayer_cpu.h:54
void process(FrameBuffer *input, FrameBuffer *output, DebayerParams params)
Process the bayer data into the requested format.
Definition: debayer_cpu.cpp:692
std::vector< PixelFormat > formats(PixelFormat input)
Get the supported output formats.
Definition: debayer_cpu.cpp:516
std::tuple< unsigned int, unsigned int > strideAndFrameSize(const PixelFormat &outputFormat, const Size &size)
Get the stride and the frame size.
Definition: debayer_cpu.cpp:527
Base debayering class.
Definition: debayer.h:31
Frame buffer data and its associated dynamic metadata.
Definition: framebuffer.h:50
Base object to support automatic signal disconnection.
Definition: object.h:25
libcamera image pixel format
Definition: pixel_format.h:18
RAII-style wrapper for file descriptors.
Definition: shared_fd.h:17
Describe a range of sizes.
Definition: geometry.h:201
Describe a two-dimensional size.
Definition: geometry.h:53
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:15
Configuration parameters for a stream.
Definition: stream.h:41