OpenShot Library | libopenshot  0.5.0
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 <set>
19 #include <atomic>
20 #include <cstdint>
21 #include <utility>
22 
23 #include "TimelineBase.h"
24 #include "ReaderBase.h"
25 
26 #include "Color.h"
27 #include "Clip.h"
28 #include "EffectBase.h"
29 #include "Fraction.h"
30 #include "Frame.h"
31 #include "KeyFrame.h"
32 #ifdef USE_OPENCV
33 #include "TrackedObjectBBox.h"
34 #endif
35 #include "TrackedObjectBase.h"
36 
37 
38 
39 namespace openshot {
40 
41  // Forward decls
42  class FrameMapper;
43  class CacheBase;
44 
48  struct CompareClips {
49  bool operator()(openshot::Clip* lhs, openshot::Clip* rhs) const {
50  // Strict-weak ordering (no <=) to keep sort well-defined
51  if (lhs == rhs) return false; // irreflexive
52  if (lhs->Layer() != rhs->Layer())
53  return lhs->Layer() < rhs->Layer();
54  if (lhs->Position() != rhs->Position())
55  return lhs->Position() < rhs->Position();
56  // Stable tie-breaker on address to avoid equivalence when layer/position match
57  return std::less<openshot::Clip*>()(lhs, rhs);
58  }
59  };
60 
66  if( lhs->Layer() < rhs->Layer() ) return true;
67  if( lhs->Layer() == rhs->Layer() && lhs->Position() < rhs->Position() ) return true;
68  if( lhs->Layer() == rhs->Layer() && lhs->Position() == rhs->Position() && lhs->Order() > rhs->Order() ) return true;
69  return false;
70  }};
71 
75  bool operator()(const openshot::Clip* lhs, const openshot::Clip* rhs) {
76  return (lhs->Position() + lhs->Duration()) < (rhs->Position() + rhs->Duration());
77  }};
78 
81  bool operator()(const openshot::EffectBase* lhs, const openshot::EffectBase* rhs) {
82  return (lhs->Position() + lhs->Duration()) < (rhs->Position() + rhs->Duration());
83  }};
84 
154  private:
155  bool is_open;
156  bool auto_map_clips;
157  std::list<openshot::Clip*> clips;
158  std::list<openshot::Clip*> closing_clips;
159  std::map<openshot::Clip*, openshot::Clip*> open_clips;
160  std::set<openshot::Clip*> allocated_clips;
161  std::list<openshot::EffectBase*> effects;
162  std::set<openshot::EffectBase*> allocated_effects;
163  openshot::CacheBase *final_cache;
164  std::set<openshot::FrameMapper*> allocated_frame_mappers;
165  bool managed_cache;
166  std::string path;
167  double max_time;
168  double min_time;
169  std::atomic<uint64_t> cache_epoch;
170  std::atomic<int> safe_edit_frames_remaining;
171 
172  std::map<std::string, std::shared_ptr<openshot::TrackedObjectBase>> tracked_objects;
173 
175  void add_layer(std::shared_ptr<openshot::Frame> new_frame, openshot::Clip* source_clip, int64_t clip_frame_number, bool is_top_clip, bool force_safe_composite, float max_volume);
176 
178  std::pair<float, float> ResolveTransitionAudioGains(openshot::Clip* source_clip, int64_t timeline_frame_number, bool is_top_clip) const;
179 
181  void apply_mapper_to_clip(openshot::Clip* clip);
182 
183  // Apply JSON Diffs to various objects contained in this timeline
184  void apply_json_to_clips(Json::Value change);
185  void apply_json_to_effects(Json::Value change);
186  void apply_json_to_effects(Json::Value change, openshot::EffectBase* existing_effect);
187  void apply_json_to_timeline(Json::Value change);
188 
190  void calculate_max_duration();
191 
193  double calculate_time(int64_t number, openshot::Fraction rate);
194 
201  std::vector<openshot::Clip*> find_intersecting_clips(int64_t requested_frame, int number_of_frames, bool include);
202 
204  std::shared_ptr<openshot::Frame> GetOrCreateFrame(std::shared_ptr<Frame> background_frame, openshot::Clip* clip, int64_t number, openshot::TimelineInfoStruct* options);
205 
207  bool isEqual(double a, double b);
208 
210  void sort_clips();
211 
213  void sort_effects();
214 
216  void update_open_clips(openshot::Clip *clip, bool does_clip_intersect);
217 
219  void BumpCacheEpoch();
220 
221  public:
222 
230  Timeline(int width, int height, openshot::Fraction fps, int sample_rate, int channels, openshot::ChannelLayout channel_layout);
231 
235 
243  Timeline(const std::string& projectPath, bool convert_absolute_paths);
244 
245  virtual ~Timeline();
246 
248  void AddTrackedObject(std::shared_ptr<openshot::TrackedObjectBase> trackedObject);
250  std::shared_ptr<openshot::TrackedObjectBase> GetTrackedObject(std::string id) const;
252  std::list<std::string> GetTrackedObjectsIds() const;
254  #ifdef USE_OPENCV
255  std::string GetTrackedObjectValues(std::string id, int64_t frame_number) const;
256  #endif
257 
260  void AddClip(openshot::Clip* clip);
261 
264  void AddEffect(openshot::EffectBase* effect);
265 
267  std::shared_ptr<openshot::Frame> apply_effects(std::shared_ptr<openshot::Frame> frame, int64_t timeline_frame_number, int layer, TimelineInfoStruct* options);
268 
270  void ApplyMapperToClips();
271 
273  bool AutoMapClips() { return auto_map_clips; };
274 
276  void AutoMapClips(bool auto_map) { auto_map_clips = auto_map; };
277 
279  void Clear();
280 
283  void ClearAllCache(bool deep=false);
284 
286  std::list<openshot::Clip*> Clips() override { return clips; };
287 
289  openshot::Clip* GetClip(const std::string& id);
290 
292  openshot::EffectBase* GetClipEffect(const std::string& id);
293 
295  openshot::EffectBase* GetEffect(const std::string& id);
296 
298  double GetMaxTime();
300  int64_t GetMaxFrame();
301 
303  double GetMinTime();
305  int64_t GetMinFrame();
306 
308  void Close() override;
309 
311  std::list<openshot::EffectBase*> Effects() { return effects; };
312 
314  std::list<openshot::EffectBase*> ClipEffects() const;
315 
317  openshot::CacheBase* GetCache() override { return final_cache; };
318 
321  void SetCache(openshot::CacheBase* new_cache);
322 
324  uint64_t CacheEpoch() const { return cache_epoch.load(std::memory_order_relaxed); };
325  int SafeEditFramesRemaining() const { return safe_edit_frames_remaining.load(std::memory_order_relaxed); };
326 
331  std::shared_ptr<openshot::Frame> GetFrame(int64_t requested_frame) override;
332 
333  // Curves for the viewport
337 
338  // Background color
340 
342  bool IsOpen() override { return is_open; };
343 
345  std::string Name() override { return "Timeline"; };
346 
347  // Get and Set JSON methods
348  std::string Json() const override;
349  void SetJson(const std::string value) override;
350  Json::Value JsonValue() const override;
351  void SetJsonValue(const Json::Value root) override;
352 
355  void SetMaxSize(int width, int height);
356 
361  void ApplyJsonDiff(std::string value);
362 
364  void Open() override;
365 
369 
372  void RemoveEffect(openshot::EffectBase* effect);
373 
376  void SortTimeline() { sort_clips(); sort_effects(); }
377  };
378 
379 }
380 
381 #endif // OPENSHOT_TIMELINE_H
openshot::Timeline::RemoveClip
void RemoveClip(openshot::Clip *clip)
Remove an openshot::Clip from the timeline.
Definition: Timeline.cpp:399
openshot::CompareClipEndFrames::operator()
bool operator()(const openshot::Clip *lhs, const openshot::Clip *rhs)
Definition: Timeline.h:75
TimelineBase.h
Header file for Timeline class.
openshot::Timeline::GetFrame
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame) override
Definition: Timeline.cpp:948
openshot::EffectBase
This abstract class is the base class, used by all effects in libopenshot.
Definition: EffectBase.h:56
openshot::Timeline::~Timeline
virtual ~Timeline()
Definition: Timeline.cpp:212
openshot::Timeline::viewport_x
openshot::Keyframe viewport_x
Curve representing the x coordinate for the viewport.
Definition: Timeline.h:335
openshot::CompareClipEndFrames
Definition: Timeline.h:74
openshot::Timeline::SetMaxSize
void SetMaxSize(int width, int height)
Definition: Timeline.cpp:1825
Clip.h
Header file for Clip class.
Fraction.h
Header file for Fraction class.
openshot::CompareEffectEndFrames::operator()
bool operator()(const openshot::EffectBase *lhs, const openshot::EffectBase *rhs)
Definition: Timeline.h:81
openshot::Timeline::Clips
std::list< openshot::Clip * > Clips() override
Return a list of clips on the timeline.
Definition: Timeline.h:286
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::Timeline::ApplyJsonDiff
void ApplyJsonDiff(std::string value)
Apply a special formatted JSON object, which represents a change to the timeline (add,...
Definition: Timeline.cpp:1370
openshot::Clip
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:89
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:30
openshot::ReaderBase::info
openshot::ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:88
openshot::Timeline::GetMinFrame
int64_t GetMinFrame()
Look up the start frame number of the first element on the timeline (first frame is 1)
Definition: Timeline.cpp:487
openshot::Timeline::ClearAllCache
void ClearAllCache(bool deep=false)
Definition: Timeline.cpp:1784
openshot::Timeline::GetTrackedObjectsIds
std::list< std::string > GetTrackedObjectsIds() const
Return the ID's of the tracked objects as a list of strings.
Definition: Timeline.cpp:264
openshot::CompareEffectEndFrames
Like CompareClipEndFrames, but for effects.
Definition: Timeline.h:80
EffectBase.h
Header file for EffectBase class.
openshot::ClipBase::Position
void Position(float value)
Set the Id of this clip object
Definition: ClipBase.cpp:19
KeyFrame.h
Header file for the Keyframe class.
openshot::Timeline::SafeEditFramesRemaining
int SafeEditFramesRemaining() const
Definition: Timeline.h:325
openshot::CacheBase
All cache managers in libopenshot are based on this CacheBase class.
Definition: CacheBase.h:34
openshot::Timeline::apply_effects
std::shared_ptr< openshot::Frame > apply_effects(std::shared_ptr< openshot::Frame > frame, int64_t timeline_frame_number, int layer, TimelineInfoStruct *options)
Apply global/timeline effects to the source frame (if any)
Definition: Timeline.cpp:551
openshot::ReaderBase::clip
openshot::ClipBase * clip
Pointer to the parent clip instance (if any)
Definition: ReaderBase.h:80
openshot::Color
This class represents a color (used on the timeline and clips)
Definition: Color.h:27
openshot::Timeline::GetCache
openshot::CacheBase * GetCache() override
Get the cache object used by this reader.
Definition: Timeline.h:317
openshot::Timeline::AutoMapClips
void AutoMapClips(bool auto_map)
Automatically map all clips to the timeline's framerate and samplerate.
Definition: Timeline.h:276
openshot::Timeline::GetClip
openshot::Clip * GetClip(const std::string &id)
Look up a single clip by ID.
Definition: Timeline.cpp:418
openshot::Keyframe
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:53
openshot::CompareClips::operator()
bool operator()(openshot::Clip *lhs, openshot::Clip *rhs) const
Definition: Timeline.h:49
openshot::Timeline::Open
void Open() override
Open the reader (and start consuming resources)
Definition: Timeline.cpp:936
openshot::Timeline::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Timeline.cpp:1248
openshot::Timeline::GetTrackedObjectValues
std::string GetTrackedObjectValues(std::string id, int64_t frame_number) const
Return the trackedObject's properties as a JSON string.
Definition: Timeline.cpp:280
openshot::Timeline::GetMaxTime
double GetMaxTime()
Look up the end time of the latest timeline element.
Definition: Timeline.cpp:473
openshot::Timeline
This class represents a timeline.
Definition: Timeline.h:153
openshot::Timeline::ClipEffects
std::list< openshot::EffectBase * > ClipEffects() const
Return the list of effects on all clips.
Definition: Timeline.cpp:454
openshot::ReaderInfo
This struct contains info about a media file, such as height, width, frames per second,...
Definition: ReaderBase.h:38
openshot::CompareEffects
Definition: Timeline.h:64
openshot::CompareEffects::operator()
bool operator()(openshot::EffectBase *lhs, openshot::EffectBase *rhs)
Definition: Timeline.h:65
openshot::Timeline::CacheEpoch
uint64_t CacheEpoch() const
Return the current cache invalidation epoch.
Definition: Timeline.h:324
openshot::TimelineInfoStruct
This struct contains info about the current Timeline clip instance.
Definition: TimelineBase.h:32
openshot::Timeline::Clear
void Clear()
Clear all clips, effects, and frame mappers from timeline (and free memory)
Definition: Timeline.cpp:868
openshot::Timeline::Name
std::string Name() override
Return the type name of the class.
Definition: Timeline.h:345
openshot::Timeline::RemoveEffect
void RemoveEffect(openshot::EffectBase *effect)
Remove an effect from the timeline.
Definition: Timeline.cpp:380
Frame.h
Header file for Frame class.
openshot::Timeline::Close
void Close() override
Close the timeline reader (and any resources it was consuming)
Definition: Timeline.cpp:914
openshot::Timeline::AddClip
void AddClip(openshot::Clip *clip)
Add an openshot::Clip to the timeline.
Definition: Timeline.cpp:338
openshot::EffectBase::Order
int Order() const
Get the order that this effect should be executed.
Definition: EffectBase.h:182
openshot::Timeline::AddTrackedObject
void AddTrackedObject(std::shared_ptr< openshot::TrackedObjectBase > trackedObject)
Add to the tracked_objects map a pointer to a tracked object (TrackedObjectBBox)
Definition: Timeline.cpp:229
ReaderBase.h
Header file for ReaderBase class.
openshot::Timeline::SortTimeline
void SortTimeline()
Sort all clips and effects on timeline - which affects the internal order of clips and effects arrays...
Definition: Timeline.h:376
openshot::Timeline::color
openshot::Color color
Background color of timeline canvas.
Definition: Timeline.h:339
openshot::Timeline::GetEffect
openshot::EffectBase * GetEffect(const std::string &id)
Look up a timeline effect by ID.
Definition: Timeline.cpp:430
openshot::CompareClips
Definition: Timeline.h:48
openshot::TimelineBase
This class represents a timeline (used for building generic timeline implementations)
Definition: TimelineBase.h:42
openshot::Timeline::Timeline
Timeline(int width, int height, openshot::Fraction fps, int sample_rate, int channels, openshot::ChannelLayout channel_layout)
Constructor for the timeline (which configures the default frame properties)
Definition: Timeline.cpp:34
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:75
openshot::Timeline::Effects
std::list< openshot::EffectBase * > Effects()
Return the list of effects on the timeline.
Definition: Timeline.h:311
openshot::Timeline::Json
std::string Json() const override
Generate JSON string of this object.
Definition: Timeline.cpp:1207
openshot::Timeline::GetMaxFrame
int64_t GetMaxFrame()
Look up the end frame number of the latest element on the timeline.
Definition: Timeline.cpp:479
openshot::Timeline::IsOpen
bool IsOpen() override
Determine if reader is open or closed.
Definition: Timeline.h:342
openshot::Timeline::AutoMapClips
bool AutoMapClips()
Determine if clips are automatically mapped to the timeline's framerate and samplerate.
Definition: Timeline.h:273
Color.h
Header file for Color class.
openshot::Timeline::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Timeline.cpp:1268
openshot::Timeline::ApplyMapperToClips
void ApplyMapperToClips()
Apply the timeline's framerate and samplerate to all clips.
Definition: Timeline.cpp:527
openshot::Timeline::viewport_y
openshot::Keyframe viewport_y
Curve representing the y coordinate for the viewport.
Definition: Timeline.h:336
openshot::ChannelLayout
ChannelLayout
This enumeration determines the audio channel layout (such as stereo, mono, 5 point surround,...
Definition: ChannelLayouts.h:28
TrackedObjectBBox.h
Header file for the TrackedObjectBBox class.
openshot::Timeline::GetClipEffect
openshot::EffectBase * GetClipEffect(const std::string &id)
Look up a clip effect by ID.
Definition: Timeline.cpp:441
TrackedObjectBase.h
Header file for the TrackedObjectBase class.
openshot::ClipBase::Layer
void Layer(int value)
Set layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.cpp:31
openshot::Timeline::viewport_scale
openshot::Keyframe viewport_scale
Curve representing the scale of the viewport (0 to 100)
Definition: Timeline.h:334
openshot::Timeline::AddEffect
void AddEffect(openshot::EffectBase *effect)
Add an effect to the timeline.
Definition: Timeline.cpp:364
openshot::Timeline::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Timeline.cpp:1214
openshot::Timeline::GetTrackedObject
std::shared_ptr< openshot::TrackedObjectBase > GetTrackedObject(std::string id) const
Return tracked object pointer by it's id.
Definition: Timeline.cpp:247
openshot::Timeline::GetMinTime
double GetMinTime()
Look up the position/start time of the first timeline element.
Definition: Timeline.cpp:495
openshot::Timeline::SetCache
void SetCache(openshot::CacheBase *new_cache)
Definition: Timeline.cpp:1191