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  int waveform_column_map_width;
56  int waveform_column_map_columns;
57  double avg_luma;
58  int clipped_shadows;
59  int clipped_highlights;
60  int clipped_red;
61  int clipped_green;
62  int clipped_blue;
63  std::vector<uint32_t> histogram_luma;
64  std::vector<uint32_t> histogram_red;
65  std::vector<uint32_t> histogram_green;
66  std::vector<uint32_t> histogram_blue;
67  std::vector<uint32_t> waveform_luma;
68  std::vector<uint32_t> waveform_red;
69  std::vector<uint32_t> waveform_green;
70  std::vector<uint32_t> waveform_blue;
71  std::vector<uint32_t> vectorscope;
72 
73  bool audio_present;
74  int audio_channels;
75  int audio_samples;
76  int audio_sample_rate;
77  std::vector<float> audio_peak;
78  std::vector<float> audio_rms;
79  std::vector<uint32_t> audio_clipped_samples;
80  std::vector<std::vector<float>> audio_waveform_min;
81  std::vector<std::vector<float>> audio_waveform_max;
82  mutable Json::Value scope_data;
83  mutable bool json_dirty;
84 
85  void reset();
86  void reset_video();
87  void reset_audio();
88  void ensure_video_buffers();
89  void ensure_audio_buffers();
90  void rebuild_waveform_column_map(int width);
91  void analyze_video();
92  void analyze_audio();
93  void analyze();
94  void rebuild_json() const;
95  static std::vector<int> copy_to_int_vector(const std::vector<uint32_t>& values);
96 
97  public:
99  FrameScope();
100 
102  FrameScope(std::shared_ptr<Frame> frame, int waveform_columns = 256, int audio_buckets = 256, int vectorscope_size = 256);
103 
105  void SetFrame(std::shared_ptr<Frame> new_frame);
106 
108  std::shared_ptr<Frame> GetFrame() const { return frame; }
109 
111  void SetWaveformColumns(int columns);
112 
114  void SetAudioBuckets(int buckets);
115 
117  void SetVectorscopeSize(int size);
118 
120  void SetVideoRegionNormalized(float x, float y, float width, float height);
121 
123  void ClearVideoRegion();
124 
126  bool HasVideoRegion() const { return roi_enabled; }
127 
129  bool HasVideo() const { return video_present; }
130 
132  bool HasAudio() const { return audio_present; }
133 
135  int GetVideoWidth() const { return video_width; }
136 
138  int GetVideoHeight() const { return video_height; }
139 
141  int GetWaveformColumns() const { return waveform_columns; }
142 
144  int GetWaveformBins() const { return waveform_bins; }
145 
147  int GetVectorscopeSize() const { return vectorscope_size; }
148 
150  const std::vector<uint32_t>& GetVideoHistogramLumaBins() const { return histogram_luma; }
151 
153  const std::vector<uint32_t>& GetVideoHistogramRedBins() const { return histogram_red; }
154 
156  const std::vector<uint32_t>& GetVideoHistogramGreenBins() const { return histogram_green; }
157 
159  const std::vector<uint32_t>& GetVideoHistogramBlueBins() const { return histogram_blue; }
160 
162  const std::vector<uint32_t>& GetVideoWaveformLumaBins() const { return waveform_luma; }
163 
165  const std::vector<uint32_t>& GetVideoWaveformRedBins() const { return waveform_red; }
166 
168  const std::vector<uint32_t>& GetVideoWaveformGreenBins() const { return waveform_green; }
169 
171  const std::vector<uint32_t>& GetVideoWaveformBlueBins() const { return waveform_blue; }
172 
174  const std::vector<uint32_t>& GetVideoVectorscopeBins() const { return vectorscope; }
175 
177  std::vector<int> GetVideoVectorscope() const { return copy_to_int_vector(vectorscope); }
178 
180  std::vector<int> GetVideoHistogramLuma() const { return copy_to_int_vector(histogram_luma); }
181 
183  std::vector<int> GetVideoHistogramRed() const { return copy_to_int_vector(histogram_red); }
184 
186  std::vector<int> GetVideoHistogramGreen() const { return copy_to_int_vector(histogram_green); }
187 
189  std::vector<int> GetVideoHistogramBlue() const { return copy_to_int_vector(histogram_blue); }
190 
192  std::vector<int> GetVideoWaveformLuma() const { return copy_to_int_vector(waveform_luma); }
193 
195  std::vector<int> GetVideoWaveformRed() const { return copy_to_int_vector(waveform_red); }
196 
198  std::vector<int> GetVideoWaveformGreen() const { return copy_to_int_vector(waveform_green); }
199 
201  std::vector<int> GetVideoWaveformBlue() const { return copy_to_int_vector(waveform_blue); }
202 
204  double GetVideoAverageLuma() const { return avg_luma; }
205 
207  int GetVideoClippedShadows() const { return clipped_shadows; }
208 
210  int GetVideoClippedHighlights() const { return clipped_highlights; }
211 
213  int GetVideoClippedRed() const { return clipped_red; }
214 
216  int GetVideoClippedGreen() const { return clipped_green; }
217 
219  int GetVideoClippedBlue() const { return clipped_blue; }
220 
222  int GetAudioChannels() const { return audio_channels; }
223 
225  int GetAudioSamples() const { return audio_samples; }
226 
228  int GetAudioSampleRate() const { return audio_sample_rate; }
229 
231  int GetAudioBuckets() const { return audio_buckets; }
232 
234  const std::vector<float>& GetAudioPeakLevelsRef() const { return audio_peak; }
235 
237  const std::vector<float>& GetAudioRmsLevelsRef() const { return audio_rms; }
238 
240  const std::vector<uint32_t>& GetAudioClippedSamplesRef() const { return audio_clipped_samples; }
241 
243  std::vector<float> GetAudioPeakLevels() const { return audio_peak; }
244 
246  std::vector<float> GetAudioRmsLevels() const { return audio_rms; }
247 
249  std::vector<int> GetAudioClippedSamples() const { return copy_to_int_vector(audio_clipped_samples); }
250 
252  std::vector<float> GetAudioWaveformMin(int channel) const;
253 
255  std::vector<float> GetAudioWaveformMax(int channel) const;
256 
258  Json::Value JsonValue() const;
259 
261  std::string Json() const;
262  };
263 
264 }
265 
266 #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:153
openshot::FrameScope::GetAudioSampleRate
int GetAudioSampleRate() const
Return the analyzed audio sample rate.
Definition: FrameScope.h:228
openshot::FrameScope::GetVideoHistogramGreen
std::vector< int > GetVideoHistogramGreen() const
Return the green histogram bins.
Definition: FrameScope.h:186
openshot::FrameScope::GetAudioClippedSamples
std::vector< int > GetAudioClippedSamples() const
Return per-channel clipped sample counts.
Definition: FrameScope.h:249
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:68
openshot::FrameScope::GetVideoClippedGreen
int GetVideoClippedGreen() const
Return the clipped green-channel pixel count.
Definition: FrameScope.h:216
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:144
openshot::FrameScope::GetAudioRmsLevelsRef
const std::vector< float > & GetAudioRmsLevelsRef() const
Return per-channel RMS levels.
Definition: FrameScope.h:237
openshot::FrameScope::GetVideoHistogramGreenBins
const std::vector< uint32_t > & GetVideoHistogramGreenBins() const
Return the green histogram bins by reference.
Definition: FrameScope.h:156
openshot::FrameScope::GetVideoWaveformLuma
std::vector< int > GetVideoWaveformLuma() const
Return the flattened luma waveform bins.
Definition: FrameScope.h:192
openshot::FrameScope::GetAudioChannels
int GetAudioChannels() const
Return the number of analyzed audio channels.
Definition: FrameScope.h:222
openshot::FrameScope::GetVideoClippedHighlights
int GetVideoClippedHighlights() const
Return the clipped highlight pixel count.
Definition: FrameScope.h:210
openshot::FrameScope::GetVectorscopeSize
int GetVectorscopeSize() const
Return the vectorscope plane edge length.
Definition: FrameScope.h:147
openshot::FrameScope::GetVideoHistogramBlue
std::vector< int > GetVideoHistogramBlue() const
Return the blue histogram bins.
Definition: FrameScope.h:189
openshot::FrameScope::GetVideoHistogramBlueBins
const std::vector< uint32_t > & GetVideoHistogramBlueBins() const
Return the blue histogram bins by reference.
Definition: FrameScope.h:159
openshot::FrameScope::GetVideoHistogramRed
std::vector< int > GetVideoHistogramRed() const
Return the red histogram bins.
Definition: FrameScope.h:183
openshot::FrameScope::GetVideoVectorscopeBins
const std::vector< uint32_t > & GetVideoVectorscopeBins() const
Return the flattened vectorscope density plane by reference.
Definition: FrameScope.h:174
openshot::FrameScope::Json
std::string Json() const
Return the current scope payload as a JSON string.
Definition: FrameScope.cpp:463
openshot::FrameScope::GetAudioRmsLevels
std::vector< float > GetAudioRmsLevels() const
Return per-channel RMS levels.
Definition: FrameScope.h:246
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:132
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:138
openshot::FrameScope::GetVideoWaveformBlue
std::vector< int > GetVideoWaveformBlue() const
Return the flattened blue waveform bins.
Definition: FrameScope.h:201
openshot::FrameScope::GetVideoHistogramLumaBins
const std::vector< uint32_t > & GetVideoHistogramLumaBins() const
Return the luma histogram bins by reference.
Definition: FrameScope.h:150
openshot::FrameScope::GetVideoClippedShadows
int GetVideoClippedShadows() const
Return the clipped shadow pixel count.
Definition: FrameScope.h:207
openshot::FrameScope::GetAudioPeakLevelsRef
const std::vector< float > & GetAudioPeakLevelsRef() const
Return per-channel peak levels.
Definition: FrameScope.h:234
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:483
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:219
openshot::FrameScope::GetAudioBuckets
int GetAudioBuckets() const
Return the number of audio waveform buckets.
Definition: FrameScope.h:231
openshot::FrameScope::GetVideoVectorscope
std::vector< int > GetVideoVectorscope() const
Return the flattened vectorscope density plane.
Definition: FrameScope.h:177
Frame.h
Header file for Frame class.
openshot::FrameScope::GetFrame
std::shared_ptr< Frame > GetFrame() const
Return the currently analyzed frame.
Definition: FrameScope.h:108
openshot::FrameScope::HasVideo
bool HasVideo() const
Return whether the current frame has analyzable video data.
Definition: FrameScope.h:129
openshot::FrameScope::GetVideoAverageLuma
double GetVideoAverageLuma() const
Return the average luma of the analyzed frame.
Definition: FrameScope.h:204
openshot::FrameScope::GetVideoWaveformGreen
std::vector< int > GetVideoWaveformGreen() const
Return the flattened green waveform bins.
Definition: FrameScope.h:198
openshot::FrameScope::GetVideoHistogramLuma
std::vector< int > GetVideoHistogramLuma() const
Return the luma histogram bins.
Definition: FrameScope.h:180
openshot::FrameScope::HasVideoRegion
bool HasVideoRegion() const
Return whether a video ROI is enabled.
Definition: FrameScope.h:126
openshot::FrameScope::JsonValue
Json::Value JsonValue() const
Return the current scope payload as a Json::Value tree.
Definition: FrameScope.cpp:457
openshot::FrameScope::GetAudioWaveformMin
std::vector< float > GetAudioWaveformMin(int channel) const
Return one channel of audio waveform minimum values.
Definition: FrameScope.cpp:477
openshot::FrameScope::GetAudioSamples
int GetAudioSamples() const
Return the number of analyzed audio samples.
Definition: FrameScope.h:225
openshot::FrameScope::GetVideoWaveformRedBins
const std::vector< uint32_t > & GetVideoWaveformRedBins() const
Return the flattened red waveform bins by reference.
Definition: FrameScope.h:165
openshot::FrameScope::GetVideoWaveformLumaBins
const std::vector< uint32_t > & GetVideoWaveformLumaBins() const
Return the flattened luma waveform bins by reference.
Definition: FrameScope.h:162
openshot::FrameScope::GetWaveformColumns
int GetWaveformColumns() const
Return the number of waveform columns.
Definition: FrameScope.h:141
openshot::FrameScope::GetVideoWaveformBlueBins
const std::vector< uint32_t > & GetVideoWaveformBlueBins() const
Return the flattened blue waveform bins by reference.
Definition: FrameScope.h:171
openshot::FrameScope::GetVideoWaveformRed
std::vector< int > GetVideoWaveformRed() const
Return the flattened red waveform bins.
Definition: FrameScope.h:195
openshot::FrameScope::GetVideoClippedRed
int GetVideoClippedRed() const
Return the clipped red-channel pixel count.
Definition: FrameScope.h:213
openshot::FrameScope::GetVideoWidth
int GetVideoWidth() const
Return the analyzed video width.
Definition: FrameScope.h:135
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:240
openshot::FrameScope::GetVideoWaveformGreenBins
const std::vector< uint32_t > & GetVideoWaveformGreenBins() const
Return the flattened green waveform bins by reference.
Definition: FrameScope.h:168
openshot::FrameScope::GetAudioPeakLevels
std::vector< float > GetAudioPeakLevels() const
Return per-channel peak levels.
Definition: FrameScope.h:243