Loading...
Searching...
No Matches
Timeline.h
Go to the documentation of this file.
// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#ifndef OPENSHOT_TIMELINE_H
#define OPENSHOT_TIMELINE_H
#include <list>
#include <memory>
#include <mutex>
#include <set>
#include <QtGui/QImage>
#include <QtGui/QPainter>
#include <QtCore/QRegularExpression>
#include "TimelineBase.h"
#include "ReaderBase.h"
#include "Color.h"
#include "Clip.h"
#include "EffectBase.h"
#include "Fraction.h"
#include "Frame.h"
#include "KeyFrame.h"
#ifdef USE_OPENCV
#include "TrackedObjectBBox.h"
#endif
#include "TrackedObjectBase.h"
namespace openshot {
// Forward decls
class FrameMapper;
class CacheBase;
struct CompareClips {
bool operator()(openshot::Clip* lhs, openshot::Clip* rhs) const {
// Strict-weak ordering (no <=) to keep sort well-defined
if (lhs == rhs) return false; // irreflexive
if (lhs->Layer() != rhs->Layer())
return lhs->Layer() < rhs->Layer();
if (lhs->Position() != rhs->Position())
return lhs->Position() < rhs->Position();
// Stable tie-breaker on address to avoid equivalence when layer/position match
return std::less<openshot::Clip*>()(lhs, rhs);
}
};
struct CompareEffects{
bool operator()( openshot::EffectBase* lhs, openshot::EffectBase* rhs){
if( lhs->Layer() < rhs->Layer() ) return true;
if( lhs->Layer() == rhs->Layer() && lhs->Position() < rhs->Position() ) return true;
if( lhs->Layer() == rhs->Layer() && lhs->Position() == rhs->Position() && lhs->Order() > rhs->Order() ) return true;
return false;
}};
struct CompareClipEndFrames {
bool operator()(const openshot::Clip* lhs, const openshot::Clip* rhs) {
return (lhs->Position() + lhs->Duration()) <= (rhs->Position() + rhs->Duration());
}};
struct CompareEffectEndFrames {
bool operator()(const openshot::EffectBase* lhs, const openshot::EffectBase* rhs) {
return (lhs->Position() + lhs->Duration()) <= (rhs->Position() + rhs->Duration());
}};
class Timeline : public openshot::TimelineBase, public openshot::ReaderBase {
private:
bool is_open;
bool auto_map_clips;
std::list<openshot::Clip*> clips;
std::list<openshot::Clip*> closing_clips;
std::map<openshot::Clip*, openshot::Clip*> open_clips;
std::set<openshot::Clip*> allocated_clips;
std::list<openshot::EffectBase*> effects;
std::set<openshot::EffectBase*> allocated_effects;
openshot::CacheBase *final_cache;
std::set<openshot::FrameMapper*> allocated_frame_mappers;
bool managed_cache;
std::string path;
double max_time;
double min_time;
std::map<std::string, std::shared_ptr<openshot::TrackedObjectBase>> tracked_objects;
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);
void apply_mapper_to_clip(openshot::Clip* clip);
// Apply JSON Diffs to various objects contained in this timeline
void apply_json_to_clips(Json::Value change);
void apply_json_to_effects(Json::Value change);
void apply_json_to_effects(Json::Value change, openshot::EffectBase* existing_effect);
void apply_json_to_timeline(Json::Value change);
void calculate_max_duration();
double calculate_time(int64_t number, openshot::Fraction rate);
std::vector<openshot::Clip*> find_intersecting_clips(int64_t requested_frame, int number_of_frames, bool include);
std::shared_ptr<openshot::Frame> GetOrCreateFrame(std::shared_ptr<Frame> background_frame, openshot::Clip* clip, int64_t number, openshot::TimelineInfoStruct* options);
bool isEqual(double a, double b);
void sort_clips();
void sort_effects();
void update_open_clips(openshot::Clip *clip, bool does_clip_intersect);
public:
Timeline(int width, int height, openshot::Fraction fps, int sample_rate, int channels, openshot::ChannelLayout channel_layout);
Timeline(ReaderInfo info);
Timeline(const std::string& projectPath, bool convert_absolute_paths);
virtual ~Timeline();
void AddTrackedObject(std::shared_ptr<openshot::TrackedObjectBase> trackedObject);
std::shared_ptr<openshot::TrackedObjectBase> GetTrackedObject(std::string id) const;
std::list<std::string> GetTrackedObjectsIds() const;
#ifdef USE_OPENCV
std::string GetTrackedObjectValues(std::string id, int64_t frame_number) const;
#endif
void AddClip(openshot::Clip* clip);
void AddEffect(openshot::EffectBase* effect);
std::shared_ptr<openshot::Frame> apply_effects(std::shared_ptr<openshot::Frame> frame, int64_t timeline_frame_number, int layer, TimelineInfoStruct* options);
void ApplyMapperToClips();
bool AutoMapClips() { return auto_map_clips; };
void AutoMapClips(bool auto_map) { auto_map_clips = auto_map; };
void Clear();
void ClearAllCache(bool deep=false);
std::list<openshot::Clip*> Clips() override { return clips; };
openshot::Clip* GetClip(const std::string& id);
openshot::EffectBase* GetClipEffect(const std::string& id);
openshot::EffectBase* GetEffect(const std::string& id);
double GetMaxTime();
int64_t GetMaxFrame();
double GetMinTime();
int64_t GetMinFrame();
void Close() override;
std::list<openshot::EffectBase*> Effects() { return effects; };
std::list<openshot::EffectBase*> ClipEffects() const;
openshot::CacheBase* GetCache() override { return final_cache; };
void SetCache(openshot::CacheBase* new_cache);
std::shared_ptr<openshot::Frame> GetFrame(int64_t requested_frame) override;
// Curves for the viewport
openshot::Keyframe viewport_scale;
openshot::Keyframe viewport_x;
openshot::Keyframe viewport_y;
// Background color
openshot::Color color;
bool IsOpen() override { return is_open; };
std::string Name() override { return "Timeline"; };
// Get and Set JSON methods
std::string Json() const override;
void SetJson(const std::string value) override;
Json::Value JsonValue() const override;
void SetJsonValue(const Json::Value root) override;
void SetMaxSize(int width, int height);
void ApplyJsonDiff(std::string value);
void Open() override;
void RemoveClip(openshot::Clip* clip);
void RemoveEffect(openshot::EffectBase* effect);
void SortTimeline() { sort_clips(); sort_effects(); }
};
}
#endif // OPENSHOT_TIMELINE_H
Clip.h
Header file for Clip class.
Color.h
Header file for Color class.
EffectBase.h
Header file for EffectBase class.
Fraction.h
Header file for Fraction class.
Frame.h
Header file for Frame class.
KeyFrame.h
Header file for the Keyframe class.
ReaderBase.h
Header file for ReaderBase class.
TimelineBase.h
Header file for Timeline class.
TrackedObjectBBox.h
Header file for the TrackedObjectBBox class.
TrackedObjectBase.h
Header file for the TrackedObjectBase class.
openshot::CacheBase
All cache managers in libopenshot are based on this CacheBase class.
Definition CacheBase.h:35
openshot::ClipBase::Duration
float Duration() const
Get the length of this clip (in seconds).
Definition ClipBase.h:90
openshot::ClipBase::Layer
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers).
Definition ClipBase.h:87
openshot::ClipBase::Position
float Position() const
Get position on timeline (in seconds).
Definition ClipBase.h:86
openshot::Clip
This class represents a clip (used to arrange readers on the timeline).
Definition Clip.h:89
openshot::Color
This class represents a color (used on the timeline and clips).
Definition Color.h:27
openshot::EffectBase
This abstract class is the base class, used by all effects in libopenshot.
Definition EffectBase.h:54
openshot::EffectBase::Order
int Order() const
Get the order that this effect should be executed.
Definition EffectBase.h:116
openshot::Fraction
This class represents a fraction.
Definition Fraction.h:30
openshot::FrameMapper
This class creates a mapping between 2 different frame rates, applying a specific pull-down technique...
Definition FrameMapper.h:193
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::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition ReaderBase.h:76
openshot::ReaderBase::info
openshot::ReaderInfo info
Information about the current media file.
Definition ReaderBase.h:88
openshot::ReaderBase::clip
openshot::ClipBase * clip
Pointer to the parent clip instance (if any).
Definition ReaderBase.h:80
openshot::TimelineBase
This class represents a timeline (used for building generic timeline implementations).
Definition TimelineBase.h:41
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:224
openshot::Timeline::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition Timeline.cpp:1194
openshot::Timeline::Name
std::string Name() override
Return the type name of the class.
Definition Timeline.h:334
openshot::Timeline::viewport_scale
openshot::Keyframe viewport_scale
Curve representing the scale of the viewport (0 to 100).
Definition Timeline.h:323
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:1347
openshot::Timeline::GetClipEffect
openshot::EffectBase * GetClipEffect(const std::string &id)
Look up a clip effect by ID.
Definition Timeline.cpp:436
openshot::Timeline::AddClip
void AddClip(openshot::Clip *clip)
Add an openshot::Clip to the timeline.
Definition Timeline.cpp:333
openshot::Timeline::~Timeline
virtual ~Timeline()
Definition Timeline.cpp:207
openshot::Timeline::ClipEffects
std::list< openshot::EffectBase * > ClipEffects() const
Return the list of effects on all clips.
Definition Timeline.cpp:449
openshot::Timeline::AutoMapClips
void AutoMapClips(bool auto_map)
Automatically map all clips to the timeline's framerate and samplerate.
Definition Timeline.h:269
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:259
openshot::Timeline::Json
std::string Json() const override
Generate JSON string of this object.
Definition Timeline.cpp:1187
openshot::Timeline::Effects
std::list< openshot::EffectBase * > Effects()
Return the list of effects on the timeline.
Definition Timeline.h:304
openshot::Timeline::GetMaxFrame
int64_t GetMaxFrame()
Look up the end frame number of the latest element on the timeline.
Definition Timeline.cpp:474
openshot::Timeline::GetMinTime
double GetMinTime()
Look up the position/start time of the first timeline element.
Definition Timeline.cpp:490
openshot::Timeline::GetFrame
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame) override
Definition Timeline.cpp:937
openshot::Timeline::AutoMapClips
bool AutoMapClips()
Determine if clips are automatically mapped to the timeline's framerate and samplerate.
Definition Timeline.h:266
openshot::Timeline::ApplyMapperToClips
void ApplyMapperToClips()
Apply the timeline's framerate and samplerate to all clips.
Definition Timeline.cpp:522
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:365
openshot::Timeline::color
openshot::Color color
Background color of timeline canvas.
Definition Timeline.h:328
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:275
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:31
openshot::Timeline::GetTrackedObject
std::shared_ptr< openshot::TrackedObjectBase > GetTrackedObject(std::string id) const
Return tracked object pointer by it's id.
Definition Timeline.cpp:242
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:482
openshot::Timeline::GetEffect
openshot::EffectBase * GetEffect(const std::string &id)
Look up a timeline effect by ID.
Definition Timeline.cpp:425
openshot::Timeline::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition Timeline.cpp:1248
openshot::Timeline::GetClip
openshot::Clip * GetClip(const std::string &id)
Look up a single clip by ID.
Definition Timeline.cpp:413
openshot::Timeline::ClearAllCache
void ClearAllCache(bool deep=false)
Definition Timeline.cpp:1746
openshot::Timeline::AddEffect
void AddEffect(openshot::EffectBase *effect)
Add an effect to the timeline.
Definition Timeline.cpp:359
openshot::Timeline::Clips
std::list< openshot::Clip * > Clips() override
Return a list of clips on the timeline.
Definition Timeline.h:279
openshot::Timeline::SetCache
void SetCache(openshot::CacheBase *new_cache)
Definition Timeline.cpp:1171
openshot::Timeline::IsOpen
bool IsOpen() override
Determine if reader is open or closed.
Definition Timeline.h:331
openshot::Timeline::GetCache
openshot::CacheBase * GetCache() override
Get the cache object used by this reader.
Definition Timeline.h:310
openshot::Timeline::Clear
void Clear()
Clear all clips, effects, and frame mappers from timeline (and free memory).
Definition Timeline.cpp:857
openshot::Timeline::viewport_x
openshot::Keyframe viewport_x
Curve representing the x coordinate for the viewport.
Definition Timeline.h:324
openshot::Timeline::RemoveClip
void RemoveClip(openshot::Clip *clip)
Remove an openshot::Clip from the timeline.
Definition Timeline.cpp:394
openshot::Timeline::SetMaxSize
void SetMaxSize(int width, int height)
Definition Timeline.cpp:1782
openshot::Timeline::GetMaxTime
double GetMaxTime()
Look up the end time of the latest timeline element.
Definition Timeline.cpp:468
openshot::Timeline::RemoveEffect
void RemoveEffect(openshot::EffectBase *effect)
Remove an effect from the timeline.
Definition Timeline.cpp:375
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:546
openshot::Timeline::Open
void Open() override
Open the reader (and start consuming resources).
Definition Timeline.cpp:925
openshot::Timeline::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition Timeline.cpp:1228
openshot::Timeline::viewport_y
openshot::Keyframe viewport_y
Curve representing the y coordinate for the viewport.
Definition Timeline.h:325
openshot::Timeline::Close
void Close() override
Close the timeline reader (and any resources it was consuming).
Definition Timeline.cpp:903
openshot
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
openshot::ChannelLayout
ChannelLayout
This enumeration determines the audio channel layout (such as stereo, mono, 5 point surround,...
Definition ChannelLayouts.h:29
openshot::CompareClipEndFrames
Definition Timeline.h:75
openshot::CompareClipEndFrames::operator()
bool operator()(const openshot::Clip *lhs, const openshot::Clip *rhs)
Definition Timeline.h:76
openshot::CompareClips
Definition Timeline.h:49
openshot::CompareClips::operator()
bool operator()(openshot::Clip *lhs, openshot::Clip *rhs) const
Definition Timeline.h:50
openshot::CompareEffectEndFrames
Like CompareClipEndFrames, but for effects.
Definition Timeline.h:81
openshot::CompareEffectEndFrames::operator()
bool operator()(const openshot::EffectBase *lhs, const openshot::EffectBase *rhs)
Definition Timeline.h:82
openshot::CompareEffects
Definition Timeline.h:65
openshot::CompareEffects::operator()
bool operator()(openshot::EffectBase *lhs, openshot::EffectBase *rhs)
Definition Timeline.h:66
openshot::ReaderInfo
This struct contains info about a media file, such as height, width, frames per second,...
Definition ReaderBase.h:39
openshot::TimelineInfoStruct
This struct contains info about the current Timeline clip instance.
Definition TimelineBase.h:33