OpenShot Library | libopenshot  0.3.1
Timeline.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_TIMELINE_H
14 #define OPENSHOT_TIMELINE_H
15 
16 #include <list>
17 #include <memory>
18 #include <mutex>
19 #include <set>
20 #include <QtGui/QImage>
21 #include <QtGui/QPainter>
22 #include <QtCore/QRegularExpression>
23 
24 #include "TimelineBase.h"
25 #include "ReaderBase.h"
26 
27 #include "Color.h"
28 #include "Clip.h"
29 #include "EffectBase.h"
30 #include "Fraction.h"
31 #include "Frame.h"
32 #include "KeyFrame.h"
33 #ifdef USE_OPENCV
34 #include "TrackedObjectBBox.h"
35 #endif
36 #include "TrackedObjectBase.h"
37 
38 
39 
40 namespace openshot {
41 
42  // Forward decls
43  class FrameMapper;
44  class CacheBase;
45 
49  struct CompareClips{
51  if( lhs->Layer() < rhs->Layer() ) return true;
52  if( lhs->Layer() == rhs->Layer() && lhs->Position() <= rhs->Position() ) return true;
53  return false;
54  }};
55 
61  if( lhs->Layer() < rhs->Layer() ) return true;
62  if( lhs->Layer() == rhs->Layer() && lhs->Position() < rhs->Position() ) return true;
63  if( lhs->Layer() == rhs->Layer() && lhs->Position() == rhs->Position() && lhs->Order() > rhs->Order() ) return true;
64  return false;
65  }};
66 
70  bool operator()(const openshot::Clip* lhs, const openshot::Clip* rhs) {
71  return (lhs->Position() + lhs->Duration())
72  <= (rhs->Position() + rhs->Duration());
73  }};
74 
77  bool operator()(const openshot::EffectBase* lhs, const openshot::EffectBase* rhs) {
78  return (lhs->Position() + lhs->Duration())
79  <= (rhs->Position() + rhs->Duration());
80  }};
81 
151  private:
152  bool is_open;
153  bool auto_map_clips;
154  std::list<openshot::Clip*> clips;
155  std::list<openshot::Clip*> closing_clips;
156  std::map<openshot::Clip*, openshot::Clip*> open_clips;
157  std::set<openshot::Clip*> allocated_clips;
158  std::list<openshot::EffectBase*> effects;
159  std::set<openshot::EffectBase*> allocated_effects;
160  openshot::CacheBase *final_cache;
161  std::set<openshot::FrameMapper*> allocated_frame_mappers;
162  bool managed_cache;
163  std::string path;
164  int max_concurrent_frames;
165  double max_time;
166 
167  std::map<std::string, std::shared_ptr<openshot::TrackedObjectBase>> tracked_objects;
168 
170  void add_layer(std::shared_ptr<openshot::Frame> new_frame, openshot::Clip* source_clip, int64_t clip_frame_number, bool is_top_clip, float max_volume);
171 
173  void apply_mapper_to_clip(openshot::Clip* clip);
174 
175  // Apply JSON Diffs to various objects contained in this timeline
176  void apply_json_to_clips(Json::Value change);
177  void apply_json_to_effects(Json::Value change);
178  void apply_json_to_effects(Json::Value change, openshot::EffectBase* existing_effect);
179  void apply_json_to_timeline(Json::Value change);
180 
182  void calculate_max_duration();
183 
185  double calculate_time(int64_t number, openshot::Fraction rate);
186 
193  std::vector<openshot::Clip*> find_intersecting_clips(int64_t requested_frame, int number_of_frames, bool include);
194 
196  std::shared_ptr<openshot::Frame> GetOrCreateFrame(std::shared_ptr<Frame> background_frame, openshot::Clip* clip, int64_t number, openshot::TimelineInfoStruct* options);
197 
199  bool isEqual(double a, double b);
200 
202  void sort_clips();
203 
205  void sort_effects();
206 
208  void update_open_clips(openshot::Clip *clip, bool does_clip_intersect);
209 
210  public:
211 
219  Timeline(int width, int height, openshot::Fraction fps, int sample_rate, int channels, openshot::ChannelLayout channel_layout);
220 
223  Timeline(ReaderInfo info);
224 
232  Timeline(const std::string& projectPath, bool convert_absolute_paths);
233 
234  virtual ~Timeline();
235 
237  void AddTrackedObject(std::shared_ptr<openshot::TrackedObjectBase> trackedObject);
239  std::shared_ptr<openshot::TrackedObjectBase> GetTrackedObject(std::string id) const;
241  std::list<std::string> GetTrackedObjectsIds() const;
243  #ifdef USE_OPENCV
244  std::string GetTrackedObjectValues(std::string id, int64_t frame_number) const;
245  #endif
246 
249  void AddClip(openshot::Clip* clip);
250 
253  void AddEffect(openshot::EffectBase* effect);
254 
256  std::shared_ptr<openshot::Frame> apply_effects(std::shared_ptr<openshot::Frame> frame, int64_t timeline_frame_number, int layer);
257 
259  void ApplyMapperToClips();
260 
262  bool AutoMapClips() { return auto_map_clips; };
263 
265  void AutoMapClips(bool auto_map) { auto_map_clips = auto_map; };
266 
268  void Clear();
269 
272  void ClearAllCache(bool deep=false);
273 
275  std::list<openshot::Clip*> Clips() override { return clips; };
276 
278  openshot::Clip* GetClip(const std::string& id);
279 
281  openshot::EffectBase* GetClipEffect(const std::string& id);
282 
284  openshot::EffectBase* GetEffect(const std::string& id);
285 
287  double GetMaxTime();
289  int64_t GetMaxFrame();
290 
292  void Close() override;
293 
295  std::list<openshot::EffectBase*> Effects() { return effects; };
296 
298  std::list<openshot::EffectBase*> ClipEffects() const;
299 
301  openshot::CacheBase* GetCache() override { return final_cache; };
302 
305  void SetCache(openshot::CacheBase* new_cache);
306 
311  std::shared_ptr<openshot::Frame> GetFrame(int64_t requested_frame) override;
312 
313  // Curves for the viewport
317 
318  // Background color
320 
322  bool IsOpen() override { return is_open; };
323 
325  std::string Name() override { return "Timeline"; };
326 
327  // Get and Set JSON methods
328  std::string Json() const override;
329  void SetJson(const std::string value) override;
330  Json::Value JsonValue() const override;
331  void SetJsonValue(const Json::Value root) override;
332 
335  void SetMaxSize(int width, int height);
336 
341  void ApplyJsonDiff(std::string value);
342 
344  void Open() override;
345 
348  void RemoveClip(openshot::Clip* clip);
349 
352  void RemoveEffect(openshot::EffectBase* effect);
353 
356  void SortTimeline() { sort_clips(); sort_effects(); }
357  };
358 
359 }
360 
361 #endif // OPENSHOT_TIMELINE_H
openshot::Color color
Background color of timeline canvas.
Definition: Timeline.h:319
openshot::CacheBase * GetCache() override
Get the cache object used by this reader.
Definition: Timeline.h:301
bool operator()(const openshot::EffectBase *lhs, const openshot::EffectBase *rhs)
Definition: Timeline.h:77
Header file for Fraction class.
This class represents a timeline (used for building generic timeline implementations) ...
Definition: TimelineBase.h:40
This abstract class is the base class, used by all effects in libopenshot.
Definition: EffectBase.h:52
Header file for ReaderBase class.
bool AutoMapClips()
Determine if clips are automatically mapped to the timeline&#39;s framerate and samplerate.
Definition: Timeline.h:262
This struct contains info about the current Timeline clip instance.
Definition: TimelineBase.h:32
bool operator()(openshot::Clip *lhs, openshot::Clip *rhs)
Definition: Timeline.h:50
std::string Name() override
Return the type name of the class.
Definition: Timeline.h:325
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:75
Header file for the Keyframe class.
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:91
Header file for Frame class.
Header file for Clip class.
This class represents a fraction.
Definition: Fraction.h:30
This struct contains info about a media file, such as height, width, frames per second, etc...
Definition: ReaderBase.h:38
All cache managers in libopenshot are based on this CacheBase class.
Definition: CacheBase.h:34
ChannelLayout
This enumeration determines the audio channel layout (such as stereo, mono, 5 point surround...
openshot::Keyframe viewport_y
Curve representing the y coordinate for the viewport.
Definition: Timeline.h:316
bool operator()(openshot::EffectBase *lhs, openshot::EffectBase *rhs)
Definition: Timeline.h:60
bool IsOpen() override
Determine if reader is open or closed.
Definition: Timeline.h:322
int Order() const
Get the order that this effect should be executed.
Definition: EffectBase.h:112
Header file for Color class.
openshot::Keyframe viewport_x
Curve representing the x coordinate for the viewport.
Definition: Timeline.h:315
void AutoMapClips(bool auto_map)
Automatically map all clips to the timeline&#39;s framerate and samplerate.
Definition: Timeline.h:265
This class represents a color (used on the timeline and clips)
Definition: Color.h:27
Header file for the TrackedObjectBBox class.
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:90
std::list< openshot::EffectBase * > Effects()
Return the list of effects on the timeline.
Definition: Timeline.h:295
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
Header file for EffectBase class.
Header file for Timeline class.
openshot::Keyframe viewport_scale
Curve representing the scale of the viewport (0 to 100)
Definition: Timeline.h:314
void SortTimeline()
Sort all clips and effects on timeline - which affects the internal order of clips and effects arrays...
Definition: Timeline.h:356
Like CompareClipEndFrames, but for effects.
Definition: Timeline.h:76
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:86
Header file for the TrackedObjectBase class.
A Keyframe is a collection of Point instances, which is used to vary a number or property over time...
Definition: KeyFrame.h:53
std::list< openshot::Clip * > Clips() override
Return a list of clips on the timeline.
Definition: Timeline.h:275
bool operator()(const openshot::Clip *lhs, const openshot::Clip *rhs)
Definition: Timeline.h:70
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:87
This class represents a timeline.
Definition: Timeline.h:150