1 /**
2  * @file
3  * @brief Header file for ClipBase class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC
12  * <http://www.openshotstudios.com/>. This file is part of
13  * OpenShot Library (libopenshot), an open-source project dedicated to
14  * delivering high quality video editing and animation solutions to the
15  * world. For more information visit <http://www.openshot.org/>.
16  *
17  * OpenShot Library (libopenshot) is free software: you can redistribute it
18  * and/or modify it under the terms of the GNU Lesser General Public License
19  * as published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * OpenShot Library (libopenshot) is distributed in the hope that it will be
23  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #ifndef OPENSHOT_CLIPBASE_H
32 #define OPENSHOT_CLIPBASE_H
33 
34 #include <memory>
35 #include <sstream>
36 #include "CacheMemory.h"
37 #include "Frame.h"
38 #include "Point.h"
39 #include "KeyFrame.h"
40 #include "Json.h"
41 #include "TimelineBase.h"
42 
43 
44 namespace openshot {
45 	/**
46 	 * @brief This abstract class is the base class, used by all clips in libopenshot.
47 	 *
48 	 * Clips are objects that attach to the timeline and can be layered and positioned
49 	 * together. There are 2 primary types of clips: Effects and Video/Audio Clips.
50 	 */
51 	class ClipBase {
52 	protected:
53 		std::string id; ///< ID Property for all derived Clip and Effect classes.
54 		float position; ///< The position on the timeline where this clip should start playing
55 		int layer; ///< The layer this clip is on. Lower clips are covered up by higher clips.
56 		float start; ///< The position in seconds to start playing (used to trim the beginning of a clip)
57 		float end; ///< The position in seconds to end playing (used to trim the ending of a clip)
58 		std::string previous_properties; ///< This string contains the previous JSON properties
59 		openshot::TimelineBase* timeline; ///< Pointer to the parent timeline instance (if any)
60 
61 		/// Generate JSON for a property
62 		Json::Value add_property_json(std::string name, float value, std::string type, std::string memo, const Keyframe* keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame) const;
63 
64 		/// Generate JSON choice for a property (dropdown properties)
65 		Json::Value add_property_choice_json(std::string name, int value, int selected_value) const;
66 
67 	public:
68 		CacheMemory cache;
69 
70 		/// Constructor for the base clip
ClipBase()71 		ClipBase() {
72 			// Initialize values
73 			position = 0.0;
74 			layer = 0;
75 			start = 0.0;
76 			end = 0.0;
77 			previous_properties = "";
78 			timeline = NULL;
79 		};
80 
81 		// Compare a clip using the Position() property
82 		bool operator< ( ClipBase& a) { return (Position() < a.Position()); }
83 		bool operator<= ( ClipBase& a) { return (Position() <= a.Position()); }
84 		bool operator> ( ClipBase& a) { return (Position() > a.Position()); }
85 		bool operator>= ( ClipBase& a) { return (Position() >= a.Position()); }
86 
87 		/// @brief This method is required for all derived classes of ClipBase, and returns a
88 		/// new openshot::Frame object. All Clip keyframes and effects are resolved into
89 		/// pixels.
90 		///
91 		/// @returns A new openshot::Frame object
92 		/// @param frame_number The frame number (starting at 1) of the clip or effect on the timeline.
93 		virtual std::shared_ptr<openshot::Frame> GetFrame(int64_t frame_number) = 0;
94 
95 		/// @brief This method is required for all derived classes of ClipBase, and returns a
96 		/// modified openshot::Frame object
97 		///
98 		/// The frame object is passed into this method and used as a starting point / background (pixels).
99 		/// All Clip keyframes and effects are resolved into pixels.
100 		///
101 		/// @returns The modified openshot::Frame object
102 		/// @param frame This is ignored on Clip, due to caching optimizations. This frame instance is clobbered with the source frame.
103 		/// @param frame_number The frame number (starting at 1) of the clip or effect on the timeline.
104 		virtual std::shared_ptr<openshot::Frame> GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number) = 0;
105 
106 		// Get basic properties
Id()107 		std::string Id() const { return id; } ///< Get the Id of this clip object
Position()108 		float Position() const { return position; } ///< Get position on timeline (in seconds)
Layer()109 		int Layer() const { return layer; } ///< Get layer of clip on timeline (lower number is covered by higher numbers)
Start()110 		float Start() const { return start; } ///< Get start position (in seconds) of clip (trim start of video)
End()111 		float End() const { return end; } ///< Get end position (in seconds) of clip (trim end of video)
Duration()112 		float Duration() const { return end - start; } ///< Get the length of this clip (in seconds)
ParentTimeline()113 		openshot::TimelineBase* ParentTimeline() { return timeline; } ///< Get the associated Timeline pointer (if any)
114 
115 		// Set basic properties
Id(std::string value)116 		void Id(std::string value) { id = value; } ///> Set the Id of this clip object
Position(float value)117 		void Position(float value) { position = value; } ///< Set position on timeline (in seconds)
Layer(int value)118 		void Layer(int value) { layer = value; } ///< Set layer of clip on timeline (lower number is covered by higher numbers)
Start(float value)119 		void Start(float value) { start = value; } ///< Set start position (in seconds) of clip (trim start of video)
End(float value)120 		void End(float value) { end = value; } ///< Set end position (in seconds) of clip (trim end of video)
ParentTimeline(openshot::TimelineBase * new_timeline)121 		void ParentTimeline(openshot::TimelineBase* new_timeline) { timeline = new_timeline; } ///< Set associated Timeline pointer
122 
123 		// Get and Set JSON methods
124 		virtual std::string Json() const = 0; ///< Generate JSON string of this object
125 		virtual void SetJson(const std::string value) = 0; ///< Load JSON string into this object
126 		virtual Json::Value JsonValue() const = 0; ///< Generate Json::Value for this object
127 		virtual void SetJsonValue(const Json::Value root) = 0; ///< Load Json::Value into this object
128 
129 		/// Get all properties for a specific frame (perfect for a UI to display the current state
130 		/// of all properties at any time)
131 		virtual std::string PropertiesJSON(int64_t requested_frame) const = 0;
132 
133 		virtual ~ClipBase() = default;
134 	};
135 
136 
137 }
138 
139 #endif
140