OpenShot Library | libopenshot  0.1.1
Frame.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Header file for Frame class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @section LICENSE
7  *
8  * Copyright (c) 2008-2014 OpenShot Studios, LLC
9  * <http://www.openshotstudios.com/>. This file is part of
10  * OpenShot Library (libopenshot), an open-source project dedicated to
11  * delivering high quality video editing and animation solutions to the
12  * world. For more information visit <http://www.openshot.org/>.
13  *
14  * OpenShot Library (libopenshot) is free software: you can redistribute it
15  * and/or modify it under the terms of the GNU Lesser General Public License
16  * as published by the Free Software Foundation, either version 3 of the
17  * License, or (at your option) any later version.
18  *
19  * OpenShot Library (libopenshot) is distributed in the hope that it will be
20  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 #ifndef OPENSHOT_FRAME_H
29 #define OPENSHOT_FRAME_H
30 
31 /// Do not include the juce unittest headers, because it collides with unittest++
32 #ifndef __JUCE_UNITTEST_JUCEHEADER__
33  #define __JUCE_UNITTEST_JUCEHEADER__
34 #endif
35 #ifndef _NDEBUG
36  // Define NO debug for JUCE on mac os
37  #define _NDEBUG
38 #endif
39 
40 #include <iomanip>
41 #include <sstream>
42 #include <queue>
43 #include <QtWidgets/QApplication>
44 #include <QtGui/QImage>
45 #include <QtGui/QColor>
46 #include <QtGui/QBitmap>
47 #include <QtCore/QString>
48 #include <QtCore/QVector>
49 #include <QtGui/QPainter>
50 #include <QtWidgets/QHBoxLayout>
51 #include <QtWidgets/QWidget>
52 #include <QtWidgets/QLabel>
53 #include <tr1/memory>
54 #include <unistd.h>
55 #ifdef USE_IMAGEMAGICK
56  #include "Magick++.h"
57 #endif
58 #include "JuceLibraryCode/JuceHeader.h"
59 #include "ChannelLayouts.h"
60 #include "AudioBufferSource.h"
61 #include "AudioResampler.h"
62 #include "Fraction.h"
63 
64 
65 using namespace std;
66 
67 namespace openshot
68 {
69  /**
70  * @brief This class represents a single frame of video (i.e. image & audio data)
71  *
72  * FileReaders (such as FFmpegReader) use instances of this class to store the individual frames of video,
73  * which include both the image data (i.e. pixels) and audio samples. An openshot::Frame also has many debug
74  * methods, such as the ability to display the image (using X11), play the audio samples (using JUCE), or
75  * display the audio waveform as an image.
76  *
77  * FileWriters (such as FFmpegWriter) use instances of this class to create new video files, image files, or
78  * video streams. So, think of these openshot::Frame instances as the smallest unit of work in a video
79  * editor.
80  *
81  * There are many ways to create an instance of an openshot::Frame:
82  * @code
83  *
84  * // Most basic: a blank frame (300x200 blank image, 48kHz audio silence)
85  * Frame();
86  *
87  * // Image only settings (48kHz audio silence)
88  * Frame(1, // Frame number
89  * 720, // Width of image
90  * 480, // Height of image
91  * "#000000" // HTML color code of background color
92  * );
93  *
94  * // Audio only (300x200 blank image)
95  * Frame(number, // Frame number
96  * 44100, // Sample rate of audio stream
97  * 2 // Number of audio channels
98  * );
99  *
100  * // Image and Audio settings (user defines all key settings)
101  * Frame(number, // Frame number
102  * 720, // Width of image
103  * 480, // Height of image
104  * "#000000" // HTML color code of background color
105  * 44100, // Sample rate of audio stream
106  * 2 // Number of audio channels
107  * );
108  *
109  * // Some methods require a shared pointer to an openshot::Frame object.
110  * tr1::shared_ptr<Frame> f(new Frame(1, 720, 480, "#000000", 44100, 2));
111  *
112  * @endcode
113  */
114  class Frame
115  {
116  private:
117  tr1::shared_ptr<QImage> image;
118  tr1::shared_ptr<QImage> wave_image;
119  tr1::shared_ptr<juce::AudioSampleBuffer> audio;
120  tr1::shared_ptr<QApplication> previewApp;
121  CriticalSection addingImageSection;
122  const unsigned char *qbuffer;
123  Fraction pixel_ratio;
124  int channels;
125  ChannelLayout channel_layout;
126  int width;
127  int height;
128  int sample_rate;
129 
130  /// Constrain a color value from 0 to 255
131  int constrain(int color_value);
132 
133  public:
134  long int number; ///< This is the frame number (starting at 1)
135  bool has_audio_data; ///< This frame has been loaded with audio data
136  bool has_image_data; ///< This frame has been loaded with pixel data
137 
138  /// Constructor - blank frame (300x200 blank image, 48kHz audio silence)
139  Frame();
140 
141  /// Constructor - image only (48kHz audio silence)
142  Frame(long int number, int width, int height, string color);
143 
144  /// Constructor - audio only (300x200 blank image)
145  Frame(long int number, int samples, int channels);
146 
147  /// Constructor - image & audio
148  Frame(long int number, int width, int height, string color, int samples, int channels);
149 
150  /// Copy constructor
151  Frame ( const Frame &other );
152 
153  /// Assignment operator
154  //Frame& operator= (const Frame& other);
155 
156  /// Destructor
157  ~Frame();
158 
159  /// Add (or replace) pixel data to the frame (based on a solid color)
160  void AddColor(int width, int height, string color);
161 
162  /// Add (or replace) pixel data to the frame
163  void AddImage(int width, int height, int bytes_per_pixel, QImage::Format type, const unsigned char *pixels_);
164 
165  /// Add (or replace) pixel data to the frame
166  void AddImage(tr1::shared_ptr<QImage> new_image);
167 
168  /// Add (or replace) pixel data to the frame (for only the odd or even lines)
169  void AddImage(tr1::shared_ptr<QImage> new_image, bool only_odd_lines);
170 
171 #ifdef USE_IMAGEMAGICK
172  /// Add (or replace) pixel data to the frame from an ImageMagick Image
173  void AddMagickImage(tr1::shared_ptr<Magick::Image> new_image);
174 #endif
175 
176  /// Add audio samples to a specific channel
177  void AddAudio(bool replaceSamples, int destChannel, int destStartSample, const float* source, int numSamples, float gainToApplyToSource);
178 
179  /// Add audio silence
180  void AddAudioSilence(int numSamples);
181 
182  /// Apply gain ramp (i.e. fading volume)
183  void ApplyGainRamp(int destChannel, int destStartSample, int numSamples, float initial_gain, float final_gain);
184 
185  /// Channel Layout of audio samples. A frame needs to keep track of this, since Writers do not always
186  /// know the original channel layout of a frame's audio samples (i.e. mono, stereo, 5 point surround, etc...)
187  ChannelLayout ChannelsLayout();
188 
189  // Set the channel layout of audio samples (i.e. mono, stereo, 5 point surround, etc...)
190  void ChannelsLayout(ChannelLayout new_channel_layout) { channel_layout = new_channel_layout; };
191 
192  /// Clean up buffer after QImage is deleted
193  static void cleanUpBuffer(void *info);
194 
195  /// Clear the waveform image (and deallocate it's memory)
196  void ClearWaveform();
197 
198  /// Copy data and pointers from another Frame instance
199  void DeepCopy(const Frame& other);
200 
201  /// Display the frame image to the screen (primarily used for debugging reasons)
202  void Display();
203 
204  /// Display the wave form
205  void DisplayWaveform();
206 
207  /// Get an array of sample data
208  float* GetAudioSamples(int channel);
209 
210  /// Get an array of sample data (all channels interleaved together), using any sample rate
211  float* GetInterleavedAudioSamples(int new_sample_rate, AudioResampler* resampler, int* sample_count);
212 
213  // Get a planar array of sample data, using any sample rate
214  float* GetPlanarAudioSamples(int new_sample_rate, AudioResampler* resampler, int* sample_count);
215 
216  /// Get number of audio channels
217  int GetAudioChannelsCount();
218 
219  /// Get number of audio channels
220  int GetAudioSamplesCount();
221 
222  juce::AudioSampleBuffer *GetAudioSampleBuffer();
223 
224  /// Get the size in bytes of this frame (rough estimate)
225  int64 GetBytes();
226 
227  /// Get pointer to Qt QImage image object
228  tr1::shared_ptr<QImage> GetImage();
229 
230 #ifdef USE_IMAGEMAGICK
231  /// Get pointer to ImageMagick image object
232  tr1::shared_ptr<Magick::Image> GetMagickImage();
233 #endif
234 
235  /// Set Pixel Aspect Ratio
236  Fraction GetPixelRatio() { return pixel_ratio; };
237 
238  /// Get pixel data (as packets)
239  const unsigned char* GetPixels();
240 
241  /// Get pixel data (for only a single scan-line)
242  const unsigned char* GetPixels(int row);
243 
244  /// Get height of image
245  int GetHeight();
246 
247  /// Calculate the # of samples per video frame (for the current frame number)
248  int GetSamplesPerFrame(Fraction fps, int sample_rate, int channels);
249 
250  /// Calculate the # of samples per video frame (for a specific frame number and frame rate)
251  static int GetSamplesPerFrame(long int frame_number, Fraction fps, int sample_rate, int channels);
252 
253  /// Get an audio waveform image
254  tr1::shared_ptr<QImage> GetWaveform(int width, int height, int Red, int Green, int Blue, int Alpha);
255 
256  /// Get an audio waveform image pixels
257  const unsigned char* GetWaveformPixels(int width, int height, int Red, int Green, int Blue, int Alpha);
258 
259  /// Get height of image
260  int GetWidth();
261 
262  /// Resize audio container to hold more (or less) samples and channels
263  void ResizeAudio(int channels, int length, int sample_rate, ChannelLayout channel_layout);
264 
265  /// Get the original sample rate of this frame's audio data
266  int SampleRate();
267 
268  /// Set the original sample rate of this frame's audio data
269  void SampleRate(int orig_sample_rate) { sample_rate = orig_sample_rate; };
270 
271  /// Save the frame image to the specified path. The image format can be BMP, JPG, JPEG, PNG, PPM, XBM, XPM
272  void Save(string path, float scale, string format="PNG", int quality=100);
273 
274  /// Set frame number
275  void SetFrameNumber(int number);
276 
277  /// Set Pixel Aspect Ratio
278  void SetPixelRatio(int num, int den);
279 
280  /// Thumbnail the frame image with tons of options to the specified path. The image format is determined from the extension (i.e. image.PNG, image.JPEG).
281  /// This method allows for masks, overlays, background color, and much more accurate resizing (including padding and centering)
282  void Thumbnail(string path, int new_width, int new_height, string mask_path, string overlay_path,
283  string background_color, bool ignore_aspect, string format="png", int quality=100) throw(InvalidFile);
284 
285  /// Play audio samples for this frame
286  void Play();
287  };
288 
289 }
290 
291 #endif
Header file for Fraction class.
void ChannelsLayout(ChannelLayout new_channel_layout)
Definition: Frame.h:190
This class represents a single frame of video (i.e. image & audio data)
Definition: Frame.h:114
Header file for AudioBufferSource class.
long int number
This is the frame number (starting at 1)
Definition: Frame.h:134
Fraction GetPixelRatio()
Set Pixel Aspect Ratio.
Definition: Frame.h:236
Header file for AudioResampler class.
Exception for files that can not be found or opened.
Definition: Exceptions.h:132
bool has_audio_data
This frame has been loaded with audio data.
Definition: Frame.h:135
This class represents a fraction.
Definition: Fraction.h:42
Header file for ChannelLayout class.
ChannelLayout
This enumeration determines the audio channel layout (such as stereo, mono, 5 point surround...
bool has_image_data
This frame has been loaded with pixel data.
Definition: Frame.h:136
void SampleRate(int orig_sample_rate)
Set the original sample rate of this frame's audio data.
Definition: Frame.h:269
This class is used to resample audio data for many sequential frames.