OpenShot Library | libopenshot  0.1.3
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 #include "ZmqLogger.h"
56 #ifdef USE_IMAGEMAGICK
57  #include "Magick++.h"
58 #endif
59 #include "JuceLibraryCode/JuceHeader.h"
60 #include "ChannelLayouts.h"
61 #include "AudioBufferSource.h"
62 #include "AudioResampler.h"
63 #include "Fraction.h"
64 
65 
66 using namespace std;
67 
68 namespace openshot
69 {
70  /**
71  * @brief This class represents a single frame of video (i.e. image & audio data)
72  *
73  * FileReaders (such as FFmpegReader) use instances of this class to store the individual frames of video,
74  * which include both the image data (i.e. pixels) and audio samples. An openshot::Frame also has many debug
75  * methods, such as the ability to display the image (using X11), play the audio samples (using JUCE), or
76  * display the audio waveform as an image.
77  *
78  * FileWriters (such as FFmpegWriter) use instances of this class to create new video files, image files, or
79  * video streams. So, think of these openshot::Frame instances as the smallest unit of work in a video
80  * editor.
81  *
82  * There are many ways to create an instance of an openshot::Frame:
83  * @code
84  *
85  * // Most basic: a blank frame (300x200 blank image, 48kHz audio silence)
86  * Frame();
87  *
88  * // Image only settings (48kHz audio silence)
89  * Frame(1, // Frame number
90  * 720, // Width of image
91  * 480, // Height of image
92  * "#000000" // HTML color code of background color
93  * );
94  *
95  * // Audio only (300x200 blank image)
96  * Frame(number, // Frame number
97  * 44100, // Sample rate of audio stream
98  * 2 // Number of audio channels
99  * );
100  *
101  * // Image and Audio settings (user defines all key settings)
102  * Frame(number, // Frame number
103  * 720, // Width of image
104  * 480, // Height of image
105  * "#000000" // HTML color code of background color
106  * 44100, // Sample rate of audio stream
107  * 2 // Number of audio channels
108  * );
109  *
110  * // Some methods require a shared pointer to an openshot::Frame object.
111  * tr1::shared_ptr<Frame> f(new Frame(1, 720, 480, "#000000", 44100, 2));
112  *
113  * @endcode
114  */
115  class Frame
116  {
117  private:
118  tr1::shared_ptr<QImage> image;
119  tr1::shared_ptr<QImage> wave_image;
120  tr1::shared_ptr<juce::AudioSampleBuffer> audio;
121  tr1::shared_ptr<QApplication> previewApp;
122  CriticalSection addingImageSection;
123  const unsigned char *qbuffer;
124  Fraction pixel_ratio;
125  int channels;
126  ChannelLayout channel_layout;
127  int width;
128  int height;
129  int sample_rate;
130 
131  /// Constrain a color value from 0 to 255
132  int constrain(int color_value);
133 
134  public:
135  long int number; ///< This is the frame number (starting at 1)
136  bool has_audio_data; ///< This frame has been loaded with audio data
137  bool has_image_data; ///< This frame has been loaded with pixel data
138 
139  /// Constructor - blank frame (300x200 blank image, 48kHz audio silence)
140  Frame();
141 
142  /// Constructor - image only (48kHz audio silence)
143  Frame(long int number, int width, int height, string color);
144 
145  /// Constructor - audio only (300x200 blank image)
146  Frame(long int number, int samples, int channels);
147 
148  /// Constructor - image & audio
149  Frame(long int number, int width, int height, string color, int samples, int channels);
150 
151  /// Copy constructor
152  Frame ( const Frame &other );
153 
154  /// Assignment operator
155  //Frame& operator= (const Frame& other);
156 
157  /// Destructor
158  ~Frame();
159 
160  /// Add (or replace) pixel data to the frame (based on a solid color)
161  void AddColor(int new_width, int new_height, string color);
162 
163  /// Add (or replace) pixel data to the frame
164  void AddImage(int new_width, int new_height, int bytes_per_pixel, QImage::Format type, const unsigned char *pixels_);
165 
166  /// Add (or replace) pixel data to the frame
167  void AddImage(tr1::shared_ptr<QImage> new_image);
168 
169  /// Add (or replace) pixel data to the frame (for only the odd or even lines)
170  void AddImage(tr1::shared_ptr<QImage> new_image, bool only_odd_lines);
171 
172 #ifdef USE_IMAGEMAGICK
173  /// Add (or replace) pixel data to the frame from an ImageMagick Image
174  void AddMagickImage(tr1::shared_ptr<Magick::Image> new_image);
175 #endif
176 
177  /// Add audio samples to a specific channel
178  void AddAudio(bool replaceSamples, int destChannel, int destStartSample, const float* source, int numSamples, float gainToApplyToSource);
179 
180  /// Add audio silence
181  void AddAudioSilence(int numSamples);
182 
183  /// Apply gain ramp (i.e. fading volume)
184  void ApplyGainRamp(int destChannel, int destStartSample, int numSamples, float initial_gain, float final_gain);
185 
186  /// Channel Layout of audio samples. A frame needs to keep track of this, since Writers do not always
187  /// know the original channel layout of a frame's audio samples (i.e. mono, stereo, 5 point surround, etc...)
188  ChannelLayout ChannelsLayout();
189 
190  // Set the channel layout of audio samples (i.e. mono, stereo, 5 point surround, etc...)
191  void ChannelsLayout(ChannelLayout new_channel_layout) { channel_layout = new_channel_layout; };
192 
193  /// Clean up buffer after QImage is deleted
194  static void cleanUpBuffer(void *info);
195 
196  /// Clear the waveform image (and deallocate it's memory)
197  void ClearWaveform();
198 
199  /// Copy data and pointers from another Frame instance
200  void DeepCopy(const Frame& other);
201 
202  /// Display the frame image to the screen (primarily used for debugging reasons)
203  void Display();
204 
205  /// Display the wave form
206  void DisplayWaveform();
207 
208  /// Get magnitude of range of samples (if channel is -1, return average of all channels for that sample)
209  float GetAudioSample(int channel, int sample, int magnitude_range);
210 
211  /// Get an array of sample data
212  float* GetAudioSamples(int channel);
213 
214  /// Get an array of sample data (all channels interleaved together), using any sample rate
215  float* GetInterleavedAudioSamples(int new_sample_rate, AudioResampler* resampler, int* sample_count);
216 
217  // Get a planar array of sample data, using any sample rate
218  float* GetPlanarAudioSamples(int new_sample_rate, AudioResampler* resampler, int* sample_count);
219 
220  /// Get number of audio channels
221  int GetAudioChannelsCount();
222 
223  /// Get number of audio samples
224  int GetAudioSamplesCount();
225 
226  juce::AudioSampleBuffer *GetAudioSampleBuffer();
227 
228  /// Get the size in bytes of this frame (rough estimate)
229  int64 GetBytes();
230 
231  /// Get pointer to Qt QImage image object
232  tr1::shared_ptr<QImage> GetImage();
233 
234 #ifdef USE_IMAGEMAGICK
235  /// Get pointer to ImageMagick image object
236  tr1::shared_ptr<Magick::Image> GetMagickImage();
237 #endif
238 
239  /// Set Pixel Aspect Ratio
240  Fraction GetPixelRatio() { return pixel_ratio; };
241 
242  /// Get pixel data (as packets)
243  const unsigned char* GetPixels();
244 
245  /// Get pixel data (for only a single scan-line)
246  const unsigned char* GetPixels(int row);
247 
248  /// Get height of image
249  int GetHeight();
250 
251  /// Calculate the # of samples per video frame (for the current frame number)
252  int GetSamplesPerFrame(Fraction fps, int sample_rate, int channels);
253 
254  /// Calculate the # of samples per video frame (for a specific frame number and frame rate)
255  static int GetSamplesPerFrame(long int frame_number, Fraction fps, int sample_rate, int channels);
256 
257  /// Get an audio waveform image
258  tr1::shared_ptr<QImage> GetWaveform(int width, int height, int Red, int Green, int Blue, int Alpha);
259 
260  /// Get an audio waveform image pixels
261  const unsigned char* GetWaveformPixels(int width, int height, int Red, int Green, int Blue, int Alpha);
262 
263  /// Get height of image
264  int GetWidth();
265 
266  /// Resize audio container to hold more (or less) samples and channels
267  void ResizeAudio(int channels, int length, int sample_rate, ChannelLayout channel_layout);
268 
269  /// Get the original sample rate of this frame's audio data
270  int SampleRate();
271 
272  /// Set the original sample rate of this frame's audio data
273  void SampleRate(int orig_sample_rate) { sample_rate = orig_sample_rate; };
274 
275  /// Save the frame image to the specified path. The image format can be BMP, JPG, JPEG, PNG, PPM, XBM, XPM
276  void Save(string path, float scale, string format="PNG", int quality=100);
277 
278  /// Set frame number
279  void SetFrameNumber(int number);
280 
281  /// Set Pixel Aspect Ratio
282  void SetPixelRatio(int num, int den);
283 
284  /// 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).
285  /// This method allows for masks, overlays, background color, and much more accurate resizing (including padding and centering)
286  void Thumbnail(string path, int new_width, int new_height, string mask_path, string overlay_path,
287  string background_color, bool ignore_aspect, string format="png", int quality=100) throw(InvalidFile);
288 
289  /// Play audio samples for this frame
290  void Play();
291  };
292 
293 }
294 
295 #endif
Header file for Fraction class.
void ChannelsLayout(ChannelLayout new_channel_layout)
Definition: Frame.h:191
This class represents a single frame of video (i.e. image & audio data)
Definition: Frame.h:115
Header file for AudioBufferSource class.
long int number
This is the frame number (starting at 1)
Definition: Frame.h:135
Fraction GetPixelRatio()
Set Pixel Aspect Ratio.
Definition: Frame.h:240
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:136
This class represents a fraction.
Definition: Fraction.h:42
Header file for ZeroMQ-based Logger class.
Header file for ChannelLayout class.
ChannelLayout
This enumeration determines the audio channel layout (such as stereo, mono, 5 point surround...
This namespace is the default namespace for all code in the openshot library.
bool has_image_data
This frame has been loaded with pixel data.
Definition: Frame.h:137
void SampleRate(int orig_sample_rate)
Set the original sample rate of this frame&#39;s audio data.
Definition: Frame.h:273
This class is used to resample audio data for many sequential frames.