OpenShot Library | libopenshot  0.1.1
Clip.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Header file for Clip 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_CLIP_H
29 #define OPENSHOT_CLIP_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 
36 #include <tr1/memory>
37 #include <string>
38 #include <QtGui/QImage>
39 #include "JuceLibraryCode/JuceHeader.h"
40 #include "AudioResampler.h"
41 #include "ClipBase.h"
42 #include "Color.h"
43 #include "Enums.h"
44 #include "EffectBase.h"
45 #include "Effects.h"
46 #include "FFmpegReader.h"
47 #include "Fraction.h"
48 #include "FrameMapper.h"
49 #ifdef USE_IMAGEMAGICK
50  #include "ImageReader.h"
51  #include "TextReader.h"
52 #endif
53 #include "QtImageReader.h"
54 #include "ChunkReader.h"
55 #include "KeyFrame.h"
56 #include "ReaderBase.h"
57 #include "DummyReader.h"
58 
59 using namespace std;
60 using namespace openshot;
61 
62 namespace openshot {
63 
64  /// Comparison method for sorting effect pointers (by Position, Layer, and Order). Effects are sorted
65  /// from lowest layer to top layer (since that is sequence clips are combined), and then by
66  /// position, and then by effect order.
68  bool operator()( EffectBase* lhs, EffectBase* rhs){
69  if( lhs->Layer() < rhs->Layer() ) return true;
70  if( lhs->Layer() == rhs->Layer() && lhs->Position() < rhs->Position() ) return true;
71  if( lhs->Layer() == rhs->Layer() && lhs->Position() == rhs->Position() && lhs->Order() > rhs->Order() ) return true;
72  return false;
73  }};
74 
75  /**
76  * @brief This class represents a clip (used to arrange readers on the timeline)
77  *
78  * Each image, video, or audio file is represented on a layer as a clip. A clip has many
79  * properties that affect how it behaves on the timeline, such as its size, position,
80  * transparency, rotation, speed, volume, etc...
81  *
82  * @code
83  * // Create some clips
84  * Clip c1(new ImageReader("MyAwesomeLogo.jpeg"));
85  * Clip c2(new FFmpegReader("BackgroundVideo.webm"));
86  *
87  * // CLIP 1 (logo) - Set some clip properties (with Keyframes)
88  * c1.Position(0.0); // Set the position or location (in seconds) on the timeline
89  * c1.gravity = GRAVITY_LEFT; // Set the alignment / gravity of the clip (position on the screen)
90  * c1.scale = SCALE_CROP; // Set the scale mode (how the image is resized to fill the screen)
91  * c1.Layer(1); // Set the layer of the timeline (higher layers cover up images of lower layers)
92  * c1.Start(0.0); // Set the starting position of the video (trim the left side of the video)
93  * c1.End(16.0); // Set the ending position of the video (trim the right side of the video)
94  * c1.alpha.AddPoint(1, 0.0); // Set the alpha to transparent on frame #1
95  * c1.alpha.AddPoint(500, 0.0); // Keep the alpha transparent until frame #500
96  * c1.alpha.AddPoint(565, 1.0); // Animate the alpha from transparent to visible (between frame #501 and #565)
97  *
98  * // CLIP 2 (background video) - Set some clip properties (with Keyframes)
99  * c2.Position(0.0); // Set the position or location (in seconds) on the timeline
100  * c2.Start(10.0); // Set the starting position of the video (trim the left side of the video)
101  * c2.Layer(0); // Set the layer of the timeline (higher layers cover up images of lower layers)
102  * c2.alpha.AddPoint(1, 1.0); // Set the alpha to visible on frame #1
103  * c2.alpha.AddPoint(150, 0.0); // Animate the alpha to transparent (between frame 2 and frame #150)
104  * c2.alpha.AddPoint(360, 0.0, LINEAR); // Keep the alpha transparent until frame #360
105  * c2.alpha.AddPoint(384, 1.0); // Animate the alpha to visible (between frame #360 and frame #384)
106  * @endcode
107  */
108  class Clip : public ClipBase {
109  protected:
110  /// Section lock for multiple threads
111  CriticalSection getFrameCriticalSection;
112 
113  private:
114  bool waveform; ///< Should a waveform be used instead of the clip's image
115  list<EffectBase*> effects; ///<List of clips on this timeline
116 
117  // Audio resampler (if time mapping)
118  AudioResampler *resampler;
119  AudioSampleBuffer *audio_cache;
120 
121  // File Reader object
122  ReaderBase* reader;
123  bool manage_reader;
124 
125  /// Adjust frame number minimum value
126  long int adjust_frame_number_minimum(long int frame_number);
127 
128  /// Apply effects to the source frame (if any)
129  tr1::shared_ptr<Frame> apply_effects(tr1::shared_ptr<Frame> frame);
130 
131  /// Get file extension
132  string get_file_extension(string path);
133 
134  /// Get a frame object or create a blank one
135  tr1::shared_ptr<Frame> GetOrCreateFrame(long int number);
136 
137  /// Adjust the audio and image of a time mapped frame
138  tr1::shared_ptr<Frame> get_time_mapped_frame(tr1::shared_ptr<Frame> frame, long int frame_number) throw(ReaderClosed);
139 
140  /// Init default settings for a clip
141  void init_settings();
142 
143  /// Sort effects by order
144  void sort_effects();
145 
146  /// Reverse an audio buffer
147  void reverse_buffer(juce::AudioSampleBuffer* buffer);
148 
149  public:
150  GravityType gravity; ///< The gravity of a clip determines where it snaps to it's parent
151  ScaleType scale; ///< The scale determines how a clip should be resized to fit it's parent
152  AnchorType anchor; ///< The anchor determines what parent a clip should snap to
153 
154  /// Default Constructor
155  Clip();
156 
157  /// @brief Constructor with filepath (reader is automatically created... by guessing file extensions)
158  /// @param path The path of a reader (video file, image file, etc...). The correct reader will be used automatically.
159  Clip(string path);
160 
161  /// @brief Constructor with reader
162  /// @param new_reader The reader to be used by this clip
163  Clip(ReaderBase* new_reader);
164 
165  /// Destructor
166  ~Clip();
167 
168  /// @brief Add an effect to the clip
169  /// @param effect Add an effect to the clip. An effect can modify the audio or video of an openshot::Frame.
170  void AddEffect(EffectBase* effect);
171 
172  /// Close the internal reader
173  void Close() throw(ReaderClosed);
174 
175  /// Return the list of effects on the timeline
176  list<EffectBase*> Effects() { return effects; };
177 
178  /// @brief Get an openshot::Frame object for a specific frame number of this timeline.
179  ///
180  /// @returns The requested frame (containing the image)
181  /// @param requested_frame The frame number that is requested
182  tr1::shared_ptr<Frame> GetFrame(long int requested_frame) throw(ReaderClosed);
183 
184  /// Open the internal reader
185  void Open() throw(InvalidFile, ReaderClosed);
186 
187  /// @brief Set the current reader
188  /// @param new_reader The reader to be used by this clip
189  void Reader(ReaderBase* new_reader);
190 
191  /// Get the current reader
192  ReaderBase* Reader() throw(ReaderClosed);
193 
194  /// Override End() method
195  float End() throw(ReaderClosed); ///< Get end position (in seconds) of clip (trim end of video), which can be affected by the time curve.
196  void End(float value) { end = value; } ///< Set end position (in seconds) of clip (trim end of video)
197 
198  /// Get and Set JSON methods
199  string Json(); ///< Generate JSON string of this object
200  void SetJson(string value) throw(InvalidJSON); ///< Load JSON string into this object
201  Json::Value JsonValue(); ///< Generate Json::JsonValue for this object
202  void SetJsonValue(Json::Value root); ///< Load Json::JsonValue into this object
203 
204  /// Get all properties for a specific frame (perfect for a UI to display the current state
205  /// of all properties at any time)
206  string PropertiesJSON(long int requested_frame);
207 
208  /// @brief Remove an effect from the clip
209  /// @param effect Remove an effect from the clip.
210  void RemoveEffect(EffectBase* effect);
211 
212  /// Waveform property
213  bool Waveform() { return waveform; } ///< Get the waveform property of this clip
214  void Waveform(bool value) { waveform = value; } ///< Set the waveform property of this clip
215 
216  // Scale and Location curves
217  Keyframe scale_x; ///< Curve representing the horizontal scaling in percent (0 to 100)
218  Keyframe scale_y; ///< Curve representing the vertical scaling in percent (0 to 100)
219  Keyframe location_x; ///< Curve representing the relative X position in percent based on the gravity (-100 to 100)
220  Keyframe location_y; ///< Curve representing the relative Y position in percent based on the gravity (-100 to 100)
221 
222  // Alpha and Rotation curves
223  Keyframe alpha; ///< Curve representing the alpha (1 to 0)
224  Keyframe rotation; ///< Curve representing the rotation (0 to 360)
225 
226  // Time and Volume curves
227  Keyframe time; ///< Curve representing the frames over time to play (used for speed and direction of video)
228  Keyframe volume; ///< Curve representing the volume (0 to 1)
229 
230  /// Curve representing the color of the audio wave form
232 
233  // Crop settings and curves
234  GravityType crop_gravity; ///< Cropping needs to have a gravity to determine what side we are cropping
235  Keyframe crop_width; ///< Curve representing width in percent (0.0=0%, 1.0=100%)
236  Keyframe crop_height; ///< Curve representing height in percent (0.0=0%, 1.0=100%)
237  Keyframe crop_x; ///< Curve representing X offset in percent (-1.0=-100%, 0.0=0%, 1.0=100%)
238  Keyframe crop_y; ///< Curve representing Y offset in percent (-1.0=-100%, 0.0=0%, 1.0=100%)
239 
240  // Shear and perspective curves
241  Keyframe shear_x; ///< Curve representing X shear angle in degrees (-45.0=left, 45.0=right)
242  Keyframe shear_y; ///< Curve representing Y shear angle in degrees (-45.0=down, 45.0=up)
243  Keyframe perspective_c1_x; ///< Curves representing X for coordinate 1
244  Keyframe perspective_c1_y; ///< Curves representing Y for coordinate 1
245  Keyframe perspective_c2_x; ///< Curves representing X for coordinate 2
246  Keyframe perspective_c2_y; ///< Curves representing Y for coordinate 2
247  Keyframe perspective_c3_x; ///< Curves representing X for coordinate 3
248  Keyframe perspective_c3_y; ///< Curves representing Y for coordinate 3
249  Keyframe perspective_c4_x; ///< Curves representing X for coordinate 4
250  Keyframe perspective_c4_y; ///< Curves representing Y for coordinate 4
251 
252  };
253 
254 
255 }
256 
257 #endif
Keyframe perspective_c3_x
Curves representing X for coordinate 3.
Definition: Clip.h:247
Header file for Fraction class.
Keyframe scale_y
Curve representing the vertical scaling in percent (0 to 100)
Definition: Clip.h:218
Keyframe perspective_c4_x
Curves representing X for coordinate 4.
Definition: Clip.h:249
Header file for ClipBase class.
This abstract class is the base class, used by all effects in libopenshot.
Definition: EffectBase.h:66
Keyframe perspective_c1_x
Curves representing X for coordinate 1.
Definition: Clip.h:243
Keyframe perspective_c2_x
Curves representing X for coordinate 2.
Definition: Clip.h:245
Keyframe crop_x
Curve representing X offset in percent (-1.0=-100%, 0.0=0%, 1.0=100%)
Definition: Clip.h:237
Header file for DummyReader class.
Keyframe perspective_c3_y
Curves representing Y for coordinate 3.
Definition: Clip.h:248
Header file for ReaderBase class.
GravityType gravity
The gravity of a clip determines where it snaps to it's parent.
Definition: Clip.h:150
Keyframe volume
Curve representing the volume (0 to 1)
Definition: Clip.h:228
This header includes all commonly used effects for libopenshot, for ease-of-use.
Header file for FFmpegReader class.
bool operator()(EffectBase *lhs, EffectBase *rhs)
Definition: Clip.h:68
Keyframe time
Curve representing the frames over time to play (used for speed and direction of video) ...
Definition: Clip.h:227
ScaleType
This enumeration determines how clips are scaled to fit their parent container.
Definition: Enums.h:49
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:95
int Layer()
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:78
Exception when a reader is closed, and a frame is requested.
Definition: Exceptions.h:234
Header file for the Keyframe class.
Color wave_color
Curve representing the color of the audio wave form.
Definition: Clip.h:231
Keyframe crop_width
Curve representing width in percent (0.0=0%, 1.0=100%)
Definition: Clip.h:235
Keyframe location_x
Curve representing the relative X position in percent based on the gravity (-100 to 100) ...
Definition: Clip.h:219
Keyframe location_y
Curve representing the relative Y position in percent based on the gravity (-100 to 100) ...
Definition: Clip.h:220
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:108
Keyframe perspective_c1_y
Curves representing Y for coordinate 1.
Definition: Clip.h:244
Keyframe crop_y
Curve representing Y offset in percent (-1.0=-100%, 0.0=0%, 1.0=100%)
Definition: Clip.h:238
Keyframe shear_x
Curve representing X shear angle in degrees (-45.0=left, 45.0=right)
Definition: Clip.h:241
bool Waveform()
Waveform property.
Definition: Clip.h:213
ScaleType scale
The scale determines how a clip should be resized to fit it's parent.
Definition: Clip.h:151
Header file for ChunkReader class.
Header file for AudioResampler class.
Exception for files that can not be found or opened.
Definition: Exceptions.h:132
Header file for TextReader class.
float Position()
Get position on timeline (in seconds)
Definition: ClipBase.h:77
Header file for the FrameMapper class.
This abstract class is the base class, used by all clips in libopenshot.
Definition: ClipBase.h:52
Keyframe rotation
Curve representing the rotation (0 to 360)
Definition: Clip.h:224
Header file for Color class.
Keyframe shear_y
Curve representing Y shear angle in degrees (-45.0=down, 45.0=up)
Definition: Clip.h:242
AnchorType
This enumeration determines what parent a clip should be aligned to.
Definition: Enums.h:58
This class represents a color (used on the timeline and clips)
Definition: Color.h:42
Header file for TextReader class.
GravityType crop_gravity
Cropping needs to have a gravity to determine what side we are cropping.
Definition: Clip.h:234
Header file for EffectBase class.
AnchorType anchor
The anchor determines what parent a clip should snap to.
Definition: Clip.h:152
Exception for invalid JSON.
Definition: Exceptions.h:152
Keyframe alpha
Curve representing the alpha (1 to 0)
Definition: Clip.h:223
Header file for QtImageReader class.
Keyframe scale_x
Curve representing the horizontal scaling in percent (0 to 100)
Definition: Clip.h:217
Keyframe perspective_c2_y
Curves representing Y for coordinate 2.
Definition: Clip.h:246
CriticalSection getFrameCriticalSection
Section lock for multiple threads.
Definition: Clip.h:111
Header file for ImageReader class.
A Keyframe is a collection of Point instances, which is used to vary a number or property over time...
Definition: KeyFrame.h:64
void Waveform(bool value)
Set the waveform property of this clip.
Definition: Clip.h:214
Keyframe perspective_c4_y
Curves representing Y for coordinate 4.
Definition: Clip.h:250
int Order()
Get the order that this effect should be executed.
Definition: EffectBase.h:101
GravityType
This enumeration determines how clips are aligned to their parent container.
Definition: Enums.h:35
This class is used to resample audio data for many sequential frames.
Keyframe crop_height
Curve representing height in percent (0.0=0%, 1.0=100%)
Definition: Clip.h:236