Blender V5.3
effects.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 * SPDX-FileCopyrightText: 2003-2024 Blender Authors
3 * SPDX-FileCopyrightText: 2005-2006 Peter Schlaile <peter [at] schlaile [dot] de>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later */
6
10
11#include "BLI_math_filter.hh"
12
13#include "BKE_fcurve.hh"
14#include "BKE_scene.hh"
15
16#include "DNA_scene_types.h"
17#include "DNA_sequence_types.h"
18
20#include "IMB_imbuf.hh"
21#include "IMB_metadata.hh"
22
23#include "PRF_profile.hh"
24
25#include "RNA_prototypes.hh"
26
27#include "SEQ_render.hh"
28
29#include "effects.hh"
30#include "render.hh"
31
32namespace blender::seq {
33
35 const SeqResult &ibuf1,
36 const SeqResult &ibuf2,
37 bool uninitialized_pixels)
38{
39 PRF_scope_with_name("SeqFxPrepareImbufs", ProfileCategory::Draw);
41 Scene *scene = context->scene;
42 int x = context->rectx;
43 int y = context->recty;
44 ImBufFlags base_flags = uninitialized_pixels ? ImBufFlags::UninitializedPixels :
46
47 /* Convert everything to the sequencer colorspace before applying the effects. */
48 const ColorSpace *sequencer_colorspace = IMB_colormanagement_space_get_named(
50 const bool use_float =
51 (ibuf1.is_valid() &&
52 (ibuf1.image->float_data() || &ibuf1.image->byte_colorspace() != sequencer_colorspace)) ||
53 (ibuf2.is_valid() &&
54 (ibuf2.image->float_data() || &ibuf2.image->byte_colorspace() != sequencer_colorspace));
55
56 out.image = IMB_allocImBuf(
57 x, y, (use_float ? ImBufFlags::FloatData : ImBufFlags::ByteData) | base_flags);
59
60 if (use_float) {
61 if (ibuf1.is_valid()) {
62 ensure_ibuf_is_sequencer_space(scene, ibuf1.image, true);
63 }
64 if (ibuf2.is_valid()) {
65 ensure_ibuf_is_sequencer_space(scene, ibuf2.image, true);
66 }
67 }
68
69 /* If effect only affecting a single channel, forward input's metadata to the output. */
70 if (ibuf1.is_valid() && ibuf1.image == ibuf2.image) {
71 IMB_metadata_copy(out.image, ibuf1.image);
72 }
73
74 return out;
75}
76
78{
79 int n = 2 * size + 1;
80 Array<float> gaussian(n);
81
82 float sum = 0.0f;
83 float fac = (rad > 0.0f ? 1.0f / rad : 0.0f);
84 for (int i = -size; i <= size; i++) {
85 float val = math::filter_kernel_value(math::FilterKernel::Gauss, float(i) * fac);
86 sum += val;
87 gaussian[i + size] = val;
88 }
89
90 float inv_sum = 1.0f / sum;
91 for (int i = 0; i < n; i++) {
92 gaussian[i] *= inv_sum;
93 }
94
95 return gaussian;
96}
97
98static void init_noop(Strip * /*strip*/) {}
99
100static void copy_effect_default(Strip *dst, const Strip *src, const int /*flag*/)
101{
102 dst->effectdata = MEM_dupalloc_void(src->effectdata);
103}
104
105static StripEarlyOut early_out_noop(const Strip * /*strip*/, float /*fac*/)
106{
108}
109
110StripEarlyOut early_out_fade(const Strip * /*strip*/, float fac)
111{
112 if (fac == 0.0f) {
114 }
115 if (fac == 1.0f) {
117 }
119}
120
121StripEarlyOut early_out_mul_input2(const Strip * /*strip*/, float fac)
122{
123 if (fac == 0.0f) {
125 }
127}
128
129StripEarlyOut early_out_mul_input1(const Strip * /*strip*/, float fac)
130{
131 if (fac == 0.0f) {
133 }
135}
136
138{
139 if (strip->effectdata == nullptr) {
141 if (h.init != nullptr) {
142 h.init(strip);
143 }
144 }
145}
146
147void effect_free(Strip *strip)
148{
150 if (h.free != nullptr) {
151 h.free(strip, true);
152 BLI_assert(strip->effectdata == nullptr);
153 }
154}
155
157{
158 EffectHandle rval;
159
160 rval.init = init_noop;
161 rval.free = nullptr;
163 rval.execute = nullptr;
165
166 switch (strip_type) {
167 case STRIP_TYPE_CROSS:
169 break;
172 break;
175 break;
176 case STRIP_TYPE_ADD:
178 break;
179 case STRIP_TYPE_SUB:
181 break;
182 case STRIP_TYPE_MUL:
184 break;
187 break;
190 break;
193 break;
194 case STRIP_TYPE_WIPE:
196 break;
197 case STRIP_TYPE_GLOW:
199 break;
200 case STRIP_TYPE_SPEED:
202 break;
203 case STRIP_TYPE_COLOR:
205 break;
208 break;
211 break;
214 break;
215 case STRIP_TYPE_TEXT:
217 break;
218 default:
219 break;
220 }
221
222 return rval;
223}
224
226{
227 EffectHandle rval;
228
229 rval.init = init_noop;
230 rval.free = nullptr;
232 rval.execute = nullptr;
233 rval.copy = nullptr;
234
235 switch (blend) {
238 break;
239 case STRIP_BLEND_ADD:
241 break;
242 case STRIP_BLEND_SUB:
244 break;
247 break;
250 break;
253 break;
254 case STRIP_BLEND_MUL:
256 break;
269 case STRIP_BLEND_HUE:
276 break;
277 default:
278 break;
279 }
280
281 return rval;
282}
283
285{
286 EffectHandle h = {};
287 if (strip->is_effect()) {
288 h = effect_handle_get(strip->type);
289 }
290 return h;
291}
292
294{
295 EffectHandle h = {};
296 if (strip->blend_mode != STRIP_BLEND_REPLACE) {
297 h = effect_handle_for_blend_mode_get(strip->blend_mode);
298 }
299 return h;
300}
301
302static float transition_fader_calc(const Scene *scene, const Strip *strip, float timeline_frame)
303{
304 float fac = float(timeline_frame - strip->left_handle());
305 /* Compositor with no inputs can have strip->len not be updated,
306 * since most of existing editing code assumes no-input effects never need the length.
307 * So for the fader, just calculated it here directly. */
308 if (strip->type == STRIP_TYPE_COMPOSITOR) {
309 fac /= strip->enddisp - strip->startdisp;
310 }
311 else {
312 fac /= strip->length(scene);
313 }
314 fac = math::clamp(fac, 0.0f, 1.0f);
315 return fac;
316}
317
319 Strip *strip,
320 float timeline_frame,
321 const bool is_current_frame)
322{
323 if (strip->flag & SEQ_USE_EFFECT_DEFAULT_FADE) {
324 if (effect_is_transition(strip->type)) {
325 return transition_fader_calc(scene, strip, timeline_frame);
326 }
327 return 1.0f;
328 }
329
330 const FCurve *fcu = id_data_find_fcurve(
331 &scene->id, strip, RNA_Strip, "effect_fader", 0, nullptr);
332 if (fcu) {
333 /* At the current frame, a value that differs from the curve is an interactive user edit
334 * that has not been committed to the curve yet. Use it directly so that preview reflects
335 * this edited state. Note that we need to evaluate at the scene time, so that a retimed
336 * meta strip case is covered. */
337 if (is_current_frame &&
338 strip->effect_fader != evaluate_fcurve(fcu, BKE_scene_frame_get(scene)))
339 {
340 return strip->effect_fader;
341 }
342 return evaluate_fcurve(fcu, timeline_frame);
343 }
344 return strip->effect_fader;
345}
346
348{
349 if (!strip_type_is_effect(type)) {
350 return 0;
351 }
352
353 /* Zero input effects. Note: compositor is here too, but it supports
354 * any input count. */
355 if (ELEM(type,
361 {
362 return 0;
363 }
364
365 /* One input effects. */
367 return 1;
368 }
369
370 /* Others are two inputs. */
371 return 2;
372}
373
375{
376 return (type >= STRIP_TYPE_CROSS && type <= STRIP_TYPE_COMPOSITOR) ||
377 (type >= STRIP_TYPE_WIPE && type <= STRIP_TYPE_ADJUSTMENT) ||
379}
380
385
386} // namespace blender::seq
#define PRF_scope_with_name(ui_name, category)
#define ELEM(...)
T clamp(const T &a, const T &min, const T &max)
float filter_kernel_value(FilterKernel kernel, float x)
void ensure_ibuf_is_sequencer_space(const Scene *scene, ImBuf *ibuf, bool make_float)
Definition render.cc:170
EffectHandle strip_blend_mode_handle_get(Strip *strip)
Definition effects.cc:293
void mul_effect_get_handle(EffectHandle &rval)
static void copy_effect_default(Strip *dst, const Strip *src, const int)
Definition effects.cc:100
void effect_ensure_initialized(Strip *strip)
Definition effects.cc:137
EffectHandle strip_effect_handle_get(Strip *strip)
Definition effects.cc:284
void alpha_under_effect_get_handle(EffectHandle &rval)
void wipe_effect_get_handle(EffectHandle &rval)
void gaussian_blur_effect_get_handle(EffectHandle &rval)
void solid_color_effect_get_handle(EffectHandle &rval)
StripEarlyOut early_out_mul_input2(const Strip *, float fac)
Definition effects.cc:121
void text_effect_get_handle(EffectHandle &rval)
bool effect_is_transition(StripType type)
Definition effects.cc:381
void effect_free(Strip *strip)
Definition effects.cc:147
Array< float > make_gaussian_blur_kernel(float rad, int size)
Definition effects.cc:77
void gamma_cross_effect_get_handle(EffectHandle &rval)
void blend_mode_effect_get_handle(EffectHandle &rval)
SeqResult prepare_effect_imbufs(const RenderData *context, const SeqResult &ibuf1, const SeqResult &ibuf2, bool uninitialized_pixels)
Definition effects.cc:34
void compositor_effect_get_handle(EffectHandle &rval)
void cross_effect_get_handle(EffectHandle &rval)
void multi_camera_effect_get_handle(EffectHandle &rval)
static StripEarlyOut early_out_noop(const Strip *, float)
Definition effects.cc:105
void speed_effect_get_handle(EffectHandle &rval)
void add_effect_get_handle(EffectHandle &rval)
static float transition_fader_calc(const Scene *scene, const Strip *strip, float timeline_frame)
Definition effects.cc:302
StripEarlyOut early_out_mul_input1(const Strip *, float fac)
Definition effects.cc:129
static void init_noop(Strip *)
Definition effects.cc:98
EffectHandle effect_handle_get(StripType strip_type)
Definition effects.cc:156
void adjustment_effect_get_handle(EffectHandle &rval)
void sub_effect_get_handle(EffectHandle &rval)
static EffectHandle effect_handle_for_blend_mode_get(StripBlendMode blend)
Definition effects.cc:225
int effect_type_get_min_num_inputs(StripType type)
Definition effects.cc:347
void glow_effect_get_handle(EffectHandle &rval)
void color_mix_effect_get_handle(EffectHandle &rval)
float effect_fader_calc(Scene *scene, Strip *strip, float timeline_frame, const bool is_current_frame)
Definition effects.cc:318
void alpha_over_effect_get_handle(EffectHandle &rval)
void seq_imbuf_assign_sequencer_space(const Scene *scene, ImBuf *ibuf)
Definition render.cc:108
bool strip_type_is_effect(StripType type)
Definition effects.cc:374
StripEarlyOut early_out_fade(const Strip *, float fac)
Definition effects.cc:110
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void * MEM_dupalloc_void(const void *vmemh) ATTR_WARN_UNUSED_RESULT
float BKE_scene_frame_get(const Scene *scene)
Definition scene.cc:2595
@ SEQ_USE_EFFECT_DEFAULT_FADE
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
void IMB_metadata_copy(ImBuf *ibuf_dst, const ImBuf *ibuf_src)
const ColorSpace * IMB_colormanagement_space_get_named(const char *name)
FCurve * id_data_find_fcurve(ID *id, void *data, StructRNA *type, const char *prop_name, int index, bool *r_driven)
ocio::ColorSpace ColorSpace
Definition BLF_api.hh:42
float evaluate_fcurve(const FCurve *fcu, float evaltime)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const ColorSpace & byte_colorspace() const
Definition allocimbuf.cc:98
const float * float_data() const
ColorManagedColorspaceSettings sequencer_colorspace_settings
void(*) copy(Strip *dst, const Strip *src, int flag)
Definition effects.hh:44
void(*) free(Strip *strip, bool do_id_user)
Definition effects.hh:47
void(*) init(Strip *strip)
Definition effects.hh:41
StripEarlyOut(*) early_out(const Strip *strip, float fac)
Definition effects.hh:49
SeqResult(*) execute(const RenderData *context, SeqRenderState *state, Strip *strip, float timeline_frame, float fac, const SeqResult &input1, const SeqResult &input2)
Definition effects.hh:52
bool is_valid() const
Definition render.hh:54