2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
22 * - Blender Foundation, 2003-2009
23 * - Peter Schlaile <peter [at] schlaile [dot] de> 2005/2006
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenkernel/intern/sequencer.c
37 #include "MEM_guardedalloc.h"
39 #include "DNA_sequence_types.h"
40 #include "DNA_movieclip_types.h"
41 #include "DNA_mask_types.h"
42 #include "DNA_scene_types.h"
43 #include "DNA_anim_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_sound_types.h"
48 #include "BLI_fileops.h"
49 #include "BLI_listbase.h"
50 #include "BLI_path_util.h"
51 #include "BLI_string.h"
52 #include "BLI_string_utf8.h"
53 #include "BLI_threads.h"
54 #include "BLI_utildefines.h"
57 # include "BLI_winstuff.h"
62 #include "BLF_translation.h"
64 #include "BKE_animsys.h"
65 #include "BKE_depsgraph.h"
66 #include "BKE_global.h"
67 #include "BKE_image.h"
69 #include "BKE_sequencer.h"
70 #include "BKE_movieclip.h"
71 #include "BKE_fcurve.h"
72 #include "BKE_scene.h"
74 #include "BKE_library.h"
75 #include "BKE_idprop.h"
77 #include "RNA_access.h"
79 #include "RE_pipeline.h"
83 #include "IMB_imbuf.h"
84 #include "IMB_imbuf_types.h"
85 #include "IMB_colormanagement.h"
87 #include "BKE_context.h"
88 #include "BKE_sound.h"
91 # include "AUD_C-API.h"
94 static ImBuf *seq_render_strip_stack(const SeqRenderData *context, ListBase *seqbasep, float cfra, int chanshown);
95 static ImBuf *seq_render_strip(const SeqRenderData *context, Sequence *seq, float cfra);
96 static void seq_free_animdata(Scene *scene, Sequence *seq);
97 static ImBuf *seq_render_mask(const SeqRenderData *context, Mask *mask, float nr, bool make_float);
98 static size_t seq_num_files(Scene *scene, char views_format, const bool is_multiview);
99 static void seq_anim_add_suffix(Scene *scene, struct anim *anim, const size_t view_id);
101 /* **** XXX ******** */
103 ListBase seqbase_clipboard;
104 int seqbase_clipboard_frame;
105 SequencerDrawView sequencer_view3d_cb = NULL; /* NULL in background mode */
107 #if 0 /* unused function */
108 static void printf_strip(Sequence *seq)
110 fprintf(stderr, "name: '%s', len:%d, start:%d, (startofs:%d, endofs:%d), "
111 "(startstill:%d, endstill:%d), machine:%d, (startdisp:%d, enddisp:%d)\n",
112 seq->name, seq->len, seq->start, seq->startofs, seq->endofs, seq->startstill, seq->endstill, seq->machine,
113 seq->startdisp, seq->enddisp);
115 fprintf(stderr, "\tseq_tx_set_final_left: %d %d\n\n", seq_tx_get_final_left(seq, 0),
116 seq_tx_get_final_right(seq, 0));
120 int BKE_sequencer_base_recursive_apply(ListBase *seqbase, int (*apply_func)(Sequence *seq, void *), void *arg)
123 for (iseq = seqbase->first; iseq; iseq = iseq->next) {
124 if (BKE_sequencer_recursive_apply(iseq, apply_func, arg) == -1)
125 return -1; /* bail out */
130 int BKE_sequencer_recursive_apply(Sequence *seq, int (*apply_func)(Sequence *, void *), void *arg)
132 int ret = apply_func(seq, arg);
135 return -1; /* bail out */
137 if (ret && seq->seqbase.first)
138 ret = BKE_sequencer_base_recursive_apply(&seq->seqbase, apply_func, arg);
143 /*********************** alloc / free functions *************************/
147 static void free_proxy_seq(Sequence *seq)
149 if (seq->strip && seq->strip->proxy && seq->strip->proxy->anim) {
150 IMB_free_anim(seq->strip->proxy->anim);
151 seq->strip->proxy->anim = NULL;
155 static void seq_free_strip(Strip *strip)
161 printf("error: negative users in strip\n");
165 if (strip->stripdata) {
166 MEM_freeN(strip->stripdata);
170 if (strip->proxy->anim) {
171 IMB_free_anim(strip->proxy->anim);
174 MEM_freeN(strip->proxy);
177 MEM_freeN(strip->crop);
179 if (strip->transform) {
180 MEM_freeN(strip->transform);
186 /* only give option to skip cache locally (static func) */
187 static void BKE_sequence_free_ex(Scene *scene, Sequence *seq, const bool do_cache)
190 seq_free_strip(seq->strip);
192 BKE_sequence_free_anim(seq);
194 if (seq->type & SEQ_TYPE_EFFECT) {
195 struct SeqEffectHandle sh = BKE_sequence_get_effect(seq);
201 ((ID *)seq->sound)->us--;
204 if (seq->stereo3d_format) {
205 MEM_freeN(seq->stereo3d_format);
208 /* clipboard has no scene and will never have a sound handle or be active
209 * same goes to sequences copy for proxy rebuild job
212 Editing *ed = scene->ed;
214 if (ed->act_seq == seq)
217 if (seq->scene_sound && ELEM(seq->type, SEQ_TYPE_SOUND_RAM, SEQ_TYPE_SCENE))
218 BKE_sound_remove_scene_sound(scene, seq->scene_sound);
220 seq_free_animdata(scene, seq);
224 IDP_FreeProperty(seq->prop);
225 MEM_freeN(seq->prop);
229 BKE_sequence_modifier_clear(seq);
231 /* free cached data used by this strip,
232 * also invalidate cache for all dependent sequences
234 * be _very_ careful here, invalidating cache loops over the scene sequences and
235 * assumes the listbase is valid for all strips, this may not be the case if lists are being freed.
236 * this is optional BKE_sequence_invalidate_cache
240 BKE_sequence_invalidate_cache(scene, seq);
247 void BKE_sequence_free(Scene *scene, Sequence *seq)
249 BKE_sequence_free_ex(scene, seq, true);
252 /* Function to free imbuf and anim data on changes */
253 void BKE_sequence_free_anim(Sequence *seq)
255 while (seq->anims.last) {
256 StripAnim *sanim = seq->anims.last;
257 BLI_remlink(&seq->anims, sanim);
260 IMB_free_anim(sanim->anim);
268 /* cache must be freed before calling this function
269 * since it leaves the seqbase in an invalid state */
270 static void seq_free_sequence_recurse(Scene *scene, Sequence *seq)
272 Sequence *iseq, *iseq_next;
274 for (iseq = seq->seqbase.first; iseq; iseq = iseq_next) {
275 iseq_next = iseq->next;
276 seq_free_sequence_recurse(scene, iseq);
279 BKE_sequence_free_ex(scene, seq, false);
283 Editing *BKE_sequencer_editing_get(Scene *scene, bool alloc)
286 BKE_sequencer_editing_ensure(scene);
291 void BKE_sequencer_free_clipboard(void)
293 Sequence *seq, *nseq;
295 BKE_sequencer_base_clipboard_pointers_free(&seqbase_clipboard);
297 for (seq = seqbase_clipboard.first; seq; seq = nseq) {
299 seq_free_sequence_recurse(NULL, seq);
301 BLI_listbase_clear(&seqbase_clipboard);
304 /* -------------------------------------------------------------------- */
305 /* Manage pointers in the clipboard.
306 * note that these pointers should _never_ be access in the sequencer,
307 * they are only for storage while in the clipboard
308 * notice 'newid' is used for temp pointer storage here, validate on access.
310 #define ID_PT (*id_pt)
311 static void seqclipboard_ptr_free(ID **id_pt)
314 BLI_assert(ID_PT->newid != NULL);
319 static void seqclipboard_ptr_store(ID **id_pt)
323 ID_PT = MEM_dupallocN(ID_PT);
324 ID_PT->newid = id_prev;
327 static void seqclipboard_ptr_restore(Main *bmain, ID **id_pt)
330 const ListBase *lb = which_libbase(bmain, GS(ID_PT->name));
333 BLI_assert(ID_PT->newid != NULL);
334 if (BLI_findindex(lb, (ID_PT)->newid) != -1) {
335 /* the pointer is still valid */
336 id_restore = (ID_PT)->newid;
339 /* the pointer of the same name still exists */
340 id_restore = BLI_findstring(lb, (ID_PT)->name + 2, offsetof(ID, name) + 2);
343 if (id_restore == NULL) {
344 /* check for a data with the same filename */
345 switch (GS(ID_PT->name)) {
348 id_restore = BLI_findstring(lb, ((bSound *)ID_PT)->name, offsetof(bSound, name));
349 if (id_restore == NULL) {
350 id_restore = BKE_sound_new_file(bmain, ((bSound *)ID_PT)->name);
351 (ID_PT)->newid = id_restore; /* reuse next time */
357 id_restore = BLI_findstring(lb, ((MovieClip *)ID_PT)->name, offsetof(MovieClip, name));
358 if (id_restore == NULL) {
359 id_restore = BKE_movieclip_file_add(bmain, ((MovieClip *)ID_PT)->name);
360 (ID_PT)->newid = id_restore; /* reuse next time */
372 void BKE_sequence_clipboard_pointers_free(Sequence *seq)
374 seqclipboard_ptr_free((ID **)&seq->scene);
375 seqclipboard_ptr_free((ID **)&seq->scene_camera);
376 seqclipboard_ptr_free((ID **)&seq->clip);
377 seqclipboard_ptr_free((ID **)&seq->mask);
378 seqclipboard_ptr_free((ID **)&seq->sound);
380 void BKE_sequence_clipboard_pointers_store(Sequence *seq)
382 seqclipboard_ptr_store((ID **)&seq->scene);
383 seqclipboard_ptr_store((ID **)&seq->scene_camera);
384 seqclipboard_ptr_store((ID **)&seq->clip);
385 seqclipboard_ptr_store((ID **)&seq->mask);
386 seqclipboard_ptr_store((ID **)&seq->sound);
388 void BKE_sequence_clipboard_pointers_restore(Sequence *seq, Main *bmain)
390 seqclipboard_ptr_restore(bmain, (ID **)&seq->scene);
391 seqclipboard_ptr_restore(bmain, (ID **)&seq->scene_camera);
392 seqclipboard_ptr_restore(bmain, (ID **)&seq->clip);
393 seqclipboard_ptr_restore(bmain, (ID **)&seq->mask);
394 seqclipboard_ptr_restore(bmain, (ID **)&seq->sound);
397 /* recursive versions of funcions above */
398 void BKE_sequencer_base_clipboard_pointers_free(ListBase *seqbase)
401 for (seq = seqbase->first; seq; seq = seq->next) {
402 BKE_sequence_clipboard_pointers_free(seq);
403 BKE_sequencer_base_clipboard_pointers_free(&seq->seqbase);
406 void BKE_sequencer_base_clipboard_pointers_store(ListBase *seqbase)
409 for (seq = seqbase->first; seq; seq = seq->next) {
410 BKE_sequence_clipboard_pointers_store(seq);
411 BKE_sequencer_base_clipboard_pointers_store(&seq->seqbase);
414 void BKE_sequencer_base_clipboard_pointers_restore(ListBase *seqbase, Main *bmain)
417 for (seq = seqbase->first; seq; seq = seq->next) {
418 BKE_sequence_clipboard_pointers_restore(seq, bmain);
419 BKE_sequencer_base_clipboard_pointers_restore(&seq->seqbase, bmain);
423 /* end clipboard pointer mess */
426 Editing *BKE_sequencer_editing_ensure(Scene *scene)
428 if (scene->ed == NULL) {
431 ed = scene->ed = MEM_callocN(sizeof(Editing), "addseq");
432 ed->seqbasep = &ed->seqbase;
438 void BKE_sequencer_editing_free(Scene *scene)
440 Editing *ed = scene->ed;
446 /* this may not be the active scene!, could be smarter about this */
447 BKE_sequencer_cache_cleanup();
451 /* handle cache freeing above */
452 BKE_sequence_free_ex(scene, seq, false);
456 BLI_freelistN(&ed->metastack);
463 /*********************** Sequencer color space functions *************************/
465 static void sequencer_imbuf_assign_spaces(Scene *scene, ImBuf *ibuf)
467 if (ibuf->rect_float) {
468 IMB_colormanagement_assign_float_colorspace(ibuf, scene->sequencer_colorspace_settings.name);
472 void BKE_sequencer_imbuf_to_sequencer_space(Scene *scene, ImBuf *ibuf, bool make_float)
474 const char *from_colorspace = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_SCENE_LINEAR);
475 const char *to_colorspace = scene->sequencer_colorspace_settings.name;
476 const char *float_colorspace = IMB_colormanagement_get_float_colorspace(ibuf);
478 if (!ibuf->rect_float) {
480 const char *byte_colorspace = IMB_colormanagement_get_rect_colorspace(ibuf);
481 if (make_float || !STREQ(to_colorspace, byte_colorspace)) {
482 /* If byte space is not in sequencer's working space, we deliver float color space,
483 * this is to to prevent data loss.
486 /* when converting byte buffer to float in sequencer we need to make float
487 * buffer be in sequencer's working space, which is currently only doable
492 * OCIO_TODO: would be nice to support direct single transform from byte to sequencer's
495 IMB_float_from_rect(ibuf);
506 if (from_colorspace && from_colorspace[0] != '\0') {
508 imb_freerectImBuf(ibuf);
510 if (!STREQ(float_colorspace, to_colorspace)) {
511 IMB_colormanagement_transform_threaded(ibuf->rect_float, ibuf->x, ibuf->y, ibuf->channels,
512 from_colorspace, to_colorspace, true);
513 sequencer_imbuf_assign_spaces(scene, ibuf);
518 void BKE_sequencer_imbuf_from_sequencer_space(Scene *scene, ImBuf *ibuf)
520 const char *from_colorspace = scene->sequencer_colorspace_settings.name;
521 const char *to_colorspace = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_SCENE_LINEAR);
523 if (!ibuf->rect_float)
526 if (to_colorspace && to_colorspace[0] != '\0') {
527 IMB_colormanagement_transform_threaded(ibuf->rect_float, ibuf->x, ibuf->y, ibuf->channels,
528 from_colorspace, to_colorspace, true);
529 IMB_colormanagement_assign_float_colorspace(ibuf, to_colorspace);
533 void BKE_sequencer_pixel_from_sequencer_space_v4(struct Scene *scene, float pixel[4])
535 const char *from_colorspace = scene->sequencer_colorspace_settings.name;
536 const char *to_colorspace = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_SCENE_LINEAR);
538 if (to_colorspace && to_colorspace[0] != '\0') {
539 IMB_colormanagement_transform_v4(pixel, from_colorspace, to_colorspace);
542 /* if no color management enables fallback to legacy conversion */
543 srgb_to_linearrgb_v4(pixel, pixel);
547 /*********************** sequencer pipeline functions *************************/
549 void BKE_sequencer_new_render_data(
550 EvaluationContext *eval_ctx,
551 Main *bmain, Scene *scene, int rectx, int recty,
552 int preview_render_size,
553 SeqRenderData *r_context)
555 r_context->eval_ctx = eval_ctx;
556 r_context->bmain = bmain;
557 r_context->scene = scene;
558 r_context->rectx = rectx;
559 r_context->recty = recty;
560 r_context->preview_render_size = preview_render_size;
561 r_context->motion_blur_samples = 0;
562 r_context->motion_blur_shutter = 0;
563 r_context->skip_cache = false;
564 r_context->is_proxy_render = false;
565 r_context->view_id = 0;
568 /* ************************* iterator ************************** */
569 /* *************** (replaces old WHILE_SEQ) ********************* */
570 /* **************** use now SEQ_BEGIN () SEQ_END ***************** */
572 /* sequence strip iterator:
573 * - builds a full array, recursively into meta strips
576 static void seq_count(ListBase *seqbase, int *tot)
580 for (seq = seqbase->first; seq; seq = seq->next) {
583 if (seq->seqbase.first)
584 seq_count(&seq->seqbase, tot);
588 static void seq_build_array(ListBase *seqbase, Sequence ***array, int depth)
592 for (seq = seqbase->first; seq; seq = seq->next) {
595 if (seq->seqbase.first)
596 seq_build_array(&seq->seqbase, array, depth + 1);
603 static void seq_array(Editing *ed, Sequence ***seqarray, int *tot, bool use_pointer)
614 seq_count(ed->seqbasep, tot);
616 seq_count(&ed->seqbase, tot);
621 *seqarray = array = MEM_mallocN(sizeof(Sequence *) * (*tot), "SeqArray");
623 seq_build_array(ed->seqbasep, &array, 0);
625 seq_build_array(&ed->seqbase, &array, 0);
628 void BKE_sequence_iterator_begin(Editing *ed, SeqIterator *iter, bool use_pointer)
630 memset(iter, 0, sizeof(*iter));
631 seq_array(ed, &iter->array, &iter->tot, use_pointer);
635 iter->seq = iter->array[iter->cur];
640 void BKE_sequence_iterator_next(SeqIterator *iter)
642 if (++iter->cur < iter->tot)
643 iter->seq = iter->array[iter->cur];
648 void BKE_sequence_iterator_end(SeqIterator *iter)
651 MEM_freeN(iter->array);
656 static int metaseq_start(Sequence *metaseq)
658 return metaseq->start + metaseq->startofs;
661 static int metaseq_end(Sequence *metaseq)
663 return metaseq->start + metaseq->len - metaseq->endofs;
666 static void seq_update_sound_bounds_recursive_rec(Scene *scene, Sequence *metaseq, int start, int end)
670 /* for sound we go over full meta tree to update bounds of the sound strips,
671 * since sound is played outside of evaluating the imbufs, */
672 for (seq = metaseq->seqbase.first; seq; seq = seq->next) {
673 if (seq->type == SEQ_TYPE_META) {
674 seq_update_sound_bounds_recursive_rec(scene, seq, max_ii(start, metaseq_start(seq)),
675 min_ii(end, metaseq_end(seq)));
677 else if (ELEM(seq->type, SEQ_TYPE_SOUND_RAM, SEQ_TYPE_SCENE)) {
678 if (seq->scene_sound) {
679 int startofs = seq->startofs;
680 int endofs = seq->endofs;
681 if (seq->startofs + seq->start < start)
682 startofs = start - seq->start;
684 if (seq->start + seq->len - seq->endofs > end)
685 endofs = seq->start + seq->len - end;
687 BKE_sound_move_scene_sound(scene, seq->scene_sound, seq->start + startofs,
688 seq->start + seq->len - endofs, startofs + seq->anim_startofs);
694 static void seq_update_sound_bounds_recursive(Scene *scene, Sequence *metaseq)
696 seq_update_sound_bounds_recursive_rec(scene, metaseq, metaseq_start(metaseq), metaseq_end(metaseq));
699 void BKE_sequence_calc_disp(Scene *scene, Sequence *seq)
701 if (seq->startofs && seq->startstill)
703 if (seq->endofs && seq->endstill)
706 seq->startdisp = seq->start + seq->startofs - seq->startstill;
707 seq->enddisp = seq->start + seq->len - seq->endofs + seq->endstill;
709 seq->handsize = 10.0; /* 10 frames */
710 if (seq->enddisp - seq->startdisp < 10) {
711 seq->handsize = (float)(0.5 * (seq->enddisp - seq->startdisp));
713 else if (seq->enddisp - seq->startdisp > 250) {
714 seq->handsize = (float)((seq->enddisp - seq->startdisp) / 25);
717 if (ELEM(seq->type, SEQ_TYPE_SOUND_RAM, SEQ_TYPE_SCENE)) {
718 BKE_sequencer_update_sound_bounds(scene, seq);
720 else if (seq->type == SEQ_TYPE_META) {
721 seq_update_sound_bounds_recursive(scene, seq);
725 void BKE_sequence_calc(Scene *scene, Sequence *seq)
730 /* check all metas recursively */
731 seqm = seq->seqbase.first;
733 if (seqm->seqbase.first) BKE_sequence_calc(scene, seqm);
737 /* effects and meta: automatic start and end */
739 if (seq->type & SEQ_TYPE_EFFECT) {
741 if (seq->seq2 == NULL)
742 seq->seq2 = seq->seq1;
743 if (seq->seq3 == NULL)
744 seq->seq3 = seq->seq1;
746 /* effecten go from seq1 -> seq2: test */
748 /* we take the largest start and smallest end */
750 // seq->start = seq->startdisp = MAX2(seq->seq1->startdisp, seq->seq2->startdisp);
751 // seq->enddisp = MIN2(seq->seq1->enddisp, seq->seq2->enddisp);
754 /* XXX These resets should not be necessary, but users used to be able to
755 * edit effect's length, leading to strange results. See [#29190] */
756 seq->startofs = seq->endofs = seq->startstill = seq->endstill = 0;
757 seq->start = seq->startdisp = max_iii(seq->seq1->startdisp, seq->seq2->startdisp, seq->seq3->startdisp);
758 seq->enddisp = min_iii(seq->seq1->enddisp, seq->seq2->enddisp, seq->seq3->enddisp);
759 /* we cant help if strips don't overlap, it wont give useful results.
760 * but at least ensure 'len' is never negative which causes bad bugs elsewhere. */
761 if (seq->enddisp < seq->startdisp) {
762 /* simple start/end swap */
763 seq->start = seq->enddisp;
764 seq->enddisp = seq->startdisp;
765 seq->startdisp = seq->start;
766 seq->flag |= SEQ_INVALID_EFFECT;
769 seq->flag &= ~SEQ_INVALID_EFFECT;
772 seq->len = seq->enddisp - seq->startdisp;
775 BKE_sequence_calc_disp(scene, seq);
779 if (seq->type == SEQ_TYPE_META) {
780 seqm = seq->seqbase.first;
785 if (seqm->startdisp < min) min = seqm->startdisp;
786 if (seqm->enddisp > max) max = seqm->enddisp;
789 seq->start = min + seq->anim_startofs;
790 seq->len = max - min;
791 seq->len -= seq->anim_startofs;
792 seq->len -= seq->anim_endofs;
794 seq_update_sound_bounds_recursive(scene, seq);
796 BKE_sequence_calc_disp(scene, seq);
800 static void seq_multiview_name(Scene *scene, const size_t view_id, const char *prefix,
801 const char *ext, char *r_path, size_t r_size)
803 const char *suffix = BKE_scene_multiview_view_id_suffix_get(&scene->r, view_id);
804 BLI_snprintf(r_path, r_size, "%s%s%s", prefix, suffix, ext);
807 /* note: caller should run BKE_sequence_calc(scene, seq) after */
808 void BKE_sequence_reload_new_file(Scene *scene, Sequence *seq, const bool lock_range)
811 int prev_startdisp = 0, prev_enddisp = 0;
812 /* note: don't rename the strip, will break animation curves */
815 SEQ_TYPE_MOVIE, SEQ_TYPE_IMAGE, SEQ_TYPE_SOUND_RAM,
816 SEQ_TYPE_SCENE, SEQ_TYPE_META, SEQ_TYPE_MOVIECLIP, SEQ_TYPE_MASK) == 0)
822 /* keep so we don't have to move the actual start and end points (only the data) */
823 BKE_sequence_calc_disp(scene, seq);
824 prev_startdisp = seq->startdisp;
825 prev_enddisp = seq->enddisp;
832 size_t olen = MEM_allocN_len(seq->strip->stripdata) / sizeof(StripElem);
835 seq->len -= seq->anim_startofs;
836 seq->len -= seq->anim_endofs;
845 bool is_multiview_loaded = false;
846 const bool is_multiview = (seq->flag & SEQ_USE_VIEWS) != 0 &&
847 (scene->r.scemode & R_MULTIVIEW) != 0;
849 BLI_join_dirfile(path, sizeof(path), seq->strip->dir,
850 seq->strip->stripdata->name);
851 BLI_path_abs(path, G.main->name);
853 BKE_sequence_free_anim(seq);
855 if (is_multiview && (seq->views_format == R_IMF_VIEWS_INDIVIDUAL)) {
856 char prefix[FILE_MAX];
858 size_t totfiles = seq_num_files(scene, seq->views_format, true);
861 BKE_scene_multiview_view_prefix_get(scene, path, prefix, &ext);
863 if (prefix[0] != '\0') {
864 for (i = 0; i < totfiles; i++) {
868 seq_multiview_name(scene, i, prefix, ext, str, FILE_MAX);
869 anim = openanim(str, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
870 seq->streamindex, seq->strip->colorspace_settings.name);
873 seq_anim_add_suffix(scene, anim, i);
874 sanim = MEM_mallocN(sizeof(StripAnim), "Strip Anim");
875 BLI_addtail(&seq->anims, sanim);
879 is_multiview_loaded = true;
883 if (is_multiview_loaded == false) {
885 anim = openanim(path, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
886 seq->streamindex, seq->strip->colorspace_settings.name);
888 sanim = MEM_mallocN(sizeof(StripAnim), "Strip Anim");
889 BLI_addtail(&seq->anims, sanim);
894 /* use the first video as reference for everything */
895 sanim = seq->anims.first;
897 if ((!sanim) || (!sanim->anim)) {
901 seq->len = IMB_anim_get_duration(sanim->anim, seq->strip->proxy ? seq->strip->proxy->tc : IMB_TC_RECORD_RUN);
903 seq->anim_preseek = IMB_anim_get_preseek(sanim->anim);
905 seq->len -= seq->anim_startofs;
906 seq->len -= seq->anim_endofs;
912 case SEQ_TYPE_MOVIECLIP:
913 if (seq->clip == NULL)
916 seq->len = BKE_movieclip_get_duration(seq->clip);
918 seq->len -= seq->anim_startofs;
919 seq->len -= seq->anim_endofs;
925 if (seq->mask == NULL)
927 seq->len = BKE_mask_get_duration(seq->mask);
928 seq->len -= seq->anim_startofs;
929 seq->len -= seq->anim_endofs;
934 case SEQ_TYPE_SOUND_RAM:
935 #ifdef WITH_AUDASPACE
938 seq->len = ceil((double)AUD_getInfo(seq->sound->playback_handle).length * FPS);
939 seq->len -= seq->anim_startofs;
940 seq->len -= seq->anim_endofs;
950 seq->len = (seq->scene) ? seq->scene->r.efra - seq->scene->r.sfra + 1 : 0;
951 seq->len -= seq->anim_startofs;
952 seq->len -= seq->anim_endofs;
963 BKE_sequence_tx_set_final_left(seq, prev_startdisp);
964 BKE_sequence_tx_set_final_right(seq, prev_enddisp);
965 BKE_sequence_single_fix(seq);
968 BKE_sequence_calc(scene, seq);
971 void BKE_sequencer_sort(Scene *scene)
973 /* all strips together per kind, and in order of y location ("machine") */
974 ListBase seqbase, effbase;
975 Editing *ed = BKE_sequencer_editing_get(scene, false);
976 Sequence *seq, *seqt;
981 BLI_listbase_clear(&seqbase);
982 BLI_listbase_clear(&effbase);
984 while ((seq = BLI_pophead(ed->seqbasep))) {
986 if (seq->type & SEQ_TYPE_EFFECT) {
987 seqt = effbase.first;
989 if (seqt->machine >= seq->machine) {
990 BLI_insertlinkbefore(&effbase, seqt, seq);
996 BLI_addtail(&effbase, seq);
999 seqt = seqbase.first;
1001 if (seqt->machine >= seq->machine) {
1002 BLI_insertlinkbefore(&seqbase, seqt, seq);
1008 BLI_addtail(&seqbase, seq);
1012 BLI_movelisttolist(&seqbase, &effbase);
1013 *(ed->seqbasep) = seqbase;
1016 static int clear_scene_in_allseqs_cb(Sequence *seq, void *arg_pt)
1018 if (seq->scene == (Scene *)arg_pt)
1023 void BKE_sequencer_clear_scene_in_allseqs(Main *bmain, Scene *scene)
1027 /* when a scene is deleted: test all seqs */
1028 for (scene_iter = bmain->scene.first; scene_iter; scene_iter = scene_iter->id.next) {
1029 if (scene_iter != scene && scene_iter->ed) {
1030 BKE_sequencer_base_recursive_apply(&scene_iter->ed->seqbase, clear_scene_in_allseqs_cb, scene);
1035 typedef struct SeqUniqueInfo {
1037 char name_src[SEQ_NAME_MAXSTR];
1038 char name_dest[SEQ_NAME_MAXSTR];
1043 static void seqbase_unique_name(ListBase *seqbasep, SeqUniqueInfo *sui)
1046 for (seq = seqbasep->first; seq; seq = seq->next) {
1047 if ((sui->seq != seq) && STREQ(sui->name_dest, seq->name + 2)) {
1048 /* SEQ_NAME_MAXSTR -4 for the number, -1 for \0, - 2 for prefix */
1049 BLI_snprintf(sui->name_dest, sizeof(sui->name_dest), "%.*s.%03d", SEQ_NAME_MAXSTR - 4 - 1 - 2,
1050 sui->name_src, sui->count++);
1051 sui->match = 1; /* be sure to re-scan */
1056 static int seqbase_unique_name_recursive_cb(Sequence *seq, void *arg_pt)
1058 if (seq->seqbase.first)
1059 seqbase_unique_name(&seq->seqbase, (SeqUniqueInfo *)arg_pt);
1063 void BKE_sequence_base_unique_name_recursive(ListBase *seqbasep, Sequence *seq)
1068 BLI_strncpy(sui.name_src, seq->name + 2, sizeof(sui.name_src));
1069 BLI_strncpy(sui.name_dest, seq->name + 2, sizeof(sui.name_dest));
1072 sui.match = 1; /* assume the worst to start the loop */
1074 /* Strip off the suffix */
1075 if ((dot = strrchr(sui.name_src, '.'))) {
1080 sui.count = atoi(dot) + 1;
1085 seqbase_unique_name(seqbasep, &sui);
1086 BKE_sequencer_base_recursive_apply(seqbasep, seqbase_unique_name_recursive_cb, &sui);
1089 BLI_strncpy(seq->name + 2, sui.name_dest, sizeof(seq->name) - 2);
1092 static const char *give_seqname_by_type(int type)
1095 case SEQ_TYPE_META: return "Meta";
1096 case SEQ_TYPE_IMAGE: return "Image";
1097 case SEQ_TYPE_SCENE: return "Scene";
1098 case SEQ_TYPE_MOVIE: return "Movie";
1099 case SEQ_TYPE_MOVIECLIP: return "Clip";
1100 case SEQ_TYPE_MASK: return "Mask";
1101 case SEQ_TYPE_SOUND_RAM: return "Audio";
1102 case SEQ_TYPE_CROSS: return "Cross";
1103 case SEQ_TYPE_GAMCROSS: return "Gamma Cross";
1104 case SEQ_TYPE_ADD: return "Add";
1105 case SEQ_TYPE_SUB: return "Sub";
1106 case SEQ_TYPE_MUL: return "Mul";
1107 case SEQ_TYPE_ALPHAOVER: return "Alpha Over";
1108 case SEQ_TYPE_ALPHAUNDER: return "Alpha Under";
1109 case SEQ_TYPE_OVERDROP: return "Over Drop";
1110 case SEQ_TYPE_WIPE: return "Wipe";
1111 case SEQ_TYPE_GLOW: return "Glow";
1112 case SEQ_TYPE_TRANSFORM: return "Transform";
1113 case SEQ_TYPE_COLOR: return "Color";
1114 case SEQ_TYPE_MULTICAM: return "Multicam";
1115 case SEQ_TYPE_ADJUSTMENT: return "Adjustment";
1116 case SEQ_TYPE_SPEED: return "Speed";
1117 case SEQ_TYPE_GAUSSIAN_BLUR: return "Gaussian Blur";
1123 const char *BKE_sequence_give_name(Sequence *seq)
1125 const char *name = give_seqname_by_type(seq->type);
1128 if (seq->type < SEQ_TYPE_EFFECT) {
1129 return seq->strip->dir;
1138 /*********************** DO THE SEQUENCE *************************/
1140 static void make_black_ibuf(ImBuf *ibuf)
1146 if (ibuf == NULL || (ibuf->rect == NULL && ibuf->rect_float == NULL)) {
1150 tot = ibuf->x * ibuf->y;
1153 rect_float = ibuf->rect_float;
1156 memset(rect, 0, tot * sizeof(char) * 4);
1160 memset(rect_float, 0, tot * sizeof(float) * 4);
1164 static void multibuf(ImBuf *ibuf, const float fmul)
1171 rt = (char *)ibuf->rect;
1172 rt_float = ibuf->rect_float;
1175 const int imul = (int)(256.0f * fmul);
1176 a = ibuf->x * ibuf->y;
1178 rt[0] = min_ii((imul * rt[0]) >> 8, 255);
1179 rt[1] = min_ii((imul * rt[1]) >> 8, 255);
1180 rt[2] = min_ii((imul * rt[2]) >> 8, 255);
1181 rt[3] = min_ii((imul * rt[3]) >> 8, 255);
1187 a = ibuf->x * ibuf->y;
1189 rt_float[0] *= fmul;
1190 rt_float[1] *= fmul;
1191 rt_float[2] *= fmul;
1192 rt_float[3] *= fmul;
1199 static float give_stripelem_index(Sequence *seq, float cfra)
1202 int sta = seq->start;
1203 int end = seq->start + seq->len - 1;
1205 if (seq->type & SEQ_TYPE_EFFECT) {
1213 if (seq->flag & SEQ_REVERSE_FRAMES) {
1214 /*reverse frame in this sequence */
1215 if (cfra <= sta) nr = end - sta;
1216 else if (cfra >= end) nr = 0;
1217 else nr = end - cfra;
1220 if (cfra <= sta) nr = 0;
1221 else if (cfra >= end) nr = end - sta;
1222 else nr = cfra - sta;
1225 if (seq->strobe < 1.0f) seq->strobe = 1.0f;
1227 if (seq->strobe > 1.0f) {
1228 nr -= fmodf((double)nr, (double)seq->strobe);
1234 StripElem *BKE_sequencer_give_stripelem(Sequence *seq, int cfra)
1236 StripElem *se = seq->strip->stripdata;
1238 if (seq->type == SEQ_TYPE_IMAGE) {
1239 /* only IMAGE strips use the whole array, MOVIE strips use only the first element,
1240 * all other strips don't use this...
1243 int nr = (int) give_stripelem_index(seq, cfra);
1245 if (nr == -1 || se == NULL)
1248 se += nr + seq->anim_startofs;
1253 static int evaluate_seq_frame_gen(Sequence **seq_arr, ListBase *seqbase, int cfra, int chanshown)
1256 Sequence *effect_inputs[MAXSEQ + 1];
1257 int i, totseq = 0, num_effect_inputs = 0;
1259 memset(seq_arr, 0, sizeof(Sequence *) * (MAXSEQ + 1));
1261 seq = seqbase->first;
1263 if (seq->startdisp <= cfra && seq->enddisp > cfra) {
1264 if ((seq->type & SEQ_TYPE_EFFECT)) {
1266 effect_inputs[num_effect_inputs++] = seq->seq1;
1270 effect_inputs[num_effect_inputs++] = seq->seq2;
1274 effect_inputs[num_effect_inputs++] = seq->seq3;
1278 seq_arr[seq->machine] = seq;
1284 /* Drop strips which are used for effect inputs, we don't want
1285 * them to blend into render stack in any other way than effect
1288 for (i = 0; i < num_effect_inputs; i++) {
1289 seq = effect_inputs[i];
1290 /* It's possible that effetc strip would be placed to the same
1291 * 'machine' as it's inputs. We don't want to clear such strips
1294 if (seq_arr[seq->machine] && seq_arr[seq->machine]->type & SEQ_TYPE_EFFECT) {
1297 /* If we're shown a specified channel, then we want to see the stirps
1298 * which belongs to this machine.
1300 if (chanshown != 0 && chanshown <= seq->machine) {
1303 seq_arr[seq->machine] = NULL;
1309 int BKE_sequencer_evaluate_frame(Scene *scene, int cfra)
1311 Editing *ed = BKE_sequencer_editing_get(scene, false);
1312 Sequence *seq_arr[MAXSEQ + 1];
1317 return evaluate_seq_frame_gen(seq_arr, ed->seqbasep, cfra, 0);
1320 static bool video_seq_is_rendered(Sequence *seq)
1322 return (seq && !(seq->flag & SEQ_MUTE) && seq->type != SEQ_TYPE_SOUND_RAM);
1325 static int get_shown_sequences(ListBase *seqbasep, int cfra, int chanshown, Sequence **seq_arr_out)
1327 Sequence *seq_arr[MAXSEQ + 1];
1335 if (evaluate_seq_frame_gen(seq_arr, seqbasep, cfra, chanshown)) {
1339 for (; b > 0; b--) {
1340 if (video_seq_is_rendered(seq_arr[b])) {
1348 for (; b > 0; b--) {
1349 if (video_seq_is_rendered(seq_arr[b])) {
1350 if (seq_arr[b]->blend_mode == SEQ_BLEND_REPLACE) {
1356 for (; b <= chanshown && b >= 0; b++) {
1357 if (video_seq_is_rendered(seq_arr[b])) {
1358 seq_arr_out[cnt++] = seq_arr[b];
1365 /*********************** proxy management *************************/
1367 typedef struct SeqIndexBuildContext {
1368 struct IndexBuildContext *index_context;
1378 Sequence *seq, *orig_seq;
1379 } SeqIndexBuildContext;
1381 #define PROXY_MAXFILE (2 * FILE_MAXDIR + FILE_MAXFILE)
1383 static IMB_Proxy_Size seq_rendersize_to_proxysize(int size)
1386 return IMB_PROXY_NONE;
1389 return IMB_PROXY_100;
1392 return IMB_PROXY_75;
1395 return IMB_PROXY_50;
1397 return IMB_PROXY_25;
1400 static double seq_rendersize_to_scale_factor(int size)
1414 /* the number of files will vary according to the stereo format */
1415 static size_t seq_num_files(Scene *scene, char views_format, const bool is_multiview)
1417 if (!is_multiview) {
1420 else if (views_format == R_IMF_VIEWS_STEREO_3D) {
1423 /* R_IMF_VIEWS_INDIVIDUAL */
1425 return BKE_scene_multiview_num_views_get(&scene->r);
1429 static void seq_proxy_index_dir_set(struct anim *anim, const char *base_dir)
1432 char fname[FILE_MAXFILE];
1434 IMB_anim_get_fname(anim, fname, FILE_MAXFILE);
1435 BLI_strncpy(dir, base_dir, sizeof(dir));
1436 BLI_path_append(dir, sizeof(dir), fname);
1437 IMB_anim_set_index_dir(anim, dir);
1440 static void seq_open_anim_file(Scene *scene, Sequence *seq, bool openfile)
1443 char name[FILE_MAX];
1446 bool is_multiview_loaded = false;
1447 Editing *ed = scene->ed;
1448 const bool is_multiview = (seq->flag & SEQ_USE_VIEWS) != 0 && (scene->r.scemode & R_MULTIVIEW) != 0;
1450 if ((seq->anims.first != NULL) && (((StripAnim *)seq->anims.first)->anim != NULL)) {
1454 /* reset all the previously created anims */
1455 BKE_sequence_free_anim(seq);
1457 BLI_join_dirfile(name, sizeof(name),
1458 seq->strip->dir, seq->strip->stripdata->name);
1459 BLI_path_abs(name, G.main->name);
1461 proxy = seq->strip->proxy;
1463 use_proxy = proxy && ((proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_DIR) != 0 ||
1464 (ed->proxy_storage == SEQ_EDIT_PROXY_DIR_STORAGE));
1467 if (ed->proxy_storage == SEQ_EDIT_PROXY_DIR_STORAGE) {
1468 if (ed->proxy_dir[0] == 0)
1469 BLI_strncpy(dir, "//BL_proxy", sizeof(dir));
1471 BLI_strncpy(dir, ed->proxy_dir, sizeof(dir));
1474 BLI_strncpy(dir, seq->strip->proxy->dir, sizeof(dir));
1476 BLI_path_abs(dir, G.main->name);
1479 if (is_multiview && seq->views_format == R_IMF_VIEWS_INDIVIDUAL) {
1480 size_t totfiles = seq_num_files(scene, seq->views_format, true);
1481 char prefix[FILE_MAX];
1485 BKE_scene_multiview_view_prefix_get(scene, name, prefix, &ext);
1487 if (prefix[0] != '\0') {
1488 for (i = 0; i < totfiles; i++) {
1489 const char *suffix = BKE_scene_multiview_view_id_suffix_get(&scene->r, i);
1491 StripAnim *sanim = MEM_mallocN(sizeof(StripAnim), "Strip Anim");
1493 BLI_addtail(&seq->anims, sanim);
1495 BLI_snprintf(str, sizeof(str), "%s%s%s", prefix, suffix, ext);
1498 sanim->anim = openanim(
1499 str, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
1500 seq->streamindex, seq->strip->colorspace_settings.name);
1503 sanim->anim = openanim_noload(
1504 str, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
1505 seq->streamindex, seq->strip->colorspace_settings.name);
1510 seq_anim_add_suffix(scene, sanim->anim, i);
1512 /* we already have the suffix */
1513 IMB_suffix_anim(sanim->anim, suffix);
1518 sanim->anim = openanim(
1519 name, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
1520 seq->streamindex, seq->strip->colorspace_settings.name);
1523 sanim->anim = openanim_noload(
1524 name, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
1525 seq->streamindex, seq->strip->colorspace_settings.name);
1528 /* no individual view files - monoscopic, stereo 3d or exr multiview */
1532 if (sanim->anim && use_proxy) {
1533 seq_proxy_index_dir_set(sanim->anim, dir);
1536 is_multiview_loaded = true;
1540 if (is_multiview_loaded == false) {
1543 sanim = MEM_mallocN(sizeof(StripAnim), "Strip Anim");
1544 BLI_addtail(&seq->anims, sanim);
1547 sanim->anim = openanim(
1548 name, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
1549 seq->streamindex, seq->strip->colorspace_settings.name);
1552 sanim->anim = openanim_noload(
1553 name, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
1554 seq->streamindex, seq->strip->colorspace_settings.name);
1557 if (sanim->anim && use_proxy) {
1558 seq_proxy_index_dir_set(sanim->anim, dir);
1563 static bool seq_proxy_get_fname(Editing *ed, Sequence *seq, int cfra, int render_size, char *name, const size_t view_id)
1566 char dir[PROXY_MAXFILE];
1568 char suffix[24] = {'\0'};
1570 StripProxy *proxy = seq->strip->proxy;
1575 /* MOVIE tracks (only exception: custom files) are now handled
1576 * internally by ImBuf module for various reasons: proper time code
1577 * support, quicker index build, using one file instead
1578 * of a full directory of jpeg files, etc. Trying to support old
1579 * and new method at once could lead to funny effects, if people
1580 * have both, a directory full of jpeg files and proxy avis, so
1581 * sorry folks, please rebuild your proxies... */
1583 sanim = BLI_findlink(&seq->anims, view_id);
1585 if (ed->proxy_storage == SEQ_EDIT_PROXY_DIR_STORAGE) {
1586 char fname[FILE_MAXFILE];
1587 if (ed->proxy_dir[0] == 0)
1588 BLI_strncpy(dir, "//BL_proxy", sizeof(dir));
1590 BLI_strncpy(dir, ed->proxy_dir, sizeof(dir));
1592 if (sanim && sanim->anim) {
1593 IMB_anim_get_fname(sanim->anim, fname, FILE_MAXFILE);
1595 else if (seq->type == SEQ_TYPE_IMAGE) {
1598 BLI_path_append(dir, sizeof(dir), fname);
1599 BLI_path_abs(name, G.main->name);
1601 else if ((proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_DIR) && (proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_FILE)) {
1602 BLI_strncpy(dir, seq->strip->proxy->dir, sizeof(dir));
1604 else if (sanim && sanim->anim && (proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_DIR)) {
1605 char fname[FILE_MAXFILE];
1606 BLI_strncpy(dir, seq->strip->proxy->dir, sizeof(dir));
1607 IMB_anim_get_fname(sanim->anim, fname, FILE_MAXFILE);
1608 BLI_path_append(dir, sizeof(dir), fname);
1610 else if (seq->type == SEQ_TYPE_IMAGE) {
1611 BLI_snprintf(dir, PROXY_MAXFILE, "%s/BL_proxy", seq->strip->dir);
1618 BLI_snprintf(suffix, sizeof(suffix), "_%zu", view_id);
1620 if (proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_FILE && sanim && sanim->anim &&
1621 ed->proxy_storage != SEQ_EDIT_PROXY_DIR_STORAGE)
1623 BLI_join_dirfile(name, PROXY_MAXFILE,
1625 BLI_path_abs(name, G.main->name);
1626 BLI_snprintf(name, PROXY_MAXFILE, "%s_%s", name, suffix);
1631 /* generate a separate proxy directory for each preview size */
1633 if (seq->type == SEQ_TYPE_IMAGE) {
1634 BLI_snprintf(name, PROXY_MAXFILE, "%s/images/%d/%s_proxy%s", dir, render_size,
1635 BKE_sequencer_give_stripelem(seq, cfra)->name, suffix);
1639 frameno = (int)give_stripelem_index(seq, cfra) + seq->anim_startofs;
1640 BLI_snprintf(name, PROXY_MAXFILE, "%s/proxy_misc/%d/####%s", dir, render_size, suffix);
1643 BLI_path_abs(name, G.main->name);
1644 BLI_path_frame(name, frameno, 0);
1646 strcat(name, ".jpg");
1651 static ImBuf *seq_proxy_fetch(const SeqRenderData *context, Sequence *seq, int cfra)
1653 char name[PROXY_MAXFILE];
1654 IMB_Proxy_Size psize = seq_rendersize_to_proxysize(context->preview_render_size);
1656 int render_size = context->preview_render_size;
1657 StripProxy *proxy = seq->strip->proxy;
1658 Editing *ed = context->scene->ed;
1661 if (!(seq->flag & SEQ_USE_PROXY)) {
1665 /* dirty hack to distinguish 100% render size from PROXY_100 */
1666 if (render_size == 99) {
1670 size_flags = proxy->build_size_flags;
1672 /* only use proxies, if they are enabled (even if present!) */
1673 if (psize == IMB_PROXY_NONE || ((size_flags & psize) != psize)) {
1677 if (proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_FILE) {
1678 int frameno = (int)give_stripelem_index(seq, cfra) + seq->anim_startofs;
1679 if (proxy->anim == NULL) {
1680 if (seq_proxy_get_fname(ed, seq, cfra, render_size, name, context->view_id) == 0) {
1684 proxy->anim = openanim(name, IB_rect, 0, seq->strip->colorspace_settings.name);
1686 if (proxy->anim == NULL) {
1690 seq_open_anim_file(context->scene, seq, true);
1691 sanim = seq->anims.first;
1693 frameno = IMB_anim_index_get_frame_index(sanim ? sanim->anim : NULL, seq->strip->proxy->tc, frameno);
1695 return IMB_anim_absolute(proxy->anim, frameno, IMB_TC_NONE, IMB_PROXY_NONE);
1698 if (seq_proxy_get_fname(ed, seq, cfra, render_size, name, context->view_id) == 0) {
1702 if (BLI_exists(name)) {
1703 ImBuf *ibuf = IMB_loadiffname(name, IB_rect, NULL);
1706 sequencer_imbuf_assign_spaces(context->scene, ibuf);
1715 static void seq_proxy_build_frame(const SeqRenderData *context, Sequence *seq, int cfra,
1716 int proxy_render_size, const bool overwrite)
1718 char name[PROXY_MAXFILE];
1722 ImBuf *ibuf_tmp, *ibuf;
1723 Editing *ed = context->scene->ed;
1725 if (!seq_proxy_get_fname(ed, seq, cfra, proxy_render_size, name, context->view_id)) {
1729 if (!overwrite && BLI_exists(name)) {
1733 ibuf_tmp = seq_render_strip(context, seq, cfra);
1735 rectx = (proxy_render_size * ibuf_tmp->x) / 100;
1736 recty = (proxy_render_size * ibuf_tmp->y) / 100;
1738 if (ibuf_tmp->x != rectx || ibuf_tmp->y != recty) {
1739 ibuf = IMB_dupImBuf(ibuf_tmp);
1740 IMB_freeImBuf(ibuf_tmp);
1741 IMB_scalefastImBuf(ibuf, (short)rectx, (short)recty);
1747 /* depth = 32 is intentionally left in, otherwise ALPHA channels
1749 quality = seq->strip->proxy->quality;
1750 ibuf->ftype = JPG | quality;
1752 /* unsupported feature only confuses other s/w */
1753 if (ibuf->planes == 32)
1756 BLI_make_existing_file(name);
1758 ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
1763 IMB_freeImBuf(ibuf);
1766 /* returns whether the file this context would read from even exist, if not, don't create the context
1768 static bool seq_proxy_multiview_context_invalid(Sequence *seq, Scene *scene, const size_t view_id)
1770 if ((scene->r.scemode & R_MULTIVIEW) == 0)
1773 if ((seq->type == SEQ_TYPE_IMAGE) && (seq->views_format == R_IMF_VIEWS_INDIVIDUAL)) {
1774 static char prefix[FILE_MAX];
1775 static char *ext = NULL;
1779 char path[FILE_MAX];
1780 BLI_join_dirfile(path, sizeof(path), seq->strip->dir,
1781 seq->strip->stripdata->name);
1782 BLI_path_abs(path, G.main->name);
1783 BKE_scene_multiview_view_prefix_get(scene, path, prefix, &ext);
1789 if (prefix[0] == '\0')
1790 return view_id != 0;
1792 seq_multiview_name(scene, view_id, prefix, ext, str, FILE_MAX);
1794 if (BLI_access(str, R_OK) == 0)
1797 return view_id != 0;
1802 /** This returns the maximum possible number of required contexts
1804 static size_t seq_proxy_context_count(Sequence *seq, Scene *scene)
1806 size_t num_views = 1;
1808 if ((scene->r.scemode & R_MULTIVIEW) == 0)
1811 switch (seq->type) {
1812 case SEQ_TYPE_MOVIE:
1814 num_views = BLI_listbase_count(&seq->anims);
1817 case SEQ_TYPE_IMAGE:
1819 switch (seq->views_format) {
1820 case R_IMF_VIEWS_INDIVIDUAL:
1821 num_views = BKE_scene_multiview_num_views_get(&scene->r);
1823 case R_IMF_VIEWS_STEREO_3D:
1826 case R_IMF_VIEWS_MULTIVIEW:
1827 /* not supported at the moment */
1839 void BKE_sequencer_proxy_rebuild_context(Main *bmain, Scene *scene, Sequence *seq, struct GSet *file_list, ListBase *queue)
1841 SeqIndexBuildContext *context;
1847 if (!seq->strip || !seq->strip->proxy) {
1851 if (!(seq->flag & SEQ_USE_PROXY)) {
1855 num_files = seq_proxy_context_count(seq, scene);
1857 for (i = 0; i < num_files; i++) {
1858 if (seq_proxy_multiview_context_invalid(seq, scene, i))
1861 context = MEM_callocN(sizeof(SeqIndexBuildContext), "seq proxy rebuild context");
1863 nseq = BKE_sequence_dupli_recursive(scene, scene, seq, 0);
1865 context->tc_flags = nseq->strip->proxy->build_tc_flags;
1866 context->size_flags = nseq->strip->proxy->build_size_flags;
1867 context->quality = nseq->strip->proxy->quality;
1868 context->overwrite = (nseq->strip->proxy->build_flags & SEQ_PROXY_SKIP_EXISTING) == 0;
1870 context->bmain = bmain;
1871 context->scene = scene;
1872 context->orig_seq = seq;
1873 context->seq = nseq;
1875 context->view_id = i; /* only for images */
1877 link = BLI_genericNodeN(context);
1878 BLI_addtail(queue, link);
1880 if (nseq->type == SEQ_TYPE_MOVIE) {
1883 seq_open_anim_file(scene, nseq, true);
1884 sanim = BLI_findlink(&nseq->anims, i);
1887 context->index_context = IMB_anim_index_rebuild_context(sanim->anim,
1888 context->tc_flags, context->size_flags, context->quality,
1889 context->overwrite, file_list);
1895 void BKE_sequencer_proxy_rebuild(SeqIndexBuildContext *context, short *stop, short *do_update, float *progress)
1897 const bool overwrite = context->overwrite;
1898 SeqRenderData render_context;
1899 Sequence *seq = context->seq;
1900 Scene *scene = context->scene;
1901 Main *bmain = context->bmain;
1904 if (seq->type == SEQ_TYPE_MOVIE) {
1905 if (context->index_context) {
1906 IMB_anim_index_rebuild(context->index_context, stop, do_update, progress);
1912 if (!(seq->flag & SEQ_USE_PROXY)) {
1916 /* that's why it is called custom... */
1917 if (seq->strip->proxy && seq->strip->proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_FILE) {
1921 /* fail safe code */
1923 BKE_sequencer_new_render_data(
1924 bmain->eval_ctx, bmain, context->scene,
1925 (scene->r.size * (float) scene->r.xsch) / 100.0f + 0.5f,
1926 (scene->r.size * (float) scene->r.ysch) / 100.0f + 0.5f, 100,
1929 render_context.skip_cache = true;
1930 render_context.is_proxy_render = true;
1931 render_context.view_id = context->view_id;
1933 for (cfra = seq->startdisp + seq->startstill; cfra < seq->enddisp - seq->endstill; cfra++) {
1934 if (context->size_flags & IMB_PROXY_25) {
1935 seq_proxy_build_frame(&render_context, seq, cfra, 25, overwrite);
1937 if (context->size_flags & IMB_PROXY_50) {
1938 seq_proxy_build_frame(&render_context, seq, cfra, 50, overwrite);
1940 if (context->size_flags & IMB_PROXY_75) {
1941 seq_proxy_build_frame(&render_context, seq, cfra, 75, overwrite);
1943 if (context->size_flags & IMB_PROXY_100) {
1944 seq_proxy_build_frame(&render_context, seq, cfra, 100, overwrite);
1947 *progress = (float) (cfra - seq->startdisp - seq->startstill) / (seq->enddisp - seq->endstill - seq->startdisp - seq->startstill);
1950 if (*stop || G.is_break)
1955 void BKE_sequencer_proxy_rebuild_finish(SeqIndexBuildContext *context, bool stop)
1957 if (context->index_context) {
1960 for (sanim = context->seq->anims.first; sanim; sanim = sanim->next)
1961 IMB_close_anim_proxies(sanim->anim);
1963 for (sanim = context->orig_seq->anims.first; sanim; sanim = sanim->next)
1964 IMB_close_anim_proxies(sanim->anim);
1966 IMB_anim_index_rebuild_finish(context->index_context, stop);
1969 seq_free_sequence_recurse(NULL, context->seq);
1974 void BKE_sequencer_proxy_set(struct Sequence *seq, bool value)
1977 seq->flag |= SEQ_USE_PROXY;
1978 if (seq->strip->proxy == NULL) {
1979 seq->strip->proxy = MEM_callocN(sizeof(struct StripProxy), "StripProxy");
1980 seq->strip->proxy->quality = 90;
1981 seq->strip->proxy->build_tc_flags = SEQ_PROXY_TC_ALL;
1982 seq->strip->proxy->build_size_flags = SEQ_PROXY_IMAGE_SIZE_25;
1986 seq->flag ^= SEQ_USE_PROXY;
1990 /*********************** color balance *************************/
1992 static StripColorBalance calc_cb(StripColorBalance *cb_)
1994 StripColorBalance cb = *cb_;
1997 for (c = 0; c < 3; c++) {
1998 cb.lift[c] = 2.0f - cb.lift[c];
2001 if (cb.flag & SEQ_COLOR_BALANCE_INVERSE_LIFT) {
2002 for (c = 0; c < 3; c++) {
2003 /* tweak to give more subtle results
2004 * values above 1.0 are scaled */
2005 if (cb.lift[c] > 1.0f)
2006 cb.lift[c] = pow(cb.lift[c] - 1.0f, 2.0) + 1.0;
2008 cb.lift[c] = 2.0f - cb.lift[c];
2012 if (cb.flag & SEQ_COLOR_BALANCE_INVERSE_GAIN) {
2013 for (c = 0; c < 3; c++) {
2014 if (cb.gain[c] != 0.0f) {
2015 cb.gain[c] = 1.0f / cb.gain[c];
2018 cb.gain[c] = 1000000; /* should be enough :) */
2023 if (!(cb.flag & SEQ_COLOR_BALANCE_INVERSE_GAMMA)) {
2024 for (c = 0; c < 3; c++) {
2025 if (cb.gamma[c] != 0.0f) {
2026 cb.gamma[c] = 1.0f / cb.gamma[c];
2029 cb.gamma[c] = 1000000; /* should be enough :) */
2037 /* note: lift is actually 2-lift */
2038 MINLINE float color_balance_fl(float in, const float lift, const float gain, const float gamma, const float mul)
2040 float x = (((in - 1.0f) * lift) + 1.0f) * gain;
2046 return powf(x, gamma) * mul;
2049 static void make_cb_table_float(float lift, float gain, float gamma,
2050 float *table, float mul)
2054 for (y = 0; y < 256; y++) {
2055 float v = color_balance_fl((float)y * (1.0f / 255.0f), lift, gain, gamma, mul);
2061 static void color_balance_byte_byte(StripColorBalance *cb_, unsigned char *rect, unsigned char *mask_rect, int width, int height, float mul)
2063 //unsigned char cb_tab[3][256];
2064 unsigned char *cp = rect;
2065 unsigned char *e = cp + width * 4 * height;
2066 unsigned char *m = mask_rect;
2068 StripColorBalance cb = calc_cb(cb_);
2074 straight_uchar_to_premul_float(p, cp);
2076 for (c = 0; c < 3; c++) {
2077 float t = color_balance_fl(p[c], cb.lift[c], cb.gain[c], cb.gamma[c], mul);
2080 float m_normal = (float) m[c] / 255.0f;
2082 p[c] = p[c] * (1.0f - m_normal) + t * m_normal;
2088 premul_float_to_straight_uchar(cp, p);
2096 static void color_balance_byte_float(StripColorBalance *cb_, unsigned char *rect, float *rect_float, unsigned char *mask_rect, int width, int height, float mul)
2098 float cb_tab[4][256];
2100 unsigned char *p = rect;
2101 unsigned char *e = p + width * 4 * height;
2102 unsigned char *m = mask_rect;
2104 StripColorBalance cb;
2110 for (c = 0; c < 3; c++) {
2111 make_cb_table_float(cb.lift[c], cb.gain[c], cb.gamma[c], cb_tab[c], mul);
2114 for (i = 0; i < 256; i++) {
2115 cb_tab[3][i] = ((float)i) * (1.0f / 255.0f);
2120 float t[3] = {m[0] / 255.0f, m[1] / 255.0f, m[2] / 255.0f};
2122 p[0] = p[0] * (1.0f - t[0]) + t[0] * cb_tab[0][p[0]];
2123 p[1] = p[1] * (1.0f - t[1]) + t[1] * cb_tab[1][p[1]];
2124 p[2] = p[2] * (1.0f - t[2]) + t[2] * cb_tab[2][p[2]];
2129 o[0] = cb_tab[0][p[0]];
2130 o[1] = cb_tab[1][p[1]];
2131 o[2] = cb_tab[2][p[2]];
2134 o[3] = cb_tab[3][p[3]];
2140 static void color_balance_float_float(StripColorBalance *cb_, float *rect_float, float *mask_rect_float, int width, int height, float mul)
2142 float *p = rect_float;
2143 const float *e = rect_float + width * 4 * height;
2144 const float *m = mask_rect_float;
2145 StripColorBalance cb = calc_cb(cb_);
2149 for (c = 0; c < 3; c++) {
2150 float t = color_balance_fl(p[c], cb.lift[c], cb.gain[c], cb.gamma[c], mul);
2153 p[c] = p[c] * (1.0f - m[c]) + t * m[c];
2164 typedef struct ColorBalanceInitData {
2165 StripColorBalance *cb;
2170 } ColorBalanceInitData;
2172 typedef struct ColorBalanceThread {
2173 StripColorBalance *cb;
2178 unsigned char *rect, *mask_rect;
2179 float *rect_float, *mask_rect_float;
2182 } ColorBalanceThread;
2184 static void color_balance_init_handle(void *handle_v, int start_line, int tot_line, void *init_data_v)
2186 ColorBalanceThread *handle = (ColorBalanceThread *) handle_v;
2187 ColorBalanceInitData *init_data = (ColorBalanceInitData *) init_data_v;
2188 ImBuf *ibuf = init_data->ibuf;
2189 ImBuf *mask = init_data->mask;
2191 int offset = 4 * start_line * ibuf->x;
2193 memset(handle, 0, sizeof(ColorBalanceThread));
2195 handle->cb = init_data->cb;
2196 handle->mul = init_data->mul;
2197 handle->width = ibuf->x;
2198 handle->height = tot_line;
2199 handle->make_float = init_data->make_float;
2202 handle->rect = (unsigned char *) ibuf->rect + offset;
2204 if (ibuf->rect_float)
2205 handle->rect_float = ibuf->rect_float + offset;
2209 handle->mask_rect = (unsigned char *) mask->rect + offset;
2211 if (mask->rect_float)
2212 handle->mask_rect_float = mask->rect_float + offset;
2215 handle->mask_rect = NULL;
2216 handle->mask_rect_float = NULL;
2220 static void *color_balance_do_thread(void *thread_data_v)
2222 ColorBalanceThread *thread_data = (ColorBalanceThread *) thread_data_v;
2223 StripColorBalance *cb = thread_data->cb;
2224 int width = thread_data->width, height = thread_data->height;
2225 unsigned char *rect = thread_data->rect;
2226 unsigned char *mask_rect = thread_data->mask_rect;
2227 float *rect_float = thread_data->rect_float;
2228 float *mask_rect_float = thread_data->mask_rect_float;
2229 float mul = thread_data->mul;
2232 color_balance_float_float(cb, rect_float, mask_rect_float, width, height, mul);
2234 else if (thread_data->make_float) {
2235 color_balance_byte_float(cb, rect, rect_float, mask_rect, width, height, mul);
2238 color_balance_byte_byte(cb, rect, mask_rect, width, height, mul);
2244 /* cfra is offset by fra_offset only in case we are using a real mask. */
2245 ImBuf *BKE_sequencer_render_mask_input(
2246 const SeqRenderData *context, int mask_input_type, Sequence *mask_sequence, Mask *mask_id,
2247 int cfra, int fra_offset, bool make_float)
2249 ImBuf *mask_input = NULL;
2251 if (mask_input_type == SEQUENCE_MASK_INPUT_STRIP) {
2252 if (mask_sequence) {
2253 mask_input = seq_render_strip(context, mask_sequence, cfra);
2256 if (!mask_input->rect_float)
2257 IMB_float_from_rect(mask_input);
2260 if (!mask_input->rect)
2261 IMB_rect_from_float(mask_input);
2265 else if (mask_input_type == SEQUENCE_MASK_INPUT_ID) {
2266 mask_input = seq_render_mask(context, mask_id, cfra - fra_offset, make_float);
2272 void BKE_sequencer_color_balance_apply(StripColorBalance *cb, ImBuf *ibuf, float mul, bool make_float, ImBuf *mask_input)
2274 ColorBalanceInitData init_data;
2276 if (!ibuf->rect_float && make_float)
2277 imb_addrectfloatImBuf(ibuf);
2280 init_data.ibuf = ibuf;
2281 init_data.mul = mul;
2282 init_data.make_float = make_float;
2283 init_data.mask = mask_input;
2285 IMB_processor_apply_threaded(ibuf->y, sizeof(ColorBalanceThread), &init_data,
2286 color_balance_init_handle, color_balance_do_thread);
2288 /* color balance either happens on float buffer or byte buffer, but never on both,
2289 * free byte buffer if there's float buffer since float buffer would be used for
2290 * color balance in favor of byte buffer
2292 if (ibuf->rect_float && ibuf->rect)
2293 imb_freerectImBuf(ibuf);
2297 * input preprocessing for SEQ_TYPE_IMAGE, SEQ_TYPE_MOVIE, SEQ_TYPE_MOVIECLIP and SEQ_TYPE_SCENE
2299 * Do all the things you can't really do afterwards using sequence effects
2300 * (read: before rescaling to render resolution has been done)
2302 * Order is important!
2305 * - Crop and transform in image source coordinate space
2306 * - Flip X + Flip Y (could be done afterwards, backward compatibility)
2307 * - Promote image to float data (affects pipeline operations afterwards)
2308 * - Color balance (is most efficient in the byte -> float
2309 * (future: half -> float should also work fine!)
2310 * case, if done on load, since we can use lookup tables)
2314 bool BKE_sequencer_input_have_to_preprocess(const SeqRenderData *context, Sequence *seq, float UNUSED(cfra))
2318 if (context->is_proxy_render) {
2322 if (seq->flag & (SEQ_FILTERY | SEQ_USE_CROP | SEQ_USE_TRANSFORM | SEQ_FLIPX | SEQ_FLIPY | SEQ_MAKE_FLOAT)) {
2328 if (seq->blend_mode == SEQ_BLEND_REPLACE) {
2329 mul *= seq->blend_opacity / 100.0f;
2336 if (seq->sat != 1.0f) {
2340 if (seq->modifiers.first) {
2347 static ImBuf *input_preprocess(const SeqRenderData *context, Sequence *seq, float cfra, ImBuf *ibuf,
2348 const bool is_proxy_image, const bool is_preprocessed)
2350 Scene *scene = context->scene;
2353 ibuf = IMB_makeSingleUser(ibuf);
2355 if ((seq->flag & SEQ_FILTERY) &&
2356 !ELEM(seq->type, SEQ_TYPE_MOVIE, SEQ_TYPE_MOVIECLIP))
2361 if (seq->flag & (SEQ_USE_CROP | SEQ_USE_TRANSFORM)) {
2363 StripTransform t = {0};
2366 if (is_proxy_image) {
2367 double f = seq_rendersize_to_scale_factor(context->preview_render_size);
2370 IMB_scalefastImBuf(ibuf, ibuf->x / f, ibuf->y / f);
2374 if (seq->flag & SEQ_USE_CROP && seq->strip->crop) {
2375 c = *seq->strip->crop;
2377 if (seq->flag & SEQ_USE_TRANSFORM && seq->strip->transform) {
2378 t = *seq->strip->transform;
2381 if (is_preprocessed) {
2382 double xscale = scene->r.xsch ? ((double)context->rectx / (double)scene->r.xsch) : 1.0;
2383 double yscale = scene->r.ysch ? ((double)context->recty / (double)scene->r.ysch) : 1.0;
2384 if (seq->flag & SEQ_USE_TRANSFORM) {
2388 if (seq->flag & SEQ_USE_CROP) {
2396 sx = ibuf->x - c.left - c.right;
2397 sy = ibuf->y - c.top - c.bottom;
2399 if (seq->flag & SEQ_USE_TRANSFORM) {
2400 if (is_preprocessed) {
2401 dx = context->rectx;
2402 dy = context->recty;
2414 if (c.top + c.bottom >= ibuf->y ||
2415 c.left + c.right >= ibuf->x ||
2416 t.xofs >= dx || t.yofs >= dy)
2418 make_black_ibuf(ibuf);
2421 ImBuf *i = IMB_allocImBuf(dx, dy, 32, ibuf->rect_float ? IB_rectfloat : IB_rect);
2423 IMB_rectcpy(i, ibuf, t.xofs, t.yofs, c.left, c.bottom, sx, sy);
2424 sequencer_imbuf_assign_spaces(scene, i);
2426 IMB_freeImBuf(ibuf);
2432 if (seq->flag & SEQ_FLIPX) {
2436 if (seq->flag & SEQ_FLIPY) {
2440 if (seq->sat != 1.0f) {
2441 IMB_saturation(ibuf, seq->sat);
2446 if (seq->blend_mode == SEQ_BLEND_REPLACE) {
2447 mul *= seq->blend_opacity / 100.0f;
2450 if (seq->flag & SEQ_MAKE_FLOAT) {
2451 if (!ibuf->rect_float) {
2452 BKE_sequencer_imbuf_to_sequencer_space(scene, ibuf, true);
2456 imb_freerectImBuf(ibuf);
2461 multibuf(ibuf, mul);
2464 if (ibuf->x != context->rectx || ibuf->y != context->recty) {
2465 if (scene->r.mode & R_OSA) {
2466 IMB_scaleImBuf(ibuf, (short)context->rectx, (short)context->recty);
2469 IMB_scalefastImBuf(ibuf, (short)context->rectx, (short)context->recty);
2473 if (seq->modifiers.first) {
2474 ImBuf *ibuf_new = BKE_sequence_modifier_apply_stack(context, seq, ibuf, cfra);
2476 if (ibuf_new != ibuf) {
2477 IMB_freeImBuf(ibuf);
2485 static ImBuf *copy_from_ibuf_still(const SeqRenderData *context, Sequence *seq, float nr)
2491 ibuf = BKE_sequencer_cache_get(context, seq, seq->start, SEQ_STRIPELEM_IBUF_STARTSTILL);
2493 else if (nr == seq->len - 1) {
2494 ibuf = BKE_sequencer_cache_get(context, seq, seq->start, SEQ_STRIPELEM_IBUF_ENDSTILL);
2498 rval = IMB_dupImBuf(ibuf);
2499 IMB_freeImBuf(ibuf);
2505 static void copy_to_ibuf_still(const SeqRenderData *context, Sequence *seq, float nr, ImBuf *ibuf)
2507 /* warning: ibuf may be NULL if the video fails to load */
2508 if (nr == 0 || nr == seq->len - 1) {
2509 /* we have to store a copy, since the passed ibuf
2510 * could be preprocessed afterwards (thereby silently
2511 * changing the cached image... */
2512 ibuf = IMB_dupImBuf(ibuf);
2515 sequencer_imbuf_assign_spaces(context->scene, ibuf);
2519 BKE_sequencer_cache_put(context, seq, seq->start, SEQ_STRIPELEM_IBUF_STARTSTILL, ibuf);
2522 if (nr == seq->len - 1) {
2523 BKE_sequencer_cache_put(context, seq, seq->start, SEQ_STRIPELEM_IBUF_ENDSTILL, ibuf);
2526 IMB_freeImBuf(ibuf);
2530 /*********************** strip rendering functions *************************/
2532 typedef struct RenderEffectInitData {
2533 struct SeqEffectHandle *sh;
2534 const SeqRenderData *context;
2536 float cfra, facf0, facf1;
2537 ImBuf *ibuf1, *ibuf2, *ibuf3;
2540 } RenderEffectInitData;
2542 typedef struct RenderEffectThread {
2543 struct SeqEffectHandle *sh;
2544 const SeqRenderData *context;
2546 float cfra, facf0, facf1;
2547 ImBuf *ibuf1, *ibuf2, *ibuf3;
2550 int start_line, tot_line;
2551 } RenderEffectThread;
2553 static void render_effect_execute_init_handle(void *handle_v, int start_line, int tot_line, void *init_data_v)
2555 RenderEffectThread *handle = (RenderEffectThread *) handle_v;
2556 RenderEffectInitData *init_data = (RenderEffectInitData *) init_data_v;
2558 handle->sh = init_data->sh;
2559 handle->context = init_data->context;
2560 handle->seq = init_data->seq;
2561 handle->cfra = init_data->cfra;
2562 handle->facf0 = init_data->facf0;
2563 handle->facf1 = init_data->facf1;
2564 handle->ibuf1 = init_data->ibuf1;
2565 handle->ibuf2 = init_data->ibuf2;
2566 handle->ibuf3 = init_data->ibuf3;
2567 handle->out = init_data->out;
2569 handle->start_line = start_line;
2570 handle->tot_line = tot_line;
2573 static void *render_effect_execute_do_thread(void *thread_data_v)
2575 RenderEffectThread *thread_data = (RenderEffectThread *) thread_data_v;
2577 thread_data->sh->execute_slice(thread_data->context, thread_data->seq, thread_data->cfra,
2578 thread_data->facf0, thread_data->facf1, thread_data->ibuf1,
2579 thread_data->ibuf2, thread_data->ibuf3, thread_data->start_line,
2580 thread_data->tot_line, thread_data->out);
2585 static ImBuf *seq_render_effect_execute_threaded(struct SeqEffectHandle *sh, const SeqRenderData *context, Sequence *seq,
2586 float cfra, float facf0, float facf1,
2587 ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *ibuf3)
2589 RenderEffectInitData init_data;
2590 ImBuf *out = sh->init_execution(context, ibuf1, ibuf2, ibuf3);
2593 init_data.context = context;
2594 init_data.seq = seq;
2595 init_data.cfra = cfra;
2596 init_data.facf0 = facf0;
2597 init_data.facf1 = facf1;
2598 init_data.ibuf1 = ibuf1;
2599 init_data.ibuf2 = ibuf2;
2600 init_data.ibuf3 = ibuf3;
2601 init_data.out = out;
2603 IMB_processor_apply_threaded(out->y, sizeof(RenderEffectThread), &init_data,
2604 render_effect_execute_init_handle, render_effect_execute_do_thread);
2609 static ImBuf *seq_render_effect_strip_impl(const SeqRenderData *context, Sequence *seq, float cfra)
2611 Scene *scene = context->scene;
2615 struct SeqEffectHandle sh = BKE_sequence_get_effect(seq);
2621 ibuf[0] = ibuf[1] = ibuf[2] = NULL;
2623 input[0] = seq->seq1; input[1] = seq->seq2; input[2] = seq->seq3;
2625 if (!sh.execute && !(sh.execute_slice && sh.init_execution)) {
2626 /* effect not supported in this version... */
2627 out = IMB_allocImBuf(context->rectx, context->recty, 32, IB_rect);
2631 if (seq->flag & SEQ_USE_EFFECT_DEFAULT_FADE) {
2632 sh.get_default_fac(seq, cfra, &fac, &facf);
2634 if ((scene->r.mode & R_FIELDS) == 0)
2638 fcu = id_data_find_fcurve(&scene->id, seq, &RNA_Sequence, "effect_fader", 0, NULL);
2640 fac = facf = evaluate_fcurve(fcu, cfra);
2641 if (scene->r.mode & R_FIELDS) {
2642 facf = evaluate_fcurve(fcu, cfra + 0.5f);
2646 fac = facf = seq->effect_fader;
2650 early_out = sh.early_out(seq, fac, facf);
2652 switch (early_out) {
2653 case EARLY_NO_INPUT:
2654 out = sh.execute(context, seq, cfra, fac, facf, NULL, NULL, NULL);
2656 case EARLY_DO_EFFECT:
2657 for (i = 0; i < 3; i++) {
2659 ibuf[i] = seq_render_strip(context, input[i], cfra);
2662 if (ibuf[0] && ibuf[1]) {
2663 if (sh.multithreaded)
2664 out = seq_render_effect_execute_threaded(&sh, context, seq, cfra, fac, facf, ibuf[0], ibuf[1], ibuf[2]);
2666 out = sh.execute(context, seq, cfra, fac, facf, ibuf[0], ibuf[1], ibuf[2]);
2669 case EARLY_USE_INPUT_1:
2671 ibuf[0] = seq_render_strip(context, input[0], cfra);
2674 if (BKE_sequencer_input_have_to_preprocess(context, seq, cfra)) {
2675 out = IMB_dupImBuf(ibuf[0]);
2683 case EARLY_USE_INPUT_2:
2685 ibuf[1] = seq_render_strip(context, input[1], cfra);
2688 if (BKE_sequencer_input_have_to_preprocess(context, seq, cfra)) {
2689 out = IMB_dupImBuf(ibuf[1]);
2699 for (i = 0; i < 3; i++) {
2700 IMB_freeImBuf(ibuf[i]);
2704 out = IMB_allocImBuf(context->rectx, context->recty, 32, IB_rect);
2710 static ImBuf *seq_render_image_strip(const SeqRenderData *context, Sequence *seq, float nr, float cfra)
2713 char name[FILE_MAX];
2714 bool is_multiview = (seq->flag & SEQ_USE_VIEWS) != 0 &&
2715 (context->scene->r.scemode & R_MULTIVIEW) != 0;
2716 StripElem *s_elem = BKE_sequencer_give_stripelem(seq, cfra);
2720 BLI_join_dirfile(name, sizeof(name), seq->strip->dir, s_elem->name);
2721 BLI_path_abs(name, G.main->name);
2725 if (seq->alpha_mode == SEQ_ALPHA_PREMUL)
2726 flag |= IB_alphamode_premul;
2729 /* don't do anything */
2731 else if (is_multiview) {
2732 size_t totfiles = seq_num_files(context->scene, seq->views_format, true);
2734 struct ImBuf **ibufs_arr;
2735 char prefix[FILE_MAX];
2740 BKE_scene_multiview_view_prefix_get(context->scene, name, prefix, &ext);
2741 if (prefix[0] == '\0') {
2742 goto monoview_image;
2749 totviews = BKE_scene_multiview_num_views_get(&context->scene->r);
2750 ibufs_arr = MEM_callocN(sizeof(ImBuf *) * totviews, "Sequence Image Views Imbufs");
2752 for (i = 0; i < totfiles; i++) {
2754 if (prefix[0] == '\0') {
2755 ibufs_arr[i] = IMB_loadiffname(name, flag, seq->strip->colorspace_settings.name);
2759 seq_multiview_name(context->scene, i, prefix, ext, str, FILE_MAX);
2760 ibufs_arr[i] = IMB_loadiffname(str, flag, seq->strip->colorspace_settings.name);
2764 /* we don't need both (speed reasons)! */
2765 if (ibufs_arr[i]->rect_float && ibufs_arr[i]->rect)
2766 imb_freerectImBuf(ibufs_arr[i]);
2770 if (seq->views_format == R_IMF_VIEWS_STEREO_3D && ibufs_arr[0])
2771 IMB_ImBufFromStereo3d(seq->stereo3d_format, ibufs_arr[0], &ibufs_arr[0], &ibufs_arr[1]);
2773 for (i = 0; i < totviews; i++) {
2775 SeqRenderData localcontext = *context;
2776 localcontext.view_id = i;
2778 /* all sequencer color is done in SRGB space, linear gives odd crossfades */
2779 BKE_sequencer_imbuf_to_sequencer_space(context->scene, ibufs_arr[i], false);
2781 if (i != context->view_id) {
2782 copy_to_ibuf_still(&localcontext, seq, nr, ibufs_arr[i]);
2783 BKE_sequencer_cache_put(&localcontext, seq, cfra, SEQ_STRIPELEM_IBUF, ibufs_arr[i]);
2788 /* return the original requested ImBuf */
2789 ibuf = ibufs_arr[context->view_id];
2791 s_elem->orig_width = ibufs_arr[0]->x;
2792 s_elem->orig_height = ibufs_arr[0]->y;
2795 /* "remove" the others (decrease their refcount) */
2796 for (i = 0; i < totviews; i++) {
2797 if (ibufs_arr[i] != ibuf) {
2798 IMB_freeImBuf(ibufs_arr[i]);
2802 MEM_freeN(ibufs_arr);
2806 if ((ibuf = IMB_loadiffname(name, flag, seq->strip->colorspace_settings.name))) {
2807 /* we don't need both (speed reasons)! */
2808 if (ibuf->rect_float && ibuf->rect)
2809 imb_freerectImBuf(ibuf);
2811 /* all sequencer color is done in SRGB space, linear gives odd crossfades */
2812 BKE_sequencer_imbuf_to_sequencer_space(context->scene, ibuf, false);
2814 s_elem->orig_width = ibuf->x;
2815 s_elem->orig_height = ibuf->y;
2822 static ImBuf *seq_render_movie_strip(const SeqRenderData *context, Sequence *seq, float nr, float cfra)
2826 bool is_multiview = (seq->flag & SEQ_USE_VIEWS) != 0 &&
2827 (context->scene->r.scemode & R_MULTIVIEW) != 0;
2829 /* load all the videos */
2830 seq_open_anim_file(context->scene, seq, false);
2835 size_t totfiles = seq_num_files(context->scene, seq->views_format, true);
2838 if (totfiles != BLI_listbase_count_ex(&seq->anims, totfiles + 1))
2839 goto monoview_movie;
2841 totviews = BKE_scene_multiview_num_views_get(&context->scene->r);
2842 ibuf_arr = MEM_callocN(sizeof(ImBuf *) * totviews, "Sequence Image Views Imbufs");
2844 for (i = 0, sanim = seq->anims.first; sanim; sanim = sanim->next, i++) {
2846 IMB_Proxy_Size proxy_size = seq_rendersize_to_proxysize(context->preview_render_size);
2847 IMB_anim_set_preseek(sanim->anim, seq->anim_preseek);
2849 ibuf_arr[i] = IMB_anim_absolute(sanim->anim, nr + seq->anim_startofs,
2850 seq->strip->proxy ? seq->strip->proxy->tc : IMB_TC_RECORD_RUN,
2853 /* fetching for requested proxy size failed, try fetching the original instead */
2854 if (!ibuf_arr[i] && proxy_size != IMB_PROXY_NONE) {
2855 ibuf_arr[i] = IMB_anim_absolute(sanim->anim, nr + seq->anim_startofs,
2856 seq->strip->proxy ? seq->strip->proxy->tc : IMB_TC_RECORD_RUN,
2860 /* we don't need both (speed reasons)! */
2861 if (ibuf_arr[i]->rect_float && ibuf_arr[i]->rect)
2862 imb_freerectImBuf(ibuf_arr[i]);
2867 if (seq->views_format == R_IMF_VIEWS_STEREO_3D) {
2869 IMB_ImBufFromStereo3d(seq->stereo3d_format, ibuf_arr[0], &ibuf_arr[0], &ibuf_arr[1]);
2872 /* probably proxy hasn't been created yet */
2873 MEM_freeN(ibuf_arr);
2878 for (i = 0; i < totviews; i++) {
2879 SeqRenderData localcontext = *context;
2880 localcontext.view_id = i;
2883 /* all sequencer color is done in SRGB space, linear gives odd crossfades */
2884 BKE_sequencer_imbuf_to_sequencer_space(context->scene, ibuf_arr[i], false);
2886 if (i != context->view_id) {
2887 copy_to_ibuf_still(&localcontext, seq, nr, ibuf_arr[i]);
2888 BKE_sequencer_cache_put(&localcontext, seq, cfra, SEQ_STRIPELEM_IBUF, ibuf_arr[i]);
2892 /* return the original requested ImBuf */
2893 ibuf = ibuf_arr[context->view_id];
2895 seq->strip->stripdata->orig_width = ibuf->x;
2896 seq->strip->stripdata->orig_height = ibuf->y;
2899 /* "remove" the others (decrease their refcount) */
2900 for (i = 0; i < totviews; i++) {
2901 if (ibuf_arr[i] != ibuf) {
2902 IMB_freeImBuf(ibuf_arr[i]);
2906 MEM_freeN(ibuf_arr);
2910 sanim = seq->anims.first;
2911 if (sanim && sanim->anim) {
2912 IMB_Proxy_Size proxy_size = seq_rendersize_to_proxysize(context->preview_render_size);
2913 IMB_anim_set_preseek(sanim->anim, seq->anim_preseek);
2915 ibuf = IMB_anim_absolute(sanim->anim, nr + seq->anim_startofs,
2916 seq->strip->proxy ? seq->strip->proxy->tc : IMB_TC_RECORD_RUN,
2919 /* fetching for requested proxy size failed, try fetching the original instead */
2920 if (!ibuf && proxy_size != IMB_PROXY_NONE) {
2921 ibuf = IMB_anim_absolute(sanim->anim, nr + seq->anim_startofs,
2922 seq->strip->proxy ? seq->strip->proxy->tc : IMB_TC_RECORD_RUN,
2926 BKE_sequencer_imbuf_to_sequencer_space(context->scene, ibuf, false);
2928 /* we don't need both (speed reasons)! */
2929 if (ibuf->rect_float && ibuf->rect) {
2930 imb_freerectImBuf(ibuf);
2933 seq->strip->stripdata->orig_width = ibuf->x;
2934 seq->strip->stripdata->orig_height = ibuf->y;
2941 static ImBuf *seq_render_movieclip_strip(const SeqRenderData *context, Sequence *seq, float nr)
2945 float tloc[2], tscale, tangle;
2951 memset(&user, 0, sizeof(MovieClipUser));
2953 BKE_movieclip_user_set_frame(&user, nr + seq->anim_startofs + seq->clip->start_frame);
2955 user.render_size = MCLIP_PROXY_RENDER_SIZE_FULL;
2957 switch (seq_rendersize_to_proxysize(context->preview_render_size)) {
2958 case IMB_PROXY_NONE:
2959 user.render_size = MCLIP_PROXY_RENDER_SIZE_FULL;
2962 user.render_size = MCLIP_PROXY_RENDER_SIZE_100;
2965 user.render_size = MCLIP_PROXY_RENDER_SIZE_75;
2968 user.render_size = MCLIP_PROXY_RENDER_SIZE_50;
2971 user.render_size = MCLIP_PROXY_RENDER_SIZE_25;
2975 if (seq->clip_flag & SEQ_MOVIECLIP_RENDER_UNDISTORTED) {
2976 user.render_flag = MCLIP_PROXY_RENDER_UNDISTORT;
2979 if (seq->clip_flag & SEQ_MOVIECLIP_RENDER_STABILIZED) {
2980 ibuf = BKE_movieclip_get_stable_ibuf(seq->clip, &user, tloc, &tscale, &tangle, 0);
2983 ibuf = BKE_movieclip_get_ibuf_flag(seq->clip, &user, 0, MOVIECLIP_CACHE_SKIP);
2990 static ImBuf *seq_render_mask(const SeqRenderData *context, Mask *mask, float nr, bool make_float)
2992 /* TODO - add option to rasterize to alpha imbuf? */
3002 MaskRasterHandle *mr_handle;
3004 mask_temp = BKE_mask_copy_nolib(mask);
3006 BKE_mask_evaluate(mask_temp, mask->sfra + nr, true);
3008 maskbuf = MEM_mallocN(sizeof(float) * context->rectx * context->recty, __func__);
3010 mr_handle = BKE_maskrasterize_handle_new();
3012 BKE_maskrasterize_handle_init(mr_handle, mask_temp, context->rectx, context->recty, true, true, true);
3014 BKE_mask_free_nolib(mask_temp);
3015 MEM_freeN(mask_temp);
3017 BKE_maskrasterize_buffer(mr_handle, context->rectx, context->recty, maskbuf);
3019 BKE_maskrasterize_handle_free(mr_handle);
3025 const float *fp_src;
3028 ibuf = IMB_allocImBuf(context->rectx, context->recty, 32, IB_rectfloat);
3031 fp_dst = ibuf->rect_float;
3032 i = context->rectx * context->recty;
3034 fp_dst[0] = fp_dst[1] = fp_dst[2] = *fp_src;
3043 const float *fp_src;
3044 unsigned char *ub_dst;
3046 ibuf = IMB_allocImBuf(context->rectx, context->recty, 32, IB_rect);
3049 ub_dst = (unsigned char *)ibuf->rect;
3050 i = context->rectx * context->recty;
3052 ub_dst[0] = ub_dst[1] = ub_dst[2] = (unsigned char)(*fp_src * 255.0f); /* already clamped */
3065 static ImBuf *seq_render_mask_strip(const SeqRenderData *context, Sequence *seq, float nr)
3067 bool make_float = (seq->flag & SEQ_MAKE_FLOAT) != 0;
3069 return seq_render_mask(context, seq->mask, nr, make_float);
3072 static ImBuf *seq_render_scene_strip(const SeqRenderData *context, Sequence *seq, float nr, float cfra)
3078 ListBase oldmarkers;
3081 * Hack! This function can be called from do_render_seq(), in that case
3082 * the seq->scene can already have a Render initialized with same name,
3083 * so we have to use a default name. (compositor uses scene name to
3085 * However, when called from within the UI (image preview in sequencer)
3086 * we do want to use scene Render, that way the render result is defined
3087 * for display in render/imagewindow
3089 * Hmm, don't see, why we can't do that all the time,
3090 * and since G.is_rendering is uhm, gone... (Peter)
3094 * Using the same name for the renders works just fine as the do_render_seq()
3095 * render is not used while the scene strips are rendered.
3097 * However rendering from UI (through sequencer_preview_area_draw) can crash in
3098 * very many cases since other renders (material preview, an actual render etc.)
3099 * can be started while this sequence preview render is running. The only proper
3100 * solution is to make the sequencer preview render a proper job, which can be
3101 * stopped when needed. This would also give a nice progress bar for the preview
3102 * space so that users know there's something happening.
3104 * As a result the active scene now only uses OpenGL rendering for the sequencer
3105 * preview. This is far from nice, but is the only way to prevent crashes at this
3111 const bool is_rendering = G.is_rendering;