Blender V5.3
node_composite_movie_clip.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_array.hh"
7
8#include "IMB_imbuf.hh"
9
10#include "BKE_context.hh"
11#include "BKE_lib_id.hh"
12#include "BKE_movieclip.hh"
13#include "BKE_tracking.hh"
14
15#include "RNA_access.hh"
16
17#include "UI_interface.hh"
19
20#include "GPU_texture.hh"
21
22#include "COM_node_operation.hh"
23#include "COM_utilities.hh"
24
26
28
30{
31 b.add_output<decl::Color>("Image"_ustr).structure_type(StructureType::Dynamic);
32 b.add_output<decl::Float>("Alpha"_ustr).structure_type(StructureType::Dynamic);
33 b.add_output<decl::Float>("Offset X"_ustr);
34 b.add_output<decl::Float>("Offset Y"_ustr);
35 b.add_output<decl::Float>("Scale"_ustr);
36 b.add_output<decl::Float>("Angle"_ustr);
37}
38
39static void node_init(bNodeTree * /*node_tree*/, bNode *node)
40{
41 MovieClipUser *user = MEM_new<MovieClipUser>(__func__);
42 node->storage = user;
43 user->framenr = 1;
44
45 node->flag |= NODE_PREVIEW;
46}
47
48static void node_init_api(const bContext *C, PointerRNA *node_ptr)
49{
50 bNode *node = node_ptr->data_as<bNode>();
51 Scene *scene = CTX_data_scene(C);
52 node->id = id_cast<ID *>(scene->clip);
53 id_us_plus(node->id);
54}
55
56static void node_draw_buttons(ui::Layout &layout, bContext *C, PointerRNA *ptr)
57{
58 template_id(&layout, C, ptr, "clip", nullptr, "CLIP_OT_open", nullptr);
59}
60
62{
63 layout.use_property_split_set(true);
64 layout.use_property_decorate_set(false);
65 uiTemplateMovieClip(&layout, C, ptr, "clip", false);
66}
67
68using namespace blender::compositor;
69
71 public:
73
74 void execute() override
75 {
76 ImBuf *movie_clip_buffer = get_movie_clip_buffer();
77
78 compute_image(movie_clip_buffer);
79 compute_alpha(movie_clip_buffer);
80 compute_stabilization_data(movie_clip_buffer);
81
82 IMB_freeImBuf(movie_clip_buffer);
83 }
84
85 void compute_image(ImBuf *movie_clip_buffer)
86 {
87 Result &result = this->get_result("Image");
88 if (!result.should_compute()) {
89 return;
90 }
91
92 if (!movie_clip_buffer) {
93 result.allocate_invalid();
94 return;
95 }
96
97 const int2 size = int2(movie_clip_buffer->x, movie_clip_buffer->y);
98 result.allocate_texture(Domain(size));
99
100 if (context().use_gpu()) {
101 GPU_texture_update(result, GPU_DATA_FLOAT, movie_clip_buffer->float_data());
102 }
103 else {
104 const float *src = movie_clip_buffer->float_data();
105 parallel_for(size, [&](const int2 texel) {
106 int64_t pixel_index = (int64_t(texel.y) * size.x + texel.x) * 4;
107 result.store_pixel(texel, Color(src + pixel_index));
108 });
109 }
110 }
111
112 void compute_alpha(ImBuf *movie_clip_buffer)
113 {
114 Result &result = get_result("Alpha");
115 if (!result.should_compute()) {
116 return;
117 }
118
119 if (!movie_clip_buffer) {
120 result.allocate_single_value();
121 result.set_single_value(1.0f);
122 return;
123 }
124
125 const int2 size = int2(movie_clip_buffer->x, movie_clip_buffer->y);
126 result.allocate_texture(Domain(size));
127
128 if (context().use_gpu()) {
129 Array<float> alpha_values(size.x * size.y);
130 const float *src = movie_clip_buffer->float_data();
131 parallel_for(size, [&](const int2 texel) {
132 int64_t pixel_index = int64_t(texel.y) * size.x + texel.x;
133 int64_t input_pixel_index = pixel_index * 4;
134 alpha_values[pixel_index] = src[input_pixel_index + 3];
135 });
137 }
138 else {
139 const float *src = movie_clip_buffer->float_data();
140 parallel_for(size, [&](const int2 texel) {
141 int64_t pixel_index = (int64_t(texel.y) * size.x + texel.x) * 4;
142 result.store_pixel(texel, src[pixel_index + 3]);
143 });
144 }
145 }
146
147 void compute_stabilization_data(ImBuf *movie_clip_buffer)
148 {
149 /* The movie clip buffer is invalid or missing, set appropriate fallback values. */
150 if (!movie_clip_buffer) {
151 Result &x_offset_result = this->get_result("Offset X");
152 if (x_offset_result.should_compute()) {
153 x_offset_result.allocate_single_value();
154 x_offset_result.set_single_value(0.0f);
155 }
156 Result &y_offset_result = this->get_result("Offset Y");
157 if (y_offset_result.should_compute()) {
158 y_offset_result.allocate_single_value();
159 y_offset_result.set_single_value(0.0f);
160 }
161 Result &scale_result = this->get_result("Scale");
162 if (scale_result.should_compute()) {
163 scale_result.allocate_single_value();
164 scale_result.set_single_value(1.0f);
165 }
166 Result &angle_result = this->get_result("Angle");
167 if (angle_result.should_compute()) {
168 angle_result.allocate_single_value();
169 angle_result.set_single_value(0.0f);
170 }
171 return;
172 }
173
174 MovieClip *movie_clip = get_movie_clip();
175 const int frame_number = BKE_movieclip_remap_scene_to_clip_frame(movie_clip,
176 context().get_frame_number());
177 const int width = movie_clip_buffer->x;
178 const int height = movie_clip_buffer->y;
179
180 /* If the movie clip has no stabilization data, it will initialize the given values with
181 * fallback values regardless, so no need to handle that case. */
182 float2 offset;
183 float scale, angle;
185 movie_clip, frame_number, width, height, offset, &scale, &angle);
186
187 Result &x_offset_result = this->get_result("Offset X");
188 if (x_offset_result.should_compute()) {
189 x_offset_result.allocate_single_value();
190 x_offset_result.set_single_value(offset.x);
191 }
192 Result &y_offset_result = this->get_result("Offset Y");
193 if (y_offset_result.should_compute()) {
194 y_offset_result.allocate_single_value();
195 y_offset_result.set_single_value(offset.y);
196 }
197 Result &scale_result = this->get_result("Scale");
198 if (scale_result.should_compute()) {
199 scale_result.allocate_single_value();
200 scale_result.set_single_value(scale);
201 }
202 Result &angle_result = this->get_result("Angle");
203 if (angle_result.should_compute()) {
204 angle_result.allocate_single_value();
205 angle_result.set_single_value(angle);
206 }
207 }
208
209 /* Get a float image buffer contacting the movie content at the current frame. If the movie clip
210 * does not exist or is invalid, return nullptr. */
212 {
213 MovieClip *movie_clip = get_movie_clip();
214 if (!movie_clip) {
215 return nullptr;
216 }
217
218 MovieClipUser *movie_clip_user = get_movie_clip_user();
219 BKE_movieclip_user_set_frame(movie_clip_user, context().get_frame_number());
220
221 ImBuf *movie_clip_buffer = BKE_movieclip_get_ibuf(movie_clip, movie_clip_user);
222 if (!movie_clip_buffer) {
223 return nullptr;
224 }
225
226 if (movie_clip_buffer->float_data()) {
227 return movie_clip_buffer;
228 }
229
230 /* Create a float buffer from the byte buffer if it exists, if not, return nullptr. */
231 IMB_float_from_byte(movie_clip_buffer);
232 if (!movie_clip_buffer->float_data()) {
233 return nullptr;
234 }
235
236 return movie_clip_buffer;
237 }
238
240 {
241 return reinterpret_cast<MovieClip *>(node().id);
242 }
243
245 {
246 return static_cast<MovieClipUser *>(node().storage);
247 }
248};
249
251{
252 return new MovieClipOperation(context, node);
253}
254
255static void node_register()
256{
257 static bke::bNodeType ntype;
258
259 cmp_node_type_base(&ntype, "CompositorNodeMovieClip"_ustr, CMP_NODE_MOVIECLIP);
260 ntype.ui_name = "Movie Clip";
261 ntype.ui_description =
262 "Input image or movie from a movie clip data-block, typically used for motion tracking";
263 ntype.enum_name_legacy = "MOVIECLIP";
264 ntype.nclass = NODE_CLASS_INPUT;
265 ntype.declare = node_declare;
269 ntype.initfunc = node_init;
271 ntype.flag |= NODE_PREVIEW;
274
276}
278
279} // namespace blender::nodes::node_composite_movie_clip_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define CMP_NODE_MOVIECLIP
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define C
Definition RandGen.cpp:29
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
const T * data() const
Definition BLI_array.hh:323
Domain(const int2 &size)
Definition domain.cc:20
NodeOperation(Context &context, const bNode &node)
Result & get_result(StringRef identifier)
Definition operation.cc:40
Context & context() const
Definition operation.cc:57
bool should_compute() const
Definition result.cc:1124
void set_single_value(const T &value)
float3x3 scale(float3x3 mat, float2 scale)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
void parallel_for(const int2 range, const Function &function)
static void node_init(bNodeTree *, bNode *node)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_draw_buttons(ui::Layout &layout, bContext *C, PointerRNA *ptr)
static void node_init_api(const bContext *C, PointerRNA *node_ptr)
static void node_draw_buttons_extended(ui::Layout &layout, bContext *C, PointerRNA *ptr)
static void node_declare(NodeDeclarationBuilder &b)
__int64 int64_t
Definition BKE_text.h:90
void GPU_texture_update(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
void id_us_plus(ID *id)
Definition lib_id.cc:433
void BKE_tracking_stabilization_data_get(MovieClip *clip, int framenr, int width, int height, float translation[2], float *scale, float *angle)
void IMB_float_from_byte(ImBuf *ibuf)
void IMB_freeImBuf(ImBuf *ibuf)
ImBuf * BKE_movieclip_get_ibuf(MovieClip *clip, const MovieClipUser *user)
void BKE_movieclip_user_set_frame(MovieClipUser *user, int framenr)
float BKE_movieclip_remap_scene_to_clip_frame(const MovieClip *clip, float framenr)
void uiTemplateMovieClip(ui::Layout *layout, bContext *C, PointerRNA *ptr, StringRefNull propname, bool compact)
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
Scene * CTX_data_scene(const bContext *C)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
const float * float_data() const
T * data_as() const
Definition RNA_types.hh:139
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
void(*) initfunc_api(const bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:333
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
void(*) draw_buttons_ex(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:292
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
void use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)