OpenShot Library | libopenshot  0.7.0
Clip.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_CLIP_H
14 #define OPENSHOT_CLIP_H
15 
16 #ifdef USE_OPENCV
17  #define int64 opencv_broken_int
18  #define uint64 opencv_broken_uint
19  #include <opencv2/opencv.hpp>
20  #include <opencv2/core.hpp>
21  #undef uint64
22  #undef int64
23 
24 #endif
25 
26 #include <memory>
27 #include <string>
28 
29 #include "AudioLocation.h"
30 #include "ClipBase.h"
31 #include "ReaderBase.h"
32 
33 #include "Color.h"
34 #include "Enums.h"
35 #include "EffectBase.h"
36 #include "EffectInfo.h"
37 #include "KeyFrame.h"
38 #include "TrackedObjectBase.h"
39 
40 namespace openshot {
41  class AudioResampler;
42  class EffectInfo;
43  class Frame;
44 
50  if( lhs->Layer() < rhs->Layer() ) return true;
51  if( lhs->Layer() == rhs->Layer() && lhs->Position() < rhs->Position() ) return true;
52  if( lhs->Layer() == rhs->Layer() && lhs->Position() == rhs->Position() && lhs->Order() > rhs->Order() ) return true;
53  return false;
54  }};
55 
89  class Clip : public openshot::ClipBase, public openshot::ReaderBase {
90  protected:
92  std::recursive_mutex getFrameMutex;
93 
96 
98  void init_settings();
99 
101  void init_reader_settings();
102 
104  void init_reader_rotation();
105 
106  private:
107  enum class ReaderOrientationMode {
108  Reader,
109  LegacyClipTransform
110  };
111 
112  bool waveform;
113  int waveform_mode;
114  ReaderOrientationMode reader_orientation_mode;
115  std::list<openshot::EffectBase*> effects;
116  bool is_open;
117  std::string parentObjectId;
118  std::shared_ptr<openshot::TrackedObjectBase> parentTrackedObject;
119  openshot::Clip* parentClipObject;
120 
122  CacheMemory final_cache;
123 
124  // Audio resampler (if time mapping)
125  openshot::AudioResampler *resampler;
126 
127  // File Reader object
128  openshot::ReaderBase* reader;
129 
132  openshot::ReaderBase* allocated_reader;
133 
135  int64_t adjust_frame_number_minimum(int64_t frame_number);
136 
138  void apply_background(std::shared_ptr<openshot::Frame> frame,
139  std::shared_ptr<openshot::Frame> background_frame,
140  bool update_frame_image = true);
141 
143  void apply_effects(std::shared_ptr<openshot::Frame> frame, int64_t timeline_frame_number, TimelineInfoStruct* options, bool before_keyframes);
144 
146  void apply_keyframes(std::shared_ptr<Frame> frame, QSize timeline_size);
147 
149  void apply_waveform(std::shared_ptr<Frame> frame, QSize timeline_size);
150 
152  int64_t adjust_timeline_framenumber(int64_t clip_frame_number);
153 
155  QTransform get_transform(std::shared_ptr<Frame> frame, int width, int height);
156 
158  static std::string get_file_extension(std::string path);
159 
161  std::shared_ptr<openshot::Frame> GetOrCreateFrame(int64_t number, bool enable_time=true);
162 
164  double resolve_timeline_fps() const;
165 
167  int64_t curve_extent_frames() const;
168 
170  int64_t trim_extent_frames(double fps_value) const;
171 
173  void apply_timemapping(std::shared_ptr<openshot::Frame> frame);
174 
176  bool isNear(double a, double b);
177 
179  void sort_effects();
180 
182  QSize scale_size(QSize source_size, ScaleType source_scale, int target_width, int target_height);
183 
184  public:
191 
192  #ifdef USE_OPENCV
193  bool COMPILED_WITH_CV = true;
194  #else
195  bool COMPILED_WITH_CV = false;
196  #endif
197 
199  Clip();
200 
203  Clip(std::string path);
204 
207  static openshot::ReaderBase* CreateReader(std::string path, bool inspect_reader=true);
208 
211  Clip(openshot::ReaderBase* new_reader);
212 
214  virtual ~Clip();
215 
217  openshot::CacheMemory* GetCache() override { return &final_cache; };
218 
220  bool IsOpen() override { return is_open; };
221 
223  std::string GetAttachedId() const { return parentObjectId; };
225  void SetAttachedId(std::string value) { parentObjectId = value; };
226 
228  void AttachToObject(std::string object_id);
229 
231  void SetAttachedObject(std::shared_ptr<openshot::TrackedObjectBase> trackedObject);
233  void SetAttachedClip(Clip* clipObject);
235  std::shared_ptr<openshot::TrackedObjectBase> GetAttachedObject() const { return parentTrackedObject; };
237  Clip* GetAttachedClip() const { return parentClipObject; };
238 
240  std::string Name() override { return "Clip"; };
241 
244  void AddEffect(openshot::EffectBase* effect);
245 
247  void Close() override;
248 
251 
253  std::shared_ptr<openshot::TrackedObjectBase> GetParentTrackedObject();
254 
256  std::list<openshot::EffectBase*> Effects() { return effects; };
257 
259  openshot::EffectBase* GetEffect(const std::string& id);
260 
266  std::shared_ptr<openshot::Frame> GetFrame(int64_t clip_frame_number) override;
267 
278  std::shared_ptr<openshot::Frame> GetFrame(std::shared_ptr<openshot::Frame> background_frame, int64_t clip_frame_number) override;
279 
292  std::shared_ptr<openshot::Frame> GetFrame(std::shared_ptr<openshot::Frame> background_frame, int64_t clip_frame_number, openshot::TimelineInfoStruct* options);
293 
295  void Open() override;
296 
299  void Reader(openshot::ReaderBase* new_reader);
300 
303 
304  // Override End() position (in seconds) of clip (trim end of video)
305  float End() const override;
306  void End(float value) override;
307  openshot::TimelineBase* ParentTimeline() override { return timeline; }
308  void ParentTimeline(openshot::TimelineBase* new_timeline) override;
309 
310  // Get and Set JSON methods
311  std::string Json() const override;
312  void SetJson(const std::string value) override;
313  Json::Value JsonValue() const override;
314  void SetJsonValue(const Json::Value root) override;
315 
318  std::string PropertiesJSON(int64_t requested_frame) const override;
319 
322  void RemoveEffect(openshot::EffectBase* effect);
323 
324  // Waveform property
325  bool Waveform() { return waveform; }
326  void Waveform(bool value) { waveform = value; }
327  int WaveformMode() { return waveform_mode; }
328  void WaveformMode(int value) { waveform_mode = value; }
329 
330  // Scale, Location, and Alpha curves
336 
337  // Rotation and Shear curves (origin point (x,y) is adjustable for both rotation and shear)
343 
344  // Time and Volume curves
347 
350 
351  // Perspective curves
360 
361  // Audio channel filter and mappings
364 
365  // Override has_video and has_audio properties of clip (and their readers)
368  };
369 } // namespace
370 
371 #endif // OPENSHOT_CLIP_H
openshot::Clip::Open
void Open() override
Open the internal reader.
Definition: Clip.cpp:387
openshot::ClipBase::timeline
openshot::TimelineBase * timeline
Pointer to the parent timeline instance (if any)
Definition: ClipBase.h:40
openshot::EffectBase
This abstract class is the base class, used by all effects in libopenshot.
Definition: EffectBase.h:56
openshot::Clip::anchor
openshot::AnchorType anchor
The anchor determines what parent a clip should snap to.
Definition: Clip.h:187
openshot::Clip::CreateReader
static openshot::ReaderBase * CreateReader(std::string path, bool inspect_reader=true)
Definition: Clip.cpp:249
openshot::Clip::GetAttachedId
std::string GetAttachedId() const
Get and set the object id that this clip is attached to.
Definition: Clip.h:223
openshot::Clip::previous_location
AudioLocation previous_location
Previous time-mapped audio location.
Definition: Clip.h:95
openshot::Clip::GetCache
openshot::CacheMemory * GetCache() override
Get the cache object (always return NULL for this reader)
Definition: Clip.h:217
openshot::Clip::GetEffect
openshot::EffectBase * GetEffect(const std::string &id)
Look up an effect by ID.
Definition: Clip.cpp:540
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AnimatedCurve.h:24
openshot::Clip::scale_y
openshot::Keyframe scale_y
Curve representing the vertical scaling in percent (0 to 1)
Definition: Clip.h:332
openshot::Clip::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Clip.cpp:825
openshot::AudioLocation
This struct holds the associated video frame and starting sample # for an audio packet.
Definition: AudioLocation.h:25
openshot::Clip::time
openshot::Keyframe time
Curve representing the frames over time to play (used for speed and direction of video)
Definition: Clip.h:345
openshot::CompositeType
CompositeType
This enumeration determines how clips are composited onto lower layers.
Definition: Enums.h:75
openshot::Clip
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:89
openshot::Clip::alpha
openshot::Keyframe alpha
Curve representing the alpha (1 to 0)
Definition: Clip.h:335
openshot::Clip::End
float End() const override
Get end position (in seconds) of clip (trim end of video), which can be affected by the time curve.
Definition: Clip.cpp:423
AudioLocation.h
Header file for AudioLocation class.
openshot::Clip::origin_x
openshot::Keyframe origin_x
Curve representing X origin point (0.0=0% (left), 1.0=100% (right))
Definition: Clip.h:341
openshot::Clip::ParentTimeline
void ParentTimeline(openshot::TimelineBase *new_timeline) override
Set associated Timeline pointer.
Definition: Clip.cpp:450
openshot::Clip::GetFrame
std::shared_ptr< openshot::Frame > GetFrame(int64_t clip_frame_number) override
Get an openshot::Frame object for a specific frame number of this clip. The image size and number of ...
Definition: Clip.cpp:458
openshot::Clip::Close
void Close() override
Close the internal reader.
Definition: Clip.cpp:408
openshot::Clip::location_y
openshot::Keyframe location_y
Curve representing the relative Y position in percent based on the gravity (-1 to 1)
Definition: Clip.h:334
openshot::GravityType
GravityType
This enumeration determines how clips are aligned to their parent container.
Definition: Enums.h:21
openshot::Clip::origin_y
openshot::Keyframe origin_y
Curve representing Y origin point (0.0=0% (top), 1.0=100% (bottom))
Definition: Clip.h:342
openshot::Clip::GetParentTrackedObject
std::shared_ptr< openshot::TrackedObjectBase > GetParentTrackedObject()
Return the associated Parent Tracked Object (if any)
Definition: Clip.cpp:561
openshot::Clip::Effects
std::list< openshot::EffectBase * > Effects()
Return the list of effects on the timeline.
Definition: Clip.h:256
openshot::Clip::channel_mapping
openshot::Keyframe channel_mapping
A number representing an audio channel to output (only works when filtering a channel)
Definition: Clip.h:363
openshot::Clip::AddEffect
void AddEffect(openshot::EffectBase *effect)
Add an effect to the clip.
Definition: Clip.cpp:1269
openshot::Clip::~Clip
virtual ~Clip()
Destructor.
Definition: Clip.cpp:287
EffectBase.h
Header file for EffectBase class.
openshot::Clip::Json
std::string Json() const override
Generate JSON string of this object.
Definition: Clip.cpp:818
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::CompareClipEffects
Definition: Clip.h:48
openshot::Clip::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Clip.cpp:1043
openshot::Color
This class represents a color (used on the timeline and clips)
Definition: Color.h:27
openshot::Clip::display
openshot::FrameDisplayType display
The format to display the frame number (if any)
Definition: Clip.h:188
openshot::Clip::perspective_c2_y
openshot::Keyframe perspective_c2_y
Curves representing Y for coordinate 2.
Definition: Clip.h:355
openshot::Clip::scale_x
openshot::Keyframe scale_x
Curve representing the horizontal scaling in percent (0 to 1)
Definition: Clip.h:331
openshot::AudioResampler
This class is used to resample audio data for many sequential frames.
Definition: AudioResampler.h:30
openshot::Clip::SetAttachedId
void SetAttachedId(std::string value)
Set id of the object id that this clip is attached to.
Definition: Clip.h:225
openshot::Clip::perspective_c3_y
openshot::Keyframe perspective_c3_y
Curves representing Y for coordinate 3.
Definition: Clip.h:357
openshot::Clip::GetAttachedClip
Clip * GetAttachedClip() const
Return a pointer to the clip this clip is attached to.
Definition: Clip.h:237
openshot::Clip::perspective_c4_y
openshot::Keyframe perspective_c4_y
Curves representing Y for coordinate 4.
Definition: Clip.h:359
openshot::Clip::has_video
openshot::Keyframe has_video
An optional override to determine if this clip has video (-1=undefined, 0=no, 1=yes)
Definition: Clip.h:367
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::Clip::gravity
openshot::GravityType gravity
The gravity of a clip determines where it snaps to its parent.
Definition: Clip.h:185
openshot::Clip::Name
std::string Name() override
Return the type name of the class.
Definition: Clip.h:240
EffectInfo.h
Header file for the EffectInfo class.
openshot::CacheMemory
This class is a memory-based cache manager for Frame objects.
Definition: CacheMemory.h:29
openshot::Clip::composite
openshot::CompositeType composite
How this clip is composited onto lower layers.
Definition: Clip.h:190
openshot::Clip::perspective_c1_x
openshot::Keyframe perspective_c1_x
Curves representing X for coordinate 1.
Definition: Clip.h:352
openshot::Clip::init_settings
void init_settings()
Init default settings for a clip.
Definition: Clip.cpp:70
openshot::TimelineInfoStruct
This struct contains info about the current Timeline clip instance.
Definition: TimelineBase.h:32
path
path
Definition: FFmpegWriter.cpp:1481
openshot::Clip::IsOpen
bool IsOpen() override
Determine if reader is open or closed.
Definition: Clip.h:220
openshot::Clip::Reader
openshot::ReaderBase * Reader()
Get the current reader.
Definition: Clip.cpp:377
openshot::Clip::perspective_c2_x
openshot::Keyframe perspective_c2_x
Curves representing X for coordinate 2.
Definition: Clip.h:354
openshot::Clip::volume
openshot::Keyframe volume
Curve representing the volume (0 to 1)
Definition: Clip.h:346
openshot::EffectBase::Order
int Order() const
Get the order that this effect should be executed.
Definition: EffectBase.h:182
openshot::Clip::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Clip.cpp:1026
ReaderBase.h
Header file for ReaderBase class.
openshot::Clip::SetAttachedClip
void SetAttachedClip(Clip *clipObject)
Set the pointer to the clip this clip is attached to.
Definition: Clip.cpp:335
openshot::Clip::perspective_c4_x
openshot::Keyframe perspective_c4_x
Curves representing X for coordinate 4.
Definition: Clip.h:358
openshot::Clip::COMPILED_WITH_CV
bool COMPILED_WITH_CV
Definition: Clip.h:193
openshot::Clip::perspective_c1_y
openshot::Keyframe perspective_c1_y
Curves representing Y for coordinate 1.
Definition: Clip.h:353
openshot::Clip::channel_filter
openshot::Keyframe channel_filter
A number representing an audio channel to filter (clears all other channels)
Definition: Clip.h:362
openshot::Clip::init_reader_rotation
void init_reader_rotation()
Update default rotation from reader.
Definition: Clip.cpp:151
Enums.h
Header file for TextReader class.
openshot::Clip::init_reader_settings
void init_reader_settings()
Init reader info details.
Definition: Clip.cpp:138
openshot::TimelineBase
This class represents a timeline (used for building generic timeline implementations)
Definition: TimelineBase.h:41
openshot::Clip::Clip
Clip()
Default Constructor.
Definition: Clip.cpp:207
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:75
openshot::Clip::SetAttachedObject
void SetAttachedObject(std::shared_ptr< openshot::TrackedObjectBase > trackedObject)
Set the pointer to the trackedObject this clip is attached to.
Definition: Clip.cpp:330
openshot::Clip::scale
openshot::ScaleType scale
The scale determines how a clip should be resized to fit its parent.
Definition: Clip.h:186
Color.h
Header file for Color class.
openshot::AnchorType
AnchorType
This enumeration determines what parent a clip should be aligned to.
Definition: Enums.h:44
openshot::ScaleType
ScaleType
This enumeration determines how clips are scaled to fit their parent container.
Definition: Enums.h:35
openshot::Clip::AttachToObject
void AttachToObject(std::string object_id)
Attach clip to Tracked Object or to another Clip.
Definition: Clip.cpp:307
openshot::Clip::has_audio
openshot::Keyframe has_audio
An optional override to determine if this clip has audio (-1=undefined, 0=no, 1=yes)
Definition: Clip.h:366
openshot::Clip::GetParentClip
openshot::Clip * GetParentClip()
Return the associated ParentClip (if any)
Definition: Clip.cpp:552
openshot::Clip::rotation
openshot::Keyframe rotation
Curve representing the rotation (0 to 360)
Definition: Clip.h:338
openshot::CompareClipEffects::operator()
bool operator()(openshot::EffectBase *lhs, openshot::EffectBase *rhs)
Definition: Clip.h:49
openshot::Clip::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Clip.cpp:959
openshot::Clip::perspective_c3_x
openshot::Keyframe perspective_c3_x
Curves representing X for coordinate 3.
Definition: Clip.h:356
openshot::ClipBase
This abstract class is the base class, used by all clips in libopenshot.
Definition: ClipBase.h:32
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::VolumeMixType
VolumeMixType
This enumeration determines the strategy when mixing audio with other clips.
Definition: Enums.h:67
ClipBase.h
Header file for ClipBase class.
openshot::Clip::wave_color
openshot::Color wave_color
Curve representing the color of the audio wave form.
Definition: Clip.h:349
openshot::Clip::shear_y
openshot::Keyframe shear_y
Curve representing Y shear angle in degrees (-45.0=down, 45.0=up)
Definition: Clip.h:340
openshot::Clip::RemoveEffect
void RemoveEffect(openshot::EffectBase *effect)
Remove an effect from the clip.
Definition: Clip.cpp:1316
openshot::Clip::mixing
openshot::VolumeMixType mixing
What strategy should be followed when mixing audio with other clips.
Definition: Clip.h:189
openshot::Clip::GetAttachedObject
std::shared_ptr< openshot::TrackedObjectBase > GetAttachedObject() const
Return a pointer to the trackedObject this clip is attached to.
Definition: Clip.h:235
openshot::Clip::shear_x
openshot::Keyframe shear_x
Curve representing X shear angle in degrees (-45.0=left, 45.0=right)
Definition: Clip.h:339
openshot::Clip::location_x
openshot::Keyframe location_x
Curve representing the relative X position in percent based on the gravity (-1 to 1)
Definition: Clip.h:333
openshot::Clip::getFrameMutex
std::recursive_mutex getFrameMutex
Mutex for multiple threads.
Definition: Clip.h:92
openshot::FrameDisplayType
FrameDisplayType
This enumeration determines the display format of the clip's frame number (if any)....
Definition: Enums.h:51