libcamera v0.7.0+1595-4b6c47bd-nvm
Supporting cameras in Linux since 2019
Loading...
Searching...
No Matches
swstats_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 * CPU based software statistics implementation
10 */
11
12#pragma once
13
14#include <stdint.h>
15#include <vector>
16
18
19#include <libcamera/geometry.h>
20
23#include "libcamera/internal/global_configuration.h"
24#include "libcamera/internal/shared_mem_object.h"
25#include "libcamera/internal/software_isp/swisp_stats.h"
26
27#include "benchmark.h"
28
29namespace libcamera {
30
31class PixelFormat;
32class MappedFrameBuffer;
33struct StreamConfiguration;
34
36{
37public:
38 SwStatsCpu(const GlobalConfiguration &configuration);
39 ~SwStatsCpu() = default;
40
41 /*
42 * The combination of pipeline + sensor delays means that
43 * exposure changes can take up to 3 frames to get applied,
44 * Run stats once every 4 frames to ensure any previous
45 * exposure changes have been applied.
46 */
47 static constexpr uint32_t kStatPerNumFrames = 4;
48
49 bool isValid() const { return sharedStats_.fd().isValid(); }
50
51 const SharedFD &getStatsFD() { return sharedStats_.fd(); }
52
53 const Size &patternSize() { return patternSize_; }
54
55 int configure(const StreamConfiguration &inputCfg, unsigned int statsBufferCount = 1);
56 void setWindow(const Rectangle &window);
57 void startFrame(uint32_t frame);
58 void finishFrame(uint32_t frame, uint32_t bufferId);
59 void processFrame(uint32_t frame, uint32_t bufferId, FrameBuffer *input);
60
61 void processLine0(uint32_t frame, unsigned int y, const uint8_t *src[], unsigned int statsBufferIndex = 0)
62 {
63 if (frame % kStatPerNumFrames)
64 return;
65
66 if ((y & ySkipMask_) || y < static_cast<unsigned int>(window_.y) ||
67 y >= (window_.y + window_.height))
68 return;
69
70 (this->*stats0_)(src, stats_[statsBufferIndex]);
71 }
72
73 void processLine2(uint32_t frame, unsigned int y, const uint8_t *src[], unsigned int statsBufferIndex = 0)
74 {
75 if (frame % kStatPerNumFrames)
76 return;
77
78 if ((y & ySkipMask_) || y < static_cast<unsigned int>(window_.y) ||
79 y >= (window_.y + window_.height))
80 return;
81
82 (this->*stats2_)(src, stats_[statsBufferIndex]);
83 }
84
86
87private:
88 using statsProcessFn = void (SwStatsCpu::*)(const uint8_t *src[], SwIspStats &stats);
89 using processFrameFn = void (SwStatsCpu::*)(MappedFrameBuffer &in);
90
91 int setupStandardBayerOrder(BayerFormat::Order order);
92 /* Bayer 8 bpp unpacked */
93 void statsBGGR8Line0(const uint8_t *src[], SwIspStats &stats);
94 /* Bayer 10 bpp unpacked */
95 void statsBGGR10Line0(const uint8_t *src[], SwIspStats &stats);
96 /* Bayer 12 bpp unpacked */
97 void statsBGGR12Line0(const uint8_t *src[], SwIspStats &stats);
98 /* Bayer 10 bpp packed */
99 void statsBGGR10PLine0(const uint8_t *src[], SwIspStats &stats);
100 void statsGBRG10PLine0(const uint8_t *src[], SwIspStats &stats);
101
102 void processBayerFrame2(MappedFrameBuffer &in);
103
104 processFrameFn processFrame_;
105
106 /* Variables set by configure(), used every line */
107 statsProcessFn stats0_;
108 statsProcessFn stats2_;
109 bool swapLines_;
110
111 unsigned int ySkipMask_;
112
113 Rectangle window_;
114
115 Size patternSize_;
116
117 unsigned int xShift_;
118 unsigned int stride_;
119
120 std::vector<SwIspStats> stats_;
121 SharedMemObject<SwIspStats> sharedStats_;
122 Benchmark bench_;
123};
124
125} /* 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
Simple builtin benchmark.
Definition benchmark.h:22
Frame buffer data and its associated dynamic metadata.
Definition framebuffer.h:50
Support for global libcamera configuration.
Definition global_configuration.h:22
Map a FrameBuffer using the MappedBuffer interface.
Definition mapped_framebuffer.h:47
Describe a rectangle's position and dimensions.
Definition geometry.h:247
int y
The vertical coordinate of the rectangle's top-left corner.
Definition geometry.h:279
unsigned int height
The distance between the top and bottom sides.
Definition geometry.h:281
RAII-style wrapper for file descriptors.
Definition shared_fd.h:17
Helper class to allocate an object in shareable memory.
Definition shared_mem_object.h:60
Generic signal and slot communication mechanism.
Definition signal.h:39
Describe a two-dimensional size.
Definition geometry.h:51
Class for gathering statistics on the CPU.
Definition swstats_cpu.h:36
const SharedFD & getStatsFD()
Get the file descriptor for the statistics.
Definition swstats_cpu.h:51
void processLine2(uint32_t frame, unsigned int y, const uint8_t *src[], unsigned int statsBufferIndex=0)
Process line 2 and 3.
Definition swstats_cpu.h:73
void finishFrame(uint32_t frame, uint32_t bufferId)
Finish statistics calculation for the current frame.
Definition swstats_cpu.cpp:353
void setWindow(const Rectangle &window)
Specify window coordinates over which to gather statistics.
Definition swstats_cpu.cpp:494
int configure(const StreamConfiguration &inputCfg, unsigned int statsBufferCount=1)
Configure the statistics object for the passed in input format.
Definition swstats_cpu.cpp:414
void processLine0(uint32_t frame, unsigned int y, const uint8_t *src[], unsigned int statsBufferIndex=0)
Process line 0.
Definition swstats_cpu.h:61
Signal< uint32_t, uint32_t > statsReady
Signals that the statistics are ready.
Definition swstats_cpu.h:85
bool isValid() const
Gets whether the statistics object is valid.
Definition swstats_cpu.h:49
void processFrame(uint32_t frame, uint32_t bufferId, FrameBuffer *input)
Calculate statistics for a frame in one go.
Definition swstats_cpu.cpp:540
static constexpr uint32_t kStatPerNumFrames
Run stats once every kStatPerNumFrames frames.
Definition swstats_cpu.h:47
const Size & patternSize()
Get the pattern size.
Definition swstats_cpu.h:53
void startFrame(uint32_t frame)
Reset state to start statistics gathering for a new frame.
Definition swstats_cpu.cpp:332
Data structures related to geometric objects.
Internal frame buffer handling support.
Top-level libcamera namespace.
Definition backtrace.h:17
Signal & slot implementation.
Configuration parameters for a stream.
Definition stream.h:40
Struct that holds the statistics for the Software ISP.
Definition swisp_stats.h:24