Blender V5.3
effect_speed.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_fcurve.hh"
10
11#include "DNA_scene_types.h"
12#include "DNA_sequence_types.h"
13
14#include "IMB_imbuf.hh"
15
16#include "PRF_profile.hh"
17
18#include "RNA_prototypes.hh"
19
20#include "SEQ_render.hh"
21#include "SEQ_time.hh"
22
23#include "effects.hh"
24#include "render.hh"
25
26namespace blender::seq {
27
28static void init_speed_effect(Strip *strip)
29{
30 SpeedControlVars *data = MEM_new<SpeedControlVars>("speedcontrolvars");
31 strip->effectdata = data;
32 data->speed_control_type = SEQ_SPEED_STRETCH;
33 data->speed_fader = 1.0f;
34 data->speed_fader_length = 0.0f;
35 data->speed_fader_frame_number = 0.0f;
36}
37
38static void free_speed_effect(Strip *strip, const bool /*do_id_user*/)
39{
40 if (strip->effectdata) {
41 SpeedControlVars *v = static_cast<SpeedControlVars *>(strip->effectdata);
42 if (v->frameMap) {
43 MEM_delete(v->frameMap);
44 }
45 MEM_delete(v);
46 strip->effectdata = nullptr;
47 }
48}
49
50static void copy_speed_effect(Strip *dst, const Strip *src, const int /*flag*/)
51{
52 SpeedControlVars *v = MEM_dupalloc(static_cast<SpeedControlVars *>(src->effectdata));
53 v->frameMap = nullptr;
54 dst->effectdata = v;
55}
56
57static StripEarlyOut early_out_speed(const Strip * /*strip*/, float /*fac*/)
58{
60}
61
63{
64 return id_data_find_fcurve(&scene->id, strip, RNA_Strip, "speed_factor", 0, nullptr);
65}
66
68{
69 const int effect_strip_length = strip->right_handle(scene) - strip->left_handle();
70
71 if ((strip->input1 == nullptr) || (effect_strip_length < 1)) {
72 return; /* Make COVERITY happy and check for (CID 598) input strip. */
73 }
74
75 const FCurve *fcu = strip_effect_speed_speed_factor_curve_get(scene, strip);
76 if (fcu == nullptr) {
77 return;
78 }
79
80 SpeedControlVars *v = static_cast<SpeedControlVars *>(strip->effectdata);
81 if (v->frameMap) {
82 MEM_delete(v->frameMap);
83 }
84
85 v->frameMap = MEM_new_array_uninitialized<float>(size_t(effect_strip_length), __func__);
86 v->frameMap[0] = 0.0f;
87
88 float target_frame = 0;
89 for (int frame_index = 1; frame_index < effect_strip_length; frame_index++) {
90 target_frame += evaluate_fcurve(fcu, strip->left_handle() + frame_index);
91 const int target_frame_max = strip->input1->length(scene);
92 CLAMP(target_frame, 0, target_frame_max);
93 v->frameMap[frame_index] = target_frame;
94 }
95}
96
98{
99 const SpeedControlVars *v = static_cast<SpeedControlVars *>(strip->effectdata);
100 if (v->frameMap != nullptr) {
101 return;
102 }
103
104 strip_effect_speed_rebuild_map(scene, strip);
105}
106
108 Strip *strip_speed,
109 float timeline_frame,
110 int input)
111{
112 if (strip_speed->input1 == nullptr) {
113 return 0.0f;
114 }
115
116 strip_effect_handle_get(strip_speed); /* Ensure, that data are initialized. */
117 int frame_index = round_fl_to_int(give_frame_index(scene, strip_speed, timeline_frame));
118 SpeedControlVars *s = static_cast<SpeedControlVars *>(strip_speed->effectdata);
119 const Strip *source = strip_speed->input1;
120
121 float target_frame = 0.0f;
122 switch (s->speed_control_type) {
123 case SEQ_SPEED_STRETCH: {
124 /* Only right handle controls effect speed! */
125 const float target_content_length = source->length(scene) - source->startofs;
126 const float speed_effetct_length = strip_speed->right_handle(scene) -
127 strip_speed->left_handle();
128 const float ratio = frame_index / speed_effetct_length;
129 target_frame = target_content_length * ratio;
130 break;
131 }
132 case SEQ_SPEED_MULTIPLY: {
133 const FCurve *fcu = strip_effect_speed_speed_factor_curve_get(scene, strip_speed);
134 if (fcu != nullptr) {
135 strip_effect_speed_frame_map_ensure(scene, strip_speed);
136 target_frame = s->frameMap[frame_index];
137 }
138 else {
139 target_frame = frame_index * s->speed_fader;
140 if (s->speed_fader < 0) {
141 /* Treat `target_frame` as a negative offset from the last frame of the strip. */
142 target_frame += source->length(scene);
143 }
144 }
145 break;
146 }
147 case SEQ_SPEED_LENGTH:
148 target_frame = source->length(scene) * (s->speed_fader_length / 100.0f);
149 break;
151 target_frame = s->speed_fader_frame_number;
152 break;
153 }
154
155 CLAMP(target_frame, 0, source->length(scene));
156 target_frame += strip_speed->start;
157
158 /* No interpolation. */
159 if ((s->flags & SEQ_SPEED_USE_INTERPOLATION) == 0) {
160 return target_frame;
161 }
162
163 /* Interpolation is used, switch between current and next frame based on which input is
164 * requested. */
165 return input == 0 ? target_frame : ceil(target_frame);
166}
167
169 Strip *strip_speed,
170 float timeline_frame)
171{
172 const float target_frame = strip_speed_effect_target_frame_get(
173 scene, strip_speed, timeline_frame, 0);
174 return target_frame - floor(target_frame);
175}
176
179 Strip *strip,
180 float timeline_frame,
181 float fac,
182 const SeqResult &ibuf1,
183 const SeqResult &ibuf2)
184{
187
188 const SpeedControlVars *s = static_cast<SpeedControlVars *>(strip->effectdata);
190
192 fac = speed_effect_interpolation_ratio_get(context->scene, strip, timeline_frame);
193 /* Current frame is ibuf1, next frame is ibuf2. */
194 out = cross_effect.execute(context, state, nullptr, timeline_frame, fac, ibuf1, ibuf2);
195 return out;
196 }
197
198 /* No interpolation. */
199 out.image = IMB_dupImBuf(ibuf1.image);
200 out.is_opaque_before_transform = !ibuf1.image->can_contain_alpha();
201 return out;
202}
203
212
213} // namespace blender::seq
#define PRF_scope_with_name(ui_name, category)
#define CLAMP(a, b, c)
#define floor
#define ceil
const int state
static float speed_effect_interpolation_ratio_get(Scene *scene, Strip *strip_speed, float timeline_frame)
static SeqResult do_speed_effect(const RenderData *context, SeqRenderState *state, Strip *strip, float timeline_frame, float fac, const SeqResult &ibuf1, const SeqResult &ibuf2)
EffectHandle strip_effect_handle_get(Strip *strip)
Definition effects.cc:284
static void free_speed_effect(Strip *strip, const bool)
float give_frame_index(const Scene *scene, const Strip *strip, float timeline_frame)
Definition strip_time.cc:43
static void copy_speed_effect(Strip *dst, const Strip *src, const int)
static void init_speed_effect(Strip *strip)
static FCurve * strip_effect_speed_speed_factor_curve_get(Scene *scene, Strip *strip)
static void strip_effect_speed_frame_map_ensure(Scene *scene, Strip *strip)
float strip_speed_effect_target_frame_get(Scene *scene, Strip *strip_speed, float timeline_frame, int input)
void speed_effect_get_handle(EffectHandle &rval)
void strip_effect_speed_rebuild_map(Scene *scene, Strip *strip)
EffectHandle effect_handle_get(StripType strip_type)
Definition effects.cc:156
static StripEarlyOut early_out_speed(const Strip *, float)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
MINLINE int round_fl_to_int(float a)
ATTR_WARN_UNUSED_RESULT const BMVert * v
ImBuf * IMB_dupImBuf(const ImBuf *ibuf1)
BMesh const char void * data
@ SEQ_SPEED_USE_INTERPOLATION
FCurve * id_data_find_fcurve(ID *id, void *data, StructRNA *type, const char *prop_name, int index, bool *r_driven)
float evaluate_fcurve(const FCurve *fcu, float evaltime)
bool can_contain_alpha() const
eEffectSpeedControlType speed_control_type
eEffectSpeedControlFlags flags
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