OpenShot Library | libopenshot  0.7.0
FrameScope.h
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2026 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #ifndef OPENSHOT_FRAMESCOPE_H
14 #define OPENSHOT_FRAMESCOPE_H
15 
16 #include "Frame.h"
17 #include "Json.h"
18 
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 namespace openshot {
25 
38  class FrameScope {
39  private:
40  std::shared_ptr<Frame> frame;
41  int waveform_columns;
42  int audio_buckets;
43  int vectorscope_size;
44  bool roi_enabled;
45  float roi_x;
46  float roi_y;
47  float roi_width;
48  float roi_height;
49 
50  bool video_present;
51  int video_width;
52  int video_height;
53  int waveform_bins;
54  std::vector<int> waveform_column_map;
55  std::vector<size_t> waveform_offset_map; // pre-multiplied: waveform_column_map[x] * waveform_bins
56  int waveform_column_map_width;
57  int waveform_column_map_columns;
58  double avg_luma;
59  int clipped_shadows;
60  int clipped_highlights;
61  int clipped_red;
62  int clipped_green;
63  int clipped_blue;
64  std::vector<uint32_t> histogram_luma;
65  std::vector<uint32_t> histogram_red;
66  std::vector<uint32_t> histogram_green;
67  std::vector<uint32_t> histogram_blue;
68  std::vector<uint32_t> waveform_luma;
69  std::vector<uint32_t> waveform_red;
70  std::vector<uint32_t> waveform_green;
71  std::vector<uint32_t> waveform_blue;
72  std::vector<uint32_t> vectorscope;
73 
74  bool audio_present;
75  int audio_channels;
76  int audio_samples;
77  int audio_sample_rate;
78  std::vector<float> audio_peak;
79  std::vector<float> audio_rms;
80  std::vector<uint32_t> audio_clipped_samples;
81  std::vector<std::vector<float>> audio_waveform_min;
82  std::vector<std::vector<float>> audio_waveform_max;
83  mutable Json::Value scope_data;
84  mutable bool json_dirty;
85 
86  void reset();
87  void reset_video();
88  void reset_audio();
89  void ensure_video_buffers();
90  void ensure_audio_buffers();
91  void rebuild_waveform_column_map(int width);
92  void analyze_video();
93  void analyze_audio();
94  void analyze();
95  void rebuild_json() const;
96  static std::vector<int> copy_to_int_vector(const std::vector<uint32_t>& values);
97 
98  public:
100  FrameScope();
101 
103  FrameScope(std::shared_ptr<Frame> frame, int waveform_columns = 256, int audio_buckets = 256, int vectorscope_size = 256);
104 
106  void SetFrame(std::shared_ptr<Frame> new_frame);
107 
109  std::shared_ptr<Frame> GetFrame() const { return frame; }
110 
112  void SetWaveformColumns(int columns);
113 
115  void SetAudioBuckets(int buckets);
116 
118  void SetVectorscopeSize(int size);
119 
121  void SetVideoRegionNormalized(float x, float y, float width, float height);
122 
124  void ClearVideoRegion();
125 
127  bool HasVideoRegion() const { return roi_enabled; }
128 
130  bool HasVideo() const { return video_present; }
131 
133  bool HasAudio() const { return audio_present; }
134 
136  int GetVideoWidth() const { return video_width; }
137 
139  int GetVideoHeight() const { return video_height; }
140 
142  int GetWaveformColumns() const { return waveform_columns; }
143 
145  int GetWaveformBins() const { return waveform_bins; }
146 
148  int GetVectorscopeSize() const { return vectorscope_size; }
149 
151  const std::vector<uint32_t>& GetVideoHistogramLumaBins() const { return histogram_luma; }
152 
154  const std::vector<uint32_t>& GetVideoHistogramRedBins() const { return histogram_red; }
155 
157  const std::vector<uint32_t>& GetVideoHistogramGreenBins() const { return histogram_green; }
158 
160  const std::vector<uint32_t>& GetVideoHistogramBlueBins() const { return histogram_blue; }
161 
163  const std::vector<uint32_t>& GetVideoWaveformLumaBins() const { return waveform_luma; }
164 
166  const std::vector<uint32_t>& GetVideoWaveformRedBins() const { return waveform_red; }
167 
169  const std::vector<uint32_t>& GetVideoWaveformGreenBins() const { return waveform_green; }
170 
172  const std::vector<uint32_t>& GetVideoWaveformBlueBins() const { return waveform_blue; }
173 
175  const std::vector<uint32_t>& GetVideoVectorscopeBins() const { return vectorscope; }
176 
178  std::vector<int> GetVideoVectorscope() const { return copy_to_int_vector(vectorscope); }
179 
181  std::vector<int> GetVideoHistogramLuma() const { return copy_to_int_vector(histogram_luma); }
182 
184  std::vector<int> GetVideoHistogramRed() const { return copy_to_int_vector(histogram_red); }
185 
187  std::vector<int> GetVideoHistogramGreen() const { return copy_to_int_vector(histogram_green); }
188 
190  std::vector<int> GetVideoHistogramBlue() const { return copy_to_int_vector(histogram_blue); }
191 
193  std::vector<int> GetVideoWaveformLuma() const { return copy_to_int_vector(waveform_luma); }
194 
196  std::vector<int> GetVideoWaveformRed() const { return copy_to_int_vector(waveform_red); }
197 
199  std::vector<int> GetVideoWaveformGreen() const { return copy_to_int_vector(waveform_green); }
200 
202  std::vector<int> GetVideoWaveformBlue() const { return copy_to_int_vector(waveform_blue); }
203 
205  double GetVideoAverageLuma() const { return avg_luma; }
206 
208  int GetVideoClippedShadows() const { return clipped_shadows; }
209 
211  int GetVideoClippedHighlights() const { return clipped_highlights; }
212 
214  int GetVideoClippedRed() const { return clipped_red; }
215 
217  int GetVideoClippedGreen() const { return clipped_green; }
218 
220  int GetVideoClippedBlue() const { return clipped_blue; }
221 
223  int GetAudioChannels() const { return audio_channels; }
224 
226  int GetAudioSamples() const { return audio_samples; }
227 
229  int GetAudioSampleRate() const { return audio_sample_rate; }
230 
232  int GetAudioBuckets() const { return audio_buckets; }
233 
235  const std::vector<float>& GetAudioPeakLevelsRef() const { return audio_peak; }
236 
238  const std::vector<float>& GetAudioRmsLevelsRef() const { return audio_rms; }
239 
241  const std::vector<uint32_t>& GetAudioClippedSamplesRef() const { return audio_clipped_samples; }
242 
244  std::vector<float> GetAudioPeakLevels() const { return audio_peak; }
245 
247  std::vector<float> GetAudioRmsLevels() const { return audio_rms; }
248 
250  std::vector<int> GetAudioClippedSamples() const { return copy_to_int_vector(audio_clipped_samples); }
251 
253  std::vector<float> GetAudioWaveformMin(int channel) const;
254 
256  std::vector<float> GetAudioWaveformMax(int channel) const;
257 
259  Json::Value JsonValue() const;
260 
262  std::string Json() const;
263  };
264 
265 }
266 
267 #endif
openshot::FrameScope::ClearVideoRegion
void ClearVideoRegion()
Clear any video ROI and re-analyze the full frame.
Definition: FrameScope.cpp:215
openshot::FrameScope::GetVideoHistogramRedBins
const std::vector< uint32_t > & GetVideoHistogramRedBins() const
Return the red histogram bins by reference.
Definition: FrameScope.h:154
openshot::FrameScope::GetAudioSampleRate
int GetAudioSampleRate() const
Return the analyzed audio sample rate.
Definition: FrameScope.h:229
openshot::FrameScope::GetVideoHistogramGreen
std::vector< int > GetVideoHistogramGreen() const
Return the green histogram bins.
Definition: FrameScope.h:187
openshot::FrameScope::GetAudioClippedSamples
std::vector< int > GetAudioClippedSamples() const
Return per-channel clipped sample counts.
Definition: FrameScope.h:250
openshot::FrameScope::SetFrame
void SetFrame(std::shared_ptr< Frame > new_frame)
Replace the current frame and recompute the scope data.
Definition: FrameScope.cpp:178
openshot::FrameScope::SetVectorscopeSize
void SetVectorscopeSize(int size)
Set the vectorscope plane edge length and re-analyze video.
Definition: FrameScope.cpp:197
openshot::FrameScope::FrameScope
FrameScope()
Create an empty scope analyzer with default bucket sizes.
Definition: FrameScope.cpp:64
openshot::FrameScope::GetVideoClippedGreen
int GetVideoClippedGreen() const
Return the clipped green-channel pixel count.
Definition: FrameScope.h:217
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AnimatedCurve.h:24
openshot::FrameScope::GetWaveformBins
int GetWaveformBins() const
Return the number of vertical waveform bins.
Definition: FrameScope.h:145
openshot::FrameScope::GetAudioRmsLevelsRef
const std::vector< float > & GetAudioRmsLevelsRef() const
Return per-channel RMS levels.
Definition: FrameScope.h:238
openshot::FrameScope::GetVideoHistogramGreenBins
const std::vector< uint32_t > & GetVideoHistogramGreenBins() const
Return the green histogram bins by reference.
Definition: FrameScope.h:157
openshot::FrameScope::GetVideoWaveformLuma
std::vector< int > GetVideoWaveformLuma() const
Return the flattened luma waveform bins.
Definition: FrameScope.h:193
openshot::FrameScope::GetAudioChannels
int GetAudioChannels() const
Return the number of analyzed audio channels.
Definition: FrameScope.h:223
openshot::FrameScope::GetVideoClippedHighlights
int GetVideoClippedHighlights() const
Return the clipped highlight pixel count.
Definition: FrameScope.h:211
openshot::FrameScope::GetVectorscopeSize
int GetVectorscopeSize() const
Return the vectorscope plane edge length.
Definition: FrameScope.h:148
openshot::FrameScope::GetVideoHistogramBlue
std::vector< int > GetVideoHistogramBlue() const
Return the blue histogram bins.
Definition: FrameScope.h:190
openshot::FrameScope::GetVideoHistogramBlueBins
const std::vector< uint32_t > & GetVideoHistogramBlueBins() const
Return the blue histogram bins by reference.
Definition: FrameScope.h:160
openshot::FrameScope::GetVideoHistogramRed
std::vector< int > GetVideoHistogramRed() const
Return the red histogram bins.
Definition: FrameScope.h:184
openshot::FrameScope::GetVideoVectorscopeBins
const std::vector< uint32_t > & GetVideoVectorscopeBins() const
Return the flattened vectorscope density plane by reference.
Definition: FrameScope.h:175
openshot::FrameScope::Json
std::string Json() const
Return the current scope payload as a JSON string.
Definition: FrameScope.cpp:470
openshot::FrameScope::GetAudioRmsLevels
std::vector< float > GetAudioRmsLevels() const
Return per-channel RMS levels.
Definition: FrameScope.h:247
openshot::FrameScope::SetAudioBuckets
void SetAudioBuckets(int buckets)
Set the number of audio buckets and re-analyze.
Definition: FrameScope.cpp:190
openshot::FrameScope::HasAudio
bool HasAudio() const
Return whether the current frame has analyzable audio data.
Definition: FrameScope.h:133
openshot::FrameScope::SetWaveformColumns
void SetWaveformColumns(int columns)
Set the number of horizontal waveform columns and re-analyze.
Definition: FrameScope.cpp:183
openshot::FrameScope::GetVideoHeight
int GetVideoHeight() const
Return the analyzed video height.
Definition: FrameScope.h:139
openshot::FrameScope::GetVideoWaveformBlue
std::vector< int > GetVideoWaveformBlue() const
Return the flattened blue waveform bins.
Definition: FrameScope.h:202
openshot::FrameScope::GetVideoHistogramLumaBins
const std::vector< uint32_t > & GetVideoHistogramLumaBins() const
Return the luma histogram bins by reference.
Definition: FrameScope.h:151
openshot::FrameScope::GetVideoClippedShadows
int GetVideoClippedShadows() const
Return the clipped shadow pixel count.
Definition: FrameScope.h:208
openshot::FrameScope::GetAudioPeakLevelsRef
const std::vector< float > & GetAudioPeakLevelsRef() const
Return per-channel peak levels.
Definition: FrameScope.h:235
openshot::FrameScope
Analyze a single Frame and expose scope-friendly JSON.
Definition: FrameScope.h:38
openshot::FrameScope::GetAudioWaveformMax
std::vector< float > GetAudioWaveformMax(int channel) const
Return one channel of audio waveform maximum values.
Definition: FrameScope.cpp:490
openshot::FrameScope::SetVideoRegionNormalized
void SetVideoRegionNormalized(float x, float y, float width, float height)
Set a normalized ROI for video analysis and re-analyze video.
Definition: FrameScope.cpp:204
openshot::FrameScope::GetVideoClippedBlue
int GetVideoClippedBlue() const
Return the clipped blue-channel pixel count.
Definition: FrameScope.h:220
openshot::FrameScope::GetAudioBuckets
int GetAudioBuckets() const
Return the number of audio waveform buckets.
Definition: FrameScope.h:232
openshot::FrameScope::GetVideoVectorscope
std::vector< int > GetVideoVectorscope() const
Return the flattened vectorscope density plane.
Definition: FrameScope.h:178
Frame.h
Header file for Frame class.
openshot::FrameScope::GetFrame
std::shared_ptr< Frame > GetFrame() const
Return the currently analyzed frame.
Definition: FrameScope.h:109
openshot::FrameScope::HasVideo
bool HasVideo() const
Return whether the current frame has analyzable video data.
Definition: FrameScope.h:130
openshot::FrameScope::GetVideoAverageLuma
double GetVideoAverageLuma() const
Return the average luma of the analyzed frame.
Definition: FrameScope.h:205
openshot::FrameScope::GetVideoWaveformGreen
std::vector< int > GetVideoWaveformGreen() const
Return the flattened green waveform bins.
Definition: FrameScope.h:199
openshot::FrameScope::GetVideoHistogramLuma
std::vector< int > GetVideoHistogramLuma() const
Return the luma histogram bins.
Definition: FrameScope.h:181
openshot::FrameScope::HasVideoRegion
bool HasVideoRegion() const
Return whether a video ROI is enabled.
Definition: FrameScope.h:127
openshot::FrameScope::JsonValue
Json::Value JsonValue() const
Return the current scope payload as a Json::Value tree.
Definition: FrameScope.cpp:464
openshot::FrameScope::GetAudioWaveformMin
std::vector< float > GetAudioWaveformMin(int channel) const
Return one channel of audio waveform minimum values.
Definition: FrameScope.cpp:484
openshot::FrameScope::GetAudioSamples
int GetAudioSamples() const
Return the number of analyzed audio samples.
Definition: FrameScope.h:226
openshot::FrameScope::GetVideoWaveformRedBins
const std::vector< uint32_t > & GetVideoWaveformRedBins() const
Return the flattened red waveform bins by reference.
Definition: FrameScope.h:166
openshot::FrameScope::GetVideoWaveformLumaBins
const std::vector< uint32_t > & GetVideoWaveformLumaBins() const
Return the flattened luma waveform bins by reference.
Definition: FrameScope.h:163
openshot::FrameScope::GetWaveformColumns
int GetWaveformColumns() const
Return the number of waveform columns.
Definition: FrameScope.h:142
openshot::FrameScope::GetVideoWaveformBlueBins
const std::vector< uint32_t > & GetVideoWaveformBlueBins() const
Return the flattened blue waveform bins by reference.
Definition: FrameScope.h:172
openshot::FrameScope::GetVideoWaveformRed
std::vector< int > GetVideoWaveformRed() const
Return the flattened red waveform bins.
Definition: FrameScope.h:196
openshot::FrameScope::GetVideoClippedRed
int GetVideoClippedRed() const
Return the clipped red-channel pixel count.
Definition: FrameScope.h:214
openshot::FrameScope::GetVideoWidth
int GetVideoWidth() const
Return the analyzed video width.
Definition: FrameScope.h:136
Json.h
Header file for JSON class.
openshot::FrameScope::GetAudioClippedSamplesRef
const std::vector< uint32_t > & GetAudioClippedSamplesRef() const
Return per-channel clipped sample counts.
Definition: FrameScope.h:241
openshot::FrameScope::GetVideoWaveformGreenBins
const std::vector< uint32_t > & GetVideoWaveformGreenBins() const
Return the flattened green waveform bins by reference.
Definition: FrameScope.h:169
openshot::FrameScope::GetAudioPeakLevels
std::vector< float > GetAudioPeakLevels() const
Return per-channel peak levels.
Definition: FrameScope.h:244