OpenShot Library | libopenshot  0.3.0
Frame.h
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2019 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #ifndef OPENSHOT_FRAME_H
14 #define OPENSHOT_FRAME_H
15 
16 #ifdef USE_OPENCV
17  #define int64 opencv_broken_int
18  #define uint64 opencv_broken_uint
19  #include <opencv2/imgproc/imgproc.hpp>
20  #undef uint64
21  #undef int64
22 #endif
23 
24 #include <memory>
25 #include <mutex>
26 #include <sstream>
27 #include <queue>
28 
29 #include "ChannelLayouts.h"
30 #include "Fraction.h"
31 
32 #include <QColor>
33 #include <QImage>
34 
35 class QApplication;
36 
37 namespace juce {
38  template <typename Type> class AudioBuffer;
39 }
40 
41 namespace openshot
42 {
43  class AudioBufferSource;
44  class AudioResampler;
90  class Frame
91  {
92  private:
93  std::shared_ptr<QImage> image;
94  std::shared_ptr<QImage> wave_image;
95 
96  std::shared_ptr<QApplication> previewApp;
97  std::recursive_mutex addingImageMutex;
98  std::recursive_mutex addingAudioMutex;
99  openshot::Fraction pixel_ratio;
100  int channels;
101  ChannelLayout channel_layout;
102  int width;
103  int height;
104  int sample_rate;
105  std::string color;
106  int64_t max_audio_sample;
107 
108 #ifdef USE_OPENCV
109  cv::Mat imagecv;
110 #endif
111 
113  int constrain(int color_value);
114 
115  public:
116  std::shared_ptr<juce::AudioBuffer<float>> audio;
117  int64_t number;
120 
121 
123  Frame();
124 
126  Frame(int64_t number, int width, int height, std::string color);
127 
129  Frame(int64_t number, int samples, int channels);
130 
132  Frame(int64_t number, int width, int height, std::string color, int samples, int channels);
133 
135  Frame ( const Frame &other );
136 
138  Frame& operator= (const Frame& other);
139 
141  virtual ~Frame();
142 
144  void AddColor(int new_width, int new_height, std::string new_color);
145 
147  void AddColor(const QColor& new_color);
148 
150  void AddImage(int new_width, int new_height, int bytes_per_pixel, QImage::Format type, const unsigned char *pixels_);
151 
153  void AddImage(std::shared_ptr<QImage> new_image);
154 
156  void AddImage(std::shared_ptr<QImage> new_image, bool only_odd_lines);
157 
159  void AddAudio(bool replaceSamples, int destChannel, int destStartSample, const float* source, int numSamples, float gainToApplyToSource);
160 
162  void AddAudioSilence(int numSamples);
163 
165  void ApplyGainRamp(int destChannel, int destStartSample, int numSamples, float initial_gain, float final_gain);
166 
169  openshot::ChannelLayout ChannelsLayout();
170 
171  // Set the channel layout of audio samples (i.e. mono, stereo, 5 point surround, etc...)
172  void ChannelsLayout(openshot::ChannelLayout new_channel_layout) { channel_layout = new_channel_layout; };
173 
175  void ClearWaveform();
176 
178  void DeepCopy(const Frame& other);
179 
181  void Display();
182 
184  void DisplayWaveform();
185 
187  float GetAudioSample(int channel, int sample, int magnitude_range);
188 
190  float* GetAudioSamples(int channel);
191 
193  float* GetInterleavedAudioSamples(int new_sample_rate, openshot::AudioResampler* resampler, int* sample_count);
194 
195  // Get a planar array of sample data, using any sample rate
196  float* GetPlanarAudioSamples(int new_sample_rate, openshot::AudioResampler* resampler, int* sample_count);
197 
199  int GetAudioChannelsCount();
200 
202  int GetAudioSamplesCount();
203 
204  juce::AudioBuffer<float> *GetAudioSampleBuffer();
205 
207  int64_t GetBytes();
208 
210  std::shared_ptr<QImage> GetImage();
211 
213  openshot::Fraction GetPixelRatio() { return pixel_ratio; };
214 
216  const unsigned char* GetPixels();
217 
219  const unsigned char* GetPixels(int row);
220 
222  bool CheckPixel(int row, int col, int red, int green, int blue, int alpha, int threshold);
223 
225  int GetHeight();
226 
228  int GetSamplesPerFrame(openshot::Fraction fps, int sample_rate, int channels);
229 
231  static int GetSamplesPerFrame(int64_t frame_number, openshot::Fraction fps, int sample_rate, int channels);
232 
234  std::shared_ptr<QImage> GetWaveform(int width, int height, int Red, int Green, int Blue, int Alpha);
235 
237  const unsigned char* GetWaveformPixels(int width, int height, int Red, int Green, int Blue, int Alpha);
238 
240  int GetWidth();
241 
243  void ResizeAudio(int channels, int length, int sample_rate, openshot::ChannelLayout channel_layout);
244 
246  int SampleRate();
247 
249  void SampleRate(int orig_sample_rate) { sample_rate = orig_sample_rate; };
250 
252  void Save(std::string path, float scale, std::string format="PNG", int quality=100);
253 
255  void SetFrameNumber(int64_t number);
256 
258  void SetPixelRatio(int num, int den);
259 
262  void Thumbnail(std::string path, int new_width, int new_height, std::string mask_path, std::string overlay_path,
263  std::string background_color, bool ignore_aspect, std::string format="png", int quality=100, float rotate=0.0);
264 
266  void Play();
267 
268 #ifdef USE_OPENCV
269  cv::Mat Qimage2mat( std::shared_ptr<QImage>& qimage);
271 
273  std::shared_ptr<QImage> Mat2Qimage(cv::Mat img);
274 
276  cv::Mat GetImageCV();
277 
279  void SetImageCV(cv::Mat _image);
280 #endif
281  };
282 
283 }
284 
285 #endif
openshot::Fraction GetPixelRatio()
Set Pixel Aspect Ratio.
Definition: Frame.h:213
Header file for Fraction class.
This class represents a single frame of video (i.e. image & audio data)
Definition: Frame.h:90
int64_t number
This is the frame number (starting at 1)
Definition: Frame.h:117
bool has_audio_data
This frame has been loaded with audio data.
Definition: Frame.h:118
This class represents a fraction.
Definition: Fraction.h:30
Header file for ChannelLayout class.
ChannelLayout
This enumeration determines the audio channel layout (such as stereo, mono, 5 point surround...
void ChannelsLayout(openshot::ChannelLayout new_channel_layout)
Definition: Frame.h:172
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
std::shared_ptr< juce::AudioBuffer< float > > audio
Definition: Frame.h:116
bool has_image_data
This frame has been loaded with pixel data.
Definition: Frame.h:119
void SampleRate(int orig_sample_rate)
Set the original sample rate of this frame&#39;s audio data.
Definition: Frame.h:249
This class is used to resample audio data for many sequential frames.