OpenShot Library | libopenshot  0.4.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 
27 #include "ChannelLayouts.h"
28 #include "Fraction.h"
29 
30 #include <QColor>
31 #include <QImage>
32 
33 class QApplication;
34 
35 namespace juce {
36  template <typename Type> class AudioBuffer;
37 }
38 
39 namespace openshot
40 {
41  class AudioBufferSource;
42  class AudioResampler;
88  class Frame
89  {
90  private:
91  std::shared_ptr<QImage> image;
92  std::shared_ptr<QImage> wave_image;
93 
94  std::shared_ptr<QApplication> previewApp;
95  std::recursive_mutex addingImageMutex;
96  std::recursive_mutex addingAudioMutex;
97  openshot::Fraction pixel_ratio;
98  int channels;
99  ChannelLayout channel_layout;
100  int width;
101  int height;
102  int sample_rate;
103  std::string color;
104  int64_t max_audio_sample;
105  bool audio_reversed;
106 
107 #ifdef USE_OPENCV
108  cv::Mat imagecv;
109 #endif
110 
112  int constrain(int color_value);
113 
114  public:
115  std::shared_ptr<juce::AudioBuffer<float>> audio;
116  int64_t number;
119 
120 
122  Frame();
123 
125  Frame(int64_t number, int width, int height, std::string color);
126 
128  Frame(int64_t number, int samples, int channels);
129 
131  Frame(int64_t number, int width, int height, std::string color, int samples, int channels);
132 
134  Frame ( const Frame &other );
135 
137  Frame& operator= (const Frame& other);
138 
140  virtual ~Frame();
141 
143  void AddColor(int new_width, int new_height, std::string new_color);
144 
146  void AddColor(const QColor& new_color);
147 
149  void AddImage(int new_width, int new_height, int bytes_per_pixel, QImage::Format type, const unsigned char *pixels_);
150 
152  void AddImage(std::shared_ptr<QImage> new_image);
153 
155  void AddImage(std::shared_ptr<QImage> new_image, bool only_odd_lines);
156 
158  void AddAudio(bool replaceSamples, int destChannel, int destStartSample, const float* source, int numSamples, float gainToApplyToSource);
159 
161  void AddAudioSilence(int numSamples);
162 
164  void ApplyGainRamp(int destChannel, int destStartSample, int numSamples, float initial_gain, float final_gain);
165 
169 
170  // Set the channel layout of audio samples (i.e. mono, stereo, 5 point surround, etc...)
171  void ChannelsLayout(openshot::ChannelLayout new_channel_layout) { channel_layout = new_channel_layout; };
172 
174  void ClearWaveform();
175 
177  void DeepCopy(const Frame& other);
178 
180  void Display();
181 
183  void DisplayWaveform();
184 
186  float GetAudioSample(int channel, int sample, int magnitude_range);
187 
189  float* GetAudioSamples(int channel);
190 
192  float* GetInterleavedAudioSamples(int* sample_count);
193 
195  int GetAudioChannelsCount();
196 
198  int GetAudioSamplesCount();
199 
201 
203  int64_t GetBytes();
204 
206  std::shared_ptr<QImage> GetImage();
207 
209  openshot::Fraction GetPixelRatio() { return pixel_ratio; };
210 
212  const unsigned char* GetPixels();
213 
215  const unsigned char* GetPixels(int row);
216 
218  bool CheckPixel(int row, int col, int red, int green, int blue, int alpha, int threshold);
219 
221  int GetHeight();
222 
224  int GetSamplesPerFrame(openshot::Fraction fps, int sample_rate, int channels);
225 
227  static int GetSamplesPerFrame(int64_t frame_number, openshot::Fraction fps, int sample_rate, int channels);
228 
230  std::shared_ptr<QImage> GetWaveform(int width, int height, int Red, int Green, int Blue, int Alpha);
231 
233  const unsigned char* GetWaveformPixels(int width, int height, int Red, int Green, int Blue, int Alpha);
234 
236  int GetWidth();
237 
239  void ResizeAudio(int channels, int length, int sample_rate, openshot::ChannelLayout channel_layout);
240 
242  int SampleRate();
243 
245  void SampleRate(int orig_sample_rate) { sample_rate = orig_sample_rate; };
246 
249  void ReverseAudio();
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::Frame::GetWaveformPixels
const unsigned char * GetWaveformPixels(int width, int height, int Red, int Green, int Blue, int Alpha)
Get an audio waveform image pixels.
Definition: Frame.cpp:257
openshot::Frame::SampleRate
int SampleRate()
Get the original sample rate of this frame's audio data.
Definition: Frame.cpp:501
ChannelLayouts.h
Header file for ChannelLayout class.
openshot::Frame::operator=
Frame & operator=(const Frame &other)
Assignment operator.
Definition: Frame.cpp:77
Fraction.h
Header file for Fraction class.
openshot::Frame::GetAudioSamples
float * GetAudioSamples(int channel)
Get an array of sample data (and optional reverse the sample values)
Definition: Frame.cpp:313
openshot::Frame::SetFrameNumber
void SetFrameNumber(int64_t number)
Set frame number.
Definition: Frame.cpp:449
openshot::Frame::has_audio_data
bool has_audio_data
This frame has been loaded with audio data.
Definition: Frame.h:117
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
juce::AudioBuffer
Definition: Frame.h:36
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:30
openshot::Frame::GetPixelRatio
openshot::Fraction GetPixelRatio()
Set Pixel Aspect Ratio.
Definition: Frame.h:209
openshot::Frame::Thumbnail
void Thumbnail(std::string path, int new_width, int new_height, std::string mask_path, std::string overlay_path, std::string background_color, bool ignore_aspect, std::string format="png", int quality=100, float rotate=0.0)
Definition: Frame.cpp:542
openshot::Frame
This class represents a single frame of video (i.e. image & audio data)
Definition: Frame.h:88
openshot::Frame::ChannelsLayout
void ChannelsLayout(openshot::ChannelLayout new_channel_layout)
Definition: Frame.h:171
openshot::Frame::has_image_data
bool has_image_data
This frame has been loaded with pixel data.
Definition: Frame.h:118
openshot::Frame::AddAudioSilence
void AddAudioSilence(int numSamples)
Add audio silence.
Definition: Frame.cpp:986
openshot::Frame::GetBytes
int64_t GetBytes()
Get the size in bytes of this frame (rough estimate)
Definition: Frame.cpp:379
openshot::Frame::ResizeAudio
void ResizeAudio(int channels, int length, int sample_rate, openshot::ChannelLayout channel_layout)
Resize audio container to hold more (or less) samples and channels.
Definition: Frame.cpp:791
openshot::Frame::ReverseAudio
void ReverseAudio()
Definition: Frame.cpp:806
openshot::Frame::GetPixels
const unsigned char * GetPixels()
Get pixel data (as packets)
Definition: Frame.cpp:396
openshot::Frame::AddImage
void AddImage(int new_width, int new_height, int bytes_per_pixel, QImage::Format type, const unsigned char *pixels_)
Add (or replace) pixel data to the frame.
Definition: Frame.cpp:699
juce
Definition: Robotization.h:29
openshot::Frame::ApplyGainRamp
void ApplyGainRamp(int destChannel, int destStartSample, int numSamples, float initial_gain, float final_gain)
Apply gain ramp (i.e. fading volume)
Definition: Frame.cpp:846
openshot::Frame::GetAudioChannelsCount
int GetAudioChannelsCount()
Get number of audio channels.
Definition: Frame.cpp:357
openshot::Frame::GetHeight
int GetHeight()
Get height of image.
Definition: Frame.cpp:489
path
path
Definition: FFmpegWriter.cpp:1476
openshot::Frame::GetSamplesPerFrame
int GetSamplesPerFrame(openshot::Fraction fps, int sample_rate, int channels)
Calculate the # of samples per video frame (for the current frame number)
Definition: Frame.cpp:483
openshot::Frame::GetInterleavedAudioSamples
float * GetInterleavedAudioSamples(int *sample_count)
Get an array of sample data (all channels interleaved together), using any sample rate.
Definition: Frame.cpp:323
openshot::Frame::CheckPixel
bool CheckPixel(int row, int col, int red, int green, int blue, int alpha, int threshold)
Check a specific pixel color value (returns True/False)
Definition: Frame.cpp:420
openshot::Frame::GetWaveform
std::shared_ptr< QImage > GetWaveform(int width, int height, int Red, int Green, int Blue, int Alpha)
Get an audio waveform image.
Definition: Frame.cpp:158
openshot::Frame::AddAudio
void AddAudio(bool replaceSamples, int destChannel, int destStartSample, const float *source, int numSamples, float gainToApplyToSource)
Add audio samples to a specific channel.
Definition: Frame.cpp:815
openshot::Frame::SampleRate
void SampleRate(int orig_sample_rate)
Set the original sample rate of this frame's audio data.
Definition: Frame.h:245
openshot::Frame::GetWidth
int GetWidth()
Get height of image.
Definition: Frame.cpp:495
openshot::Frame::audio
std::shared_ptr< juce::AudioBuffer< float > > audio
Definition: Frame.h:115
openshot::Frame::ClearWaveform
void ClearWaveform()
Clear the waveform image (and deallocate its memory)
Definition: Frame.cpp:250
openshot::Frame::SetPixelRatio
void SetPixelRatio(int num, int den)
Set Pixel Aspect Ratio.
Definition: Frame.cpp:442
openshot::Frame::Save
void Save(std::string path, float scale, std::string format="PNG", int quality=100)
Save the frame image to the specified path. The image format can be BMP, JPG, JPEG,...
Definition: Frame.cpp:514
openshot::Frame::GetImage
std::shared_ptr< QImage > GetImage()
Get pointer to Qt QImage image object.
Definition: Frame.cpp:855
openshot::Frame::GetImageCV
cv::Mat GetImageCV()
Get pointer to OpenCV Mat image object.
Definition: Frame.cpp:879
openshot::Frame::ChannelsLayout
openshot::ChannelLayout ChannelsLayout()
Definition: Frame.cpp:507
openshot::Frame::GetAudioSampleBuffer
juce::AudioBuffer< float > * GetAudioSampleBuffer()
Definition: Frame.cpp:373
openshot::ChannelLayout
ChannelLayout
This enumeration determines the audio channel layout (such as stereo, mono, 5 point surround,...
Definition: ChannelLayouts.h:28
openshot::Frame::Play
void Play()
Play audio samples for this frame.
Definition: Frame.cpp:915
openshot::Frame::DeepCopy
void DeepCopy(const Frame &other)
Copy data and pointers from another Frame instance.
Definition: Frame.cpp:86
openshot::Frame::GetAudioSample
float GetAudioSample(int channel, int sample, int magnitude_range)
Get magnitude of range of samples (if channel is -1, return average of all channels for that sample)
Definition: Frame.cpp:300
openshot::Frame::number
int64_t number
This is the frame number (starting at 1)
Definition: Frame.h:116
openshot::Frame::Mat2Qimage
std::shared_ptr< QImage > Mat2Qimage(cv::Mat img)
Convert OpenCV Mat to QImage.
Definition: Frame.cpp:893
openshot::Frame::SetImageCV
void SetImageCV(cv::Mat _image)
Set pointer to OpenCV image object.
Definition: Frame.cpp:907
openshot::Frame::Display
void Display()
Display the frame image to the screen (primarily used for debugging reasons)
Definition: Frame.cpp:119
openshot::Frame::~Frame
virtual ~Frame()
Destructor.
Definition: Frame.cpp:109
openshot::Frame::Qimage2mat
cv::Mat Qimage2mat(std::shared_ptr< QImage > &qimage)
Convert Qimage to Mat.
Definition: Frame.cpp:868
openshot::Frame::Frame
Frame()
Constructor - blank frame.
Definition: Frame.cpp:58
openshot::Frame::GetAudioSamplesCount
int GetAudioSamplesCount()
Get number of audio samples.
Definition: Frame.cpp:367
openshot::Frame::AddColor
void AddColor(int new_width, int new_height, std::string new_color)
Add (or replace) pixel data to the frame (based on a solid color)
Definition: Frame.cpp:676
openshot::Frame::DisplayWaveform
void DisplayWaveform()
Display the wave form.
Definition: Frame.cpp:267