Blender V5.3
sequencer/intern/utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 * SPDX-FileCopyrightText: 2003-2009 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 <algorithm>
12#include <cstdlib>
13#include <cstring>
14
15#include "MEM_guardedalloc.h"
16
17#include "DNA_scene_types.h"
18#include "DNA_sequence_types.h"
19
20#include "RNA_path.hh"
21
22#include "BLI_listbase.hh"
23#include "BLI_string.hh"
24#include "BLI_string_utf8.hh"
25#include "BLI_string_utils.hh"
26
27#include "BLT_translation.hh"
28
29#include "BKE_animsys.hh"
30#include "BKE_idprop.hh"
31#include "BKE_image.hh"
32#include "BKE_library.hh"
33#include "BKE_scene.hh"
34
35#include "SEQ_channels.hh"
36#include "SEQ_edit.hh"
37#include "SEQ_iterator.hh"
38#include "SEQ_relations.hh"
39#include "SEQ_render.hh"
40#include "SEQ_select.hh"
41#include "SEQ_sequencer.hh"
42#include "SEQ_utils.hh"
43
44#include "IMB_imbuf_types.hh"
45
46#include "MOV_read.hh"
47
49#include "utils.hh"
50
51namespace blender::seq {
52
60
61static void seqbase_unique_name(ListBaseT<Strip> *seqbasep, StripUniqueInfo *sui)
62{
63 for (Strip &strip : *seqbasep) {
64 if ((sui->strip != &strip) && STREQ(sui->name_dest, strip.name + 2)) {
65 /* STRIP_NAME_MAXSTR -4 for the number, -1 for \0, - 2 for r_prefix */
67 sui->name_dest, "%.*s.%03d", STRIP_NAME_MAXSTR - 4 - 1 - 2, sui->name_src, sui->count++);
68 sui->match = 1; /* be sure to re-scan */
69 }
70 }
71}
72
73static bool seqbase_unique_name_recursive_fn(Strip *strip, void *arg_pt)
74{
75 if (strip->seqbase.first_) {
76 seqbase_unique_name(&strip->seqbase, static_cast<StripUniqueInfo *>(arg_pt));
77 }
78 return true;
79}
80
81void strip_unique_name_set(Scene *scene, ListBaseT<Strip> *seqbasep, Strip *strip)
82{
84 char *dot;
85 sui.strip = strip;
86 STRNCPY(sui.name_src, strip->name + 2);
87 STRNCPY(sui.name_dest, sui.name_src);
88 sui.count = 1;
89 sui.match = 1; /* assume the worst to start the loop */
90
91 /* Strip off the suffix only if it is purely numeric. */
92 if ((dot = strrchr(sui.name_src, '.'))) {
93 char *suffix = dot + 1;
94 if (BLI_string_is_decimal(suffix)) {
95 *dot = '\0';
96 sui.count = atoi(suffix) + 1;
97 }
98 }
99
100 while (sui.match) {
101 sui.match = 0;
102 seqbase_unique_name(seqbasep, &sui);
104 }
105
106 edit_strip_name_set(scene, strip, sui.name_dest);
107}
108
109const char *get_default_stripname_by_type(int type)
110{
111 switch (type) {
112 case STRIP_TYPE_META:
114 case STRIP_TYPE_IMAGE:
115 return CTX_DATA_(BLT_I18NCONTEXT_ID_SEQUENCE, "Image");
116 case STRIP_TYPE_SCENE:
117 return CTX_DATA_(BLT_I18NCONTEXT_ID_SEQUENCE, "Scene");
118 case STRIP_TYPE_MOVIE:
119 return CTX_DATA_(BLT_I18NCONTEXT_ID_SEQUENCE, "Movie");
122 case STRIP_TYPE_MASK:
124 case STRIP_TYPE_SOUND:
125 return CTX_DATA_(BLT_I18NCONTEXT_ID_SEQUENCE, "Audio");
126 case STRIP_TYPE_CROSS:
127 return CTX_DATA_(BLT_I18NCONTEXT_ID_SEQUENCE, "Crossfade");
129 return CTX_DATA_(BLT_I18NCONTEXT_ID_SEQUENCE, "Gamma Crossfade");
131 return CTX_DATA_(BLT_I18NCONTEXT_ID_SEQUENCE, "Compositor");
132 case STRIP_TYPE_ADD:
134 case STRIP_TYPE_SUB:
135 return CTX_DATA_(BLT_I18NCONTEXT_ID_SEQUENCE, "Subtract");
136 case STRIP_TYPE_MUL:
137 return CTX_DATA_(BLT_I18NCONTEXT_ID_SEQUENCE, "Multiply");
139 return CTX_DATA_(BLT_I18NCONTEXT_ID_SEQUENCE, "Alpha Over");
141 return CTX_DATA_(BLT_I18NCONTEXT_ID_SEQUENCE, "Alpha Under");
143 return CTX_DATA_(BLT_I18NCONTEXT_ID_SEQUENCE, "Color Mix");
144 case STRIP_TYPE_WIPE:
146 case STRIP_TYPE_GLOW:
148 case STRIP_TYPE_COLOR:
149 return CTX_DATA_(BLT_I18NCONTEXT_ID_SEQUENCE, "Color");
151 return CTX_DATA_(BLT_I18NCONTEXT_ID_SEQUENCE, "Multicam");
153 return CTX_DATA_(BLT_I18NCONTEXT_ID_SEQUENCE, "Adjustment");
154 case STRIP_TYPE_SPEED:
155 return CTX_DATA_(BLT_I18NCONTEXT_ID_SEQUENCE, "Speed");
157 return CTX_DATA_(BLT_I18NCONTEXT_ID_SEQUENCE, "Gaussian Blur");
158 case STRIP_TYPE_TEXT:
160 default:
161 return nullptr;
162 }
163}
164
165const char *strip_give_name(const Strip *strip)
166{
167 const char *name = get_default_stripname_by_type(strip->type);
168
169 if (!name) {
170 if (!strip->is_effect()) {
171 return strip->data->dirpath;
172 }
173
174 return DATA_("Effect");
175 }
176 return name;
177}
178
179ListBaseT<Strip> *get_seqbase_from_strip(Strip *strip,
180 ListBaseT<SeqTimelineChannel> **r_channels,
181 int *r_offset)
182{
183 ListBaseT<Strip> *seqbase = nullptr;
184
185 switch (strip->type) {
186 case STRIP_TYPE_META: {
187 seqbase = &strip->seqbase;
188 *r_channels = &strip->channels;
189 *r_offset = strip->content_start();
190 break;
191 }
192 case STRIP_TYPE_SCENE: {
193 if (strip->flag & SEQ_SCENE_STRIPS && strip->scene) {
194 Editing *ed = editing_get(strip->scene);
195 if (ed) {
196 seqbase = &ed->seqbase;
197 *r_channels = &ed->channels;
198 *r_offset = strip->scene->r.sfra;
199 }
200 }
201 break;
202 }
203 default:
204 break;
205 }
206
207 return seqbase;
208}
209
211{
212 MOV_close(reader);
213}
214
216{
217 if (strip.runtime->movie_metadata != nullptr) {
218 IDP_FreeProperty(strip.runtime->movie_metadata);
219 }
220 strip.runtime->movie_metadata = nullptr;
221 strip.runtime->movie_metadata_is_loaded = false;
222}
223
225{
227 const IDProperty *metadata = MOV_load_metadata(&reader);
228 strip.runtime->movie_metadata = metadata != nullptr ? IDP_CopyProperty(metadata) : nullptr;
229 strip.runtime->movie_metadata_is_loaded = true;
230}
231
233{
234 if (!strip.runtime->movie_metadata_is_loaded) {
236 if (reader) {
237 movie_metadata_set_from_reader(strip, *reader.reader());
238 }
239 }
240 return strip.runtime->movie_metadata;
241}
242
243const Strip *strip_topmost_get(const Scene *scene, int frame)
244{
245 Editing *ed = scene->ed;
246
247 if (!ed) {
248 return nullptr;
249 }
250
251 ListBaseT<SeqTimelineChannel> *channels = channels_displayed_get(ed);
252 const Strip *best_strip = nullptr;
253 int best_channel = -1;
254
255 for (const Strip &strip : *ed->current_strips()) {
256 if (render_is_muted(channels, &strip) || !strip.intersects_frame(scene, frame)) {
257 continue;
258 }
259 /* Only use strips that generate an image, not ones that combine
260 * other strips or apply some effect. */
261 if (ELEM(strip.type,
268 {
269 if (strip.channel > best_channel) {
270 best_strip = &strip;
271 best_channel = strip.channel;
272 }
273 }
274 }
275 return best_strip;
276}
277
278ListBaseT<Strip> *get_seqbase_by_strip(const Scene *scene, Strip *strip)
279{
280 Editing *ed = editing_get(scene);
281 ListBaseT<Strip> *main_seqbase = &ed->seqbase;
282 Strip *strip_meta = lookup_meta_by_strip(ed, strip);
283
284 if (strip_meta != nullptr) {
285 return &strip_meta->seqbase;
286 }
287 if (BLI_findindex(main_seqbase, strip) != -1) {
288 return main_seqbase;
289 }
290 return nullptr;
291}
292
293Strip *strip_from_strip_elem(ListBaseT<Strip> *seqbase, StripElem *se)
294{
295 Strip *istrip;
296
297 for (istrip = seqbase->first(); istrip; istrip = istrip->next) {
298 Strip *strip_found;
299 if ((istrip->data && istrip->data->stripdata) &&
300 ARRAY_HAS_ITEM(se, istrip->data->stripdata, istrip->content_length()))
301 {
302 break;
303 }
304 if ((strip_found = strip_from_strip_elem(&istrip->seqbase, se))) {
305 istrip = strip_found;
306 break;
307 }
308 }
309
310 return istrip;
311}
312
313Strip *get_strip_by_name(ListBaseT<Strip> *seqbase, const char *name, bool recursive)
314{
315 for (Strip &istrip : *seqbase) {
316 if (STREQ(name, istrip.name + 2)) {
317 return &istrip;
318 }
319 if (recursive && !istrip.seqbase.is_empty()) {
320 Strip *rseq = get_strip_by_name(&istrip.seqbase, name, true);
321 if (rseq != nullptr) {
322 return rseq;
323 }
324 }
325 }
326
327 return nullptr;
328}
329
331{
332 Strip *strip_act = select_active_get(scene);
333
334 if (strip_act && strip_act->type == STRIP_TYPE_MASK) {
335 return strip_act->mask;
336 }
337
338 return nullptr;
339}
340
342{
343 if (strip->data && strip->data->stripdata) {
344 const char *filename = strip->data->stripdata->filename;
346 }
347}
348
349bool strip_has_valid_data(const Strip *strip)
350{
351 switch (strip->type) {
352 case STRIP_TYPE_MASK:
353 return (strip->mask != nullptr);
355 return (strip->clip != nullptr);
356 case STRIP_TYPE_SCENE:
357 return (strip->scene != nullptr);
358 case STRIP_TYPE_SOUND:
359 return (strip->sound != nullptr);
360 default:
361 return true;
362 }
363}
364
366{
367 switch (strip->type) {
368 case STRIP_TYPE_IMAGE:
369 case STRIP_TYPE_SCENE:
370 case STRIP_TYPE_MOVIE:
372 case STRIP_TYPE_MASK:
373 case STRIP_TYPE_COLOR:
374 case STRIP_TYPE_TEXT:
375 return true;
376 default:
377 return false;
378 }
379}
380
381void set_scale_to_fit(const Strip *strip,
382 const int image_width,
383 const int image_height,
384 const int preview_width,
385 const int preview_height,
386 const eSeqImageFitMethod fit_method)
387{
388 StripTransform *transform = strip->data->transform;
389
390 switch (fit_method) {
391 case SEQ_SCALE_TO_FIT:
392 transform->scale_x = transform->scale_y = std::min(
393 float(preview_width) / float(image_width), float(preview_height) / float(image_height));
394
395 break;
397
398 transform->scale_x = transform->scale_y = std::max(
399 float(preview_width) / float(image_width), float(preview_height) / float(image_height));
400 break;
402 transform->scale_x = float(preview_width) / float(image_width);
403 transform->scale_y = float(preview_height) / float(image_height);
404 break;
406 transform->scale_x = 1.0f;
407 transform->scale_y = 1.0f;
408 break;
409 }
410}
411
412void ensure_unique_name(Strip *strip, Scene *scene, const DriverMap &driver_map)
413{
414 char name[STRIP_NAME_MAXSTR];
415
416 STRNCPY_UTF8(name, strip->name + 2);
417 strip_unique_name_set(scene, &scene->ed->seqbase, strip);
419 "sequence_editor.strips_all",
421 RNA_path_name_to_infix(strip->name + 2),
422 /*verify_paths=*/false,
423 driver_map);
424
425 if (strip->type == STRIP_TYPE_META) {
426 for (Strip &strip_child : strip->seqbase) {
427 ensure_unique_name(&strip_child, scene, driver_map);
428 }
429 }
430}
431
432} // namespace blender::seq
#define SNPRINTF(dst, format,...)
#define STRNCPY_UTF8(dst, src)
#define BLT_I18NCONTEXT_ID_SEQUENCE
#define CTX_DATA_(context, msgid)
#define DATA_(msgid)
#define STRIP_NAME_MAXSTR
Read Blender memory allocation and freeing.
#define ARRAY_HAS_ITEM(arr_item, arr_start, arr_len)
#define ELEM(...)
#define STREQ(a, b)
const T & data(const Action &owning_action) const
float dot(VecOp< float, D >, VecOp< float, D >) RET
std::string filename(const std::string_view &str, size_t pos)
Strip * strip_from_strip_elem(ListBaseT< Strip > *seqbase, StripElem *se)
Strip * lookup_meta_by_strip(Editing *ed, const Strip *key)
bool sequencer_strip_generates_image(Strip *strip)
const Strip * strip_topmost_get(const Scene *scene, int frame)
void foreach_strip(ListBaseT< Strip > *seqbase, ForEachFunc callback, void *user_data)
Definition iterator.cc:60
void strip_unique_name_set(Scene *scene, ListBaseT< Strip > *seqbasep, Strip *strip)
bool render_is_muted(const ListBaseT< SeqTimelineChannel > *channels, const Strip *strip)
Definition render.cc:2241
ListBaseT< Strip > * get_seqbase_by_strip(const Scene *scene, Strip *strip)
Editing * editing_get(const Scene *scene)
Definition sequencer.cc:304
void edit_strip_name_set(Scene *scene, Strip *strip, const char *new_name)
MovieReaderAccessor movie_reader_cache_acquire_any(const Scene &scene, const Strip &strip)
const char * strip_give_name(const Strip *strip)
static void seqbase_unique_name(ListBaseT< Strip > *seqbasep, StripUniqueInfo *sui)
void movie_metadata_invalidate(Strip &strip)
IDProperty * movie_metadata_ensure(Scene &scene, Strip &strip)
Strip * select_active_get(const Scene *scene)
bool strip_has_valid_data(const Strip *strip)
void ensure_unique_name(Strip *strip, Scene *scene, const DriverMap &driver_map)
const char * get_default_stripname_by_type(int type)
void movie_metadata_set_from_reader(Strip &strip, MovieReader &reader)
Strip * get_strip_by_name(ListBaseT< Strip > *seqbase, const char *name, bool recursive)
ListBaseT< SeqTimelineChannel > * channels_displayed_get(const Editing *ed)
Definition channels.cc:26
ListBaseT< Strip > * get_seqbase_from_strip(Strip *strip, ListBaseT< SeqTimelineChannel > **r_channels, int *r_offset)
static bool seqbase_unique_name_recursive_fn(Strip *strip, void *arg_pt)
void alpha_mode_from_file_extension(Strip *strip)
Mask * active_mask_get(Scene *scene)
void set_scale_to_fit(const Strip *strip, const int image_width, const int image_height, const int preview_width, const int preview_height, const eSeqImageFitMethod fit_method)
bool BLI_string_is_decimal(const char *string) ATTR_NONNULL(1)
char * STRNCPY(char(&dst)[N], const char *src)
@ SEQ_USE_ORIGINAL_SIZE
@ SEQ_STRETCH_TO_FILL
void MOV_close(MovieReader *anim)
Definition movie_read.cc:70
IDProperty * IDP_CopyProperty(const IDProperty *prop) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:986
std::string RNA_path_name_to_infix(const StringRefNull string)
Definition rna_path.cc:1419
void BKE_animdata_fix_paths(ID &id, StringRef prefix, StringRef old_infix, StringRef new_infix, bool verify_paths, const DriverMap &driver_map)
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
char BKE_image_alpha_mode_from_extension_ex(const char *filepath)
IDProperty * MOV_load_metadata(MovieReader *anim)
Definition movie_read.cc:90
void IDP_FreeProperty(IDProperty *prop)
Definition idprop.cc:1403
Map< ID *, Vector< DriverTarget * > > DriverMap
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ListBaseT< Strip > seqbase
struct Editing * ed
void operator()(MovieReader *reader) const