OpenShot Library | libopenshot  0.1.2
Timeline.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Header file for Timeline 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_TIMELINE_H
29 #define OPENSHOT_TIMELINE_H
30 
31 #include <list>
32 #include <tr1/memory>
33 #include <QtGui/QImage>
34 #include <QtGui/QPainter>
35 #include "Cache.h"
36 #include "Color.h"
37 #include "Clip.h"
38 #include "Point.h"
39 #include "EffectBase.h"
40 #include "Effects.h"
41 #include "EffectInfo.h"
42 #include "Fraction.h"
43 #include "Frame.h"
44 #include "FrameMapper.h"
45 #include "KeyFrame.h"
46 #include "OpenMPUtilities.h"
47 #include "ReaderBase.h"
48 
49 using namespace std;
50 using namespace openshot;
51 
52 namespace openshot {
53 
54  /// Comparison method for sorting clip pointers (by Layer and then Position). Clips are sorted
55  /// from lowest layer to top layer (since that is the sequence they need to be combined), and then
56  /// by position (left to right).
57  struct CompareClips{
58  bool operator()( Clip* lhs, Clip* rhs){
59  if( lhs->Layer() < rhs->Layer() ) return true;
60  if( lhs->Layer() == rhs->Layer() && lhs->Position() <= rhs->Position() ) return true;
61  return false;
62  }};
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 timeline
77  *
78  * The timeline is one of the <b>most important</b> features of a video editor, and controls all
79  * aspects of how video, image, and audio clips are combined together, and how the final
80  * video output will be rendered. It has a collection of layers and clips, that arrange,
81  * sequence, and generate the final video output.
82  *
83  * The <b>following graphic</b> displays a timeline, and how clips can be arranged, scaled, and layered together. It
84  * also demonstrates how the viewport can be scaled smaller than the canvas, which can be used to zoom and pan around the
85  * canvas (i.e. pan & scan).
86  * \image html /doc/images/Timeline_Layers.png
87  *
88  * The <b>following graphic</b> displays how the playhead determines which frames to combine and layer.
89  * \image html /doc/images/Playhead.png
90  *
91  * Lets take a look at what the code looks like:
92  * @code
93  * // Create a Timeline
94  * Timeline t(1280, // width
95  * 720, // height
96  * Fraction(25,1), // framerate
97  * 44100, // sample rate
98  * 2 // channels
99  * );
100  *
101  * // Create some clips
102  * Clip c1(new ImageReader("MyAwesomeLogo.jpeg"));
103  * Clip c2(new FFmpegReader("BackgroundVideo.webm"));
104  *
105  * // CLIP 1 (logo) - Set some clip properties (with Keyframes)
106  * c1.Position(0.0); // Set the position or location (in seconds) on the timeline
107  * c1.gravity = GRAVITY_LEFT; // Set the alignment / gravity of the clip (position on the screen)
108  * c1.scale = SCALE_CROP; // Set the scale mode (how the image is resized to fill the screen)
109  * c1.Layer(1); // Set the layer of the timeline (higher layers cover up images of lower layers)
110  * c1.Start(0.0); // Set the starting position of the video (trim the left side of the video)
111  * c1.End(16.0); // Set the ending position of the video (trim the right side of the video)
112  * c1.alpha.AddPoint(1, 0.0); // Set the alpha to transparent on frame #1
113  * c1.alpha.AddPoint(500, 0.0); // Keep the alpha transparent until frame #500
114  * c1.alpha.AddPoint(565, 1.0); // Animate the alpha from transparent to visible (between frame #501 and #565)
115  *
116  * // CLIP 2 (background video) - Set some clip properties (with Keyframes)
117  * c2.Position(0.0); // Set the position or location (in seconds) on the timeline
118  * c2.Start(10.0); // Set the starting position of the video (trim the left side of the video)
119  * c2.Layer(0); // Set the layer of the timeline (higher layers cover up images of lower layers)
120  * c2.alpha.AddPoint(1, 1.0); // Set the alpha to visible on frame #1
121  * c2.alpha.AddPoint(150, 0.0); // Animate the alpha to transparent (between frame 2 and frame #150)
122  * c2.alpha.AddPoint(360, 0.0, LINEAR); // Keep the alpha transparent until frame #360
123  * c2.alpha.AddPoint(384, 1.0); // Animate the alpha to visible (between frame #360 and frame #384)
124  *
125  * // Add clips to timeline
126  * t.AddClip(&c1);
127  * t.AddClip(&c2);
128  *
129  * // Open the timeline reader
130  * t.Open();
131  *
132  * // Get frame number 1 from the timeline (This will generate a new frame, made up from the previous clips and settings)
133  * tr1::shared_ptr<Frame> f = t.GetFrame(1);
134  *
135  * // Now that we have an openshot::Frame object, lets have some fun!
136  * f->Display(); // Display the frame on the screen
137  *
138  * // Close the timeline reader
139  * t.Close();
140  * @endcode
141  */
142  class Timeline : public ReaderBase {
143  private:
144  bool is_open; ///<Is Timeline Open?
145  bool auto_map_clips; ///< Auto map framerates and sample rates to all clips
146  list<Clip*> clips; ///<List of clips on this timeline
147  list<Clip*> closing_clips; ///<List of clips that need to be closed
148  map<Clip*, Clip*> open_clips; ///<List of 'opened' clips on this timeline
149  list<EffectBase*> effects; ///<List of clips on this timeline
150  Cache final_cache; ///<Final cache of timeline frames
151 
152  /// Process a new layer of video or audio
153  void add_layer(tr1::shared_ptr<Frame> new_frame, Clip* source_clip, long int clip_frame_number, long int timeline_frame_number, bool is_top_clip);
154 
155  /// Apply a FrameMapper to a clip which matches the settings of this timeline
156  void apply_mapper_to_clip(Clip* clip);
157 
158  /// Apply JSON Diffs to various objects contained in this timeline
159  void apply_json_to_clips(Json::Value change) throw(InvalidJSONKey); ///<Apply JSON diff to clips
160  void apply_json_to_effects(Json::Value change) throw(InvalidJSONKey); ///< Apply JSON diff to effects
161  void apply_json_to_effects(Json::Value change, EffectBase* existing_effect) throw(InvalidJSONKey); ///<Apply JSON diff to a specific effect
162  void apply_json_to_timeline(Json::Value change) throw(InvalidJSONKey); ///<Apply JSON diff to timeline properties
163 
164  /// Calculate time of a frame number, based on a framerate
165  float calculate_time(long int number, Fraction rate);
166 
167  /// Find intersecting (or non-intersecting) openshot::Clip objects
168  ///
169  /// @returns A list of openshot::Clip objects
170  /// @param requested_frame The frame number that is requested.
171  /// @param number_of_frames The number of frames to check
172  /// @param include Include or Exclude intersecting clips
173  vector<Clip*> find_intersecting_clips(long int requested_frame, int number_of_frames, bool include);
174 
175  /// Get or generate a blank frame
176  tr1::shared_ptr<Frame> GetOrCreateFrame(Clip* clip, long int number);
177 
178  /// Apply effects to the source frame (if any)
179  tr1::shared_ptr<Frame> apply_effects(tr1::shared_ptr<Frame> frame, long int timeline_frame_number, int layer);
180 
181  /// Compare 2 floating point numbers for equality
182  bool isEqual(double a, double b);
183 
184  /// Sort clips by position on the timeline
185  void sort_clips();
186 
187  /// Sort effects by position on the timeline
188  void sort_effects();
189 
190  /// Update the list of 'opened' clips
191  void update_open_clips(Clip *clip, bool does_clip_intersect);
192 
193  public:
194 
195  /// @brief Default Constructor for the timeline (which sets the canvas width and height and FPS)
196  /// @param width The width of the timeline (and thus, the generated openshot::Frame objects)
197  /// @param height The height of the timeline (and thus, the generated openshot::Frame objects)
198  /// @param fps The frames rate of the timeline
199  /// @param sample_rate The sample rate of the timeline's audio
200  /// @param channels The number of audio channels of the timeline
201  /// @param channel_layout The channel layout (i.e. mono, stereo, 3 point surround, etc...)
202  Timeline(int width, int height, Fraction fps, int sample_rate, int channels, ChannelLayout channel_layout);
203 
204  /// @brief Add an openshot::Clip to the timeline
205  /// @param clip Add an openshot::Clip to the timeline. A clip can contain any type of Reader.
206  void AddClip(Clip* clip) throw(ReaderClosed);
207 
208  /// @brief Add an effect to the timeline
209  /// @param effect Add an effect to the timeline. An effect can modify the audio or video of an openshot::Frame.
210  void AddEffect(EffectBase* effect);
211 
212  /// Apply the timeline's framerate and samplerate to all clips
213  void ApplyMapperToClips();
214 
215  /// Determine if clips are automatically mapped to the timeline's framerate and samplerate
216  bool AutoMapClips() { return auto_map_clips; };
217 
218  /// @brief Automatically map all clips to the timeline's framerate and samplerate
219  void AutoMapClips(bool auto_map) { auto_map_clips = auto_map; };
220 
221  /// Return a list of clips on the timeline
222  list<Clip*> Clips() { return clips; };
223 
224  /// Close the timeline reader (and any resources it was consuming)
225  void Close();
226 
227  /// Return the list of effects on the timeline
228  list<EffectBase*> Effects() { return effects; };
229 
230  /// Get the cache object used by this reader
231  Cache* GetCache() { return &final_cache; };
232 
233  /// Get an openshot::Frame object for a specific frame number of this timeline.
234  ///
235  /// @returns The requested frame (containing the image)
236  /// @param requested_frame The frame number that is requested.
237  tr1::shared_ptr<Frame> GetFrame(long int requested_frame) throw(ReaderClosed, OutOfBoundsFrame);
238 
239  // Curves for the viewport
240  Keyframe viewport_scale; ///<Curve representing the scale of the viewport (0 to 100)
241  Keyframe viewport_x; ///<Curve representing the x coordinate for the viewport
242  Keyframe viewport_y; ///<Curve representing the y coordinate for the viewport
243 
244  // Background color
245  Color color; ///<Background color of timeline canvas
246 
247  /// Determine if reader is open or closed
248  bool IsOpen() { return is_open; };
249 
250  /// Return the type name of the class
251  string Name() { return "Timeline"; };
252 
253  /// Get and Set JSON methods
254  string Json(); ///< Generate JSON string of this object
255  void SetJson(string value) throw(InvalidJSON); ///< Load JSON string into this object
256  Json::Value JsonValue(); ///< Generate Json::JsonValue for this object
257  void SetJsonValue(Json::Value root) throw(InvalidFile, ReaderClosed); ///< Load Json::JsonValue into this object
258 
259  /// @brief Apply a special formatted JSON object, which represents a change to the timeline (add, update, delete)
260  /// This is primarily designed to keep the timeline (and its child objects... such as clips and effects) in sync
261  /// with another application... such as OpenShot Video Editor (http://www.openshot.org).
262  /// @param value A JSON string containing a key, value, and type of change.
263  void ApplyJsonDiff(string value) throw(InvalidJSON, InvalidJSONKey);
264 
265  /// Open the reader (and start consuming resources)
266  void Open();
267 
268  /// @brief Remove an openshot::Clip from the timeline
269  /// @param clip Remove an openshot::Clip from the timeline.
270  void RemoveClip(Clip* clip);
271 
272  /// @brief Remove an effect from the timeline
273  /// @param effect Remove an effect from the timeline.
274  void RemoveEffect(EffectBase* effect);
275 
276  };
277 
278 
279 }
280 
281 #endif
Header file for Fraction class.
This abstract class is the base class, used by all effects in libopenshot.
Definition: EffectBase.h:66
Keyframe viewport_scale
Curve representing the scale of the viewport (0 to 100)
Definition: Timeline.h:240
Header file for ReaderBase class.
bool AutoMapClips()
Determine if clips are automatically mapped to the timeline&#39;s framerate and samplerate.
Definition: Timeline.h:216
Header file for OpenMPUtilities (set some common macros)
Header file for Point class.
This header includes all commonly used effects for libopenshot, for ease-of-use.
Keyframe viewport_y
Curve representing the y coordinate for the viewport.
Definition: Timeline.h:242
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.
Exception for missing JSON Change key.
Definition: Exceptions.h:182
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:109
Header file for Frame class.
Exception for files that can not be found or opened.
Definition: Exceptions.h:132
Header file for Cache class.
Header file for Clip class.
float Position()
Get position on timeline (in seconds)
Definition: ClipBase.h:77
bool operator()(Clip *lhs, Clip *rhs)
Definition: Timeline.h:58
This class represents a fraction.
Definition: Fraction.h:42
Header file for the FrameMapper class.
This class is a cache manager for Frame objects.
Definition: Cache.h:55
ChannelLayout
This enumeration determines the audio channel layout (such as stereo, mono, 5 point surround...
Header file for Color class.
void AutoMapClips(bool auto_map)
Automatically map all clips to the timeline&#39;s framerate and samplerate.
Definition: Timeline.h:219
bool IsOpen()
Determine if reader is open or closed.
Definition: Timeline.h:248
Exception for frames that are out of bounds.
Definition: Exceptions.h:202
This class represents a color (used on the timeline and clips)
Definition: Color.h:42
Cache * GetCache()
Get the cache object used by this reader.
Definition: Timeline.h:231
This namespace is the default namespace for all code in the openshot library.
Header file for EffectBase class.
Exception for invalid JSON.
Definition: Exceptions.h:152
Keyframe viewport_x
Curve representing the x coordinate for the viewport.
Definition: Timeline.h:241
Color color
Background color of timeline canvas.
Definition: Timeline.h:245
string Name()
Return the type name of the class.
Definition: Timeline.h:251
list< EffectBase * > Effects()
Return the list of effects on the timeline.
Definition: Timeline.h:228
list< Clip * > Clips()
Return a list of clips on the timeline.
Definition: Timeline.h:222
Header file for the EffectInfo class.
A Keyframe is a collection of Point instances, which is used to vary a number or property over time...
Definition: KeyFrame.h:64
int Order()
Get the order that this effect should be executed.
Definition: EffectBase.h:101
bool operator()(EffectBase *lhs, EffectBase *rhs)
Definition: Timeline.h:68
This class represents a timeline.
Definition: Timeline.h:142