OpenShot Library | libopenshot  0.5.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  bool waveform;
108  std::list<openshot::EffectBase*> effects;
109  bool is_open;
110  std::string parentObjectId;
111  std::shared_ptr<openshot::TrackedObjectBase> parentTrackedObject;
112  openshot::Clip* parentClipObject;
113 
115  CacheMemory final_cache;
116 
117  // Audio resampler (if time mapping)
118  openshot::AudioResampler *resampler;
119 
120  // File Reader object
121  openshot::ReaderBase* reader;
122 
125  openshot::ReaderBase* allocated_reader;
126 
128  int64_t adjust_frame_number_minimum(int64_t frame_number);
129 
131  void apply_background(std::shared_ptr<openshot::Frame> frame, std::shared_ptr<openshot::Frame> background_frame);
132 
134  void apply_effects(std::shared_ptr<openshot::Frame> frame, int64_t timeline_frame_number, TimelineInfoStruct* options, bool before_keyframes);
135 
137  void apply_keyframes(std::shared_ptr<Frame> frame, QSize timeline_size);
138 
140  void apply_waveform(std::shared_ptr<Frame> frame, QSize timeline_size);
141 
143  int64_t adjust_timeline_framenumber(int64_t clip_frame_number);
144 
146  QTransform get_transform(std::shared_ptr<Frame> frame, int width, int height);
147 
149  std::string get_file_extension(std::string path);
150 
152  std::shared_ptr<openshot::Frame> GetOrCreateFrame(int64_t number, bool enable_time=true);
153 
155  double resolve_timeline_fps() const;
156 
158  int64_t curve_extent_frames() const;
159 
161  int64_t trim_extent_frames(double fps_value) const;
162 
164  void apply_timemapping(std::shared_ptr<openshot::Frame> frame);
165 
167  bool isNear(double a, double b);
168 
170  void sort_effects();
171 
173  QSize scale_size(QSize source_size, ScaleType source_scale, int target_width, int target_height);
174 
175  public:
182 
183  #ifdef USE_OPENCV
184  bool COMPILED_WITH_CV = true;
185  #else
186  bool COMPILED_WITH_CV = false;
187  #endif
188 
190  Clip();
191 
194  Clip(std::string path);
195 
198  Clip(openshot::ReaderBase* new_reader);
199 
201  virtual ~Clip();
202 
204  openshot::CacheMemory* GetCache() override { return &final_cache; };
205 
207  bool IsOpen() override { return is_open; };
208 
210  std::string GetAttachedId() const { return parentObjectId; };
212  void SetAttachedId(std::string value) { parentObjectId = value; };
213 
215  void AttachToObject(std::string object_id);
216 
218  void SetAttachedObject(std::shared_ptr<openshot::TrackedObjectBase> trackedObject);
220  void SetAttachedClip(Clip* clipObject);
222  std::shared_ptr<openshot::TrackedObjectBase> GetAttachedObject() const { return parentTrackedObject; };
224  Clip* GetAttachedClip() const { return parentClipObject; };
225 
227  std::string Name() override { return "Clip"; };
228 
231  void AddEffect(openshot::EffectBase* effect);
232 
234  void Close() override;
235 
238 
240  std::shared_ptr<openshot::TrackedObjectBase> GetParentTrackedObject();
241 
243  std::list<openshot::EffectBase*> Effects() { return effects; };
244 
246  openshot::EffectBase* GetEffect(const std::string& id);
247 
253  std::shared_ptr<openshot::Frame> GetFrame(int64_t clip_frame_number) override;
254 
265  std::shared_ptr<openshot::Frame> GetFrame(std::shared_ptr<openshot::Frame> background_frame, int64_t clip_frame_number) override;
266 
279  std::shared_ptr<openshot::Frame> GetFrame(std::shared_ptr<openshot::Frame> background_frame, int64_t clip_frame_number, openshot::TimelineInfoStruct* options);
280 
282  void Open() override;
283 
286  void Reader(openshot::ReaderBase* new_reader);
287 
290 
291  // Override End() position (in seconds) of clip (trim end of video)
292  float End() const override;
293  void End(float value) override;
295  void ParentTimeline(openshot::TimelineBase* new_timeline) override;
296 
297  // Get and Set JSON methods
298  std::string Json() const override;
299  void SetJson(const std::string value) override;
300  Json::Value JsonValue() const override;
301  void SetJsonValue(const Json::Value root) override;
302 
305  std::string PropertiesJSON(int64_t requested_frame) const override;
306 
309  void RemoveEffect(openshot::EffectBase* effect);
310 
311  // Waveform property
312  bool Waveform() { return waveform; }
313  void Waveform(bool value) { waveform = value; }
314 
315  // Scale, Location, and Alpha curves
321 
322  // Rotation and Shear curves (origin point (x,y) is adjustable for both rotation and shear)
328 
329  // Time and Volume curves
332 
335 
336  // Perspective curves
345 
346  // Audio channel filter and mappings
349 
350  // Override has_video and has_audio properties of clip (and their readers)
353  };
354 } // namespace
355 
356 #endif // OPENSHOT_CLIP_H
Header file for AudioLocation class.
Header file for ClipBase class.
Header file for Color class.
Header file for EffectBase class.
Header file for the EffectInfo class.
Header file for TextReader class.
Header file for the Keyframe class.
Header file for ReaderBase class.
Header file for the TrackedObjectBase class.
This class is used to resample audio data for many sequential frames.
This class is a memory-based cache manager for Frame objects.
Definition: CacheMemory.h:29
This abstract class is the base class, used by all clips in libopenshot.
Definition: ClipBase.h:32
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:87
openshot::TimelineBase * timeline
Pointer to the parent timeline instance (if any)
Definition: ClipBase.h:40
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
void SetAttachedObject(std::shared_ptr< openshot::TrackedObjectBase > trackedObject)
Set the pointer to the trackedObject this clip is attached to.
Definition: Clip.cpp:327
openshot::Keyframe scale_x
Curve representing the horizontal scaling in percent (0 to 1)
Definition: Clip.h:316
openshot::Keyframe location_y
Curve representing the relative Y position in percent based on the gravity (-1 to 1)
Definition: Clip.h:319
std::list< openshot::EffectBase * > Effects()
Return the list of effects on the timeline.
Definition: Clip.h:243
openshot::Keyframe shear_x
Curve representing X shear angle in degrees (-45.0=left, 45.0=right)
Definition: Clip.h:324
openshot::Keyframe perspective_c4_x
Curves representing X for coordinate 4.
Definition: Clip.h:343
openshot::AnchorType anchor
The anchor determines what parent a clip should snap to.
Definition: Clip.h:178
openshot::VolumeMixType mixing
What strategy should be followed when mixing audio with other clips.
Definition: Clip.h:180
void Open() override
Open the internal reader.
Definition: Clip.cpp:383
std::string GetAttachedId() const
Get and set the object id that this clip is attached to.
Definition: Clip.h:210
openshot::Keyframe rotation
Curve representing the rotation (0 to 360)
Definition: Clip.h:323
openshot::Keyframe channel_filter
A number representing an audio channel to filter (clears all other channels)
Definition: Clip.h:347
openshot::FrameDisplayType display
The format to display the frame number (if any)
Definition: Clip.h:179
void init_reader_rotation()
Update default rotation from reader.
Definition: Clip.cpp:147
Clip()
Default Constructor.
Definition: Clip.cpp:198
openshot::Keyframe perspective_c1_x
Curves representing X for coordinate 1.
Definition: Clip.h:337
void AttachToObject(std::string object_id)
Attach clip to Tracked Object or to another Clip.
Definition: Clip.cpp:304
std::string Json() const override
Generate JSON string of this object.
Definition: Clip.cpp:812
openshot::TimelineBase * ParentTimeline() override
Get the associated Timeline pointer (if any)
Definition: Clip.h:294
openshot::EffectBase * GetEffect(const std::string &id)
Look up an effect by ID.
Definition: Clip.cpp:534
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Clip.cpp:1015
openshot::Keyframe alpha
Curve representing the alpha (1 to 0)
Definition: Clip.h:320
openshot::Keyframe has_audio
An optional override to determine if this clip has audio (-1=undefined, 0=no, 1=yes)
Definition: Clip.h:351
Clip * GetAttachedClip() const
Return a pointer to the clip this clip is attached to.
Definition: Clip.h:224
openshot::Keyframe perspective_c3_x
Curves representing X for coordinate 3.
Definition: Clip.h:341
void init_reader_settings()
Init reader info details.
Definition: Clip.cpp:134
openshot::Keyframe perspective_c1_y
Curves representing Y for coordinate 1.
Definition: Clip.h:338
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Clip.cpp:941
void SetAttachedClip(Clip *clipObject)
Set the pointer to the clip this clip is attached to.
Definition: Clip.cpp:332
openshot::Keyframe perspective_c4_y
Curves representing Y for coordinate 4.
Definition: Clip.h:344
openshot::Keyframe time
Curve representing the frames over time to play (used for speed and direction of video)
Definition: Clip.h:330
openshot::Clip * GetParentClip()
Return the associated ParentClip (if any)
Definition: Clip.cpp:546
bool Waveform()
Get the waveform property of this clip.
Definition: Clip.h:312
openshot::CompositeType composite
How this clip is composited onto lower layers.
Definition: Clip.h:181
openshot::GravityType gravity
The gravity of a clip determines where it snaps to its parent.
Definition: Clip.h:176
std::shared_ptr< openshot::TrackedObjectBase > GetAttachedObject() const
Return a pointer to the trackedObject this clip is attached to.
Definition: Clip.h:222
AudioLocation previous_location
Previous time-mapped audio location.
Definition: Clip.h:95
openshot::Keyframe perspective_c3_y
Curves representing Y for coordinate 3.
Definition: Clip.h:342
std::shared_ptr< openshot::TrackedObjectBase > GetParentTrackedObject()
Return the associated Parent Tracked Object (if any)
Definition: Clip.cpp:555
void AddEffect(openshot::EffectBase *effect)
Add an effect to the clip.
Definition: Clip.cpp:1210
void Close() override
Close the internal reader.
Definition: Clip.cpp:404
void Waveform(bool value)
Set the waveform property of this clip.
Definition: Clip.h:313
virtual ~Clip()
Destructor.
Definition: Clip.cpp:284
openshot::Keyframe perspective_c2_y
Curves representing Y for coordinate 2.
Definition: Clip.h:340
openshot::Keyframe volume
Curve representing the volume (0 to 1)
Definition: Clip.h:331
openshot::Keyframe shear_y
Curve representing Y shear angle in degrees (-45.0=down, 45.0=up)
Definition: Clip.h:325
openshot::Keyframe scale_y
Curve representing the vertical scaling in percent (0 to 1)
Definition: Clip.h:317
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:419
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:454
std::string Name() override
Return the type name of the class.
Definition: Clip.h:227
openshot::ReaderBase * Reader()
Get the current reader.
Definition: Clip.cpp:373
void RemoveEffect(openshot::EffectBase *effect)
Remove an effect from the clip.
Definition: Clip.cpp:1257
void SetAttachedId(std::string value)
Set id of the object id that this clip is attached to.
Definition: Clip.h:212
openshot::Keyframe channel_mapping
A number representing an audio channel to output (only works when filtering a channel)
Definition: Clip.h:348
openshot::Keyframe has_video
An optional override to determine if this clip has video (-1=undefined, 0=no, 1=yes)
Definition: Clip.h:352
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Clip.cpp:819
bool COMPILED_WITH_CV
Definition: Clip.h:184
openshot::Color wave_color
Curve representing the color of the audio wave form.
Definition: Clip.h:334
void init_settings()
Init default settings for a clip.
Definition: Clip.cpp:68
openshot::Keyframe perspective_c2_x
Curves representing X for coordinate 2.
Definition: Clip.h:339
openshot::ScaleType scale
The scale determines how a clip should be resized to fit its parent.
Definition: Clip.h:177
openshot::Keyframe location_x
Curve representing the relative X position in percent based on the gravity (-1 to 1)
Definition: Clip.h:318
openshot::CacheMemory * GetCache() override
Get the cache object (always return NULL for this reader)
Definition: Clip.h:204
openshot::Keyframe origin_x
Curve representing X origin point (0.0=0% (left), 1.0=100% (right))
Definition: Clip.h:326
std::recursive_mutex getFrameMutex
Mutex for multiple threads.
Definition: Clip.h:92
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Clip.cpp:998
openshot::Keyframe origin_y
Curve representing Y origin point (0.0=0% (top), 1.0=100% (bottom))
Definition: Clip.h:327
bool IsOpen() override
Determine if reader is open or closed.
Definition: Clip.h:207
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
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
This class represents a timeline (used for building generic timeline implementations)
Definition: TimelineBase.h:41
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:29
AnchorType
This enumeration determines what parent a clip should be aligned to.
Definition: Enums.h:45
GravityType
This enumeration determines how clips are aligned to their parent container.
Definition: Enums.h:22
ScaleType
This enumeration determines how clips are scaled to fit their parent container.
Definition: Enums.h:36
VolumeMixType
This enumeration determines the strategy when mixing audio with other clips.
Definition: Enums.h:68
FrameDisplayType
This enumeration determines the display format of the clip's frame number (if any)....
Definition: Enums.h:52
CompositeType
This enumeration determines how clips are composited onto lower layers.
Definition: Enums.h:75
This struct holds the associated video frame and starting sample # for an audio packet.
Definition: AudioLocation.h:25
bool operator()(openshot::EffectBase *lhs, openshot::EffectBase *rhs)
Definition: Clip.h:49
This struct contains info about the current Timeline clip instance.
Definition: TimelineBase.h:33