Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Antalik <richardantalik@gmail.com>2020-10-05 03:58:56 +0300
committerRichard Antalik <richardantalik@gmail.com>2020-10-05 03:58:56 +0300
commit18d7aeacf2f79e7a72f15d24a1948a39078c16c9 (patch)
tree02f14510f69f364b7efc2caf07c56c6a1a6699ad /source/blender/sequencer
parentc5143573585c9dd3af541024540f77295cf0babf (diff)
Move sequencer sources from blenkernel
This is first step of refactoring task T77580. Next step will be breaking up files into smaller ones. Reviewed By: sergey, brecht Differential Revision: https://developer.blender.org/D8492
Diffstat (limited to 'source/blender/sequencer')
-rw-r--r--source/blender/sequencer/BKE_sequencer.h645
-rw-r--r--source/blender/sequencer/CMakeLists.txt56
-rw-r--r--source/blender/sequencer/intern/effects.c4291
-rw-r--r--source/blender/sequencer/intern/image_cache.c1455
-rw-r--r--source/blender/sequencer/intern/modifier.c1130
-rw-r--r--source/blender/sequencer/intern/prefetch.c568
-rw-r--r--source/blender/sequencer/intern/sequencer.c6193
7 files changed, 14338 insertions, 0 deletions
diff --git a/source/blender/sequencer/BKE_sequencer.h b/source/blender/sequencer/BKE_sequencer.h
new file mode 100644
index 00000000000..04be46f0ccf
--- /dev/null
+++ b/source/blender/sequencer/BKE_sequencer.h
@@ -0,0 +1,645 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2004 Blender Foundation.
+ * All rights reserved.
+ */
+
+#pragma once
+
+/** \file
+ * \ingroup bke
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct Depsgraph;
+struct Editing;
+struct GPUOffScreen;
+struct GSet;
+struct ImBuf;
+struct Main;
+struct Mask;
+struct ReportList;
+struct Scene;
+struct Sequence;
+struct SequenceModifierData;
+struct Stereo3dFormat;
+struct StripColorBalance;
+struct StripElem;
+struct TextVars;
+struct bContext;
+struct bSound;
+
+struct SeqIndexBuildContext;
+
+#define EARLY_NO_INPUT -1
+#define EARLY_DO_EFFECT 0
+#define EARLY_USE_INPUT_1 1
+#define EARLY_USE_INPUT_2 2
+
+/* sequence iterator */
+
+typedef struct SeqIterator {
+ struct Sequence **array;
+ int tot, cur;
+
+ struct Sequence *seq;
+ int valid;
+} SeqIterator;
+
+void BKE_sequence_iterator_begin(struct Editing *ed,
+ SeqIterator *iter,
+ const bool use_current_sequences);
+void BKE_sequence_iterator_next(SeqIterator *iter);
+void BKE_sequence_iterator_end(SeqIterator *iter);
+
+#define SEQ_ALL_BEGIN(ed, _seq) \
+ { \
+ SeqIterator iter_macro; \
+ for (BKE_sequence_iterator_begin(ed, &iter_macro, false); iter_macro.valid; \
+ BKE_sequence_iterator_next(&iter_macro)) { \
+ _seq = iter_macro.seq;
+
+#define SEQ_ALL_END \
+ } \
+ BKE_sequence_iterator_end(&iter_macro); \
+ } \
+ ((void)0)
+
+#define SEQ_CURRENT_BEGIN(_ed, _seq) \
+ { \
+ SeqIterator iter_macro; \
+ for (BKE_sequence_iterator_begin(_ed, &iter_macro, true); iter_macro.valid; \
+ BKE_sequence_iterator_next(&iter_macro)) { \
+ _seq = iter_macro.seq;
+
+#define SEQ_CURRENT_END SEQ_ALL_END
+
+typedef enum eSeqTaskId {
+ SEQ_TASK_MAIN_RENDER,
+ SEQ_TASK_PREFETCH_RENDER,
+} eSeqTaskId;
+
+typedef struct SeqRenderData {
+ struct Main *bmain;
+ struct Depsgraph *depsgraph;
+ struct Scene *scene;
+ int rectx;
+ int recty;
+ int preview_render_size;
+ int for_render;
+ int motion_blur_samples;
+ float motion_blur_shutter;
+ bool skip_cache;
+ bool is_proxy_render;
+ bool is_prefetch_render;
+ int view_id;
+ /* ID of task for asigning temp cache entries to particular task(thread, etc.) */
+ eSeqTaskId task_id;
+
+ /* special case for OpenGL render */
+ struct GPUOffScreen *gpu_offscreen;
+ // int gpu_samples;
+ // bool gpu_full_samples;
+} SeqRenderData;
+
+void BKE_sequencer_new_render_data(struct Main *bmain,
+ struct Depsgraph *depsgraph,
+ struct Scene *scene,
+ int rectx,
+ int recty,
+ int preview_render_size,
+ int for_render,
+ SeqRenderData *r_context);
+
+int BKE_sequencer_cmp_time_startdisp(const void *a, const void *b);
+
+/* Wipe effect */
+enum {
+ DO_SINGLE_WIPE,
+ DO_DOUBLE_WIPE,
+ /* DO_BOX_WIPE, */ /* UNUSED */
+ /* DO_CROSS_WIPE, */ /* UNUSED */
+ DO_IRIS_WIPE,
+ DO_CLOCK_WIPE,
+};
+
+struct SeqEffectHandle {
+ bool multithreaded;
+ bool supports_mask;
+
+ /* constructors & destructor */
+ /* init is _only_ called on first creation */
+ void (*init)(struct Sequence *seq);
+
+ /* number of input strips needed
+ * (called directly after construction) */
+ int (*num_inputs)(void);
+
+ /* load is called first time after readblenfile in
+ * get_sequence_effect automatically */
+ void (*load)(struct Sequence *seqconst);
+
+ /* duplicate */
+ void (*copy)(struct Sequence *dst, struct Sequence *src, const int flag);
+
+ /* destruct */
+ void (*free)(struct Sequence *seq, const bool do_id_user);
+
+ /* returns: -1: no input needed,
+ * 0: no early out,
+ * 1: out = ibuf1,
+ * 2: out = ibuf2 */
+ int (*early_out)(struct Sequence *seq, float facf0, float facf1);
+
+ /* stores the y-range of the effect IPO */
+ void (*store_icu_yrange)(struct Sequence *seq, short adrcode, float *ymin, float *ymax);
+
+ /* stores the default facf0 and facf1 if no IPO is present */
+ void (*get_default_fac)(struct Sequence *seq, float cfra, float *facf0, float *facf1);
+
+ /* execute the effect
+ * sequence effects are only required to either support
+ * float-rects or byte-rects
+ * (mixed cases are handled one layer up...) */
+
+ struct ImBuf *(*execute)(const SeqRenderData *context,
+ struct Sequence *seq,
+ float cfra,
+ float facf0,
+ float facf1,
+ struct ImBuf *ibuf1,
+ struct ImBuf *ibuf2,
+ struct ImBuf *ibuf3);
+
+ struct ImBuf *(*init_execution)(const SeqRenderData *context,
+ struct ImBuf *ibuf1,
+ struct ImBuf *ibuf2,
+ struct ImBuf *ibuf3);
+
+ void (*execute_slice)(const SeqRenderData *context,
+ struct Sequence *seq,
+ float cfra,
+ float facf0,
+ float facf1,
+ struct ImBuf *ibuf1,
+ struct ImBuf *ibuf2,
+ struct ImBuf *ibuf3,
+ int start_line,
+ int total_lines,
+ struct ImBuf *out);
+};
+
+/* ********************* prototypes *************** */
+
+/* **********************************************************************
+ * sequencer.c
+ *
+ * sequencer render functions
+ * ********************************************************************** */
+double BKE_sequencer_rendersize_to_scale_factor(int size);
+
+struct ImBuf *BKE_sequencer_give_ibuf(const SeqRenderData *context, float cfra, int chanshown);
+struct ImBuf *BKE_sequencer_give_ibuf_direct(const SeqRenderData *context,
+ float cfra,
+ struct Sequence *seq);
+struct ImBuf *BKE_sequencer_give_ibuf_seqbase(const SeqRenderData *context,
+ float cfra,
+ int chan_shown,
+ struct ListBase *seqbasep);
+struct ImBuf *BKE_sequencer_effect_execute_threaded(struct SeqEffectHandle *sh,
+ const SeqRenderData *context,
+ struct Sequence *seq,
+ float cfra,
+ float facf0,
+ float facf1,
+ struct ImBuf *ibuf1,
+ struct ImBuf *ibuf2,
+ struct ImBuf *ibuf3);
+
+/* **********************************************************************
+ * sequencer.c
+ *
+ * sequencer color space functions
+ * ********************************************************************** */
+
+void BKE_sequencer_imbuf_to_sequencer_space(struct Scene *scene,
+ struct ImBuf *ibuf,
+ bool make_float);
+void BKE_sequencer_imbuf_from_sequencer_space(struct Scene *scene, struct ImBuf *ibuf);
+void BKE_sequencer_pixel_from_sequencer_space_v4(struct Scene *scene, float pixel[4]);
+
+/* **********************************************************************
+ * sequencer scene functions
+ * ********************************************************************** */
+struct Editing *BKE_sequencer_editing_get(struct Scene *scene, bool alloc);
+struct Editing *BKE_sequencer_editing_ensure(struct Scene *scene);
+void BKE_sequencer_editing_free(struct Scene *scene, const bool do_id_user);
+
+void BKE_sequencer_sort(struct Scene *scene);
+
+struct Sequence *BKE_sequencer_from_elem(ListBase *seqbase, struct StripElem *se);
+struct Sequence *BKE_sequencer_active_get(struct Scene *scene);
+int BKE_sequencer_active_get_pair(struct Scene *scene,
+ struct Sequence **seq_act,
+ struct Sequence **seq_other);
+void BKE_sequencer_active_set(struct Scene *scene, struct Sequence *seq);
+struct Mask *BKE_sequencer_mask_get(struct Scene *scene);
+
+/* apply functions recursively */
+int BKE_sequencer_base_recursive_apply(struct ListBase *seqbase,
+ int (*apply_fn)(struct Sequence *seq, void *),
+ void *arg);
+int BKE_sequencer_recursive_apply(struct Sequence *seq,
+ int (*apply_fn)(struct Sequence *, void *),
+ void *arg);
+
+/* maintenance functions, mostly for RNA */
+/* extern */
+
+void BKE_sequencer_free_clipboard(void);
+
+void BKE_sequencer_base_clipboard_pointers_free(struct ListBase *seqbase);
+void BKE_sequencer_base_clipboard_pointers_store(struct Main *bmain, struct ListBase *seqbase);
+void BKE_sequencer_base_clipboard_pointers_restore(struct ListBase *seqbase, struct Main *bmain);
+
+void BKE_sequence_free(struct Scene *scene, struct Sequence *seq, const bool do_clean_animdata);
+void BKE_sequence_free_anim(struct Sequence *seq);
+const char *BKE_sequence_give_name(struct Sequence *seq);
+ListBase *BKE_sequence_seqbase_get(struct Sequence *seq, int *r_offset);
+void BKE_sequence_calc(struct Scene *scene, struct Sequence *seq);
+void BKE_sequence_calc_disp(struct Scene *scene, struct Sequence *seq);
+void BKE_sequence_reload_new_file(struct Main *bmain,
+ struct Scene *scene,
+ struct Sequence *seq,
+ const bool lock_range);
+void BKE_sequence_movie_reload_if_needed(struct Main *bmain,
+ struct Scene *scene,
+ struct Sequence *seq,
+ bool *r_was_reloaded,
+ bool *r_can_produce_frames);
+int BKE_sequencer_evaluate_frame(struct Scene *scene, int cfra);
+int BKE_sequencer_get_shown_sequences(struct ListBase *seqbasep,
+ int cfra,
+ int chanshown,
+ struct Sequence **seq_arr_out);
+
+struct StripElem *BKE_sequencer_give_stripelem(struct Sequence *seq, int cfra);
+
+/* intern */
+void BKE_sequencer_update_changed_seq_and_deps(struct Scene *scene,
+ struct Sequence *changed_seq,
+ int len_change,
+ int ibuf_change);
+bool BKE_sequencer_input_have_to_preprocess(const SeqRenderData *context,
+ struct Sequence *seq,
+ float cfra);
+
+bool BKE_sequencer_proxy_rebuild_context(struct Main *bmain,
+ struct Depsgraph *depsgraph,
+ struct Scene *scene,
+ struct Sequence *seq,
+ struct GSet *file_list,
+ ListBase *queue);
+void BKE_sequencer_proxy_rebuild(struct SeqIndexBuildContext *context,
+ short *stop,
+ short *do_update,
+ float *progress);
+void BKE_sequencer_proxy_rebuild_finish(struct SeqIndexBuildContext *context, bool stop);
+
+void BKE_sequencer_proxy_set(struct Sequence *seq, bool value);
+/* **********************************************************************
+ * seqcache.c
+ *
+ * Sequencer memory cache management functions
+ * ********************************************************************** */
+
+#define SEQ_CACHE_COST_MAX 10.0f
+
+struct ImBuf *BKE_sequencer_cache_get(const SeqRenderData *context,
+ struct Sequence *seq,
+ float cfra,
+ int type,
+ bool skip_disk_cache);
+void BKE_sequencer_cache_put(const SeqRenderData *context,
+ struct Sequence *seq,
+ float cfra,
+ int type,
+ struct ImBuf *i,
+ float cost,
+ bool skip_disk_cache);
+bool BKE_sequencer_cache_put_if_possible(const SeqRenderData *context,
+ struct Sequence *seq,
+ float cfra,
+ int type,
+ struct ImBuf *nval,
+ float cost,
+ bool skip_disk_cache);
+bool BKE_sequencer_cache_recycle_item(struct Scene *scene);
+void BKE_sequencer_cache_free_temp_cache(struct Scene *scene, short id, int cfra);
+void BKE_sequencer_cache_destruct(struct Scene *scene);
+void BKE_sequencer_cache_cleanup_all(struct Main *bmain);
+void BKE_sequencer_cache_cleanup(struct Scene *scene);
+void BKE_sequencer_cache_cleanup_sequence(struct Scene *scene,
+ struct Sequence *seq,
+ struct Sequence *seq_changed,
+ int invalidate_types,
+ bool force_seq_changed_range);
+void BKE_sequencer_cache_iterate(struct Scene *scene,
+ void *userdata,
+ bool callback_init(void *userdata, size_t item_count),
+ bool callback_iter(void *userdata,
+ struct Sequence *seq,
+ int cfra,
+ int cache_type,
+ float cost));
+bool BKE_sequencer_cache_is_full(struct Scene *scene);
+
+/* **********************************************************************
+ * seqprefetch.c
+ *
+ * Sequencer frame prefetching
+ * ********************************************************************** */
+
+void BKE_sequencer_prefetch_start(const SeqRenderData *context, float cfra, float cost);
+void BKE_sequencer_prefetch_stop_all(void);
+void BKE_sequencer_prefetch_stop(struct Scene *scene);
+void BKE_sequencer_prefetch_free(struct Scene *scene);
+bool BKE_sequencer_prefetch_need_redraw(struct Main *bmain, struct Scene *scene);
+bool BKE_sequencer_prefetch_job_is_running(struct Scene *scene);
+void BKE_sequencer_prefetch_get_time_range(struct Scene *scene, int *start, int *end);
+SeqRenderData *BKE_sequencer_prefetch_get_original_context(const SeqRenderData *context);
+struct Sequence *BKE_sequencer_prefetch_get_original_sequence(struct Sequence *seq,
+ struct Scene *scene);
+
+/* **********************************************************************
+ * seqeffects.c
+ *
+ * Sequencer effect strip management functions
+ * **********************************************************************
+ */
+
+/* intern */
+struct SeqEffectHandle BKE_sequence_get_blend(struct Sequence *seq);
+void BKE_sequence_effect_speed_rebuild_map(struct Scene *scene, struct Sequence *seq, bool force);
+float BKE_sequencer_speed_effect_target_frame_get(const SeqRenderData *context,
+ struct Sequence *seq,
+ float cfra,
+ int input);
+
+/* extern */
+struct SeqEffectHandle BKE_sequence_get_effect(struct Sequence *seq);
+int BKE_sequence_effect_get_num_inputs(int seq_type);
+int BKE_sequence_effect_get_supports_mask(int seq_type);
+void BKE_sequencer_text_font_unload(struct TextVars *data, const bool do_id_user);
+void BKE_sequencer_text_font_load(struct TextVars *data, const bool do_id_user);
+
+/* **********************************************************************
+ * Sequencer editing functions
+ * **********************************************************************
+ */
+
+/* for transform but also could use elsewhere */
+int BKE_sequence_tx_get_final_left(struct Sequence *seq, bool metaclip);
+int BKE_sequence_tx_get_final_right(struct Sequence *seq, bool metaclip);
+void BKE_sequence_tx_set_final_left(struct Sequence *seq, int val);
+void BKE_sequence_tx_set_final_right(struct Sequence *seq, int val);
+void BKE_sequence_tx_handle_xlimits(struct Sequence *seq, int leftflag, int rightflag);
+bool BKE_sequence_tx_test(struct Sequence *seq);
+bool BKE_sequence_tx_fullupdate_test(struct Sequence *seq);
+bool BKE_sequence_single_check(struct Sequence *seq);
+void BKE_sequence_single_fix(struct Sequence *seq);
+bool BKE_sequence_test_overlap(struct ListBase *seqbasep, struct Sequence *test);
+void BKE_sequence_translate(struct Scene *scene, struct Sequence *seq, int delta);
+void BKE_sequence_sound_init(struct Scene *scene, struct Sequence *seq);
+const struct Sequence *BKE_sequencer_foreground_frame_get(const struct Scene *scene, int frame);
+struct ListBase *BKE_sequence_seqbase(struct ListBase *seqbase, struct Sequence *seq);
+struct Sequence *BKE_sequence_metastrip(ListBase *seqbase /* = ed->seqbase */,
+ struct Sequence *meta /* = NULL */,
+ struct Sequence *seq);
+
+void BKE_sequencer_offset_animdata(struct Scene *scene, struct Sequence *seq, int ofs);
+void BKE_sequencer_dupe_animdata(struct Scene *scene, const char *name_src, const char *name_dst);
+bool BKE_sequence_base_shuffle_ex(struct ListBase *seqbasep,
+ struct Sequence *test,
+ struct Scene *evil_scene,
+ int channel_delta);
+bool BKE_sequence_base_shuffle(struct ListBase *seqbasep,
+ struct Sequence *test,
+ struct Scene *evil_scene);
+bool BKE_sequence_base_shuffle_time(ListBase *seqbasep,
+ struct Scene *evil_scene,
+ ListBase *markers,
+ const bool use_sync_markers);
+bool BKE_sequence_base_isolated_sel_check(struct ListBase *seqbase);
+void BKE_sequencer_free_imbuf(struct Scene *scene, struct ListBase *seqbasep, bool for_render);
+struct Sequence *BKE_sequence_dupli_recursive(const struct Scene *scene_src,
+ struct Scene *scene_dst,
+ struct ListBase *new_seq_list,
+ struct Sequence *seq,
+ int dupe_flag);
+int BKE_sequence_swap(struct Sequence *seq_a, struct Sequence *seq_b, const char **error_str);
+
+bool BKE_sequence_check_depend(struct Sequence *seq, struct Sequence *cur);
+void BKE_sequence_invalidate_cache_raw(struct Scene *scene, struct Sequence *seq);
+void BKE_sequence_invalidate_cache_preprocessed(struct Scene *scene, struct Sequence *seq);
+void BKE_sequence_invalidate_cache_composite(struct Scene *scene, struct Sequence *seq);
+void BKE_sequence_invalidate_dependent(struct Scene *scene, struct Sequence *seq);
+void BKE_sequence_invalidate_scene_strips(struct Main *bmain, struct Scene *scene_target);
+void BKE_sequence_invalidate_movieclip_strips(struct Main *bmain, struct MovieClip *clip_target);
+void BKE_sequence_invalidate_cache_in_range(struct Scene *scene,
+ struct Sequence *seq,
+ struct Sequence *range_mask,
+ int invalidate_types);
+
+void BKE_sequencer_update_sound_bounds_all(struct Scene *scene);
+void BKE_sequencer_update_sound_bounds(struct Scene *scene, struct Sequence *seq);
+void BKE_sequencer_update_muting(struct Editing *ed);
+void BKE_sequencer_update_sound(struct Scene *scene, struct bSound *sound);
+
+void BKE_sequencer_refresh_sound_length(struct Main *bmain, struct Scene *scene);
+
+void BKE_sequence_base_unique_name_recursive(ListBase *seqbasep, struct Sequence *seq);
+void BKE_sequence_base_dupli_recursive(const struct Scene *scene_src,
+ struct Scene *scene_dst,
+ struct ListBase *nseqbase,
+ const struct ListBase *seqbase,
+ int dupe_flag,
+ const int flag);
+bool BKE_sequence_is_valid_check(struct Sequence *seq);
+
+void BKE_sequencer_clear_scene_in_allseqs(struct Main *bmain, struct Scene *sce);
+
+struct Sequence *BKE_sequence_get_by_name(struct ListBase *seqbase,
+ const char *name,
+ bool recursive);
+
+/* api for adding new sequence strips */
+typedef struct SeqLoadInfo {
+ int start_frame;
+ int end_frame;
+ int channel;
+ int flag; /* use sound, replace sel */
+ int type;
+ int len; /* only for image strips */
+ char path[1024]; /* 1024 = FILE_MAX */
+
+ /* multiview */
+ char views_format;
+ struct Stereo3dFormat *stereo3d_format;
+
+ /* return values */
+ char name[64];
+ struct Sequence *seq_sound; /* for movie's */
+ int tot_success;
+ int tot_error;
+} SeqLoadInfo;
+
+/* SeqLoadInfo.flag */
+#define SEQ_LOAD_REPLACE_SEL (1 << 0)
+#define SEQ_LOAD_FRAME_ADVANCE (1 << 1)
+#define SEQ_LOAD_MOVIE_SOUND (1 << 2)
+#define SEQ_LOAD_SOUND_CACHE (1 << 3)
+#define SEQ_LOAD_SYNC_FPS (1 << 4)
+#define SEQ_LOAD_SOUND_MONO (1 << 5)
+
+/* seq_dupli' flags */
+#define SEQ_DUPE_UNIQUE_NAME (1 << 0)
+#define SEQ_DUPE_CONTEXT (1 << 1)
+#define SEQ_DUPE_ANIM (1 << 2)
+#define SEQ_DUPE_ALL (1 << 3) /* otherwise only selected are copied */
+#define SEQ_DUPE_IS_RECURSIVE_CALL (1 << 4)
+
+/* use as an api function */
+typedef struct Sequence *(*SeqLoadFn)(struct bContext *, ListBase *, struct SeqLoadInfo *);
+
+struct Sequence *BKE_sequence_alloc(ListBase *lb, int cfra, int machine, int type);
+
+/* Generate new UUID for the given sequence. */
+void BKE_sequence_session_uuid_generate(struct Sequence *sequence);
+
+void BKE_sequence_alpha_mode_from_extension(struct Sequence *seq);
+void BKE_sequence_init_colorspace(struct Sequence *seq);
+
+float BKE_sequence_get_fps(struct Scene *scene, struct Sequence *seq);
+float BKE_sequencer_give_stripelem_index(struct Sequence *seq, float cfra);
+
+/* RNA enums, just to be more readable */
+enum {
+ SEQ_SIDE_NONE = 0,
+ SEQ_SIDE_LEFT,
+ SEQ_SIDE_RIGHT,
+ SEQ_SIDE_BOTH,
+ SEQ_SIDE_NO_CHANGE,
+};
+int BKE_sequencer_find_next_prev_edit(struct Scene *scene,
+ int cfra,
+ const short side,
+ const bool do_skip_mute,
+ const bool do_center,
+ const bool do_unselected);
+
+struct Sequence *BKE_sequencer_add_image_strip(struct bContext *C,
+ ListBase *seqbasep,
+ struct SeqLoadInfo *seq_load);
+struct Sequence *BKE_sequencer_add_sound_strip(struct bContext *C,
+ ListBase *seqbasep,
+ struct SeqLoadInfo *seq_load);
+struct Sequence *BKE_sequencer_add_movie_strip(struct bContext *C,
+ ListBase *seqbasep,
+ struct SeqLoadInfo *seq_load);
+
+/* copy/paste */
+extern ListBase seqbase_clipboard;
+extern int seqbase_clipboard_frame;
+
+/* modifiers */
+typedef struct SequenceModifierTypeInfo {
+ /* default name for the modifier */
+ char name[64]; /* MAX_NAME */
+
+ /* DNA structure name used on load/save filed */
+ char struct_name[64]; /* MAX_NAME */
+
+ /* size of modifier data structure, used by allocation */
+ int struct_size;
+
+ /* data initialization */
+ void (*init_data)(struct SequenceModifierData *smd);
+
+ /* free data used by modifier,
+ * only modifier-specific data should be freed, modifier descriptor would
+ * be freed outside of this callback
+ */
+ void (*free_data)(struct SequenceModifierData *smd);
+
+ /* copy data from one modifier to another */
+ void (*copy_data)(struct SequenceModifierData *smd, struct SequenceModifierData *target);
+
+ /* apply modifier on a given image buffer */
+ void (*apply)(struct SequenceModifierData *smd, struct ImBuf *ibuf, struct ImBuf *mask);
+} SequenceModifierTypeInfo;
+
+const struct SequenceModifierTypeInfo *BKE_sequence_modifier_type_info_get(int type);
+
+struct SequenceModifierData *BKE_sequence_modifier_new(struct Sequence *seq,
+ const char *name,
+ int type);
+bool BKE_sequence_modifier_remove(struct Sequence *seq, struct SequenceModifierData *smd);
+void BKE_sequence_modifier_clear(struct Sequence *seq);
+void BKE_sequence_modifier_free(struct SequenceModifierData *smd);
+void BKE_sequence_modifier_unique_name(struct Sequence *seq, struct SequenceModifierData *smd);
+struct SequenceModifierData *BKE_sequence_modifier_find_by_name(struct Sequence *seq,
+ const char *name);
+struct ImBuf *BKE_sequence_modifier_apply_stack(const SeqRenderData *context,
+ struct Sequence *seq,
+ struct ImBuf *ibuf,
+ int cfra);
+void BKE_sequence_modifier_list_copy(struct Sequence *seqn, struct Sequence *seq);
+
+int BKE_sequence_supports_modifiers(struct Sequence *seq);
+
+/* internal filters */
+struct ImBuf *BKE_sequencer_render_mask_input(const SeqRenderData *context,
+ int mask_input_type,
+ struct Sequence *mask_sequence,
+ struct Mask *mask_id,
+ int cfra,
+ int fra_offset,
+ bool make_float);
+void BKE_sequencer_color_balance_apply(struct StripColorBalance *cb,
+ struct ImBuf *ibuf,
+ float mul,
+ bool make_float,
+ struct ImBuf *mask_input);
+
+void BKE_sequencer_all_free_anim_ibufs(struct Scene *scene, int cfra);
+bool BKE_sequencer_check_scene_recursion(struct Scene *scene, struct ReportList *reports);
+bool BKE_sequencer_render_loop_check(struct Sequence *seq_main, struct Sequence *seq);
+void BKE_sequencer_flag_for_removal(struct Scene *scene,
+ struct ListBase *seqbase,
+ struct Sequence *seq);
+void BKE_sequencer_remove_flagged_sequences(struct Scene *scene, struct ListBase *seqbase);
+
+/* A debug and development function which checks whether sequences have unique UUIDs.
+ * Errors will be reported to the console. */
+void BKE_sequencer_check_uuids_unique_and_report(const struct Scene *scene);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/source/blender/sequencer/CMakeLists.txt b/source/blender/sequencer/CMakeLists.txt
new file mode 100644
index 00000000000..75b5f2fef64
--- /dev/null
+++ b/source/blender/sequencer/CMakeLists.txt
@@ -0,0 +1,56 @@
+# ***** BEGIN GPL LICENSE BLOCK *****
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# The Original Code is Copyright (C) 2011, Blender Foundation
+# All rights reserved.
+# ***** END GPL LICENSE BLOCK *****
+
+set(INC
+ .
+ ../blenkernel
+ ../blenlib
+ ../blentranslation
+ ../depsgraph
+ ../imbuf
+ ../makesdna
+ ../makesrna
+ ../render/extern/include
+ ../blenfont
+ ../windowmanager
+ ../../../intern/atomic
+ ../../../intern/guardedalloc
+)
+
+set(INC_SYS
+
+)
+
+set(SRC
+ BKE_sequencer.h
+
+ intern/sequencer.c
+ intern/image_cache.c
+ intern/effects.c
+ intern/modifier.c
+ intern/prefetch.c
+)
+
+set(LIB
+ bf_blenkernel
+ bf_blenlib
+)
+
+blender_add_lib(bf_sequencer "${SRC}" "${INC}" "${INC_SYS}" "${LIB}")
diff --git a/source/blender/sequencer/intern/effects.c b/source/blender/sequencer/intern/effects.c
new file mode 100644
index 00000000000..b4bc2d25155
--- /dev/null
+++ b/source/blender/sequencer/intern/effects.c
@@ -0,0 +1,4291 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * - Blender Foundation, 2003-2009
+ * - Peter Schlaile <peter [at] schlaile [dot] de> 2005/2006
+ */
+
+/** \file
+ * \ingroup bke
+ */
+
+#include <math.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "MEM_guardedalloc.h"
+
+#include "BLI_listbase.h"
+#include "BLI_math.h" /* windows needs for M_PI */
+#include "BLI_path_util.h"
+#include "BLI_rect.h"
+#include "BLI_string.h"
+#include "BLI_threads.h"
+#include "BLI_utildefines.h"
+
+#include "DNA_anim_types.h"
+#include "DNA_scene_types.h"
+#include "DNA_sequence_types.h"
+#include "DNA_space_types.h"
+
+#include "BKE_fcurve.h"
+#include "BKE_lib_id.h"
+#include "BKE_main.h"
+#include "BKE_sequencer.h"
+
+#include "IMB_colormanagement.h"
+#include "IMB_imbuf.h"
+#include "IMB_imbuf_types.h"
+#include "IMB_metadata.h"
+
+#include "BLI_math_color_blend.h"
+
+#include "RNA_access.h"
+
+#include "RE_pipeline.h"
+
+#include "BLF_api.h"
+
+static struct SeqEffectHandle get_sequence_effect_impl(int seq_type);
+
+static void slice_get_byte_buffers(const SeqRenderData *context,
+ const ImBuf *ibuf1,
+ const ImBuf *ibuf2,
+ const ImBuf *ibuf3,
+ const ImBuf *out,
+ int start_line,
+ unsigned char **rect1,
+ unsigned char **rect2,
+ unsigned char **rect3,
+ unsigned char **rect_out)
+{
+ int offset = 4 * start_line * context->rectx;
+
+ *rect1 = (unsigned char *)ibuf1->rect + offset;
+ *rect_out = (unsigned char *)out->rect + offset;
+
+ if (ibuf2) {
+ *rect2 = (unsigned char *)ibuf2->rect + offset;
+ }
+
+ if (ibuf3) {
+ *rect3 = (unsigned char *)ibuf3->rect + offset;
+ }
+}
+
+static void slice_get_float_buffers(const SeqRenderData *context,
+ const ImBuf *ibuf1,
+ const ImBuf *ibuf2,
+ const ImBuf *ibuf3,
+ const ImBuf *out,
+ int start_line,
+ float **rect1,
+ float **rect2,
+ float **rect3,
+ float **rect_out)
+{
+ int offset = 4 * start_line * context->rectx;
+
+ *rect1 = ibuf1->rect_float + offset;
+ *rect_out = out->rect_float + offset;
+
+ if (ibuf2) {
+ *rect2 = ibuf2->rect_float + offset;
+ }
+
+ if (ibuf3) {
+ *rect3 = ibuf3->rect_float + offset;
+ }
+}
+
+/*********************** Glow effect *************************/
+
+enum {
+ GlowR = 0,
+ GlowG = 1,
+ GlowB = 2,
+ GlowA = 3,
+};
+
+static ImBuf *prepare_effect_imbufs(const SeqRenderData *context,
+ ImBuf *ibuf1,
+ ImBuf *ibuf2,
+ ImBuf *ibuf3)
+{
+ ImBuf *out;
+ Scene *scene = context->scene;
+ int x = context->rectx;
+ int y = context->recty;
+
+ if (!ibuf1 && !ibuf2 && !ibuf3) {
+ /* hmmm, global float option ? */
+ out = IMB_allocImBuf(x, y, 32, IB_rect);
+ }
+ else if ((ibuf1 && ibuf1->rect_float) || (ibuf2 && ibuf2->rect_float) ||
+ (ibuf3 && ibuf3->rect_float)) {
+ /* if any inputs are rectfloat, output is float too */
+
+ out = IMB_allocImBuf(x, y, 32, IB_rectfloat);
+ }
+ else {
+ out = IMB_allocImBuf(x, y, 32, IB_rect);
+ }
+
+ if (out->rect_float) {
+ if (ibuf1 && !ibuf1->rect_float) {
+ BKE_sequencer_imbuf_to_sequencer_space(scene, ibuf1, true);
+ }
+
+ if (ibuf2 && !ibuf2->rect_float) {
+ BKE_sequencer_imbuf_to_sequencer_space(scene, ibuf2, true);
+ }
+
+ if (ibuf3 && !ibuf3->rect_float) {
+ BKE_sequencer_imbuf_to_sequencer_space(scene, ibuf3, true);
+ }
+
+ IMB_colormanagement_assign_float_colorspace(out, scene->sequencer_colorspace_settings.name);
+ }
+ else {
+ if (ibuf1 && !ibuf1->rect) {
+ IMB_rect_from_float(ibuf1);
+ }
+
+ if (ibuf2 && !ibuf2->rect) {
+ IMB_rect_from_float(ibuf2);
+ }
+
+ if (ibuf3 && !ibuf3->rect) {
+ IMB_rect_from_float(ibuf3);
+ }
+ }
+
+ /* If effect only affecting a single channel, forward input's metadata to the output. */
+ if (ibuf1 != NULL && ibuf1 == ibuf2 && ibuf2 == ibuf3) {
+ IMB_metadata_copy(out, ibuf1);
+ }
+
+ return out;
+}
+
+/*********************** Alpha Over *************************/
+
+static void init_alpha_over_or_under(Sequence *seq)
+{
+ Sequence *seq1 = seq->seq1;
+ Sequence *seq2 = seq->seq2;
+
+ seq->seq2 = seq1;
+ seq->seq1 = seq2;
+}
+
+static void do_alphaover_effect_byte(float facf0,
+ float facf1,
+ int x,
+ int y,
+ unsigned char *rect1,
+ unsigned char *rect2,
+ unsigned char *out)
+{
+ float fac2, mfac, fac, fac4;
+ int xo;
+ unsigned char *cp1, *cp2, *rt;
+ float tempc[4], rt1[4], rt2[4];
+
+ xo = x;
+ cp1 = rect1;
+ cp2 = rect2;
+ rt = out;
+
+ fac2 = facf0;
+ fac4 = facf1;
+
+ while (y--) {
+ x = xo;
+ while (x--) {
+ /* rt = rt1 over rt2 (alpha from rt1) */
+
+ straight_uchar_to_premul_float(rt1, cp1);
+ straight_uchar_to_premul_float(rt2, cp2);
+
+ fac = fac2;
+ mfac = 1.0f - fac2 * rt1[3];
+
+ if (fac <= 0.0f) {
+ *((unsigned int *)rt) = *((unsigned int *)cp2);
+ }
+ else if (mfac <= 0.0f) {
+ *((unsigned int *)rt) = *((unsigned int *)cp1);
+ }
+ else {
+ tempc[0] = fac * rt1[0] + mfac * rt2[0];
+ tempc[1] = fac * rt1[1] + mfac * rt2[1];
+ tempc[2] = fac * rt1[2] + mfac * rt2[2];
+ tempc[3] = fac * rt1[3] + mfac * rt2[3];
+
+ premul_float_to_straight_uchar(rt, tempc);
+ }
+ cp1 += 4;
+ cp2 += 4;
+ rt += 4;
+ }
+
+ if (y == 0) {
+ break;
+ }
+ y--;
+
+ x = xo;
+ while (x--) {
+ straight_uchar_to_premul_float(rt1, cp1);
+ straight_uchar_to_premul_float(rt2, cp2);
+
+ fac = fac4;
+ mfac = 1.0f - (fac4 * rt1[3]);
+
+ if (fac <= 0.0f) {
+ *((unsigned int *)rt) = *((unsigned int *)cp2);
+ }
+ else if (mfac <= 0.0f) {
+ *((unsigned int *)rt) = *((unsigned int *)cp1);
+ }
+ else {
+ tempc[0] = fac * rt1[0] + mfac * rt2[0];
+ tempc[1] = fac * rt1[1] + mfac * rt2[1];
+ tempc[2] = fac * rt1[2] + mfac * rt2[2];
+ tempc[3] = fac * rt1[3] + mfac * rt2[3];
+
+ premul_float_to_straight_uchar(rt, tempc);
+ }
+ cp1 += 4;
+ cp2 += 4;
+ rt += 4;
+ }
+ }
+}
+
+static void do_alphaover_effect_float(
+ float facf0, float facf1, int x, int y, float *rect1, float *rect2, float *out)
+{
+ float fac2, mfac, fac, fac4;
+ int xo;
+ float *rt1, *rt2, *rt;
+
+ xo = x;
+ rt1 = rect1;
+ rt2 = rect2;
+ rt = out;
+
+ fac2 = facf0;
+ fac4 = facf1;
+
+ while (y--) {
+ x = xo;
+ while (x--) {
+ /* rt = rt1 over rt2 (alpha from rt1) */
+
+ fac = fac2;
+ mfac = 1.0f - (fac2 * rt1[3]);
+
+ if (fac <= 0.0f) {
+ memcpy(rt, rt2, sizeof(float[4]));
+ }
+ else if (mfac <= 0) {
+ memcpy(rt, rt1, sizeof(float[4]));
+ }
+ else {
+ rt[0] = fac * rt1[0] + mfac * rt2[0];
+ rt[1] = fac * rt1[1] + mfac * rt2[1];
+ rt[2] = fac * rt1[2] + mfac * rt2[2];
+ rt[3] = fac * rt1[3] + mfac * rt2[3];
+ }
+ rt1 += 4;
+ rt2 += 4;
+ rt += 4;
+ }
+
+ if (y == 0) {
+ break;
+ }
+ y--;
+
+ x = xo;
+ while (x--) {
+ fac = fac4;
+ mfac = 1.0f - (fac4 * rt1[3]);
+
+ if (fac <= 0.0f) {
+ memcpy(rt, rt2, sizeof(float[4]));
+ }
+ else if (mfac <= 0.0f) {
+ memcpy(rt, rt1, sizeof(float[4]));
+ }
+ else {
+ rt[0] = fac * rt1[0] + mfac * rt2[0];
+ rt[1] = fac * rt1[1] + mfac * rt2[1];
+ rt[2] = fac * rt1[2] + mfac * rt2[2];
+ rt[3] = fac * rt1[3] + mfac * rt2[3];
+ }
+ rt1 += 4;
+ rt2 += 4;
+ rt += 4;
+ }
+ }
+}
+
+static void do_alphaover_effect(const SeqRenderData *context,
+ Sequence *UNUSED(seq),
+ float UNUSED(cfra),
+ float facf0,
+ float facf1,
+ ImBuf *ibuf1,
+ ImBuf *ibuf2,
+ ImBuf *UNUSED(ibuf3),
+ int start_line,
+ int total_lines,
+ ImBuf *out)
+{
+ if (out->rect_float) {
+ float *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+
+ slice_get_float_buffers(
+ context, ibuf1, ibuf2, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+
+ do_alphaover_effect_float(facf0, facf1, context->rectx, total_lines, rect1, rect2, rect_out);
+ }
+ else {
+ unsigned char *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+
+ slice_get_byte_buffers(
+ context, ibuf1, ibuf2, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+
+ do_alphaover_effect_byte(facf0, facf1, context->rectx, total_lines, rect1, rect2, rect_out);
+ }
+}
+
+/*********************** Alpha Under *************************/
+
+static void do_alphaunder_effect_byte(float facf0,
+ float facf1,
+ int x,
+ int y,
+ unsigned char *rect1,
+ unsigned char *rect2,
+ unsigned char *out)
+{
+ float fac2, fac, fac4;
+ int xo;
+ unsigned char *cp1, *cp2, *rt;
+ float tempc[4], rt1[4], rt2[4];
+
+ xo = x;
+ cp1 = rect1;
+ cp2 = rect2;
+ rt = out;
+
+ fac2 = facf0;
+ fac4 = facf1;
+
+ while (y--) {
+ x = xo;
+ while (x--) {
+ /* rt = rt1 under rt2 (alpha from rt2) */
+ straight_uchar_to_premul_float(rt1, cp1);
+ straight_uchar_to_premul_float(rt2, cp2);
+
+ /* this complex optimization is because the
+ * 'skybuf' can be crossed in
+ */
+ if (rt2[3] <= 0.0f && fac2 >= 1.0f) {
+ *((unsigned int *)rt) = *((unsigned int *)cp1);
+ }
+ else if (rt2[3] >= 1.0f) {
+ *((unsigned int *)rt) = *((unsigned int *)cp2);
+ }
+ else {
+ fac = (fac2 * (1.0f - rt2[3]));
+
+ if (fac <= 0) {
+ *((unsigned int *)rt) = *((unsigned int *)cp2);
+ }
+ else {
+ tempc[0] = (fac * rt1[0] + rt2[0]);
+ tempc[1] = (fac * rt1[1] + rt2[1]);
+ tempc[2] = (fac * rt1[2] + rt2[2]);
+ tempc[3] = (fac * rt1[3] + rt2[3]);
+
+ premul_float_to_straight_uchar(rt, tempc);
+ }
+ }
+ cp1 += 4;
+ cp2 += 4;
+ rt += 4;
+ }
+
+ if (y == 0) {
+ break;
+ }
+ y--;
+
+ x = xo;
+ while (x--) {
+ straight_uchar_to_premul_float(rt1, cp1);
+ straight_uchar_to_premul_float(rt2, cp2);
+
+ if (rt2[3] <= 0.0f && fac4 >= 1.0f) {
+ *((unsigned int *)rt) = *((unsigned int *)cp1);
+ }
+ else if (rt2[3] >= 1.0f) {
+ *((unsigned int *)rt) = *((unsigned int *)cp2);
+ }
+ else {
+ fac = (fac4 * (1.0f - rt2[3]));
+
+ if (fac <= 0) {
+ *((unsigned int *)rt) = *((unsigned int *)cp2);
+ }
+ else {
+ tempc[0] = (fac * rt1[0] + rt2[0]);
+ tempc[1] = (fac * rt1[1] + rt2[1]);
+ tempc[2] = (fac * rt1[2] + rt2[2]);
+ tempc[3] = (fac * rt1[3] + rt2[3]);
+
+ premul_float_to_straight_uchar(rt, tempc);
+ }
+ }
+ cp1 += 4;
+ cp2 += 4;
+ rt += 4;
+ }
+ }
+}
+
+static void do_alphaunder_effect_float(
+ float facf0, float facf1, int x, int y, float *rect1, float *rect2, float *out)
+{
+ float fac2, fac, fac4;
+ int xo;
+ float *rt1, *rt2, *rt;
+
+ xo = x;
+ rt1 = rect1;
+ rt2 = rect2;
+ rt = out;
+
+ fac2 = facf0;
+ fac4 = facf1;
+
+ while (y--) {
+ x = xo;
+ while (x--) {
+ /* rt = rt1 under rt2 (alpha from rt2) */
+
+ /* this complex optimization is because the
+ * 'skybuf' can be crossed in
+ */
+ if (rt2[3] <= 0 && fac2 >= 1.0f) {
+ memcpy(rt, rt1, sizeof(float[4]));
+ }
+ else if (rt2[3] >= 1.0f) {
+ memcpy(rt, rt2, sizeof(float[4]));
+ }
+ else {
+ fac = fac2 * (1.0f - rt2[3]);
+
+ if (fac == 0) {
+ memcpy(rt, rt2, sizeof(float[4]));
+ }
+ else {
+ rt[0] = fac * rt1[0] + rt2[0];
+ rt[1] = fac * rt1[1] + rt2[1];
+ rt[2] = fac * rt1[2] + rt2[2];
+ rt[3] = fac * rt1[3] + rt2[3];
+ }
+ }
+ rt1 += 4;
+ rt2 += 4;
+ rt += 4;
+ }
+
+ if (y == 0) {
+ break;
+ }
+ y--;
+
+ x = xo;
+ while (x--) {
+ if (rt2[3] <= 0 && fac4 >= 1.0f) {
+ memcpy(rt, rt1, sizeof(float[4]));
+ }
+ else if (rt2[3] >= 1.0f) {
+ memcpy(rt, rt2, sizeof(float[4]));
+ }
+ else {
+ fac = fac4 * (1.0f - rt2[3]);
+
+ if (fac == 0) {
+ memcpy(rt, rt2, sizeof(float[4]));
+ }
+ else {
+ rt[0] = fac * rt1[0] + rt2[0];
+ rt[1] = fac * rt1[1] + rt2[1];
+ rt[2] = fac * rt1[2] + rt2[2];
+ rt[3] = fac * rt1[3] + rt2[3];
+ }
+ }
+ rt1 += 4;
+ rt2 += 4;
+ rt += 4;
+ }
+ }
+}
+
+static void do_alphaunder_effect(const SeqRenderData *context,
+ Sequence *UNUSED(seq),
+ float UNUSED(cfra),
+ float facf0,
+ float facf1,
+ ImBuf *ibuf1,
+ ImBuf *ibuf2,
+ ImBuf *UNUSED(ibuf3),
+ int start_line,
+ int total_lines,
+ ImBuf *out)
+{
+ if (out->rect_float) {
+ float *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+
+ slice_get_float_buffers(
+ context, ibuf1, ibuf2, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+
+ do_alphaunder_effect_float(facf0, facf1, context->rectx, total_lines, rect1, rect2, rect_out);
+ }
+ else {
+ unsigned char *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+
+ slice_get_byte_buffers(
+ context, ibuf1, ibuf2, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+
+ do_alphaunder_effect_byte(facf0, facf1, context->rectx, total_lines, rect1, rect2, rect_out);
+ }
+}
+
+/*********************** Cross *************************/
+
+static void do_cross_effect_byte(float facf0,
+ float facf1,
+ int x,
+ int y,
+ unsigned char *rect1,
+ unsigned char *rect2,
+ unsigned char *out)
+{
+ int fac1, fac2, fac3, fac4;
+ int xo;
+ unsigned char *rt1, *rt2, *rt;
+
+ xo = x;
+ rt1 = rect1;
+ rt2 = rect2;
+ rt = out;
+
+ fac2 = (int)(256.0f * facf0);
+ fac1 = 256 - fac2;
+ fac4 = (int)(256.0f * facf1);
+ fac3 = 256 - fac4;
+
+ while (y--) {
+ x = xo;
+ while (x--) {
+ rt[0] = (fac1 * rt1[0] + fac2 * rt2[0]) >> 8;
+ rt[1] = (fac1 * rt1[1] + fac2 * rt2[1]) >> 8;
+ rt[2] = (fac1 * rt1[2] + fac2 * rt2[2]) >> 8;
+ rt[3] = (fac1 * rt1[3] + fac2 * rt2[3]) >> 8;
+
+ rt1 += 4;
+ rt2 += 4;
+ rt += 4;
+ }
+
+ if (y == 0) {
+ break;
+ }
+ y--;
+
+ x = xo;
+ while (x--) {
+ rt[0] = (fac3 * rt1[0] + fac4 * rt2[0]) >> 8;
+ rt[1] = (fac3 * rt1[1] + fac4 * rt2[1]) >> 8;
+ rt[2] = (fac3 * rt1[2] + fac4 * rt2[2]) >> 8;
+ rt[3] = (fac3 * rt1[3] + fac4 * rt2[3]) >> 8;
+
+ rt1 += 4;
+ rt2 += 4;
+ rt += 4;
+ }
+ }
+}
+
+static void do_cross_effect_float(
+ float facf0, float facf1, int x, int y, float *rect1, float *rect2, float *out)
+{
+ float fac1, fac2, fac3, fac4;
+ int xo;
+ float *rt1, *rt2, *rt;
+
+ xo = x;
+ rt1 = rect1;
+ rt2 = rect2;
+ rt = out;
+
+ fac2 = facf0;
+ fac1 = 1.0f - fac2;
+ fac4 = facf1;
+ fac3 = 1.0f - fac4;
+
+ while (y--) {
+ x = xo;
+ while (x--) {
+ rt[0] = fac1 * rt1[0] + fac2 * rt2[0];
+ rt[1] = fac1 * rt1[1] + fac2 * rt2[1];
+ rt[2] = fac1 * rt1[2] + fac2 * rt2[2];
+ rt[3] = fac1 * rt1[3] + fac2 * rt2[3];
+
+ rt1 += 4;
+ rt2 += 4;
+ rt += 4;
+ }
+
+ if (y == 0) {
+ break;
+ }
+ y--;
+
+ x = xo;
+ while (x--) {
+ rt[0] = fac3 * rt1[0] + fac4 * rt2[0];
+ rt[1] = fac3 * rt1[1] + fac4 * rt2[1];
+ rt[2] = fac3 * rt1[2] + fac4 * rt2[2];
+ rt[3] = fac3 * rt1[3] + fac4 * rt2[3];
+
+ rt1 += 4;
+ rt2 += 4;
+ rt += 4;
+ }
+ }
+}
+
+static void do_cross_effect(const SeqRenderData *context,
+ Sequence *UNUSED(seq),
+ float UNUSED(cfra),
+ float facf0,
+ float facf1,
+ ImBuf *ibuf1,
+ ImBuf *ibuf2,
+ ImBuf *UNUSED(ibuf3),
+ int start_line,
+ int total_lines,
+ ImBuf *out)
+{
+ if (out->rect_float) {
+ float *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+
+ slice_get_float_buffers(
+ context, ibuf1, ibuf2, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+
+ do_cross_effect_float(facf0, facf1, context->rectx, total_lines, rect1, rect2, rect_out);
+ }
+ else {
+ unsigned char *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+
+ slice_get_byte_buffers(
+ context, ibuf1, ibuf2, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+
+ do_cross_effect_byte(facf0, facf1, context->rectx, total_lines, rect1, rect2, rect_out);
+ }
+}
+
+/*********************** Gamma Cross *************************/
+
+/* copied code from initrender.c */
+static unsigned short gamtab[65536];
+static unsigned short igamtab1[256];
+static bool gamma_tabs_init = false;
+
+#define RE_GAMMA_TABLE_SIZE 400
+
+static float gamma_range_table[RE_GAMMA_TABLE_SIZE + 1];
+static float gamfactor_table[RE_GAMMA_TABLE_SIZE];
+static float inv_gamma_range_table[RE_GAMMA_TABLE_SIZE + 1];
+static float inv_gamfactor_table[RE_GAMMA_TABLE_SIZE];
+static float color_domain_table[RE_GAMMA_TABLE_SIZE + 1];
+static float color_step;
+static float inv_color_step;
+static float valid_gamma;
+static float valid_inv_gamma;
+
+static void makeGammaTables(float gamma)
+{
+ /* we need two tables: one forward, one backward */
+ int i;
+
+ valid_gamma = gamma;
+ valid_inv_gamma = 1.0f / gamma;
+ color_step = 1.0f / RE_GAMMA_TABLE_SIZE;
+ inv_color_step = (float)RE_GAMMA_TABLE_SIZE;
+
+ /* We could squeeze out the two range tables to gain some memory */
+ for (i = 0; i < RE_GAMMA_TABLE_SIZE; i++) {
+ color_domain_table[i] = i * color_step;
+ gamma_range_table[i] = pow(color_domain_table[i], valid_gamma);
+ inv_gamma_range_table[i] = pow(color_domain_table[i], valid_inv_gamma);
+ }
+
+ /* The end of the table should match 1.0 carefully. In order to avoid
+ * rounding errors, we just set this explicitly. The last segment may
+ * have a different length than the other segments, but our
+ * interpolation is insensitive to that
+ */
+ color_domain_table[RE_GAMMA_TABLE_SIZE] = 1.0;
+ gamma_range_table[RE_GAMMA_TABLE_SIZE] = 1.0;
+ inv_gamma_range_table[RE_GAMMA_TABLE_SIZE] = 1.0;
+
+ /* To speed up calculations, we make these calc factor tables. They are
+ * multiplication factors used in scaling the interpolation
+ */
+ for (i = 0; i < RE_GAMMA_TABLE_SIZE; i++) {
+ gamfactor_table[i] = inv_color_step * (gamma_range_table[i + 1] - gamma_range_table[i]);
+ inv_gamfactor_table[i] = inv_color_step *
+ (inv_gamma_range_table[i + 1] - inv_gamma_range_table[i]);
+ }
+}
+
+static float gammaCorrect(float c)
+{
+ int i;
+ float res;
+
+ i = floorf(c * inv_color_step);
+ /* Clip to range [0, 1]: outside, just do the complete calculation.
+ * We may have some performance problems here. Stretching up the LUT
+ * may help solve that, by exchanging LUT size for the interpolation.
+ * Negative colors are explicitly handled.
+ */
+ if (UNLIKELY(i < 0)) {
+ res = -powf(-c, valid_gamma);
+ }
+ else if (i >= RE_GAMMA_TABLE_SIZE) {
+ res = powf(c, valid_gamma);
+ }
+ else {
+ res = gamma_range_table[i] + ((c - color_domain_table[i]) * gamfactor_table[i]);
+ }
+
+ return res;
+}
+
+/* ------------------------------------------------------------------------- */
+
+static float invGammaCorrect(float c)
+{
+ int i;
+ float res = 0.0;
+
+ i = floorf(c * inv_color_step);
+ /* Negative colors are explicitly handled */
+ if (UNLIKELY(i < 0)) {
+ res = -powf(-c, valid_inv_gamma);
+ }
+ else if (i >= RE_GAMMA_TABLE_SIZE) {
+ res = powf(c, valid_inv_gamma);
+ }
+ else {
+ res = inv_gamma_range_table[i] + ((c - color_domain_table[i]) * inv_gamfactor_table[i]);
+ }
+
+ return res;
+}
+
+static void gamtabs(float gamma)
+{
+ float val, igamma = 1.0f / gamma;
+ int a;
+
+ /* gamtab: in short, out short */
+ for (a = 0; a < 65536; a++) {
+ val = a;
+ val /= 65535.0f;
+
+ if (gamma == 2.0f) {
+ val = sqrtf(val);
+ }
+ else if (gamma != 1.0f) {
+ val = powf(val, igamma);
+ }
+
+ gamtab[a] = (65535.99f * val);
+ }
+ /* inverse gamtab1 : in byte, out short */
+ for (a = 1; a <= 256; a++) {
+ if (gamma == 2.0f) {
+ igamtab1[a - 1] = a * a - 1;
+ }
+ else if (gamma == 1.0f) {
+ igamtab1[a - 1] = 256 * a - 1;
+ }
+ else {
+ val = a / 256.0f;
+ igamtab1[a - 1] = (65535.0 * pow(val, gamma)) - 1;
+ }
+ }
+}
+
+static void build_gammatabs(void)
+{
+ if (gamma_tabs_init == false) {
+ gamtabs(2.0f);
+ makeGammaTables(2.0f);
+ gamma_tabs_init = true;
+ }
+}
+
+static void init_gammacross(Sequence *UNUSED(seq))
+{
+}
+
+static void load_gammacross(Sequence *UNUSED(seq))
+{
+}
+
+static void free_gammacross(Sequence *UNUSED(seq), const bool UNUSED(do_id_user))
+{
+}
+
+static void do_gammacross_effect_byte(float facf0,
+ float UNUSED(facf1),
+ int x,
+ int y,
+ unsigned char *rect1,
+ unsigned char *rect2,
+ unsigned char *out)
+{
+ float fac1, fac2;
+ int xo;
+ unsigned char *cp1, *cp2, *rt;
+ float rt1[4], rt2[4], tempc[4];
+
+ xo = x;
+ cp1 = rect1;
+ cp2 = rect2;
+ rt = out;
+
+ fac2 = facf0;
+ fac1 = 1.0f - fac2;
+
+ while (y--) {
+ x = xo;
+ while (x--) {
+ straight_uchar_to_premul_float(rt1, cp1);
+ straight_uchar_to_premul_float(rt2, cp2);
+
+ tempc[0] = gammaCorrect(fac1 * invGammaCorrect(rt1[0]) + fac2 * invGammaCorrect(rt2[0]));
+ tempc[1] = gammaCorrect(fac1 * invGammaCorrect(rt1[1]) + fac2 * invGammaCorrect(rt2[1]));
+ tempc[2] = gammaCorrect(fac1 * invGammaCorrect(rt1[2]) + fac2 * invGammaCorrect(rt2[2]));
+ tempc[3] = gammaCorrect(fac1 * invGammaCorrect(rt1[3]) + fac2 * invGammaCorrect(rt2[3]));
+
+ premul_float_to_straight_uchar(rt, tempc);
+ cp1 += 4;
+ cp2 += 4;
+ rt += 4;
+ }
+
+ if (y == 0) {
+ break;
+ }
+ y--;
+
+ x = xo;
+ while (x--) {
+ straight_uchar_to_premul_float(rt1, cp1);
+ straight_uchar_to_premul_float(rt2, cp2);
+
+ tempc[0] = gammaCorrect(fac1 * invGammaCorrect(rt1[0]) + fac2 * invGammaCorrect(rt2[0]));
+ tempc[1] = gammaCorrect(fac1 * invGammaCorrect(rt1[1]) + fac2 * invGammaCorrect(rt2[1]));
+ tempc[2] = gammaCorrect(fac1 * invGammaCorrect(rt1[2]) + fac2 * invGammaCorrect(rt2[2]));
+ tempc[3] = gammaCorrect(fac1 * invGammaCorrect(rt1[3]) + fac2 * invGammaCorrect(rt2[3]));
+
+ premul_float_to_straight_uchar(rt, tempc);
+ cp1 += 4;
+ cp2 += 4;
+ rt += 4;
+ }
+ }
+}
+
+static void do_gammacross_effect_float(
+ float facf0, float UNUSED(facf1), int x, int y, float *rect1, float *rect2, float *out)
+{
+ float fac1, fac2;
+ int xo;
+ float *rt1, *rt2, *rt;
+
+ xo = x;
+ rt1 = rect1;
+ rt2 = rect2;
+ rt = out;
+
+ fac2 = facf0;
+ fac1 = 1.0f - fac2;
+
+ while (y--) {
+ x = xo * 4;
+ while (x--) {
+ *rt = gammaCorrect(fac1 * invGammaCorrect(*rt1) + fac2 * invGammaCorrect(*rt2));
+ rt1++;
+ rt2++;
+ rt++;
+ }
+
+ if (y == 0) {
+ break;
+ }
+ y--;
+
+ x = xo * 4;
+ while (x--) {
+ *rt = gammaCorrect(fac1 * invGammaCorrect(*rt1) + fac2 * invGammaCorrect(*rt2));
+
+ rt1++;
+ rt2++;
+ rt++;
+ }
+ }
+}
+
+static struct ImBuf *gammacross_init_execution(const SeqRenderData *context,
+ ImBuf *ibuf1,
+ ImBuf *ibuf2,
+ ImBuf *ibuf3)
+{
+ ImBuf *out = prepare_effect_imbufs(context, ibuf1, ibuf2, ibuf3);
+ build_gammatabs();
+
+ return out;
+}
+
+static void do_gammacross_effect(const SeqRenderData *context,
+ Sequence *UNUSED(seq),
+ float UNUSED(cfra),
+ float facf0,
+ float facf1,
+ ImBuf *ibuf1,
+ ImBuf *ibuf2,
+ ImBuf *UNUSED(ibuf3),
+ int start_line,
+ int total_lines,
+ ImBuf *out)
+{
+ if (out->rect_float) {
+ float *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+
+ slice_get_float_buffers(
+ context, ibuf1, ibuf2, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+
+ do_gammacross_effect_float(facf0, facf1, context->rectx, total_lines, rect1, rect2, rect_out);
+ }
+ else {
+ unsigned char *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+
+ slice_get_byte_buffers(
+ context, ibuf1, ibuf2, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+
+ do_gammacross_effect_byte(facf0, facf1, context->rectx, total_lines, rect1, rect2, rect_out);
+ }
+}
+
+/*********************** Add *************************/
+
+static void do_add_effect_byte(float facf0,
+ float facf1,
+ int x,
+ int y,
+ unsigned char *rect1,
+ unsigned char *rect2,
+ unsigned char *out)
+{
+ int xo, fac1, fac3;
+ unsigned char *cp1, *cp2, *rt;
+
+ xo = x;
+ cp1 = rect1;
+ cp2 = rect2;
+ rt = out;
+
+ fac1 = (int)(256.0f * facf0);
+ fac3 = (int)(256.0f * facf1);
+
+ while (y--) {
+ x = xo;
+
+ while (x--) {
+ const int m = fac1 * (int)cp2[3];
+ rt[0] = min_ii(cp1[0] + ((m * cp2[0]) >> 16), 255);
+ rt[1] = min_ii(cp1[1] + ((m * cp2[1]) >> 16), 255);
+ rt[2] = min_ii(cp1[2] + ((m * cp2[2]) >> 16), 255);
+ rt[3] = cp1[3];
+
+ cp1 += 4;
+ cp2 += 4;
+ rt += 4;
+ }
+
+ if (y == 0) {
+ break;
+ }
+ y--;
+
+ x = xo;
+ while (x--) {
+ const int m = fac3 * (int)cp2[3];
+ rt[0] = min_ii(cp1[0] + ((m * cp2[0]) >> 16), 255);
+ rt[1] = min_ii(cp1[1] + ((m * cp2[1]) >> 16), 255);
+ rt[2] = min_ii(cp1[2] + ((m * cp2[2]) >> 16), 255);
+ rt[3] = cp1[3];
+
+ cp1 += 4;
+ cp2 += 4;
+ rt += 4;
+ }
+ }
+}
+
+static void do_add_effect_float(
+ float facf0, float facf1, int x, int y, float *rect1, float *rect2, float *out)
+{
+ int xo;
+ float fac1, fac3;
+ float *rt1, *rt2, *rt;
+
+ xo = x;
+ rt1 = rect1;
+ rt2 = rect2;
+ rt = out;
+
+ fac1 = facf0;
+ fac3 = facf1;
+
+ while (y--) {
+ x = xo;
+ while (x--) {
+ const float m = (1.0f - (rt1[3] * (1.0f - fac1))) * rt2[3];
+ rt[0] = rt1[0] + m * rt2[0];
+ rt[1] = rt1[1] + m * rt2[1];
+ rt[2] = rt1[2] + m * rt2[2];
+ rt[3] = rt1[3];
+
+ rt1 += 4;
+ rt2 += 4;
+ rt += 4;
+ }
+
+ if (y == 0) {
+ break;
+ }
+ y--;
+
+ x = xo;
+ while (x--) {
+ const float m = (1.0f - (rt1[3] * (1.0f - fac3))) * rt2[3];
+ rt[0] = rt1[0] + m * rt2[0];
+ rt[1] = rt1[1] + m * rt2[1];
+ rt[2] = rt1[2] + m * rt2[2];
+ rt[3] = rt1[3];
+
+ rt1 += 4;
+ rt2 += 4;
+ rt += 4;
+ }
+ }
+}
+
+static void do_add_effect(const SeqRenderData *context,
+ Sequence *UNUSED(seq),
+ float UNUSED(cfra),
+ float facf0,
+ float facf1,
+ ImBuf *ibuf1,
+ ImBuf *ibuf2,
+ ImBuf *UNUSED(ibuf3),
+ int start_line,
+ int total_lines,
+ ImBuf *out)
+{
+ if (out->rect_float) {
+ float *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+
+ slice_get_float_buffers(
+ context, ibuf1, ibuf2, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+
+ do_add_effect_float(facf0, facf1, context->rectx, total_lines, rect1, rect2, rect_out);
+ }
+ else {
+ unsigned char *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+
+ slice_get_byte_buffers(
+ context, ibuf1, ibuf2, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+
+ do_add_effect_byte(facf0, facf1, context->rectx, total_lines, rect1, rect2, rect_out);
+ }
+}
+
+/*********************** Sub *************************/
+
+static void do_sub_effect_byte(float facf0,
+ float facf1,
+ int x,
+ int y,
+ unsigned char *rect1,
+ unsigned char *rect2,
+ unsigned char *out)
+{
+ int xo, fac1, fac3;
+ unsigned char *cp1, *cp2, *rt;
+
+ xo = x;
+ cp1 = rect1;
+ cp2 = rect2;
+ rt = out;
+
+ fac1 = (int)(256.0f * facf0);
+ fac3 = (int)(256.0f * facf1);
+
+ while (y--) {
+ x = xo;
+ while (x--) {
+ const int m = fac1 * (int)cp2[3];
+ rt[0] = max_ii(cp1[0] - ((m * cp2[0]) >> 16), 0);
+ rt[1] = max_ii(cp1[1] - ((m * cp2[1]) >> 16), 0);
+ rt[2] = max_ii(cp1[2] - ((m * cp2[2]) >> 16), 0);
+ rt[3] = cp1[3];
+
+ cp1 += 4;
+ cp2 += 4;
+ rt += 4;
+ }
+
+ if (y == 0) {
+ break;
+ }
+ y--;
+
+ x = xo;
+ while (x--) {
+ const int m = fac3 * (int)cp2[3];
+ rt[0] = max_ii(cp1[0] - ((m * cp2[0]) >> 16), 0);
+ rt[1] = max_ii(cp1[1] - ((m * cp2[1]) >> 16), 0);
+ rt[2] = max_ii(cp1[2] - ((m * cp2[2]) >> 16), 0);
+ rt[3] = cp1[3];
+
+ cp1 += 4;
+ cp2 += 4;
+ rt += 4;
+ }
+ }
+}
+
+static void do_sub_effect_float(
+ float UNUSED(facf0), float facf1, int x, int y, float *rect1, float *rect2, float *out)
+{
+ int xo;
+ float /* fac1, */ fac3_inv;
+ float *rt1, *rt2, *rt;
+
+ xo = x;
+ rt1 = rect1;
+ rt2 = rect2;
+ rt = out;
+
+ /* UNUSED */
+ // fac1 = facf0;
+ fac3_inv = 1.0f - facf1;
+
+ while (y--) {
+ x = xo;
+ while (x--) {
+ const float m = (1.0f - (rt1[3] * fac3_inv)) * rt2[3];
+ rt[0] = max_ff(rt1[0] - m * rt2[0], 0.0f);
+ rt[1] = max_ff(rt1[1] - m * rt2[1], 0.0f);
+ rt[2] = max_ff(rt1[2] - m * rt2[2], 0.0f);
+ rt[3] = rt1[3];
+
+ rt1 += 4;
+ rt2 += 4;
+ rt += 4;
+ }
+
+ if (y == 0) {
+ break;
+ }
+ y--;
+
+ x = xo;
+ while (x--) {
+ const float m = (1.0f - (rt1[3] * fac3_inv)) * rt2[3];
+ rt[0] = max_ff(rt1[0] - m * rt2[0], 0.0f);
+ rt[1] = max_ff(rt1[1] - m * rt2[1], 0.0f);
+ rt[2] = max_ff(rt1[2] - m * rt2[2], 0.0f);
+ rt[3] = rt1[3];
+
+ rt1 += 4;
+ rt2 += 4;
+ rt += 4;
+ }
+ }
+}
+
+static void do_sub_effect(const SeqRenderData *context,
+ Sequence *UNUSED(seq),
+ float UNUSED(cfra),
+ float facf0,
+ float facf1,
+ ImBuf *ibuf1,
+ ImBuf *ibuf2,
+ ImBuf *UNUSED(ibuf3),
+ int start_line,
+ int total_lines,
+ ImBuf *out)
+{
+ if (out->rect_float) {
+ float *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+
+ slice_get_float_buffers(
+ context, ibuf1, ibuf2, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+
+ do_sub_effect_float(facf0, facf1, context->rectx, total_lines, rect1, rect2, rect_out);
+ }
+ else {
+ unsigned char *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+
+ slice_get_byte_buffers(
+ context, ibuf1, ibuf2, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+
+ do_sub_effect_byte(facf0, facf1, context->rectx, total_lines, rect1, rect2, rect_out);
+ }
+}
+
+/*********************** Drop *************************/
+
+/* Must be > 0 or add precopy, etc to the function */
+#define XOFF 8
+#define YOFF 8
+
+static void do_drop_effect_byte(float facf0,
+ float facf1,
+ int x,
+ int y,
+ unsigned char *rect2i,
+ unsigned char *rect1i,
+ unsigned char *outi)
+{
+ int temp, fac, fac1, fac2;
+ unsigned char *rt1, *rt2, *out;
+ int field = 1;
+
+ const int width = x;
+ const int height = y;
+ const int xoff = min_ii(XOFF, width);
+ const int yoff = min_ii(YOFF, height);
+
+ fac1 = (int)(70.0f * facf0);
+ fac2 = (int)(70.0f * facf1);
+
+ rt2 = rect2i + yoff * 4 * width;
+ rt1 = rect1i;
+ out = outi;
+ for (y = 0; y < height - yoff; y++) {
+ if (field) {
+ fac = fac1;
+ }
+ else {
+ fac = fac2;
+ }
+ field = !field;
+
+ memcpy(out, rt1, sizeof(*out) * xoff * 4);
+ rt1 += xoff * 4;
+ out += xoff * 4;
+
+ for (x = xoff; x < width; x++) {
+ temp = ((fac * rt2[3]) >> 8);
+
+ *(out++) = MAX2(0, *rt1 - temp);
+ rt1++;
+ *(out++) = MAX2(0, *rt1 - temp);
+ rt1++;
+ *(out++) = MAX2(0, *rt1 - temp);
+ rt1++;
+ *(out++) = MAX2(0, *rt1 - temp);
+ rt1++;
+ rt2 += 4;
+ }
+ rt2 += xoff * 4;
+ }
+ memcpy(out, rt1, sizeof(*out) * yoff * 4 * width);
+}
+
+static void do_drop_effect_float(
+ float facf0, float facf1, int x, int y, float *rect2i, float *rect1i, float *outi)
+{
+ float temp, fac, fac1, fac2;
+ float *rt1, *rt2, *out;
+ int field = 1;
+
+ const int width = x;
+ const int height = y;
+ const int xoff = min_ii(XOFF, width);
+ const int yoff = min_ii(YOFF, height);
+
+ fac1 = 70.0f * facf0;
+ fac2 = 70.0f * facf1;
+
+ rt2 = rect2i + yoff * 4 * width;
+ rt1 = rect1i;
+ out = outi;
+ for (y = 0; y < height - yoff; y++) {
+ if (field) {
+ fac = fac1;
+ }
+ else {
+ fac = fac2;
+ }
+ field = !field;
+
+ memcpy(out, rt1, sizeof(*out) * xoff * 4);
+ rt1 += xoff * 4;
+ out += xoff * 4;
+
+ for (x = xoff; x < width; x++) {
+ temp = fac * rt2[3];
+
+ *(out++) = MAX2(0.0f, *rt1 - temp);
+ rt1++;
+ *(out++) = MAX2(0.0f, *rt1 - temp);
+ rt1++;
+ *(out++) = MAX2(0.0f, *rt1 - temp);
+ rt1++;
+ *(out++) = MAX2(0.0f, *rt1 - temp);
+ rt1++;
+ rt2 += 4;
+ }
+ rt2 += xoff * 4;
+ }
+ memcpy(out, rt1, sizeof(*out) * yoff * 4 * width);
+}
+
+/*********************** Mul *************************/
+
+static void do_mul_effect_byte(float facf0,
+ float facf1,
+ int x,
+ int y,
+ unsigned char *rect1,
+ unsigned char *rect2,
+ unsigned char *out)
+{
+ int xo, fac1, fac3;
+ unsigned char *rt1, *rt2, *rt;
+
+ xo = x;
+ rt1 = rect1;
+ rt2 = rect2;
+ rt = out;
+
+ fac1 = (int)(256.0f * facf0);
+ fac3 = (int)(256.0f * facf1);
+
+ /* formula:
+ * fac * (a * b) + (1 - fac) * a => fac * a * (b - 1) + axaux = c * px + py * s; //+centx
+ * yaux = -s * px + c * py; //+centy
+ */
+
+ while (y--) {
+
+ x = xo;
+ while (x--) {
+
+ rt[0] = rt1[0] + ((fac1 * rt1[0] * (rt2[0] - 255)) >> 16);
+ rt[1] = rt1[1] + ((fac1 * rt1[1] * (rt2[1] - 255)) >> 16);
+ rt[2] = rt1[2] + ((fac1 * rt1[2] * (rt2[2] - 255)) >> 16);
+ rt[3] = rt1[3] + ((fac1 * rt1[3] * (rt2[3] - 255)) >> 16);
+
+ rt1 += 4;
+ rt2 += 4;
+ rt += 4;
+ }
+
+ if (y == 0) {
+ break;
+ }
+ y--;
+
+ x = xo;
+ while (x--) {
+
+ rt[0] = rt1[0] + ((fac3 * rt1[0] * (rt2[0] - 255)) >> 16);
+ rt[1] = rt1[1] + ((fac3 * rt1[1] * (rt2[1] - 255)) >> 16);
+ rt[2] = rt1[2] + ((fac3 * rt1[2] * (rt2[2] - 255)) >> 16);
+ rt[3] = rt1[3] + ((fac3 * rt1[3] * (rt2[3] - 255)) >> 16);
+
+ rt1 += 4;
+ rt2 += 4;
+ rt += 4;
+ }
+ }
+}
+
+static void do_mul_effect_float(
+ float facf0, float facf1, int x, int y, float *rect1, float *rect2, float *out)
+{
+ int xo;
+ float fac1, fac3;
+ float *rt1, *rt2, *rt;
+
+ xo = x;
+ rt1 = rect1;
+ rt2 = rect2;
+ rt = out;
+
+ fac1 = facf0;
+ fac3 = facf1;
+
+ /* formula:
+ * fac * (a * b) + (1 - fac) * a => fac * a * (b - 1) + a
+ */
+
+ while (y--) {
+ x = xo;
+ while (x--) {
+ rt[0] = rt1[0] + fac1 * rt1[0] * (rt2[0] - 1.0f);
+ rt[1] = rt1[1] + fac1 * rt1[1] * (rt2[1] - 1.0f);
+ rt[2] = rt1[2] + fac1 * rt1[2] * (rt2[2] - 1.0f);
+ rt[3] = rt1[3] + fac1 * rt1[3] * (rt2[3] - 1.0f);
+
+ rt1 += 4;
+ rt2 += 4;
+ rt += 4;
+ }
+
+ if (y == 0) {
+ break;
+ }
+ y--;
+
+ x = xo;
+ while (x--) {
+ rt[0] = rt1[0] + fac3 * rt1[0] * (rt2[0] - 1.0f);
+ rt[1] = rt1[1] + fac3 * rt1[1] * (rt2[1] - 1.0f);
+ rt[2] = rt1[2] + fac3 * rt1[2] * (rt2[2] - 1.0f);
+ rt[3] = rt1[3] + fac3 * rt1[3] * (rt2[3] - 1.0f);
+
+ rt1 += 4;
+ rt2 += 4;
+ rt += 4;
+ }
+ }
+}
+
+static void do_mul_effect(const SeqRenderData *context,
+ Sequence *UNUSED(seq),
+ float UNUSED(cfra),
+ float facf0,
+ float facf1,
+ ImBuf *ibuf1,
+ ImBuf *ibuf2,
+ ImBuf *UNUSED(ibuf3),
+ int start_line,
+ int total_lines,
+ ImBuf *out)
+{
+ if (out->rect_float) {
+ float *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+
+ slice_get_float_buffers(
+ context, ibuf1, ibuf2, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+
+ do_mul_effect_float(facf0, facf1, context->rectx, total_lines, rect1, rect2, rect_out);
+ }
+ else {
+ unsigned char *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+
+ slice_get_byte_buffers(
+ context, ibuf1, ibuf2, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+
+ do_mul_effect_byte(facf0, facf1, context->rectx, total_lines, rect1, rect2, rect_out);
+ }
+}
+
+/*********************** Blend Mode ***************************************/
+typedef void (*IMB_blend_func_byte)(unsigned char *dst,
+ const unsigned char *src1,
+ const unsigned char *src2);
+typedef void (*IMB_blend_func_float)(float *dst, const float *src1, const float *src2);
+
+BLI_INLINE void apply_blend_function_byte(float facf0,
+ float facf1,
+ int x,
+ int y,
+ unsigned char *rect1,
+ unsigned char *rect2,
+ unsigned char *out,
+ IMB_blend_func_byte blend_function)
+{
+ int xo;
+ unsigned char *rt1, *rt2, *rt;
+ unsigned int achannel;
+ xo = x;
+ rt1 = rect1;
+ rt2 = rect2;
+ rt = out;
+ while (y--) {
+ for (x = xo; x > 0; x--) {
+ achannel = rt1[3];
+ rt1[3] = (unsigned int)achannel * facf0;
+ blend_function(rt, rt1, rt2);
+ rt1[3] = achannel;
+ rt[3] = rt1[3];
+ rt1 += 4;
+ rt2 += 4;
+ rt += 4;
+ }
+ if (y == 0) {
+ break;
+ }
+ y--;
+ for (x = xo; x > 0; x--) {
+ achannel = rt1[3];
+ rt1[3] = (unsigned int)achannel * facf1;
+ blend_function(rt, rt1, rt2);
+ rt1[3] = achannel;
+ rt[3] = rt1[3];
+ rt1 += 4;
+ rt2 += 4;
+ rt += 4;
+ }
+ }
+}
+
+BLI_INLINE void apply_blend_function_float(float facf0,
+ float facf1,
+ int x,
+ int y,
+ float *rect1,
+ float *rect2,
+ float *out,
+ IMB_blend_func_float blend_function)
+{
+ int xo;
+ float *rt1, *rt2, *rt;
+ float achannel;
+ xo = x;
+ rt1 = rect1;
+ rt2 = rect2;
+ rt = out;
+ while (y--) {
+ for (x = xo; x > 0; x--) {
+ achannel = rt1[3];
+ rt1[3] = achannel * facf0;
+ blend_function(rt, rt1, rt2);
+ rt1[3] = achannel;
+ rt[3] = rt1[3];
+ rt1 += 4;
+ rt2 += 4;
+ rt += 4;
+ }
+ if (y == 0) {
+ break;
+ }
+ y--;
+ for (x = xo; x > 0; x--) {
+ achannel = rt1[3];
+ rt1[3] = achannel * facf1;
+ blend_function(rt, rt1, rt2);
+ rt1[3] = achannel;
+ rt[3] = rt1[3];
+ rt1 += 4;
+ rt2 += 4;
+ rt += 4;
+ }
+ }
+}
+
+static void do_blend_effect_float(
+ float facf0, float facf1, int x, int y, float *rect1, float *rect2, int btype, float *out)
+{
+ switch (btype) {
+ case SEQ_TYPE_ADD:
+ apply_blend_function_float(facf0, facf1, x, y, rect1, rect2, out, blend_color_add_float);
+ break;
+ case SEQ_TYPE_SUB:
+ apply_blend_function_float(facf0, facf1, x, y, rect1, rect2, out, blend_color_sub_float);
+ break;
+ case SEQ_TYPE_MUL:
+ apply_blend_function_float(facf0, facf1, x, y, rect1, rect2, out, blend_color_mul_float);
+ break;
+ case SEQ_TYPE_DARKEN:
+ apply_blend_function_float(facf0, facf1, x, y, rect1, rect2, out, blend_color_darken_float);
+ break;
+ case SEQ_TYPE_COLOR_BURN:
+ apply_blend_function_float(facf0, facf1, x, y, rect1, rect2, out, blend_color_burn_float);
+ break;
+ case SEQ_TYPE_LINEAR_BURN:
+ apply_blend_function_float(
+ facf0, facf1, x, y, rect1, rect2, out, blend_color_linearburn_float);
+ break;
+ case SEQ_TYPE_SCREEN:
+ apply_blend_function_float(facf0, facf1, x, y, rect1, rect2, out, blend_color_screen_float);
+ break;
+ case SEQ_TYPE_LIGHTEN:
+ apply_blend_function_float(facf0, facf1, x, y, rect1, rect2, out, blend_color_lighten_float);
+ break;
+ case SEQ_TYPE_DODGE:
+ apply_blend_function_float(facf0, facf1, x, y, rect1, rect2, out, blend_color_dodge_float);
+ break;
+ case SEQ_TYPE_OVERLAY:
+ apply_blend_function_float(facf0, facf1, x, y, rect1, rect2, out, blend_color_overlay_float);
+ break;
+ case SEQ_TYPE_SOFT_LIGHT:
+ apply_blend_function_float(
+ facf0, facf1, x, y, rect1, rect2, out, blend_color_softlight_float);
+ break;
+ case SEQ_TYPE_HARD_LIGHT:
+ apply_blend_function_float(
+ facf0, facf1, x, y, rect1, rect2, out, blend_color_hardlight_float);
+ break;
+ case SEQ_TYPE_PIN_LIGHT:
+ apply_blend_function_float(
+ facf0, facf1, x, y, rect1, rect2, out, blend_color_pinlight_float);
+ break;
+ case SEQ_TYPE_LIN_LIGHT:
+ apply_blend_function_float(
+ facf0, facf1, x, y, rect1, rect2, out, blend_color_linearlight_float);
+ break;
+ case SEQ_TYPE_VIVID_LIGHT:
+ apply_blend_function_float(
+ facf0, facf1, x, y, rect1, rect2, out, blend_color_vividlight_float);
+ break;
+ case SEQ_TYPE_BLEND_COLOR:
+ apply_blend_function_float(facf0, facf1, x, y, rect1, rect2, out, blend_color_color_float);
+ break;
+ case SEQ_TYPE_HUE:
+ apply_blend_function_float(facf0, facf1, x, y, rect1, rect2, out, blend_color_hue_float);
+ break;
+ case SEQ_TYPE_SATURATION:
+ apply_blend_function_float(
+ facf0, facf1, x, y, rect1, rect2, out, blend_color_saturation_float);
+ break;
+ case SEQ_TYPE_VALUE:
+ apply_blend_function_float(
+ facf0, facf1, x, y, rect1, rect2, out, blend_color_luminosity_float);
+ break;
+ case SEQ_TYPE_DIFFERENCE:
+ apply_blend_function_float(
+ facf0, facf1, x, y, rect1, rect2, out, blend_color_difference_float);
+ break;
+ case SEQ_TYPE_EXCLUSION:
+ apply_blend_function_float(
+ facf0, facf1, x, y, rect1, rect2, out, blend_color_exclusion_float);
+ break;
+ default:
+ break;
+ }
+}
+
+static void do_blend_effect_byte(float facf0,
+ float facf1,
+ int x,
+ int y,
+ unsigned char *rect1,
+ unsigned char *rect2,
+ int btype,
+ unsigned char *out)
+{
+ switch (btype) {
+ case SEQ_TYPE_ADD:
+ apply_blend_function_byte(facf0, facf1, x, y, rect1, rect2, out, blend_color_add_byte);
+ break;
+ case SEQ_TYPE_SUB:
+ apply_blend_function_byte(facf0, facf1, x, y, rect1, rect2, out, blend_color_sub_byte);
+ break;
+ case SEQ_TYPE_MUL:
+ apply_blend_function_byte(facf0, facf1, x, y, rect1, rect2, out, blend_color_mul_byte);
+ break;
+ case SEQ_TYPE_DARKEN:
+ apply_blend_function_byte(facf0, facf1, x, y, rect1, rect2, out, blend_color_darken_byte);
+ break;
+ case SEQ_TYPE_COLOR_BURN:
+ apply_blend_function_byte(facf0, facf1, x, y, rect1, rect2, out, blend_color_burn_byte);
+ break;
+ case SEQ_TYPE_LINEAR_BURN:
+ apply_blend_function_byte(
+ facf0, facf1, x, y, rect1, rect2, out, blend_color_linearburn_byte);
+ break;
+ case SEQ_TYPE_SCREEN:
+ apply_blend_function_byte(facf0, facf1, x, y, rect1, rect2, out, blend_color_screen_byte);
+ break;
+ case SEQ_TYPE_LIGHTEN:
+ apply_blend_function_byte(facf0, facf1, x, y, rect1, rect2, out, blend_color_lighten_byte);
+ break;
+ case SEQ_TYPE_DODGE:
+ apply_blend_function_byte(facf0, facf1, x, y, rect1, rect2, out, blend_color_dodge_byte);
+ break;
+ case SEQ_TYPE_OVERLAY:
+ apply_blend_function_byte(facf0, facf1, x, y, rect1, rect2, out, blend_color_overlay_byte);
+ break;
+ case SEQ_TYPE_SOFT_LIGHT:
+ apply_blend_function_byte(facf0, facf1, x, y, rect1, rect2, out, blend_color_softlight_byte);
+ break;
+ case SEQ_TYPE_HARD_LIGHT:
+ apply_blend_function_byte(facf0, facf1, x, y, rect1, rect2, out, blend_color_hardlight_byte);
+ break;
+ case SEQ_TYPE_PIN_LIGHT:
+ apply_blend_function_byte(facf0, facf1, x, y, rect1, rect2, out, blend_color_pinlight_byte);
+ break;
+ case SEQ_TYPE_LIN_LIGHT:
+ apply_blend_function_byte(
+ facf0, facf1, x, y, rect1, rect2, out, blend_color_linearlight_byte);
+ break;
+ case SEQ_TYPE_VIVID_LIGHT:
+ apply_blend_function_byte(
+ facf0, facf1, x, y, rect1, rect2, out, blend_color_vividlight_byte);
+ break;
+ case SEQ_TYPE_BLEND_COLOR:
+ apply_blend_function_byte(facf0, facf1, x, y, rect1, rect2, out, blend_color_color_byte);
+ break;
+ case SEQ_TYPE_HUE:
+ apply_blend_function_byte(facf0, facf1, x, y, rect1, rect2, out, blend_color_hue_byte);
+ break;
+ case SEQ_TYPE_SATURATION:
+ apply_blend_function_byte(
+ facf0, facf1, x, y, rect1, rect2, out, blend_color_saturation_byte);
+ break;
+ case SEQ_TYPE_VALUE:
+ apply_blend_function_byte(
+ facf0, facf1, x, y, rect1, rect2, out, blend_color_luminosity_byte);
+ break;
+ case SEQ_TYPE_DIFFERENCE:
+ apply_blend_function_byte(
+ facf0, facf1, x, y, rect1, rect2, out, blend_color_difference_byte);
+ break;
+ case SEQ_TYPE_EXCLUSION:
+ apply_blend_function_byte(facf0, facf1, x, y, rect1, rect2, out, blend_color_exclusion_byte);
+ break;
+ default:
+ break;
+ }
+}
+
+static void do_blend_mode_effect(const SeqRenderData *context,
+ Sequence *seq,
+ float UNUSED(cfra),
+ float facf0,
+ float facf1,
+ ImBuf *ibuf1,
+ ImBuf *ibuf2,
+ ImBuf *UNUSED(ibuf3),
+ int start_line,
+ int total_lines,
+ ImBuf *out)
+{
+ if (out->rect_float) {
+ float *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+ slice_get_float_buffers(
+ context, ibuf1, ibuf2, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+ do_blend_effect_float(
+ facf0, facf1, context->rectx, total_lines, rect1, rect2, seq->blend_mode, rect_out);
+ }
+ else {
+ unsigned char *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+ slice_get_byte_buffers(
+ context, ibuf1, ibuf2, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+ do_blend_effect_byte(
+ facf0, facf1, context->rectx, total_lines, rect1, rect2, seq->blend_mode, rect_out);
+ }
+}
+/*********************** Color Mix Effect *************************/
+static void init_colormix_effect(Sequence *seq)
+{
+ ColorMixVars *data;
+
+ if (seq->effectdata) {
+ MEM_freeN(seq->effectdata);
+ }
+ seq->effectdata = MEM_callocN(sizeof(ColorMixVars), "colormixvars");
+ data = (ColorMixVars *)seq->effectdata;
+ data->blend_effect = SEQ_TYPE_OVERLAY;
+ data->factor = 1.0f;
+}
+
+static void do_colormix_effect(const SeqRenderData *context,
+ Sequence *seq,
+ float UNUSED(cfra),
+ float UNUSED(facf0),
+ float UNUSED(facf1),
+ ImBuf *ibuf1,
+ ImBuf *ibuf2,
+ ImBuf *UNUSED(ibuf3),
+ int start_line,
+ int total_lines,
+ ImBuf *out)
+{
+ float facf;
+
+ ColorMixVars *data = seq->effectdata;
+ facf = data->factor;
+
+ if (out->rect_float) {
+ float *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+ slice_get_float_buffers(
+ context, ibuf1, ibuf2, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+ do_blend_effect_float(
+ facf, facf, context->rectx, total_lines, rect1, rect2, data->blend_effect, rect_out);
+ }
+ else {
+ unsigned char *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+ slice_get_byte_buffers(
+ context, ibuf1, ibuf2, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+ do_blend_effect_byte(
+ facf, facf, context->rectx, total_lines, rect1, rect2, data->blend_effect, rect_out);
+ }
+}
+
+/*********************** Wipe *************************/
+
+typedef struct WipeZone {
+ float angle;
+ int flip;
+ int xo, yo;
+ int width;
+ float pythangle;
+} WipeZone;
+
+static void precalc_wipe_zone(WipeZone *wipezone, WipeVars *wipe, int xo, int yo)
+{
+ wipezone->flip = (wipe->angle < 0.0f);
+ wipezone->angle = tanf(fabsf(wipe->angle));
+ wipezone->xo = xo;
+ wipezone->yo = yo;
+ wipezone->width = (int)(wipe->edgeWidth * ((xo + yo) / 2.0f));
+ wipezone->pythangle = 1.0f / sqrtf(wipezone->angle * wipezone->angle + 1.0f);
+}
+
+/* This function calculates the blur band for the wipe effects */
+static float in_band(float width, float dist, int side, int dir)
+{
+ float alpha;
+
+ if (width == 0) {
+ return (float)side;
+ }
+
+ if (width < dist) {
+ return (float)side;
+ }
+
+ if (side == 1) {
+ alpha = (dist + 0.5f * width) / (width);
+ }
+ else {
+ alpha = (0.5f * width - dist) / (width);
+ }
+
+ if (dir == 0) {
+ alpha = 1 - alpha;
+ }
+
+ return alpha;
+}
+
+static float check_zone(WipeZone *wipezone, int x, int y, Sequence *seq, float facf0)
+{
+ float posx, posy, hyp, hyp2, angle, hwidth, b1, b2, b3, pointdist;
+ /* some future stuff */
+ /* float hyp3, hyp4, b4, b5 */
+ float temp1, temp2, temp3, temp4; /* some placeholder variables */
+ int xo = wipezone->xo;
+ int yo = wipezone->yo;
+ float halfx = xo * 0.5f;
+ float halfy = yo * 0.5f;
+ float widthf, output = 0;
+ WipeVars *wipe = (WipeVars *)seq->effectdata;
+ int width;
+
+ if (wipezone->flip) {
+ x = xo - x;
+ }
+ angle = wipezone->angle;
+
+ if (wipe->forward) {
+ posx = facf0 * xo;
+ posy = facf0 * yo;
+ }
+ else {
+ posx = xo - facf0 * xo;
+ posy = yo - facf0 * yo;
+ }
+
+ switch (wipe->wipetype) {
+ case DO_SINGLE_WIPE:
+ width = min_ii(wipezone->width, facf0 * yo);
+ width = min_ii(width, yo - facf0 * yo);
+
+ if (angle == 0.0f) {
+ b1 = posy;
+ b2 = y;
+ hyp = fabsf(y - posy);
+ }
+ else {
+ b1 = posy - (-angle) * posx;
+ b2 = y - (-angle) * x;
+ hyp = fabsf(angle * x + y + (-posy - angle * posx)) * wipezone->pythangle;
+ }
+
+ if (angle < 0) {
+ temp1 = b1;
+ b1 = b2;
+ b2 = temp1;
+ }
+
+ if (wipe->forward) {
+ if (b1 < b2) {
+ output = in_band(width, hyp, 1, 1);
+ }
+ else {
+ output = in_band(width, hyp, 0, 1);
+ }
+ }
+ else {
+ if (b1 < b2) {
+ output = in_band(width, hyp, 0, 1);
+ }
+ else {
+ output = in_band(width, hyp, 1, 1);
+ }
+ }
+ break;
+
+ case DO_DOUBLE_WIPE:
+ if (!wipe->forward) {
+ facf0 = 1.0f - facf0; /* Go the other direction */
+ }
+
+ width = wipezone->width; /* calculate the blur width */
+ hwidth = width * 0.5f;
+ if (angle == 0) {
+ b1 = posy * 0.5f;
+ b3 = yo - posy * 0.5f;
+ b2 = y;
+
+ hyp = fabsf(y - posy * 0.5f);
+ hyp2 = fabsf(y - (yo - posy * 0.5f));
+ }
+ else {
+ b1 = posy * 0.5f - (-angle) * posx * 0.5f;
+ b3 = (yo - posy * 0.5f) - (-angle) * (xo - posx * 0.5f);
+ b2 = y - (-angle) * x;
+
+ hyp = fabsf(angle * x + y + (-posy * 0.5f - angle * posx * 0.5f)) * wipezone->pythangle;
+ hyp2 = fabsf(angle * x + y + (-(yo - posy * 0.5f) - angle * (xo - posx * 0.5f))) *
+ wipezone->pythangle;
+ }
+
+ hwidth = min_ff(hwidth, fabsf(b3 - b1) / 2.0f);
+
+ if (b2 < b1 && b2 < b3) {
+ output = in_band(hwidth, hyp, 0, 1);
+ }
+ else if (b2 > b1 && b2 > b3) {
+ output = in_band(hwidth, hyp2, 0, 1);
+ }
+ else {
+ if (hyp < hwidth && hyp2 > hwidth) {
+ output = in_band(hwidth, hyp, 1, 1);
+ }
+ else if (hyp > hwidth && hyp2 < hwidth) {
+ output = in_band(hwidth, hyp2, 1, 1);
+ }
+ else {
+ output = in_band(hwidth, hyp2, 1, 1) * in_band(hwidth, hyp, 1, 1);
+ }
+ }
+ if (!wipe->forward) {
+ output = 1 - output;
+ }
+ break;
+ case DO_CLOCK_WIPE:
+ /*
+ * temp1: angle of effect center in rads
+ * temp2: angle of line through (halfx, halfy) and (x, y) in rads
+ * temp3: angle of low side of blur
+ * temp4: angle of high side of blur
+ */
+ output = 1.0f - facf0;
+ widthf = wipe->edgeWidth * 2.0f * (float)M_PI;
+ temp1 = 2.0f * (float)M_PI * facf0;
+
+ if (wipe->forward) {
+ temp1 = 2.0f * (float)M_PI - temp1;
+ }
+
+ x = x - halfx;
+ y = y - halfy;
+
+ temp2 = asin(abs(y) / hypot(x, y));
+ if (x <= 0 && y >= 0) {
+ temp2 = (float)M_PI - temp2;
+ }
+ else if (x <= 0 && y <= 0) {
+ temp2 += (float)M_PI;
+ }
+ else if (x >= 0 && y <= 0) {
+ temp2 = 2.0f * (float)M_PI - temp2;
+ }
+
+ if (wipe->forward) {
+ temp3 = temp1 - (widthf * 0.5f) * facf0;
+ temp4 = temp1 + (widthf * 0.5f) * (1 - facf0);
+ }
+ else {
+ temp3 = temp1 - (widthf * 0.5f) * (1 - facf0);
+ temp4 = temp1 + (widthf * 0.5f) * facf0;
+ }
+ if (temp3 < 0) {
+ temp3 = 0;
+ }
+ if (temp4 > 2.0f * (float)M_PI) {
+ temp4 = 2.0f * (float)M_PI;
+ }
+
+ if (temp2 < temp3) {
+ output = 0;
+ }
+ else if (temp2 > temp4) {
+ output = 1;
+ }
+ else {
+ output = (temp2 - temp3) / (temp4 - temp3);
+ }
+ if (x == 0 && y == 0) {
+ output = 1;
+ }
+ if (output != output) {
+ output = 1;
+ }
+ if (wipe->forward) {
+ output = 1 - output;
+ }
+ break;
+ case DO_IRIS_WIPE:
+ if (xo > yo) {
+ yo = xo;
+ }
+ else {
+ xo = yo;
+ }
+
+ if (!wipe->forward) {
+ facf0 = 1 - facf0;
+ }
+
+ width = wipezone->width;
+ hwidth = width * 0.5f;
+
+ temp1 = (halfx - (halfx)*facf0);
+ pointdist = hypotf(temp1, temp1);
+
+ temp2 = hypotf(halfx - x, halfy - y);
+ if (temp2 > pointdist) {
+ output = in_band(hwidth, fabsf(temp2 - pointdist), 0, 1);
+ }
+ else {
+ output = in_band(hwidth, fabsf(temp2 - pointdist), 1, 1);
+ }
+
+ if (!wipe->forward) {
+ output = 1 - output;
+ }
+
+ break;
+ }
+ if (output < 0) {
+ output = 0;
+ }
+ else if (output > 1) {
+ output = 1;
+ }
+ return output;
+}
+
+static void init_wipe_effect(Sequence *seq)
+{
+ if (seq->effectdata) {
+ MEM_freeN(seq->effectdata);
+ }
+
+ seq->effectdata = MEM_callocN(sizeof(WipeVars), "wipevars");
+}
+
+static int num_inputs_wipe(void)
+{
+ return 2;
+}
+
+static void free_wipe_effect(Sequence *seq, const bool UNUSED(do_id_user))
+{
+ if (seq->effectdata) {
+ MEM_freeN(seq->effectdata);
+ }
+
+ seq->effectdata = NULL;
+}
+
+static void copy_wipe_effect(Sequence *dst, Sequence *src, const int UNUSED(flag))
+{
+ dst->effectdata = MEM_dupallocN(src->effectdata);
+}
+
+static void do_wipe_effect_byte(Sequence *seq,
+ float facf0,
+ float UNUSED(facf1),
+ int x,
+ int y,
+ unsigned char *rect1,
+ unsigned char *rect2,
+ unsigned char *out)
+{
+ WipeZone wipezone;
+ WipeVars *wipe = (WipeVars *)seq->effectdata;
+ int xo, yo;
+ unsigned char *cp1, *cp2, *rt;
+
+ precalc_wipe_zone(&wipezone, wipe, x, y);
+
+ cp1 = rect1;
+ cp2 = rect2;
+ rt = out;
+
+ xo = x;
+ yo = y;
+ for (y = 0; y < yo; y++) {
+ for (x = 0; x < xo; x++) {
+ float check = check_zone(&wipezone, x, y, seq, facf0);
+ if (check) {
+ if (cp1) {
+ float rt1[4], rt2[4], tempc[4];
+
+ straight_uchar_to_premul_float(rt1, cp1);
+ straight_uchar_to_premul_float(rt2, cp2);
+
+ tempc[0] = rt1[0] * check + rt2[0] * (1 - check);
+ tempc[1] = rt1[1] * check + rt2[1] * (1 - check);
+ tempc[2] = rt1[2] * check + rt2[2] * (1 - check);
+ tempc[3] = rt1[3] * check + rt2[3] * (1 - check);
+
+ premul_float_to_straight_uchar(rt, tempc);
+ }
+ else {
+ rt[0] = 0;
+ rt[1] = 0;
+ rt[2] = 0;
+ rt[3] = 255;
+ }
+ }
+ else {
+ if (cp2) {
+ rt[0] = cp2[0];
+ rt[1] = cp2[1];
+ rt[2] = cp2[2];
+ rt[3] = cp2[3];
+ }
+ else {
+ rt[0] = 0;
+ rt[1] = 0;
+ rt[2] = 0;
+ rt[3] = 255;
+ }
+ }
+
+ rt += 4;
+ if (cp1 != NULL) {
+ cp1 += 4;
+ }
+ if (cp2 != NULL) {
+ cp2 += 4;
+ }
+ }
+ }
+}
+
+static void do_wipe_effect_float(Sequence *seq,
+ float facf0,
+ float UNUSED(facf1),
+ int x,
+ int y,
+ float *rect1,
+ float *rect2,
+ float *out)
+{
+ WipeZone wipezone;
+ WipeVars *wipe = (WipeVars *)seq->effectdata;
+ int xo, yo;
+ float *rt1, *rt2, *rt;
+
+ precalc_wipe_zone(&wipezone, wipe, x, y);
+
+ rt1 = rect1;
+ rt2 = rect2;
+ rt = out;
+
+ xo = x;
+ yo = y;
+ for (y = 0; y < yo; y++) {
+ for (x = 0; x < xo; x++) {
+ float check = check_zone(&wipezone, x, y, seq, facf0);
+ if (check) {
+ if (rt1) {
+ rt[0] = rt1[0] * check + rt2[0] * (1 - check);
+ rt[1] = rt1[1] * check + rt2[1] * (1 - check);
+ rt[2] = rt1[2] * check + rt2[2] * (1 - check);
+ rt[3] = rt1[3] * check + rt2[3] * (1 - check);
+ }
+ else {
+ rt[0] = 0;
+ rt[1] = 0;
+ rt[2] = 0;
+ rt[3] = 1.0;
+ }
+ }
+ else {
+ if (rt2) {
+ rt[0] = rt2[0];
+ rt[1] = rt2[1];
+ rt[2] = rt2[2];
+ rt[3] = rt2[3];
+ }
+ else {
+ rt[0] = 0;
+ rt[1] = 0;
+ rt[2] = 0;
+ rt[3] = 1.0;
+ }
+ }
+
+ rt += 4;
+ if (rt1 != NULL) {
+ rt1 += 4;
+ }
+ if (rt2 != NULL) {
+ rt2 += 4;
+ }
+ }
+ }
+}
+
+static ImBuf *do_wipe_effect(const SeqRenderData *context,
+ Sequence *seq,
+ float UNUSED(cfra),
+ float facf0,
+ float facf1,
+ ImBuf *ibuf1,
+ ImBuf *ibuf2,
+ ImBuf *ibuf3)
+{
+ ImBuf *out = prepare_effect_imbufs(context, ibuf1, ibuf2, ibuf3);
+
+ if (out->rect_float) {
+ do_wipe_effect_float(seq,
+ facf0,
+ facf1,
+ context->rectx,
+ context->recty,
+ ibuf1->rect_float,
+ ibuf2->rect_float,
+ out->rect_float);
+ }
+ else {
+ do_wipe_effect_byte(seq,
+ facf0,
+ facf1,
+ context->rectx,
+ context->recty,
+ (unsigned char *)ibuf1->rect,
+ (unsigned char *)ibuf2->rect,
+ (unsigned char *)out->rect);
+ }
+
+ return out;
+}
+
+/*********************** Transform *************************/
+
+static void init_transform_effect(Sequence *seq)
+{
+ TransformVars *transform;
+
+ if (seq->effectdata) {
+ MEM_freeN(seq->effectdata);
+ }
+
+ seq->effectdata = MEM_callocN(sizeof(TransformVars), "transformvars");
+
+ transform = (TransformVars *)seq->effectdata;
+
+ transform->ScalexIni = 1.0f;
+ transform->ScaleyIni = 1.0f;
+
+ transform->xIni = 0.0f;
+ transform->yIni = 0.0f;
+
+ transform->rotIni = 0.0f;
+
+ transform->interpolation = 1;
+ transform->percent = 1;
+ transform->uniform_scale = 0;
+}
+
+static int num_inputs_transform(void)
+{
+ return 1;
+}
+
+static void free_transform_effect(Sequence *seq, const bool UNUSED(do_id_user))
+{
+ if (seq->effectdata) {
+ MEM_freeN(seq->effectdata);
+ }
+ seq->effectdata = NULL;
+}
+
+static void copy_transform_effect(Sequence *dst, Sequence *src, const int UNUSED(flag))
+{
+ dst->effectdata = MEM_dupallocN(src->effectdata);
+}
+
+static void transform_image(int x,
+ int y,
+ int start_line,
+ int total_lines,
+ ImBuf *ibuf1,
+ ImBuf *out,
+ float scale_x,
+ float scale_y,
+ float translate_x,
+ float translate_y,
+ float rotate,
+ int interpolation)
+{
+ /* Rotate */
+ float s = sinf(rotate);
+ float c = cosf(rotate);
+
+ for (int yi = start_line; yi < start_line + total_lines; yi++) {
+ for (int xi = 0; xi < x; xi++) {
+ /* translate point */
+ float xt = xi - translate_x;
+ float yt = yi - translate_y;
+
+ /* rotate point with center ref */
+ float xr = c * xt + s * yt;
+ float yr = -s * xt + c * yt;
+
+ /* scale point with center ref */
+ xt = xr / scale_x;
+ yt = yr / scale_y;
+
+ /* undo reference center point */
+ xt += (x / 2.0f);
+ yt += (y / 2.0f);
+
+ /* interpolate */
+ switch (interpolation) {
+ case 0:
+ nearest_interpolation(ibuf1, out, xt, yt, xi, yi);
+ break;
+ case 1:
+ bilinear_interpolation(ibuf1, out, xt, yt, xi, yi);
+ break;
+ case 2:
+ bicubic_interpolation(ibuf1, out, xt, yt, xi, yi);
+ break;
+ }
+ }
+ }
+}
+
+static void do_transform_effect(const SeqRenderData *context,
+ Sequence *seq,
+ float UNUSED(cfra),
+ float UNUSED(facf0),
+ float UNUSED(facf1),
+ ImBuf *ibuf1,
+ ImBuf *UNUSED(ibuf2),
+ ImBuf *UNUSED(ibuf3),
+ int start_line,
+ int total_lines,
+ ImBuf *out)
+{
+ Scene *scene = context->scene;
+ TransformVars *transform = (TransformVars *)seq->effectdata;
+ float scale_x, scale_y, translate_x, translate_y, rotate_radians;
+
+ /* Scale */
+ if (transform->uniform_scale) {
+ scale_x = scale_y = transform->ScalexIni;
+ }
+ else {
+ scale_x = transform->ScalexIni;
+ scale_y = transform->ScaleyIni;
+ }
+
+ int x = context->rectx;
+ int y = context->recty;
+
+ /* Translate */
+ if (!transform->percent) {
+ float rd_s = (scene->r.size / 100.0f);
+
+ translate_x = transform->xIni * rd_s + (x / 2.0f);
+ translate_y = transform->yIni * rd_s + (y / 2.0f);
+ }
+ else {
+ translate_x = x * (transform->xIni / 100.0f) + (x / 2.0f);
+ translate_y = y * (transform->yIni / 100.0f) + (y / 2.0f);
+ }
+
+ /* Rotate */
+ rotate_radians = DEG2RADF(transform->rotIni);
+
+ transform_image(x,
+ y,
+ start_line,
+ total_lines,
+ ibuf1,
+ out,
+ scale_x,
+ scale_y,
+ translate_x,
+ translate_y,
+ rotate_radians,
+ transform->interpolation);
+}
+
+/*********************** Glow *************************/
+
+static void RVBlurBitmap2_float(float *map, int width, int height, float blur, int quality)
+{
+ /* Much better than the previous blur!
+ * We do the blurring in two passes which is a whole lot faster.
+ * I changed the math around to implement an actual Gaussian distribution.
+ *
+ * Watch out though, it tends to misbehave with large blur values on
+ * a small bitmap. Avoid avoid! */
+
+ float *temp = NULL, *swap;
+ float *filter = NULL;
+ int x, y, i, fx, fy;
+ int index, ix, halfWidth;
+ float fval, k, curColor[4], curColor2[4], weight = 0;
+
+ /* If we're not really blurring, bail out */
+ if (blur <= 0) {
+ return;
+ }
+
+ /* Allocate memory for the tempmap and the blur filter matrix */
+ temp = MEM_mallocN(sizeof(float[4]) * width * height, "blurbitmaptemp");
+ if (!temp) {
+ return;
+ }
+
+ /* Allocate memory for the filter elements */
+ halfWidth = ((quality + 1) * blur);
+ filter = (float *)MEM_mallocN(sizeof(float) * halfWidth * 2, "blurbitmapfilter");
+ if (!filter) {
+ MEM_freeN(temp);
+ return;
+ }
+
+ /* Apparently we're calculating a bell curve based on the standard deviation (or radius)
+ * This code is based on an example posted to comp.graphics.algorithms by
+ * Blancmange <bmange@airdmhor.gen.nz>
+ */
+
+ k = -1.0f / (2.0f * (float)M_PI * blur * blur);
+
+ for (ix = 0; ix < halfWidth; ix++) {
+ weight = (float)exp(k * (ix * ix));
+ filter[halfWidth - ix] = weight;
+ filter[halfWidth + ix] = weight;
+ }
+ filter[0] = weight;
+
+ /* Normalize the array */
+ fval = 0;
+ for (ix = 0; ix < halfWidth * 2; ix++) {
+ fval += filter[ix];
+ }
+
+ for (ix = 0; ix < halfWidth * 2; ix++) {
+ filter[ix] /= fval;
+ }
+
+ /* Blur the rows */
+ for (y = 0; y < height; y++) {
+ /* Do the left & right strips */
+ for (x = 0; x < halfWidth; x++) {
+ fx = 0;
+ zero_v4(curColor);
+ zero_v4(curColor2);
+
+ for (i = x - halfWidth; i < x + halfWidth; i++) {
+ if ((i >= 0) && (i < width)) {
+ index = (i + y * width) * 4;
+ madd_v4_v4fl(curColor, map + index, filter[fx]);
+
+ index = (width - 1 - i + y * width) * 4;
+ madd_v4_v4fl(curColor2, map + index, filter[fx]);
+ }
+ fx++;
+ }
+ index = (x + y * width) * 4;
+ copy_v4_v4(temp + index, curColor);
+
+ index = (width - 1 - x + y * width) * 4;
+ copy_v4_v4(temp + index, curColor2);
+ }
+
+ /* Do the main body */
+ for (x = halfWidth; x < width - halfWidth; x++) {
+ fx = 0;
+ zero_v4(curColor);
+ for (i = x - halfWidth; i < x + halfWidth; i++) {
+ index = (i + y * width) * 4;
+ madd_v4_v4fl(curColor, map + index, filter[fx]);
+ fx++;
+ }
+ index = (x + y * width) * 4;
+ copy_v4_v4(temp + index, curColor);
+ }
+ }
+
+ /* Swap buffers */
+ swap = temp;
+ temp = map;
+ map = swap;
+
+ /* Blur the columns */
+ for (x = 0; x < width; x++) {
+ /* Do the top & bottom strips */
+ for (y = 0; y < halfWidth; y++) {
+ fy = 0;
+ zero_v4(curColor);
+ zero_v4(curColor2);
+ for (i = y - halfWidth; i < y + halfWidth; i++) {
+ if ((i >= 0) && (i < height)) {
+ /* Bottom */
+ index = (x + i * width) * 4;
+ madd_v4_v4fl(curColor, map + index, filter[fy]);
+
+ /* Top */
+ index = (x + (height - 1 - i) * width) * 4;
+ madd_v4_v4fl(curColor2, map + index, filter[fy]);
+ }
+ fy++;
+ }
+ index = (x + y * width) * 4;
+ copy_v4_v4(temp + index, curColor);
+
+ index = (x + (height - 1 - y) * width) * 4;
+ copy_v4_v4(temp + index, curColor2);
+ }
+
+ /* Do the main body */
+ for (y = halfWidth; y < height - halfWidth; y++) {
+ fy = 0;
+ zero_v4(curColor);
+ for (i = y - halfWidth; i < y + halfWidth; i++) {
+ index = (x + i * width) * 4;
+ madd_v4_v4fl(curColor, map + index, filter[fy]);
+ fy++;
+ }
+ index = (x + y * width) * 4;
+ copy_v4_v4(temp + index, curColor);
+ }
+ }
+
+ /* Swap buffers */
+ swap = temp;
+ temp = map; /* map = swap; */ /* UNUSED */
+
+ /* Tidy up */
+ MEM_freeN(filter);
+ MEM_freeN(temp);
+}
+
+static void RVAddBitmaps_float(float *a, float *b, float *c, int width, int height)
+{
+ int x, y, index;
+
+ for (y = 0; y < height; y++) {
+ for (x = 0; x < width; x++) {
+ index = (x + y * width) * 4;
+ c[index + GlowR] = min_ff(1.0f, a[index + GlowR] + b[index + GlowR]);
+ c[index + GlowG] = min_ff(1.0f, a[index + GlowG] + b[index + GlowG]);
+ c[index + GlowB] = min_ff(1.0f, a[index + GlowB] + b[index + GlowB]);
+ c[index + GlowA] = min_ff(1.0f, a[index + GlowA] + b[index + GlowA]);
+ }
+ }
+}
+
+static void RVIsolateHighlights_float(
+ const float *in, float *out, int width, int height, float threshold, float boost, float clamp)
+{
+ int x, y, index;
+ float intensity;
+
+ for (y = 0; y < height; y++) {
+ for (x = 0; x < width; x++) {
+ index = (x + y * width) * 4;
+
+ /* Isolate the intensity */
+ intensity = (in[index + GlowR] + in[index + GlowG] + in[index + GlowB] - threshold);
+ if (intensity > 0) {
+ out[index + GlowR] = min_ff(clamp, (in[index + GlowR] * boost * intensity));
+ out[index + GlowG] = min_ff(clamp, (in[index + GlowG] * boost * intensity));
+ out[index + GlowB] = min_ff(clamp, (in[index + GlowB] * boost * intensity));
+ out[index + GlowA] = min_ff(clamp, (in[index + GlowA] * boost * intensity));
+ }
+ else {
+ out[index + GlowR] = 0;
+ out[index + GlowG] = 0;
+ out[index + GlowB] = 0;
+ out[index + GlowA] = 0;
+ }
+ }
+ }
+}
+
+static void init_glow_effect(Sequence *seq)
+{
+ GlowVars *glow;
+
+ if (seq->effectdata) {
+ MEM_freeN(seq->effectdata);
+ }
+
+ seq->effectdata = MEM_callocN(sizeof(GlowVars), "glowvars");
+
+ glow = (GlowVars *)seq->effectdata;
+ glow->fMini = 0.25;
+ glow->fClamp = 1.0;
+ glow->fBoost = 0.5;
+ glow->dDist = 3.0;
+ glow->dQuality = 3;
+ glow->bNoComp = 0;
+}
+
+static int num_inputs_glow(void)
+{
+ return 1;
+}
+
+static void free_glow_effect(Sequence *seq, const bool UNUSED(do_id_user))
+{
+ if (seq->effectdata) {
+ MEM_freeN(seq->effectdata);
+ }
+
+ seq->effectdata = NULL;
+}
+
+static void copy_glow_effect(Sequence *dst, Sequence *src, const int UNUSED(flag))
+{
+ dst->effectdata = MEM_dupallocN(src->effectdata);
+}
+
+static void do_glow_effect_byte(Sequence *seq,
+ int render_size,
+ float facf0,
+ float UNUSED(facf1),
+ int x,
+ int y,
+ unsigned char *rect1,
+ unsigned char *UNUSED(rect2),
+ unsigned char *out)
+{
+ float *outbuf, *inbuf;
+ GlowVars *glow = (GlowVars *)seq->effectdata;
+
+ inbuf = MEM_mallocN(sizeof(float[4]) * x * y, "glow effect input");
+ outbuf = MEM_mallocN(sizeof(float[4]) * x * y, "glow effect output");
+
+ IMB_buffer_float_from_byte(inbuf, rect1, IB_PROFILE_SRGB, IB_PROFILE_SRGB, false, x, y, x, x);
+ IMB_buffer_float_premultiply(inbuf, x, y);
+
+ RVIsolateHighlights_float(
+ inbuf, outbuf, x, y, glow->fMini * 3.0f, glow->fBoost * facf0, glow->fClamp);
+ RVBlurBitmap2_float(outbuf, x, y, glow->dDist * (render_size / 100.0f), glow->dQuality);
+ if (!glow->bNoComp) {
+ RVAddBitmaps_float(inbuf, outbuf, outbuf, x, y);
+ }
+
+ IMB_buffer_float_unpremultiply(outbuf, x, y);
+ IMB_buffer_byte_from_float(
+ out, outbuf, 4, 0.0f, IB_PROFILE_SRGB, IB_PROFILE_SRGB, false, x, y, x, x);
+
+ MEM_freeN(inbuf);
+ MEM_freeN(outbuf);
+}
+
+static void do_glow_effect_float(Sequence *seq,
+ int render_size,
+ float facf0,
+ float UNUSED(facf1),
+ int x,
+ int y,
+ float *rect1,
+ float *UNUSED(rect2),
+ float *out)
+{
+ float *outbuf = out;
+ float *inbuf = rect1;
+ GlowVars *glow = (GlowVars *)seq->effectdata;
+
+ RVIsolateHighlights_float(
+ inbuf, outbuf, x, y, glow->fMini * 3.0f, glow->fBoost * facf0, glow->fClamp);
+ RVBlurBitmap2_float(outbuf, x, y, glow->dDist * (render_size / 100.0f), glow->dQuality);
+ if (!glow->bNoComp) {
+ RVAddBitmaps_float(inbuf, outbuf, outbuf, x, y);
+ }
+}
+
+static ImBuf *do_glow_effect(const SeqRenderData *context,
+ Sequence *seq,
+ float UNUSED(cfra),
+ float facf0,
+ float facf1,
+ ImBuf *ibuf1,
+ ImBuf *ibuf2,
+ ImBuf *ibuf3)
+{
+ ImBuf *out = prepare_effect_imbufs(context, ibuf1, ibuf2, ibuf3);
+
+ int render_size = 100 * context->rectx / context->scene->r.xsch;
+
+ if (out->rect_float) {
+ do_glow_effect_float(seq,
+ render_size,
+ facf0,
+ facf1,
+ context->rectx,
+ context->recty,
+ ibuf1->rect_float,
+ NULL,
+ out->rect_float);
+ }
+ else {
+ do_glow_effect_byte(seq,
+ render_size,
+ facf0,
+ facf1,
+ context->rectx,
+ context->recty,
+ (unsigned char *)ibuf1->rect,
+ NULL,
+ (unsigned char *)out->rect);
+ }
+
+ return out;
+}
+
+/*********************** Solid color *************************/
+
+static void init_solid_color(Sequence *seq)
+{
+ SolidColorVars *cv;
+
+ if (seq->effectdata) {
+ MEM_freeN(seq->effectdata);
+ }
+
+ seq->effectdata = MEM_callocN(sizeof(SolidColorVars), "solidcolor");
+
+ cv = (SolidColorVars *)seq->effectdata;
+ cv->col[0] = cv->col[1] = cv->col[2] = 0.5;
+}
+
+static int num_inputs_color(void)
+{
+ return 0;
+}
+
+static void free_solid_color(Sequence *seq, const bool UNUSED(do_id_user))
+{
+ if (seq->effectdata) {
+ MEM_freeN(seq->effectdata);
+ }
+
+ seq->effectdata = NULL;
+}
+
+static void copy_solid_color(Sequence *dst, Sequence *src, const int UNUSED(flag))
+{
+ dst->effectdata = MEM_dupallocN(src->effectdata);
+}
+
+static int early_out_color(Sequence *UNUSED(seq), float UNUSED(facf0), float UNUSED(facf1))
+{
+ return EARLY_NO_INPUT;
+}
+
+static ImBuf *do_solid_color(const SeqRenderData *context,
+ Sequence *seq,
+ float UNUSED(cfra),
+ float facf0,
+ float facf1,
+ ImBuf *ibuf1,
+ ImBuf *ibuf2,
+ ImBuf *ibuf3)
+{
+ ImBuf *out = prepare_effect_imbufs(context, ibuf1, ibuf2, ibuf3);
+
+ SolidColorVars *cv = (SolidColorVars *)seq->effectdata;
+
+ unsigned char *rect;
+ float *rect_float;
+ int x; /*= context->rectx;*/ /*UNUSED*/
+ int y; /*= context->recty;*/ /*UNUSED*/
+
+ if (out->rect) {
+ unsigned char col0[3];
+ unsigned char col1[3];
+
+ col0[0] = facf0 * cv->col[0] * 255;
+ col0[1] = facf0 * cv->col[1] * 255;
+ col0[2] = facf0 * cv->col[2] * 255;
+
+ col1[0] = facf1 * cv->col[0] * 255;
+ col1[1] = facf1 * cv->col[1] * 255;
+ col1[2] = facf1 * cv->col[2] * 255;
+
+ rect = (unsigned char *)out->rect;
+
+ for (y = 0; y < out->y; y++) {
+ for (x = 0; x < out->x; x++, rect += 4) {
+ rect[0] = col0[0];
+ rect[1] = col0[1];
+ rect[2] = col0[2];
+ rect[3] = 255;
+ }
+ y++;
+ if (y < out->y) {
+ for (x = 0; x < out->x; x++, rect += 4) {
+ rect[0] = col1[0];
+ rect[1] = col1[1];
+ rect[2] = col1[2];
+ rect[3] = 255;
+ }
+ }
+ }
+ }
+ else if (out->rect_float) {
+ float col0[3];
+ float col1[3];
+
+ col0[0] = facf0 * cv->col[0];
+ col0[1] = facf0 * cv->col[1];
+ col0[2] = facf0 * cv->col[2];
+
+ col1[0] = facf1 * cv->col[0];
+ col1[1] = facf1 * cv->col[1];
+ col1[2] = facf1 * cv->col[2];
+
+ rect_float = out->rect_float;
+
+ for (y = 0; y < out->y; y++) {
+ for (x = 0; x < out->x; x++, rect_float += 4) {
+ rect_float[0] = col0[0];
+ rect_float[1] = col0[1];
+ rect_float[2] = col0[2];
+ rect_float[3] = 1.0;
+ }
+ y++;
+ if (y < out->y) {
+ for (x = 0; x < out->x; x++, rect_float += 4) {
+ rect_float[0] = col1[0];
+ rect_float[1] = col1[1];
+ rect_float[2] = col1[2];
+ rect_float[3] = 1.0;
+ }
+ }
+ }
+ }
+ return out;
+}
+
+/*********************** Mulitcam *************************/
+
+/* no effect inputs for multicam, we use give_ibuf_seq */
+static int num_inputs_multicam(void)
+{
+ return 0;
+}
+
+static int early_out_multicam(Sequence *UNUSED(seq), float UNUSED(facf0), float UNUSED(facf1))
+{
+ return EARLY_NO_INPUT;
+}
+
+static ImBuf *do_multicam(const SeqRenderData *context,
+ Sequence *seq,
+ float cfra,
+ float UNUSED(facf0),
+ float UNUSED(facf1),
+ ImBuf *UNUSED(ibuf1),
+ ImBuf *UNUSED(ibuf2),
+ ImBuf *UNUSED(ibuf3))
+{
+ ImBuf *out;
+ Editing *ed;
+ ListBase *seqbasep;
+
+ if (seq->multicam_source == 0 || seq->multicam_source >= seq->machine) {
+ return NULL;
+ }
+
+ ed = context->scene->ed;
+ if (!ed) {
+ return NULL;
+ }
+ seqbasep = BKE_sequence_seqbase(&ed->seqbase, seq);
+ if (!seqbasep) {
+ return NULL;
+ }
+
+ out = BKE_sequencer_give_ibuf_seqbase(context, cfra, seq->multicam_source, seqbasep);
+
+ return out;
+}
+
+/*********************** Adjustment *************************/
+
+/* no effect inputs for adjustment, we use give_ibuf_seq */
+static int num_inputs_adjustment(void)
+{
+ return 0;
+}
+
+static int early_out_adjustment(Sequence *UNUSED(seq), float UNUSED(facf0), float UNUSED(facf1))
+{
+ return EARLY_NO_INPUT;
+}
+
+static ImBuf *do_adjustment_impl(const SeqRenderData *context, Sequence *seq, float cfra)
+{
+ Editing *ed;
+ ListBase *seqbasep;
+ ImBuf *i = NULL;
+
+ ed = context->scene->ed;
+
+ seqbasep = BKE_sequence_seqbase(&ed->seqbase, seq);
+
+ if (seq->machine > 1) {
+ i = BKE_sequencer_give_ibuf_seqbase(context, cfra, seq->machine - 1, seqbasep);
+ }
+
+ /* found nothing? so let's work the way up the metastrip stack, so
+ * that it is possible to group a bunch of adjustment strips into
+ * a metastrip and have that work on everything below the metastrip
+ */
+
+ if (!i) {
+ Sequence *meta;
+
+ meta = BKE_sequence_metastrip(&ed->seqbase, NULL, seq);
+
+ if (meta) {
+ i = do_adjustment_impl(context, meta, cfra);
+ }
+ }
+
+ return i;
+}
+
+static ImBuf *do_adjustment(const SeqRenderData *context,
+ Sequence *seq,
+ float cfra,
+ float UNUSED(facf0),
+ float UNUSED(facf1),
+ ImBuf *UNUSED(ibuf1),
+ ImBuf *UNUSED(ibuf2),
+ ImBuf *UNUSED(ibuf3))
+{
+ ImBuf *out;
+ Editing *ed;
+
+ ed = context->scene->ed;
+
+ if (!ed) {
+ return NULL;
+ }
+
+ out = do_adjustment_impl(context, seq, cfra);
+
+ return out;
+}
+
+/*********************** Speed *************************/
+
+static void init_speed_effect(Sequence *seq)
+{
+ SpeedControlVars *v;
+
+ if (seq->effectdata) {
+ MEM_freeN(seq->effectdata);
+ }
+
+ seq->effectdata = MEM_callocN(sizeof(SpeedControlVars), "speedcontrolvars");
+
+ v = (SpeedControlVars *)seq->effectdata;
+ v->globalSpeed = 1.0;
+ v->frameMap = NULL;
+ v->flags |= SEQ_SPEED_INTEGRATE; /* should be default behavior */
+ v->length = 0;
+}
+
+static void load_speed_effect(Sequence *seq)
+{
+ SpeedControlVars *v = (SpeedControlVars *)seq->effectdata;
+
+ v->frameMap = NULL;
+ v->length = 0;
+}
+
+static int num_inputs_speed(void)
+{
+ return 1;
+}
+
+static void free_speed_effect(Sequence *seq, const bool UNUSED(do_id_user))
+{
+ SpeedControlVars *v = (SpeedControlVars *)seq->effectdata;
+ if (v->frameMap) {
+ MEM_freeN(v->frameMap);
+ }
+ if (seq->effectdata) {
+ MEM_freeN(seq->effectdata);
+ }
+ seq->effectdata = NULL;
+}
+
+static void copy_speed_effect(Sequence *dst, Sequence *src, const int UNUSED(flag))
+{
+ SpeedControlVars *v;
+ dst->effectdata = MEM_dupallocN(src->effectdata);
+ v = (SpeedControlVars *)dst->effectdata;
+ v->frameMap = NULL;
+ v->length = 0;
+}
+
+static int early_out_speed(Sequence *UNUSED(seq), float UNUSED(facf0), float UNUSED(facf1))
+{
+ return EARLY_DO_EFFECT;
+}
+
+static void store_icu_yrange_speed(Sequence *seq, short UNUSED(adrcode), float *ymin, float *ymax)
+{
+ SpeedControlVars *v = (SpeedControlVars *)seq->effectdata;
+
+ /* if not already done, load / initialize data */
+ BKE_sequence_get_effect(seq);
+
+ if ((v->flags & SEQ_SPEED_INTEGRATE) != 0) {
+ *ymin = -100.0;
+ *ymax = 100.0;
+ }
+ else {
+ if (v->flags & SEQ_SPEED_COMPRESS_IPO_Y) {
+ *ymin = 0.0;
+ *ymax = 1.0;
+ }
+ else {
+ *ymin = 0.0;
+ *ymax = seq->len;
+ }
+ }
+}
+
+void BKE_sequence_effect_speed_rebuild_map(Scene *scene, Sequence *seq, bool force)
+{
+ int cfra;
+ float fallback_fac = 1.0f;
+ SpeedControlVars *v = (SpeedControlVars *)seq->effectdata;
+ FCurve *fcu = NULL;
+ int flags = v->flags;
+
+ /* if not already done, load / initialize data */
+ BKE_sequence_get_effect(seq);
+
+ if ((force == false) && (seq->len == v->length) && (v->frameMap != NULL)) {
+ return;
+ }
+ if ((seq->seq1 == NULL) || (seq->len < 1)) {
+ /* make coverity happy and check for (CID 598) input strip ... */
+ return;
+ }
+
+ /* XXX - new in 2.5x. should we use the animation system this way?
+ * The fcurve is needed because many frames need evaluating at once - campbell */
+ fcu = id_data_find_fcurve(&scene->id, seq, &RNA_Sequence, "speed_factor", 0, NULL);
+ if (!v->frameMap || v->length != seq->len) {
+ if (v->frameMap) {
+ MEM_freeN(v->frameMap);
+ }
+
+ v->length = seq->len;
+
+ v->frameMap = MEM_callocN(sizeof(float) * v->length, "speedcontrol frameMap");
+ }
+
+ fallback_fac = 1.0;
+
+ if (seq->flag & SEQ_USE_EFFECT_DEFAULT_FADE) {
+ if ((seq->seq1->enddisp != seq->seq1->start) && (seq->seq1->len != 0)) {
+ fallback_fac = (float)seq->seq1->len / (float)(seq->seq1->enddisp - seq->seq1->start);
+ flags = SEQ_SPEED_INTEGRATE;
+ fcu = NULL;
+ }
+ }
+ else {
+ /* if there is no fcurve, use value as simple multiplier */
+ if (!fcu) {
+ fallback_fac = seq->speed_fader; /* same as speed_factor in rna*/
+ }
+ }
+
+ if (flags & SEQ_SPEED_INTEGRATE) {
+ float cursor = 0;
+ float facf;
+
+ v->frameMap[0] = 0;
+ v->lastValidFrame = 0;
+
+ for (cfra = 1; cfra < v->length; cfra++) {
+ if (fcu) {
+ facf = evaluate_fcurve(fcu, seq->startdisp + cfra);
+ }
+ else {
+ facf = fallback_fac;
+ }
+ facf *= v->globalSpeed;
+
+ cursor += facf;
+
+ if (cursor >= seq->seq1->len) {
+ v->frameMap[cfra] = seq->seq1->len - 1;
+ }
+ else {
+ v->frameMap[cfra] = cursor;
+ v->lastValidFrame = cfra;
+ }
+ }
+ }
+ else {
+ float facf;
+
+ v->lastValidFrame = 0;
+ for (cfra = 0; cfra < v->length; cfra++) {
+
+ if (fcu) {
+ facf = evaluate_fcurve(fcu, seq->startdisp + cfra);
+ }
+ else {
+ facf = fallback_fac;
+ }
+
+ if (flags & SEQ_SPEED_COMPRESS_IPO_Y) {
+ facf *= seq->seq1->len;
+ }
+ facf *= v->globalSpeed;
+
+ if (facf >= seq->seq1->len) {
+ facf = seq->seq1->len - 1;
+ }
+ else {
+ v->lastValidFrame = cfra;
+ }
+ v->frameMap[cfra] = facf;
+ }
+ }
+}
+
+/* Override cfra when rendering speed effect input. */
+float BKE_sequencer_speed_effect_target_frame_get(const SeqRenderData *context,
+ Sequence *seq,
+ float cfra,
+ int input)
+{
+ int nr = BKE_sequencer_give_stripelem_index(seq, cfra);
+ SpeedControlVars *s = (SpeedControlVars *)seq->effectdata;
+ BKE_sequence_effect_speed_rebuild_map(context->scene, seq, false);
+
+ /* No interpolation. */
+ if ((s->flags & SEQ_SPEED_USE_INTERPOLATION) == 0) {
+ return seq->start + s->frameMap[nr];
+ }
+
+ /* We need to provide current and next image for interpolation. */
+ if (input == 0) { /* Current frame. */
+ return floor(seq->start + s->frameMap[nr]);
+ }
+ /* Next frame. */
+ return ceil(seq->start + s->frameMap[nr]);
+}
+
+static float speed_effect_interpolation_ratio_get(SpeedControlVars *s, Sequence *seq, float cfra)
+{
+ int nr = BKE_sequencer_give_stripelem_index(seq, cfra);
+ return s->frameMap[nr] - floor(s->frameMap[nr]);
+}
+
+static ImBuf *do_speed_effect(const SeqRenderData *context,
+ Sequence *seq,
+ float cfra,
+ float facf0,
+ float facf1,
+ ImBuf *ibuf1,
+ ImBuf *ibuf2,
+ ImBuf *ibuf3)
+{
+ SpeedControlVars *s = (SpeedControlVars *)seq->effectdata;
+ struct SeqEffectHandle cross_effect = get_sequence_effect_impl(SEQ_TYPE_CROSS);
+ ImBuf *out;
+
+ if (s->flags & SEQ_SPEED_USE_INTERPOLATION) {
+ out = prepare_effect_imbufs(context, ibuf1, ibuf2, ibuf3);
+ facf0 = facf1 = speed_effect_interpolation_ratio_get(s, seq, cfra);
+ /* Current frame is ibuf1, next frame is ibuf2. */
+ out = BKE_sequencer_effect_execute_threaded(
+ &cross_effect, context, NULL, cfra, facf0, facf1, ibuf1, ibuf2, ibuf3);
+ return out;
+ }
+
+ /* No interpolation. */
+ return IMB_dupImBuf(ibuf1);
+}
+
+/*********************** overdrop *************************/
+
+static void do_overdrop_effect(const SeqRenderData *context,
+ Sequence *UNUSED(seq),
+ float UNUSED(cfra),
+ float facf0,
+ float facf1,
+ ImBuf *ibuf1,
+ ImBuf *ibuf2,
+ ImBuf *UNUSED(ibuf3),
+ int start_line,
+ int total_lines,
+ ImBuf *out)
+{
+ int x = context->rectx;
+ int y = total_lines;
+
+ if (out->rect_float) {
+ float *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+
+ slice_get_float_buffers(
+ context, ibuf1, ibuf2, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+
+ do_drop_effect_float(facf0, facf1, x, y, rect1, rect2, rect_out);
+ do_alphaover_effect_float(facf0, facf1, x, y, rect1, rect2, rect_out);
+ }
+ else {
+ unsigned char *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+
+ slice_get_byte_buffers(
+ context, ibuf1, ibuf2, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+
+ do_drop_effect_byte(facf0, facf1, x, y, rect1, rect2, rect_out);
+ do_alphaover_effect_byte(facf0, facf1, x, y, rect1, rect2, rect_out);
+ }
+}
+
+/*********************** Gaussian Blur *************************/
+
+/* NOTE: This gaussian blur implementation accumulates values in the square
+ * kernel rather that doing X direction and then Y direction because of the
+ * lack of using multiple-staged filters.
+ *
+ * Once we can we'll implement a way to apply filter as multiple stages we
+ * can optimize hell of a lot in here.
+ */
+
+static void init_gaussian_blur_effect(Sequence *seq)
+{
+ if (seq->effectdata) {
+ MEM_freeN(seq->effectdata);
+ }
+
+ seq->effectdata = MEM_callocN(sizeof(WipeVars), "wipevars");
+}
+
+static int num_inputs_gaussian_blur(void)
+{
+ return 1;
+}
+
+static void free_gaussian_blur_effect(Sequence *seq, const bool UNUSED(do_id_user))
+{
+ if (seq->effectdata) {
+ MEM_freeN(seq->effectdata);
+ }
+
+ seq->effectdata = NULL;
+}
+
+static void copy_gaussian_blur_effect(Sequence *dst, Sequence *src, const int UNUSED(flag))
+{
+ dst->effectdata = MEM_dupallocN(src->effectdata);
+}
+
+static int early_out_gaussian_blur(Sequence *seq, float UNUSED(facf0), float UNUSED(facf1))
+{
+ GaussianBlurVars *data = seq->effectdata;
+ if (data->size_x == 0.0f && data->size_y == 0) {
+ return EARLY_USE_INPUT_1;
+ }
+ return EARLY_DO_EFFECT;
+}
+
+/* TODO(sergey): De-duplicate with compositor. */
+static float *make_gaussian_blur_kernel(float rad, int size)
+{
+ float *gausstab, sum, val;
+ float fac;
+ int i, n;
+
+ n = 2 * size + 1;
+
+ gausstab = (float *)MEM_mallocN(sizeof(float) * n, __func__);
+
+ sum = 0.0f;
+ fac = (rad > 0.0f ? 1.0f / rad : 0.0f);
+ for (i = -size; i <= size; i++) {
+ val = RE_filter_value(R_FILTER_GAUSS, (float)i * fac);
+ sum += val;
+ gausstab[i + size] = val;
+ }
+
+ sum = 1.0f / sum;
+ for (i = 0; i < n; i++) {
+ gausstab[i] *= sum;
+ }
+
+ return gausstab;
+}
+
+static void do_gaussian_blur_effect_byte_x(Sequence *seq,
+ int start_line,
+ int x,
+ int y,
+ int frame_width,
+ int UNUSED(frame_height),
+ const unsigned char *rect,
+ unsigned char *out)
+{
+#define INDEX(_x, _y) (((_y) * (x) + (_x)) * 4)
+ GaussianBlurVars *data = seq->effectdata;
+ const int size_x = (int)(data->size_x + 0.5f);
+ int i, j;
+
+ /* Make gaussian weight table. */
+ float *gausstab_x;
+ gausstab_x = make_gaussian_blur_kernel(data->size_x, size_x);
+
+ for (i = 0; i < y; i++) {
+ for (j = 0; j < x; j++) {
+ int out_index = INDEX(j, i);
+ float accum[4] = {0.0f, 0.0f, 0.0f, 0.0f};
+ float accum_weight = 0.0f;
+
+ for (int current_x = j - size_x; current_x <= j + size_x; current_x++) {
+ if (current_x < 0 || current_x >= frame_width) {
+ /* Out of bounds. */
+ continue;
+ }
+ int index = INDEX(current_x, i + start_line);
+ float weight = gausstab_x[current_x - j + size_x];
+ accum[0] += rect[index] * weight;
+ accum[1] += rect[index + 1] * weight;
+ accum[2] += rect[index + 2] * weight;
+ accum[3] += rect[index + 3] * weight;
+ accum_weight += weight;
+ }
+
+ float inv_accum_weight = 1.0f / accum_weight;
+ out[out_index + 0] = accum[0] * inv_accum_weight;
+ out[out_index + 1] = accum[1] * inv_accum_weight;
+ out[out_index + 2] = accum[2] * inv_accum_weight;
+ out[out_index + 3] = accum[3] * inv_accum_weight;
+ }
+ }
+
+ MEM_freeN(gausstab_x);
+#undef INDEX
+}
+
+static void do_gaussian_blur_effect_byte_y(Sequence *seq,
+ int start_line,
+ int x,
+ int y,
+ int UNUSED(frame_width),
+ int frame_height,
+ const unsigned char *rect,
+ unsigned char *out)
+{
+#define INDEX(_x, _y) (((_y) * (x) + (_x)) * 4)
+ GaussianBlurVars *data = seq->effectdata;
+ const int size_y = (int)(data->size_y + 0.5f);
+ int i, j;
+
+ /* Make gaussian weight table. */
+ float *gausstab_y;
+ gausstab_y = make_gaussian_blur_kernel(data->size_y, size_y);
+
+ for (i = 0; i < y; i++) {
+ for (j = 0; j < x; j++) {
+ int out_index = INDEX(j, i);
+ float accum[4] = {0.0f, 0.0f, 0.0f, 0.0f};
+ float accum_weight = 0.0f;
+ for (int current_y = i - size_y; current_y <= i + size_y; current_y++) {
+ if (current_y < -start_line || current_y + start_line >= frame_height) {
+ /* Out of bounds. */
+ continue;
+ }
+ int index = INDEX(j, current_y + start_line);
+ float weight = gausstab_y[current_y - i + size_y];
+ accum[0] += rect[index] * weight;
+ accum[1] += rect[index + 1] * weight;
+ accum[2] += rect[index + 2] * weight;
+ accum[3] += rect[index + 3] * weight;
+ accum_weight += weight;
+ }
+ float inv_accum_weight = 1.0f / accum_weight;
+ out[out_index + 0] = accum[0] * inv_accum_weight;
+ out[out_index + 1] = accum[1] * inv_accum_weight;
+ out[out_index + 2] = accum[2] * inv_accum_weight;
+ out[out_index + 3] = accum[3] * inv_accum_weight;
+ }
+ }
+
+ MEM_freeN(gausstab_y);
+#undef INDEX
+}
+
+static void do_gaussian_blur_effect_float_x(Sequence *seq,
+ int start_line,
+ int x,
+ int y,
+ int frame_width,
+ int UNUSED(frame_height),
+ float *rect,
+ float *out)
+{
+#define INDEX(_x, _y) (((_y) * (x) + (_x)) * 4)
+ GaussianBlurVars *data = seq->effectdata;
+ const int size_x = (int)(data->size_x + 0.5f);
+ int i, j;
+
+ /* Make gaussian weight table. */
+ float *gausstab_x;
+ gausstab_x = make_gaussian_blur_kernel(data->size_x, size_x);
+
+ for (i = 0; i < y; i++) {
+ for (j = 0; j < x; j++) {
+ int out_index = INDEX(j, i);
+ float accum[4] = {0.0f, 0.0f, 0.0f, 0.0f};
+ float accum_weight = 0.0f;
+ for (int current_x = j - size_x; current_x <= j + size_x; current_x++) {
+ if (current_x < 0 || current_x >= frame_width) {
+ /* Out of bounds. */
+ continue;
+ }
+ int index = INDEX(current_x, i + start_line);
+ float weight = gausstab_x[current_x - j + size_x];
+ madd_v4_v4fl(accum, &rect[index], weight);
+ accum_weight += weight;
+ }
+ mul_v4_v4fl(&out[out_index], accum, 1.0f / accum_weight);
+ }
+ }
+
+ MEM_freeN(gausstab_x);
+#undef INDEX
+}
+
+static void do_gaussian_blur_effect_float_y(Sequence *seq,
+ int start_line,
+ int x,
+ int y,
+ int UNUSED(frame_width),
+ int frame_height,
+ float *rect,
+ float *out)
+{
+#define INDEX(_x, _y) (((_y) * (x) + (_x)) * 4)
+ GaussianBlurVars *data = seq->effectdata;
+ const int size_y = (int)(data->size_y + 0.5f);
+ int i, j;
+
+ /* Make gaussian weight table. */
+ float *gausstab_y;
+ gausstab_y = make_gaussian_blur_kernel(data->size_y, size_y);
+
+ for (i = 0; i < y; i++) {
+ for (j = 0; j < x; j++) {
+ int out_index = INDEX(j, i);
+ float accum[4] = {0.0f, 0.0f, 0.0f, 0.0f};
+ float accum_weight = 0.0f;
+ for (int current_y = i - size_y; current_y <= i + size_y; current_y++) {
+ if (current_y < -start_line || current_y + start_line >= frame_height) {
+ /* Out of bounds. */
+ continue;
+ }
+ int index = INDEX(j, current_y + start_line);
+ float weight = gausstab_y[current_y - i + size_y];
+ madd_v4_v4fl(accum, &rect[index], weight);
+ accum_weight += weight;
+ }
+ mul_v4_v4fl(&out[out_index], accum, 1.0f / accum_weight);
+ }
+ }
+
+ MEM_freeN(gausstab_y);
+#undef INDEX
+}
+
+static void do_gaussian_blur_effect_x_cb(const SeqRenderData *context,
+ Sequence *seq,
+ ImBuf *ibuf,
+ int start_line,
+ int total_lines,
+ ImBuf *out)
+{
+ if (out->rect_float) {
+ float *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+
+ slice_get_float_buffers(
+ context, ibuf, NULL, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+
+ do_gaussian_blur_effect_float_x(seq,
+ start_line,
+ context->rectx,
+ total_lines,
+ context->rectx,
+ context->recty,
+ ibuf->rect_float,
+ rect_out);
+ }
+ else {
+ unsigned char *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+
+ slice_get_byte_buffers(
+ context, ibuf, NULL, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+
+ do_gaussian_blur_effect_byte_x(seq,
+ start_line,
+ context->rectx,
+ total_lines,
+ context->rectx,
+ context->recty,
+ (unsigned char *)ibuf->rect,
+ rect_out);
+ }
+}
+
+static void do_gaussian_blur_effect_y_cb(const SeqRenderData *context,
+ Sequence *seq,
+ ImBuf *ibuf,
+ int start_line,
+ int total_lines,
+ ImBuf *out)
+{
+ if (out->rect_float) {
+ float *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+
+ slice_get_float_buffers(
+ context, ibuf, NULL, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+
+ do_gaussian_blur_effect_float_y(seq,
+ start_line,
+ context->rectx,
+ total_lines,
+ context->rectx,
+ context->recty,
+ ibuf->rect_float,
+ rect_out);
+ }
+ else {
+ unsigned char *rect1 = NULL, *rect2 = NULL, *rect_out = NULL;
+
+ slice_get_byte_buffers(
+ context, ibuf, NULL, NULL, out, start_line, &rect1, &rect2, NULL, &rect_out);
+
+ do_gaussian_blur_effect_byte_y(seq,
+ start_line,
+ context->rectx,
+ total_lines,
+ context->rectx,
+ context->recty,
+ (unsigned char *)ibuf->rect,
+ rect_out);
+ }
+}
+
+typedef struct RenderGaussianBlurEffectInitData {
+ const SeqRenderData *context;
+ Sequence *seq;
+ ImBuf *ibuf;
+ ImBuf *out;
+} RenderGaussianBlurEffectInitData;
+
+typedef struct RenderGaussianBlurEffectThread {
+ const SeqRenderData *context;
+ Sequence *seq;
+ ImBuf *ibuf;
+ ImBuf *out;
+ int start_line, tot_line;
+} RenderGaussianBlurEffectThread;
+
+static void render_effect_execute_init_handle(void *handle_v,
+ int start_line,
+ int tot_line,
+ void *init_data_v)
+{
+ RenderGaussianBlurEffectThread *handle = (RenderGaussianBlurEffectThread *)handle_v;
+ RenderGaussianBlurEffectInitData *init_data = (RenderGaussianBlurEffectInitData *)init_data_v;
+
+ handle->context = init_data->context;
+ handle->seq = init_data->seq;
+ handle->ibuf = init_data->ibuf;
+ handle->out = init_data->out;
+
+ handle->start_line = start_line;
+ handle->tot_line = tot_line;
+}
+
+static void *render_effect_execute_do_x_thread(void *thread_data_v)
+{
+ RenderGaussianBlurEffectThread *thread_data = (RenderGaussianBlurEffectThread *)thread_data_v;
+ do_gaussian_blur_effect_x_cb(thread_data->context,
+ thread_data->seq,
+ thread_data->ibuf,
+ thread_data->start_line,
+ thread_data->tot_line,
+ thread_data->out);
+ return NULL;
+}
+
+static void *render_effect_execute_do_y_thread(void *thread_data_v)
+{
+ RenderGaussianBlurEffectThread *thread_data = (RenderGaussianBlurEffectThread *)thread_data_v;
+ do_gaussian_blur_effect_y_cb(thread_data->context,
+ thread_data->seq,
+ thread_data->ibuf,
+ thread_data->start_line,
+ thread_data->tot_line,
+ thread_data->out);
+
+ return NULL;
+}
+
+static ImBuf *do_gaussian_blur_effect(const SeqRenderData *context,
+ Sequence *seq,
+ float UNUSED(cfra),
+ float UNUSED(facf0),
+ float UNUSED(facf1),
+ ImBuf *ibuf1,
+ ImBuf *UNUSED(ibuf2),
+ ImBuf *UNUSED(ibuf3))
+{
+ ImBuf *out = prepare_effect_imbufs(context, ibuf1, NULL, NULL);
+
+ RenderGaussianBlurEffectInitData init_data;
+
+ init_data.context = context;
+ init_data.seq = seq;
+ init_data.ibuf = ibuf1;
+ init_data.out = out;
+
+ IMB_processor_apply_threaded(out->y,
+ sizeof(RenderGaussianBlurEffectThread),
+ &init_data,
+ render_effect_execute_init_handle,
+ render_effect_execute_do_x_thread);
+
+ ibuf1 = out;
+ init_data.ibuf = ibuf1;
+ out = prepare_effect_imbufs(context, ibuf1, NULL, NULL);
+ init_data.out = out;
+
+ IMB_processor_apply_threaded(out->y,
+ sizeof(RenderGaussianBlurEffectThread),
+ &init_data,
+ render_effect_execute_init_handle,
+ render_effect_execute_do_y_thread);
+
+ IMB_freeImBuf(ibuf1);
+
+ return out;
+}
+
+/*********************** text *************************/
+
+static void init_text_effect(Sequence *seq)
+{
+ TextVars *data;
+
+ if (seq->effectdata) {
+ MEM_freeN(seq->effectdata);
+ }
+
+ data = seq->effectdata = MEM_callocN(sizeof(TextVars), "textvars");
+ data->text_font = NULL;
+ data->text_blf_id = -1;
+ data->text_size = 30;
+
+ copy_v4_fl(data->color, 1.0f);
+ data->shadow_color[3] = 1.0f;
+
+ BLI_strncpy(data->text, "Text", sizeof(data->text));
+
+ data->loc[0] = 0.5f;
+ data->align = SEQ_TEXT_ALIGN_X_CENTER;
+ data->align_y = SEQ_TEXT_ALIGN_Y_BOTTOM;
+}
+
+void BKE_sequencer_text_font_unload(TextVars *data, const bool do_id_user)
+{
+ if (data) {
+ /* Unlink the VFont */
+ if (do_id_user && data->text_font != NULL) {
+ id_us_min(&data->text_font->id);
+ data->text_font = NULL;
+ }
+
+ /* Unload the BLF font. */
+ if (data->text_blf_id >= 0) {
+ BLF_unload_id(data->text_blf_id);
+ }
+ }
+}
+
+void BKE_sequencer_text_font_load(TextVars *data, const bool do_id_user)
+{
+ if (data->text_font != NULL) {
+ if (do_id_user) {
+ id_us_plus(&data->text_font->id);
+ }
+
+ char path[FILE_MAX];
+ STRNCPY(path, data->text_font->filepath);
+ BLI_assert(BLI_thread_is_main());
+ BLI_path_abs(path, ID_BLEND_PATH_FROM_GLOBAL(&data->text_font->id));
+
+ data->text_blf_id = BLF_load(path);
+ }
+}
+
+static void free_text_effect(Sequence *seq, const bool do_id_user)
+{
+ TextVars *data = seq->effectdata;
+ BKE_sequencer_text_font_unload(data, do_id_user);
+
+ if (data) {
+ MEM_freeN(data);
+ seq->effectdata = NULL;
+ }
+}
+
+static void load_text_effect(Sequence *seq)
+{
+ TextVars *data = seq->effectdata;
+ BKE_sequencer_text_font_load(data, false);
+}
+
+static void copy_text_effect(Sequence *dst, Sequence *src, const int flag)
+{
+ dst->effectdata = MEM_dupallocN(src->effectdata);
+ TextVars *data = dst->effectdata;
+
+ data->text_blf_id = -1;
+ BKE_sequencer_text_font_load(data, (flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0);
+}
+
+static int num_inputs_text(void)
+{
+ return 0;
+}
+
+static int early_out_text(Sequence *seq, float UNUSED(facf0), float UNUSED(facf1))
+{
+ TextVars *data = seq->effectdata;
+ if (data->text[0] == 0 || data->text_size < 1 ||
+ ((data->color[3] == 0.0f) &&
+ (data->shadow_color[3] == 0.0f || (data->flag & SEQ_TEXT_SHADOW) == 0))) {
+ return EARLY_USE_INPUT_1;
+ }
+ return EARLY_NO_INPUT;
+}
+
+static ImBuf *do_text_effect(const SeqRenderData *context,
+ Sequence *seq,
+ float UNUSED(cfra),
+ float UNUSED(facf0),
+ float UNUSED(facf1),
+ ImBuf *ibuf1,
+ ImBuf *ibuf2,
+ ImBuf *ibuf3)
+{
+ ImBuf *out = prepare_effect_imbufs(context, ibuf1, ibuf2, ibuf3);
+ TextVars *data = seq->effectdata;
+ int width = out->x;
+ int height = out->y;
+ struct ColorManagedDisplay *display;
+ const char *display_device;
+ int font = blf_mono_font_render;
+ int line_height;
+ int y_ofs, x, y;
+ double proxy_size_comp;
+
+ if (data->text_blf_id == SEQ_FONT_NOT_LOADED) {
+ data->text_blf_id = -1;
+
+ if (data->text_font) {
+ data->text_blf_id = BLF_load(data->text_font->filepath);
+ }
+ }
+
+ if (data->text_blf_id >= 0) {
+ font = data->text_blf_id;
+ }
+
+ display_device = context->scene->display_settings.display_device;
+ display = IMB_colormanagement_display_get_named(display_device);
+
+ /* Compensate text size for preview render size. */
+ proxy_size_comp = context->scene->r.size / 100.0;
+ if (context->preview_render_size != SEQ_RENDER_SIZE_SCENE) {
+ proxy_size_comp *= BKE_sequencer_rendersize_to_scale_factor(context->preview_render_size);
+ }
+
+ /* set before return */
+ BLF_size(font, proxy_size_comp * data->text_size, 72);
+
+ BLF_enable(font, BLF_WORD_WRAP);
+
+ /* use max width to enable newlines only */
+ BLF_wordwrap(font, (data->wrap_width != 0.0f) ? data->wrap_width * width : -1);
+
+ BLF_buffer(
+ font, out->rect_float, (unsigned char *)out->rect, width, height, out->channels, display);
+
+ line_height = BLF_height_max(font);
+
+ y_ofs = -BLF_descender(font);
+
+ x = (data->loc[0] * width);
+ y = (data->loc[1] * height) + y_ofs;
+
+ if ((data->align == SEQ_TEXT_ALIGN_X_LEFT) && (data->align_y == SEQ_TEXT_ALIGN_Y_TOP)) {
+ y -= line_height;
+ }
+ else {
+ /* vars for calculating wordwrap */
+ struct {
+ struct ResultBLF info;
+ rctf rect;
+ } wrap;
+
+ BLF_boundbox_ex(font, data->text, sizeof(data->text), &wrap.rect, &wrap.info);
+
+ if (data->align == SEQ_TEXT_ALIGN_X_RIGHT) {
+ x -= BLI_rctf_size_x(&wrap.rect);
+ }
+ else if (data->align == SEQ_TEXT_ALIGN_X_CENTER) {
+ x -= BLI_rctf_size_x(&wrap.rect) / 2;
+ }
+
+ if (data->align_y == SEQ_TEXT_ALIGN_Y_TOP) {
+ y -= line_height;
+ }
+ else if (data->align_y == SEQ_TEXT_ALIGN_Y_BOTTOM) {
+ y += (wrap.info.lines - 1) * line_height;
+ }
+ else if (data->align_y == SEQ_TEXT_ALIGN_Y_CENTER) {
+ y += (((wrap.info.lines - 1) / 2) * line_height) - (line_height / 2);
+ }
+ }
+
+ /* BLF_SHADOW won't work with buffers, instead use cheap shadow trick */
+ if (data->flag & SEQ_TEXT_SHADOW) {
+ int fontx, fonty;
+ fontx = BLF_width_max(font);
+ fonty = line_height;
+ BLF_position(font, x + max_ii(fontx / 25, 1), y + max_ii(fonty / 25, 1), 0.0f);
+ BLF_buffer_col(font, data->shadow_color);
+ BLF_draw_buffer(font, data->text, BLF_DRAW_STR_DUMMY_MAX);
+ }
+
+ BLF_position(font, x, y, 0.0f);
+ BLF_buffer_col(font, data->color);
+ BLF_draw_buffer(font, data->text, BLF_DRAW_STR_DUMMY_MAX);
+
+ BLF_buffer(font, NULL, NULL, 0, 0, 0, NULL);
+
+ BLF_disable(font, BLF_WORD_WRAP);
+
+ return out;
+}
+
+/*********************** sequence effect factory *************************/
+
+static void init_noop(Sequence *UNUSED(seq))
+{
+}
+
+static void load_noop(Sequence *UNUSED(seq))
+{
+}
+
+static void free_noop(Sequence *UNUSED(seq), const bool UNUSED(do_id_user))
+{
+}
+
+static int num_inputs_default(void)
+{
+ return 2;
+}
+
+static void copy_effect_default(Sequence *dst, Sequence *src, const int UNUSED(flag))
+{
+ dst->effectdata = MEM_dupallocN(src->effectdata);
+}
+
+static void free_effect_default(Sequence *seq, const bool UNUSED(do_id_user))
+{
+ if (seq->effectdata) {
+ MEM_freeN(seq->effectdata);
+ }
+
+ seq->effectdata = NULL;
+}
+
+static int early_out_noop(Sequence *UNUSED(seq), float UNUSED(facf0), float UNUSED(facf1))
+{
+ return EARLY_DO_EFFECT;
+}
+
+static int early_out_fade(Sequence *UNUSED(seq), float facf0, float facf1)
+{
+ if (facf0 == 0.0f && facf1 == 0.0f) {
+ return EARLY_USE_INPUT_1;
+ }
+ if (facf0 == 1.0f && facf1 == 1.0f) {
+ return EARLY_USE_INPUT_2;
+ }
+ return EARLY_DO_EFFECT;
+}
+
+static int early_out_mul_input2(Sequence *UNUSED(seq), float facf0, float facf1)
+{
+ if (facf0 == 0.0f && facf1 == 0.0f) {
+ return EARLY_USE_INPUT_1;
+ }
+ return EARLY_DO_EFFECT;
+}
+
+static void store_icu_yrange_noop(Sequence *UNUSED(seq),
+ short UNUSED(adrcode),
+ float *UNUSED(ymin),
+ float *UNUSED(ymax))
+{
+ /* defaults are fine */
+}
+
+static void get_default_fac_noop(Sequence *UNUSED(seq),
+ float UNUSED(cfra),
+ float *facf0,
+ float *facf1)
+{
+ *facf0 = *facf1 = 1.0;
+}
+
+static void get_default_fac_fade(Sequence *seq, float cfra, float *facf0, float *facf1)
+{
+ *facf0 = (float)(cfra - seq->startdisp);
+ *facf1 = (float)(*facf0 + 0.5f);
+ *facf0 /= seq->len;
+ *facf1 /= seq->len;
+}
+
+static struct ImBuf *init_execution(const SeqRenderData *context,
+ ImBuf *ibuf1,
+ ImBuf *ibuf2,
+ ImBuf *ibuf3)
+{
+ ImBuf *out = prepare_effect_imbufs(context, ibuf1, ibuf2, ibuf3);
+
+ return out;
+}
+
+static struct SeqEffectHandle get_sequence_effect_impl(int seq_type)
+{
+ struct SeqEffectHandle rval;
+ int sequence_type = seq_type;
+
+ rval.multithreaded = false;
+ rval.supports_mask = false;
+ rval.init = init_noop;
+ rval.num_inputs = num_inputs_default;
+ rval.load = load_noop;
+ rval.free = free_noop;
+ rval.early_out = early_out_noop;
+ rval.get_default_fac = get_default_fac_noop;
+ rval.store_icu_yrange = store_icu_yrange_noop;
+ rval.execute = NULL;
+ rval.init_execution = init_execution;
+ rval.execute_slice = NULL;
+ rval.copy = NULL;
+
+ switch (sequence_type) {
+ case SEQ_TYPE_CROSS:
+ rval.multithreaded = true;
+ rval.execute_slice = do_cross_effect;
+ rval.early_out = early_out_fade;
+ rval.get_default_fac = get_default_fac_fade;
+ break;
+ case SEQ_TYPE_GAMCROSS:
+ rval.multithreaded = true;
+ rval.init = init_gammacross;
+ rval.load = load_gammacross;
+ rval.free = free_gammacross;
+ rval.early_out = early_out_fade;
+ rval.get_default_fac = get_default_fac_fade;
+ rval.init_execution = gammacross_init_execution;
+ rval.execute_slice = do_gammacross_effect;
+ break;
+ case SEQ_TYPE_ADD:
+ rval.multithreaded = true;
+ rval.execute_slice = do_add_effect;
+ rval.early_out = early_out_mul_input2;
+ break;
+ case SEQ_TYPE_SUB:
+ rval.multithreaded = true;
+ rval.execute_slice = do_sub_effect;
+ rval.early_out = early_out_mul_input2;
+ break;
+ case SEQ_TYPE_MUL:
+ rval.multithreaded = true;
+ rval.execute_slice = do_mul_effect;
+ rval.early_out = early_out_mul_input2;
+ break;
+ case SEQ_TYPE_SCREEN:
+ case SEQ_TYPE_OVERLAY:
+ case SEQ_TYPE_COLOR_BURN:
+ case SEQ_TYPE_LINEAR_BURN:
+ case SEQ_TYPE_DARKEN:
+ case SEQ_TYPE_LIGHTEN:
+ case SEQ_TYPE_DODGE:
+ case SEQ_TYPE_SOFT_LIGHT:
+ case SEQ_TYPE_HARD_LIGHT:
+ case SEQ_TYPE_PIN_LIGHT:
+ case SEQ_TYPE_LIN_LIGHT:
+ case SEQ_TYPE_VIVID_LIGHT:
+ case SEQ_TYPE_BLEND_COLOR:
+ case SEQ_TYPE_HUE:
+ case SEQ_TYPE_SATURATION:
+ case SEQ_TYPE_VALUE:
+ case SEQ_TYPE_DIFFERENCE:
+ case SEQ_TYPE_EXCLUSION:
+ rval.multithreaded = true;
+ rval.execute_slice = do_blend_mode_effect;
+ rval.early_out = early_out_mul_input2;
+ break;
+ case SEQ_TYPE_COLORMIX:
+ rval.multithreaded = true;
+ rval.init = init_colormix_effect;
+ rval.free = free_effect_default;
+ rval.copy = copy_effect_default;
+ rval.execute_slice = do_colormix_effect;
+ rval.early_out = early_out_mul_input2;
+ break;
+ case SEQ_TYPE_ALPHAOVER:
+ rval.multithreaded = true;
+ rval.init = init_alpha_over_or_under;
+ rval.execute_slice = do_alphaover_effect;
+ break;
+ case SEQ_TYPE_OVERDROP:
+ rval.multithreaded = true;
+ rval.execute_slice = do_overdrop_effect;
+ break;
+ case SEQ_TYPE_ALPHAUNDER:
+ rval.multithreaded = true;
+ rval.init = init_alpha_over_or_under;
+ rval.execute_slice = do_alphaunder_effect;
+ break;
+ case SEQ_TYPE_WIPE:
+ rval.init = init_wipe_effect;
+ rval.num_inputs = num_inputs_wipe;
+ rval.free = free_wipe_effect;
+ rval.copy = copy_wipe_effect;
+ rval.early_out = early_out_fade;
+ rval.get_default_fac = get_default_fac_fade;
+ rval.execute = do_wipe_effect;
+ break;
+ case SEQ_TYPE_GLOW:
+ rval.init = init_glow_effect;
+ rval.num_inputs = num_inputs_glow;
+ rval.free = free_glow_effect;
+ rval.copy = copy_glow_effect;
+ rval.execute = do_glow_effect;
+ break;
+ case SEQ_TYPE_TRANSFORM:
+ rval.multithreaded = true;
+ rval.init = init_transform_effect;
+ rval.num_inputs = num_inputs_transform;
+ rval.free = free_transform_effect;
+ rval.copy = copy_transform_effect;
+ rval.execute_slice = do_transform_effect;
+ break;
+ case SEQ_TYPE_SPEED:
+ rval.init = init_speed_effect;
+ rval.num_inputs = num_inputs_speed;
+ rval.load = load_speed_effect;
+ rval.free = free_speed_effect;
+ rval.copy = copy_speed_effect;
+ rval.execute = do_speed_effect;
+ rval.early_out = early_out_speed;
+ rval.store_icu_yrange = store_icu_yrange_speed;
+ break;
+ case SEQ_TYPE_COLOR:
+ rval.init = init_solid_color;
+ rval.num_inputs = num_inputs_color;
+ rval.early_out = early_out_color;
+ rval.free = free_solid_color;
+ rval.copy = copy_solid_color;
+ rval.execute = do_solid_color;
+ break;
+ case SEQ_TYPE_MULTICAM:
+ rval.num_inputs = num_inputs_multicam;
+ rval.early_out = early_out_multicam;
+ rval.execute = do_multicam;
+ break;
+ case SEQ_TYPE_ADJUSTMENT:
+ rval.supports_mask = true;
+ rval.num_inputs = num_inputs_adjustment;
+ rval.early_out = early_out_adjustment;
+ rval.execute = do_adjustment;
+ break;
+ case SEQ_TYPE_GAUSSIAN_BLUR:
+ rval.init = init_gaussian_blur_effect;
+ rval.num_inputs = num_inputs_gaussian_blur;
+ rval.free = free_gaussian_blur_effect;
+ rval.copy = copy_gaussian_blur_effect;
+ rval.early_out = early_out_gaussian_blur;
+ rval.execute = do_gaussian_blur_effect;
+ break;
+ case SEQ_TYPE_TEXT:
+ rval.num_inputs = num_inputs_text;
+ rval.init = init_text_effect;
+ rval.free = free_text_effect;
+ rval.load = load_text_effect;
+ rval.copy = copy_text_effect;
+ rval.early_out = early_out_text;
+ rval.execute = do_text_effect;
+ break;
+ }
+
+ return rval;
+}
+
+struct SeqEffectHandle BKE_sequence_get_effect(Sequence *seq)
+{
+ struct SeqEffectHandle rval = {false, false, NULL};
+
+ if (seq->type & SEQ_TYPE_EFFECT) {
+ rval = get_sequence_effect_impl(seq->type);
+ if ((seq->flag & SEQ_EFFECT_NOT_LOADED) != 0) {
+ rval.load(seq);
+ seq->flag &= ~SEQ_EFFECT_NOT_LOADED;
+ }
+ }
+
+ return rval;
+}
+
+struct SeqEffectHandle BKE_sequence_get_blend(Sequence *seq)
+{
+ struct SeqEffectHandle rval = {false, false, NULL};
+
+ if (seq->blend_mode != 0) {
+ if ((seq->flag & SEQ_EFFECT_NOT_LOADED) != 0) {
+ /* load the effect first */
+ rval = get_sequence_effect_impl(seq->type);
+ rval.load(seq);
+ }
+
+ rval = get_sequence_effect_impl(seq->blend_mode);
+ if ((seq->flag & SEQ_EFFECT_NOT_LOADED) != 0) {
+ /* now load the blend and unset unloaded flag */
+ rval.load(seq);
+ seq->flag &= ~SEQ_EFFECT_NOT_LOADED;
+ }
+ }
+
+ return rval;
+}
+
+int BKE_sequence_effect_get_num_inputs(int seq_type)
+{
+ struct SeqEffectHandle rval = get_sequence_effect_impl(seq_type);
+
+ int cnt = rval.num_inputs();
+ if (rval.execute || (rval.execute_slice && rval.init_execution)) {
+ return cnt;
+ }
+ return 0;
+}
+
+int BKE_sequence_effect_get_supports_mask(int seq_type)
+{
+ struct SeqEffectHandle rval = get_sequence_effect_impl(seq_type);
+
+ return rval.supports_mask;
+}
diff --git a/source/blender/sequencer/intern/image_cache.c b/source/blender/sequencer/intern/image_cache.c
new file mode 100644
index 00000000000..7d2858050be
--- /dev/null
+++ b/source/blender/sequencer/intern/image_cache.c
@@ -0,0 +1,1455 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Peter Schlaile <peter [at] schlaile [dot] de> 2010
+ */
+
+/** \file
+ * \ingroup bke
+ */
+
+#include <memory.h>
+#include <stddef.h>
+#include <time.h>
+
+#include "MEM_guardedalloc.h"
+
+#include "DNA_scene_types.h"
+#include "DNA_sequence_types.h"
+#include "DNA_space_types.h" /* for FILE_MAX. */
+
+#include "IMB_colormanagement.h"
+#include "IMB_imbuf.h"
+#include "IMB_imbuf_types.h"
+
+#include "BLI_blenlib.h"
+#include "BLI_endian_switch.h"
+#include "BLI_fileops.h"
+#include "BLI_fileops_types.h"
+#include "BLI_ghash.h"
+#include "BLI_listbase.h"
+#include "BLI_mempool.h"
+#include "BLI_path_util.h"
+#include "BLI_threads.h"
+
+#include "BKE_global.h"
+#include "BKE_main.h"
+#include "BKE_scene.h"
+#include "BKE_sequencer.h"
+
+/**
+ * Sequencer Cache Design Notes
+ * ============================
+ *
+ * Function:
+ * All images created during rendering are added to cache, even if the cache is already full.
+ * This is because:
+ * - one image may be needed multiple times during rendering.
+ * - keeping the last rendered frame allows us for faster re-render when user edits strip in stack
+ * - we can decide if we keep frame only when it's completely rendered. Otherwise we risk having
+ * "holes" in the cache, which can be annoying
+ * If the cache is full all entries for pending frame will have is_temp_cache set.
+ *
+ * Linking: We use links to reduce number of iterations over entries needed to manage cache.
+ * Entries are linked in order as they are put into cache.
+ * Only permanent (is_temp_cache = 0) cache entries are linked.
+ * Putting #SEQ_CACHE_STORE_FINAL_OUT will reset linking
+ *
+ * Only entire frame can be freed to release resources for new entries (recycling).
+ * Once again, this is to reduce number of iterations, but also more controllable than removing
+ * entries one by one in reverse order to their creation.
+ *
+ * User can exclude caching of some images. Such entries will have is_temp_cache set.
+ *
+ *
+ * Disk Cache Design Notes
+ * =======================
+ *
+ * Disk cache uses directory specified in user preferences
+ * For each cached non-temp image, image data and supplementary info are written to HDD.
+ * Multiple(DCACHE_IMAGES_PER_FILE) images share the same file.
+ * Each of these files contains header DiskCacheHeader followed by image data.
+ * Zlib compression with user definable level can be used to compress image data(per image)
+ * Images are written in order in which they are rendered.
+ * Overwriting of individual entry is not possible.
+ * Stored images are deleted by invalidation, or when size of all files exceeds maximum
+ * size specified in user preferences.
+ * To distinguish 2 blend files with same name, scene->ed->disk_cache_timestamp
+ * is used as UID. Blend file can still be copied manually which may cause conflict.
+ *
+ */
+
+/* <cache type>-<resolution X>x<resolution Y>-<rendersize>%(<view_id>)-<frame no>.dcf */
+#define DCACHE_FNAME_FORMAT "%d-%dx%d-%d%%(%d)-%d.dcf"
+#define DCACHE_IMAGES_PER_FILE 100
+#define DCACHE_CURRENT_VERSION 1
+#define COLORSPACE_NAME_MAX 64 /* XXX: defined in imb intern */
+
+typedef struct DiskCacheHeaderEntry {
+ unsigned char encoding;
+ uint64_t frameno;
+ uint64_t size_compressed;
+ uint64_t size_raw;
+ uint64_t offset;
+ char colorspace_name[COLORSPACE_NAME_MAX];
+} DiskCacheHeaderEntry;
+
+typedef struct DiskCacheHeader {
+ DiskCacheHeaderEntry entry[DCACHE_IMAGES_PER_FILE];
+} DiskCacheHeader;
+
+typedef struct SeqDiskCache {
+ Main *bmain;
+ int64_t timestamp;
+ ListBase files;
+ ThreadMutex read_write_mutex;
+ size_t size_total;
+} SeqDiskCache;
+
+typedef struct DiskCacheFile {
+ struct DiskCacheFile *next, *prev;
+ char path[FILE_MAX];
+ char dir[FILE_MAXDIR];
+ char file[FILE_MAX];
+ BLI_stat_t fstat;
+ int cache_type;
+ int rectx;
+ int recty;
+ int render_size;
+ int view_id;
+ int start_frame;
+} DiskCacheFile;
+
+typedef struct SeqCache {
+ Main *bmain;
+ struct GHash *hash;
+ ThreadMutex iterator_mutex;
+ struct BLI_mempool *keys_pool;
+ struct BLI_mempool *items_pool;
+ struct SeqCacheKey *last_key;
+ size_t memory_used;
+ SeqDiskCache *disk_cache;
+} SeqCache;
+
+typedef struct SeqCacheItem {
+ struct SeqCache *cache_owner;
+ struct ImBuf *ibuf;
+} SeqCacheItem;
+
+typedef struct SeqCacheKey {
+ struct SeqCache *cache_owner;
+ void *userkey;
+ struct SeqCacheKey *link_prev; /* Used for linking intermediate items to final frame. */
+ struct SeqCacheKey *link_next; /* Used for linking intermediate items to final frame. */
+ struct Sequence *seq;
+ SeqRenderData context;
+ float nfra;
+ float cost; /* In short: render time(s) divided by playback frame duration(s) */
+ bool is_temp_cache; /* this cache entry will be freed before rendering next frame */
+ /* ID of task for asigning temp cache entries to particular task(thread, etc.) */
+ eSeqTaskId task_id;
+ int type;
+} SeqCacheKey;
+
+static ThreadMutex cache_create_lock = BLI_MUTEX_INITIALIZER;
+static float seq_cache_cfra_to_frame_index(Sequence *seq, float cfra);
+static float seq_cache_frame_index_to_cfra(Sequence *seq, float nfra);
+
+static char *seq_disk_cache_base_dir(void)
+{
+ return U.sequencer_disk_cache_dir;
+}
+
+static int seq_disk_cache_compression_level(void)
+{
+ switch (U.sequencer_disk_cache_compression) {
+ case USER_SEQ_DISK_CACHE_COMPRESSION_NONE:
+ return 0;
+ case USER_SEQ_DISK_CACHE_COMPRESSION_LOW:
+ return 1;
+ case USER_SEQ_DISK_CACHE_COMPRESSION_HIGH:
+ return 9;
+ }
+
+ return U.sequencer_disk_cache_compression;
+}
+
+static size_t seq_disk_cache_size_limit(void)
+{
+ return (size_t)U.sequencer_disk_cache_size_limit * (1024 * 1024 * 1024);
+}
+
+static bool seq_disk_cache_is_enabled(Main *bmain)
+{
+ return (U.sequencer_disk_cache_dir[0] != '\0' && U.sequencer_disk_cache_size_limit != 0 &&
+ (U.sequencer_disk_cache_flag & SEQ_CACHE_DISK_CACHE_ENABLE) != 0 &&
+ bmain->name[0] != '\0');
+}
+
+static DiskCacheFile *seq_disk_cache_add_file_to_list(SeqDiskCache *disk_cache, const char *path)
+{
+
+ DiskCacheFile *cache_file = MEM_callocN(sizeof(DiskCacheFile), "SeqDiskCacheFile");
+ char dir[FILE_MAXDIR], file[FILE_MAX];
+ BLI_split_dirfile(path, dir, file, sizeof(dir), sizeof(file));
+ BLI_strncpy(cache_file->path, path, sizeof(cache_file->path));
+ BLI_strncpy(cache_file->dir, dir, sizeof(cache_file->dir));
+ BLI_strncpy(cache_file->file, file, sizeof(cache_file->file));
+ sscanf(file,
+ DCACHE_FNAME_FORMAT,
+ &cache_file->cache_type,
+ &cache_file->rectx,
+ &cache_file->recty,
+ &cache_file->render_size,
+ &cache_file->view_id,
+ &cache_file->start_frame);
+ cache_file->start_frame *= DCACHE_IMAGES_PER_FILE;
+ BLI_addtail(&disk_cache->files, cache_file);
+ return cache_file;
+}
+
+static void seq_disk_cache_get_files(SeqDiskCache *disk_cache, char *path)
+{
+ struct direntry *filelist, *fl;
+ uint nbr, i;
+ disk_cache->size_total = 0;
+
+ i = nbr = BLI_filelist_dir_contents(path, &filelist);
+ fl = filelist;
+ while (i--) {
+ /* Don't follow links. */
+ const eFileAttributes file_attrs = BLI_file_attributes(fl->path);
+ if (file_attrs & FILE_ATTR_ANY_LINK) {
+ fl++;
+ continue;
+ }
+
+ char file[FILE_MAX];
+ BLI_split_dirfile(fl->path, NULL, file, 0, sizeof(file));
+
+ bool is_dir = BLI_is_dir(fl->path);
+ if (is_dir && !FILENAME_IS_CURRPAR(file)) {
+ char subpath[FILE_MAX];
+ BLI_strncpy(subpath, fl->path, sizeof(subpath));
+ BLI_path_slash_ensure(subpath);
+ seq_disk_cache_get_files(disk_cache, subpath);
+ }
+
+ if (!is_dir) {
+ const char *ext = BLI_path_extension(fl->path);
+ if (ext && ext[1] == 'd' && ext[2] == 'c' && ext[3] == 'f') {
+ DiskCacheFile *cache_file = seq_disk_cache_add_file_to_list(disk_cache, fl->path);
+ cache_file->fstat = fl->s;
+ disk_cache->size_total += cache_file->fstat.st_size;
+ }
+ }
+ fl++;
+ }
+ BLI_filelist_free(filelist, nbr);
+}
+
+static DiskCacheFile *seq_disk_cache_get_oldest_file(SeqDiskCache *disk_cache)
+{
+ DiskCacheFile *oldest_file = disk_cache->files.first;
+ if (oldest_file == NULL) {
+ return NULL;
+ }
+ for (DiskCacheFile *cache_file = oldest_file->next; cache_file; cache_file = cache_file->next) {
+ if (cache_file->fstat.st_mtime < oldest_file->fstat.st_mtime) {
+ oldest_file = cache_file;
+ }
+ }
+
+ return oldest_file;
+}
+
+static void seq_disk_cache_delete_file(SeqDiskCache *disk_cache, DiskCacheFile *file)
+{
+ disk_cache->size_total -= file->fstat.st_size;
+ BLI_delete(file->path, false, false);
+ BLI_remlink(&disk_cache->files, file);
+ MEM_freeN(file);
+}
+
+static bool seq_disk_cache_enforce_limits(SeqDiskCache *disk_cache)
+{
+ BLI_mutex_lock(&disk_cache->read_write_mutex);
+ while (disk_cache->size_total > seq_disk_cache_size_limit()) {
+ DiskCacheFile *oldest_file = seq_disk_cache_get_oldest_file(disk_cache);
+
+ if (!oldest_file) {
+ /* We shouldn't enforce limits with no files, do re-scan. */
+ seq_disk_cache_get_files(disk_cache, seq_disk_cache_base_dir());
+ continue;
+ }
+
+ if (BLI_exists(oldest_file->path) == 0) {
+ /* File may have been manually deleted during runtime, do re-scan. */
+ BLI_freelistN(&disk_cache->files);
+ seq_disk_cache_get_files(disk_cache, seq_disk_cache_base_dir());
+ continue;
+ }
+
+ seq_disk_cache_delete_file(disk_cache, oldest_file);
+ }
+ BLI_mutex_unlock(&disk_cache->read_write_mutex);
+
+ return true;
+}
+
+static DiskCacheFile *seq_disk_cache_get_file_entry_by_path(SeqDiskCache *disk_cache, char *path)
+{
+ DiskCacheFile *cache_file = disk_cache->files.first;
+
+ for (; cache_file; cache_file = cache_file->next) {
+ if (BLI_strcasecmp(cache_file->path, path) == 0) {
+ return cache_file;
+ }
+ }
+
+ return NULL;
+}
+
+/* Update file size and timestamp. */
+static void seq_disk_cache_update_file(SeqDiskCache *disk_cache, char *path)
+{
+ DiskCacheFile *cache_file;
+ int64_t size_before;
+ int64_t size_after;
+
+ cache_file = seq_disk_cache_get_file_entry_by_path(disk_cache, path);
+ size_before = cache_file->fstat.st_size;
+
+ if (BLI_stat(path, &cache_file->fstat) == -1) {
+ BLI_assert(false);
+ memset(&cache_file->fstat, 0, sizeof(BLI_stat_t));
+ }
+
+ size_after = cache_file->fstat.st_size;
+ disk_cache->size_total += size_after - size_before;
+}
+
+/* Path format:
+ * <cache dir>/<project name>/<scene name>-<timestamp>/<seq name>/DCACHE_FNAME_FORMAT
+ */
+
+static void seq_disk_cache_get_project_dir(SeqDiskCache *disk_cache, char *path, size_t path_len)
+{
+ char main_name[FILE_MAX];
+ BLI_split_file_part(BKE_main_blendfile_path(disk_cache->bmain), main_name, sizeof(main_name));
+ BLI_strncpy(path, seq_disk_cache_base_dir(), path_len);
+ BLI_path_append(path, path_len, main_name);
+}
+
+static void seq_disk_cache_get_dir(
+ SeqDiskCache *disk_cache, Scene *scene, Sequence *seq, char *path, size_t path_len)
+{
+ char scene_name[MAX_ID_NAME + 22]; /* + -%PRId64 */
+ char seq_name[SEQ_NAME_MAXSTR];
+ char project_dir[FILE_MAX];
+
+ seq_disk_cache_get_project_dir(disk_cache, project_dir, sizeof(project_dir));
+ sprintf(scene_name, "%s-%" PRId64, scene->id.name, disk_cache->timestamp);
+ BLI_strncpy(seq_name, seq->name, sizeof(seq_name));
+ BLI_filename_make_safe(scene_name);
+ BLI_filename_make_safe(seq_name);
+ BLI_strncpy(path, project_dir, path_len);
+ BLI_path_append(path, path_len, scene_name);
+ BLI_path_append(path, path_len, seq_name);
+}
+
+static void seq_disk_cache_get_file_path(SeqDiskCache *disk_cache,
+ SeqCacheKey *key,
+ char *path,
+ size_t path_len)
+{
+ seq_disk_cache_get_dir(disk_cache, key->context.scene, key->seq, path, path_len);
+ int frameno = (int)key->nfra / DCACHE_IMAGES_PER_FILE;
+ char cache_filename[FILE_MAXFILE];
+ sprintf(cache_filename,
+ DCACHE_FNAME_FORMAT,
+ key->type,
+ key->context.rectx,
+ key->context.recty,
+ key->context.preview_render_size,
+ key->context.view_id,
+ frameno);
+
+ BLI_path_append(path, path_len, cache_filename);
+}
+
+static void seq_disk_cache_create_version_file(char *path)
+{
+ BLI_make_existing_file(path);
+
+ FILE *file = BLI_fopen(path, "w");
+ if (file) {
+ fprintf(file, "%d", DCACHE_CURRENT_VERSION);
+ fclose(file);
+ }
+}
+
+static void seq_disk_cache_handle_versioning(SeqDiskCache *disk_cache)
+{
+ char path[FILE_MAX];
+ char path_version_file[FILE_MAX];
+ int version = 0;
+
+ seq_disk_cache_get_project_dir(disk_cache, path, sizeof(path));
+ BLI_strncpy(path_version_file, path, sizeof(path_version_file));
+ BLI_path_append(path_version_file, sizeof(path_version_file), "cache_version");
+
+ if (BLI_exists(path)) {
+ FILE *file = BLI_fopen(path_version_file, "r");
+
+ if (file) {
+ fscanf(file, "%d", &version);
+ fclose(file);
+ }
+
+ if (version != DCACHE_CURRENT_VERSION) {
+ BLI_delete(path, false, true);
+ seq_disk_cache_create_version_file(path_version_file);
+ }
+ }
+ else {
+ seq_disk_cache_create_version_file(path_version_file);
+ }
+}
+
+static void seq_disk_cache_delete_invalid_files(SeqDiskCache *disk_cache,
+ Scene *scene,
+ Sequence *seq,
+ int invalidate_types,
+ int range_start,
+ int range_end)
+{
+ DiskCacheFile *next_file, *cache_file = disk_cache->files.first;
+ char cache_dir[FILE_MAX];
+ seq_disk_cache_get_dir(disk_cache, scene, seq, cache_dir, sizeof(cache_dir));
+ BLI_path_slash_ensure(cache_dir);
+
+ while (cache_file) {
+ next_file = cache_file->next;
+ if (cache_file->cache_type & invalidate_types) {
+ if (STREQ(cache_dir, cache_file->dir)) {
+ int cfra_start = seq_cache_frame_index_to_cfra(seq, cache_file->start_frame);
+ if (cfra_start > range_start && cfra_start <= range_end) {
+ seq_disk_cache_delete_file(disk_cache, cache_file);
+ }
+ }
+ }
+ cache_file = next_file;
+ }
+}
+
+static void seq_disk_cache_invalidate(Scene *scene,
+ Sequence *seq,
+ Sequence *seq_changed,
+ int invalidate_types)
+{
+ int start;
+ int end;
+ SeqDiskCache *disk_cache = scene->ed->cache->disk_cache;
+
+ BLI_mutex_lock(&disk_cache->read_write_mutex);
+
+ start = seq_changed->startdisp - DCACHE_IMAGES_PER_FILE;
+ end = seq_changed->enddisp;
+
+ seq_disk_cache_delete_invalid_files(disk_cache, scene, seq, invalidate_types, start, end);
+
+ BLI_mutex_unlock(&disk_cache->read_write_mutex);
+}
+
+static size_t deflate_imbuf_to_file(ImBuf *ibuf,
+ FILE *file,
+ int level,
+ DiskCacheHeaderEntry *header_entry)
+{
+ if (ibuf->rect) {
+ return BLI_gzip_mem_to_file_at_pos(
+ ibuf->rect, header_entry->size_raw, file, header_entry->offset, level);
+ }
+
+ return BLI_gzip_mem_to_file_at_pos(
+ ibuf->rect_float, header_entry->size_raw, file, header_entry->offset, level);
+}
+
+static size_t inflate_file_to_imbuf(ImBuf *ibuf, FILE *file, DiskCacheHeaderEntry *header_entry)
+{
+ if (ibuf->rect) {
+ return BLI_ungzip_file_to_mem_at_pos(
+ ibuf->rect, header_entry->size_raw, file, header_entry->offset);
+ }
+
+ return BLI_ungzip_file_to_mem_at_pos(
+ ibuf->rect_float, header_entry->size_raw, file, header_entry->offset);
+}
+
+static void seq_disk_cache_read_header(FILE *file, DiskCacheHeader *header)
+{
+ fseek(file, 0, 0);
+ fread(header, sizeof(*header), 1, file);
+
+ for (int i = 0; i < DCACHE_IMAGES_PER_FILE; i++) {
+ if ((ENDIAN_ORDER == B_ENDIAN) && header->entry[i].encoding == 0) {
+ BLI_endian_switch_uint64(&header->entry[i].frameno);
+ BLI_endian_switch_uint64(&header->entry[i].offset);
+ BLI_endian_switch_uint64(&header->entry[i].size_compressed);
+ BLI_endian_switch_uint64(&header->entry[i].size_raw);
+ }
+ }
+}
+
+static size_t seq_disk_cache_write_header(FILE *file, DiskCacheHeader *header)
+{
+ fseek(file, 0, 0);
+ return fwrite(header, sizeof(*header), 1, file);
+}
+
+static int seq_disk_cache_add_header_entry(SeqCacheKey *key, ImBuf *ibuf, DiskCacheHeader *header)
+{
+ int i;
+ uint64_t offset = sizeof(*header);
+
+ /* Lookup free entry, get offset for new data. */
+ for (i = 0; i < DCACHE_IMAGES_PER_FILE; i++) {
+ if (header->entry[i].size_compressed == 0) {
+ break;
+ }
+ }
+
+ /* Attempt to write beyond set entry limit.
+ * Reset file header and start writing from beginning.
+ */
+ if (i == DCACHE_IMAGES_PER_FILE) {
+ i = 0;
+ memset(header, 0, sizeof(*header));
+ }
+
+ /* Calculate offset for image data. */
+ if (i > 0) {
+ offset = header->entry[i - 1].offset + header->entry[i - 1].size_compressed;
+ }
+
+ if (ENDIAN_ORDER == B_ENDIAN) {
+ header->entry[i].encoding = 255;
+ }
+ else {
+ header->entry[i].encoding = 0;
+ }
+
+ header->entry[i].offset = offset;
+ header->entry[i].frameno = key->nfra;
+
+ /* Store colorspace name of ibuf. */
+ const char *colorspace_name;
+ if (ibuf->rect) {
+ header->entry[i].size_raw = ibuf->x * ibuf->y * ibuf->channels;
+ colorspace_name = IMB_colormanagement_get_rect_colorspace(ibuf);
+ }
+ else {
+ header->entry[i].size_raw = ibuf->x * ibuf->y * ibuf->channels * 4;
+ colorspace_name = IMB_colormanagement_get_float_colorspace(ibuf);
+ }
+ BLI_strncpy(
+ header->entry[i].colorspace_name, colorspace_name, sizeof(header->entry[i].colorspace_name));
+
+ return i;
+}
+
+static int seq_disk_cache_get_header_entry(SeqCacheKey *key, DiskCacheHeader *header)
+{
+ for (int i = 0; i < DCACHE_IMAGES_PER_FILE; i++) {
+ if (header->entry[i].frameno == key->nfra) {
+ return i;
+ }
+ }
+
+ return -1;
+}
+
+static bool seq_disk_cache_write_file(SeqDiskCache *disk_cache, SeqCacheKey *key, ImBuf *ibuf)
+{
+ char path[FILE_MAX];
+
+ seq_disk_cache_get_file_path(disk_cache, key, path, sizeof(path));
+ BLI_make_existing_file(path);
+
+ FILE *file = BLI_fopen(path, "rb+");
+ if (!file) {
+ file = BLI_fopen(path, "wb+");
+ if (!file) {
+ return false;
+ }
+ seq_disk_cache_add_file_to_list(disk_cache, path);
+ }
+
+ DiskCacheHeader header;
+ memset(&header, 0, sizeof(header));
+ seq_disk_cache_read_header(file, &header);
+ int entry_index = seq_disk_cache_add_header_entry(key, ibuf, &header);
+ size_t bytes_written = deflate_imbuf_to_file(
+ ibuf, file, seq_disk_cache_compression_level(), &header.entry[entry_index]);
+
+ if (bytes_written != 0) {
+ /* Last step is writing header, as image data can be overwritten,
+ * but missing data would cause problems.
+ */
+ header.entry[entry_index].size_compressed = bytes_written;
+ seq_disk_cache_write_header(file, &header);
+ seq_disk_cache_update_file(disk_cache, path);
+ fclose(file);
+
+ return true;
+ }
+
+ return false;
+}
+
+static ImBuf *seq_disk_cache_read_file(SeqDiskCache *disk_cache, SeqCacheKey *key)
+{
+ char path[FILE_MAX];
+ DiskCacheHeader header;
+
+ seq_disk_cache_get_file_path(disk_cache, key, path, sizeof(path));
+ BLI_make_existing_file(path);
+
+ FILE *file = BLI_fopen(path, "rb");
+ if (!file) {
+ return NULL;
+ }
+
+ seq_disk_cache_read_header(file, &header);
+ int entry_index = seq_disk_cache_get_header_entry(key, &header);
+
+ /* Item not found. */
+ if (entry_index < 0) {
+ fclose(file);
+ return NULL;
+ }
+
+ ImBuf *ibuf;
+ uint64_t size_char = (uint64_t)key->context.rectx * key->context.recty * 4;
+ uint64_t size_float = (uint64_t)key->context.rectx * key->context.recty * 16;
+ size_t expected_size;
+
+ if (header.entry[entry_index].size_raw == size_char) {
+ expected_size = size_char;
+ ibuf = IMB_allocImBuf(key->context.rectx, key->context.recty, 32, IB_rect);
+ IMB_colormanagement_assign_rect_colorspace(ibuf, header.entry[entry_index].colorspace_name);
+ }
+ else if (header.entry[entry_index].size_raw == size_float) {
+ expected_size = size_float;
+ ibuf = IMB_allocImBuf(key->context.rectx, key->context.recty, 32, IB_rectfloat);
+ IMB_colormanagement_assign_float_colorspace(ibuf, header.entry[entry_index].colorspace_name);
+ }
+ else {
+ fclose(file);
+ return NULL;
+ }
+
+ size_t bytes_read = inflate_file_to_imbuf(ibuf, file, &header.entry[entry_index]);
+
+ /* Sanity check. */
+ if (bytes_read != expected_size) {
+ fclose(file);
+ IMB_freeImBuf(ibuf);
+ return NULL;
+ }
+ BLI_file_touch(path);
+ seq_disk_cache_update_file(disk_cache, path);
+ fclose(file);
+
+ return ibuf;
+}
+
+#undef DCACHE_FNAME_FORMAT
+#undef DCACHE_IMAGES_PER_FILE
+#undef COLORSPACE_NAME_MAX
+#undef DCACHE_CURRENT_VERSION
+
+static bool seq_cmp_render_data(const SeqRenderData *a, const SeqRenderData *b)
+{
+ return ((a->preview_render_size != b->preview_render_size) || (a->rectx != b->rectx) ||
+ (a->recty != b->recty) || (a->bmain != b->bmain) || (a->scene != b->scene) ||
+ (a->motion_blur_shutter != b->motion_blur_shutter) ||
+ (a->motion_blur_samples != b->motion_blur_samples) ||
+ (a->scene->r.views_format != b->scene->r.views_format) || (a->view_id != b->view_id));
+}
+
+static unsigned int seq_hash_render_data(const SeqRenderData *a)
+{
+ unsigned int rval = a->rectx + a->recty;
+
+ rval ^= a->preview_render_size;
+ rval ^= ((intptr_t)a->bmain) << 6;
+ rval ^= ((intptr_t)a->scene) << 6;
+ rval ^= (int)(a->motion_blur_shutter * 100.0f) << 10;
+ rval ^= a->motion_blur_samples << 16;
+ rval ^= ((a->scene->r.views_format * 2) + a->view_id) << 24;
+
+ return rval;
+}
+
+static unsigned int seq_cache_hashhash(const void *key_)
+{
+ const SeqCacheKey *key = key_;
+ unsigned int rval = seq_hash_render_data(&key->context);
+
+ rval ^= *(const unsigned int *)&key->nfra;
+ rval += key->type;
+ rval ^= ((intptr_t)key->seq) << 6;
+
+ return rval;
+}
+
+static bool seq_cache_hashcmp(const void *a_, const void *b_)
+{
+ const SeqCacheKey *a = a_;
+ const SeqCacheKey *b = b_;
+
+ return ((a->seq != b->seq) || (a->nfra != b->nfra) || (a->type != b->type) ||
+ seq_cmp_render_data(&a->context, &b->context));
+}
+
+static float seq_cache_cfra_to_frame_index(Sequence *seq, float cfra)
+{
+ return cfra - seq->start;
+}
+
+static float seq_cache_frame_index_to_cfra(Sequence *seq, float nfra)
+{
+ return nfra + seq->start;
+}
+
+static SeqCache *seq_cache_get_from_scene(Scene *scene)
+{
+ if (scene && scene->ed && scene->ed->cache) {
+ return scene->ed->cache;
+ }
+
+ return NULL;
+}
+
+static void seq_cache_lock(Scene *scene)
+{
+ SeqCache *cache = seq_cache_get_from_scene(scene);
+
+ if (cache) {
+ BLI_mutex_lock(&cache->iterator_mutex);
+ }
+}
+
+static void seq_cache_unlock(Scene *scene)
+{
+ SeqCache *cache = seq_cache_get_from_scene(scene);
+
+ if (cache) {
+ BLI_mutex_unlock(&cache->iterator_mutex);
+ }
+}
+
+static size_t seq_cache_get_mem_total(void)
+{
+ return ((size_t)U.memcachelimit) * 1024 * 1024;
+}
+
+static void seq_cache_keyfree(void *val)
+{
+ SeqCacheKey *key = val;
+ BLI_mempool_free(key->cache_owner->keys_pool, key);
+}
+
+static void seq_cache_valfree(void *val)
+{
+ SeqCacheItem *item = (SeqCacheItem *)val;
+ SeqCache *cache = item->cache_owner;
+
+ if (item->ibuf) {
+ cache->memory_used -= IMB_get_size_in_memory(item->ibuf);
+ IMB_freeImBuf(item->ibuf);
+ }
+
+ BLI_mempool_free(item->cache_owner->items_pool, item);
+}
+
+static void seq_cache_put(SeqCache *cache, SeqCacheKey *key, ImBuf *ibuf)
+{
+ SeqCacheItem *item;
+ item = BLI_mempool_alloc(cache->items_pool);
+ item->cache_owner = cache;
+ item->ibuf = ibuf;
+
+ if (BLI_ghash_reinsert(cache->hash, key, item, seq_cache_keyfree, seq_cache_valfree)) {
+ IMB_refImBuf(ibuf);
+ cache->last_key = key;
+ cache->memory_used += IMB_get_size_in_memory(ibuf);
+ }
+}
+
+static ImBuf *seq_cache_get(SeqCache *cache, SeqCacheKey *key)
+{
+ SeqCacheItem *item = BLI_ghash_lookup(cache->hash, key);
+
+ if (item && item->ibuf) {
+ IMB_refImBuf(item->ibuf);
+
+ return item->ibuf;
+ }
+
+ return NULL;
+}
+
+static void seq_cache_relink_keys(SeqCacheKey *link_next, SeqCacheKey *link_prev)
+{
+ if (link_next) {
+ link_next->link_prev = link_prev;
+ }
+ if (link_prev) {
+ link_prev->link_next = link_next;
+ }
+}
+
+/* Choose a key out of 2 candidates(leftmost and rightmost items)
+ * to recycle based on currently used strategy */
+static SeqCacheKey *seq_cache_choose_key(Scene *scene, SeqCacheKey *lkey, SeqCacheKey *rkey)
+{
+ SeqCacheKey *finalkey = NULL;
+
+ /* Ideally, cache would not need to check the state of prefetching task
+ * that is tricky to do however, because prefetch would need to know,
+ * if a key, that is about to be created would be removed by itself.
+ *
+ * This can happen because only FINAL_OUT item insertion will trigger recycling
+ * but that is also the point, where prefetch can be suspended.
+ *
+ * We could use temp cache as a shield and later make it a non-temporary entry,
+ * but it is not worth of increasing system complexity.
+ */
+ if (scene->ed->cache_flag & SEQ_CACHE_PREFETCH_ENABLE &&
+ BKE_sequencer_prefetch_job_is_running(scene)) {
+ int pfjob_start, pfjob_end;
+ BKE_sequencer_prefetch_get_time_range(scene, &pfjob_start, &pfjob_end);
+
+ if (lkey) {
+ int lkey_cfra = seq_cache_frame_index_to_cfra(lkey->seq, lkey->nfra);
+ if (lkey_cfra < pfjob_start || lkey_cfra > pfjob_end) {
+ return lkey;
+ }
+ }
+
+ if (rkey) {
+ int rkey_cfra = seq_cache_frame_index_to_cfra(rkey->seq, rkey->nfra);
+ if (rkey_cfra < pfjob_start || rkey_cfra > pfjob_end) {
+ return rkey;
+ }
+ }
+
+ return NULL;
+ }
+
+ if (rkey && lkey) {
+ int lkey_cfra = seq_cache_frame_index_to_cfra(lkey->seq, lkey->nfra);
+ int rkey_cfra = seq_cache_frame_index_to_cfra(rkey->seq, rkey->nfra);
+
+ if (lkey_cfra > rkey_cfra) {
+ SeqCacheKey *swapkey = lkey;
+ lkey = rkey;
+ rkey = swapkey;
+ }
+
+ int l_diff = scene->r.cfra - lkey_cfra;
+ int r_diff = rkey_cfra - scene->r.cfra;
+
+ if (l_diff > r_diff) {
+ finalkey = lkey;
+ }
+ else {
+ finalkey = rkey;
+ }
+ }
+ else {
+ if (lkey) {
+ finalkey = lkey;
+ }
+ else {
+ finalkey = rkey;
+ }
+ }
+ return finalkey;
+}
+
+static void seq_cache_recycle_linked(Scene *scene, SeqCacheKey *base)
+{
+ SeqCache *cache = seq_cache_get_from_scene(scene);
+ if (!cache) {
+ return;
+ }
+
+ SeqCacheKey *next = base->link_next;
+
+ while (base) {
+ SeqCacheKey *prev = base->link_prev;
+ BLI_ghash_remove(cache->hash, base, seq_cache_keyfree, seq_cache_valfree);
+ base = prev;
+ }
+
+ base = next;
+ while (base) {
+ next = base->link_next;
+ BLI_ghash_remove(cache->hash, base, seq_cache_keyfree, seq_cache_valfree);
+ base = next;
+ }
+}
+
+static SeqCacheKey *seq_cache_get_item_for_removal(Scene *scene)
+{
+ SeqCache *cache = seq_cache_get_from_scene(scene);
+ SeqCacheKey *finalkey = NULL;
+ /* Leftmost key. */
+ SeqCacheKey *lkey = NULL;
+ /* Rightmost key. */
+ SeqCacheKey *rkey = NULL;
+ SeqCacheKey *key = NULL;
+
+ GHashIterator gh_iter;
+ BLI_ghashIterator_init(&gh_iter, cache->hash);
+ int total_count = 0;
+ int cheap_count = 0;
+
+ while (!BLI_ghashIterator_done(&gh_iter)) {
+ key = BLI_ghashIterator_getKey(&gh_iter);
+ SeqCacheItem *item = BLI_ghashIterator_getValue(&gh_iter);
+ BLI_ghashIterator_step(&gh_iter);
+
+ /* This shouldn't happen, but better be safe than sorry. */
+ if (!item->ibuf) {
+ seq_cache_recycle_linked(scene, key);
+ /* Can not continue iterating after linked remove. */
+ BLI_ghashIterator_init(&gh_iter, cache->hash);
+ continue;
+ }
+
+ if (key->is_temp_cache || key->link_next != NULL) {
+ continue;
+ }
+
+ total_count++;
+
+ if (key->cost <= scene->ed->recycle_max_cost) {
+ cheap_count++;
+ if (lkey) {
+ if (seq_cache_frame_index_to_cfra(key->seq, key->nfra) <
+ seq_cache_frame_index_to_cfra(lkey->seq, lkey->nfra)) {
+ lkey = key;
+ }
+ }
+ else {
+ lkey = key;
+ }
+ if (rkey) {
+ if (seq_cache_frame_index_to_cfra(key->seq, key->nfra) >
+ seq_cache_frame_index_to_cfra(rkey->seq, rkey->nfra)) {
+ rkey = key;
+ }
+ }
+ else {
+ rkey = key;
+ }
+ }
+ }
+
+ finalkey = seq_cache_choose_key(scene, lkey, rkey);
+
+ return finalkey;
+}
+
+/* Find only "base" keys.
+ * Sources(other types) for a frame must be freed all at once.
+ */
+bool BKE_sequencer_cache_recycle_item(Scene *scene)
+{
+ size_t memory_total = seq_cache_get_mem_total();
+ SeqCache *cache = seq_cache_get_from_scene(scene);
+ if (!cache) {
+ return false;
+ }
+
+ seq_cache_lock(scene);
+
+ while (cache->memory_used > memory_total) {
+ SeqCacheKey *finalkey = seq_cache_get_item_for_removal(scene);
+
+ if (finalkey) {
+ seq_cache_recycle_linked(scene, finalkey);
+ }
+ else {
+ seq_cache_unlock(scene);
+ return false;
+ }
+ }
+ seq_cache_unlock(scene);
+ return true;
+}
+
+static void seq_cache_set_temp_cache_linked(Scene *scene, SeqCacheKey *base)
+{
+ SeqCache *cache = seq_cache_get_from_scene(scene);
+
+ if (!cache || !base) {
+ return;
+ }
+
+ SeqCacheKey *next = base->link_next;
+
+ while (base) {
+ SeqCacheKey *prev = base->link_prev;
+ base->is_temp_cache = true;
+ base = prev;
+ }
+
+ base = next;
+ while (base) {
+ next = base->link_next;
+ base->is_temp_cache = true;
+ base = next;
+ }
+}
+
+static void seq_disk_cache_create(Main *bmain, Scene *scene)
+{
+ BLI_mutex_lock(&cache_create_lock);
+ SeqCache *cache = seq_cache_get_from_scene(scene);
+
+ if (cache == NULL) {
+ return;
+ }
+
+ if (cache->disk_cache != NULL) {
+ return;
+ }
+
+ cache->disk_cache = MEM_callocN(sizeof(SeqDiskCache), "SeqDiskCache");
+ cache->disk_cache->bmain = bmain;
+ BLI_mutex_init(&cache->disk_cache->read_write_mutex);
+ seq_disk_cache_handle_versioning(cache->disk_cache);
+ seq_disk_cache_get_files(cache->disk_cache, seq_disk_cache_base_dir());
+ cache->disk_cache->timestamp = scene->ed->disk_cache_timestamp;
+ BLI_mutex_unlock(&cache_create_lock);
+}
+
+static void seq_cache_create(Main *bmain, Scene *scene)
+{
+ BLI_mutex_lock(&cache_create_lock);
+ if (scene->ed->cache == NULL) {
+ SeqCache *cache = MEM_callocN(sizeof(SeqCache), "SeqCache");
+ cache->keys_pool = BLI_mempool_create(sizeof(SeqCacheKey), 0, 64, BLI_MEMPOOL_NOP);
+ cache->items_pool = BLI_mempool_create(sizeof(SeqCacheItem), 0, 64, BLI_MEMPOOL_NOP);
+ cache->hash = BLI_ghash_new(seq_cache_hashhash, seq_cache_hashcmp, "SeqCache hash");
+ cache->last_key = NULL;
+ cache->bmain = bmain;
+ BLI_mutex_init(&cache->iterator_mutex);
+ scene->ed->cache = cache;
+
+ if (scene->ed->disk_cache_timestamp == 0) {
+ scene->ed->disk_cache_timestamp = time(NULL);
+ }
+ }
+ BLI_mutex_unlock(&cache_create_lock);
+}
+
+/* ***************************** API ****************************** */
+
+void BKE_sequencer_cache_free_temp_cache(Scene *scene, short id, int cfra)
+{
+ SeqCache *cache = seq_cache_get_from_scene(scene);
+ if (!cache) {
+ return;
+ }
+
+ seq_cache_lock(scene);
+
+ GHashIterator gh_iter;
+ BLI_ghashIterator_init(&gh_iter, cache->hash);
+ while (!BLI_ghashIterator_done(&gh_iter)) {
+ SeqCacheKey *key = BLI_ghashIterator_getKey(&gh_iter);
+ BLI_ghashIterator_step(&gh_iter);
+
+ if (key->is_temp_cache && key->task_id == id &&
+ seq_cache_frame_index_to_cfra(key->seq, key->nfra) != cfra) {
+ BLI_ghash_remove(cache->hash, key, seq_cache_keyfree, seq_cache_valfree);
+ }
+ }
+ seq_cache_unlock(scene);
+}
+
+void BKE_sequencer_cache_destruct(Scene *scene)
+{
+ SeqCache *cache = seq_cache_get_from_scene(scene);
+ if (!cache) {
+ return;
+ }
+
+ BLI_ghash_free(cache->hash, seq_cache_keyfree, seq_cache_valfree);
+ BLI_mempool_destroy(cache->keys_pool);
+ BLI_mempool_destroy(cache->items_pool);
+ BLI_mutex_end(&cache->iterator_mutex);
+
+ if (cache->disk_cache != NULL) {
+ BLI_freelistN(&cache->disk_cache->files);
+ BLI_mutex_end(&cache->disk_cache->read_write_mutex);
+ MEM_freeN(cache->disk_cache);
+ }
+
+ MEM_freeN(cache);
+ scene->ed->cache = NULL;
+}
+
+void BKE_sequencer_cache_cleanup_all(Main *bmain)
+{
+ for (Scene *scene = bmain->scenes.first; scene != NULL; scene = scene->id.next) {
+ BKE_sequencer_cache_cleanup(scene);
+ }
+}
+void BKE_sequencer_cache_cleanup(Scene *scene)
+{
+ BKE_sequencer_prefetch_stop(scene);
+
+ SeqCache *cache = seq_cache_get_from_scene(scene);
+ if (!cache) {
+ return;
+ }
+
+ seq_cache_lock(scene);
+
+ GHashIterator gh_iter;
+ BLI_ghashIterator_init(&gh_iter, cache->hash);
+ while (!BLI_ghashIterator_done(&gh_iter)) {
+ SeqCacheKey *key = BLI_ghashIterator_getKey(&gh_iter);
+
+ BLI_ghashIterator_step(&gh_iter);
+ BLI_ghash_remove(cache->hash, key, seq_cache_keyfree, seq_cache_valfree);
+ }
+ cache->last_key = NULL;
+ seq_cache_unlock(scene);
+}
+
+void BKE_sequencer_cache_cleanup_sequence(Scene *scene,
+ Sequence *seq,
+ Sequence *seq_changed,
+ int invalidate_types,
+ bool force_seq_changed_range)
+{
+ SeqCache *cache = seq_cache_get_from_scene(scene);
+ if (!cache) {
+ return;
+ }
+
+ if (seq_disk_cache_is_enabled(cache->bmain) && cache->disk_cache != NULL) {
+ seq_disk_cache_invalidate(scene, seq, seq_changed, invalidate_types);
+ }
+
+ seq_cache_lock(scene);
+
+ int range_start = seq_changed->startdisp;
+ int range_end = seq_changed->enddisp;
+
+ if (!force_seq_changed_range) {
+ if (seq->startdisp > range_start) {
+ range_start = seq->startdisp;
+ }
+
+ if (seq->enddisp < range_end) {
+ range_end = seq->enddisp;
+ }
+ }
+
+ int invalidate_composite = invalidate_types & SEQ_CACHE_STORE_FINAL_OUT;
+ int invalidate_source = invalidate_types & (SEQ_CACHE_STORE_RAW | SEQ_CACHE_STORE_PREPROCESSED |
+ SEQ_CACHE_STORE_COMPOSITE);
+
+ GHashIterator gh_iter;
+ BLI_ghashIterator_init(&gh_iter, cache->hash);
+ while (!BLI_ghashIterator_done(&gh_iter)) {
+ SeqCacheKey *key = BLI_ghashIterator_getKey(&gh_iter);
+ BLI_ghashIterator_step(&gh_iter);
+
+ int key_cfra = seq_cache_frame_index_to_cfra(key->seq, key->nfra);
+
+ /* Clean all final and composite in intersection of seq and seq_changed. */
+ if (key->type & invalidate_composite && key_cfra >= range_start && key_cfra <= range_end) {
+ if (key->link_next || key->link_prev) {
+ seq_cache_relink_keys(key->link_next, key->link_prev);
+ }
+
+ BLI_ghash_remove(cache->hash, key, seq_cache_keyfree, seq_cache_valfree);
+ }
+
+ if (key->type & invalidate_source && key->seq == seq && key_cfra >= seq_changed->startdisp &&
+ key_cfra <= seq_changed->enddisp) {
+ if (key->link_next || key->link_prev) {
+ seq_cache_relink_keys(key->link_next, key->link_prev);
+ }
+
+ BLI_ghash_remove(cache->hash, key, seq_cache_keyfree, seq_cache_valfree);
+ }
+ }
+ cache->last_key = NULL;
+ seq_cache_unlock(scene);
+}
+
+struct ImBuf *BKE_sequencer_cache_get(
+ const SeqRenderData *context, Sequence *seq, float cfra, int type, bool skip_disk_cache)
+{
+
+ if (context->skip_cache || context->is_proxy_render || !seq) {
+ return NULL;
+ }
+
+ Scene *scene = context->scene;
+
+ if (context->is_prefetch_render) {
+ context = BKE_sequencer_prefetch_get_original_context(context);
+ scene = context->scene;
+ seq = BKE_sequencer_prefetch_get_original_sequence(seq, scene);
+ }
+
+ if (!seq) {
+ return NULL;
+ }
+
+ if (!scene->ed->cache) {
+ seq_cache_create(context->bmain, scene);
+ }
+
+ seq_cache_lock(scene);
+ SeqCache *cache = seq_cache_get_from_scene(scene);
+ ImBuf *ibuf = NULL;
+ SeqCacheKey key;
+
+ /* Try RAM cache: */
+ if (cache && seq) {
+ key.seq = seq;
+ key.context = *context;
+ key.nfra = seq_cache_cfra_to_frame_index(seq, cfra);
+ key.type = type;
+
+ ibuf = seq_cache_get(cache, &key);
+ }
+ seq_cache_unlock(scene);
+
+ if (ibuf) {
+ return ibuf;
+ }
+
+ /* Try disk cache: */
+ if (!skip_disk_cache && seq_disk_cache_is_enabled(context->bmain)) {
+ if (cache->disk_cache == NULL) {
+ seq_disk_cache_create(context->bmain, context->scene);
+ }
+
+ BLI_mutex_lock(&cache->disk_cache->read_write_mutex);
+ ibuf = seq_disk_cache_read_file(cache->disk_cache, &key);
+ BLI_mutex_unlock(&cache->disk_cache->read_write_mutex);
+ if (ibuf) {
+ if (key.type == SEQ_CACHE_STORE_FINAL_OUT) {
+ BKE_sequencer_cache_put_if_possible(context, seq, cfra, type, ibuf, 0.0f, true);
+ }
+ else {
+ BKE_sequencer_cache_put(context, seq, cfra, type, ibuf, 0.0f, true);
+ }
+ }
+ }
+
+ return ibuf;
+}
+
+bool BKE_sequencer_cache_put_if_possible(const SeqRenderData *context,
+ Sequence *seq,
+ float cfra,
+ int type,
+ ImBuf *ibuf,
+ float cost,
+ bool skip_disk_cache)
+{
+ Scene *scene = context->scene;
+
+ if (context->is_prefetch_render) {
+ context = BKE_sequencer_prefetch_get_original_context(context);
+ scene = context->scene;
+ seq = BKE_sequencer_prefetch_get_original_sequence(seq, scene);
+ }
+
+ if (!seq) {
+ return false;
+ }
+
+ if (BKE_sequencer_cache_recycle_item(scene)) {
+ BKE_sequencer_cache_put(context, seq, cfra, type, ibuf, cost, skip_disk_cache);
+ return true;
+ }
+
+ seq_cache_set_temp_cache_linked(scene, scene->ed->cache->last_key);
+ scene->ed->cache->last_key = NULL;
+ return false;
+}
+
+void BKE_sequencer_cache_put(const SeqRenderData *context,
+ Sequence *seq,
+ float cfra,
+ int type,
+ ImBuf *i,
+ float cost,
+ bool skip_disk_cache)
+{
+ if (i == NULL || context->skip_cache || context->is_proxy_render || !seq) {
+ return;
+ }
+
+ Scene *scene = context->scene;
+
+ if (context->is_prefetch_render) {
+ context = BKE_sequencer_prefetch_get_original_context(context);
+ scene = context->scene;
+ seq = BKE_sequencer_prefetch_get_original_sequence(seq, scene);
+ BLI_assert(seq != NULL);
+ }
+
+ /* Prevent reinserting, it breaks cache key linking. */
+ ImBuf *test = BKE_sequencer_cache_get(context, seq, cfra, type, true);
+ if (test) {
+ IMB_freeImBuf(test);
+ return;
+ }
+
+ if (!scene->ed->cache) {
+ seq_cache_create(context->bmain, scene);
+ }
+
+ seq_cache_lock(scene);
+
+ SeqCache *cache = seq_cache_get_from_scene(scene);
+ int flag;
+
+ if (seq->cache_flag & SEQ_CACHE_OVERRIDE) {
+ flag = seq->cache_flag;
+ /* Final_out is invalid in context of sequence override. */
+ flag -= seq->cache_flag & SEQ_CACHE_STORE_FINAL_OUT;
+ /* If global setting is enabled however, use it. */
+ flag |= scene->ed->cache_flag & SEQ_CACHE_STORE_FINAL_OUT;
+ }
+ else {
+ flag = scene->ed->cache_flag;
+ }
+
+ if (cost > SEQ_CACHE_COST_MAX) {
+ cost = SEQ_CACHE_COST_MAX;
+ }
+
+ SeqCacheKey *key;
+ key = BLI_mempool_alloc(cache->keys_pool);
+ key->cache_owner = cache;
+ key->seq = seq;
+ key->context = *context;
+ key->nfra = seq_cache_cfra_to_frame_index(seq, cfra);
+ key->type = type;
+ key->cost = cost;
+ key->link_prev = NULL;
+ key->link_next = NULL;
+ key->is_temp_cache = true;
+ key->task_id = context->task_id;
+
+ /* Item stored for later use */
+ if (flag & type) {
+ key->is_temp_cache = false;
+ key->link_prev = cache->last_key;
+ }
+
+ SeqCacheKey *temp_last_key = cache->last_key;
+ seq_cache_put(cache, key, i);
+
+ /* Restore pointer to previous item as this one will be freed when stack is rendered. */
+ if (key->is_temp_cache) {
+ cache->last_key = temp_last_key;
+ }
+
+ /* Set last_key's reference to this key so we can look up chain backwards.
+ * Item is already put in cache, so cache->last_key points to current key.
+ */
+ if (flag & type && temp_last_key) {
+ temp_last_key->link_next = cache->last_key;
+ }
+
+ /* Reset linking. */
+ if (key->type == SEQ_CACHE_STORE_FINAL_OUT) {
+ cache->last_key = NULL;
+ }
+
+ seq_cache_unlock(scene);
+
+ if (!key->is_temp_cache && !skip_disk_cache) {
+ if (seq_disk_cache_is_enabled(context->bmain)) {
+ if (cache->disk_cache == NULL) {
+ seq_disk_cache_create(context->bmain, context->scene);
+ }
+
+ BLI_mutex_lock(&cache->disk_cache->read_write_mutex);
+ seq_disk_cache_write_file(cache->disk_cache, key, i);
+ BLI_mutex_unlock(&cache->disk_cache->read_write_mutex);
+ seq_disk_cache_enforce_limits(cache->disk_cache);
+ }
+ }
+}
+
+void BKE_sequencer_cache_iterate(
+ struct Scene *scene,
+ void *userdata,
+ bool callback_init(void *userdata, size_t item_count),
+ bool callback_iter(void *userdata, struct Sequence *seq, int nfra, int cache_type, float cost))
+{
+ SeqCache *cache = seq_cache_get_from_scene(scene);
+ if (!cache) {
+ return;
+ }
+
+ seq_cache_lock(scene);
+ bool interrupt = callback_init(userdata, BLI_ghash_len(cache->hash));
+
+ GHashIterator gh_iter;
+ BLI_ghashIterator_init(&gh_iter, cache->hash);
+
+ while (!BLI_ghashIterator_done(&gh_iter) && !interrupt) {
+ SeqCacheKey *key = BLI_ghashIterator_getKey(&gh_iter);
+ BLI_ghashIterator_step(&gh_iter);
+
+ interrupt = callback_iter(userdata, key->seq, key->nfra, key->type, key->cost);
+ }
+
+ cache->last_key = NULL;
+ seq_cache_unlock(scene);
+}
+
+bool BKE_sequencer_cache_is_full(Scene *scene)
+{
+ size_t memory_total = seq_cache_get_mem_total();
+ SeqCache *cache = seq_cache_get_from_scene(scene);
+ if (!cache) {
+ return false;
+ }
+
+ return memory_total < cache->memory_used;
+}
diff --git a/source/blender/sequencer/intern/modifier.c b/source/blender/sequencer/intern/modifier.c
new file mode 100644
index 00000000000..a38fe252731
--- /dev/null
+++ b/source/blender/sequencer/intern/modifier.c
@@ -0,0 +1,1130 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2012 Blender Foundation.
+ * All rights reserved.
+ */
+
+/** \file
+ * \ingroup bke
+ */
+
+#include <stddef.h>
+#include <string.h>
+
+#include "MEM_guardedalloc.h"
+
+#include "BLI_listbase.h"
+#include "BLI_math.h"
+#include "BLI_string.h"
+#include "BLI_string_utils.h"
+#include "BLI_utildefines.h"
+
+#include "BLT_translation.h"
+
+#include "DNA_mask_types.h"
+#include "DNA_scene_types.h"
+#include "DNA_sequence_types.h"
+
+#include "BKE_colortools.h"
+#include "BKE_sequencer.h"
+
+#include "IMB_colormanagement.h"
+#include "IMB_imbuf.h"
+#include "IMB_imbuf_types.h"
+
+static SequenceModifierTypeInfo *modifiersTypes[NUM_SEQUENCE_MODIFIER_TYPES];
+static bool modifierTypesInit = false;
+
+/* -------------------------------------------------------------------- */
+/** \name Modifier Multi-Threading Utilities
+ * \{ */
+
+typedef void (*modifier_apply_threaded_cb)(int width,
+ int height,
+ unsigned char *rect,
+ float *rect_float,
+ unsigned char *mask_rect,
+ const float *mask_rect_float,
+ void *data_v);
+
+typedef struct ModifierInitData {
+ ImBuf *ibuf;
+ ImBuf *mask;
+ void *user_data;
+
+ modifier_apply_threaded_cb apply_callback;
+} ModifierInitData;
+
+typedef struct ModifierThread {
+ int width, height;
+
+ unsigned char *rect, *mask_rect;
+ float *rect_float, *mask_rect_float;
+
+ void *user_data;
+
+ modifier_apply_threaded_cb apply_callback;
+} ModifierThread;
+
+static ImBuf *modifier_mask_get(SequenceModifierData *smd,
+ const SeqRenderData *context,
+ int cfra,
+ int fra_offset,
+ bool make_float)
+{
+ return BKE_sequencer_render_mask_input(context,
+ smd->mask_input_type,
+ smd->mask_sequence,
+ smd->mask_id,
+ cfra,
+ fra_offset,
+ make_float);
+}
+
+static void modifier_init_handle(void *handle_v, int start_line, int tot_line, void *init_data_v)
+{
+ ModifierThread *handle = (ModifierThread *)handle_v;
+ ModifierInitData *init_data = (ModifierInitData *)init_data_v;
+ ImBuf *ibuf = init_data->ibuf;
+ ImBuf *mask = init_data->mask;
+
+ int offset = 4 * start_line * ibuf->x;
+
+ memset(handle, 0, sizeof(ModifierThread));
+
+ handle->width = ibuf->x;
+ handle->height = tot_line;
+ handle->apply_callback = init_data->apply_callback;
+ handle->user_data = init_data->user_data;
+
+ if (ibuf->rect) {
+ handle->rect = (unsigned char *)ibuf->rect + offset;
+ }
+
+ if (ibuf->rect_float) {
+ handle->rect_float = ibuf->rect_float + offset;
+ }
+
+ if (mask) {
+ if (mask->rect) {
+ handle->mask_rect = (unsigned char *)mask->rect + offset;
+ }
+
+ if (mask->rect_float) {
+ handle->mask_rect_float = mask->rect_float + offset;
+ }
+ }
+ else {
+ handle->mask_rect = NULL;
+ handle->mask_rect_float = NULL;
+ }
+}
+
+static void *modifier_do_thread(void *thread_data_v)
+{
+ ModifierThread *td = (ModifierThread *)thread_data_v;
+
+ td->apply_callback(td->width,
+ td->height,
+ td->rect,
+ td->rect_float,
+ td->mask_rect,
+ td->mask_rect_float,
+ td->user_data);
+
+ return NULL;
+}
+
+static void modifier_apply_threaded(ImBuf *ibuf,
+ ImBuf *mask,
+ modifier_apply_threaded_cb apply_callback,
+ void *user_data)
+{
+ ModifierInitData init_data;
+
+ init_data.ibuf = ibuf;
+ init_data.mask = mask;
+ init_data.user_data = user_data;
+
+ init_data.apply_callback = apply_callback;
+
+ IMB_processor_apply_threaded(
+ ibuf->y, sizeof(ModifierThread), &init_data, modifier_init_handle, modifier_do_thread);
+}
+
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Color Balance Modifier
+ * \{ */
+
+static void colorBalance_init_data(SequenceModifierData *smd)
+{
+ ColorBalanceModifierData *cbmd = (ColorBalanceModifierData *)smd;
+ int c;
+
+ cbmd->color_multiply = 1.0f;
+
+ for (c = 0; c < 3; c++) {
+ cbmd->color_balance.lift[c] = 1.0f;
+ cbmd->color_balance.gamma[c] = 1.0f;
+ cbmd->color_balance.gain[c] = 1.0f;
+ }
+}
+
+static void colorBalance_apply(SequenceModifierData *smd, ImBuf *ibuf, ImBuf *mask)
+{
+ ColorBalanceModifierData *cbmd = (ColorBalanceModifierData *)smd;
+
+ BKE_sequencer_color_balance_apply(&cbmd->color_balance, ibuf, cbmd->color_multiply, false, mask);
+}
+
+static SequenceModifierTypeInfo seqModifier_ColorBalance = {
+ CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "Color Balance"), /* name */
+ "ColorBalanceModifierData", /* struct_name */
+ sizeof(ColorBalanceModifierData), /* struct_size */
+ colorBalance_init_data, /* init_data */
+ NULL, /* free_data */
+ NULL, /* copy_data */
+ colorBalance_apply, /* apply */
+};
+
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name White Balance Modifier
+ * \{ */
+
+static void whiteBalance_init_data(SequenceModifierData *smd)
+{
+ WhiteBalanceModifierData *cbmd = (WhiteBalanceModifierData *)smd;
+ copy_v3_fl(cbmd->white_value, 1.0f);
+}
+
+typedef struct WhiteBalanceThreadData {
+ float white[3];
+} WhiteBalanceThreadData;
+
+static void whiteBalance_apply_threaded(int width,
+ int height,
+ unsigned char *rect,
+ float *rect_float,
+ unsigned char *mask_rect,
+ const float *mask_rect_float,
+ void *data_v)
+{
+ int x, y;
+ float multiplier[3];
+
+ WhiteBalanceThreadData *data = (WhiteBalanceThreadData *)data_v;
+
+ multiplier[0] = (data->white[0] != 0.0f) ? 1.0f / data->white[0] : FLT_MAX;
+ multiplier[1] = (data->white[1] != 0.0f) ? 1.0f / data->white[1] : FLT_MAX;
+ multiplier[2] = (data->white[2] != 0.0f) ? 1.0f / data->white[2] : FLT_MAX;
+
+ for (y = 0; y < height; y++) {
+ for (x = 0; x < width; x++) {
+ int pixel_index = (y * width + x) * 4;
+ float rgba[4], result[4], mask[3] = {1.0f, 1.0f, 1.0f};
+
+ if (rect_float) {
+ copy_v3_v3(rgba, rect_float + pixel_index);
+ }
+ else {
+ straight_uchar_to_premul_float(rgba, rect + pixel_index);
+ }
+
+ copy_v4_v4(result, rgba);
+#if 0
+ mul_v3_v3(result, multiplier);
+#else
+ /* similar to division without the clipping */
+ for (int i = 0; i < 3; i++) {
+ result[i] = 1.0f - powf(1.0f - rgba[i], multiplier[i]);
+ }
+#endif
+
+ if (mask_rect_float) {
+ copy_v3_v3(mask, mask_rect_float + pixel_index);
+ }
+ else if (mask_rect) {
+ rgb_uchar_to_float(mask, mask_rect + pixel_index);
+ }
+
+ result[0] = rgba[0] * (1.0f - mask[0]) + result[0] * mask[0];
+ result[1] = rgba[1] * (1.0f - mask[1]) + result[1] * mask[1];
+ result[2] = rgba[2] * (1.0f - mask[2]) + result[2] * mask[2];
+
+ if (rect_float) {
+ copy_v3_v3(rect_float + pixel_index, result);
+ }
+ else {
+ premul_float_to_straight_uchar(rect + pixel_index, result);
+ }
+ }
+ }
+}
+
+static void whiteBalance_apply(SequenceModifierData *smd, ImBuf *ibuf, ImBuf *mask)
+{
+ WhiteBalanceThreadData data;
+ WhiteBalanceModifierData *wbmd = (WhiteBalanceModifierData *)smd;
+
+ copy_v3_v3(data.white, wbmd->white_value);
+
+ modifier_apply_threaded(ibuf, mask, whiteBalance_apply_threaded, &data);
+}
+
+static SequenceModifierTypeInfo seqModifier_WhiteBalance = {
+ CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "White Balance"), /* name */
+ "WhiteBalanceModifierData", /* struct_name */
+ sizeof(WhiteBalanceModifierData), /* struct_size */
+ whiteBalance_init_data, /* init_data */
+ NULL, /* free_data */
+ NULL, /* copy_data */
+ whiteBalance_apply, /* apply */
+};
+
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Curves Modifier
+ * \{ */
+
+static void curves_init_data(SequenceModifierData *smd)
+{
+ CurvesModifierData *cmd = (CurvesModifierData *)smd;
+
+ BKE_curvemapping_set_defaults(&cmd->curve_mapping, 4, 0.0f, 0.0f, 1.0f, 1.0f);
+}
+
+static void curves_free_data(SequenceModifierData *smd)
+{
+ CurvesModifierData *cmd = (CurvesModifierData *)smd;
+
+ BKE_curvemapping_free_data(&cmd->curve_mapping);
+}
+
+static void curves_copy_data(SequenceModifierData *target, SequenceModifierData *smd)
+{
+ CurvesModifierData *cmd = (CurvesModifierData *)smd;
+ CurvesModifierData *cmd_target = (CurvesModifierData *)target;
+
+ BKE_curvemapping_copy_data(&cmd_target->curve_mapping, &cmd->curve_mapping);
+}
+
+static void curves_apply_threaded(int width,
+ int height,
+ unsigned char *rect,
+ float *rect_float,
+ unsigned char *mask_rect,
+ const float *mask_rect_float,
+ void *data_v)
+{
+ CurveMapping *curve_mapping = (CurveMapping *)data_v;
+ int x, y;
+
+ for (y = 0; y < height; y++) {
+ for (x = 0; x < width; x++) {
+ int pixel_index = (y * width + x) * 4;
+
+ if (rect_float) {
+ float *pixel = rect_float + pixel_index;
+ float result[3];
+
+ BKE_curvemapping_evaluate_premulRGBF(curve_mapping, result, pixel);
+
+ if (mask_rect_float) {
+ const float *m = mask_rect_float + pixel_index;
+
+ pixel[0] = pixel[0] * (1.0f - m[0]) + result[0] * m[0];
+ pixel[1] = pixel[1] * (1.0f - m[1]) + result[1] * m[1];
+ pixel[2] = pixel[2] * (1.0f - m[2]) + result[2] * m[2];
+ }
+ else {
+ pixel[0] = result[0];
+ pixel[1] = result[1];
+ pixel[2] = result[2];
+ }
+ }
+ if (rect) {
+ unsigned char *pixel = rect + pixel_index;
+ float result[3], tempc[4];
+
+ straight_uchar_to_premul_float(tempc, pixel);
+
+ BKE_curvemapping_evaluate_premulRGBF(curve_mapping, result, tempc);
+
+ if (mask_rect) {
+ float t[3];
+
+ rgb_uchar_to_float(t, mask_rect + pixel_index);
+
+ tempc[0] = tempc[0] * (1.0f - t[0]) + result[0] * t[0];
+ tempc[1] = tempc[1] * (1.0f - t[1]) + result[1] * t[1];
+ tempc[2] = tempc[2] * (1.0f - t[2]) + result[2] * t[2];
+ }
+ else {
+ tempc[0] = result[0];
+ tempc[1] = result[1];
+ tempc[2] = result[2];
+ }
+
+ premul_float_to_straight_uchar(pixel, tempc);
+ }
+ }
+ }
+}
+
+static void curves_apply(struct SequenceModifierData *smd, ImBuf *ibuf, ImBuf *mask)
+{
+ CurvesModifierData *cmd = (CurvesModifierData *)smd;
+
+ const float black[3] = {0.0f, 0.0f, 0.0f};
+ const float white[3] = {1.0f, 1.0f, 1.0f};
+
+ BKE_curvemapping_init(&cmd->curve_mapping);
+
+ BKE_curvemapping_premultiply(&cmd->curve_mapping, 0);
+ BKE_curvemapping_set_black_white(&cmd->curve_mapping, black, white);
+
+ modifier_apply_threaded(ibuf, mask, curves_apply_threaded, &cmd->curve_mapping);
+
+ BKE_curvemapping_premultiply(&cmd->curve_mapping, 1);
+}
+
+static SequenceModifierTypeInfo seqModifier_Curves = {
+ CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "Curves"), /* name */
+ "CurvesModifierData", /* struct_name */
+ sizeof(CurvesModifierData), /* struct_size */
+ curves_init_data, /* init_data */
+ curves_free_data, /* free_data */
+ curves_copy_data, /* copy_data */
+ curves_apply, /* apply */
+};
+
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Hue Correct Modifier
+ * \{ */
+
+static void hue_correct_init_data(SequenceModifierData *smd)
+{
+ HueCorrectModifierData *hcmd = (HueCorrectModifierData *)smd;
+ int c;
+
+ BKE_curvemapping_set_defaults(&hcmd->curve_mapping, 1, 0.0f, 0.0f, 1.0f, 1.0f);
+ hcmd->curve_mapping.preset = CURVE_PRESET_MID9;
+
+ for (c = 0; c < 3; c++) {
+ CurveMap *cuma = &hcmd->curve_mapping.cm[c];
+
+ BKE_curvemap_reset(
+ cuma, &hcmd->curve_mapping.clipr, hcmd->curve_mapping.preset, CURVEMAP_SLOPE_POSITIVE);
+ }
+
+ /* default to showing Saturation */
+ hcmd->curve_mapping.cur = 1;
+}
+
+static void hue_correct_free_data(SequenceModifierData *smd)
+{
+ HueCorrectModifierData *hcmd = (HueCorrectModifierData *)smd;
+
+ BKE_curvemapping_free_data(&hcmd->curve_mapping);
+}
+
+static void hue_correct_copy_data(SequenceModifierData *target, SequenceModifierData *smd)
+{
+ HueCorrectModifierData *hcmd = (HueCorrectModifierData *)smd;
+ HueCorrectModifierData *hcmd_target = (HueCorrectModifierData *)target;
+
+ BKE_curvemapping_copy_data(&hcmd_target->curve_mapping, &hcmd->curve_mapping);
+}
+
+static void hue_correct_apply_threaded(int width,
+ int height,
+ unsigned char *rect,
+ float *rect_float,
+ unsigned char *mask_rect,
+ const float *mask_rect_float,
+ void *data_v)
+{
+ CurveMapping *curve_mapping = (CurveMapping *)data_v;
+ int x, y;
+
+ for (y = 0; y < height; y++) {
+ for (x = 0; x < width; x++) {
+ int pixel_index = (y * width + x) * 4;
+ float pixel[3], result[3], mask[3] = {1.0f, 1.0f, 1.0f};
+ float hsv[3], f;
+
+ if (rect_float) {
+ copy_v3_v3(pixel, rect_float + pixel_index);
+ }
+ else {
+ rgb_uchar_to_float(pixel, rect + pixel_index);
+ }
+
+ rgb_to_hsv(pixel[0], pixel[1], pixel[2], hsv, hsv + 1, hsv + 2);
+
+ /* adjust hue, scaling returned default 0.5 up to 1 */
+ f = BKE_curvemapping_evaluateF(curve_mapping, 0, hsv[0]);
+ hsv[0] += f - 0.5f;
+
+ /* adjust saturation, scaling returned default 0.5 up to 1 */
+ f = BKE_curvemapping_evaluateF(curve_mapping, 1, hsv[0]);
+ hsv[1] *= (f * 2.0f);
+
+ /* adjust value, scaling returned default 0.5 up to 1 */
+ f = BKE_curvemapping_evaluateF(curve_mapping, 2, hsv[0]);
+ hsv[2] *= (f * 2.f);
+
+ hsv[0] = hsv[0] - floorf(hsv[0]); /* mod 1.0 */
+ CLAMP(hsv[1], 0.0f, 1.0f);
+
+ /* convert back to rgb */
+ hsv_to_rgb(hsv[0], hsv[1], hsv[2], result, result + 1, result + 2);
+
+ if (mask_rect_float) {
+ copy_v3_v3(mask, mask_rect_float + pixel_index);
+ }
+ else if (mask_rect) {
+ rgb_uchar_to_float(mask, mask_rect + pixel_index);
+ }
+
+ result[0] = pixel[0] * (1.0f - mask[0]) + result[0] * mask[0];
+ result[1] = pixel[1] * (1.0f - mask[1]) + result[1] * mask[1];
+ result[2] = pixel[2] * (1.0f - mask[2]) + result[2] * mask[2];
+
+ if (rect_float) {
+ copy_v3_v3(rect_float + pixel_index, result);
+ }
+ else {
+ rgb_float_to_uchar(rect + pixel_index, result);
+ }
+ }
+ }
+}
+
+static void hue_correct_apply(struct SequenceModifierData *smd, ImBuf *ibuf, ImBuf *mask)
+{
+ HueCorrectModifierData *hcmd = (HueCorrectModifierData *)smd;
+
+ BKE_curvemapping_init(&hcmd->curve_mapping);
+
+ modifier_apply_threaded(ibuf, mask, hue_correct_apply_threaded, &hcmd->curve_mapping);
+}
+
+static SequenceModifierTypeInfo seqModifier_HueCorrect = {
+ CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "Hue Correct"), /* name */
+ "HueCorrectModifierData", /* struct_name */
+ sizeof(HueCorrectModifierData), /* struct_size */
+ hue_correct_init_data, /* init_data */
+ hue_correct_free_data, /* free_data */
+ hue_correct_copy_data, /* copy_data */
+ hue_correct_apply, /* apply */
+};
+
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Bright/Contrast Modifier
+ * \{ */
+
+typedef struct BrightContrastThreadData {
+ float bright;
+ float contrast;
+} BrightContrastThreadData;
+
+static void brightcontrast_apply_threaded(int width,
+ int height,
+ unsigned char *rect,
+ float *rect_float,
+ unsigned char *mask_rect,
+ const float *mask_rect_float,
+ void *data_v)
+{
+ BrightContrastThreadData *data = (BrightContrastThreadData *)data_v;
+ int x, y;
+
+ float i;
+ int c;
+ float a, b, v;
+ float brightness = data->bright / 100.0f;
+ float contrast = data->contrast;
+ float delta = contrast / 200.0f;
+ /*
+ * The algorithm is by Werner D. Streidt
+ * (http://visca.com/ffactory/archives/5-99/msg00021.html)
+ * Extracted of OpenCV demhist.c
+ */
+ if (contrast > 0) {
+ a = 1.0f - delta * 2.0f;
+ a = 1.0f / max_ff(a, FLT_EPSILON);
+ b = a * (brightness - delta);
+ }
+ else {
+ delta *= -1;
+ a = max_ff(1.0f - delta * 2.0f, 0.0f);
+ b = a * brightness + delta;
+ }
+
+ for (y = 0; y < height; y++) {
+ for (x = 0; x < width; x++) {
+ int pixel_index = (y * width + x) * 4;
+
+ if (rect) {
+ unsigned char *pixel = rect + pixel_index;
+
+ for (c = 0; c < 3; c++) {
+ i = (float)pixel[c] / 255.0f;
+ v = a * i + b;
+
+ if (mask_rect) {
+ unsigned char *m = mask_rect + pixel_index;
+ float t = (float)m[c] / 255.0f;
+
+ v = (float)pixel[c] / 255.0f * (1.0f - t) + v * t;
+ }
+
+ pixel[c] = unit_float_to_uchar_clamp(v);
+ }
+ }
+ else if (rect_float) {
+ float *pixel = rect_float + pixel_index;
+
+ for (c = 0; c < 3; c++) {
+ i = pixel[c];
+ v = a * i + b;
+
+ if (mask_rect_float) {
+ const float *m = mask_rect_float + pixel_index;
+
+ pixel[c] = pixel[c] * (1.0f - m[c]) + v * m[c];
+ }
+ else {
+ pixel[c] = v;
+ }
+ }
+ }
+ }
+ }
+}
+
+static void brightcontrast_apply(struct SequenceModifierData *smd, ImBuf *ibuf, ImBuf *mask)
+{
+ BrightContrastModifierData *bcmd = (BrightContrastModifierData *)smd;
+ BrightContrastThreadData data;
+
+ data.bright = bcmd->bright;
+ data.contrast = bcmd->contrast;
+
+ modifier_apply_threaded(ibuf, mask, brightcontrast_apply_threaded, &data);
+}
+
+static SequenceModifierTypeInfo seqModifier_BrightContrast = {
+ CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "Bright/Contrast"), /* name */
+ "BrightContrastModifierData", /* struct_name */
+ sizeof(BrightContrastModifierData), /* struct_size */
+ NULL, /* init_data */
+ NULL, /* free_data */
+ NULL, /* copy_data */
+ brightcontrast_apply, /* apply */
+};
+
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Mask Modifier
+ * \{ */
+
+static void maskmodifier_apply_threaded(int width,
+ int height,
+ unsigned char *rect,
+ float *rect_float,
+ unsigned char *mask_rect,
+ const float *mask_rect_float,
+ void *UNUSED(data_v))
+{
+ int x, y;
+
+ if (rect && !mask_rect) {
+ return;
+ }
+
+ if (rect_float && !mask_rect_float) {
+ return;
+ }
+
+ for (y = 0; y < height; y++) {
+ for (x = 0; x < width; x++) {
+ int pixel_index = (y * width + x) * 4;
+
+ if (rect) {
+ unsigned char *pixel = rect + pixel_index;
+ unsigned char *mask_pixel = mask_rect + pixel_index;
+ unsigned char mask = min_iii(mask_pixel[0], mask_pixel[1], mask_pixel[2]);
+
+ /* byte buffer is straight, so only affect on alpha itself,
+ * this is the only way to alpha-over byte strip after
+ * applying mask modifier.
+ */
+ pixel[3] = (float)(pixel[3] * mask) / 255.0f;
+ }
+ else if (rect_float) {
+ int c;
+ float *pixel = rect_float + pixel_index;
+ const float *mask_pixel = mask_rect_float + pixel_index;
+ float mask = min_fff(mask_pixel[0], mask_pixel[1], mask_pixel[2]);
+
+ /* float buffers are premultiplied, so need to premul color
+ * as well to make it easy to alpha-over masted strip.
+ */
+ for (c = 0; c < 4; c++) {
+ pixel[c] = pixel[c] * mask;
+ }
+ }
+ }
+ }
+}
+
+static void maskmodifier_apply(struct SequenceModifierData *UNUSED(smd), ImBuf *ibuf, ImBuf *mask)
+{
+ // SequencerMaskModifierData *bcmd = (SequencerMaskModifierData *)smd;
+
+ modifier_apply_threaded(ibuf, mask, maskmodifier_apply_threaded, NULL);
+}
+
+static SequenceModifierTypeInfo seqModifier_Mask = {
+ CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "Mask"), /* name */
+ "SequencerMaskModifierData", /* struct_name */
+ sizeof(SequencerMaskModifierData), /* struct_size */
+ NULL, /* init_data */
+ NULL, /* free_data */
+ NULL, /* copy_data */
+ maskmodifier_apply, /* apply */
+};
+
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Tonemap Modifier
+ * \{ */
+
+typedef struct AvgLogLum {
+ SequencerTonemapModifierData *tmmd;
+ struct ColorSpace *colorspace;
+ float al;
+ float auto_key;
+ float lav;
+ float cav[4];
+ float igm;
+} AvgLogLum;
+
+static void tonemapmodifier_init_data(SequenceModifierData *smd)
+{
+ SequencerTonemapModifierData *tmmd = (SequencerTonemapModifierData *)smd;
+ /* Same as tonemap compositor node. */
+ tmmd->type = SEQ_TONEMAP_RD_PHOTORECEPTOR;
+ tmmd->key = 0.18f;
+ tmmd->offset = 1.0f;
+ tmmd->gamma = 1.0f;
+ tmmd->intensity = 0.0f;
+ tmmd->contrast = 0.0f;
+ tmmd->adaptation = 1.0f;
+ tmmd->correction = 0.0f;
+}
+
+static void tonemapmodifier_apply_threaded_simple(int width,
+ int height,
+ unsigned char *rect,
+ float *rect_float,
+ unsigned char *mask_rect,
+ const float *mask_rect_float,
+ void *data_v)
+{
+ AvgLogLum *avg = (AvgLogLum *)data_v;
+ for (int y = 0; y < height; y++) {
+ for (int x = 0; x < width; x++) {
+ int pixel_index = (y * width + x) * 4;
+ float input[4], output[4], mask[3] = {1.0f, 1.0f, 1.0f};
+ /* Get input value. */
+ if (rect_float) {
+ copy_v4_v4(input, &rect_float[pixel_index]);
+ }
+ else {
+ straight_uchar_to_premul_float(input, &rect[pixel_index]);
+ }
+ IMB_colormanagement_colorspace_to_scene_linear_v3(input, avg->colorspace);
+ copy_v4_v4(output, input);
+ /* Get mask value. */
+ if (mask_rect_float) {
+ copy_v3_v3(mask, mask_rect_float + pixel_index);
+ }
+ else if (mask_rect) {
+ rgb_uchar_to_float(mask, mask_rect + pixel_index);
+ }
+ /* Apply correction. */
+ mul_v3_fl(output, avg->al);
+ float dr = output[0] + avg->tmmd->offset;
+ float dg = output[1] + avg->tmmd->offset;
+ float db = output[2] + avg->tmmd->offset;
+ output[0] /= ((dr == 0.0f) ? 1.0f : dr);
+ output[1] /= ((dg == 0.0f) ? 1.0f : dg);
+ output[2] /= ((db == 0.0f) ? 1.0f : db);
+ const float igm = avg->igm;
+ if (igm != 0.0f) {
+ output[0] = powf(max_ff(output[0], 0.0f), igm);
+ output[1] = powf(max_ff(output[1], 0.0f), igm);
+ output[2] = powf(max_ff(output[2], 0.0f), igm);
+ }
+ /* Apply mask. */
+ output[0] = input[0] * (1.0f - mask[0]) + output[0] * mask[0];
+ output[1] = input[1] * (1.0f - mask[1]) + output[1] * mask[1];
+ output[2] = input[2] * (1.0f - mask[2]) + output[2] * mask[2];
+ /* Copy result back. */
+ IMB_colormanagement_scene_linear_to_colorspace_v3(output, avg->colorspace);
+ if (rect_float) {
+ copy_v4_v4(&rect_float[pixel_index], output);
+ }
+ else {
+ premul_float_to_straight_uchar(&rect[pixel_index], output);
+ }
+ }
+ }
+}
+
+static void tonemapmodifier_apply_threaded_photoreceptor(int width,
+ int height,
+ unsigned char *rect,
+ float *rect_float,
+ unsigned char *mask_rect,
+ const float *mask_rect_float,
+ void *data_v)
+{
+ AvgLogLum *avg = (AvgLogLum *)data_v;
+ const float f = expf(-avg->tmmd->intensity);
+ const float m = (avg->tmmd->contrast > 0.0f) ? avg->tmmd->contrast :
+ (0.3f + 0.7f * powf(avg->auto_key, 1.4f));
+ const float ic = 1.0f - avg->tmmd->correction, ia = 1.0f - avg->tmmd->adaptation;
+ for (int y = 0; y < height; y++) {
+ for (int x = 0; x < width; x++) {
+ int pixel_index = (y * width + x) * 4;
+ float input[4], output[4], mask[3] = {1.0f, 1.0f, 1.0f};
+ /* Get input value. */
+ if (rect_float) {
+ copy_v4_v4(input, &rect_float[pixel_index]);
+ }
+ else {
+ straight_uchar_to_premul_float(input, &rect[pixel_index]);
+ }
+ IMB_colormanagement_colorspace_to_scene_linear_v3(input, avg->colorspace);
+ copy_v4_v4(output, input);
+ /* Get mask value. */
+ if (mask_rect_float) {
+ copy_v3_v3(mask, mask_rect_float + pixel_index);
+ }
+ else if (mask_rect) {
+ rgb_uchar_to_float(mask, mask_rect + pixel_index);
+ }
+ /* Apply correction. */
+ const float L = IMB_colormanagement_get_luminance(output);
+ float I_l = output[0] + ic * (L - output[0]);
+ float I_g = avg->cav[0] + ic * (avg->lav - avg->cav[0]);
+ float I_a = I_l + ia * (I_g - I_l);
+ output[0] /= (output[0] + powf(f * I_a, m));
+ I_l = output[1] + ic * (L - output[1]);
+ I_g = avg->cav[1] + ic * (avg->lav - avg->cav[1]);
+ I_a = I_l + ia * (I_g - I_l);
+ output[1] /= (output[1] + powf(f * I_a, m));
+ I_l = output[2] + ic * (L - output[2]);
+ I_g = avg->cav[2] + ic * (avg->lav - avg->cav[2]);
+ I_a = I_l + ia * (I_g - I_l);
+ output[2] /= (output[2] + powf(f * I_a, m));
+ /* Apply mask. */
+ output[0] = input[0] * (1.0f - mask[0]) + output[0] * mask[0];
+ output[1] = input[1] * (1.0f - mask[1]) + output[1] * mask[1];
+ output[2] = input[2] * (1.0f - mask[2]) + output[2] * mask[2];
+ /* Copy result back. */
+ IMB_colormanagement_scene_linear_to_colorspace_v3(output, avg->colorspace);
+ if (rect_float) {
+ copy_v4_v4(&rect_float[pixel_index], output);
+ }
+ else {
+ premul_float_to_straight_uchar(&rect[pixel_index], output);
+ }
+ }
+ }
+}
+
+static void tonemapmodifier_apply(struct SequenceModifierData *smd, ImBuf *ibuf, ImBuf *mask)
+{
+ SequencerTonemapModifierData *tmmd = (SequencerTonemapModifierData *)smd;
+ AvgLogLum data;
+ data.tmmd = tmmd;
+ data.colorspace = (ibuf->rect_float != NULL) ? ibuf->float_colorspace : ibuf->rect_colorspace;
+ float lsum = 0.0f;
+ int p = ibuf->x * ibuf->y;
+ float *fp = ibuf->rect_float;
+ unsigned char *cp = (unsigned char *)ibuf->rect;
+ float avl, maxl = -FLT_MAX, minl = FLT_MAX;
+ const float sc = 1.0f / p;
+ float Lav = 0.f;
+ float cav[4] = {0.0f, 0.0f, 0.0f, 0.0f};
+ while (p--) {
+ float pixel[4];
+ if (fp != NULL) {
+ copy_v4_v4(pixel, fp);
+ }
+ else {
+ straight_uchar_to_premul_float(pixel, cp);
+ }
+ IMB_colormanagement_colorspace_to_scene_linear_v3(pixel, data.colorspace);
+ float L = IMB_colormanagement_get_luminance(pixel);
+ Lav += L;
+ add_v3_v3(cav, pixel);
+ lsum += logf(max_ff(L, 0.0f) + 1e-5f);
+ maxl = (L > maxl) ? L : maxl;
+ minl = (L < minl) ? L : minl;
+ if (fp != NULL) {
+ fp += 4;
+ }
+ else {
+ cp += 4;
+ }
+ }
+ data.lav = Lav * sc;
+ mul_v3_v3fl(data.cav, cav, sc);
+ maxl = logf(maxl + 1e-5f);
+ minl = logf(minl + 1e-5f);
+ avl = lsum * sc;
+ data.auto_key = (maxl > minl) ? ((maxl - avl) / (maxl - minl)) : 1.0f;
+ float al = expf(avl);
+ data.al = (al == 0.0f) ? 0.0f : (tmmd->key / al);
+ data.igm = (tmmd->gamma == 0.0f) ? 1.0f : (1.0f / tmmd->gamma);
+
+ if (tmmd->type == SEQ_TONEMAP_RD_PHOTORECEPTOR) {
+ modifier_apply_threaded(ibuf, mask, tonemapmodifier_apply_threaded_photoreceptor, &data);
+ }
+ else /* if (tmmd->type == SEQ_TONEMAP_RD_SIMPLE) */ {
+ modifier_apply_threaded(ibuf, mask, tonemapmodifier_apply_threaded_simple, &data);
+ }
+}
+
+static SequenceModifierTypeInfo seqModifier_Tonemap = {
+ CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "Tonemap"), /* name */
+ "SequencerTonemapModifierData", /* struct_name */
+ sizeof(SequencerTonemapModifierData), /* struct_size */
+ tonemapmodifier_init_data, /* init_data */
+ NULL, /* free_data */
+ NULL, /* copy_data */
+ tonemapmodifier_apply, /* apply */
+};
+
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Public Modifier Functions
+ * \{ */
+
+static void sequence_modifier_type_info_init(void)
+{
+#define INIT_TYPE(typeName) (modifiersTypes[seqModifierType_##typeName] = &seqModifier_##typeName)
+
+ INIT_TYPE(ColorBalance);
+ INIT_TYPE(Curves);
+ INIT_TYPE(HueCorrect);
+ INIT_TYPE(BrightContrast);
+ INIT_TYPE(Mask);
+ INIT_TYPE(WhiteBalance);
+ INIT_TYPE(Tonemap);
+
+#undef INIT_TYPE
+}
+
+const SequenceModifierTypeInfo *BKE_sequence_modifier_type_info_get(int type)
+{
+ if (!modifierTypesInit) {
+ sequence_modifier_type_info_init();
+ modifierTypesInit = true;
+ }
+
+ return modifiersTypes[type];
+}
+
+SequenceModifierData *BKE_sequence_modifier_new(Sequence *seq, const char *name, int type)
+{
+ SequenceModifierData *smd;
+ const SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(type);
+
+ smd = MEM_callocN(smti->struct_size, "sequence modifier");
+
+ smd->type = type;
+ smd->flag |= SEQUENCE_MODIFIER_EXPANDED;
+
+ if (!name || !name[0]) {
+ BLI_strncpy(smd->name, smti->name, sizeof(smd->name));
+ }
+ else {
+ BLI_strncpy(smd->name, name, sizeof(smd->name));
+ }
+
+ BLI_addtail(&seq->modifiers, smd);
+
+ BKE_sequence_modifier_unique_name(seq, smd);
+
+ if (smti->init_data) {
+ smti->init_data(smd);
+ }
+
+ return smd;
+}
+
+bool BKE_sequence_modifier_remove(Sequence *seq, SequenceModifierData *smd)
+{
+ if (BLI_findindex(&seq->modifiers, smd) == -1) {
+ return false;
+ }
+
+ BLI_remlink(&seq->modifiers, smd);
+ BKE_sequence_modifier_free(smd);
+
+ return true;
+}
+
+void BKE_sequence_modifier_clear(Sequence *seq)
+{
+ SequenceModifierData *smd, *smd_next;
+
+ for (smd = seq->modifiers.first; smd; smd = smd_next) {
+ smd_next = smd->next;
+ BKE_sequence_modifier_free(smd);
+ }
+
+ BLI_listbase_clear(&seq->modifiers);
+}
+
+void BKE_sequence_modifier_free(SequenceModifierData *smd)
+{
+ const SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(smd->type);
+
+ if (smti && smti->free_data) {
+ smti->free_data(smd);
+ }
+
+ MEM_freeN(smd);
+}
+
+void BKE_sequence_modifier_unique_name(Sequence *seq, SequenceModifierData *smd)
+{
+ const SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(smd->type);
+
+ BLI_uniquename(&seq->modifiers,
+ smd,
+ CTX_DATA_(BLT_I18NCONTEXT_ID_SEQUENCE, smti->name),
+ '.',
+ offsetof(SequenceModifierData, name),
+ sizeof(smd->name));
+}
+
+SequenceModifierData *BKE_sequence_modifier_find_by_name(Sequence *seq, const char *name)
+{
+ return BLI_findstring(&(seq->modifiers), name, offsetof(SequenceModifierData, name));
+}
+
+ImBuf *BKE_sequence_modifier_apply_stack(const SeqRenderData *context,
+ Sequence *seq,
+ ImBuf *ibuf,
+ int cfra)
+{
+ SequenceModifierData *smd;
+ ImBuf *processed_ibuf = ibuf;
+
+ if (seq->modifiers.first && (seq->flag & SEQ_USE_LINEAR_MODIFIERS)) {
+ processed_ibuf = IMB_dupImBuf(ibuf);
+ BKE_sequencer_imbuf_from_sequencer_space(context->scene, processed_ibuf);
+ }
+
+ for (smd = seq->modifiers.first; smd; smd = smd->next) {
+ const SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(smd->type);
+
+ /* could happen if modifier is being removed or not exists in current version of blender */
+ if (!smti) {
+ continue;
+ }
+
+ /* modifier is muted, do nothing */
+ if (smd->flag & SEQUENCE_MODIFIER_MUTE) {
+ continue;
+ }
+
+ if (smti->apply) {
+ int frame_offset;
+ if (smd->mask_time == SEQUENCE_MASK_TIME_RELATIVE) {
+ frame_offset = seq->start;
+ }
+ else /*if (smd->mask_time == SEQUENCE_MASK_TIME_ABSOLUTE)*/ {
+ frame_offset = smd->mask_id ? ((Mask *)smd->mask_id)->sfra : 0;
+ }
+
+ ImBuf *mask = modifier_mask_get(smd, context, cfra, frame_offset, ibuf->rect_float != NULL);
+
+ if (processed_ibuf == ibuf) {
+ processed_ibuf = IMB_dupImBuf(ibuf);
+ }
+
+ smti->apply(smd, processed_ibuf, mask);
+
+ if (mask) {
+ IMB_freeImBuf(mask);
+ }
+ }
+ }
+
+ if (seq->modifiers.first && (seq->flag & SEQ_USE_LINEAR_MODIFIERS)) {
+ BKE_sequencer_imbuf_to_sequencer_space(context->scene, processed_ibuf, false);
+ }
+
+ return processed_ibuf;
+}
+
+void BKE_sequence_modifier_list_copy(Sequence *seqn, Sequence *seq)
+{
+ SequenceModifierData *smd;
+
+ for (smd = seq->modifiers.first; smd; smd = smd->next) {
+ SequenceModifierData *smdn;
+ const SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(smd->type);
+
+ smdn = MEM_dupallocN(smd);
+
+ if (smti && smti->copy_data) {
+ smti->copy_data(smdn, smd);
+ }
+
+ smdn->next = smdn->prev = NULL;
+ BLI_addtail(&seqn->modifiers, smdn);
+ }
+}
+
+int BKE_sequence_supports_modifiers(Sequence *seq)
+{
+ return !ELEM(seq->type, SEQ_TYPE_SOUND_RAM, SEQ_TYPE_SOUND_HD);
+}
+
+/** \} */
diff --git a/source/blender/sequencer/intern/prefetch.c b/source/blender/sequencer/intern/prefetch.c
new file mode 100644
index 00000000000..013abb716d4
--- /dev/null
+++ b/source/blender/sequencer/intern/prefetch.c
@@ -0,0 +1,568 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ */
+
+/** \file
+ * \ingroup bke
+ */
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "MEM_guardedalloc.h"
+
+#include "DNA_anim_types.h"
+#include "DNA_scene_types.h"
+#include "DNA_screen_types.h"
+#include "DNA_sequence_types.h"
+#include "DNA_windowmanager_types.h"
+
+#include "BLI_listbase.h"
+#include "BLI_threads.h"
+
+#include "IMB_imbuf.h"
+#include "IMB_imbuf_types.h"
+
+#include "BKE_anim_data.h"
+#include "BKE_animsys.h"
+#include "BKE_context.h"
+#include "BKE_global.h"
+#include "BKE_layer.h"
+#include "BKE_lib_id.h"
+#include "BKE_main.h"
+#include "BKE_scene.h"
+#include "BKE_sequencer.h"
+
+#include "DEG_depsgraph.h"
+#include "DEG_depsgraph_build.h"
+#include "DEG_depsgraph_debug.h"
+#include "DEG_depsgraph_query.h"
+
+typedef struct PrefetchJob {
+ struct PrefetchJob *next, *prev;
+
+ struct Main *bmain;
+ struct Main *bmain_eval;
+ struct Scene *scene;
+ struct Scene *scene_eval;
+ struct Depsgraph *depsgraph;
+
+ ThreadMutex prefetch_suspend_mutex;
+ ThreadCondition prefetch_suspend_cond;
+
+ ListBase threads;
+
+ /* context */
+ struct SeqRenderData context;
+ struct SeqRenderData context_cpy;
+ struct ListBase *seqbasep;
+ struct ListBase *seqbasep_cpy;
+
+ /* prefetch area */
+ float cfra;
+ int num_frames_prefetched;
+
+ /* control */
+ bool running;
+ bool waiting;
+ bool stop;
+} PrefetchJob;
+
+static bool seq_prefetch_is_playing(Main *bmain)
+{
+ for (bScreen *screen = bmain->screens.first; screen; screen = screen->id.next) {
+ if (screen->animtimer) {
+ return true;
+ }
+ }
+ return false;
+}
+
+static bool seq_prefetch_is_scrubbing(Main *bmain)
+{
+
+ for (bScreen *screen = bmain->screens.first; screen; screen = screen->id.next) {
+ if (screen->scrubbing) {
+ return true;
+ }
+ }
+ return false;
+}
+
+static PrefetchJob *seq_prefetch_job_get(Scene *scene)
+{
+ if (scene && scene->ed) {
+ return scene->ed->prefetch_job;
+ }
+ return NULL;
+}
+
+bool BKE_sequencer_prefetch_job_is_running(Scene *scene)
+{
+ PrefetchJob *pfjob = seq_prefetch_job_get(scene);
+
+ if (!pfjob) {
+ return false;
+ }
+
+ return pfjob->running;
+}
+
+static bool seq_prefetch_job_is_waiting(Scene *scene)
+{
+ PrefetchJob *pfjob = seq_prefetch_job_get(scene);
+
+ if (!pfjob) {
+ return false;
+ }
+
+ return pfjob->waiting;
+}
+
+static Sequence *sequencer_prefetch_get_original_sequence(Sequence *seq, ListBase *seqbase)
+{
+ LISTBASE_FOREACH (Sequence *, seq_orig, seqbase) {
+ if (STREQ(seq->name, seq_orig->name)) {
+ return seq_orig;
+ }
+
+ if (seq_orig->type == SEQ_TYPE_META) {
+ Sequence *match = sequencer_prefetch_get_original_sequence(seq, &seq_orig->seqbase);
+ if (match != NULL) {
+ return match;
+ }
+ }
+ }
+
+ return NULL;
+}
+
+/* for cache context swapping */
+Sequence *BKE_sequencer_prefetch_get_original_sequence(Sequence *seq, Scene *scene)
+{
+ Editing *ed = scene->ed;
+ return sequencer_prefetch_get_original_sequence(seq, &ed->seqbase);
+}
+
+/* for cache context swapping */
+SeqRenderData *BKE_sequencer_prefetch_get_original_context(const SeqRenderData *context)
+{
+ PrefetchJob *pfjob = seq_prefetch_job_get(context->scene);
+
+ return &pfjob->context;
+}
+
+static bool seq_prefetch_is_cache_full(Scene *scene)
+{
+ PrefetchJob *pfjob = seq_prefetch_job_get(scene);
+
+ if (!BKE_sequencer_cache_is_full(pfjob->scene)) {
+ return false;
+ }
+
+ return BKE_sequencer_cache_recycle_item(pfjob->scene) == false;
+}
+
+static float seq_prefetch_cfra(PrefetchJob *pfjob)
+{
+ return pfjob->cfra + pfjob->num_frames_prefetched;
+}
+static AnimationEvalContext seq_prefetch_anim_eval_context(PrefetchJob *pfjob)
+{
+ return BKE_animsys_eval_context_construct(pfjob->depsgraph, seq_prefetch_cfra(pfjob));
+}
+
+void BKE_sequencer_prefetch_get_time_range(Scene *scene, int *start, int *end)
+{
+ PrefetchJob *pfjob = seq_prefetch_job_get(scene);
+
+ *start = pfjob->cfra;
+ *end = seq_prefetch_cfra(pfjob);
+}
+
+static void seq_prefetch_free_depsgraph(PrefetchJob *pfjob)
+{
+ if (pfjob->depsgraph != NULL) {
+ DEG_graph_free(pfjob->depsgraph);
+ }
+ pfjob->depsgraph = NULL;
+ pfjob->scene_eval = NULL;
+}
+
+static void seq_prefetch_update_depsgraph(PrefetchJob *pfjob)
+{
+ DEG_evaluate_on_framechange(pfjob->depsgraph, seq_prefetch_cfra(pfjob));
+}
+
+static void seq_prefetch_init_depsgraph(PrefetchJob *pfjob)
+{
+ Main *bmain = pfjob->bmain_eval;
+ Scene *scene = pfjob->scene;
+ ViewLayer *view_layer = BKE_view_layer_default_render(scene);
+
+ pfjob->depsgraph = DEG_graph_new(bmain, scene, view_layer, DAG_EVAL_RENDER);
+ DEG_debug_name_set(pfjob->depsgraph, "SEQUENCER PREFETCH");
+
+ /* Make sure there is a correct evaluated scene pointer. */
+ DEG_graph_build_for_render_pipeline(pfjob->depsgraph);
+
+ /* Update immediately so we have proper evaluated scene. */
+ seq_prefetch_update_depsgraph(pfjob);
+
+ pfjob->scene_eval = DEG_get_evaluated_scene(pfjob->depsgraph);
+ pfjob->scene_eval->ed->cache_flag = 0;
+}
+
+static void seq_prefetch_update_area(PrefetchJob *pfjob)
+{
+ int cfra = pfjob->scene->r.cfra;
+
+ /* rebase */
+ if (cfra > pfjob->cfra) {
+ int delta = cfra - pfjob->cfra;
+ pfjob->cfra = cfra;
+ pfjob->num_frames_prefetched -= delta;
+
+ if (pfjob->num_frames_prefetched <= 1) {
+ pfjob->num_frames_prefetched = 1;
+ }
+ }
+
+ /* reset */
+ if (cfra < pfjob->cfra) {
+ pfjob->cfra = cfra;
+ pfjob->num_frames_prefetched = 1;
+ }
+}
+
+void BKE_sequencer_prefetch_stop_all(void)
+{
+ /*TODO(Richard): Use wm_jobs for prefetch, or pass main. */
+ for (Scene *scene = G.main->scenes.first; scene; scene = scene->id.next) {
+ BKE_sequencer_prefetch_stop(scene);
+ }
+}
+
+/* Use also to update scene and context changes
+ * This function should almost always be called by cache invalidation, not directly.
+ */
+void BKE_sequencer_prefetch_stop(Scene *scene)
+{
+ PrefetchJob *pfjob;
+ pfjob = seq_prefetch_job_get(scene);
+
+ if (!pfjob) {
+ return;
+ }
+
+ pfjob->stop = true;
+
+ while (pfjob->running) {
+ BLI_condition_notify_one(&pfjob->prefetch_suspend_cond);
+ }
+}
+
+static void seq_prefetch_update_context(const SeqRenderData *context)
+{
+ PrefetchJob *pfjob;
+ pfjob = seq_prefetch_job_get(context->scene);
+
+ BKE_sequencer_new_render_data(pfjob->bmain_eval,
+ pfjob->depsgraph,
+ pfjob->scene_eval,
+ context->rectx,
+ context->recty,
+ context->preview_render_size,
+ false,
+ &pfjob->context_cpy);
+ pfjob->context_cpy.is_prefetch_render = true;
+ pfjob->context_cpy.task_id = SEQ_TASK_PREFETCH_RENDER;
+
+ BKE_sequencer_new_render_data(pfjob->bmain,
+ pfjob->depsgraph,
+ pfjob->scene,
+ context->rectx,
+ context->recty,
+ context->preview_render_size,
+ false,
+ &pfjob->context);
+ pfjob->context.is_prefetch_render = false;
+
+ /* Same ID as prefetch context, because context will be swapped, but we still
+ * want to assign this ID to cache entries created in this thread.
+ * This is to allow "temp cache" work correctly for both threads.
+ */
+ pfjob->context.task_id = SEQ_TASK_PREFETCH_RENDER;
+}
+
+static void seq_prefetch_update_scene(Scene *scene)
+{
+ PrefetchJob *pfjob = seq_prefetch_job_get(scene);
+
+ if (!pfjob) {
+ return;
+ }
+
+ seq_prefetch_free_depsgraph(pfjob);
+ seq_prefetch_init_depsgraph(pfjob);
+}
+
+static void seq_prefetch_resume(Scene *scene)
+{
+ PrefetchJob *pfjob = seq_prefetch_job_get(scene);
+
+ if (pfjob && pfjob->waiting) {
+ BLI_condition_notify_one(&pfjob->prefetch_suspend_cond);
+ }
+}
+
+void BKE_sequencer_prefetch_free(Scene *scene)
+{
+ PrefetchJob *pfjob = seq_prefetch_job_get(scene);
+ if (!pfjob) {
+ return;
+ }
+
+ BKE_sequencer_prefetch_stop(scene);
+
+ BLI_threadpool_remove(&pfjob->threads, pfjob);
+ BLI_threadpool_end(&pfjob->threads);
+ BLI_mutex_end(&pfjob->prefetch_suspend_mutex);
+ BLI_condition_end(&pfjob->prefetch_suspend_cond);
+ seq_prefetch_free_depsgraph(pfjob);
+ BKE_main_free(pfjob->bmain_eval);
+ MEM_freeN(pfjob);
+ scene->ed->prefetch_job = NULL;
+}
+
+static bool seq_prefetch_do_skip_frame(Scene *scene)
+{
+ Editing *ed = scene->ed;
+ PrefetchJob *pfjob = seq_prefetch_job_get(scene);
+ float cfra = seq_prefetch_cfra(pfjob);
+ Sequence *seq_arr[MAXSEQ + 1];
+ int count = BKE_sequencer_get_shown_sequences(ed->seqbasep, cfra, 0, seq_arr);
+ SeqRenderData *ctx = &pfjob->context_cpy;
+ ImBuf *ibuf = NULL;
+
+ /* Disable prefetching 3D scene strips, but check for disk cache. */
+ for (int i = 0; i < count; i++) {
+ if (seq_arr[i]->type == SEQ_TYPE_SCENE && (seq_arr[i]->flag & SEQ_SCENE_STRIPS) == 0) {
+ int cached_types = 0;
+
+ ibuf = BKE_sequencer_cache_get(ctx, seq_arr[i], cfra, SEQ_CACHE_STORE_FINAL_OUT, false);
+ if (ibuf != NULL) {
+ cached_types |= SEQ_CACHE_STORE_FINAL_OUT;
+ IMB_freeImBuf(ibuf);
+ ibuf = NULL;
+ }
+
+ ibuf = BKE_sequencer_cache_get(ctx, seq_arr[i], cfra, SEQ_CACHE_STORE_FINAL_OUT, false);
+ if (ibuf != NULL) {
+ cached_types |= SEQ_CACHE_STORE_COMPOSITE;
+ IMB_freeImBuf(ibuf);
+ ibuf = NULL;
+ }
+
+ ibuf = BKE_sequencer_cache_get(ctx, seq_arr[i], cfra, SEQ_CACHE_STORE_PREPROCESSED, false);
+ if (ibuf != NULL) {
+ cached_types |= SEQ_CACHE_STORE_PREPROCESSED;
+ IMB_freeImBuf(ibuf);
+ ibuf = NULL;
+ }
+
+ ibuf = BKE_sequencer_cache_get(ctx, seq_arr[i], cfra, SEQ_CACHE_STORE_RAW, false);
+ if (ibuf != NULL) {
+ cached_types |= SEQ_CACHE_STORE_RAW;
+ IMB_freeImBuf(ibuf);
+ ibuf = NULL;
+ }
+
+ if ((cached_types & (SEQ_CACHE_STORE_RAW | SEQ_CACHE_STORE_PREPROCESSED)) != 0) {
+ continue;
+ }
+
+ /* It is only safe to use these cache types if strip is last in stack. */
+ if (i == count - 1 &&
+ (cached_types & (SEQ_CACHE_STORE_PREPROCESSED | SEQ_CACHE_STORE_RAW)) != 0) {
+ continue;
+ }
+
+ return true;
+ }
+ }
+
+ return false;
+}
+
+static bool seq_prefetch_need_suspend(PrefetchJob *pfjob)
+{
+ return seq_prefetch_is_cache_full(pfjob->scene) || seq_prefetch_is_scrubbing(pfjob->bmain) ||
+ (seq_prefetch_cfra(pfjob) >= pfjob->scene->r.efra);
+}
+
+static void seq_prefetch_do_suspend(PrefetchJob *pfjob)
+{
+ BLI_mutex_lock(&pfjob->prefetch_suspend_mutex);
+ while (seq_prefetch_need_suspend(pfjob) &&
+ (pfjob->scene->ed->cache_flag & SEQ_CACHE_PREFETCH_ENABLE) && !pfjob->stop) {
+ pfjob->waiting = true;
+ BLI_condition_wait(&pfjob->prefetch_suspend_cond, &pfjob->prefetch_suspend_mutex);
+ seq_prefetch_update_area(pfjob);
+ }
+ pfjob->waiting = false;
+ BLI_mutex_unlock(&pfjob->prefetch_suspend_mutex);
+}
+
+static void *seq_prefetch_frames(void *job)
+{
+ PrefetchJob *pfjob = (PrefetchJob *)job;
+
+ while (seq_prefetch_cfra(pfjob) <= pfjob->scene->r.efra) {
+ pfjob->scene_eval->ed->prefetch_job = NULL;
+
+ seq_prefetch_update_depsgraph(pfjob);
+ AnimData *adt = BKE_animdata_from_id(&pfjob->context_cpy.scene->id);
+ AnimationEvalContext anim_eval_context = seq_prefetch_anim_eval_context(pfjob);
+ BKE_animsys_evaluate_animdata(
+ &pfjob->context_cpy.scene->id, adt, &anim_eval_context, ADT_RECALC_ALL, false);
+
+ /* This is quite hacky solution:
+ * We need cross-reference original scene with copy for cache.
+ * However depsgraph must not have this data, because it will try to kill this job.
+ * Scene copy don't reference original scene. Perhaps, this could be done by depsgraph.
+ * Set to NULL before return!
+ */
+ pfjob->scene_eval->ed->prefetch_job = pfjob;
+
+ if (seq_prefetch_do_skip_frame(pfjob->scene)) {
+ pfjob->num_frames_prefetched++;
+ continue;
+ }
+
+ ImBuf *ibuf = BKE_sequencer_give_ibuf(&pfjob->context_cpy, seq_prefetch_cfra(pfjob), 0);
+ BKE_sequencer_cache_free_temp_cache(
+ pfjob->scene, pfjob->context.task_id, seq_prefetch_cfra(pfjob));
+ IMB_freeImBuf(ibuf);
+
+ /* Suspend thread if there is nothing to be prefetched. */
+ seq_prefetch_do_suspend(pfjob);
+
+ /* Avoid "collision" with main thread, but make sure to fetch at least few frames */
+ if (pfjob->num_frames_prefetched > 5 &&
+ (seq_prefetch_cfra(pfjob) - pfjob->scene->r.cfra) < 2) {
+ break;
+ }
+
+ if (!(pfjob->scene->ed->cache_flag & SEQ_CACHE_PREFETCH_ENABLE) || pfjob->stop) {
+ break;
+ }
+
+ seq_prefetch_update_area(pfjob);
+ pfjob->num_frames_prefetched++;
+ }
+
+ BKE_sequencer_cache_free_temp_cache(
+ pfjob->scene, pfjob->context.task_id, seq_prefetch_cfra(pfjob));
+ pfjob->running = false;
+ pfjob->scene_eval->ed->prefetch_job = NULL;
+
+ return NULL;
+}
+
+static PrefetchJob *seq_prefetch_start(const SeqRenderData *context, float cfra)
+{
+ PrefetchJob *pfjob = seq_prefetch_job_get(context->scene);
+
+ if (!pfjob) {
+ if (context->scene->ed) {
+ pfjob = (PrefetchJob *)MEM_callocN(sizeof(PrefetchJob), "PrefetchJob");
+ context->scene->ed->prefetch_job = pfjob;
+
+ BLI_threadpool_init(&pfjob->threads, seq_prefetch_frames, 1);
+ BLI_mutex_init(&pfjob->prefetch_suspend_mutex);
+ BLI_condition_init(&pfjob->prefetch_suspend_cond);
+
+ pfjob->bmain = context->bmain;
+ pfjob->bmain_eval = BKE_main_new();
+
+ pfjob->scene = context->scene;
+ seq_prefetch_init_depsgraph(pfjob);
+ }
+ }
+ seq_prefetch_update_scene(context->scene);
+ seq_prefetch_update_context(context);
+
+ pfjob->cfra = cfra;
+ pfjob->num_frames_prefetched = 1;
+
+ pfjob->waiting = false;
+ pfjob->stop = false;
+ pfjob->running = true;
+
+ BLI_threadpool_remove(&pfjob->threads, pfjob);
+ BLI_threadpool_insert(&pfjob->threads, pfjob);
+
+ return pfjob;
+}
+
+/* Start or resume prefetching*/
+void BKE_sequencer_prefetch_start(const SeqRenderData *context, float cfra, float cost)
+{
+ Scene *scene = context->scene;
+ Editing *ed = scene->ed;
+ bool has_strips = (bool)ed->seqbasep->first;
+
+ if (!context->is_prefetch_render && !context->is_proxy_render) {
+ bool playing = seq_prefetch_is_playing(context->bmain);
+ bool scrubbing = seq_prefetch_is_scrubbing(context->bmain);
+ bool running = BKE_sequencer_prefetch_job_is_running(scene);
+ seq_prefetch_resume(scene);
+ /* conditions to start:
+ * prefetch enabled, prefetch not running, not scrubbing,
+ * not playing and rendering-expensive footage, cache storage enabled, has strips to render,
+ * not rendering, not doing modal transform - important, see D7820.
+ */
+ if ((ed->cache_flag & SEQ_CACHE_PREFETCH_ENABLE) && !running && !scrubbing &&
+ !(playing && cost > 0.9) && ed->cache_flag & SEQ_CACHE_ALL_TYPES && has_strips &&
+ !G.is_rendering && !G.moving) {
+
+ seq_prefetch_start(context, cfra);
+ }
+ }
+}
+
+bool BKE_sequencer_prefetch_need_redraw(Main *bmain, Scene *scene)
+{
+ bool playing = seq_prefetch_is_playing(bmain);
+ bool scrubbing = seq_prefetch_is_scrubbing(bmain);
+ bool running = BKE_sequencer_prefetch_job_is_running(scene);
+ bool suspended = seq_prefetch_job_is_waiting(scene);
+
+ /* force redraw, when prefetching and using cache view. */
+ if (running && !playing && !suspended && scene->ed->cache_flag & SEQ_CACHE_VIEW_ENABLE) {
+ return true;
+ }
+ /* Sometimes scrubbing flag is set when not scrubbing. In that case I want to catch "event" of
+ * stopping scrubbing */
+ if (scrubbing) {
+ return true;
+ }
+ return false;
+}
diff --git a/source/blender/sequencer/intern/sequencer.c b/source/blender/sequencer/intern/sequencer.c
new file mode 100644
index 00000000000..b8ccdec0902
--- /dev/null
+++ b/source/blender/sequencer/intern/sequencer.c
@@ -0,0 +1,6193 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * - Blender Foundation, 2003-2009
+ * - Peter Schlaile <peter [at] schlaile [dot] de> 2005/2006
+ */
+
+/** \file
+ * \ingroup bke
+ */
+
+#include <math.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include "MEM_guardedalloc.h"
+
+#include "DNA_anim_types.h"
+#include "DNA_mask_types.h"
+#include "DNA_movieclip_types.h"
+#include "DNA_object_types.h"
+#include "DNA_scene_types.h"
+#include "DNA_sequence_types.h"
+#include "DNA_sound_types.h"
+#include "DNA_space_types.h"
+#include "DNA_windowmanager_types.h"
+
+#include "BLI_fileops.h"
+#include "BLI_linklist.h"
+#include "BLI_listbase.h"
+#include "BLI_math.h"
+#include "BLI_path_util.h"
+#include "BLI_session_uuid.h"
+#include "BLI_string.h"
+#include "BLI_string_utf8.h"
+#include "BLI_threads.h"
+#include "BLI_utildefines.h"
+
+#ifdef WIN32
+# include "BLI_winstuff.h"
+#else
+# include <unistd.h>
+#endif
+
+#include "BLT_translation.h"
+
+#include "BKE_anim_data.h"
+#include "BKE_animsys.h"
+#include "BKE_fcurve.h"
+#include "BKE_global.h"
+#include "BKE_idprop.h"
+#include "BKE_image.h"
+#include "BKE_layer.h"
+#include "BKE_lib_id.h"
+#include "BKE_main.h"
+#include "BKE_mask.h"
+#include "BKE_movieclip.h"
+#include "BKE_report.h"
+#include "BKE_scene.h"
+#include "BKE_sequencer.h"
+#include "BKE_sequencer_offscreen.h"
+
+#include "DEG_depsgraph.h"
+#include "DEG_depsgraph_query.h"
+
+#include "RNA_access.h"
+
+#include "RE_pipeline.h"
+
+#include <pthread.h>
+
+#include "IMB_colormanagement.h"
+#include "IMB_imbuf.h"
+#include "IMB_imbuf_types.h"
+#include "IMB_metadata.h"
+
+#include "BKE_context.h"
+#include "BKE_sound.h"
+
+#include "RE_engine.h"
+
+#ifdef WITH_AUDASPACE
+# include <AUD_Special.h>
+#endif
+
+/* mutable state for sequencer */
+typedef struct SeqRenderState {
+ LinkNode *scene_parents;
+} SeqRenderState;
+
+static ImBuf *seq_render_strip_stack(const SeqRenderData *context,
+ SeqRenderState *state,
+ ListBase *seqbasep,
+ float cfra,
+ int chanshown);
+static ImBuf *seq_render_preprocess_ibuf(const SeqRenderData *context,
+ Sequence *seq,
+ ImBuf *ibuf,
+ float cfra,
+ clock_t begin,
+ bool use_preprocess,
+ const bool is_proxy_image);
+static ImBuf *seq_render_strip(const SeqRenderData *context,
+ SeqRenderState *state,
+ Sequence *seq,
+ float cfra);
+static void seq_free_animdata(Scene *scene, Sequence *seq);
+static ImBuf *seq_render_mask(const SeqRenderData *context, Mask *mask, float nr, bool make_float);
+static int seq_num_files(Scene *scene, char views_format, const bool is_multiview);
+static void seq_anim_add_suffix(Scene *scene, struct anim *anim, const int view_id);
+
+static ThreadMutex seq_render_mutex = BLI_MUTEX_INITIALIZER;
+
+/* **** XXX ******** */
+#define SELECT 1
+ListBase seqbase_clipboard;
+int seqbase_clipboard_frame;
+
+SequencerDrawView sequencer_view3d_fn = NULL; /* NULL in background mode */
+
+#if 0 /* unused function */
+static void printf_strip(Sequence *seq)
+{
+ fprintf(stderr,
+ "name: '%s', len:%d, start:%d, (startofs:%d, endofs:%d), "
+ "(startstill:%d, endstill:%d), machine:%d, (startdisp:%d, enddisp:%d)\n",
+ seq->name,
+ seq->len,
+ seq->start,
+ seq->startofs,
+ seq->endofs,
+ seq->startstill,
+ seq->endstill,
+ seq->machine,
+ seq->startdisp,
+ seq->enddisp);
+
+ fprintf(stderr,
+ "\tseq_tx_set_final_left: %d %d\n\n",
+ seq_tx_get_final_left(seq, 0),
+ seq_tx_get_final_right(seq, 0));
+}
+#endif
+
+static void sequencer_state_init(SeqRenderState *state)
+{
+ state->scene_parents = NULL;
+}
+
+int BKE_sequencer_base_recursive_apply(ListBase *seqbase,
+ int (*apply_fn)(Sequence *seq, void *),
+ void *arg)
+{
+ Sequence *iseq;
+ for (iseq = seqbase->first; iseq; iseq = iseq->next) {
+ if (BKE_sequencer_recursive_apply(iseq, apply_fn, arg) == -1) {
+ return -1; /* bail out */
+ }
+ }
+ return 1;
+}
+
+int BKE_sequencer_recursive_apply(Sequence *seq, int (*apply_fn)(Sequence *, void *), void *arg)
+{
+ int ret = apply_fn(seq, arg);
+
+ if (ret == -1) {
+ return -1; /* bail out */
+ }
+
+ if (ret && seq->seqbase.first) {
+ ret = BKE_sequencer_base_recursive_apply(&seq->seqbase, apply_fn, arg);
+ }
+
+ return ret;
+}
+
+/*********************** alloc / free functions *************************/
+
+/* free */
+
+static void free_proxy_seq(Sequence *seq)
+{
+ if (seq->strip && seq->strip->proxy && seq->strip->proxy->anim) {
+ IMB_free_anim(seq->strip->proxy->anim);
+ seq->strip->proxy->anim = NULL;
+ }
+}
+
+static void seq_free_strip(Strip *strip)
+{
+ strip->us--;
+ if (strip->us > 0) {
+ return;
+ }
+ if (strip->us < 0) {
+ printf("error: negative users in strip\n");
+ return;
+ }
+
+ if (strip->stripdata) {
+ MEM_freeN(strip->stripdata);
+ }
+
+ if (strip->proxy) {
+ if (strip->proxy->anim) {
+ IMB_free_anim(strip->proxy->anim);
+ }
+
+ MEM_freeN(strip->proxy);
+ }
+ if (strip->crop) {
+ MEM_freeN(strip->crop);
+ }
+ if (strip->transform) {
+ MEM_freeN(strip->transform);
+ }
+
+ MEM_freeN(strip);
+}
+
+/* only give option to skip cache locally (static func) */
+static void BKE_sequence_free_ex(Scene *scene,
+ Sequence *seq,
+ const bool do_cache,
+ const bool do_id_user,
+ const bool do_clean_animdata)
+{
+ if (seq->strip) {
+ seq_free_strip(seq->strip);
+ }
+
+ BKE_sequence_free_anim(seq);
+
+ if (seq->type & SEQ_TYPE_EFFECT) {
+ struct SeqEffectHandle sh = BKE_sequence_get_effect(seq);
+ sh.free(seq, do_id_user);
+ }
+
+ if (seq->sound && do_id_user) {
+ id_us_min(((ID *)seq->sound));
+ }
+
+ if (seq->stereo3d_format) {
+ MEM_freeN(seq->stereo3d_format);
+ }
+
+ /* clipboard has no scene and will never have a sound handle or be active
+ * same goes to sequences copy for proxy rebuild job
+ */
+ if (scene) {
+ Editing *ed = scene->ed;
+
+ if (ed->act_seq == seq) {
+ ed->act_seq = NULL;
+ }
+
+ if (seq->scene_sound && ELEM(seq->type, SEQ_TYPE_SOUND_RAM, SEQ_TYPE_SCENE)) {
+ BKE_sound_remove_scene_sound(scene, seq->scene_sound);
+ }
+
+ /* XXX This must not be done in BKE code. */
+ if (do_clean_animdata) {
+ seq_free_animdata(scene, seq);
+ }
+ }
+
+ if (seq->prop) {
+ IDP_FreePropertyContent_ex(seq->prop, do_id_user);
+ MEM_freeN(seq->prop);
+ }
+
+ /* free modifiers */
+ BKE_sequence_modifier_clear(seq);
+
+ /* free cached data used by this strip,
+ * also invalidate cache for all dependent sequences
+ *
+ * be _very_ careful here, invalidating cache loops over the scene sequences and
+ * assumes the listbase is valid for all strips,
+ * this may not be the case if lists are being freed.
+ * this is optional BKE_sequence_invalidate_cache
+ */
+ if (do_cache) {
+ if (scene) {
+ BKE_sequence_invalidate_cache_raw(scene, seq);
+ }
+ }
+
+ MEM_freeN(seq);
+}
+
+void BKE_sequence_free(Scene *scene, Sequence *seq, const bool do_clean_animdata)
+{
+ BKE_sequence_free_ex(scene, seq, true, true, do_clean_animdata);
+}
+
+/* Function to free imbuf and anim data on changes */
+void BKE_sequence_free_anim(Sequence *seq)
+{
+ while (seq->anims.last) {
+ StripAnim *sanim = seq->anims.last;
+
+ if (sanim->anim) {
+ IMB_free_anim(sanim->anim);
+ sanim->anim = NULL;
+ }
+
+ BLI_freelinkN(&seq->anims, sanim);
+ }
+ BLI_listbase_clear(&seq->anims);
+}
+
+/* cache must be freed before calling this function
+ * since it leaves the seqbase in an invalid state */
+static void seq_free_sequence_recurse(Scene *scene, Sequence *seq, const bool do_id_user)
+{
+ Sequence *iseq, *iseq_next;
+
+ for (iseq = seq->seqbase.first; iseq; iseq = iseq_next) {
+ iseq_next = iseq->next;
+ seq_free_sequence_recurse(scene, iseq, do_id_user);
+ }
+
+ BKE_sequence_free_ex(scene, seq, false, do_id_user, true);
+}
+
+Editing *BKE_sequencer_editing_get(Scene *scene, bool alloc)
+{
+ if (alloc) {
+ BKE_sequencer_editing_ensure(scene);
+ }
+ return scene->ed;
+}
+
+void BKE_sequencer_free_clipboard(void)
+{
+ Sequence *seq, *nseq;
+
+ BKE_sequencer_base_clipboard_pointers_free(&seqbase_clipboard);
+
+ for (seq = seqbase_clipboard.first; seq; seq = nseq) {
+ nseq = seq->next;
+ seq_free_sequence_recurse(NULL, seq, false);
+ }
+ BLI_listbase_clear(&seqbase_clipboard);
+}
+
+/* -------------------------------------------------------------------- */
+/* Manage pointers in the clipboard.
+ * note that these pointers should _never_ be access in the sequencer,
+ * they are only for storage while in the clipboard
+ * notice 'newid' is used for temp pointer storage here, validate on access (this is safe usage,
+ * since those data-blocks are fully out of Main lists).
+ */
+#define ID_PT (*id_pt)
+static void seqclipboard_ptr_free(Main *UNUSED(bmain), ID **id_pt)
+{
+ if (ID_PT) {
+ BLI_assert(ID_PT->newid != NULL);
+ MEM_freeN(ID_PT);
+ ID_PT = NULL;
+ }
+}
+static void seqclipboard_ptr_store(Main *UNUSED(bmain), ID **id_pt)
+{
+ if (ID_PT) {
+ ID *id_prev = ID_PT;
+ ID_PT = MEM_dupallocN(ID_PT);
+ ID_PT->newid = id_prev;
+ }
+}
+static void seqclipboard_ptr_restore(Main *bmain, ID **id_pt)
+{
+ if (ID_PT) {
+ const ListBase *lb = which_libbase(bmain, GS(ID_PT->name));
+ void *id_restore;
+
+ BLI_assert(ID_PT->newid != NULL);
+ if (BLI_findindex(lb, (ID_PT)->newid) != -1) {
+ /* the pointer is still valid */
+ id_restore = (ID_PT)->newid;
+ }
+ else {
+ /* the pointer of the same name still exists */
+ id_restore = BLI_findstring(lb, (ID_PT)->name + 2, offsetof(ID, name) + 2);
+ }
+
+ if (id_restore == NULL) {
+ /* check for a data with the same filename */
+ switch (GS(ID_PT->name)) {
+ case ID_SO: {
+ id_restore = BLI_findstring(lb, ((bSound *)ID_PT)->filepath, offsetof(bSound, filepath));
+ if (id_restore == NULL) {
+ id_restore = BKE_sound_new_file(bmain, ((bSound *)ID_PT)->filepath);
+ (ID_PT)->newid = id_restore; /* reuse next time */
+ }
+ break;
+ }
+ case ID_MC: {
+ id_restore = BLI_findstring(
+ lb, ((MovieClip *)ID_PT)->filepath, offsetof(MovieClip, filepath));
+ if (id_restore == NULL) {
+ id_restore = BKE_movieclip_file_add(bmain, ((MovieClip *)ID_PT)->filepath);
+ (ID_PT)->newid = id_restore; /* reuse next time */
+ }
+ break;
+ }
+ default:
+ break;
+ }
+ }
+
+ /* Replace with pointer to actual data-block. */
+ seqclipboard_ptr_free(bmain, id_pt);
+ ID_PT = id_restore;
+ }
+}
+#undef ID_PT
+
+static void sequence_clipboard_pointers(Main *bmain,
+ Sequence *seq,
+ void (*callback)(Main *, ID **))
+{
+ callback(bmain, (ID **)&seq->scene);
+ callback(bmain, (ID **)&seq->scene_camera);
+ callback(bmain, (ID **)&seq->clip);
+ callback(bmain, (ID **)&seq->mask);
+ callback(bmain, (ID **)&seq->sound);
+
+ if (seq->type == SEQ_TYPE_TEXT && seq->effectdata) {
+ TextVars *text_data = seq->effectdata;
+ callback(bmain, (ID **)&text_data->text_font);
+ }
+}
+
+/* recursive versions of functions above */
+void BKE_sequencer_base_clipboard_pointers_free(ListBase *seqbase)
+{
+ Sequence *seq;
+ for (seq = seqbase->first; seq; seq = seq->next) {
+ sequence_clipboard_pointers(NULL, seq, seqclipboard_ptr_free);
+ BKE_sequencer_base_clipboard_pointers_free(&seq->seqbase);
+ }
+}
+void BKE_sequencer_base_clipboard_pointers_store(Main *bmain, ListBase *seqbase)
+{
+ Sequence *seq;
+ for (seq = seqbase->first; seq; seq = seq->next) {
+ sequence_clipboard_pointers(bmain, seq, seqclipboard_ptr_store);
+ BKE_sequencer_base_clipboard_pointers_store(bmain, &seq->seqbase);
+ }
+}
+void BKE_sequencer_base_clipboard_pointers_restore(ListBase *seqbase, Main *bmain)
+{
+ Sequence *seq;
+ for (seq = seqbase->first; seq; seq = seq->next) {
+ sequence_clipboard_pointers(bmain, seq, seqclipboard_ptr_restore);
+ BKE_sequencer_base_clipboard_pointers_restore(&seq->seqbase, bmain);
+ }
+}
+
+/* end clipboard pointer mess */
+
+Editing *BKE_sequencer_editing_ensure(Scene *scene)
+{
+ if (scene->ed == NULL) {
+ Editing *ed;
+
+ ed = scene->ed = MEM_callocN(sizeof(Editing), "addseq");
+ ed->seqbasep = &ed->seqbase;
+ ed->cache = NULL;
+ ed->cache_flag = SEQ_CACHE_STORE_FINAL_OUT;
+ ed->cache_flag |= SEQ_CACHE_VIEW_FINAL_OUT;
+ ed->cache_flag |= SEQ_CACHE_VIEW_ENABLE;
+ ed->recycle_max_cost = 10.0f;
+ }
+
+ return scene->ed;
+}
+
+void BKE_sequencer_editing_free(Scene *scene, const bool do_id_user)
+{
+ Editing *ed = scene->ed;
+ Sequence *seq;
+
+ if (ed == NULL) {
+ return;
+ }
+
+ BKE_sequencer_prefetch_free(scene);
+ BKE_sequencer_cache_destruct(scene);
+
+ SEQ_ALL_BEGIN (ed, seq) {
+ /* handle cache freeing above */
+ BKE_sequence_free_ex(scene, seq, false, do_id_user, false);
+ }
+ SEQ_ALL_END;
+
+ BLI_freelistN(&ed->metastack);
+ MEM_freeN(ed);
+
+ scene->ed = NULL;
+}
+
+/*********************** Sequencer color space functions *************************/
+
+static void sequencer_imbuf_assign_spaces(Scene *scene, ImBuf *ibuf)
+{
+#if 0
+ /* Bute buffer is supposed to be in sequencer working space already. */
+ if (ibuf->rect != NULL) {
+ IMB_colormanagement_assign_rect_colorspace(ibuf, scene->sequencer_colorspace_settings.name);
+ }
+#endif
+ if (ibuf->rect_float != NULL) {
+ IMB_colormanagement_assign_float_colorspace(ibuf, scene->sequencer_colorspace_settings.name);
+ }
+}
+
+void BKE_sequencer_imbuf_to_sequencer_space(Scene *scene, ImBuf *ibuf, bool make_float)
+{
+ /* Early output check: if both buffers are NULL we have nothing to convert. */
+ if (ibuf->rect_float == NULL && ibuf->rect == NULL) {
+ return;
+ }
+ /* Get common conversion settings. */
+ const char *to_colorspace = scene->sequencer_colorspace_settings.name;
+ /* Perform actual conversion logic. */
+ if (ibuf->rect_float == NULL) {
+ /* We are not requested to give float buffer and byte buffer is already
+ * in thee required colorspace. Can skip doing anything here.
+ */
+ const char *from_colorspace = IMB_colormanagement_get_rect_colorspace(ibuf);
+ if (!make_float && STREQ(from_colorspace, to_colorspace)) {
+ return;
+ }
+ if (false) {
+ /* The idea here is to provide as fast playback as possible and
+ * enforcing float buffer here (a) uses more cache memory (b) might
+ * make some other effects slower to apply.
+ *
+ * However, this might also have negative effect by adding weird
+ * artifacts which will then not happen in final render.
+ */
+ IMB_colormanagement_transform_byte_threaded((unsigned char *)ibuf->rect,
+ ibuf->x,
+ ibuf->y,
+ ibuf->channels,
+ from_colorspace,
+ to_colorspace);
+ }
+ else {
+ /* We perform conversion to a float buffer so we don't worry about
+ * precision loss.
+ */
+ imb_addrectfloatImBuf(ibuf);
+ IMB_colormanagement_transform_from_byte_threaded(ibuf->rect_float,
+ (unsigned char *)ibuf->rect,
+ ibuf->x,
+ ibuf->y,
+ ibuf->channels,
+ from_colorspace,
+ to_colorspace);
+ /* We don't need byte buffer anymore. */
+ imb_freerectImBuf(ibuf);
+ }
+ }
+ else {
+ const char *from_colorspace = IMB_colormanagement_get_float_colorspace(ibuf);
+ /* Unknown input color space, can't perform conversion. */
+ if (from_colorspace == NULL || from_colorspace[0] == '\0') {
+ return;
+ }
+ /* We don't want both byte and float buffers around: they'll either run
+ * out of sync or conversion of byte buffer will loose precision in there.
+ */
+ if (ibuf->rect != NULL) {
+ imb_freerectImBuf(ibuf);
+ }
+ IMB_colormanagement_transform_threaded(
+ ibuf->rect_float, ibuf->x, ibuf->y, ibuf->channels, from_colorspace, to_colorspace, true);
+ }
+ sequencer_imbuf_assign_spaces(scene, ibuf);
+}
+
+void BKE_sequencer_imbuf_from_sequencer_space(Scene *scene, ImBuf *ibuf)
+{
+ const char *from_colorspace = scene->sequencer_colorspace_settings.name;
+ const char *to_colorspace = IMB_colormanagement_role_colorspace_name_get(
+ COLOR_ROLE_SCENE_LINEAR);
+
+ if (!ibuf->rect_float) {
+ return;
+ }
+
+ if (to_colorspace && to_colorspace[0] != '\0') {
+ IMB_colormanagement_transform_threaded(
+ ibuf->rect_float, ibuf->x, ibuf->y, ibuf->channels, from_colorspace, to_colorspace, true);
+ IMB_colormanagement_assign_float_colorspace(ibuf, to_colorspace);
+ }
+}
+
+void BKE_sequencer_pixel_from_sequencer_space_v4(struct Scene *scene, float pixel[4])
+{
+ const char *from_colorspace = scene->sequencer_colorspace_settings.name;
+ const char *to_colorspace = IMB_colormanagement_role_colorspace_name_get(
+ COLOR_ROLE_SCENE_LINEAR);
+
+ if (to_colorspace && to_colorspace[0] != '\0') {
+ IMB_colormanagement_transform_v4(pixel, from_colorspace, to_colorspace);
+ }
+ else {
+ /* if no color management enables fallback to legacy conversion */
+ srgb_to_linearrgb_v4(pixel, pixel);
+ }
+}
+
+/*********************** sequencer pipeline functions *************************/
+
+void BKE_sequencer_new_render_data(Main *bmain,
+ struct Depsgraph *depsgraph,
+ Scene *scene,
+ int rectx,
+ int recty,
+ int preview_render_size,
+ int for_render,
+ SeqRenderData *r_context)
+{
+ r_context->bmain = bmain;
+ r_context->depsgraph = depsgraph;
+ r_context->scene = scene;
+ r_context->rectx = rectx;
+ r_context->recty = recty;
+ r_context->preview_render_size = preview_render_size;
+ r_context->for_render = for_render;
+ r_context->motion_blur_samples = 0;
+ r_context->motion_blur_shutter = 0;
+ r_context->skip_cache = false;
+ r_context->is_proxy_render = false;
+ r_context->view_id = 0;
+ r_context->gpu_offscreen = NULL;
+ r_context->task_id = SEQ_TASK_MAIN_RENDER;
+ r_context->is_prefetch_render = false;
+}
+
+/* ************************* iterator ************************** */
+/* *************** (replaces old WHILE_SEQ) ********************* */
+/* **************** use now SEQ_ALL_BEGIN () SEQ_ALL_END ***************** */
+
+/* sequence strip iterator:
+ * - builds a full array, recursively into meta strips
+ */
+
+static void seq_count(ListBase *seqbase, int *tot)
+{
+ Sequence *seq;
+
+ for (seq = seqbase->first; seq; seq = seq->next) {
+ (*tot)++;
+
+ if (seq->seqbase.first) {
+ seq_count(&seq->seqbase, tot);
+ }
+ }
+}
+
+static void seq_build_array(ListBase *seqbase, Sequence ***array, int depth)
+{
+ Sequence *seq;
+
+ for (seq = seqbase->first; seq; seq = seq->next) {
+ seq->depth = depth;
+
+ if (seq->seqbase.first) {
+ seq_build_array(&seq->seqbase, array, depth + 1);
+ }
+
+ **array = seq;
+ (*array)++;
+ }
+}
+
+static void seq_array(Editing *ed,
+ Sequence ***seqarray,
+ int *tot,
+ const bool use_current_sequences)
+{
+ Sequence **array;
+
+ *seqarray = NULL;
+ *tot = 0;
+
+ if (ed == NULL) {
+ return;
+ }
+
+ if (use_current_sequences) {
+ seq_count(ed->seqbasep, tot);
+ }
+ else {
+ seq_count(&ed->seqbase, tot);
+ }
+
+ if (*tot == 0) {
+ return;
+ }
+
+ *seqarray = array = MEM_mallocN(sizeof(Sequence *) * (*tot), "SeqArray");
+ if (use_current_sequences) {
+ seq_build_array(ed->seqbasep, &array, 0);
+ }
+ else {
+ seq_build_array(&ed->seqbase, &array, 0);
+ }
+}
+
+void BKE_sequence_iterator_begin(Editing *ed, SeqIterator *iter, const bool use_current_sequences)
+{
+ memset(iter, 0, sizeof(*iter));
+ seq_array(ed, &iter->array, &iter->tot, use_current_sequences);
+
+ if (iter->tot) {
+ iter->cur = 0;
+ iter->seq = iter->array[iter->cur];
+ iter->valid = 1;
+ }
+}
+
+void BKE_sequence_iterator_next(SeqIterator *iter)
+{
+ if (++iter->cur < iter->tot) {
+ iter->seq = iter->array[iter->cur];
+ }
+ else {
+ iter->valid = 0;
+ }
+}
+
+void BKE_sequence_iterator_end(SeqIterator *iter)
+{
+ if (iter->array) {
+ MEM_freeN(iter->array);
+ }
+
+ iter->valid = 0;
+}
+
+static int metaseq_start(Sequence *metaseq)
+{
+ return metaseq->start + metaseq->startofs;
+}
+
+static int metaseq_end(Sequence *metaseq)
+{
+ return metaseq->start + metaseq->len - metaseq->endofs;
+}
+
+static void seq_update_sound_bounds_recursive_impl(Scene *scene,
+ Sequence *metaseq,
+ int start,
+ int end)
+{
+ Sequence *seq;
+
+ /* for sound we go over full meta tree to update bounds of the sound strips,
+ * since sound is played outside of evaluating the imbufs, */
+ for (seq = metaseq->seqbase.first; seq; seq = seq->next) {
+ if (seq->type == SEQ_TYPE_META) {
+ seq_update_sound_bounds_recursive_impl(
+ scene, seq, max_ii(start, metaseq_start(seq)), min_ii(end, metaseq_end(seq)));
+ }
+ else if (ELEM(seq->type, SEQ_TYPE_SOUND_RAM, SEQ_TYPE_SCENE)) {
+ if (seq->scene_sound) {
+ int startofs = seq->startofs;
+ int endofs = seq->endofs;
+ if (seq->startofs + seq->start < start) {
+ startofs = start - seq->start;
+ }
+
+ if (seq->start + seq->len - seq->endofs > end) {
+ endofs = seq->start + seq->len - end;
+ }
+
+ BKE_sound_move_scene_sound(scene,
+ seq->scene_sound,
+ seq->start + startofs,
+ seq->start + seq->len - endofs,
+ startofs + seq->anim_startofs);
+ }
+ }
+ }
+}
+
+static void seq_update_sound_bounds_recursive(Scene *scene, Sequence *metaseq)
+{
+ seq_update_sound_bounds_recursive_impl(
+ scene, metaseq, metaseq_start(metaseq), metaseq_end(metaseq));
+}
+
+void BKE_sequence_calc_disp(Scene *scene, Sequence *seq)
+{
+ if (seq->startofs && seq->startstill) {
+ seq->startstill = 0;
+ }
+ if (seq->endofs && seq->endstill) {
+ seq->endstill = 0;
+ }
+
+ seq->startdisp = seq->start + seq->startofs - seq->startstill;
+ seq->enddisp = seq->start + seq->len - seq->endofs + seq->endstill;
+
+ if (seq->type == SEQ_TYPE_META) {
+ seq_update_sound_bounds_recursive(scene, seq);
+ }
+}
+
+void BKE_sequence_calc(Scene *scene, Sequence *seq)
+{
+ Sequence *seqm;
+ int min, max;
+
+ /* check all metas recursively */
+ seqm = seq->seqbase.first;
+ while (seqm) {
+ if (seqm->seqbase.first) {
+ BKE_sequence_calc(scene, seqm);
+ }
+ seqm = seqm->next;
+ }
+
+ /* effects and meta: automatic start and end */
+ if (seq->type & SEQ_TYPE_EFFECT) {
+ if (seq->seq1) {
+ seq->startofs = seq->endofs = seq->startstill = seq->endstill = 0;
+ if (seq->seq3) {
+ seq->start = seq->startdisp = max_iii(
+ seq->seq1->startdisp, seq->seq2->startdisp, seq->seq3->startdisp);
+ seq->enddisp = min_iii(seq->seq1->enddisp, seq->seq2->enddisp, seq->seq3->enddisp);
+ }
+ else if (seq->seq2) {
+ seq->start = seq->startdisp = max_ii(seq->seq1->startdisp, seq->seq2->startdisp);
+ seq->enddisp = min_ii(seq->seq1->enddisp, seq->seq2->enddisp);
+ }
+ else {
+ seq->start = seq->startdisp = seq->seq1->startdisp;
+ seq->enddisp = seq->seq1->enddisp;
+ }
+ /* we cant help if strips don't overlap, it wont give useful results.
+ * but at least ensure 'len' is never negative which causes bad bugs elsewhere. */
+ if (seq->enddisp < seq->startdisp) {
+ /* simple start/end swap */
+ seq->start = seq->enddisp;
+ seq->enddisp = seq->startdisp;
+ seq->startdisp = seq->start;
+ seq->flag |= SEQ_INVALID_EFFECT;
+ }
+ else {
+ seq->flag &= ~SEQ_INVALID_EFFECT;
+ }
+
+ seq->len = seq->enddisp - seq->startdisp;
+ }
+ else {
+ BKE_sequence_calc_disp(scene, seq);
+ }
+ }
+ else {
+ if (seq->type == SEQ_TYPE_META) {
+ seqm = seq->seqbase.first;
+ if (seqm) {
+ min = MAXFRAME * 2;
+ max = -MAXFRAME * 2;
+ while (seqm) {
+ if (seqm->startdisp < min) {
+ min = seqm->startdisp;
+ }
+ if (seqm->enddisp > max) {
+ max = seqm->enddisp;
+ }
+ seqm = seqm->next;
+ }
+ seq->start = min + seq->anim_startofs;
+ seq->len = max - min;
+ seq->len -= seq->anim_startofs;
+ seq->len -= seq->anim_endofs;
+ }
+ seq_update_sound_bounds_recursive(scene, seq);
+ }
+ BKE_sequence_calc_disp(scene, seq);
+ }
+}
+
+static void seq_multiview_name(Scene *scene,
+ const int view_id,
+ const char *prefix,
+ const char *ext,
+ char *r_path,
+ size_t r_size)
+{
+ const char *suffix = BKE_scene_multiview_view_id_suffix_get(&scene->r, view_id);
+ BLI_assert(ext != NULL && suffix != NULL && prefix != NULL);
+ BLI_snprintf(r_path, r_size, "%s%s%s", prefix, suffix, ext);
+}
+
+/* note: caller should run BKE_sequence_calc(scene, seq) after */
+void BKE_sequence_reload_new_file(Main *bmain, Scene *scene, Sequence *seq, const bool lock_range)
+{
+ char path[FILE_MAX];
+ int prev_startdisp = 0, prev_enddisp = 0;
+ /* note: don't rename the strip, will break animation curves */
+
+ if (ELEM(seq->type,
+ SEQ_TYPE_MOVIE,
+ SEQ_TYPE_IMAGE,
+ SEQ_TYPE_SOUND_RAM,
+ SEQ_TYPE_SCENE,
+ SEQ_TYPE_META,
+ SEQ_TYPE_MOVIECLIP,
+ SEQ_TYPE_MASK) == 0) {
+ return;
+ }
+
+ if (lock_range) {
+ /* keep so we don't have to move the actual start and end points (only the data) */
+ BKE_sequence_calc_disp(scene, seq);
+ prev_startdisp = seq->startdisp;
+ prev_enddisp = seq->enddisp;
+ }
+
+ switch (seq->type) {
+ case SEQ_TYPE_IMAGE: {
+ /* Hack? */
+ size_t olen = MEM_allocN_len(seq->strip->stripdata) / sizeof(StripElem);
+
+ seq->len = olen;
+ seq->len -= seq->anim_startofs;
+ seq->len -= seq->anim_endofs;
+ if (seq->len < 0) {
+ seq->len = 0;
+ }
+ break;
+ }
+ case SEQ_TYPE_MOVIE: {
+ StripAnim *sanim;
+ bool is_multiview_loaded = false;
+ const bool is_multiview = (seq->flag & SEQ_USE_VIEWS) != 0 &&
+ (scene->r.scemode & R_MULTIVIEW) != 0;
+
+ BLI_join_dirfile(path, sizeof(path), seq->strip->dir, seq->strip->stripdata->name);
+ BLI_path_abs(path, BKE_main_blendfile_path_from_global());
+
+ BKE_sequence_free_anim(seq);
+
+ if (is_multiview && (seq->views_format == R_IMF_VIEWS_INDIVIDUAL)) {
+ char prefix[FILE_MAX];
+ const char *ext = NULL;
+ const int totfiles = seq_num_files(scene, seq->views_format, true);
+ int i = 0;
+
+ BKE_scene_multiview_view_prefix_get(scene, path, prefix, &ext);
+
+ if (prefix[0] != '\0') {
+ for (i = 0; i < totfiles; i++) {
+ struct anim *anim;
+ char str[FILE_MAX];
+
+ seq_multiview_name(scene, i, prefix, ext, str, FILE_MAX);
+ anim = openanim(str,
+ IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
+ seq->streamindex,
+ seq->strip->colorspace_settings.name);
+
+ if (anim) {
+ seq_anim_add_suffix(scene, anim, i);
+ sanim = MEM_mallocN(sizeof(StripAnim), "Strip Anim");
+ BLI_addtail(&seq->anims, sanim);
+ sanim->anim = anim;
+ }
+ }
+ is_multiview_loaded = true;
+ }
+ }
+
+ if (is_multiview_loaded == false) {
+ struct anim *anim;
+ anim = openanim(path,
+ IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
+ seq->streamindex,
+ seq->strip->colorspace_settings.name);
+ if (anim) {
+ sanim = MEM_mallocN(sizeof(StripAnim), "Strip Anim");
+ BLI_addtail(&seq->anims, sanim);
+ sanim->anim = anim;
+ }
+ }
+
+ /* use the first video as reference for everything */
+ sanim = seq->anims.first;
+
+ if ((!sanim) || (!sanim->anim)) {
+ return;
+ }
+
+ IMB_anim_load_metadata(sanim->anim);
+
+ seq->len = IMB_anim_get_duration(
+ sanim->anim, seq->strip->proxy ? seq->strip->proxy->tc : IMB_TC_RECORD_RUN);
+
+ seq->anim_preseek = IMB_anim_get_preseek(sanim->anim);
+
+ seq->len -= seq->anim_startofs;
+ seq->len -= seq->anim_endofs;
+ if (seq->len < 0) {
+ seq->len = 0;
+ }
+ break;
+ }
+ case SEQ_TYPE_MOVIECLIP:
+ if (seq->clip == NULL) {
+ return;
+ }
+
+ seq->len = BKE_movieclip_get_duration(seq->clip);
+
+ seq->len -= seq->anim_startofs;
+ seq->len -= seq->anim_endofs;
+ if (seq->len < 0) {
+ seq->len = 0;
+ }
+ break;
+ case SEQ_TYPE_MASK:
+ if (seq->mask == NULL) {
+ return;
+ }
+ seq->len = BKE_mask_get_duration(seq->mask);
+ seq->len -= seq->anim_startofs;
+ seq->len -= seq->anim_endofs;
+ if (seq->len < 0) {
+ seq->len = 0;
+ }
+ break;
+ case SEQ_TYPE_SOUND_RAM:
+#ifdef WITH_AUDASPACE
+ if (!seq->sound) {
+ return;
+ }
+ seq->len = ceil((double)BKE_sound_get_length(bmain, seq->sound) * FPS);
+ seq->len -= seq->anim_startofs;
+ seq->len -= seq->anim_endofs;
+ if (seq->len < 0) {
+ seq->len = 0;
+ }
+#else
+ UNUSED_VARS(bmain);
+ return;
+#endif
+ break;
+ case SEQ_TYPE_SCENE: {
+ seq->len = (seq->scene) ? seq->scene->r.efra - seq->scene->r.sfra + 1 : 0;
+ seq->len -= seq->anim_startofs;
+ seq->len -= seq->anim_endofs;
+ if (seq->len < 0) {
+ seq->len = 0;
+ }
+ break;
+ }
+ }
+
+ free_proxy_seq(seq);
+
+ if (lock_range) {
+ BKE_sequence_tx_set_final_left(seq, prev_startdisp);
+ BKE_sequence_tx_set_final_right(seq, prev_enddisp);
+ BKE_sequence_single_fix(seq);
+ }
+
+ BKE_sequence_calc(scene, seq);
+}
+
+void BKE_sequence_movie_reload_if_needed(struct Main *bmain,
+ struct Scene *scene,
+ struct Sequence *seq,
+ bool *r_was_reloaded,
+ bool *r_can_produce_frames)
+{
+ BLI_assert(seq->type == SEQ_TYPE_MOVIE ||
+ !"This function is only implemented for movie strips.");
+
+ bool must_reload = false;
+
+ /* The Sequence struct allows for multiple anim structs to be associated with one strip. This
+ * function will return true only if there is at least one 'anim' AND all anims can produce
+ * frames. */
+
+ if (BLI_listbase_is_empty(&seq->anims)) {
+ /* No anim present, so reloading is always necessary. */
+ must_reload = true;
+ }
+ else {
+ LISTBASE_FOREACH (StripAnim *, sanim, &seq->anims) {
+ if (!IMB_anim_can_produce_frames(sanim->anim)) {
+ /* Anim cannot produce frames, try reloading. */
+ must_reload = true;
+ break;
+ }
+ };
+ }
+
+ if (!must_reload) {
+ /* There are one or more anims, and all can produce frames. */
+ *r_was_reloaded = false;
+ *r_can_produce_frames = true;
+ return;
+ }
+
+ BKE_sequence_reload_new_file(bmain, scene, seq, true);
+ *r_was_reloaded = true;
+
+ if (BLI_listbase_is_empty(&seq->anims)) {
+ /* No anims present after reloading => no frames can be produced. */
+ *r_can_produce_frames = false;
+ return;
+ }
+
+ /* Check if there are still anims that cannot produce frames. */
+ LISTBASE_FOREACH (StripAnim *, sanim, &seq->anims) {
+ if (!IMB_anim_can_produce_frames(sanim->anim)) {
+ /* There still is an anim that cannot produce frames. */
+ *r_can_produce_frames = false;
+ return;
+ }
+ };
+
+ /* There are one or more anims, and all can produce frames. */
+ *r_can_produce_frames = true;
+}
+
+void BKE_sequencer_sort(Scene *scene)
+{
+ /* all strips together per kind, and in order of y location ("machine") */
+ ListBase seqbase, effbase;
+ Editing *ed = BKE_sequencer_editing_get(scene, false);
+ Sequence *seq, *seqt;
+
+ if (ed == NULL) {
+ return;
+ }
+
+ BLI_listbase_clear(&seqbase);
+ BLI_listbase_clear(&effbase);
+
+ while ((seq = BLI_pophead(ed->seqbasep))) {
+
+ if (seq->type & SEQ_TYPE_EFFECT) {
+ seqt = effbase.first;
+ while (seqt) {
+ if (seqt->machine >= seq->machine) {
+ BLI_insertlinkbefore(&effbase, seqt, seq);
+ break;
+ }
+ seqt = seqt->next;
+ }
+ if (seqt == NULL) {
+ BLI_addtail(&effbase, seq);
+ }
+ }
+ else {
+ seqt = seqbase.first;
+ while (seqt) {
+ if (seqt->machine >= seq->machine) {
+ BLI_insertlinkbefore(&seqbase, seqt, seq);
+ break;
+ }
+ seqt = seqt->next;
+ }
+ if (seqt == NULL) {
+ BLI_addtail(&seqbase, seq);
+ }
+ }
+ }
+
+ BLI_movelisttolist(&seqbase, &effbase);
+ *(ed->seqbasep) = seqbase;
+}
+
+/** Comparison function suitable to be used with BLI_listbase_sort()... */
+int BKE_sequencer_cmp_time_startdisp(const void *a, const void *b)
+{
+ const Sequence *seq_a = a;
+ const Sequence *seq_b = b;
+
+ return (seq_a->startdisp > seq_b->startdisp);
+}
+
+static int clear_scene_in_allseqs_fn(Sequence *seq, void *arg_pt)
+{
+ if (seq->scene == (Scene *)arg_pt) {
+ seq->scene = NULL;
+ }
+ return 1;
+}
+
+void BKE_sequencer_clear_scene_in_allseqs(Main *bmain, Scene *scene)
+{
+ Scene *scene_iter;
+
+ /* when a scene is deleted: test all seqs */
+ for (scene_iter = bmain->scenes.first; scene_iter; scene_iter = scene_iter->id.next) {
+ if (scene_iter != scene && scene_iter->ed) {
+ BKE_sequencer_base_recursive_apply(
+ &scene_iter->ed->seqbase, clear_scene_in_allseqs_fn, scene);
+ }
+ }
+}
+
+typedef struct SeqUniqueInfo {
+ Sequence *seq;
+ char name_src[SEQ_NAME_MAXSTR];
+ char name_dest[SEQ_NAME_MAXSTR];
+ int count;
+ int match;
+} SeqUniqueInfo;
+
+static void seqbase_unique_name(ListBase *seqbasep, SeqUniqueInfo *sui)
+{
+ Sequence *seq;
+ for (seq = seqbasep->first; seq; seq = seq->next) {
+ if ((sui->seq != seq) && STREQ(sui->name_dest, seq->name + 2)) {
+ /* SEQ_NAME_MAXSTR -4 for the number, -1 for \0, - 2 for r_prefix */
+ BLI_snprintf(sui->name_dest,
+ sizeof(sui->name_dest),
+ "%.*s.%03d",
+ SEQ_NAME_MAXSTR - 4 - 1 - 2,
+ sui->name_src,
+ sui->count++);
+ sui->match = 1; /* be sure to re-scan */
+ }
+ }
+}
+
+static int seqbase_unique_name_recursive_fn(Sequence *seq, void *arg_pt)
+{
+ if (seq->seqbase.first) {
+ seqbase_unique_name(&seq->seqbase, (SeqUniqueInfo *)arg_pt);
+ }
+ return 1;
+}
+
+void BKE_sequence_base_unique_name_recursive(ListBase *seqbasep, Sequence *seq)
+{
+ SeqUniqueInfo sui;
+ char *dot;
+ sui.seq = seq;
+ BLI_strncpy(sui.name_src, seq->name + 2, sizeof(sui.name_src));
+ BLI_strncpy(sui.name_dest, seq->name + 2, sizeof(sui.name_dest));
+
+ sui.count = 1;
+ sui.match = 1; /* assume the worst to start the loop */
+
+ /* Strip off the suffix */
+ if ((dot = strrchr(sui.name_src, '.'))) {
+ *dot = '\0';
+ dot++;
+
+ if (*dot) {
+ sui.count = atoi(dot) + 1;
+ }
+ }
+
+ while (sui.match) {
+ sui.match = 0;
+ seqbase_unique_name(seqbasep, &sui);
+ BKE_sequencer_base_recursive_apply(seqbasep, seqbase_unique_name_recursive_fn, &sui);
+ }
+
+ BLI_strncpy(seq->name + 2, sui.name_dest, sizeof(seq->name) - 2);
+}
+
+static const char *give_seqname_by_type(int type)
+{
+ switch (type) {
+ case SEQ_TYPE_META:
+ return "Meta";
+ case SEQ_TYPE_IMAGE:
+ return "Image";
+ case SEQ_TYPE_SCENE:
+ return "Scene";
+ case SEQ_TYPE_MOVIE:
+ return "Movie";
+ case SEQ_TYPE_MOVIECLIP:
+ return "Clip";
+ case SEQ_TYPE_MASK:
+ return "Mask";
+ case SEQ_TYPE_SOUND_RAM:
+ return "Audio";
+ case SEQ_TYPE_CROSS:
+ return "Cross";
+ case SEQ_TYPE_GAMCROSS:
+ return "Gamma Cross";
+ case SEQ_TYPE_ADD:
+ return "Add";
+ case SEQ_TYPE_SUB:
+ return "Sub";
+ case SEQ_TYPE_MUL:
+ return "Mul";
+ case SEQ_TYPE_ALPHAOVER:
+ return "Alpha Over";
+ case SEQ_TYPE_ALPHAUNDER:
+ return "Alpha Under";
+ case SEQ_TYPE_OVERDROP:
+ return "Over Drop";
+ case SEQ_TYPE_COLORMIX:
+ return "Color Mix";
+ case SEQ_TYPE_WIPE:
+ return "Wipe";
+ case SEQ_TYPE_GLOW:
+ return "Glow";
+ case SEQ_TYPE_TRANSFORM:
+ return "Transform";
+ case SEQ_TYPE_COLOR:
+ return "Color";
+ case SEQ_TYPE_MULTICAM:
+ return "Multicam";
+ case SEQ_TYPE_ADJUSTMENT:
+ return "Adjustment";
+ case SEQ_TYPE_SPEED:
+ return "Speed";
+ case SEQ_TYPE_GAUSSIAN_BLUR:
+ return "Gaussian Blur";
+ case SEQ_TYPE_TEXT:
+ return "Text";
+ default:
+ return NULL;
+ }
+}
+
+const char *BKE_sequence_give_name(Sequence *seq)
+{
+ const char *name = give_seqname_by_type(seq->type);
+
+ if (!name) {
+ if (!(seq->type & SEQ_TYPE_EFFECT)) {
+ return seq->strip->dir;
+ }
+
+ return "Effect";
+ }
+ return name;
+}
+
+ListBase *BKE_sequence_seqbase_get(Sequence *seq, int *r_offset)
+{
+ ListBase *seqbase = NULL;
+
+ switch (seq->type) {
+ case SEQ_TYPE_META: {
+ seqbase = &seq->seqbase;
+ *r_offset = seq->start;
+ break;
+ }
+ case SEQ_TYPE_SCENE: {
+ if (seq->flag & SEQ_SCENE_STRIPS && seq->scene) {
+ Editing *ed = BKE_sequencer_editing_get(seq->scene, false);
+ if (ed) {
+ seqbase = &ed->seqbase;
+ *r_offset = seq->scene->r.sfra;
+ }
+ }
+ break;
+ }
+ }
+
+ return seqbase;
+}
+
+/*********************** DO THE SEQUENCE *************************/
+
+static void multibuf(ImBuf *ibuf, const float fmul)
+{
+ char *rt;
+ float *rt_float;
+
+ int a;
+
+ rt = (char *)ibuf->rect;
+ rt_float = ibuf->rect_float;
+
+ if (rt) {
+ const int imul = (int)(256.0f * fmul);
+ a = ibuf->x * ibuf->y;
+ while (a--) {
+ rt[0] = min_ii((imul * rt[0]) >> 8, 255);
+ rt[1] = min_ii((imul * rt[1]) >> 8, 255);
+ rt[2] = min_ii((imul * rt[2]) >> 8, 255);
+ rt[3] = min_ii((imul * rt[3]) >> 8, 255);
+
+ rt += 4;
+ }
+ }
+ if (rt_float) {
+ a = ibuf->x * ibuf->y;
+ while (a--) {
+ rt_float[0] *= fmul;
+ rt_float[1] *= fmul;
+ rt_float[2] *= fmul;
+ rt_float[3] *= fmul;
+
+ rt_float += 4;
+ }
+ }
+}
+
+float BKE_sequencer_give_stripelem_index(Sequence *seq, float cfra)
+{
+ float nr;
+ int sta = seq->start;
+ int end = seq->start + seq->len - 1;
+
+ if (seq->type & SEQ_TYPE_EFFECT) {
+ end = seq->enddisp;
+ }
+
+ if (end < sta) {
+ return -1;
+ }
+
+ if (seq->flag & SEQ_REVERSE_FRAMES) {
+ /*reverse frame in this sequence */
+ if (cfra <= sta) {
+ nr = end - sta;
+ }
+ else if (cfra >= end) {
+ nr = 0;
+ }
+ else {
+ nr = end - cfra;
+ }
+ }
+ else {
+ if (cfra <= sta) {
+ nr = 0;
+ }
+ else if (cfra >= end) {
+ nr = end - sta;
+ }
+ else {
+ nr = cfra - sta;
+ }
+ }
+
+ if (seq->strobe < 1.0f) {
+ seq->strobe = 1.0f;
+ }
+
+ if (seq->strobe > 1.0f) {
+ nr -= fmodf((double)nr, (double)seq->strobe);
+ }
+
+ return nr;
+}
+
+StripElem *BKE_sequencer_give_stripelem(Sequence *seq, int cfra)
+{
+ StripElem *se = seq->strip->stripdata;
+
+ if (seq->type == SEQ_TYPE_IMAGE) {
+ /* only IMAGE strips use the whole array, MOVIE strips use only the first element,
+ * all other strips don't use this...
+ */
+
+ int nr = (int)BKE_sequencer_give_stripelem_index(seq, cfra);
+
+ if (nr == -1 || se == NULL) {
+ return NULL;
+ }
+
+ se += nr + seq->anim_startofs;
+ }
+ return se;
+}
+
+static int evaluate_seq_frame_gen(Sequence **seq_arr, ListBase *seqbase, int cfra, int chanshown)
+{
+ /* Use arbitrary sized linked list, the size could be over MAXSEQ. */
+ LinkNodePair effect_inputs = {NULL, NULL};
+ int totseq = 0;
+
+ memset(seq_arr, 0, sizeof(Sequence *) * (MAXSEQ + 1));
+
+ LISTBASE_FOREACH (Sequence *, seq, seqbase) {
+ if ((seq->startdisp <= cfra) && (seq->enddisp > cfra)) {
+ if ((seq->type & SEQ_TYPE_EFFECT) && !(seq->flag & SEQ_MUTE)) {
+
+ if (seq->seq1) {
+ BLI_linklist_append_alloca(&effect_inputs, seq->seq1);
+ }
+
+ if (seq->seq2) {
+ BLI_linklist_append_alloca(&effect_inputs, seq->seq2);
+ }
+
+ if (seq->seq3) {
+ BLI_linklist_append_alloca(&effect_inputs, seq->seq3);
+ }
+ }
+
+ seq_arr[seq->machine] = seq;
+ totseq++;
+ }
+ }
+
+ /* Drop strips which are used for effect inputs, we don't want
+ * them to blend into render stack in any other way than effect
+ * string rendering. */
+ for (LinkNode *seq_item = effect_inputs.list; seq_item; seq_item = seq_item->next) {
+ Sequence *seq = seq_item->link;
+ /* It's possible that effect strip would be placed to the same
+ * 'machine' as it's inputs. We don't want to clear such strips
+ * from the stack. */
+ if (seq_arr[seq->machine] && seq_arr[seq->machine]->type & SEQ_TYPE_EFFECT) {
+ continue;
+ }
+ /* If we're shown a specified channel, then we want to see the strips
+ * which belongs to this machine. */
+ if (chanshown != 0 && chanshown <= seq->machine) {
+ continue;
+ }
+ seq_arr[seq->machine] = NULL;
+ }
+
+ return totseq;
+}
+
+int BKE_sequencer_evaluate_frame(Scene *scene, int cfra)
+{
+ Editing *ed = BKE_sequencer_editing_get(scene, false);
+ Sequence *seq_arr[MAXSEQ + 1];
+
+ if (ed == NULL) {
+ return 0;
+ }
+
+ return evaluate_seq_frame_gen(seq_arr, ed->seqbasep, cfra, 0);
+}
+
+static bool video_seq_is_rendered(Sequence *seq)
+{
+ return (seq && !(seq->flag & SEQ_MUTE) && seq->type != SEQ_TYPE_SOUND_RAM);
+}
+
+int BKE_sequencer_get_shown_sequences(ListBase *seqbasep,
+ int cfra,
+ int chanshown,
+ Sequence **seq_arr_out)
+{
+ Sequence *seq_arr[MAXSEQ + 1];
+ int b = chanshown;
+ int cnt = 0;
+
+ if (b > MAXSEQ) {
+ return 0;
+ }
+
+ if (evaluate_seq_frame_gen(seq_arr, seqbasep, cfra, chanshown)) {
+ if (b == 0) {
+ b = MAXSEQ;
+ }
+ for (; b > 0; b--) {
+ if (video_seq_is_rendered(seq_arr[b])) {
+ break;
+ }
+ }
+ }
+
+ chanshown = b;
+
+ for (; b > 0; b--) {
+ if (video_seq_is_rendered(seq_arr[b])) {
+ if (seq_arr[b]->blend_mode == SEQ_BLEND_REPLACE) {
+ break;
+ }
+ }
+ }
+
+ for (; b <= chanshown && b >= 0; b++) {
+ if (video_seq_is_rendered(seq_arr[b])) {
+ seq_arr_out[cnt++] = seq_arr[b];
+ }
+ }
+
+ return cnt;
+}
+
+/*********************** proxy management *************************/
+
+typedef struct SeqIndexBuildContext {
+ struct IndexBuildContext *index_context;
+
+ int tc_flags;
+ int size_flags;
+ int quality;
+ bool overwrite;
+ int view_id;
+
+ Main *bmain;
+ Depsgraph *depsgraph;
+ Scene *scene;
+ Sequence *seq, *orig_seq;
+} SeqIndexBuildContext;
+
+#define PROXY_MAXFILE (2 * FILE_MAXDIR + FILE_MAXFILE)
+
+static IMB_Proxy_Size seq_rendersize_to_proxysize(int render_size)
+{
+ switch (render_size) {
+ case SEQ_RENDER_SIZE_PROXY_25:
+ return IMB_PROXY_25;
+ case SEQ_RENDER_SIZE_PROXY_50:
+ return IMB_PROXY_50;
+ case SEQ_RENDER_SIZE_PROXY_75:
+ return IMB_PROXY_75;
+ case SEQ_RENDER_SIZE_PROXY_100:
+ return IMB_PROXY_100;
+ }
+ return IMB_PROXY_NONE;
+}
+
+double BKE_sequencer_rendersize_to_scale_factor(int render_size)
+{
+ switch (render_size) {
+ case SEQ_RENDER_SIZE_PROXY_25:
+ return 0.25;
+ case SEQ_RENDER_SIZE_PROXY_50:
+ return 0.50;
+ case SEQ_RENDER_SIZE_PROXY_75:
+ return 0.75;
+ }
+ return 1.0;
+}
+
+/* the number of files will vary according to the stereo format */
+static int seq_num_files(Scene *scene, char views_format, const bool is_multiview)
+{
+ if (!is_multiview) {
+ return 1;
+ }
+ if (views_format == R_IMF_VIEWS_STEREO_3D) {
+ return 1;
+ }
+ /* R_IMF_VIEWS_INDIVIDUAL */
+
+ return BKE_scene_multiview_num_views_get(&scene->r);
+}
+
+static void seq_proxy_index_dir_set(struct anim *anim, const char *base_dir)
+{
+ char dir[FILE_MAX];
+ char fname[FILE_MAXFILE];
+
+ IMB_anim_get_fname(anim, fname, FILE_MAXFILE);
+ BLI_strncpy(dir, base_dir, sizeof(dir));
+ BLI_path_append(dir, sizeof(dir), fname);
+ IMB_anim_set_index_dir(anim, dir);
+}
+
+static void seq_open_anim_file(Scene *scene, Sequence *seq, bool openfile)
+{
+ char dir[FILE_MAX];
+ char name[FILE_MAX];
+ StripProxy *proxy;
+ bool use_proxy;
+ bool is_multiview_loaded = false;
+ Editing *ed = scene->ed;
+ const bool is_multiview = (seq->flag & SEQ_USE_VIEWS) != 0 &&
+ (scene->r.scemode & R_MULTIVIEW) != 0;
+
+ if ((seq->anims.first != NULL) && (((StripAnim *)seq->anims.first)->anim != NULL)) {
+ return;
+ }
+
+ /* reset all the previously created anims */
+ BKE_sequence_free_anim(seq);
+
+ BLI_join_dirfile(name, sizeof(name), seq->strip->dir, seq->strip->stripdata->name);
+ BLI_path_abs(name, BKE_main_blendfile_path_from_global());
+
+ proxy = seq->strip->proxy;
+
+ use_proxy = proxy && ((proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_DIR) != 0 ||
+ (ed->proxy_storage == SEQ_EDIT_PROXY_DIR_STORAGE));
+
+ if (use_proxy) {
+ if (ed->proxy_storage == SEQ_EDIT_PROXY_DIR_STORAGE) {
+ if (ed->proxy_dir[0] == 0) {
+ BLI_strncpy(dir, "//BL_proxy", sizeof(dir));
+ }
+ else {
+ BLI_strncpy(dir, ed->proxy_dir, sizeof(dir));
+ }
+ }
+ else {
+ BLI_strncpy(dir, seq->strip->proxy->dir, sizeof(dir));
+ }
+ BLI_path_abs(dir, BKE_main_blendfile_path_from_global());
+ }
+
+ if (is_multiview && seq->views_format == R_IMF_VIEWS_INDIVIDUAL) {
+ int totfiles = seq_num_files(scene, seq->views_format, true);
+ char prefix[FILE_MAX];
+ const char *ext = NULL;
+ int i;
+
+ BKE_scene_multiview_view_prefix_get(scene, name, prefix, &ext);
+
+ if (prefix[0] != '\0') {
+ for (i = 0; i < totfiles; i++) {
+ const char *suffix = BKE_scene_multiview_view_id_suffix_get(&scene->r, i);
+ char str[FILE_MAX];
+ StripAnim *sanim = MEM_mallocN(sizeof(StripAnim), "Strip Anim");
+
+ BLI_addtail(&seq->anims, sanim);
+
+ BLI_snprintf(str, sizeof(str), "%s%s%s", prefix, suffix, ext);
+
+ if (openfile) {
+ sanim->anim = openanim(str,
+ IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
+ seq->streamindex,
+ seq->strip->colorspace_settings.name);
+ }
+ else {
+ sanim->anim = openanim_noload(str,
+ IB_rect |
+ ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
+ seq->streamindex,
+ seq->strip->colorspace_settings.name);
+ }
+
+ if (sanim->anim) {
+ /* we already have the suffix */
+ IMB_suffix_anim(sanim->anim, suffix);
+ }
+ else {
+ if (openfile) {
+ sanim->anim = openanim(name,
+ IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
+ seq->streamindex,
+ seq->strip->colorspace_settings.name);
+ }
+ else {
+ sanim->anim = openanim_noload(name,
+ IB_rect |
+ ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
+ seq->streamindex,
+ seq->strip->colorspace_settings.name);
+ }
+
+ /* No individual view files - monoscopic, stereo 3d or EXR multi-view. */
+ totfiles = 1;
+ }
+
+ if (sanim->anim && use_proxy) {
+ seq_proxy_index_dir_set(sanim->anim, dir);
+ }
+ }
+ is_multiview_loaded = true;
+ }
+ }
+
+ if (is_multiview_loaded == false) {
+ StripAnim *sanim;
+
+ sanim = MEM_mallocN(sizeof(StripAnim), "Strip Anim");
+ BLI_addtail(&seq->anims, sanim);
+
+ if (openfile) {
+ sanim->anim = openanim(name,
+ IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
+ seq->streamindex,
+ seq->strip->colorspace_settings.name);
+ }
+ else {
+ sanim->anim = openanim_noload(name,
+ IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
+ seq->streamindex,
+ seq->strip->colorspace_settings.name);
+ }
+
+ if (sanim->anim && use_proxy) {
+ seq_proxy_index_dir_set(sanim->anim, dir);
+ }
+ }
+}
+
+static bool seq_proxy_get_custom_file_fname(Sequence *seq, char *name, const int view_id)
+{
+ char fname[FILE_MAXFILE];
+ char suffix[24];
+ StripProxy *proxy = seq->strip->proxy;
+
+ if (proxy == NULL) {
+ return false;
+ }
+
+ BLI_join_dirfile(fname, PROXY_MAXFILE, proxy->dir, proxy->file);
+ BLI_path_abs(fname, BKE_main_blendfile_path_from_global());
+
+ if (view_id > 0) {
+ BLI_snprintf(suffix, sizeof(suffix), "_%d", view_id);
+ /* TODO(sergey): This will actually append suffix after extension
+ * which is weird but how was originally coded in multi-view branch.
+ */
+ BLI_snprintf(name, PROXY_MAXFILE, "%s_%s", fname, suffix);
+ }
+ else {
+ BLI_strncpy(name, fname, PROXY_MAXFILE);
+ }
+
+ return true;
+}
+
+static bool seq_proxy_get_fname(Editing *ed,
+ Sequence *seq,
+ int cfra,
+ eSpaceSeq_Proxy_RenderSize render_size,
+ char *name,
+ const int view_id)
+{
+ char dir[PROXY_MAXFILE];
+ char suffix[24] = {'\0'};
+ StripProxy *proxy = seq->strip->proxy;
+
+ if (proxy == NULL) {
+ return false;
+ }
+
+ /* Multi-view suffix. */
+ if (view_id > 0) {
+ BLI_snprintf(suffix, sizeof(suffix), "_%d", view_id);
+ }
+
+ /* Per strip with Custom file situation is handled separately. */
+ if (proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_FILE &&
+ ed->proxy_storage != SEQ_EDIT_PROXY_DIR_STORAGE) {
+ if (seq_proxy_get_custom_file_fname(seq, name, view_id)) {
+ return true;
+ }
+ }
+
+ if (ed->proxy_storage == SEQ_EDIT_PROXY_DIR_STORAGE) {
+ /* Per project default. */
+ if (ed->proxy_dir[0] == 0) {
+ BLI_strncpy(dir, "//BL_proxy", sizeof(dir));
+ }
+ else { /* Per project with custom dir. */
+ BLI_strncpy(dir, ed->proxy_dir, sizeof(dir));
+ }
+ BLI_path_abs(name, BKE_main_blendfile_path_from_global());
+ }
+ else {
+ /* Pre strip with custom dir. */
+ if (proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_DIR) {
+ BLI_strncpy(dir, seq->strip->proxy->dir, sizeof(dir));
+ }
+ else { /* Per strip default. */
+ BLI_snprintf(dir, PROXY_MAXFILE, "%s/BL_proxy", seq->strip->dir);
+ }
+ }
+
+ /* Proxy size number to be used in path. */
+ int proxy_size_number = BKE_sequencer_rendersize_to_scale_factor(render_size) * 100;
+
+ BLI_snprintf(name,
+ PROXY_MAXFILE,
+ "%s/images/%d/%s_proxy%s",
+ dir,
+ proxy_size_number,
+ BKE_sequencer_give_stripelem(seq, cfra)->name,
+ suffix);
+ BLI_path_abs(name, BKE_main_blendfile_path_from_global());
+ strcat(name, ".jpg");
+
+ return true;
+}
+
+static bool seq_can_use_proxy(Sequence *seq, IMB_Proxy_Size psize)
+{
+ if (seq->strip->proxy == NULL) {
+ return false;
+ }
+ short size_flags = seq->strip->proxy->build_size_flags;
+ return (seq->flag & SEQ_USE_PROXY) != 0 && psize != IMB_PROXY_NONE && (size_flags & psize) != 0;
+}
+
+static ImBuf *seq_proxy_fetch(const SeqRenderData *context, Sequence *seq, int cfra)
+{
+ char name[PROXY_MAXFILE];
+ StripProxy *proxy = seq->strip->proxy;
+ const eSpaceSeq_Proxy_RenderSize psize = context->preview_render_size;
+ Editing *ed = context->scene->ed;
+ StripAnim *sanim;
+
+ /* only use proxies, if they are enabled (even if present!) */
+ if (!seq_can_use_proxy(seq, seq_rendersize_to_proxysize(psize))) {
+ return NULL;
+ }
+
+ if (proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_FILE) {
+ int frameno = (int)BKE_sequencer_give_stripelem_index(seq, cfra) + seq->anim_startofs;
+ if (proxy->anim == NULL) {
+ if (seq_proxy_get_fname(ed, seq, cfra, psize, name, context->view_id) == 0) {
+ return NULL;
+ }
+
+ proxy->anim = openanim(name, IB_rect, 0, seq->strip->colorspace_settings.name);
+ }
+ if (proxy->anim == NULL) {
+ return NULL;
+ }
+
+ seq_open_anim_file(context->scene, seq, true);
+ sanim = seq->anims.first;
+
+ frameno = IMB_anim_index_get_frame_index(
+ sanim ? sanim->anim : NULL, seq->strip->proxy->tc, frameno);
+
+ return IMB_anim_absolute(proxy->anim, frameno, IMB_TC_NONE, IMB_PROXY_NONE);
+ }
+
+ if (seq_proxy_get_fname(ed, seq, cfra, psize, name, context->view_id) == 0) {
+ return NULL;
+ }
+
+ if (BLI_exists(name)) {
+ ImBuf *ibuf = IMB_loadiffname(name, IB_rect, NULL);
+
+ if (ibuf) {
+ sequencer_imbuf_assign_spaces(context->scene, ibuf);
+ }
+
+ return ibuf;
+ }
+
+ return NULL;
+}
+
+static void seq_proxy_build_frame(const SeqRenderData *context,
+ SeqRenderState *state,
+ Sequence *seq,
+ int cfra,
+ int proxy_render_size,
+ const bool overwrite)
+{
+ char name[PROXY_MAXFILE];
+ int quality;
+ int rectx, recty;
+ int ok;
+ ImBuf *ibuf_tmp, *ibuf;
+ Editing *ed = context->scene->ed;
+
+ if (!seq_proxy_get_fname(ed, seq, cfra, proxy_render_size, name, context->view_id)) {
+ return;
+ }
+
+ if (!overwrite && BLI_exists(name)) {
+ return;
+ }
+
+ ibuf_tmp = seq_render_strip(context, state, seq, cfra);
+
+ rectx = (proxy_render_size * ibuf_tmp->x) / 100;
+ recty = (proxy_render_size * ibuf_tmp->y) / 100;
+
+ if (ibuf_tmp->x != rectx || ibuf_tmp->y != recty) {
+ ibuf = IMB_dupImBuf(ibuf_tmp);
+ IMB_metadata_copy(ibuf, ibuf_tmp);
+ IMB_freeImBuf(ibuf_tmp);
+ IMB_scalefastImBuf(ibuf, (short)rectx, (short)recty);
+ }
+ else {
+ ibuf = ibuf_tmp;
+ }
+
+ /* depth = 32 is intentionally left in, otherwise ALPHA channels
+ * won't work... */
+ quality = seq->strip->proxy->quality;
+ ibuf->ftype = IMB_FTYPE_JPG;
+ ibuf->foptions.quality = quality;
+
+ /* unsupported feature only confuses other s/w */
+ if (ibuf->planes == 32) {
+ ibuf->planes = 24;
+ }
+
+ BLI_make_existing_file(name);
+
+ ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
+ if (ok == 0) {
+ perror(name);
+ }
+
+ IMB_freeImBuf(ibuf);
+}
+
+/**
+ * Returns whether the file this context would read from even exist,
+ * if not, don't create the context
+ */
+static bool seq_proxy_multiview_context_invalid(Sequence *seq, Scene *scene, const int view_id)
+{
+ if ((scene->r.scemode & R_MULTIVIEW) == 0) {
+ return false;
+ }
+
+ if ((seq->type == SEQ_TYPE_IMAGE) && (seq->views_format == R_IMF_VIEWS_INDIVIDUAL)) {
+ static char prefix[FILE_MAX];
+ static const char *ext = NULL;
+ char str[FILE_MAX];
+
+ if (view_id == 0) {
+ char path[FILE_MAX];
+ BLI_join_dirfile(path, sizeof(path), seq->strip->dir, seq->strip->stripdata->name);
+ BLI_path_abs(path, BKE_main_blendfile_path_from_global());
+ BKE_scene_multiview_view_prefix_get(scene, path, prefix, &ext);
+ }
+ else {
+ prefix[0] = '\0';
+ }
+
+ if (prefix[0] == '\0') {
+ return view_id != 0;
+ }
+
+ seq_multiview_name(scene, view_id, prefix, ext, str, FILE_MAX);
+
+ if (BLI_access(str, R_OK) == 0) {
+ return false;
+ }
+
+ return view_id != 0;
+ }
+ return false;
+}
+
+/**
+ * This returns the maximum possible number of required contexts
+ */
+static int seq_proxy_context_count(Sequence *seq, Scene *scene)
+{
+ int num_views = 1;
+
+ if ((scene->r.scemode & R_MULTIVIEW) == 0) {
+ return 1;
+ }
+
+ switch (seq->type) {
+ case SEQ_TYPE_MOVIE: {
+ num_views = BLI_listbase_count(&seq->anims);
+ break;
+ }
+ case SEQ_TYPE_IMAGE: {
+ switch (seq->views_format) {
+ case R_IMF_VIEWS_INDIVIDUAL:
+ num_views = BKE_scene_multiview_num_views_get(&scene->r);
+ break;
+ case R_IMF_VIEWS_STEREO_3D:
+ num_views = 2;
+ break;
+ case R_IMF_VIEWS_MULTIVIEW:
+ /* not supported at the moment */
+ /* pass through */
+ default:
+ num_views = 1;
+ }
+ break;
+ }
+ }
+
+ return num_views;
+}
+
+bool BKE_sequencer_proxy_rebuild_context(Main *bmain,
+ Depsgraph *depsgraph,
+ Scene *scene,
+ Sequence *seq,
+ struct GSet *file_list,
+ ListBase *queue)
+{
+ SeqIndexBuildContext *context;
+ Sequence *nseq;
+ LinkData *link;
+ int num_files;
+ int i;
+
+ if (!seq->strip || !seq->strip->proxy) {
+ return true;
+ }
+
+ if (!(seq->flag & SEQ_USE_PROXY)) {
+ return true;
+ }
+
+ num_files = seq_proxy_context_count(seq, scene);
+
+ for (i = 0; i < num_files; i++) {
+ if (seq_proxy_multiview_context_invalid(seq, scene, i)) {
+ continue;
+ }
+
+ context = MEM_callocN(sizeof(SeqIndexBuildContext), "seq proxy rebuild context");
+
+ nseq = BKE_sequence_dupli_recursive(scene, scene, NULL, seq, 0);
+
+ context->tc_flags = nseq->strip->proxy->build_tc_flags;
+ context->size_flags = nseq->strip->proxy->build_size_flags;
+ context->quality = nseq->strip->proxy->quality;
+ context->overwrite = (nseq->strip->proxy->build_flags & SEQ_PROXY_SKIP_EXISTING) == 0;
+
+ context->bmain = bmain;
+ context->depsgraph = depsgraph;
+ context->scene = scene;
+ context->orig_seq = seq;
+ context->seq = nseq;
+
+ context->view_id = i; /* only for images */
+
+ if (nseq->type == SEQ_TYPE_MOVIE) {
+ StripAnim *sanim;
+
+ seq_open_anim_file(scene, nseq, true);
+ sanim = BLI_findlink(&nseq->anims, i);
+
+ if (sanim->anim) {
+ context->index_context = IMB_anim_index_rebuild_context(sanim->anim,
+ context->tc_flags,
+ context->size_flags,
+ context->quality,
+ context->overwrite,
+ file_list);
+ }
+ if (!context->index_context) {
+ MEM_freeN(context);
+ return false;
+ }
+ }
+
+ link = BLI_genericNodeN(context);
+ BLI_addtail(queue, link);
+ }
+ return true;
+}
+
+void BKE_sequencer_proxy_rebuild(SeqIndexBuildContext *context,
+ short *stop,
+ short *do_update,
+ float *progress)
+{
+ const bool overwrite = context->overwrite;
+ SeqRenderData render_context;
+ Sequence *seq = context->seq;
+ Scene *scene = context->scene;
+ Main *bmain = context->bmain;
+ int cfra;
+
+ if (seq->type == SEQ_TYPE_MOVIE) {
+ if (context->index_context) {
+ IMB_anim_index_rebuild(context->index_context, stop, do_update, progress);
+ }
+
+ return;
+ }
+
+ if (!(seq->flag & SEQ_USE_PROXY)) {
+ return;
+ }
+
+ /* that's why it is called custom... */
+ if (seq->strip->proxy && seq->strip->proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_FILE) {
+ return;
+ }
+
+ /* fail safe code */
+
+ BKE_sequencer_new_render_data(bmain,
+ context->depsgraph,
+ context->scene,
+ roundf((scene->r.size * (float)scene->r.xsch) / 100.0f),
+ roundf((scene->r.size * (float)scene->r.ysch) / 100.0f),
+ 100,
+ false,
+ &render_context);
+
+ render_context.skip_cache = true;
+ render_context.is_proxy_render = true;
+ render_context.view_id = context->view_id;
+
+ SeqRenderState state;
+ sequencer_state_init(&state);
+
+ for (cfra = seq->startdisp + seq->startstill; cfra < seq->enddisp - seq->endstill; cfra++) {
+ if (context->size_flags & IMB_PROXY_25) {
+ seq_proxy_build_frame(&render_context, &state, seq, cfra, 25, overwrite);
+ }
+ if (context->size_flags & IMB_PROXY_50) {
+ seq_proxy_build_frame(&render_context, &state, seq, cfra, 50, overwrite);
+ }
+ if (context->size_flags & IMB_PROXY_75) {
+ seq_proxy_build_frame(&render_context, &state, seq, cfra, 75, overwrite);
+ }
+ if (context->size_flags & IMB_PROXY_100) {
+ seq_proxy_build_frame(&render_context, &state, seq, cfra, 100, overwrite);
+ }
+
+ *progress = (float)(cfra - seq->startdisp - seq->startstill) /
+ (seq->enddisp - seq->endstill - seq->startdisp - seq->startstill);
+ *do_update = true;
+
+ if (*stop || G.is_break) {
+ break;
+ }
+ }
+}
+
+void BKE_sequencer_proxy_rebuild_finish(SeqIndexBuildContext *context, bool stop)
+{
+ if (context->index_context) {
+ StripAnim *sanim;
+
+ for (sanim = context->seq->anims.first; sanim; sanim = sanim->next) {
+ IMB_close_anim_proxies(sanim->anim);
+ }
+
+ for (sanim = context->orig_seq->anims.first; sanim; sanim = sanim->next) {
+ IMB_close_anim_proxies(sanim->anim);
+ }
+
+ IMB_anim_index_rebuild_finish(context->index_context, stop);
+ }
+
+ seq_free_sequence_recurse(NULL, context->seq, true);
+
+ MEM_freeN(context);
+}
+
+void BKE_sequencer_proxy_set(struct Sequence *seq, bool value)
+{
+ if (value) {
+ seq->flag |= SEQ_USE_PROXY;
+ if (seq->strip->proxy == NULL) {
+ seq->strip->proxy = MEM_callocN(sizeof(struct StripProxy), "StripProxy");
+ seq->strip->proxy->quality = 90;
+ seq->strip->proxy->build_tc_flags = SEQ_PROXY_TC_ALL;
+ seq->strip->proxy->build_size_flags = SEQ_PROXY_IMAGE_SIZE_25;
+ }
+ }
+ else {
+ seq->flag &= ~SEQ_USE_PROXY;
+ }
+}
+
+/*********************** color balance *************************/
+
+static StripColorBalance calc_cb(StripColorBalance *cb_)
+{
+ StripColorBalance cb = *cb_;
+ int c;
+
+ for (c = 0; c < 3; c++) {
+ cb.lift[c] = 2.0f - cb.lift[c];
+ }
+
+ if (cb.flag & SEQ_COLOR_BALANCE_INVERSE_LIFT) {
+ for (c = 0; c < 3; c++) {
+ /* tweak to give more subtle results
+ * values above 1.0 are scaled */
+ if (cb.lift[c] > 1.0f) {
+ cb.lift[c] = pow(cb.lift[c] - 1.0f, 2.0) + 1.0;
+ }
+
+ cb.lift[c] = 2.0f - cb.lift[c];
+ }
+ }
+
+ if (cb.flag & SEQ_COLOR_BALANCE_INVERSE_GAIN) {
+ for (c = 0; c < 3; c++) {
+ if (cb.gain[c] != 0.0f) {
+ cb.gain[c] = 1.0f / cb.gain[c];
+ }
+ else {
+ cb.gain[c] = 1000000; /* should be enough :) */
+ }
+ }
+ }
+
+ if (!(cb.flag & SEQ_COLOR_BALANCE_INVERSE_GAMMA)) {
+ for (c = 0; c < 3; c++) {
+ if (cb.gamma[c] != 0.0f) {
+ cb.gamma[c] = 1.0f / cb.gamma[c];
+ }
+ else {
+ cb.gamma[c] = 1000000; /* should be enough :) */
+ }
+ }
+ }
+
+ return cb;
+}
+
+/* note: lift is actually 2-lift */
+MINLINE float color_balance_fl(
+ float in, const float lift, const float gain, const float gamma, const float mul)
+{
+ float x = (((in - 1.0f) * lift) + 1.0f) * gain;
+
+ /* prevent NaN */
+ if (x < 0.f) {
+ x = 0.f;
+ }
+
+ x = powf(x, gamma) * mul;
+ CLAMP(x, FLT_MIN, FLT_MAX);
+ return x;
+}
+
+static void make_cb_table_float(float lift, float gain, float gamma, float *table, float mul)
+{
+ int y;
+
+ for (y = 0; y < 256; y++) {
+ float v = color_balance_fl((float)y * (1.0f / 255.0f), lift, gain, gamma, mul);
+
+ table[y] = v;
+ }
+}
+
+static void color_balance_byte_byte(StripColorBalance *cb_,
+ unsigned char *rect,
+ unsigned char *mask_rect,
+ int width,
+ int height,
+ float mul)
+{
+ // unsigned char cb_tab[3][256];
+ unsigned char *cp = rect;
+ unsigned char *e = cp + width * 4 * height;
+ unsigned char *m = mask_rect;
+
+ StripColorBalance cb = calc_cb(cb_);
+
+ while (cp < e) {
+ float p[4];
+ int c;
+
+ straight_uchar_to_premul_float(p, cp);
+
+ for (c = 0; c < 3; c++) {
+ float t = color_balance_fl(p[c], cb.lift[c], cb.gain[c], cb.gamma[c], mul);
+
+ if (m) {
+ float m_normal = (float)m[c] / 255.0f;
+
+ p[c] = p[c] * (1.0f - m_normal) + t * m_normal;
+ }
+ else {
+ p[c] = t;
+ }
+ }
+
+ premul_float_to_straight_uchar(cp, p);
+
+ cp += 4;
+ if (m) {
+ m += 4;
+ }
+ }
+}
+
+static void color_balance_byte_float(StripColorBalance *cb_,
+ unsigned char *rect,
+ float *rect_float,
+ unsigned char *mask_rect,
+ int width,
+ int height,
+ float mul)
+{
+ float cb_tab[4][256];
+ int c, i;
+ unsigned char *p = rect;
+ unsigned char *e = p + width * 4 * height;
+ unsigned char *m = mask_rect;
+ float *o;
+ StripColorBalance cb;
+
+ o = rect_float;
+
+ cb = calc_cb(cb_);
+
+ for (c = 0; c < 3; c++) {
+ make_cb_table_float(cb.lift[c], cb.gain[c], cb.gamma[c], cb_tab[c], mul);
+ }
+
+ for (i = 0; i < 256; i++) {
+ cb_tab[3][i] = ((float)i) * (1.0f / 255.0f);
+ }
+
+ while (p < e) {
+ if (m) {
+ const float t[3] = {m[0] / 255.0f, m[1] / 255.0f, m[2] / 255.0f};
+
+ p[0] = p[0] * (1.0f - t[0]) + t[0] * cb_tab[0][p[0]];
+ p[1] = p[1] * (1.0f - t[1]) + t[1] * cb_tab[1][p[1]];
+ p[2] = p[2] * (1.0f - t[2]) + t[2] * cb_tab[2][p[2]];
+
+ m += 4;
+ }
+ else {
+ o[0] = cb_tab[0][p[0]];
+ o[1] = cb_tab[1][p[1]];
+ o[2] = cb_tab[2][p[2]];
+ }
+
+ o[3] = cb_tab[3][p[3]];
+
+ p += 4;
+ o += 4;
+ }
+}
+
+static void color_balance_float_float(StripColorBalance *cb_,
+ float *rect_float,
+ const float *mask_rect_float,
+ int width,
+ int height,
+ float mul)
+{
+ float *p = rect_float;
+ const float *e = rect_float + width * 4 * height;
+ const float *m = mask_rect_float;
+ StripColorBalance cb = calc_cb(cb_);
+
+ while (p < e) {
+ int c;
+ for (c = 0; c < 3; c++) {
+ float t = color_balance_fl(p[c], cb.lift[c], cb.gain[c], cb.gamma[c], mul);
+
+ if (m) {
+ p[c] = p[c] * (1.0f - m[c]) + t * m[c];
+ }
+ else {
+ p[c] = t;
+ }
+ }
+
+ p += 4;
+ if (m) {
+ m += 4;
+ }
+ }
+}
+
+typedef struct ColorBalanceInitData {
+ StripColorBalance *cb;
+ ImBuf *ibuf;
+ float mul;
+ ImBuf *mask;
+ bool make_float;
+} ColorBalanceInitData;
+
+typedef struct ColorBalanceThread {
+ StripColorBalance *cb;
+ float mul;
+
+ int width, height;
+
+ unsigned char *rect, *mask_rect;
+ float *rect_float, *mask_rect_float;
+
+ bool make_float;
+} ColorBalanceThread;
+
+static void color_balance_init_handle(void *handle_v,
+ int start_line,
+ int tot_line,
+ void *init_data_v)
+{
+ ColorBalanceThread *handle = (ColorBalanceThread *)handle_v;
+ ColorBalanceInitData *init_data = (ColorBalanceInitData *)init_data_v;
+ ImBuf *ibuf = init_data->ibuf;
+ ImBuf *mask = init_data->mask;
+
+ int offset = 4 * start_line * ibuf->x;
+
+ memset(handle, 0, sizeof(ColorBalanceThread));
+
+ handle->cb = init_data->cb;
+ handle->mul = init_data->mul;
+ handle->width = ibuf->x;
+ handle->height = tot_line;
+ handle->make_float = init_data->make_float;
+
+ if (ibuf->rect) {
+ handle->rect = (unsigned char *)ibuf->rect + offset;
+ }
+
+ if (ibuf->rect_float) {
+ handle->rect_float = ibuf->rect_float + offset;
+ }
+
+ if (mask) {
+ if (mask->rect) {
+ handle->mask_rect = (unsigned char *)mask->rect + offset;
+ }
+
+ if (mask->rect_float) {
+ handle->mask_rect_float = mask->rect_float + offset;
+ }
+ }
+ else {
+ handle->mask_rect = NULL;
+ handle->mask_rect_float = NULL;
+ }
+}
+
+static void *color_balance_do_thread(void *thread_data_v)
+{
+ ColorBalanceThread *thread_data = (ColorBalanceThread *)thread_data_v;
+ StripColorBalance *cb = thread_data->cb;
+ int width = thread_data->width, height = thread_data->height;
+ unsigned char *rect = thread_data->rect;
+ unsigned char *mask_rect = thread_data->mask_rect;
+ float *rect_float = thread_data->rect_float;
+ float *mask_rect_float = thread_data->mask_rect_float;
+ float mul = thread_data->mul;
+
+ if (rect_float) {
+ color_balance_float_float(cb, rect_float, mask_rect_float, width, height, mul);
+ }
+ else if (thread_data->make_float) {
+ color_balance_byte_float(cb, rect, rect_float, mask_rect, width, height, mul);
+ }
+ else {
+ color_balance_byte_byte(cb, rect, mask_rect, width, height, mul);
+ }
+
+ return NULL;
+}
+
+/**
+ * \a cfra is offset by \a fra_offset only in case we are using a real mask.
+ */
+ImBuf *BKE_sequencer_render_mask_input(const SeqRenderData *context,
+ int mask_input_type,
+ Sequence *mask_sequence,
+ Mask *mask_id,
+ int cfra,
+ int fra_offset,
+ bool make_float)
+{
+ ImBuf *mask_input = NULL;
+
+ if (mask_input_type == SEQUENCE_MASK_INPUT_STRIP) {
+ if (mask_sequence) {
+ SeqRenderState state;
+ sequencer_state_init(&state);
+
+ mask_input = seq_render_strip(context, &state, mask_sequence, cfra);
+
+ if (make_float) {
+ if (!mask_input->rect_float) {
+ IMB_float_from_rect(mask_input);
+ }
+ }
+ else {
+ if (!mask_input->rect) {
+ IMB_rect_from_float(mask_input);
+ }
+ }
+ }
+ }
+ else if (mask_input_type == SEQUENCE_MASK_INPUT_ID) {
+ mask_input = seq_render_mask(context, mask_id, cfra - fra_offset, make_float);
+ }
+
+ return mask_input;
+}
+
+void BKE_sequencer_color_balance_apply(
+ StripColorBalance *cb, ImBuf *ibuf, float mul, bool make_float, ImBuf *mask_input)
+{
+ ColorBalanceInitData init_data;
+
+ if (!ibuf->rect_float && make_float) {
+ imb_addrectfloatImBuf(ibuf);
+ }
+
+ init_data.cb = cb;
+ init_data.ibuf = ibuf;
+ init_data.mul = mul;
+ init_data.make_float = make_float;
+ init_data.mask = mask_input;
+
+ IMB_processor_apply_threaded(ibuf->y,
+ sizeof(ColorBalanceThread),
+ &init_data,
+ color_balance_init_handle,
+ color_balance_do_thread);
+
+ /* color balance either happens on float buffer or byte buffer, but never on both,
+ * free byte buffer if there's float buffer since float buffer would be used for
+ * color balance in favor of byte buffer
+ */
+ if (ibuf->rect_float && ibuf->rect) {
+ imb_freerectImBuf(ibuf);
+ }
+}
+
+/*
+ * input preprocessing for SEQ_TYPE_IMAGE, SEQ_TYPE_MOVIE, SEQ_TYPE_MOVIECLIP and SEQ_TYPE_SCENE
+ *
+ * Do all the things you can't really do afterwards using sequence effects
+ * (read: before rescaling to render resolution has been done)
+ *
+ * Order is important!
+ *
+ * - Deinterlace
+ * - Crop and transform in image source coordinate space
+ * - Flip X + Flip Y (could be done afterwards, backward compatibility)
+ * - Promote image to float data (affects pipeline operations afterwards)
+ * - Color balance (is most efficient in the byte -> float
+ * (future: half -> float should also work fine!)
+ * case, if done on load, since we can use lookup tables)
+ * - Premultiply
+ */
+
+bool BKE_sequencer_input_have_to_preprocess(const SeqRenderData *context,
+ Sequence *seq,
+ float UNUSED(cfra))
+{
+ float mul;
+
+ if (context && context->is_proxy_render) {
+ return false;
+ }
+
+ if (seq->flag &
+ (SEQ_FILTERY | SEQ_USE_CROP | SEQ_USE_TRANSFORM | SEQ_FLIPX | SEQ_FLIPY | SEQ_MAKE_FLOAT)) {
+ return true;
+ }
+
+ mul = seq->mul;
+
+ if (seq->blend_mode == SEQ_BLEND_REPLACE) {
+ mul *= seq->blend_opacity / 100.0f;
+ }
+
+ if (mul != 1.0f) {
+ return true;
+ }
+
+ if (seq->sat != 1.0f) {
+ return true;
+ }
+
+ if (seq->modifiers.first) {
+ return true;
+ }
+
+ return false;
+}
+
+static ImBuf *input_preprocess(const SeqRenderData *context,
+ Sequence *seq,
+ float cfra,
+ ImBuf *ibuf,
+ const bool is_proxy_image)
+{
+ Scene *scene = context->scene;
+ float mul;
+
+ ibuf = IMB_makeSingleUser(ibuf);
+
+ if ((seq->flag & SEQ_FILTERY) && !ELEM(seq->type, SEQ_TYPE_MOVIE, SEQ_TYPE_MOVIECLIP)) {
+ IMB_filtery(ibuf);
+ }
+
+ if (seq->flag & (SEQ_USE_CROP | SEQ_USE_TRANSFORM)) {
+ StripCrop c = {0};
+ StripTransform t = {0};
+
+ if (seq->flag & SEQ_USE_CROP && seq->strip->crop) {
+ c = *seq->strip->crop;
+ }
+ if (seq->flag & SEQ_USE_TRANSFORM && seq->strip->transform) {
+ t = *seq->strip->transform;
+ }
+
+ /* Calculate scale factor for current image if needed. */
+ double scale_factor, image_scale_factor = 1.0;
+ if (context->preview_render_size == SEQ_RENDER_SIZE_SCENE) {
+ scale_factor = image_scale_factor = (double)scene->r.size / 100;
+ }
+ else {
+ scale_factor = BKE_sequencer_rendersize_to_scale_factor(context->preview_render_size);
+ if (!is_proxy_image) {
+ image_scale_factor = scale_factor;
+ }
+ }
+
+ if (image_scale_factor != 1.0) {
+ if (context->for_render) {
+ IMB_scaleImBuf(ibuf, ibuf->x * image_scale_factor, ibuf->y * image_scale_factor);
+ }
+ else {
+ IMB_scalefastImBuf(ibuf, ibuf->x * image_scale_factor, ibuf->y * image_scale_factor);
+ }
+ }
+
+ t.xofs *= scale_factor;
+ t.yofs *= scale_factor;
+ c.left *= scale_factor;
+ c.right *= scale_factor;
+ c.top *= scale_factor;
+ c.bottom *= scale_factor;
+
+ int sx, sy, dx, dy;
+ sx = ibuf->x - c.left - c.right;
+ sy = ibuf->y - c.top - c.bottom;
+
+ if (seq->flag & SEQ_USE_TRANSFORM) {
+ dx = context->rectx;
+ dy = context->recty;
+ }
+ else {
+ dx = sx;
+ dy = sy;
+ }
+
+ if (c.top + c.bottom >= ibuf->y || c.left + c.right >= ibuf->x || t.xofs >= dx ||
+ t.yofs >= dy) {
+ return NULL;
+ }
+
+ ImBuf *i = IMB_allocImBuf(dx, dy, 32, ibuf->rect_float ? IB_rectfloat : IB_rect);
+ IMB_rectcpy(i, ibuf, t.xofs, t.yofs, c.left, c.bottom, sx, sy);
+ sequencer_imbuf_assign_spaces(scene, i);
+ IMB_metadata_copy(i, ibuf);
+ IMB_freeImBuf(ibuf);
+ ibuf = i;
+ }
+
+ if (seq->flag & SEQ_FLIPX) {
+ IMB_flipx(ibuf);
+ }
+
+ if (seq->flag & SEQ_FLIPY) {
+ IMB_flipy(ibuf);
+ }
+
+ if (seq->sat != 1.0f) {
+ IMB_saturation(ibuf, seq->sat);
+ }
+
+ mul = seq->mul;
+
+ if (seq->blend_mode == SEQ_BLEND_REPLACE) {
+ mul *= seq->blend_opacity / 100.0f;
+ }
+
+ if (seq->flag & SEQ_MAKE_FLOAT) {
+ if (!ibuf->rect_float) {
+ BKE_sequencer_imbuf_to_sequencer_space(scene, ibuf, true);
+ }
+
+ if (ibuf->rect) {
+ imb_freerectImBuf(ibuf);
+ }
+ }
+
+ if (mul != 1.0f) {
+ multibuf(ibuf, mul);
+ }
+
+ if (ibuf->x != context->rectx || ibuf->y != context->recty) {
+ if (context->for_render) {
+ IMB_scaleImBuf(ibuf, (short)context->rectx, (short)context->recty);
+ }
+ else {
+ IMB_scalefastImBuf(ibuf, (short)context->rectx, (short)context->recty);
+ }
+ }
+
+ if (seq->modifiers.first) {
+ ImBuf *ibuf_new = BKE_sequence_modifier_apply_stack(context, seq, ibuf, cfra);
+
+ if (ibuf_new != ibuf) {
+ IMB_metadata_copy(ibuf_new, ibuf);
+ IMB_freeImBuf(ibuf);
+ ibuf = ibuf_new;
+ }
+ }
+
+ return ibuf;
+}
+
+/*********************** strip rendering functions *************************/
+
+typedef struct RenderEffectInitData {
+ struct SeqEffectHandle *sh;
+ const SeqRenderData *context;
+ Sequence *seq;
+ float cfra, facf0, facf1;
+ ImBuf *ibuf1, *ibuf2, *ibuf3;
+
+ ImBuf *out;
+} RenderEffectInitData;
+
+typedef struct RenderEffectThread {
+ struct SeqEffectHandle *sh;
+ const SeqRenderData *context;
+ Sequence *seq;
+ float cfra, facf0, facf1;
+ ImBuf *ibuf1, *ibuf2, *ibuf3;
+
+ ImBuf *out;
+ int start_line, tot_line;
+} RenderEffectThread;
+
+static void render_effect_execute_init_handle(void *handle_v,
+ int start_line,
+ int tot_line,
+ void *init_data_v)
+{
+ RenderEffectThread *handle = (RenderEffectThread *)handle_v;
+ RenderEffectInitData *init_data = (RenderEffectInitData *)init_data_v;
+
+ handle->sh = init_data->sh;
+ handle->context = init_data->context;
+ handle->seq = init_data->seq;
+ handle->cfra = init_data->cfra;
+ handle->facf0 = init_data->facf0;
+ handle->facf1 = init_data->facf1;
+ handle->ibuf1 = init_data->ibuf1;
+ handle->ibuf2 = init_data->ibuf2;
+ handle->ibuf3 = init_data->ibuf3;
+ handle->out = init_data->out;
+
+ handle->start_line = start_line;
+ handle->tot_line = tot_line;
+}
+
+static void *render_effect_execute_do_thread(void *thread_data_v)
+{
+ RenderEffectThread *thread_data = (RenderEffectThread *)thread_data_v;
+
+ thread_data->sh->execute_slice(thread_data->context,
+ thread_data->seq,
+ thread_data->cfra,
+ thread_data->facf0,
+ thread_data->facf1,
+ thread_data->ibuf1,
+ thread_data->ibuf2,
+ thread_data->ibuf3,
+ thread_data->start_line,
+ thread_data->tot_line,
+ thread_data->out);
+
+ return NULL;
+}
+
+ImBuf *BKE_sequencer_effect_execute_threaded(struct SeqEffectHandle *sh,
+ const SeqRenderData *context,
+ Sequence *seq,
+ float cfra,
+ float facf0,
+ float facf1,
+ ImBuf *ibuf1,
+ ImBuf *ibuf2,
+ ImBuf *ibuf3)
+{
+ RenderEffectInitData init_data;
+ ImBuf *out = sh->init_execution(context, ibuf1, ibuf2, ibuf3);
+
+ init_data.sh = sh;
+ init_data.context = context;
+ init_data.seq = seq;
+ init_data.cfra = cfra;
+ init_data.facf0 = facf0;
+ init_data.facf1 = facf1;
+ init_data.ibuf1 = ibuf1;
+ init_data.ibuf2 = ibuf2;
+ init_data.ibuf3 = ibuf3;
+ init_data.out = out;
+
+ IMB_processor_apply_threaded(out->y,
+ sizeof(RenderEffectThread),
+ &init_data,
+ render_effect_execute_init_handle,
+ render_effect_execute_do_thread);
+
+ return out;
+}
+
+static ImBuf *seq_render_effect_strip_impl(const SeqRenderData *context,
+ SeqRenderState *state,
+ Sequence *seq,
+ float cfra)
+{
+ Scene *scene = context->scene;
+ float fac, facf;
+ int early_out;
+ int i;
+ struct SeqEffectHandle sh = BKE_sequence_get_effect(seq);
+ FCurve *fcu = NULL;
+ ImBuf *ibuf[3];
+ Sequence *input[3];
+ ImBuf *out = NULL;
+
+ ibuf[0] = ibuf[1] = ibuf[2] = NULL;
+
+ input[0] = seq->seq1;
+ input[1] = seq->seq2;
+ input[2] = seq->seq3;
+
+ if (!sh.execute && !(sh.execute_slice && sh.init_execution)) {
+ /* effect not supported in this version... */
+ out = IMB_allocImBuf(context->rectx, context->recty, 32, IB_rect);
+ return out;
+ }
+
+ if (seq->flag & SEQ_USE_EFFECT_DEFAULT_FADE) {
+ sh.get_default_fac(seq, cfra, &fac, &facf);
+ facf = fac;
+ }
+ else {
+ fcu = id_data_find_fcurve(&scene->id, seq, &RNA_Sequence, "effect_fader", 0, NULL);
+ if (fcu) {
+ fac = facf = evaluate_fcurve(fcu, cfra);
+ }
+ else {
+ fac = facf = seq->effect_fader;
+ }
+ }
+
+ early_out = sh.early_out(seq, fac, facf);
+
+ switch (early_out) {
+ case EARLY_NO_INPUT:
+ out = sh.execute(context, seq, cfra, fac, facf, NULL, NULL, NULL);
+ break;
+ case EARLY_DO_EFFECT:
+ for (i = 0; i < 3; i++) {
+ /* Speed effect requires time remapping of `cfra` for input(s). */
+ if (input[0] && seq->type == SEQ_TYPE_SPEED) {
+ float target_frame = BKE_sequencer_speed_effect_target_frame_get(context, seq, cfra, i);
+ ibuf[i] = seq_render_strip(context, state, input[0], target_frame);
+ }
+ else { /* Other effects. */
+ if (input[i]) {
+ ibuf[i] = seq_render_strip(context, state, input[i], cfra);
+ }
+ }
+ }
+
+ if (ibuf[0] && (ibuf[1] || BKE_sequence_effect_get_num_inputs(seq->type) == 1)) {
+ if (sh.multithreaded) {
+ out = BKE_sequencer_effect_execute_threaded(
+ &sh, context, seq, cfra, fac, facf, ibuf[0], ibuf[1], ibuf[2]);
+ }
+ else {
+ out = sh.execute(context, seq, cfra, fac, facf, ibuf[0], ibuf[1], ibuf[2]);
+ }
+ }
+ break;
+ case EARLY_USE_INPUT_1:
+ if (input[0]) {
+ out = seq_render_strip(context, state, input[0], cfra);
+ }
+ break;
+ case EARLY_USE_INPUT_2:
+ if (input[1]) {
+ out = seq_render_strip(context, state, input[1], cfra);
+ }
+ break;
+ }
+
+ for (i = 0; i < 3; i++) {
+ IMB_freeImBuf(ibuf[i]);
+ }
+
+ if (out == NULL) {
+ out = IMB_allocImBuf(context->rectx, context->recty, 32, IB_rect);
+ }
+
+ return out;
+}
+
+/**
+ * Render individual view for multi-view or single (default view) for mono-view.
+ */
+static ImBuf *seq_render_image_strip_view(const SeqRenderData *context,
+ Sequence *seq,
+ char *name,
+ char *prefix,
+ const char *ext,
+ int view_id)
+{
+
+ ImBuf *ibuf = NULL;
+
+ int flag = IB_rect | IB_metadata;
+ if (seq->alpha_mode == SEQ_ALPHA_PREMUL) {
+ flag |= IB_alphamode_premul;
+ }
+
+ if (prefix[0] == '\0') {
+ ibuf = IMB_loadiffname(name, flag, seq->strip->colorspace_settings.name);
+ }
+ else {
+ char str[FILE_MAX];
+ BKE_scene_multiview_view_prefix_get(context->scene, name, prefix, &ext);
+ seq_multiview_name(context->scene, view_id, prefix, ext, str, FILE_MAX);
+ ibuf = IMB_loadiffname(str, flag, seq->strip->colorspace_settings.name);
+ }
+
+ if (ibuf == NULL) {
+ return NULL;
+ }
+
+ /* We don't need both (speed reasons)! */
+ if (ibuf->rect_float != NULL && ibuf->rect != NULL) {
+ imb_freerectImBuf(ibuf);
+ }
+
+ /* All sequencer color is done in SRGB space, linear gives odd cross-fades. */
+ BKE_sequencer_imbuf_to_sequencer_space(context->scene, ibuf, false);
+
+ return ibuf;
+}
+
+static bool seq_image_strip_is_multiview_render(
+ Scene *scene, Sequence *seq, int totfiles, char *name, char *r_prefix, const char *r_ext)
+{
+ if (totfiles > 1) {
+ BKE_scene_multiview_view_prefix_get(scene, name, r_prefix, &r_ext);
+ if (r_prefix[0] == '\0') {
+ return false;
+ }
+ }
+ else {
+ r_prefix[0] = '\0';
+ }
+
+ return (seq->flag & SEQ_USE_VIEWS) != 0 && (scene->r.scemode & R_MULTIVIEW) != 0;
+}
+
+static ImBuf *seq_render_image_strip(const SeqRenderData *context,
+ Sequence *seq,
+ float UNUSED(nr),
+ float cfra,
+ bool *r_is_proxy_image)
+{
+ char name[FILE_MAX];
+ const char *ext = NULL;
+ char prefix[FILE_MAX];
+ ImBuf *ibuf = NULL;
+
+ StripElem *s_elem = BKE_sequencer_give_stripelem(seq, cfra);
+ if (s_elem == NULL) {
+ return NULL;
+ }
+
+ BLI_join_dirfile(name, sizeof(name), seq->strip->dir, s_elem->name);
+ BLI_path_abs(name, BKE_main_blendfile_path_from_global());
+
+ /* Try to get a proxy image. */
+ ibuf = seq_proxy_fetch(context, seq, cfra);
+ if (ibuf != NULL) {
+ s_elem->orig_width = ibuf->x;
+ s_elem->orig_height = ibuf->y;
+ *r_is_proxy_image = true;
+ return ibuf;
+ }
+
+ /* Proxy not found, render original. */
+ const int totfiles = seq_num_files(context->scene, seq->views_format, true);
+ bool is_multiview_render = seq_image_strip_is_multiview_render(
+ context->scene, seq, totfiles, name, prefix, ext);
+
+ if (is_multiview_render) {
+ int totviews = BKE_scene_multiview_num_views_get(&context->scene->r);
+ ImBuf **ibufs_arr = MEM_callocN(sizeof(ImBuf *) * totviews, "Sequence Image Views Imbufs");
+
+ for (int view_id = 0; view_id < totfiles; view_id++) {
+ ibufs_arr[view_id] = seq_render_image_strip_view(context, seq, name, prefix, ext, view_id);
+ }
+
+ if (ibufs_arr[0] == NULL) {
+ return NULL;
+ }
+
+ if (seq->views_format == R_IMF_VIEWS_STEREO_3D) {
+ IMB_ImBufFromStereo3d(seq->stereo3d_format, ibufs_arr[0], &ibufs_arr[0], &ibufs_arr[1]);
+ }
+
+ for (int view_id = 0; view_id < totviews; view_id++) {
+ SeqRenderData localcontext = *context;
+ localcontext.view_id = view_id;
+
+ if (view_id != context->view_id) {
+ ibufs_arr[view_id] = seq_render_preprocess_ibuf(
+ &localcontext, seq, ibufs_arr[view_id], cfra, clock(), true, false);
+ }
+ }
+
+ /* Return the original requested ImBuf. */
+ ibuf = ibufs_arr[context->view_id];
+
+ /* Remove the others (decrease their refcount). */
+ for (int view_id = 0; view_id < totviews; view_id++) {
+ if (ibufs_arr[view_id] != ibuf) {
+ IMB_freeImBuf(ibufs_arr[view_id]);
+ }
+ }
+
+ MEM_freeN(ibufs_arr);
+ }
+ else {
+ ibuf = seq_render_image_strip_view(context, seq, name, prefix, ext, context->view_id);
+ }
+
+ if (ibuf == NULL) {
+ return NULL;
+ }
+
+ s_elem->orig_width = ibuf->x;
+ s_elem->orig_height = ibuf->y;
+
+ return ibuf;
+}
+
+static ImBuf *seq_render_movie_strip_custom_file_proxy(const SeqRenderData *context,
+ Sequence *seq,
+ int cfra)
+{
+ char name[PROXY_MAXFILE];
+ StripProxy *proxy = seq->strip->proxy;
+
+ if (proxy->anim == NULL) {
+ if (seq_proxy_get_custom_file_fname(seq, name, context->view_id)) {
+ proxy->anim = openanim(name, IB_rect, 0, seq->strip->colorspace_settings.name);
+ }
+ if (proxy->anim == NULL) {
+ return NULL;
+ }
+ }
+
+ int frameno = (int)BKE_sequencer_give_stripelem_index(seq, cfra) + seq->anim_startofs;
+ return IMB_anim_absolute(proxy->anim, frameno, IMB_TC_NONE, IMB_PROXY_NONE);
+}
+
+/**
+ * Render individual view for multi-view or single (default view) for mono-view.
+ */
+static ImBuf *seq_render_movie_strip_view(const SeqRenderData *context,
+ Sequence *seq,
+ float nr,
+ float cfra,
+ StripAnim *sanim,
+ bool *r_is_proxy_image)
+{
+ ImBuf *ibuf = NULL;
+ IMB_Proxy_Size psize = seq_rendersize_to_proxysize(context->preview_render_size);
+
+ IMB_anim_set_preseek(sanim->anim, seq->anim_preseek);
+
+ if (seq_can_use_proxy(seq, psize)) {
+ /* Try to get a proxy image.
+ * Movie proxies are handled by ImBuf module with exception of `custom file` setting. */
+ if (context->scene->ed->proxy_storage != SEQ_EDIT_PROXY_DIR_STORAGE &&
+ seq->strip->proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_FILE) {
+ ibuf = seq_render_movie_strip_custom_file_proxy(context, seq, cfra);
+ }
+ else {
+ ibuf = IMB_anim_absolute(sanim->anim,
+ nr + seq->anim_startofs,
+ seq->strip->proxy ? seq->strip->proxy->tc : IMB_TC_RECORD_RUN,
+ psize);
+ }
+
+ if (ibuf != NULL) {
+ *r_is_proxy_image = true;
+ }
+ }
+
+ /* Fetching for requested proxy size failed, try fetching the original instead. */
+ if (ibuf == NULL) {
+ ibuf = IMB_anim_absolute(sanim->anim,
+ nr + seq->anim_startofs,
+ seq->strip->proxy ? seq->strip->proxy->tc : IMB_TC_RECORD_RUN,
+ IMB_PROXY_NONE);
+ }
+ if (ibuf == NULL) {
+ return NULL;
+ }
+
+ BKE_sequencer_imbuf_to_sequencer_space(context->scene, ibuf, false);
+
+ /* We don't need both (speed reasons)! */
+ if (ibuf->rect_float != NULL && ibuf->rect != NULL) {
+ imb_freerectImBuf(ibuf);
+ }
+
+ return ibuf;
+}
+
+static ImBuf *seq_render_movie_strip(
+ const SeqRenderData *context, Sequence *seq, float nr, float cfra, bool *r_is_proxy_image)
+{
+ /* Load all the videos. */
+ seq_open_anim_file(context->scene, seq, false);
+
+ ImBuf *ibuf = NULL;
+ StripAnim *sanim = seq->anims.first;
+ const int totfiles = seq_num_files(context->scene, seq->views_format, true);
+ bool is_multiview_render = (seq->flag & SEQ_USE_VIEWS) != 0 &&
+ (context->scene->r.scemode & R_MULTIVIEW) != 0 &&
+ BLI_listbase_count_at_most(&seq->anims, totfiles + 1) == totfiles;
+
+ if (is_multiview_render) {
+ ImBuf **ibuf_arr;
+ int totviews = BKE_scene_multiview_num_views_get(&context->scene->r);
+ ibuf_arr = MEM_callocN(sizeof(ImBuf *) * totviews, "Sequence Image Views Imbufs");
+ int ibuf_view_id;
+
+ for (ibuf_view_id = 0, sanim = seq->anims.first; sanim; sanim = sanim->next, ibuf_view_id++) {
+ if (sanim->anim) {
+ ibuf_arr[ibuf_view_id] = seq_render_movie_strip_view(
+ context, seq, nr, cfra, sanim, r_is_proxy_image);
+ }
+ }
+
+ if (seq->views_format == R_IMF_VIEWS_STEREO_3D) {
+ if (ibuf_arr[0] == NULL) {
+ /* Probably proxy hasn't been created yet. */
+ MEM_freeN(ibuf_arr);
+ return NULL;
+ }
+
+ IMB_ImBufFromStereo3d(seq->stereo3d_format, ibuf_arr[0], &ibuf_arr[0], &ibuf_arr[1]);
+ }
+
+ for (int view_id = 0; view_id < totviews; view_id++) {
+ SeqRenderData localcontext = *context;
+ localcontext.view_id = view_id;
+
+ if (view_id != context->view_id) {
+ ibuf_arr[view_id] = seq_render_preprocess_ibuf(
+ &localcontext, seq, ibuf_arr[view_id], cfra, clock(), true, false);
+ }
+ }
+
+ /* Return the original requested ImBuf. */
+ ibuf = ibuf_arr[context->view_id];
+
+ /* Remove the others (decrease their refcount). */
+ for (int view_id = 0; view_id < totviews; view_id++) {
+ if (ibuf_arr[view_id] != ibuf) {
+ IMB_freeImBuf(ibuf_arr[view_id]);
+ }
+ }
+
+ MEM_freeN(ibuf_arr);
+ }
+ else {
+ ibuf = seq_render_movie_strip_view(context, seq, nr, cfra, sanim, r_is_proxy_image);
+ }
+
+ if (ibuf == NULL) {
+ return NULL;
+ }
+
+ seq->strip->stripdata->orig_width = ibuf->x;
+ seq->strip->stripdata->orig_height = ibuf->y;
+
+ return ibuf;
+}
+
+static ImBuf *seq_get_movieclip_ibuf(Sequence *seq, MovieClipUser user)
+{
+ ImBuf *ibuf = NULL;
+ float tloc[2], tscale, tangle;
+ if (seq->clip_flag & SEQ_MOVIECLIP_RENDER_STABILIZED) {
+ ibuf = BKE_movieclip_get_stable_ibuf(seq->clip, &user, tloc, &tscale, &tangle, 0);
+ }
+ else {
+ ibuf = BKE_movieclip_get_ibuf_flag(seq->clip, &user, seq->clip->flag, MOVIECLIP_CACHE_SKIP);
+ }
+ return ibuf;
+}
+
+static ImBuf *seq_render_movieclip_strip(const SeqRenderData *context,
+ Sequence *seq,
+ float nr,
+ bool *r_is_proxy_image)
+{
+ ImBuf *ibuf = NULL;
+ MovieClipUser user;
+ IMB_Proxy_Size psize = seq_rendersize_to_proxysize(context->preview_render_size);
+
+ if (!seq->clip) {
+ return NULL;
+ }
+
+ memset(&user, 0, sizeof(MovieClipUser));
+
+ BKE_movieclip_user_set_frame(&user, nr + seq->anim_startofs + seq->clip->start_frame);
+
+ user.render_size = MCLIP_PROXY_RENDER_SIZE_FULL;
+ switch (psize) {
+ case IMB_PROXY_NONE:
+ user.render_size = MCLIP_PROXY_RENDER_SIZE_FULL;
+ break;
+ case IMB_PROXY_100:
+ user.render_size = MCLIP_PROXY_RENDER_SIZE_100;
+ break;
+ case IMB_PROXY_75:
+ user.render_size = MCLIP_PROXY_RENDER_SIZE_75;
+ break;
+ case IMB_PROXY_50:
+ user.render_size = MCLIP_PROXY_RENDER_SIZE_50;
+ break;
+ case IMB_PROXY_25:
+ user.render_size = MCLIP_PROXY_RENDER_SIZE_25;
+ break;
+ }
+
+ if (seq->clip_flag & SEQ_MOVIECLIP_RENDER_UNDISTORTED) {
+ user.render_flag |= MCLIP_PROXY_RENDER_UNDISTORT;
+ }
+
+ /* Try to get a proxy image. */
+ ibuf = seq_get_movieclip_ibuf(seq, user);
+
+ if (ibuf != NULL && psize != IMB_PROXY_NONE) {
+ *r_is_proxy_image = true;
+ }
+
+ /* If proxy is not found, grab full-size frame. */
+ if (ibuf == NULL) {
+ user.render_flag |= MCLIP_PROXY_RENDER_USE_FALLBACK_RENDER;
+ ibuf = seq_get_movieclip_ibuf(seq, user);
+ }
+
+ return ibuf;
+}
+
+static ImBuf *seq_render_mask(const SeqRenderData *context, Mask *mask, float nr, bool make_float)
+{
+ /* TODO - add option to rasterize to alpha imbuf? */
+ ImBuf *ibuf = NULL;
+ float *maskbuf;
+ int i;
+
+ if (!mask) {
+ return NULL;
+ }
+
+ AnimData *adt;
+ Mask *mask_temp;
+ MaskRasterHandle *mr_handle;
+
+ mask_temp = BKE_mask_copy_nolib(mask);
+
+ BKE_mask_evaluate(mask_temp, mask->sfra + nr, true);
+
+ /* anim-data */
+ adt = BKE_animdata_from_id(&mask->id);
+ const AnimationEvalContext anim_eval_context = BKE_animsys_eval_context_construct(
+ context->depsgraph, mask->sfra + nr);
+ BKE_animsys_evaluate_animdata(&mask_temp->id, adt, &anim_eval_context, ADT_RECALC_ANIM, false);
+
+ maskbuf = MEM_mallocN(sizeof(float) * context->rectx * context->recty, __func__);
+
+ mr_handle = BKE_maskrasterize_handle_new();
+
+ BKE_maskrasterize_handle_init(
+ mr_handle, mask_temp, context->rectx, context->recty, true, true, true);
+
+ BKE_mask_free(mask_temp);
+ MEM_freeN(mask_temp);
+
+ BKE_maskrasterize_buffer(mr_handle, context->rectx, context->recty, maskbuf);
+
+ BKE_maskrasterize_handle_free(mr_handle);
+
+ if (make_float) {
+ /* pixels */
+ const float *fp_src;
+ float *fp_dst;
+
+ ibuf = IMB_allocImBuf(context->rectx, context->recty, 32, IB_rectfloat);
+
+ fp_src = maskbuf;
+ fp_dst = ibuf->rect_float;
+ i = context->rectx * context->recty;
+ while (--i) {
+ fp_dst[0] = fp_dst[1] = fp_dst[2] = *fp_src;
+ fp_dst[3] = 1.0f;
+
+ fp_src += 1;
+ fp_dst += 4;
+ }
+ }
+ else {
+ /* pixels */
+ const float *fp_src;
+ unsigned char *ub_dst;
+
+ ibuf = IMB_allocImBuf(context->rectx, context->recty, 32, IB_rect);
+
+ fp_src = maskbuf;
+ ub_dst = (unsigned char *)ibuf->rect;
+ i = context->rectx * context->recty;
+ while (--i) {
+ ub_dst[0] = ub_dst[1] = ub_dst[2] = (unsigned char)(*fp_src * 255.0f); /* already clamped */
+ ub_dst[3] = 255;
+
+ fp_src += 1;
+ ub_dst += 4;
+ }
+ }
+
+ MEM_freeN(maskbuf);
+
+ return ibuf;
+}
+
+static ImBuf *seq_render_mask_strip(const SeqRenderData *context, Sequence *seq, float nr)
+{
+ bool make_float = (seq->flag & SEQ_MAKE_FLOAT) != 0;
+
+ return seq_render_mask(context, seq->mask, nr, make_float);
+}
+
+static ImBuf *seq_render_scene_strip(const SeqRenderData *context,
+ Sequence *seq,
+ float nr,
+ float cfra)
+{
+ ImBuf *ibuf = NULL;
+ double frame;
+ Object *camera;
+
+ struct {
+ int scemode;
+ int cfra;
+ float subframe;
+
+#ifdef DURIAN_CAMERA_SWITCH
+ int mode;
+#endif
+ } orig_data;
+
+ /* Old info:
+ * Hack! This function can be called from do_render_seq(), in that case
+ * the seq->scene can already have a Render initialized with same name,
+ * so we have to use a default name. (compositor uses scene name to
+ * find render).
+ * However, when called from within the UI (image preview in sequencer)
+ * we do want to use scene Render, that way the render result is defined
+ * for display in render/imagewindow
+ *
+ * Hmm, don't see, why we can't do that all the time,
+ * and since G.is_rendering is uhm, gone... (Peter)
+ */
+
+ /* New info:
+ * Using the same name for the renders works just fine as the do_render_seq()
+ * render is not used while the scene strips are rendered.
+ *
+ * However rendering from UI (through sequencer_preview_area_draw) can crash in
+ * very many cases since other renders (material preview, an actual render etc.)
+ * can be started while this sequence preview render is running. The only proper
+ * solution is to make the sequencer preview render a proper job, which can be
+ * stopped when needed. This would also give a nice progress bar for the preview
+ * space so that users know there's something happening.
+ *
+ * As a result the active scene now only uses OpenGL rendering for the sequencer
+ * preview. This is far from nice, but is the only way to prevent crashes at this
+ * time.
+ *
+ * -jahka
+ */
+
+ const bool is_rendering = G.is_rendering;
+ const bool is_background = G.background;
+ const bool do_seq_gl = is_rendering ? 0 : (context->scene->r.seq_prev_type) != OB_RENDER;
+ bool have_comp = false;
+ bool use_gpencil = true;
+ /* do we need to re-evaluate the frame after rendering? */
+ bool is_frame_update = false;
+ Scene *scene;
+ int is_thread_main = BLI_thread_is_main();
+
+ /* don't refer to seq->scene above this point!, it can be NULL */
+ if (seq->scene == NULL) {
+ return NULL;
+ }
+
+ /* Prevent rendering scene recursively. */
+ if (seq->scene == context->scene) {
+ return NULL;
+ }
+
+ scene = seq->scene;
+ frame = (double)scene->r.sfra + (double)nr + (double)seq->anim_startofs;
+
+#if 0 /* UNUSED */
+ have_seq = (scene->r.scemode & R_DOSEQ) && scene->ed && scene->ed->seqbase.first);
+#endif
+ have_comp = (scene->r.scemode & R_DOCOMP) && scene->use_nodes && scene->nodetree;
+
+ /* Get view layer for the strip. */
+ ViewLayer *view_layer = BKE_view_layer_default_render(scene);
+ /* Depsgraph will be NULL when doing rendering. */
+ Depsgraph *depsgraph = NULL;
+
+ orig_data.scemode = scene->r.scemode;
+ orig_data.cfra = scene->r.cfra;
+ orig_data.subframe = scene->r.subframe;
+#ifdef DURIAN_CAMERA_SWITCH
+ orig_data.mode = scene->r.mode;
+#endif
+
+ BKE_scene_frame_set(scene, frame);
+
+ if (seq->scene_camera) {
+ camera = seq->scene_camera;
+ }
+ else {
+ BKE_scene_camera_switch_update(scene);
+ camera = scene->camera;
+ }
+
+ if (have_comp == false && camera == NULL) {
+ goto finally;
+ }
+
+ if (seq->flag & SEQ_SCENE_NO_GPENCIL) {
+ use_gpencil = false;
+ }
+
+ /* prevent eternal loop */
+ scene->r.scemode &= ~R_DOSEQ;
+
+#ifdef DURIAN_CAMERA_SWITCH
+ /* stooping to new low's in hackyness :( */
+ scene->r.mode |= R_NO_CAMERA_SWITCH;
+#endif
+
+ is_frame_update = (orig_data.cfra != scene->r.cfra) || (orig_data.subframe != scene->r.subframe);
+
+ if ((sequencer_view3d_fn && do_seq_gl && camera) && is_thread_main) {
+ char err_out[256] = "unknown";
+ const int width = (scene->r.xsch * scene->r.size) / 100;
+ const int height = (scene->r.ysch * scene->r.size) / 100;
+ const char *viewname = BKE_scene_multiview_render_view_name_get(&scene->r, context->view_id);
+
+ unsigned int draw_flags = V3D_OFSDRAW_NONE;
+ draw_flags |= (use_gpencil) ? V3D_OFSDRAW_SHOW_ANNOTATION : 0;
+ draw_flags |= (context->scene->r.seq_flag & R_SEQ_OVERRIDE_SCENE_SETTINGS) ?
+ V3D_OFSDRAW_OVERRIDE_SCENE_SETTINGS :
+ 0;
+
+ /* for old scene this can be uninitialized,
+ * should probably be added to do_versions at some point if the functionality stays */
+ if (context->scene->r.seq_prev_type == 0) {
+ context->scene->r.seq_prev_type = 3 /* == OB_SOLID */;
+ }
+
+ /* opengl offscreen render */
+ depsgraph = BKE_scene_ensure_depsgraph(context->bmain, scene, view_layer);
+ BKE_scene_graph_update_for_newframe(depsgraph);
+ ibuf = sequencer_view3d_fn(
+ /* set for OpenGL render (NULL when scrubbing) */
+ depsgraph,
+ scene,
+ &context->scene->display.shading,
+ context->scene->r.seq_prev_type,
+ camera,
+ width,
+ height,
+ IB_rect,
+ draw_flags,
+ scene->r.alphamode,
+ viewname,
+ context->gpu_offscreen,
+ err_out);
+ if (ibuf == NULL) {
+ fprintf(stderr, "seq_render_scene_strip failed to get opengl buffer: %s\n", err_out);
+ }
+ }
+ else {
+ Render *re = RE_GetSceneRender(scene);
+ const int totviews = BKE_scene_multiview_num_views_get(&scene->r);
+ ImBuf **ibufs_arr;
+
+ ibufs_arr = MEM_callocN(sizeof(ImBuf *) * totviews, "Sequence Image Views Imbufs");
+
+ /* XXX: this if can be removed when sequence preview rendering uses the job system
+ *
+ * disable rendered preview for sequencer while rendering -- it's very much possible
+ * that preview render will went into conflict with final render
+ *
+ * When rendering from command line renderer is called from main thread, in this
+ * case it's always safe to render scene here
+ */
+ if (!is_thread_main || is_rendering == false || is_background || context->for_render) {
+ if (re == NULL) {
+ re = RE_NewSceneRender(scene);
+ }
+
+ RE_RenderFrame(
+ re, context->bmain, scene, have_comp ? NULL : view_layer, camera, frame, false);
+
+ /* restore previous state after it was toggled on & off by RE_RenderFrame */
+ G.is_rendering = is_rendering;
+ }
+
+ for (int view_id = 0; view_id < totviews; view_id++) {
+ SeqRenderData localcontext = *context;
+ RenderResult rres;
+
+ localcontext.view_id = view_id;
+
+ RE_AcquireResultImage(re, &rres, view_id);
+
+ if (rres.rectf) {
+ ibufs_arr[view_id] = IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rectfloat);
+ memcpy(ibufs_arr[view_id]->rect_float,
+ rres.rectf,
+ sizeof(float[4]) * rres.rectx * rres.recty);
+
+ if (rres.rectz) {
+ addzbuffloatImBuf(ibufs_arr[view_id]);
+ memcpy(
+ ibufs_arr[view_id]->zbuf_float, rres.rectz, sizeof(float) * rres.rectx * rres.recty);
+ }
+
+ /* float buffers in the sequencer are not linear */
+ BKE_sequencer_imbuf_to_sequencer_space(context->scene, ibufs_arr[view_id], false);
+ }
+ else if (rres.rect32) {
+ ibufs_arr[view_id] = IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rect);
+ memcpy(ibufs_arr[view_id]->rect, rres.rect32, 4 * rres.rectx * rres.recty);
+ }
+
+ if (view_id != context->view_id) {
+ BKE_sequencer_cache_put(
+ &localcontext, seq, cfra, SEQ_CACHE_STORE_RAW, ibufs_arr[view_id], 0, false);
+ }
+
+ RE_ReleaseResultImage(re);
+ }
+
+ /* return the original requested ImBuf */
+ ibuf = ibufs_arr[context->view_id];
+
+ /* "remove" the others (decrease their refcount) */
+ for (int view_id = 0; view_id < totviews; view_id++) {
+ if (ibufs_arr[view_id] != ibuf) {
+ IMB_freeImBuf(ibufs_arr[view_id]);
+ }
+ }
+ MEM_freeN(ibufs_arr);
+ }
+
+finally:
+ /* restore */
+ scene->r.scemode = orig_data.scemode;
+ scene->r.cfra = orig_data.cfra;
+ scene->r.subframe = orig_data.subframe;
+
+ if (is_frame_update && (depsgraph != NULL)) {
+ BKE_scene_graph_update_for_newframe(depsgraph);
+ }
+
+#ifdef DURIAN_CAMERA_SWITCH
+ /* stooping to new low's in hackyness :( */
+ scene->r.mode &= orig_data.mode | ~R_NO_CAMERA_SWITCH;
+#endif
+
+ return ibuf;
+}
+
+/**
+ * Used for meta-strips & scenes with #SEQ_SCENE_STRIPS flag set.
+ */
+static ImBuf *do_render_strip_seqbase(const SeqRenderData *context,
+ SeqRenderState *state,
+ Sequence *seq,
+ float nr)
+{
+ ImBuf *ibuf = NULL;
+ ListBase *seqbase = NULL;
+ int offset;
+
+ seqbase = BKE_sequence_seqbase_get(seq, &offset);
+
+ if (seqbase && !BLI_listbase_is_empty(seqbase)) {
+
+ if (seq->flag & SEQ_SCENE_STRIPS && seq->scene) {
+ BKE_animsys_evaluate_all_animation(context->bmain, context->depsgraph, nr + offset);
+ }
+
+ ibuf = seq_render_strip_stack(context,
+ state,
+ seqbase,
+ /* scene strips don't have their start taken into account */
+ nr + offset,
+ 0);
+ }
+
+ return ibuf;
+}
+
+static ImBuf *do_render_strip_uncached(const SeqRenderData *context,
+ SeqRenderState *state,
+ Sequence *seq,
+ float cfra,
+ bool *r_is_proxy_image)
+{
+ ImBuf *ibuf = NULL;
+ float nr = BKE_sequencer_give_stripelem_index(seq, cfra);
+ int type = (seq->type & SEQ_TYPE_EFFECT) ? SEQ_TYPE_EFFECT : seq->type;
+ switch (type) {
+ case SEQ_TYPE_META: {
+ ibuf = do_render_strip_seqbase(context, state, seq, nr);
+ break;
+ }
+
+ case SEQ_TYPE_SCENE: {
+ if (seq->flag & SEQ_SCENE_STRIPS) {
+ if (seq->scene && (context->scene != seq->scene)) {
+ /* recursive check */
+ if (BLI_linklist_index(state->scene_parents, seq->scene) != -1) {
+ break;
+ }
+ LinkNode scene_parent = {
+ .next = state->scene_parents,
+ .link = seq->scene,
+ };
+ state->scene_parents = &scene_parent;
+ /* end check */
+
+ /* Use the Scene Seq's scene for the context when rendering the scene's sequences
+ * (necessary for Multicam Selector among others).
+ */
+ SeqRenderData local_context = *context;
+ local_context.scene = seq->scene;
+ local_context.skip_cache = true;
+
+ ibuf = do_render_strip_seqbase(&local_context, state, seq, nr);
+
+ /* step back in the list */
+ state->scene_parents = state->scene_parents->next;
+ }
+ }
+ else {
+ /* scene can be NULL after deletions */
+ ibuf = seq_render_scene_strip(context, seq, nr, cfra);
+ }
+
+ break;
+ }
+
+ case SEQ_TYPE_EFFECT: {
+ ibuf = seq_render_effect_strip_impl(context, state, seq, cfra);
+ break;
+ }
+
+ case SEQ_TYPE_IMAGE: {
+ ibuf = seq_render_image_strip(context, seq, nr, cfra, r_is_proxy_image);
+ break;
+ }
+
+ case SEQ_TYPE_MOVIE: {
+ ibuf = seq_render_movie_strip(context, seq, nr, cfra, r_is_proxy_image);
+ break;
+ }
+
+ case SEQ_TYPE_MOVIECLIP: {
+ ibuf = seq_render_movieclip_strip(context, seq, nr, r_is_proxy_image);
+
+ if (ibuf) {
+ /* duplicate frame so movie cache wouldn't be confused by sequencer's stuff */
+ ImBuf *i = IMB_dupImBuf(ibuf);
+ IMB_freeImBuf(ibuf);
+ ibuf = i;
+
+ if (ibuf->rect_float) {
+ BKE_sequencer_imbuf_to_sequencer_space(context->scene, ibuf, false);
+ }
+ }
+
+ break;
+ }
+
+ case SEQ_TYPE_MASK: {
+ /* ibuf is always new */
+ ibuf = seq_render_mask_strip(context, seq, nr);
+ break;
+ }
+ }
+
+ if (ibuf) {
+ sequencer_imbuf_assign_spaces(context->scene, ibuf);
+ }
+
+ return ibuf;
+}
+
+/* Estimate time spent by the program rendering the strip */
+static clock_t seq_estimate_render_cost_begin(void)
+{
+ return clock();
+}
+
+static float seq_estimate_render_cost_end(Scene *scene, clock_t begin)
+{
+ clock_t end = clock();
+ float time_spent = (float)(end - begin);
+ float time_max = (1.0f / scene->r.frs_sec) * CLOCKS_PER_SEC;
+
+ if (time_max != 0) {
+ return time_spent / time_max;
+ }
+
+ return 1;
+}
+
+static ImBuf *seq_render_preprocess_ibuf(const SeqRenderData *context,
+ Sequence *seq,
+ ImBuf *ibuf,
+ float cfra,
+ clock_t begin,
+ bool use_preprocess,
+ const bool is_proxy_image)
+{
+ if (context->is_proxy_render == false &&
+ (ibuf->x != context->rectx || ibuf->y != context->recty)) {
+ use_preprocess = true;
+ }
+
+ if (use_preprocess) {
+ float cost = seq_estimate_render_cost_end(context->scene, begin);
+
+ /* TODO(Richard): It should be possible to store in cache if image is proxy,
+ * but it adds quite a bit of complexity. Since proxies are fast to read, I would
+ * rather simplify existing code a bit. */
+ if (!is_proxy_image) {
+ BKE_sequencer_cache_put(context, seq, cfra, SEQ_CACHE_STORE_RAW, ibuf, cost, false);
+ }
+
+ /* Reset timer so we can get partial render time. */
+ begin = seq_estimate_render_cost_begin();
+ ibuf = input_preprocess(context, seq, cfra, ibuf, is_proxy_image);
+ }
+
+ float cost = seq_estimate_render_cost_end(context->scene, begin);
+ BKE_sequencer_cache_put(context, seq, cfra, SEQ_CACHE_STORE_PREPROCESSED, ibuf, cost, false);
+ return ibuf;
+}
+
+static ImBuf *seq_render_strip(const SeqRenderData *context,
+ SeqRenderState *state,
+ Sequence *seq,
+ float cfra)
+{
+ ImBuf *ibuf = NULL;
+ bool use_preprocess = false;
+ bool is_proxy_image = false;
+
+ clock_t begin = seq_estimate_render_cost_begin();
+
+ ibuf = BKE_sequencer_cache_get(context, seq, cfra, SEQ_CACHE_STORE_PREPROCESSED, false);
+ if (ibuf != NULL) {
+ return ibuf;
+ }
+
+ ibuf = BKE_sequencer_cache_get(context, seq, cfra, SEQ_CACHE_STORE_RAW, false);
+ if (ibuf == NULL) {
+ ibuf = do_render_strip_uncached(context, state, seq, cfra, &is_proxy_image);
+ }
+
+ if (ibuf) {
+ use_preprocess = BKE_sequencer_input_have_to_preprocess(context, seq, cfra);
+ ibuf = seq_render_preprocess_ibuf(
+ context, seq, ibuf, cfra, begin, use_preprocess, is_proxy_image);
+ }
+
+ if (ibuf == NULL) {
+ ibuf = IMB_allocImBuf(context->rectx, context->recty, 32, IB_rect);
+ sequencer_imbuf_assign_spaces(context->scene, ibuf);
+ }
+
+ return ibuf;
+}
+
+/*********************** strip stack rendering functions *************************/
+
+static bool seq_must_swap_input_in_blend_mode(Sequence *seq)
+{
+ bool swap_input = false;
+
+ /* bad hack, to fix crazy input ordering of
+ * those two effects */
+
+ if (ELEM(seq->blend_mode, SEQ_TYPE_ALPHAOVER, SEQ_TYPE_ALPHAUNDER, SEQ_TYPE_OVERDROP)) {
+ swap_input = true;
+ }
+
+ return swap_input;
+}
+
+static int seq_get_early_out_for_blend_mode(Sequence *seq)
+{
+ struct SeqEffectHandle sh = BKE_sequence_get_blend(seq);
+ float facf = seq->blend_opacity / 100.0f;
+ int early_out = sh.early_out(seq, facf, facf);
+
+ if (ELEM(early_out, EARLY_DO_EFFECT, EARLY_NO_INPUT)) {
+ return early_out;
+ }
+
+ if (seq_must_swap_input_in_blend_mode(seq)) {
+ if (early_out == EARLY_USE_INPUT_2) {
+ return EARLY_USE_INPUT_1;
+ }
+ if (early_out == EARLY_USE_INPUT_1) {
+ return EARLY_USE_INPUT_2;
+ }
+ }
+ return early_out;
+}
+
+static ImBuf *seq_render_strip_stack_apply_effect(
+ const SeqRenderData *context, Sequence *seq, float cfra, ImBuf *ibuf1, ImBuf *ibuf2)
+{
+ ImBuf *out;
+ struct SeqEffectHandle sh = BKE_sequence_get_blend(seq);
+ float facf = seq->blend_opacity / 100.0f;
+ int swap_input = seq_must_swap_input_in_blend_mode(seq);
+
+ if (swap_input) {
+ if (sh.multithreaded) {
+ out = BKE_sequencer_effect_execute_threaded(
+ &sh, context, seq, cfra, facf, facf, ibuf2, ibuf1, NULL);
+ }
+ else {
+ out = sh.execute(context, seq, cfra, facf, facf, ibuf2, ibuf1, NULL);
+ }
+ }
+ else {
+ if (sh.multithreaded) {
+ out = BKE_sequencer_effect_execute_threaded(
+ &sh, context, seq, cfra, facf, facf, ibuf1, ibuf2, NULL);
+ }
+ else {
+ out = sh.execute(context, seq, cfra, facf, facf, ibuf1, ibuf2, NULL);
+ }
+ }
+
+ return out;
+}
+
+static ImBuf *seq_render_strip_stack(const SeqRenderData *context,
+ SeqRenderState *state,
+ ListBase *seqbasep,
+ float cfra,
+ int chanshown)
+{
+ Sequence *seq_arr[MAXSEQ + 1];
+ int count;
+ int i;
+ ImBuf *out = NULL;
+ clock_t begin;
+
+ count = BKE_sequencer_get_shown_sequences(seqbasep, cfra, chanshown, (Sequence **)&seq_arr);
+
+ if (count == 0) {
+ return NULL;
+ }
+
+ for (i = count - 1; i >= 0; i--) {
+ int early_out;
+ Sequence *seq = seq_arr[i];
+
+ out = BKE_sequencer_cache_get(context, seq, cfra, SEQ_CACHE_STORE_COMPOSITE, false);
+
+ if (out) {
+ break;
+ }
+ if (seq->blend_mode == SEQ_BLEND_REPLACE) {
+ out = seq_render_strip(context, state, seq, cfra);
+ break;
+ }
+
+ early_out = seq_get_early_out_for_blend_mode(seq);
+
+ switch (early_out) {
+ case EARLY_NO_INPUT:
+ case EARLY_USE_INPUT_2:
+ out = seq_render_strip(context, state, seq, cfra);
+ break;
+ case EARLY_USE_INPUT_1:
+ if (i == 0) {
+ out = IMB_allocImBuf(context->rectx, context->recty, 32, IB_rect);
+ }
+ break;
+ case EARLY_DO_EFFECT:
+ if (i == 0) {
+ begin = seq_estimate_render_cost_begin();
+
+ ImBuf *ibuf1 = IMB_allocImBuf(context->rectx, context->recty, 32, IB_rect);
+ ImBuf *ibuf2 = seq_render_strip(context, state, seq, cfra);
+
+ out = seq_render_strip_stack_apply_effect(context, seq, cfra, ibuf1, ibuf2);
+
+ float cost = seq_estimate_render_cost_end(context->scene, begin);
+ BKE_sequencer_cache_put(
+ context, seq_arr[i], cfra, SEQ_CACHE_STORE_COMPOSITE, out, cost, false);
+
+ IMB_freeImBuf(ibuf1);
+ IMB_freeImBuf(ibuf2);
+ }
+ break;
+ }
+ if (out) {
+ break;
+ }
+ }
+
+ i++;
+ for (; i < count; i++) {
+ begin = seq_estimate_render_cost_begin();
+ Sequence *seq = seq_arr[i];
+
+ if (seq_get_early_out_for_blend_mode(seq) == EARLY_DO_EFFECT) {
+ ImBuf *ibuf1 = out;
+ ImBuf *ibuf2 = seq_render_strip(context, state, seq, cfra);
+
+ out = seq_render_strip_stack_apply_effect(context, seq, cfra, ibuf1, ibuf2);
+
+ IMB_freeImBuf(ibuf1);
+ IMB_freeImBuf(ibuf2);
+ }
+
+ float cost = seq_estimate_render_cost_end(context->scene, begin);
+ BKE_sequencer_cache_put(
+ context, seq_arr[i], cfra, SEQ_CACHE_STORE_COMPOSITE, out, cost, false);
+ }
+
+ return out;
+}
+
+/*
+ * returned ImBuf is refed!
+ * you have to free after usage!
+ */
+
+ImBuf *BKE_sequencer_give_ibuf(const SeqRenderData *context, float cfra, int chanshown)
+{
+ Scene *scene = context->scene;
+ Editing *ed = BKE_sequencer_editing_get(scene, false);
+ ListBase *seqbasep;
+
+ if (ed == NULL) {
+ return NULL;
+ }
+
+ if ((chanshown < 0) && !BLI_listbase_is_empty(&ed->metastack)) {
+ int count = BLI_listbase_count(&ed->metastack);
+ count = max_ii(count + chanshown, 0);
+ seqbasep = ((MetaStack *)BLI_findlink(&ed->metastack, count))->oldbasep;
+ }
+ else {
+ seqbasep = ed->seqbasep;
+ }
+
+ SeqRenderState state;
+ sequencer_state_init(&state);
+ ImBuf *out = NULL;
+ Sequence *seq_arr[MAXSEQ + 1];
+ int count;
+
+ count = BKE_sequencer_get_shown_sequences(seqbasep, cfra, chanshown, seq_arr);
+
+ if (count) {
+ out = BKE_sequencer_cache_get(
+ context, seq_arr[count - 1], cfra, SEQ_CACHE_STORE_FINAL_OUT, false);
+ }
+
+ BKE_sequencer_cache_free_temp_cache(context->scene, context->task_id, cfra);
+
+ clock_t begin = seq_estimate_render_cost_begin();
+ float cost = 0;
+
+ if (count && !out) {
+ BLI_mutex_lock(&seq_render_mutex);
+ out = seq_render_strip_stack(context, &state, seqbasep, cfra, chanshown);
+ cost = seq_estimate_render_cost_end(context->scene, begin);
+
+ if (context->is_prefetch_render) {
+ BKE_sequencer_cache_put(
+ context, seq_arr[count - 1], cfra, SEQ_CACHE_STORE_FINAL_OUT, out, cost, false);
+ }
+ else {
+ BKE_sequencer_cache_put_if_possible(
+ context, seq_arr[count - 1], cfra, SEQ_CACHE_STORE_FINAL_OUT, out, cost, false);
+ }
+ BLI_mutex_unlock(&seq_render_mutex);
+ }
+
+ BKE_sequencer_prefetch_start(context, cfra, cost);
+
+ return out;
+}
+
+ImBuf *BKE_sequencer_give_ibuf_seqbase(const SeqRenderData *context,
+ float cfra,
+ int chan_shown,
+ ListBase *seqbasep)
+{
+ SeqRenderState state;
+ sequencer_state_init(&state);
+
+ return seq_render_strip_stack(context, &state, seqbasep, cfra, chan_shown);
+}
+
+ImBuf *BKE_sequencer_give_ibuf_direct(const SeqRenderData *context, float cfra, Sequence *seq)
+{
+ SeqRenderState state;
+ sequencer_state_init(&state);
+
+ ImBuf *ibuf = seq_render_strip(context, &state, seq, cfra);
+
+ return ibuf;
+}
+
+/* check whether sequence cur depends on seq */
+bool BKE_sequence_check_depend(Sequence *seq, Sequence *cur)
+{
+ if (cur->seq1 == seq || cur->seq2 == seq || cur->seq3 == seq) {
+ return true;
+ }
+
+ /* sequences are not intersecting in time, assume no dependency exists between them */
+ if (cur->enddisp < seq->startdisp || cur->startdisp > seq->enddisp) {
+ return false;
+ }
+
+ /* checking sequence is below reference one, not dependent on it */
+ if (cur->machine < seq->machine) {
+ return false;
+ }
+
+ /* sequence is not blending with lower machines, no dependency here occurs
+ * check for non-effects only since effect could use lower machines as input
+ */
+ if ((cur->type & SEQ_TYPE_EFFECT) == 0 &&
+ ((cur->blend_mode == SEQ_BLEND_REPLACE) ||
+ (cur->blend_mode == SEQ_TYPE_CROSS && cur->blend_opacity == 100.0f))) {
+ return false;
+ }
+
+ return true;
+}
+
+static void sequence_do_invalidate_dependent(Scene *scene, Sequence *seq, ListBase *seqbase)
+{
+ Sequence *cur;
+
+ for (cur = seqbase->first; cur; cur = cur->next) {
+ if (cur == seq) {
+ continue;
+ }
+
+ if (BKE_sequence_check_depend(seq, cur)) {
+ /* Effect must be invalidated completely if they depend on invalidated seq. */
+ if ((cur->type & SEQ_TYPE_EFFECT) != 0) {
+ BKE_sequencer_cache_cleanup_sequence(scene, cur, seq, SEQ_CACHE_ALL_TYPES, false);
+ }
+ else {
+ /* In case of alpha over for example only invalidate composite image */
+ BKE_sequencer_cache_cleanup_sequence(
+ scene, cur, seq, SEQ_CACHE_STORE_COMPOSITE | SEQ_CACHE_STORE_FINAL_OUT, false);
+ }
+ }
+
+ if (cur->seqbase.first) {
+ sequence_do_invalidate_dependent(scene, seq, &cur->seqbase);
+ }
+ }
+}
+
+static void sequence_invalidate_cache(Scene *scene,
+ Sequence *seq,
+ bool invalidate_self,
+ int invalidate_types)
+{
+ Editing *ed = scene->ed;
+
+ if (invalidate_self) {
+ BKE_sequence_free_anim(seq);
+ BKE_sequencer_cache_cleanup_sequence(scene, seq, seq, invalidate_types, false);
+ }
+
+ if (seq->effectdata && seq->type == SEQ_TYPE_SPEED) {
+ BKE_sequence_effect_speed_rebuild_map(scene, seq, true);
+ }
+
+ sequence_do_invalidate_dependent(scene, seq, &ed->seqbase);
+ DEG_id_tag_update(&scene->id, ID_RECALC_SEQUENCER_STRIPS);
+ BKE_sequencer_prefetch_stop(scene);
+}
+
+void BKE_sequence_invalidate_cache_in_range(Scene *scene,
+ Sequence *seq,
+ Sequence *range_mask,
+ int invalidate_types)
+{
+ BKE_sequencer_cache_cleanup_sequence(scene, seq, range_mask, invalidate_types, true);
+}
+
+void BKE_sequence_invalidate_cache_raw(Scene *scene, Sequence *seq)
+{
+ sequence_invalidate_cache(scene, seq, true, SEQ_CACHE_ALL_TYPES);
+}
+
+void BKE_sequence_invalidate_cache_preprocessed(Scene *scene, Sequence *seq)
+{
+ sequence_invalidate_cache(scene,
+ seq,
+ true,
+ SEQ_CACHE_STORE_PREPROCESSED | SEQ_CACHE_STORE_COMPOSITE |
+ SEQ_CACHE_STORE_FINAL_OUT);
+}
+
+void BKE_sequence_invalidate_cache_composite(Scene *scene, Sequence *seq)
+{
+ if (ELEM(seq->type, SEQ_TYPE_SOUND_RAM, SEQ_TYPE_SOUND_HD)) {
+ return;
+ }
+
+ sequence_invalidate_cache(
+ scene, seq, true, SEQ_CACHE_STORE_COMPOSITE | SEQ_CACHE_STORE_FINAL_OUT);
+}
+
+void BKE_sequence_invalidate_dependent(Scene *scene, Sequence *seq)
+{
+ if (ELEM(seq->type, SEQ_TYPE_SOUND_RAM, SEQ_TYPE_SOUND_HD)) {
+ return;
+ }
+
+ sequence_invalidate_cache(
+ scene, seq, false, SEQ_CACHE_STORE_COMPOSITE | SEQ_CACHE_STORE_FINAL_OUT);
+}
+
+static void invalidate_scene_strips(Scene *scene, Scene *scene_target, ListBase *seqbase)
+{
+ for (Sequence *seq = seqbase->first; seq != NULL; seq = seq->next) {
+ if (seq->scene == scene_target) {
+ BKE_sequence_invalidate_cache_raw(scene, seq);
+ }
+
+ if (seq->seqbase.first != NULL) {
+ invalidate_scene_strips(scene, scene_target, &seq->seqbase);
+ }
+ }
+}
+
+void BKE_sequence_invalidate_scene_strips(Main *bmain, Scene *scene_target)
+{
+ for (Scene *scene = bmain->scenes.first; scene != NULL; scene = scene->id.next) {
+ if (scene->ed != NULL) {
+ invalidate_scene_strips(scene, scene_target, &scene->ed->seqbase);
+ }
+ }
+}
+
+static void invalidate_movieclip_strips(Scene *scene, MovieClip *clip_target, ListBase *seqbase)
+{
+ for (Sequence *seq = seqbase->first; seq != NULL; seq = seq->next) {
+ if (seq->clip == clip_target) {
+ BKE_sequence_invalidate_cache_raw(scene, seq);
+ }
+
+ if (seq->seqbase.first != NULL) {
+ invalidate_movieclip_strips(scene, clip_target, &seq->seqbase);
+ }
+ }
+}
+
+void BKE_sequence_invalidate_movieclip_strips(Main *bmain, MovieClip *clip_target)
+{
+ for (Scene *scene = bmain->scenes.first; scene != NULL; scene = scene->id.next) {
+ if (scene->ed != NULL) {
+ invalidate_movieclip_strips(scene, clip_target, &scene->ed->seqbase);
+ }
+ }
+}
+
+void BKE_sequencer_free_imbuf(Scene *scene, ListBase *seqbase, bool for_render)
+{
+ if (scene->ed == NULL) {
+ return;
+ }
+
+ Sequence *seq;
+
+ BKE_sequencer_cache_cleanup(scene);
+ BKE_sequencer_prefetch_stop(scene);
+
+ for (seq = seqbase->first; seq; seq = seq->next) {
+ if (for_render && CFRA >= seq->startdisp && CFRA <= seq->enddisp) {
+ continue;
+ }
+
+ if (seq->strip) {
+ if (seq->type == SEQ_TYPE_MOVIE) {
+ BKE_sequence_free_anim(seq);
+ }
+ if (seq->type == SEQ_TYPE_SPEED) {
+ BKE_sequence_effect_speed_rebuild_map(scene, seq, true);
+ }
+ }
+ if (seq->type == SEQ_TYPE_META) {
+ BKE_sequencer_free_imbuf(scene, &seq->seqbase, for_render);
+ }
+ if (seq->type == SEQ_TYPE_SCENE) {
+ /* FIXME: recurs downwards,
+ * but do recurs protection somehow! */
+ }
+ }
+}
+
+static bool update_changed_seq_recurs(
+ Scene *scene, Sequence *seq, Sequence *changed_seq, int len_change, int ibuf_change)
+{
+ Sequence *subseq;
+ bool free_imbuf = false;
+
+ /* recurs downwards to see if this seq depends on the changed seq */
+
+ if (seq == NULL) {
+ return false;
+ }
+
+ if (seq == changed_seq) {
+ free_imbuf = true;
+ }
+
+ for (subseq = seq->seqbase.first; subseq; subseq = subseq->next) {
+ if (update_changed_seq_recurs(scene, subseq, changed_seq, len_change, ibuf_change)) {
+ free_imbuf = true;
+ }
+ }
+
+ if (seq->seq1) {
+ if (update_changed_seq_recurs(scene, seq->seq1, changed_seq, len_change, ibuf_change)) {
+ free_imbuf = true;
+ }
+ }
+ if (seq->seq2 && (seq->seq2 != seq->seq1)) {
+ if (update_changed_seq_recurs(scene, seq->seq2, changed_seq, len_change, ibuf_change)) {
+ free_imbuf = true;
+ }
+ }
+ if (seq->seq3 && (seq->seq3 != seq->seq1) && (seq->seq3 != seq->seq2)) {
+ if (update_changed_seq_recurs(scene, seq->seq3, changed_seq, len_change, ibuf_change)) {
+ free_imbuf = true;
+ }
+ }
+
+ if (free_imbuf) {
+ if (ibuf_change) {
+ if (seq->type == SEQ_TYPE_MOVIE) {
+ BKE_sequence_free_anim(seq);
+ }
+ else if (seq->type == SEQ_TYPE_SPEED) {
+ BKE_sequence_effect_speed_rebuild_map(scene, seq, true);
+ }
+ }
+
+ if (len_change) {
+ BKE_sequence_calc(scene, seq);
+ }
+ }
+
+ return free_imbuf;
+}
+
+void BKE_sequencer_update_changed_seq_and_deps(Scene *scene,
+ Sequence *changed_seq,
+ int len_change,
+ int ibuf_change)
+{
+ Editing *ed = BKE_sequencer_editing_get(scene, false);
+ Sequence *seq;
+
+ if (ed == NULL) {
+ return;
+ }
+
+ for (seq = ed->seqbase.first; seq; seq = seq->next) {
+ update_changed_seq_recurs(scene, seq, changed_seq, len_change, ibuf_change);
+ }
+}
+
+/* seq funcs's for transforming internally
+ * notice the difference between start/end and left/right.
+ *
+ * left and right are the bounds at which the sequence is rendered,
+ * start and end are from the start and fixed length of the sequence.
+ */
+static int seq_tx_get_start(Sequence *seq)
+{
+ return seq->start;
+}
+static int seq_tx_get_end(Sequence *seq)
+{
+ return seq->start + seq->len;
+}
+
+int BKE_sequence_tx_get_final_left(Sequence *seq, bool metaclip)
+{
+ if (metaclip && seq->tmp) {
+ /* return the range clipped by the parents range */
+ return max_ii(BKE_sequence_tx_get_final_left(seq, false),
+ BKE_sequence_tx_get_final_left((Sequence *)seq->tmp, true));
+ }
+
+ return (seq->start - seq->startstill) + seq->startofs;
+}
+int BKE_sequence_tx_get_final_right(Sequence *seq, bool metaclip)
+{
+ if (metaclip && seq->tmp) {
+ /* return the range clipped by the parents range */
+ return min_ii(BKE_sequence_tx_get_final_right(seq, false),
+ BKE_sequence_tx_get_final_right((Sequence *)seq->tmp, true));
+ }
+
+ return ((seq->start + seq->len) + seq->endstill) - seq->endofs;
+}
+
+void BKE_sequence_tx_set_final_left(Sequence *seq, int val)
+{
+ if (val < (seq)->start) {
+ seq->startstill = abs(val - (seq)->start);
+ seq->startofs = 0;
+ }
+ else {
+ seq->startofs = abs(val - (seq)->start);
+ seq->startstill = 0;
+ }
+}
+
+void BKE_sequence_tx_set_final_right(Sequence *seq, int val)
+{
+ if (val > (seq)->start + (seq)->len) {
+ seq->endstill = abs(val - (seq->start + (seq)->len));
+ seq->endofs = 0;
+ }
+ else {
+ seq->endofs = abs(val - ((seq)->start + (seq)->len));
+ seq->endstill = 0;
+ }
+}
+
+/* used so we can do a quick check for single image seq
+ * since they work a bit differently to normal image seq's (during transform) */
+bool BKE_sequence_single_check(Sequence *seq)
+{
+ return ((seq->len == 1) &&
+ (seq->type == SEQ_TYPE_IMAGE ||
+ ((seq->type & SEQ_TYPE_EFFECT) && BKE_sequence_effect_get_num_inputs(seq->type) == 0)));
+}
+
+/* check if the selected seq's reference unselected seq's */
+bool BKE_sequence_base_isolated_sel_check(ListBase *seqbase)
+{
+ Sequence *seq;
+ /* is there more than 1 select */
+ bool ok = false;
+
+ for (seq = seqbase->first; seq; seq = seq->next) {
+ if (seq->flag & SELECT) {
+ ok = true;
+ break;
+ }
+ }
+
+ if (ok == false) {
+ return false;
+ }
+
+ /* test relationships */
+ for (seq = seqbase->first; seq; seq = seq->next) {
+ if ((seq->type & SEQ_TYPE_EFFECT) == 0) {
+ continue;
+ }
+
+ if (seq->flag & SELECT) {
+ if ((seq->seq1 && (seq->seq1->flag & SELECT) == 0) ||
+ (seq->seq2 && (seq->seq2->flag & SELECT) == 0) ||
+ (seq->seq3 && (seq->seq3->flag & SELECT) == 0)) {
+ return false;
+ }
+ }
+ else {
+ if ((seq->seq1 && (seq->seq1->flag & SELECT)) || (seq->seq2 && (seq->seq2->flag & SELECT)) ||
+ (seq->seq3 && (seq->seq3->flag & SELECT))) {
+ return false;
+ }
+ }
+ }
+
+ return true;
+}
+
+/* use to impose limits when dragging/extending - so impossible situations don't happen
+ * Cant use the SEQ_LEFTSEL and SEQ_LEFTSEL directly because the strip may be in a metastrip */
+void BKE_sequence_tx_handle_xlimits(Sequence *seq, int leftflag, int rightflag)
+{
+ if (leftflag) {
+ if (BKE_sequence_tx_get_final_left(seq, false) >=
+ BKE_sequence_tx_get_final_right(seq, false)) {
+ BKE_sequence_tx_set_final_left(seq, BKE_sequence_tx_get_final_right(seq, false) - 1);
+ }
+
+ if (BKE_sequence_single_check(seq) == 0) {
+ if (BKE_sequence_tx_get_final_left(seq, false) >= seq_tx_get_end(seq)) {
+ BKE_sequence_tx_set_final_left(seq, seq_tx_get_end(seq) - 1);
+ }
+
+ /* doesn't work now - TODO */
+#if 0
+ if (seq_tx_get_start(seq) >= seq_tx_get_final_right(seq, 0)) {
+ int ofs;
+ ofs = seq_tx_get_start(seq) - seq_tx_get_final_right(seq, 0);
+ seq->start -= ofs;
+ seq_tx_set_final_left(seq, seq_tx_get_final_left(seq, 0) + ofs);
+ }
+#endif
+ }
+ }
+
+ if (rightflag) {
+ if (BKE_sequence_tx_get_final_right(seq, false) <=
+ BKE_sequence_tx_get_final_left(seq, false)) {
+ BKE_sequence_tx_set_final_right(seq, BKE_sequence_tx_get_final_left(seq, false) + 1);
+ }
+
+ if (BKE_sequence_single_check(seq) == 0) {
+ if (BKE_sequence_tx_get_final_right(seq, false) <= seq_tx_get_start(seq)) {
+ BKE_sequence_tx_set_final_right(seq, seq_tx_get_start(seq) + 1);
+ }
+ }
+ }
+
+ /* sounds cannot be extended past their endpoints */
+ if (seq->type == SEQ_TYPE_SOUND_RAM) {
+ seq->startstill = 0;
+ seq->endstill = 0;
+ }
+}
+
+void BKE_sequence_single_fix(Sequence *seq)
+{
+ int left, start, offset;
+ if (!BKE_sequence_single_check(seq)) {
+ return;
+ }
+
+ /* make sure the image is always at the start since there is only one,
+ * adjusting its start should be ok */
+ left = BKE_sequence_tx_get_final_left(seq, false);
+ start = seq->start;
+ if (start != left) {
+ offset = left - start;
+ BKE_sequence_tx_set_final_left(seq, BKE_sequence_tx_get_final_left(seq, false) - offset);
+ BKE_sequence_tx_set_final_right(seq, BKE_sequence_tx_get_final_right(seq, false) - offset);
+ seq->start += offset;
+ }
+}
+
+bool BKE_sequence_tx_test(Sequence *seq)
+{
+ return !(seq->type & SEQ_TYPE_EFFECT) || (BKE_sequence_effect_get_num_inputs(seq->type) == 0);
+}
+
+/**
+ * Return \a true if given \a seq needs a complete cleanup of its cache when it is transformed.
+ *
+ * Some (effect) strip types need a complete recache of themselves when they are transformed,
+ * because they do not 'contain' anything and do not have any explicit relations to other strips.
+ */
+bool BKE_sequence_tx_fullupdate_test(Sequence *seq)
+{
+ return BKE_sequence_tx_test(seq) && ELEM(seq->type, SEQ_TYPE_ADJUSTMENT, SEQ_TYPE_MULTICAM);
+}
+
+static bool seq_overlap(Sequence *seq1, Sequence *seq2)
+{
+ return (seq1 != seq2 && seq1->machine == seq2->machine &&
+ ((seq1->enddisp <= seq2->startdisp) || (seq1->startdisp >= seq2->enddisp)) == 0);
+}
+
+bool BKE_sequence_test_overlap(ListBase *seqbasep, Sequence *test)
+{
+ Sequence *seq;
+
+ seq = seqbasep->first;
+ while (seq) {
+ if (seq_overlap(test, seq)) {
+ return true;
+ }
+
+ seq = seq->next;
+ }
+ return false;
+}
+
+void BKE_sequence_translate(Scene *evil_scene, Sequence *seq, int delta)
+{
+ if (delta == 0) {
+ return;
+ }
+
+ BKE_sequencer_offset_animdata(evil_scene, seq, delta);
+ seq->start += delta;
+
+ if (seq->type == SEQ_TYPE_META) {
+ Sequence *seq_child;
+ for (seq_child = seq->seqbase.first; seq_child; seq_child = seq_child->next) {
+ BKE_sequence_translate(evil_scene, seq_child, delta);
+ }
+ }
+
+ BKE_sequence_calc_disp(evil_scene, seq);
+}
+
+void BKE_sequence_sound_init(Scene *scene, Sequence *seq)
+{
+ if (seq->type == SEQ_TYPE_META) {
+ Sequence *seq_child;
+ for (seq_child = seq->seqbase.first; seq_child; seq_child = seq_child->next) {
+ BKE_sequence_sound_init(scene, seq_child);
+ }
+ }
+ else {
+ if (seq->sound) {
+ seq->scene_sound = BKE_sound_add_scene_sound_defaults(scene, seq);
+ }
+ if (seq->scene) {
+ seq->scene_sound = BKE_sound_scene_add_scene_sound_defaults(scene, seq);
+ }
+ }
+}
+
+const Sequence *BKE_sequencer_foreground_frame_get(const Scene *scene, int frame)
+{
+ const Editing *ed = scene->ed;
+ const Sequence *seq, *best_seq = NULL;
+ int best_machine = -1;
+
+ if (!ed) {
+ return NULL;
+ }
+
+ for (seq = ed->seqbasep->first; seq; seq = seq->next) {
+ if (seq->flag & SEQ_MUTE || seq->startdisp > frame || seq->enddisp <= frame) {
+ continue;
+ }
+ /* Only use strips that generate an image, not ones that combine
+ * other strips or apply some effect. */
+ if (ELEM(seq->type,
+ SEQ_TYPE_IMAGE,
+ SEQ_TYPE_META,
+ SEQ_TYPE_SCENE,
+ SEQ_TYPE_MOVIE,
+ SEQ_TYPE_COLOR,
+ SEQ_TYPE_TEXT)) {
+ if (seq->machine > best_machine) {
+ best_seq = seq;
+ best_machine = seq->machine;
+ }
+ }
+ }
+ return best_seq;
+}
+
+/* return 0 if there weren't enough space */
+bool BKE_sequence_base_shuffle_ex(ListBase *seqbasep,
+ Sequence *test,
+ Scene *evil_scene,
+ int channel_delta)
+{
+ const int orig_machine = test->machine;
+ BLI_assert(ELEM(channel_delta, -1, 1));
+
+ test->machine += channel_delta;
+ BKE_sequence_calc(evil_scene, test);
+ while (BKE_sequence_test_overlap(seqbasep, test)) {
+ if ((channel_delta > 0) ? (test->machine >= MAXSEQ) : (test->machine < 1)) {
+ break;
+ }
+
+ test->machine += channel_delta;
+ BKE_sequence_calc(
+ evil_scene,
+ test); // XXX - I don't think this is needed since were only moving vertically, Campbell.
+ }
+
+ if ((test->machine < 1) || (test->machine > MAXSEQ)) {
+ /* Blender 2.4x would remove the strip.
+ * nicer to move it to the end */
+
+ Sequence *seq;
+ int new_frame = test->enddisp;
+
+ for (seq = seqbasep->first; seq; seq = seq->next) {
+ if (seq->machine == orig_machine) {
+ new_frame = max_ii(new_frame, seq->enddisp);
+ }
+ }
+
+ test->machine = orig_machine;
+ new_frame = new_frame + (test->start - test->startdisp); /* adjust by the startdisp */
+ BKE_sequence_translate(evil_scene, test, new_frame - test->start);
+
+ BKE_sequence_calc(evil_scene, test);
+ return false;
+ }
+
+ return true;
+}
+
+bool BKE_sequence_base_shuffle(ListBase *seqbasep, Sequence *test, Scene *evil_scene)
+{
+ return BKE_sequence_base_shuffle_ex(seqbasep, test, evil_scene, 1);
+}
+
+static int shuffle_seq_time_offset_test(ListBase *seqbasep, char dir)
+{
+ int offset = 0;
+ Sequence *seq, *seq_other;
+
+ for (seq = seqbasep->first; seq; seq = seq->next) {
+ if (seq->tmp) {
+ for (seq_other = seqbasep->first; seq_other; seq_other = seq_other->next) {
+ if (!seq_other->tmp && seq_overlap(seq, seq_other)) {
+ if (dir == 'L') {
+ offset = min_ii(offset, seq_other->startdisp - seq->enddisp);
+ }
+ else {
+ offset = max_ii(offset, seq_other->enddisp - seq->startdisp);
+ }
+ }
+ }
+ }
+ }
+ return offset;
+}
+
+static int shuffle_seq_time_offset(Scene *scene, ListBase *seqbasep, char dir)
+{
+ int ofs = 0;
+ int tot_ofs = 0;
+ Sequence *seq;
+ while ((ofs = shuffle_seq_time_offset_test(seqbasep, dir))) {
+ for (seq = seqbasep->first; seq; seq = seq->next) {
+ if (seq->tmp) {
+ /* seq_test_overlap only tests display values */
+ seq->startdisp += ofs;
+ seq->enddisp += ofs;
+ }
+ }
+
+ tot_ofs += ofs;
+ }
+
+ for (seq = seqbasep->first; seq; seq = seq->next) {
+ if (seq->tmp) {
+ BKE_sequence_calc_disp(scene, seq); /* corrects dummy startdisp/enddisp values */
+ }
+ }
+
+ return tot_ofs;
+}
+
+bool BKE_sequence_base_shuffle_time(ListBase *seqbasep,
+ Scene *evil_scene,
+ ListBase *markers,
+ const bool use_sync_markers)
+{
+ /* note: seq->tmp is used to tag strips to move */
+
+ Sequence *seq;
+
+ int offset_l = shuffle_seq_time_offset(evil_scene, seqbasep, 'L');
+ int offset_r = shuffle_seq_time_offset(evil_scene, seqbasep, 'R');
+ int offset = (-offset_l < offset_r) ? offset_l : offset_r;
+
+ if (offset) {
+ for (seq = seqbasep->first; seq; seq = seq->next) {
+ if (seq->tmp) {
+ BKE_sequence_translate(evil_scene, seq, offset);
+ seq->flag &= ~SEQ_OVERLAP;
+ }
+ }
+
+ if (use_sync_markers && !(evil_scene->toolsettings->lock_markers) && (markers != NULL)) {
+ TimeMarker *marker;
+ /* affect selected markers - it's unlikely that we will want to affect all in this way? */
+ for (marker = markers->first; marker; marker = marker->next) {
+ if (marker->flag & SELECT) {
+ marker->frame += offset;
+ }
+ }
+ }
+ }
+
+ return offset ? false : true;
+}
+
+/* Unlike _update_sound_ funcs, these ones take info from audaspace to update sequence length! */
+#ifdef WITH_AUDASPACE
+static bool sequencer_refresh_sound_length_recursive(Main *bmain, Scene *scene, ListBase *seqbase)
+{
+ Sequence *seq;
+ bool changed = false;
+
+ for (seq = seqbase->first; seq; seq = seq->next) {
+ if (seq->type == SEQ_TYPE_META) {
+ if (sequencer_refresh_sound_length_recursive(bmain, scene, &seq->seqbase)) {
+ BKE_sequence_calc(scene, seq);
+ changed = true;
+ }
+ }
+ else if (seq->type == SEQ_TYPE_SOUND_RAM && seq->sound) {
+ const float length = BKE_sound_get_length(bmain, seq->sound);
+ int old = seq->len;
+ float fac;
+
+ seq->len = (int)ceil((double)length * FPS);
+ fac = (float)seq->len / (float)old;
+ old = seq->startofs;
+ seq->startofs *= fac;
+ seq->endofs *= fac;
+ seq->start += (old - seq->startofs); /* So that visual/"real" start frame does not change! */
+
+ BKE_sequence_calc(scene, seq);
+ changed = true;
+ }
+ }
+ return changed;
+}
+#endif
+
+void BKE_sequencer_refresh_sound_length(Main *bmain, Scene *scene)
+{
+#ifdef WITH_AUDASPACE
+ if (scene->ed) {
+ sequencer_refresh_sound_length_recursive(bmain, scene, &scene->ed->seqbase);
+ }
+#else
+ UNUSED_VARS(bmain, scene);
+#endif
+}
+
+void BKE_sequencer_update_sound_bounds_all(Scene *scene)
+{
+ Editing *ed = scene->ed;
+
+ if (ed) {
+ Sequence *seq;
+
+ for (seq = ed->seqbase.first; seq; seq = seq->next) {
+ if (seq->type == SEQ_TYPE_META) {
+ seq_update_sound_bounds_recursive(scene, seq);
+ }
+ else if (ELEM(seq->type, SEQ_TYPE_SOUND_RAM, SEQ_TYPE_SCENE)) {
+ BKE_sequencer_update_sound_bounds(scene, seq);
+ }
+ }
+ }
+}
+
+void BKE_sequencer_update_sound_bounds(Scene *scene, Sequence *seq)
+{
+ if (seq->type == SEQ_TYPE_SCENE) {
+ if (seq->scene && seq->scene_sound) {
+ /* We have to take into account start frame of the sequence's scene! */
+ int startofs = seq->startofs + seq->anim_startofs + seq->scene->r.sfra;
+
+ BKE_sound_move_scene_sound(scene, seq->scene_sound, seq->startdisp, seq->enddisp, startofs);
+ }
+ }
+ else {
+ BKE_sound_move_scene_sound_defaults(scene, seq);
+ }
+ /* mute is set in seq_update_muting_recursive */
+}
+
+static void seq_update_muting_recursive(ListBase *seqbasep, Sequence *metaseq, int mute)
+{
+ Sequence *seq;
+ int seqmute;
+
+ /* for sound we go over full meta tree to update muted state,
+ * since sound is played outside of evaluating the imbufs, */
+ for (seq = seqbasep->first; seq; seq = seq->next) {
+ seqmute = (mute || (seq->flag & SEQ_MUTE));
+
+ if (seq->type == SEQ_TYPE_META) {
+ /* if this is the current meta sequence, unmute because
+ * all sequences above this were set to mute */
+ if (seq == metaseq) {
+ seqmute = 0;
+ }
+
+ seq_update_muting_recursive(&seq->seqbase, metaseq, seqmute);
+ }
+ else if (ELEM(seq->type, SEQ_TYPE_SOUND_RAM, SEQ_TYPE_SCENE)) {
+ if (seq->scene_sound) {
+ BKE_sound_mute_scene_sound(seq->scene_sound, seqmute);
+ }
+ }
+ }
+}
+
+void BKE_sequencer_update_muting(Editing *ed)
+{
+ if (ed) {
+ /* mute all sounds up to current metastack list */
+ MetaStack *ms = ed->metastack.last;
+
+ if (ms) {
+ seq_update_muting_recursive(&ed->seqbase, ms->parseq, 1);
+ }
+ else {
+ seq_update_muting_recursive(&ed->seqbase, NULL, 0);
+ }
+ }
+}
+
+static void seq_update_sound_recursive(Scene *scene, ListBase *seqbasep, bSound *sound)
+{
+ Sequence *seq;
+
+ for (seq = seqbasep->first; seq; seq = seq->next) {
+ if (seq->type == SEQ_TYPE_META) {
+ seq_update_sound_recursive(scene, &seq->seqbase, sound);
+ }
+ else if (seq->type == SEQ_TYPE_SOUND_RAM) {
+ if (seq->scene_sound && sound == seq->sound) {
+ BKE_sound_update_scene_sound(seq->scene_sound, sound);
+ }
+ }
+ }
+}
+
+void BKE_sequencer_update_sound(Scene *scene, bSound *sound)
+{
+ if (scene->ed) {
+ seq_update_sound_recursive(scene, &scene->ed->seqbase, sound);
+ }
+}
+
+/* in cases where we done know the sequence's listbase */
+ListBase *BKE_sequence_seqbase(ListBase *seqbase, Sequence *seq)
+{
+ Sequence *iseq;
+ ListBase *lb = NULL;
+
+ for (iseq = seqbase->first; iseq; iseq = iseq->next) {
+ if (seq == iseq) {
+ return seqbase;
+ }
+ if (iseq->seqbase.first && (lb = BKE_sequence_seqbase(&iseq->seqbase, seq))) {
+ return lb;
+ }
+ }
+
+ return NULL;
+}
+
+Sequence *BKE_sequence_metastrip(ListBase *seqbase, Sequence *meta, Sequence *seq)
+{
+ Sequence *iseq;
+
+ for (iseq = seqbase->first; iseq; iseq = iseq->next) {
+ Sequence *rval;
+
+ if (seq == iseq) {
+ return meta;
+ }
+ if (iseq->seqbase.first && (rval = BKE_sequence_metastrip(&iseq->seqbase, iseq, seq))) {
+ return rval;
+ }
+ }
+
+ return NULL;
+}
+
+int BKE_sequence_swap(Sequence *seq_a, Sequence *seq_b, const char **error_str)
+{
+ char name[sizeof(seq_a->name)];
+
+ if (seq_a->len != seq_b->len) {
+ *error_str = N_("Strips must be the same length");
+ return 0;
+ }
+
+ /* type checking, could be more advanced but disallow sound vs non-sound copy */
+ if (seq_a->type != seq_b->type) {
+ if (seq_a->type == SEQ_TYPE_SOUND_RAM || seq_b->type == SEQ_TYPE_SOUND_RAM) {
+ *error_str = N_("Strips were not compatible");
+ return 0;
+ }
+
+ /* disallow effects to swap with non-effects strips */
+ if ((seq_a->type & SEQ_TYPE_EFFECT) != (seq_b->type & SEQ_TYPE_EFFECT)) {
+ *error_str = N_("Strips were not compatible");
+ return 0;
+ }
+
+ if ((seq_a->type & SEQ_TYPE_EFFECT) && (seq_b->type & SEQ_TYPE_EFFECT)) {
+ if (BKE_sequence_effect_get_num_inputs(seq_a->type) !=
+ BKE_sequence_effect_get_num_inputs(seq_b->type)) {
+ *error_str = N_("Strips must have the same number of inputs");
+ return 0;
+ }
+ }
+ }
+
+ SWAP(Sequence, *seq_a, *seq_b);
+
+ /* swap back names so animation fcurves don't get swapped */
+ BLI_strncpy(name, seq_a->name + 2, sizeof(name));
+ BLI_strncpy(seq_a->name + 2, seq_b->name + 2, sizeof(seq_b->name) - 2);
+ BLI_strncpy(seq_b->name + 2, name, sizeof(seq_b->name) - 2);
+
+ /* swap back opacity, and overlay mode */
+ SWAP(int, seq_a->blend_mode, seq_b->blend_mode);
+ SWAP(float, seq_a->blend_opacity, seq_b->blend_opacity);
+
+ SWAP(Sequence *, seq_a->prev, seq_b->prev);
+ SWAP(Sequence *, seq_a->next, seq_b->next);
+ SWAP(int, seq_a->start, seq_b->start);
+ SWAP(int, seq_a->startofs, seq_b->startofs);
+ SWAP(int, seq_a->endofs, seq_b->endofs);
+ SWAP(int, seq_a->startstill, seq_b->startstill);
+ SWAP(int, seq_a->endstill, seq_b->endstill);
+ SWAP(int, seq_a->machine, seq_b->machine);
+ SWAP(int, seq_a->startdisp, seq_b->startdisp);
+ SWAP(int, seq_a->enddisp, seq_b->enddisp);
+
+ return 1;
+}
+
+/* r_prefix + [" + escaped_name + "] + \0 */
+#define SEQ_RNAPATH_MAXSTR ((30 + 2 + (SEQ_NAME_MAXSTR * 2) + 2) + 1)
+
+static size_t sequencer_rna_path_prefix(char str[SEQ_RNAPATH_MAXSTR], const char *name)
+{
+ char name_esc[SEQ_NAME_MAXSTR * 2];
+
+ BLI_strescape(name_esc, name, sizeof(name_esc));
+ return BLI_snprintf_rlen(
+ str, SEQ_RNAPATH_MAXSTR, "sequence_editor.sequences_all[\"%s\"]", name_esc);
+}
+
+/* XXX - hackish function needed for transforming strips! TODO - have some better solution */
+void BKE_sequencer_offset_animdata(Scene *scene, Sequence *seq, int ofs)
+{
+ char str[SEQ_RNAPATH_MAXSTR];
+ size_t str_len;
+ FCurve *fcu;
+
+ if (scene->adt == NULL || ofs == 0 || scene->adt->action == NULL) {
+ return;
+ }
+
+ str_len = sequencer_rna_path_prefix(str, seq->name + 2);
+
+ for (fcu = scene->adt->action->curves.first; fcu; fcu = fcu->next) {
+ if (STREQLEN(fcu->rna_path, str, str_len)) {
+ unsigned int i;
+ if (fcu->bezt) {
+ for (i = 0; i < fcu->totvert; i++) {
+ BezTriple *bezt = &fcu->bezt[i];
+ bezt->vec[0][0] += ofs;
+ bezt->vec[1][0] += ofs;
+ bezt->vec[2][0] += ofs;
+ }
+ }
+ if (fcu->fpt) {
+ for (i = 0; i < fcu->totvert; i++) {
+ FPoint *fpt = &fcu->fpt[i];
+ fpt->vec[0] += ofs;
+ }
+ }
+ }
+ }
+
+ DEG_id_tag_update(&scene->adt->action->id, ID_RECALC_ANIMATION);
+}
+
+void BKE_sequencer_dupe_animdata(Scene *scene, const char *name_src, const char *name_dst)
+{
+ char str_from[SEQ_RNAPATH_MAXSTR];
+ size_t str_from_len;
+ FCurve *fcu;
+ FCurve *fcu_last;
+ FCurve *fcu_cpy;
+ ListBase lb = {NULL, NULL};
+
+ if (scene->adt == NULL || scene->adt->action == NULL) {
+ return;
+ }
+
+ str_from_len = sequencer_rna_path_prefix(str_from, name_src);
+
+ fcu_last = scene->adt->action->curves.last;
+
+ for (fcu = scene->adt->action->curves.first; fcu && fcu->prev != fcu_last; fcu = fcu->next) {
+ if (STREQLEN(fcu->rna_path, str_from, str_from_len)) {
+ fcu_cpy = BKE_fcurve_copy(fcu);
+ BLI_addtail(&lb, fcu_cpy);
+ }
+ }
+
+ /* notice validate is 0, keep this because the seq may not be added to the scene yet */
+ BKE_animdata_fix_paths_rename(
+ &scene->id, scene->adt, NULL, "sequence_editor.sequences_all", name_src, name_dst, 0, 0, 0);
+
+ /* add the original fcurves back */
+ BLI_movelisttolist(&scene->adt->action->curves, &lb);
+}
+
+/* XXX - hackish function needed to remove all fcurves belonging to a sequencer strip */
+static void seq_free_animdata(Scene *scene, Sequence *seq)
+{
+ char str[SEQ_RNAPATH_MAXSTR];
+ size_t str_len;
+ FCurve *fcu;
+
+ if (scene->adt == NULL || scene->adt->action == NULL) {
+ return;
+ }
+
+ str_len = sequencer_rna_path_prefix(str, seq->name + 2);
+
+ fcu = scene->adt->action->curves.first;
+
+ while (fcu) {
+ if (STREQLEN(fcu->rna_path, str, str_len)) {
+ FCurve *next_fcu = fcu->next;
+
+ BLI_remlink(&scene->adt->action->curves, fcu);
+ BKE_fcurve_free(fcu);
+
+ fcu = next_fcu;
+ }
+ else {
+ fcu = fcu->next;
+ }
+ }
+}
+
+#undef SEQ_RNAPATH_MAXSTR
+
+Sequence *BKE_sequence_get_by_name(ListBase *seqbase, const char *name, bool recursive)
+{
+ Sequence *iseq = NULL;
+ Sequence *rseq = NULL;
+
+ for (iseq = seqbase->first; iseq; iseq = iseq->next) {
+ if (STREQ(name, iseq->name + 2)) {
+ return iseq;
+ }
+ if (recursive && (iseq->seqbase.first) &&
+ (rseq = BKE_sequence_get_by_name(&iseq->seqbase, name, 1))) {
+ return rseq;
+ }
+ }
+
+ return NULL;
+}
+
+/**
+ * Only use as last resort when the StripElem is available but no the Sequence.
+ * (needed for RNA)
+ */
+Sequence *BKE_sequencer_from_elem(ListBase *seqbase, StripElem *se)
+{
+ Sequence *iseq;
+
+ for (iseq = seqbase->first; iseq; iseq = iseq->next) {
+ Sequence *seq_found;
+ if ((iseq->strip && iseq->strip->stripdata) &&
+ (ARRAY_HAS_ITEM(se, iseq->strip->stripdata, iseq->len))) {
+ break;
+ }
+ if ((seq_found = BKE_sequencer_from_elem(&iseq->seqbase, se))) {
+ iseq = seq_found;
+ break;
+ }
+ }
+
+ return iseq;
+}
+
+Sequence *BKE_sequencer_active_get(Scene *scene)
+{
+ Editing *ed = BKE_sequencer_editing_get(scene, false);
+
+ if (ed == NULL) {
+ return NULL;
+ }
+
+ return ed->act_seq;
+}
+
+void BKE_sequencer_active_set(Scene *scene, Sequence *seq)
+{
+ Editing *ed = BKE_sequencer_editing_get(scene, false);
+
+ if (ed == NULL) {
+ return;
+ }
+
+ ed->act_seq = seq;
+}
+
+int BKE_sequencer_active_get_pair(Scene *scene, Sequence **seq_act, Sequence **seq_other)
+{
+ Editing *ed = BKE_sequencer_editing_get(scene, false);
+
+ *seq_act = BKE_sequencer_active_get(scene);
+
+ if (*seq_act == NULL) {
+ return 0;
+ }
+
+ Sequence *seq;
+
+ *seq_other = NULL;
+
+ for (seq = ed->seqbasep->first; seq; seq = seq->next) {
+ if (seq->flag & SELECT && (seq != (*seq_act))) {
+ if (*seq_other) {
+ return 0;
+ }
+
+ *seq_other = seq;
+ }
+ }
+
+ return (*seq_other != NULL);
+}
+
+Mask *BKE_sequencer_mask_get(Scene *scene)
+{
+ Sequence *seq_act = BKE_sequencer_active_get(scene);
+
+ if (seq_act && seq_act->type == SEQ_TYPE_MASK) {
+ return seq_act->mask;
+ }
+
+ return NULL;
+}
+
+/* api like funcs for adding */
+
+static void seq_load_apply(Main *bmain, Scene *scene, Sequence *seq, SeqLoadInfo *seq_load)
+{
+ if (seq) {
+ BLI_strncpy_utf8(seq->name + 2, seq_load->name, sizeof(seq->name) - 2);
+ BLI_utf8_invalid_strip(seq->name + 2, strlen(seq->name + 2));
+ BKE_sequence_base_unique_name_recursive(&scene->ed->seqbase, seq);
+
+ if (seq_load->flag & SEQ_LOAD_FRAME_ADVANCE) {
+ seq_load->start_frame += (seq->enddisp - seq->startdisp);
+ }
+
+ if (seq_load->flag & SEQ_LOAD_REPLACE_SEL) {
+ seq_load->flag |= SELECT;
+ BKE_sequencer_active_set(scene, seq);
+ }
+
+ if (seq_load->flag & SEQ_LOAD_SOUND_MONO) {
+ seq->sound->flags |= SOUND_FLAGS_MONO;
+ BKE_sound_load(bmain, seq->sound);
+ }
+
+ if (seq_load->flag & SEQ_LOAD_SOUND_CACHE) {
+ if (seq->sound) {
+ seq->sound->flags |= SOUND_FLAGS_CACHING;
+ }
+ }
+
+ seq_load->tot_success++;
+ }
+ else {
+ seq_load->tot_error++;
+ }
+}
+
+static Strip *seq_strip_alloc(int type)
+{
+ Strip *strip = MEM_callocN(sizeof(Strip), "strip");
+
+ if (ELEM(type, SEQ_TYPE_SOUND_RAM, SEQ_TYPE_SOUND_HD) == 0) {
+ strip->transform = MEM_callocN(sizeof(struct StripTransform), "StripTransform");
+ strip->crop = MEM_callocN(sizeof(struct StripCrop), "StripCrop");
+ }
+
+ strip->us = 1;
+ return strip;
+}
+
+Sequence *BKE_sequence_alloc(ListBase *lb, int cfra, int machine, int type)
+{
+ Sequence *seq;
+
+ seq = MEM_callocN(sizeof(Sequence), "addseq");
+ BLI_addtail(lb, seq);
+
+ *((short *)seq->name) = ID_SEQ;
+ seq->name[2] = 0;
+
+ seq->flag = SELECT;
+ seq->start = cfra;
+ seq->machine = machine;
+ seq->sat = 1.0;
+ seq->mul = 1.0;
+ seq->blend_opacity = 100.0;
+ seq->volume = 1.0f;
+ seq->pitch = 1.0f;
+ seq->scene_sound = NULL;
+ seq->type = type;
+
+ seq->strip = seq_strip_alloc(type);
+ seq->stereo3d_format = MEM_callocN(sizeof(Stereo3dFormat), "Sequence Stereo Format");
+ seq->cache_flag = SEQ_CACHE_STORE_RAW | SEQ_CACHE_STORE_PREPROCESSED | SEQ_CACHE_STORE_COMPOSITE;
+
+ BKE_sequence_session_uuid_generate(seq);
+
+ return seq;
+}
+
+void BKE_sequence_session_uuid_generate(struct Sequence *sequence)
+{
+ sequence->runtime.session_uuid = BLI_session_uuid_generate();
+}
+
+void BKE_sequence_alpha_mode_from_extension(Sequence *seq)
+{
+ if (seq->strip && seq->strip->stripdata) {
+ const char *filename = seq->strip->stripdata->name;
+ seq->alpha_mode = BKE_image_alpha_mode_from_extension_ex(filename);
+ }
+}
+
+void BKE_sequence_init_colorspace(Sequence *seq)
+{
+ if (seq->strip && seq->strip->stripdata) {
+ char name[FILE_MAX];
+ ImBuf *ibuf;
+
+ BLI_join_dirfile(name, sizeof(name), seq->strip->dir, seq->strip->stripdata->name);
+ BLI_path_abs(name, BKE_main_blendfile_path_from_global());
+
+ /* initialize input color space */
+ if (seq->type == SEQ_TYPE_IMAGE) {
+ ibuf = IMB_loadiffname(
+ name, IB_test | IB_alphamode_detect, seq->strip->colorspace_settings.name);
+
+ /* byte images are default to straight alpha, however sequencer
+ * works in premul space, so mark strip to be premultiplied first
+ */
+ seq->alpha_mode = SEQ_ALPHA_STRAIGHT;
+ if (ibuf) {
+ if (ibuf->flags & IB_alphamode_premul) {
+ seq->alpha_mode = IMA_ALPHA_PREMUL;
+ }
+
+ IMB_freeImBuf(ibuf);
+ }
+ }
+ }
+}
+
+float BKE_sequence_get_fps(Scene *scene, Sequence *seq)
+{
+ switch (seq->type) {
+ case SEQ_TYPE_MOVIE: {
+ seq_open_anim_file(scene, seq, true);
+ if (BLI_listbase_is_empty(&seq->anims)) {
+ return 0.0f;
+ }
+ StripAnim *strip_anim = seq->anims.first;
+ if (strip_anim->anim == NULL) {
+ return 0.0f;
+ }
+ short frs_sec;
+ float frs_sec_base;
+ if (IMB_anim_get_fps(strip_anim->anim, &frs_sec, &frs_sec_base, true)) {
+ return (float)frs_sec / frs_sec_base;
+ }
+ break;
+ }
+ case SEQ_TYPE_MOVIECLIP:
+ if (seq->clip != NULL) {
+ return BKE_movieclip_get_fps(seq->clip);
+ }
+ break;
+ case SEQ_TYPE_SCENE:
+ if (seq->scene != NULL) {
+ return (float)seq->scene->r.frs_sec / seq->scene->r.frs_sec_base;
+ }
+ break;
+ }
+ return 0.0f;
+}
+
+/* NOTE: this function doesn't fill in image names */
+Sequence *BKE_sequencer_add_image_strip(bContext *C, ListBase *seqbasep, SeqLoadInfo *seq_load)
+{
+ Scene *scene = CTX_data_scene(C); /* only for active seq */
+ Sequence *seq;
+ Strip *strip;
+
+ seq = BKE_sequence_alloc(seqbasep, seq_load->start_frame, seq_load->channel, SEQ_TYPE_IMAGE);
+ seq->blend_mode = SEQ_TYPE_CROSS; /* so alpha adjustment fade to the strip below */
+
+ /* basic defaults */
+ seq->len = seq_load->len ? seq_load->len : 1;
+
+ strip = seq->strip;
+ strip->stripdata = MEM_callocN(seq->len * sizeof(StripElem), "stripelem");
+ BLI_strncpy(strip->dir, seq_load->path, sizeof(strip->dir));
+
+ if (seq_load->stereo3d_format) {
+ *seq->stereo3d_format = *seq_load->stereo3d_format;
+ }
+
+ seq->views_format = seq_load->views_format;
+ seq->flag |= seq_load->flag & SEQ_USE_VIEWS;
+
+ seq_load_apply(CTX_data_main(C), scene, seq, seq_load);
+ BKE_sequence_invalidate_cache_composite(scene, seq);
+
+ return seq;
+}
+
+#ifdef WITH_AUDASPACE
+Sequence *BKE_sequencer_add_sound_strip(bContext *C, ListBase *seqbasep, SeqLoadInfo *seq_load)
+{
+ Main *bmain = CTX_data_main(C);
+ Scene *scene = CTX_data_scene(C); /* only for sound */
+ Editing *ed = BKE_sequencer_editing_get(scene, false);
+ bSound *sound;
+
+ Sequence *seq; /* generic strip vars */
+ Strip *strip;
+ StripElem *se;
+
+ sound = BKE_sound_new_file(bmain, seq_load->path); /* handles relative paths */
+
+ SoundInfo info;
+ if (!BKE_sound_info_get(bmain, sound, &info)) {
+ BKE_id_free(bmain, sound);
+ return NULL;
+ }
+
+ if (info.specs.channels == SOUND_CHANNELS_INVALID) {
+ BKE_id_free(bmain, sound);
+ return NULL;
+ }
+
+ seq = BKE_sequence_alloc(seqbasep, seq_load->start_frame, seq_load->channel, SEQ_TYPE_SOUND_RAM);
+ seq->sound = sound;
+ BLI_strncpy(seq->name + 2, "Sound", SEQ_NAME_MAXSTR - 2);
+ BKE_sequence_base_unique_name_recursive(&scene->ed->seqbase, seq);
+
+ /* basic defaults */
+ /* We add a very small negative offset here, because
+ * ceil(132.0) == 133.0, not nice with videos, see T47135. */
+ seq->len = (int)ceil((double)info.length * FPS - 1e-4);
+ strip = seq->strip;
+
+ /* we only need 1 element to store the filename */
+ strip->stripdata = se = MEM_callocN(sizeof(StripElem), "stripelem");
+
+ BLI_split_dirfile(seq_load->path, strip->dir, se->name, sizeof(strip->dir), sizeof(se->name));
+
+ seq->scene_sound = NULL;
+
+ BKE_sequence_calc_disp(scene, seq);
+
+ /* last active name */
+ BLI_strncpy(ed->act_sounddir, strip->dir, FILE_MAXDIR);
+
+ seq_load_apply(bmain, scene, seq, seq_load);
+
+ /* TODO(sergey): Shall we tag here or in the operator? */
+ DEG_relations_tag_update(bmain);
+
+ return seq;
+}
+#else // WITH_AUDASPACE
+Sequence *BKE_sequencer_add_sound_strip(bContext *C, ListBase *seqbasep, SeqLoadInfo *seq_load)
+{
+ (void)C;
+ (void)seqbasep;
+ (void)seq_load;
+ return NULL;
+}
+#endif // WITH_AUDASPACE
+
+static void seq_anim_add_suffix(Scene *scene, struct anim *anim, const int view_id)
+{
+ const char *suffix = BKE_scene_multiview_view_id_suffix_get(&scene->r, view_id);
+ IMB_suffix_anim(anim, suffix);
+}
+
+Sequence *BKE_sequencer_add_movie_strip(bContext *C, ListBase *seqbasep, SeqLoadInfo *seq_load)
+{
+ Main *bmain = CTX_data_main(C);
+ Scene *scene = CTX_data_scene(C); /* only for sound */
+ char path[sizeof(seq_load->path)];
+
+ Sequence *seq; /* generic strip vars */
+ Strip *strip;
+ StripElem *se;
+ char colorspace[64] = "\0"; /* MAX_COLORSPACE_NAME */
+ bool is_multiview_loaded = false;
+ const bool is_multiview = (seq_load->flag & SEQ_USE_VIEWS) != 0;
+ const int totfiles = seq_num_files(scene, seq_load->views_format, is_multiview);
+ struct anim **anim_arr;
+ int i;
+
+ BLI_strncpy(path, seq_load->path, sizeof(path));
+ BLI_path_abs(path, BKE_main_blendfile_path(bmain));
+
+ anim_arr = MEM_callocN(sizeof(struct anim *) * totfiles, "Video files");
+
+ if (is_multiview && (seq_load->views_format == R_IMF_VIEWS_INDIVIDUAL)) {
+ char prefix[FILE_MAX];
+ const char *ext = NULL;
+ size_t j = 0;
+
+ BKE_scene_multiview_view_prefix_get(scene, path, prefix, &ext);
+
+ if (prefix[0] != '\0') {
+ for (i = 0; i < totfiles; i++) {
+ char str[FILE_MAX];
+
+ seq_multiview_name(scene, i, prefix, ext, str, FILE_MAX);
+ anim_arr[j] = openanim(str, IB_rect, 0, colorspace);
+
+ if (anim_arr[j]) {
+ seq_anim_add_suffix(scene, anim_arr[j], i);
+ j++;
+ }
+ }
+
+ if (j == 0) {
+ MEM_freeN(anim_arr);
+ return NULL;
+ }
+ is_multiview_loaded = true;
+ }
+ }
+
+ if (is_multiview_loaded == false) {
+ anim_arr[0] = openanim(path, IB_rect, 0, colorspace);
+
+ if (anim_arr[0] == NULL) {
+ MEM_freeN(anim_arr);
+ return NULL;
+ }
+ }
+
+ if (seq_load->flag & SEQ_LOAD_MOVIE_SOUND) {
+ seq_load->channel++;
+ }
+ seq = BKE_sequence_alloc(seqbasep, seq_load->start_frame, seq_load->channel, SEQ_TYPE_MOVIE);
+
+ /* multiview settings */
+ if (seq_load->stereo3d_format) {
+ *seq->stereo3d_format = *seq_load->stereo3d_format;
+ seq->views_format = seq_load->views_format;
+ }
+ seq->flag |= seq_load->flag & SEQ_USE_VIEWS;
+
+ seq->type = SEQ_TYPE_MOVIE;
+ seq->blend_mode = SEQ_TYPE_CROSS; /* so alpha adjustment fade to the strip below */
+
+ for (i = 0; i < totfiles; i++) {
+ if (anim_arr[i]) {
+ StripAnim *sanim = MEM_mallocN(sizeof(StripAnim), "Strip Anim");
+ BLI_addtail(&seq->anims, sanim);
+ sanim->anim = anim_arr[i];
+ }
+ else {
+ break;
+ }
+ }
+
+ IMB_anim_load_metadata(anim_arr[0]);
+
+ seq->anim_preseek = IMB_anim_get_preseek(anim_arr[0]);
+ BLI_strncpy(seq->name + 2, "Movie", SEQ_NAME_MAXSTR - 2);
+ BKE_sequence_base_unique_name_recursive(&scene->ed->seqbase, seq);
+
+ /* adjust scene's frame rate settings to match */
+ if (seq_load->flag & SEQ_LOAD_SYNC_FPS) {
+ IMB_anim_get_fps(anim_arr[0], &scene->r.frs_sec, &scene->r.frs_sec_base, true);
+ }
+
+ /* basic defaults */
+ seq->len = IMB_anim_get_duration(anim_arr[0], IMB_TC_RECORD_RUN);
+ strip = seq->strip;
+
+ BLI_strncpy(seq->strip->colorspace_settings.name,
+ colorspace,
+ sizeof(seq->strip->colorspace_settings.name));
+
+ /* we only need 1 element for MOVIE strips */
+ strip->stripdata = se = MEM_callocN(sizeof(StripElem), "stripelem");
+
+ BLI_split_dirfile(seq_load->path, strip->dir, se->name, sizeof(strip->dir), sizeof(se->name));
+
+ BKE_sequence_calc_disp(scene, seq);
+
+ if (seq_load->name[0] == '\0') {
+ BLI_strncpy(seq_load->name, se->name, sizeof(seq_load->name));
+ }
+
+ if (seq_load->flag & SEQ_LOAD_MOVIE_SOUND) {
+ int start_frame_back = seq_load->start_frame;
+ seq_load->channel--;
+ seq_load->seq_sound = BKE_sequencer_add_sound_strip(C, seqbasep, seq_load);
+ seq_load->start_frame = start_frame_back;
+ }
+
+ /* can be NULL */
+ seq_load_apply(CTX_data_main(C), scene, seq, seq_load);
+ BKE_sequence_invalidate_cache_composite(scene, seq);
+
+ MEM_freeN(anim_arr);
+ return seq;
+}
+
+static Sequence *seq_dupli(const Scene *scene_src,
+ Scene *scene_dst,
+ ListBase *new_seq_list,
+ Sequence *seq,
+ int dupe_flag,
+ const int flag)
+{
+ Sequence *seqn = MEM_dupallocN(seq);
+
+ if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) {
+ BKE_sequence_session_uuid_generate(seq);
+ }
+
+ seq->tmp = seqn;
+ seqn->strip = MEM_dupallocN(seq->strip);
+
+ seqn->stereo3d_format = MEM_dupallocN(seq->stereo3d_format);
+
+ /* XXX: add F-Curve duplication stuff? */
+
+ if (seq->strip->crop) {
+ seqn->strip->crop = MEM_dupallocN(seq->strip->crop);
+ }
+
+ if (seq->strip->transform) {
+ seqn->strip->transform = MEM_dupallocN(seq->strip->transform);
+ }
+
+ if (seq->strip->proxy) {
+ seqn->strip->proxy = MEM_dupallocN(seq->strip->proxy);
+ seqn->strip->proxy->anim = NULL;
+ }
+
+ if (seq->prop) {
+ seqn->prop = IDP_CopyProperty_ex(seq->prop, flag);
+ }
+
+ if (seqn->modifiers.first) {
+ BLI_listbase_clear(&seqn->modifiers);
+
+ BKE_sequence_modifier_list_copy(seqn, seq);
+ }
+
+ if (seq->type == SEQ_TYPE_META) {
+ seqn->strip->stripdata = NULL;
+
+ BLI_listbase_clear(&seqn->seqbase);
+ /* WATCH OUT!!! - This metastrip is not recursively duplicated here - do this after!!! */
+ /* - seq_dupli_recursive(&seq->seqbase, &seqn->seqbase);*/
+ }
+ else if (seq->type == SEQ_TYPE_SCENE) {
+ seqn->strip->stripdata = NULL;
+ if (seq->scene_sound) {
+ seqn->scene_sound = BKE_sound_scene_add_scene_sound_defaults(scene_dst, seqn);
+ }
+ }
+ else if (seq->type == SEQ_TYPE_MOVIECLIP) {
+ /* avoid assert */
+ }
+ else if (seq->type == SEQ_TYPE_MASK) {
+ /* avoid assert */
+ }
+ else if (seq->type == SEQ_TYPE_MOVIE) {
+ seqn->strip->stripdata = MEM_dupallocN(seq->strip->stripdata);
+ BLI_listbase_clear(&seqn->anims);
+ }
+ else if (seq->type == SEQ_TYPE_SOUND_RAM) {
+ seqn->strip->stripdata = MEM_dupallocN(seq->strip->stripdata);
+ seqn->scene_sound = NULL;
+ if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
+ id_us_plus((ID *)seqn->sound);
+ }
+ }
+ else if (seq->type == SEQ_TYPE_IMAGE) {
+ seqn->strip->stripdata = MEM_dupallocN(seq->strip->stripdata);
+ }
+ else if (seq->type & SEQ_TYPE_EFFECT) {
+ struct SeqEffectHandle sh;
+ sh = BKE_sequence_get_effect(seq);
+ if (sh.copy) {
+ sh.copy(seqn, seq, flag);
+ }
+
+ seqn->strip->stripdata = NULL;
+ }
+ else {
+ /* sequence type not handled in duplicate! Expect a crash now... */
+ BLI_assert(0);
+ }
+
+ /* When using SEQ_DUPE_UNIQUE_NAME, it is mandatory to add new sequences in relevant container
+ * (scene or meta's one), *before* checking for unique names. Otherwise the meta's list is empty
+ * and hence we miss all seqs in that meta that have already been duplicated (see T55668).
+ * Note that unique name check itself could be done at a later step in calling code, once all
+ * seqs have bee duplicated (that was first, simpler solution), but then handling of animation
+ * data will be broken (see T60194). */
+ if (new_seq_list != NULL) {
+ BLI_addtail(new_seq_list, seqn);
+ }
+
+ if (scene_src == scene_dst) {
+ if (dupe_flag & SEQ_DUPE_UNIQUE_NAME) {
+ BKE_sequence_base_unique_name_recursive(&scene_dst->ed->seqbase, seqn);
+ }
+
+ if (dupe_flag & SEQ_DUPE_ANIM) {
+ BKE_sequencer_dupe_animdata(scene_dst, seq->name + 2, seqn->name + 2);
+ }
+ }
+
+ return seqn;
+}
+
+static void seq_new_fix_links_recursive(Sequence *seq)
+{
+ SequenceModifierData *smd;
+
+ if (seq->type & SEQ_TYPE_EFFECT) {
+ if (seq->seq1 && seq->seq1->tmp) {
+ seq->seq1 = seq->seq1->tmp;
+ }
+ if (seq->seq2 && seq->seq2->tmp) {
+ seq->seq2 = seq->seq2->tmp;
+ }
+ if (seq->seq3 && seq->seq3->tmp) {
+ seq->seq3 = seq->seq3->tmp;
+ }
+ }
+ else if (seq->type == SEQ_TYPE_META) {
+ Sequence *seqn;
+ for (seqn = seq->seqbase.first; seqn; seqn = seqn->next) {
+ seq_new_fix_links_recursive(seqn);
+ }
+ }
+
+ for (smd = seq->modifiers.first; smd; smd = smd->next) {
+ if (smd->mask_sequence && smd->mask_sequence->tmp) {
+ smd->mask_sequence = smd->mask_sequence->tmp;
+ }
+ }
+}
+
+static Sequence *sequence_dupli_recursive_do(const Scene *scene_src,
+ Scene *scene_dst,
+ ListBase *new_seq_list,
+ Sequence *seq,
+ const int dupe_flag)
+{
+ Sequence *seqn;
+
+ seq->tmp = NULL;
+ seqn = seq_dupli(scene_src, scene_dst, new_seq_list, seq, dupe_flag, 0);
+ if (seq->type == SEQ_TYPE_META) {
+ Sequence *s;
+ for (s = seq->seqbase.first; s; s = s->next) {
+ sequence_dupli_recursive_do(scene_src, scene_dst, &seqn->seqbase, s, dupe_flag);
+ }
+ }
+
+ return seqn;
+}
+
+Sequence *BKE_sequence_dupli_recursive(
+ const Scene *scene_src, Scene *scene_dst, ListBase *new_seq_list, Sequence *seq, int dupe_flag)
+{
+ Sequence *seqn = sequence_dupli_recursive_do(scene_src, scene_dst, new_seq_list, seq, dupe_flag);
+
+ /* This does not need to be in recursive call itself, since it is already recursive... */
+ seq_new_fix_links_recursive(seqn);
+
+ return seqn;
+}
+
+void BKE_sequence_base_dupli_recursive(const Scene *scene_src,
+ Scene *scene_dst,
+ ListBase *nseqbase,
+ const ListBase *seqbase,
+ int dupe_flag,
+ const int flag)
+{
+ Sequence *seq;
+ Sequence *seqn = NULL;
+ Sequence *last_seq = BKE_sequencer_active_get((Scene *)scene_src);
+ /* always include meta's strips */
+ int dupe_flag_recursive = dupe_flag | SEQ_DUPE_ALL | SEQ_DUPE_IS_RECURSIVE_CALL;
+
+ for (seq = seqbase->first; seq; seq = seq->next) {
+ seq->tmp = NULL;
+ if ((seq->flag & SELECT) || (dupe_flag & SEQ_DUPE_ALL)) {
+ seqn = seq_dupli(scene_src, scene_dst, nseqbase, seq, dupe_flag, flag);
+ if (seqn) { /*should never fail */
+ if (dupe_flag & SEQ_DUPE_CONTEXT) {
+ seq->flag &= ~SEQ_ALLSEL;
+ seqn->flag &= ~(SEQ_LEFTSEL + SEQ_RIGHTSEL + SEQ_LOCK);
+ }
+
+ if (seq->type == SEQ_TYPE_META) {
+ BKE_sequence_base_dupli_recursive(
+ scene_src, scene_dst, &seqn->seqbase, &seq->seqbase, dupe_flag_recursive, flag);
+ }
+
+ if (dupe_flag & SEQ_DUPE_CONTEXT) {
+ if (seq == last_seq) {
+ BKE_sequencer_active_set(scene_dst, seqn);
+ }
+ }
+ }
+ }
+ }
+
+ /* Fix modifier links recursively from the top level only, when all sequences have been
+ * copied. */
+ if (dupe_flag & SEQ_DUPE_IS_RECURSIVE_CALL) {
+ return;
+ }
+
+ /* fix modifier linking */
+ for (seq = nseqbase->first; seq; seq = seq->next) {
+ seq_new_fix_links_recursive(seq);
+ }
+}
+
+/* called on draw, needs to be fast,
+ * we could cache and use a flag if we want to make checks for file paths resolving for eg. */
+bool BKE_sequence_is_valid_check(Sequence *seq)
+{
+ switch (seq->type) {
+ case SEQ_TYPE_MASK:
+ return (seq->mask != NULL);
+ case SEQ_TYPE_MOVIECLIP:
+ return (seq->clip != NULL);
+ case SEQ_TYPE_SCENE:
+ return (seq->scene != NULL);
+ case SEQ_TYPE_SOUND_RAM:
+ return (seq->sound != NULL);
+ }
+
+ return true;
+}
+
+int BKE_sequencer_find_next_prev_edit(Scene *scene,
+ int cfra,
+ const short side,
+ const bool do_skip_mute,
+ const bool do_center,
+ const bool do_unselected)
+{
+ Editing *ed = BKE_sequencer_editing_get(scene, false);
+ Sequence *seq;
+
+ int dist, best_dist, best_frame = cfra;
+ int seq_frames[2], seq_frames_tot;
+
+ /* In case where both is passed,
+ * frame just finds the nearest end while frame_left the nearest start. */
+
+ best_dist = MAXFRAME * 2;
+
+ if (ed == NULL) {
+ return cfra;
+ }
+
+ for (seq = ed->seqbasep->first; seq; seq = seq->next) {
+ int i;
+
+ if (do_skip_mute && (seq->flag & SEQ_MUTE)) {
+ continue;
+ }
+
+ if (do_unselected && (seq->flag & SELECT)) {
+ continue;
+ }
+
+ if (do_center) {
+ seq_frames[0] = (seq->startdisp + seq->enddisp) / 2;
+ seq_frames_tot = 1;
+ }
+ else {
+ seq_frames[0] = seq->startdisp;
+ seq_frames[1] = seq->enddisp;
+
+ seq_frames_tot = 2;
+ }
+
+ for (i = 0; i < seq_frames_tot; i++) {
+ const int seq_frame = seq_frames[i];
+
+ dist = MAXFRAME * 2;
+
+ switch (side) {
+ case SEQ_SIDE_LEFT:
+ if (seq_frame < cfra) {
+ dist = cfra - seq_frame;
+ }
+ break;
+ case SEQ_SIDE_RIGHT:
+ if (seq_frame > cfra) {
+ dist = seq_frame - cfra;
+ }
+ break;
+ case SEQ_SIDE_BOTH:
+ dist = abs(seq_frame - cfra);
+ break;
+ }
+
+ if (dist < best_dist) {
+ best_frame = seq_frame;
+ best_dist = dist;
+ }
+ }
+ }
+
+ return best_frame;
+}
+
+static void sequencer_all_free_anim_ibufs(ListBase *seqbase, int cfra)
+{
+ for (Sequence *seq = seqbase->first; seq != NULL; seq = seq->next) {
+ if (seq->enddisp < cfra || seq->startdisp > cfra) {
+ BKE_sequence_free_anim(seq);
+ }
+ if (seq->type == SEQ_TYPE_META) {
+ sequencer_all_free_anim_ibufs(&seq->seqbase, cfra);
+ }
+ }
+}
+
+void BKE_sequencer_all_free_anim_ibufs(Scene *scene, int cfra)
+{
+ Editing *ed = BKE_sequencer_editing_get(scene, false);
+ if (ed == NULL) {
+ return;
+ }
+ sequencer_all_free_anim_ibufs(&ed->seqbase, cfra);
+ BKE_sequencer_cache_cleanup(scene);
+}
+
+static bool sequencer_seq_generates_image(Sequence *seq)
+{
+ switch (seq->type) {
+ case SEQ_TYPE_IMAGE:
+ case SEQ_TYPE_SCENE:
+ case SEQ_TYPE_MOVIE:
+ case SEQ_TYPE_MOVIECLIP:
+ case SEQ_TYPE_MASK:
+ case SEQ_TYPE_COLOR:
+ case SEQ_TYPE_TEXT:
+ return true;
+ }
+ return false;
+}
+
+static Sequence *sequencer_check_scene_recursion(Scene *scene, ListBase *seqbase)
+{
+ LISTBASE_FOREACH (Sequence *, seq, seqbase) {
+ if (seq->type == SEQ_TYPE_SCENE && seq->scene == scene) {
+ return seq;
+ }
+
+ if (seq->type == SEQ_TYPE_META && sequencer_check_scene_recursion(scene, &seq->seqbase)) {
+ return seq;
+ }
+ }
+
+ return NULL;
+}
+
+bool BKE_sequencer_check_scene_recursion(Scene *scene, ReportList *reports)
+{
+ Editing *ed = BKE_sequencer_editing_get(scene, false);
+ if (ed == NULL) {
+ return false;
+ }
+
+ Sequence *recursive_seq = sequencer_check_scene_recursion(scene, &ed->seqbase);
+
+ if (recursive_seq != NULL) {
+ BKE_reportf(reports,
+ RPT_WARNING,
+ "Recursion detected in video sequencer. Strip %s at frame %d will not be rendered",
+ recursive_seq->name + 2,
+ recursive_seq->startdisp);
+
+ LISTBASE_FOREACH (Sequence *, seq, &ed->seqbase) {
+ if (seq->type != SEQ_TYPE_SCENE && sequencer_seq_generates_image(seq)) {
+ /* There are other strips to render, so render them. */
+ return false;
+ }
+ }
+ /* No other strips to render - cancel operator. */
+ return true;
+ }
+
+ return false;
+}
+
+/* Check if "seq_main" (indirectly) uses strip "seq". */
+bool BKE_sequencer_render_loop_check(Sequence *seq_main, Sequence *seq)
+{
+ if (seq_main == seq) {
+ return true;
+ }
+
+ if ((seq_main->seq1 && BKE_sequencer_render_loop_check(seq_main->seq1, seq)) ||
+ (seq_main->seq2 && BKE_sequencer_render_loop_check(seq_main->seq2, seq)) ||
+ (seq_main->seq3 && BKE_sequencer_render_loop_check(seq_main->seq3, seq))) {
+ return true;
+ }
+
+ SequenceModifierData *smd;
+ for (smd = seq_main->modifiers.first; smd; smd = smd->next) {
+ if (smd->mask_sequence && BKE_sequencer_render_loop_check(smd->mask_sequence, seq)) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+static void sequencer_flag_users_for_removal(Scene *scene, ListBase *seqbase, Sequence *seq)
+{
+ LISTBASE_FOREACH (Sequence *, user_seq, seqbase) {
+ /* Look in metas for usage of seq. */
+ if (user_seq->type == SEQ_TYPE_META) {
+ sequencer_flag_users_for_removal(scene, &user_seq->seqbase, seq);
+ }
+
+ /* Clear seq from modifiers. */
+ SequenceModifierData *smd;
+ for (smd = user_seq->modifiers.first; smd; smd = smd->next) {
+ if (smd->mask_sequence == seq) {
+ smd->mask_sequence = NULL;
+ }
+ }
+
+ /* Remove effects, that use seq. */
+ if ((user_seq->seq1 && user_seq->seq1 == seq) || (user_seq->seq2 && user_seq->seq2 == seq) ||
+ (user_seq->seq3 && user_seq->seq3 == seq)) {
+ user_seq->flag |= SEQ_FLAG_DELETE;
+ /* Strips can be used as mask even if not in same seqbase. */
+ sequencer_flag_users_for_removal(scene, &scene->ed->seqbase, user_seq);
+ }
+ }
+}
+
+/* Flag seq and its users (effects) for removal. */
+void BKE_sequencer_flag_for_removal(Scene *scene, ListBase *seqbase, Sequence *seq)
+{
+ if (seq == NULL || (seq->flag & SEQ_FLAG_DELETE) != 0) {
+ return;
+ }
+
+ /* Flag and remove meta children. */
+ if (seq->type == SEQ_TYPE_META) {
+ LISTBASE_FOREACH (Sequence *, meta_child, &seq->seqbase) {
+ BKE_sequencer_flag_for_removal(scene, &seq->seqbase, meta_child);
+ }
+ }
+
+ seq->flag |= SEQ_FLAG_DELETE;
+ sequencer_flag_users_for_removal(scene, seqbase, seq);
+}
+
+/* Remove all flagged sequences, return true if sequence is removed. */
+void BKE_sequencer_remove_flagged_sequences(Scene *scene, ListBase *seqbase)
+{
+ LISTBASE_FOREACH_MUTABLE (Sequence *, seq, seqbase) {
+ if (seq->flag & SEQ_FLAG_DELETE) {
+ if (seq->type == SEQ_TYPE_META) {
+ BKE_sequencer_remove_flagged_sequences(scene, &seq->seqbase);
+ }
+ BLI_remlink(seqbase, seq);
+ BKE_sequence_free(scene, seq, true);
+ }
+ }
+}
+
+void BKE_sequencer_check_uuids_unique_and_report(const Scene *scene)
+{
+ if (scene->ed == NULL) {
+ return;
+ }
+
+ struct GSet *used_uuids = BLI_gset_new(
+ BLI_session_uuid_ghash_hash, BLI_session_uuid_ghash_compare, "sequencer used uuids");
+
+ const Sequence *sequence;
+ SEQ_ALL_BEGIN (scene->ed, sequence) {
+ const SessionUUID *session_uuid = &sequence->runtime.session_uuid;
+ if (!BLI_session_uuid_is_generated(session_uuid)) {
+ printf("Sequence %s does not have UUID generated.\n", sequence->name);
+ continue;
+ }
+
+ if (BLI_gset_lookup(used_uuids, session_uuid) != NULL) {
+ printf("Sequence %s has duplicate UUID generated.\n", sequence->name);
+ continue;
+ }
+
+ BLI_gset_insert(used_uuids, (void *)session_uuid);
+ }
+ SEQ_ALL_END;
+
+ BLI_gset_free(used_uuids, NULL);
+}