OpenShot Library | libopenshot  0.3.3
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()) <= (rhs->Position() + rhs->Duration());
72  }};
73 
76  bool operator()(const openshot::EffectBase* lhs, const openshot::EffectBase* rhs) {
77  return (lhs->Position() + lhs->Duration()) <= (rhs->Position() + rhs->Duration());
78  }};
79 
149  private:
150  bool is_open;
151  bool auto_map_clips;
152  std::list<openshot::Clip*> clips;
153  std::list<openshot::Clip*> closing_clips;
154  std::map<openshot::Clip*, openshot::Clip*> open_clips;
155  std::set<openshot::Clip*> allocated_clips;
156  std::list<openshot::EffectBase*> effects;
157  std::set<openshot::EffectBase*> allocated_effects;
158  openshot::CacheBase *final_cache;
159  std::set<openshot::FrameMapper*> allocated_frame_mappers;
160  bool managed_cache;
161  std::string path;
162  int max_concurrent_frames;
163  double max_time;
164 
165  std::map<std::string, std::shared_ptr<openshot::TrackedObjectBase>> tracked_objects;
166 
168  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);
169 
171  void apply_mapper_to_clip(openshot::Clip* clip);
172 
173  // Apply JSON Diffs to various objects contained in this timeline
174  void apply_json_to_clips(Json::Value change);
175  void apply_json_to_effects(Json::Value change);
176  void apply_json_to_effects(Json::Value change, openshot::EffectBase* existing_effect);
177  void apply_json_to_timeline(Json::Value change);
178 
180  void calculate_max_duration();
181 
183  double calculate_time(int64_t number, openshot::Fraction rate);
184 
191  std::vector<openshot::Clip*> find_intersecting_clips(int64_t requested_frame, int number_of_frames, bool include);
192 
194  std::shared_ptr<openshot::Frame> GetOrCreateFrame(std::shared_ptr<Frame> background_frame, openshot::Clip* clip, int64_t number, openshot::TimelineInfoStruct* options);
195 
197  bool isEqual(double a, double b);
198 
200  void sort_clips();
201 
203  void sort_effects();
204 
206  void update_open_clips(openshot::Clip *clip, bool does_clip_intersect);
207 
208  public:
209 
217  Timeline(int width, int height, openshot::Fraction fps, int sample_rate, int channels, openshot::ChannelLayout channel_layout);
218 
222 
230  Timeline(const std::string& projectPath, bool convert_absolute_paths);
231 
232  virtual ~Timeline();
233 
235  void AddTrackedObject(std::shared_ptr<openshot::TrackedObjectBase> trackedObject);
237  std::shared_ptr<openshot::TrackedObjectBase> GetTrackedObject(std::string id) const;
239  std::list<std::string> GetTrackedObjectsIds() const;
241  #ifdef USE_OPENCV
242  std::string GetTrackedObjectValues(std::string id, int64_t frame_number) const;
243  #endif
244 
247  void AddClip(openshot::Clip* clip);
248 
251  void AddEffect(openshot::EffectBase* effect);
252 
254  std::shared_ptr<openshot::Frame> apply_effects(std::shared_ptr<openshot::Frame> frame, int64_t timeline_frame_number, int layer, TimelineInfoStruct* options);
255 
257  void ApplyMapperToClips();
258 
260  bool AutoMapClips() { return auto_map_clips; };
261 
263  void AutoMapClips(bool auto_map) { auto_map_clips = auto_map; };
264 
266  void Clear();
267 
270  void ClearAllCache(bool deep=false);
271 
273  std::list<openshot::Clip*> Clips() override { return clips; };
274 
276  openshot::Clip* GetClip(const std::string& id);
277 
279  openshot::EffectBase* GetClipEffect(const std::string& id);
280 
282  openshot::EffectBase* GetEffect(const std::string& id);
283 
285  double GetMaxTime();
287  int64_t GetMaxFrame();
288 
290  void Close() override;
291 
293  std::list<openshot::EffectBase*> Effects() { return effects; };
294 
296  std::list<openshot::EffectBase*> ClipEffects() const;
297 
299  openshot::CacheBase* GetCache() override { return final_cache; };
300 
303  void SetCache(openshot::CacheBase* new_cache);
304 
309  std::shared_ptr<openshot::Frame> GetFrame(int64_t requested_frame) override;
310 
311  // Curves for the viewport
315 
316  // Background color
318 
320  bool IsOpen() override { return is_open; };
321 
323  std::string Name() override { return "Timeline"; };
324 
325  // Get and Set JSON methods
326  std::string Json() const override;
327  void SetJson(const std::string value) override;
328  Json::Value JsonValue() const override;
329  void SetJsonValue(const Json::Value root) override;
330 
333  void SetMaxSize(int width, int height);
334 
339  void ApplyJsonDiff(std::string value);
340 
342  void Open() override;
343 
347 
350  void RemoveEffect(openshot::EffectBase* effect);
351 
354  void SortTimeline() { sort_clips(); sort_effects(); }
355  };
356 
357 }
358 
359 #endif // OPENSHOT_TIMELINE_H
Header file for Clip class.
Header file for Color class.
Header file for EffectBase class.
Header file for Fraction class.
Header file for Frame class.
Header file for the Keyframe class.
Header file for ReaderBase class.
Header file for Timeline class.
Header file for the TrackedObjectBBox class.
Header file for the TrackedObjectBase class.
All cache managers in libopenshot are based on this CacheBase class.
Definition: CacheBase.h:35
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:90
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:87
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:86
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:89
This class represents a color (used on the timeline and clips)
Definition: Color.h:27
This abstract class is the base class, used by all effects in libopenshot.
Definition: EffectBase.h:54
int Order() const
Get the order that this effect should be executed.
Definition: EffectBase.h:116
This class represents a fraction.
Definition: Fraction.h:30
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:53
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:76
openshot::ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:88
openshot::ClipBase * clip
Pointer to the parent clip instance (if any)
Definition: ReaderBase.h:80
This class represents a timeline (used for building generic timeline implementations)
Definition: TimelineBase.h:41
This class represents a timeline.
Definition: Timeline.h:148
void AddTrackedObject(std::shared_ptr< openshot::TrackedObjectBase > trackedObject)
Add to the tracked_objects map a pointer to a tracked object (TrackedObjectBBox)
Definition: Timeline.cpp:223
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Timeline.cpp:1120
std::string Name() override
Return the type name of the class.
Definition: Timeline.h:323
openshot::Keyframe viewport_scale
Curve representing the scale of the viewport (0 to 100)
Definition: Timeline.h:312
void ApplyJsonDiff(std::string value)
Apply a special formatted JSON object, which represents a change to the timeline (add,...
Definition: Timeline.cpp:1269
openshot::EffectBase * GetClipEffect(const std::string &id)
Look up a clip effect by ID.
Definition: Timeline.cpp:431
void AddClip(openshot::Clip *clip)
Add an openshot::Clip to the timeline.
Definition: Timeline.cpp:332
virtual ~Timeline()
Definition: Timeline.cpp:206
std::list< openshot::EffectBase * > ClipEffects() const
Return the list of effects on all clips.
Definition: Timeline.cpp:444
void AutoMapClips(bool auto_map)
Automatically map all clips to the timeline's framerate and samplerate.
Definition: Timeline.h:263
openshot::CacheBase * GetCache() override
Get the cache object used by this reader.
Definition: Timeline.h:299
std::list< std::string > GetTrackedObjectsIds() const
Return the ID's of the tracked objects as a list of strings.
Definition: Timeline.cpp:258
std::string Json() const override
Generate JSON string of this object.
Definition: Timeline.cpp:1113
int64_t GetMaxFrame()
Look up the end frame number of the latest element on the timeline.
Definition: Timeline.cpp:469
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame) override
Definition: Timeline.cpp:888
std::list< openshot::EffectBase * > Effects()
Return the list of effects on the timeline.
Definition: Timeline.h:293
bool AutoMapClips()
Determine if clips are automatically mapped to the timeline's framerate and samplerate.
Definition: Timeline.h:260
void ApplyMapperToClips()
Apply the timeline's framerate and samplerate to all clips.
Definition: Timeline.cpp:502
void SortTimeline()
Sort all clips and effects on timeline - which affects the internal order of clips and effects arrays...
Definition: Timeline.h:354
openshot::Color color
Background color of timeline canvas.
Definition: Timeline.h:317
std::string GetTrackedObjectValues(std::string id, int64_t frame_number) const
Return the trackedObject's properties as a JSON string.
Definition: Timeline.cpp:274
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:28
std::shared_ptr< openshot::TrackedObjectBase > GetTrackedObject(std::string id) const
Return tracked object pointer by it's id.
Definition: Timeline.cpp:241
openshot::EffectBase * GetEffect(const std::string &id)
Look up a timeline effect by ID.
Definition: Timeline.cpp:420
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Timeline.cpp:1174
openshot::Clip * GetClip(const std::string &id)
Look up a single clip by ID.
Definition: Timeline.cpp:408
void ClearAllCache(bool deep=false)
Definition: Timeline.cpp:1668
void AddEffect(openshot::EffectBase *effect)
Add an effect to the timeline.
Definition: Timeline.cpp:358
void SetCache(openshot::CacheBase *new_cache)
Definition: Timeline.cpp:1097
bool IsOpen() override
Determine if reader is open or closed.
Definition: Timeline.h:320
void Clear()
Clear all clips, effects, and frame mappers from timeline (and free memory)
Definition: Timeline.cpp:808
openshot::Keyframe viewport_x
Curve representing the x coordinate for the viewport.
Definition: Timeline.h:313
void RemoveClip(openshot::Clip *clip)
Remove an openshot::Clip from the timeline.
Definition: Timeline.cpp:388
void SetMaxSize(int width, int height)
Definition: Timeline.cpp:1698
double GetMaxTime()
Look up the end time of the latest timeline element.
Definition: Timeline.cpp:463
void RemoveEffect(openshot::EffectBase *effect)
Remove an effect from the timeline.
Definition: Timeline.cpp:371
std::list< openshot::Clip * > Clips() override
Return a list of clips on the timeline.
Definition: Timeline.h:273
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:526
void Open() override
Open the reader (and start consuming resources)
Definition: Timeline.cpp:876
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Timeline.cpp:1154
openshot::Keyframe viewport_y
Curve representing the y coordinate for the viewport.
Definition: Timeline.h:314
void Close() override
Close the timeline reader (and any resources it was consuming)
Definition: Timeline.cpp:854
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:29
ChannelLayout
This enumeration determines the audio channel layout (such as stereo, mono, 5 point surround,...
bool operator()(const openshot::Clip *lhs, const openshot::Clip *rhs)
Definition: Timeline.h:70
bool operator()(openshot::Clip *lhs, openshot::Clip *rhs)
Definition: Timeline.h:50
Like CompareClipEndFrames, but for effects.
Definition: Timeline.h:75
bool operator()(const openshot::EffectBase *lhs, const openshot::EffectBase *rhs)
Definition: Timeline.h:76
bool operator()(openshot::EffectBase *lhs, openshot::EffectBase *rhs)
Definition: Timeline.h:60
This struct contains info about a media file, such as height, width, frames per second,...
Definition: ReaderBase.h:39
This struct contains info about the current Timeline clip instance.
Definition: TimelineBase.h:33