b2a19c590843ab599cf3fd55e6d00318cca41c7f
[blender.git] / source / blender / blenkernel / intern / sequencer.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): 
22  * - Blender Foundation, 2003-2009
23  * - Peter Schlaile <peter [at] schlaile [dot] de> 2005/2006
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/sequencer.c
29  *  \ingroup bke
30  */
31
32 #include <stddef.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <math.h>
36
37 #include "MEM_guardedalloc.h"
38
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"
46
47 #include "BLI_math.h"
48 #include "BLI_fileops.h"
49 #include "BLI_listbase.h"
50 #include "BLI_linklist.h"
51 #include "BLI_path_util.h"
52 #include "BLI_string.h"
53 #include "BLI_string_utf8.h"
54 #include "BLI_threads.h"
55 #include "BLI_utildefines.h"
56
57 #ifdef WIN32
58 #  include "BLI_winstuff.h"
59 #else
60 #  include <unistd.h>
61 #endif
62
63 #include "BLT_translation.h"
64
65 #include "BKE_animsys.h"
66 #include "BKE_depsgraph.h"
67 #include "BKE_global.h"
68 #include "BKE_image.h"
69 #include "BKE_main.h"
70 #include "BKE_sequencer.h"
71 #include "BKE_movieclip.h"
72 #include "BKE_fcurve.h"
73 #include "BKE_scene.h"
74 #include "BKE_mask.h"
75 #include "BKE_library.h"
76 #include "BKE_idprop.h"
77
78 #include "RNA_access.h"
79
80 #include "RE_pipeline.h"
81
82 #include <pthread.h>
83
84 #include "IMB_imbuf.h"
85 #include "IMB_imbuf_types.h"
86 #include "IMB_colormanagement.h"
87
88 #include "BKE_context.h"
89 #include "BKE_sound.h"
90
91 #ifdef WITH_AUDASPACE
92 #  include AUD_SPECIAL_H
93 #endif
94
95 /* mutable state for sequencer */
96 typedef struct SeqRenderState {
97         LinkNode *scene_parents;
98 } SeqRenderState;
99
100 static ImBuf *seq_render_strip_stack(
101         const SeqRenderData *context, SeqRenderState *state, ListBase *seqbasep,
102         float cfra, int chanshown);
103 static ImBuf *seq_render_strip(
104         const SeqRenderData *context, SeqRenderState *state,
105         Sequence *seq, float cfra);
106 static void seq_free_animdata(Scene *scene, Sequence *seq);
107 static ImBuf *seq_render_mask(const SeqRenderData *context, Mask *mask, float nr, bool make_float);
108 static int seq_num_files(Scene *scene, char views_format, const bool is_multiview);
109 static void seq_anim_add_suffix(Scene *scene, struct anim *anim, const int view_id);
110
111 /* **** XXX ******** */
112 #define SELECT 1
113 ListBase seqbase_clipboard;
114 int seqbase_clipboard_frame;
115 SequencerDrawView sequencer_view3d_cb = NULL; /* NULL in background mode */
116
117 #if 0  /* unused function */
118 static void printf_strip(Sequence *seq)
119 {
120         fprintf(stderr, "name: '%s', len:%d, start:%d, (startofs:%d, endofs:%d), "
121                 "(startstill:%d, endstill:%d), machine:%d, (startdisp:%d, enddisp:%d)\n",
122                 seq->name, seq->len, seq->start, seq->startofs, seq->endofs, seq->startstill, seq->endstill, seq->machine,
123                 seq->startdisp, seq->enddisp);
124
125         fprintf(stderr, "\tseq_tx_set_final_left: %d %d\n\n", seq_tx_get_final_left(seq, 0),
126                 seq_tx_get_final_right(seq, 0));
127 }
128 #endif
129
130 static void sequencer_state_init(SeqRenderState *state)
131 {
132         state->scene_parents = NULL;
133 }
134
135 int BKE_sequencer_base_recursive_apply(ListBase *seqbase, int (*apply_func)(Sequence *seq, void *), void *arg)
136 {
137         Sequence *iseq;
138         for (iseq = seqbase->first; iseq; iseq = iseq->next) {
139                 if (BKE_sequencer_recursive_apply(iseq, apply_func, arg) == -1)
140                         return -1;  /* bail out */
141         }
142         return 1;
143 }
144
145 int BKE_sequencer_recursive_apply(Sequence *seq, int (*apply_func)(Sequence *, void *), void *arg)
146 {
147         int ret = apply_func(seq, arg);
148
149         if (ret == -1)
150                 return -1;  /* bail out */
151
152         if (ret && seq->seqbase.first)
153                 ret = BKE_sequencer_base_recursive_apply(&seq->seqbase, apply_func, arg);
154
155         return ret;
156 }
157
158 /*********************** alloc / free functions *************************/
159
160 /* free */
161
162 static void free_proxy_seq(Sequence *seq)
163 {
164         if (seq->strip && seq->strip->proxy && seq->strip->proxy->anim) {
165                 IMB_free_anim(seq->strip->proxy->anim);
166                 seq->strip->proxy->anim = NULL;
167         }
168 }
169
170 static void seq_free_strip(Strip *strip)
171 {
172         strip->us--;
173         if (strip->us > 0)
174                 return;
175         if (strip->us < 0) {
176                 printf("error: negative users in strip\n");
177                 return;
178         }
179
180         if (strip->stripdata) {
181                 MEM_freeN(strip->stripdata);
182         }
183
184         if (strip->proxy) {
185                 if (strip->proxy->anim) {
186                         IMB_free_anim(strip->proxy->anim);
187                 }
188
189                 MEM_freeN(strip->proxy);
190         }
191         if (strip->crop) {
192                 MEM_freeN(strip->crop);
193         }
194         if (strip->transform) {
195                 MEM_freeN(strip->transform);
196         }
197
198         MEM_freeN(strip);
199 }
200
201 /* only give option to skip cache locally (static func) */
202 static void BKE_sequence_free_ex(Scene *scene, Sequence *seq, const bool do_cache)
203 {
204         if (seq->strip)
205                 seq_free_strip(seq->strip);
206
207         BKE_sequence_free_anim(seq);
208
209         if (seq->type & SEQ_TYPE_EFFECT) {
210                 struct SeqEffectHandle sh = BKE_sequence_get_effect(seq);
211
212                 sh.free(seq);
213         }
214
215         if (seq->sound) {
216                 id_us_min(((ID *)seq->sound));
217         }
218
219         if (seq->stereo3d_format) {
220                 MEM_freeN(seq->stereo3d_format);
221         }
222
223         /* clipboard has no scene and will never have a sound handle or be active
224          * same goes to sequences copy for proxy rebuild job
225          */
226         if (scene) {
227                 Editing *ed = scene->ed;
228
229                 if (ed->act_seq == seq)
230                         ed->act_seq = NULL;
231
232                 if (seq->scene_sound && ELEM(seq->type, SEQ_TYPE_SOUND_RAM, SEQ_TYPE_SCENE))
233                         BKE_sound_remove_scene_sound(scene, seq->scene_sound);
234
235                 seq_free_animdata(scene, seq);
236         }
237
238         if (seq->prop) {
239                 IDP_FreeProperty(seq->prop);
240                 MEM_freeN(seq->prop);
241         }
242
243         /* free modifiers */
244         BKE_sequence_modifier_clear(seq);
245
246         /* free cached data used by this strip,
247          * also invalidate cache for all dependent sequences
248          *
249          * be _very_ careful here, invalidating cache loops over the scene sequences and
250          * assumes the listbase is valid for all strips, this may not be the case if lists are being freed.
251          * this is optional BKE_sequence_invalidate_cache
252          */
253         if (do_cache) {
254                 if (scene) {
255                         BKE_sequence_invalidate_cache(scene, seq);
256                 }
257         }
258
259         MEM_freeN(seq);
260 }
261
262 void BKE_sequence_free(Scene *scene, Sequence *seq)
263 {
264         BKE_sequence_free_ex(scene, seq, true);
265 }
266
267 /* Function to free imbuf and anim data on changes */
268 void BKE_sequence_free_anim(Sequence *seq)
269 {
270         while (seq->anims.last) {
271                 StripAnim *sanim = seq->anims.last;
272
273                 if (sanim->anim) {
274                         IMB_free_anim(sanim->anim);
275                         sanim->anim = NULL;
276                 }
277
278                 BLI_freelinkN(&seq->anims, sanim);
279         }
280         BLI_listbase_clear(&seq->anims);
281 }
282
283 /* cache must be freed before calling this function
284  * since it leaves the seqbase in an invalid state */
285 static void seq_free_sequence_recurse(Scene *scene, Sequence *seq)
286 {
287         Sequence *iseq, *iseq_next;
288
289         for (iseq = seq->seqbase.first; iseq; iseq = iseq_next) {
290                 iseq_next = iseq->next;
291                 seq_free_sequence_recurse(scene, iseq);
292         }
293
294         BKE_sequence_free_ex(scene, seq, false);
295 }
296
297
298 Editing *BKE_sequencer_editing_get(Scene *scene, bool alloc)
299 {
300         if (alloc) {
301                 BKE_sequencer_editing_ensure(scene);
302         }
303         return scene->ed;
304 }
305
306 void BKE_sequencer_free_clipboard(void)
307 {
308         Sequence *seq, *nseq;
309
310         BKE_sequencer_base_clipboard_pointers_free(&seqbase_clipboard);
311
312         for (seq = seqbase_clipboard.first; seq; seq = nseq) {
313                 nseq = seq->next;
314                 seq_free_sequence_recurse(NULL, seq);
315         }
316         BLI_listbase_clear(&seqbase_clipboard);
317 }
318
319 /* -------------------------------------------------------------------- */
320 /* Manage pointers in the clipboard.
321  * note that these pointers should _never_ be access in the sequencer,
322  * they are only for storage while in the clipboard
323  * notice 'newid' is used for temp pointer storage here, validate on access.
324  */
325 #define ID_PT (*id_pt)
326 static void seqclipboard_ptr_free(ID **id_pt)
327 {
328         if (ID_PT) {
329                 BLI_assert(ID_PT->newid != NULL);
330                 MEM_freeN(ID_PT);
331                 ID_PT = NULL;
332         }
333 }
334 static void seqclipboard_ptr_store(ID **id_pt)
335 {
336         if (ID_PT) {
337                 ID *id_prev = ID_PT;
338                 ID_PT = MEM_dupallocN(ID_PT);
339                 ID_PT->newid = id_prev;
340         }
341 }
342 static void seqclipboard_ptr_restore(Main *bmain, ID **id_pt)
343 {
344         if (ID_PT) {
345                 const ListBase *lb = which_libbase(bmain, GS(ID_PT->name));
346                 void *id_restore;
347
348                 BLI_assert(ID_PT->newid != NULL);
349                 if (BLI_findindex(lb, (ID_PT)->newid) != -1) {
350                         /* the pointer is still valid */
351                         id_restore = (ID_PT)->newid;
352                 }
353                 else {
354                         /* the pointer of the same name still exists  */
355                         id_restore = BLI_findstring(lb, (ID_PT)->name + 2, offsetof(ID, name) + 2);
356                 }
357
358                 if (id_restore == NULL) {
359                         /* check for a data with the same filename */
360                         switch (GS(ID_PT->name)) {
361                                 case ID_SO:
362                                 {
363                                         id_restore = BLI_findstring(lb, ((bSound *)ID_PT)->name, offsetof(bSound, name));
364                                         if (id_restore == NULL) {
365                                                 id_restore = BKE_sound_new_file(bmain, ((bSound *)ID_PT)->name);
366                                                 (ID_PT)->newid = id_restore;  /* reuse next time */
367                                         }
368                                         break;
369                                 }
370                                 case ID_MC:
371                                 {
372                                         id_restore = BLI_findstring(lb, ((MovieClip *)ID_PT)->name, offsetof(MovieClip, name));
373                                         if (id_restore == NULL) {
374                                                 id_restore = BKE_movieclip_file_add(bmain, ((MovieClip *)ID_PT)->name);
375                                                 (ID_PT)->newid = id_restore;  /* reuse next time */
376                                         }
377                                         break;
378                                 }
379                         }
380                 }
381
382                 ID_PT = id_restore;
383         }
384 }
385 #undef ID_PT
386
387 void BKE_sequence_clipboard_pointers_free(Sequence *seq)
388 {
389         seqclipboard_ptr_free((ID **)&seq->scene);
390         seqclipboard_ptr_free((ID **)&seq->scene_camera);
391         seqclipboard_ptr_free((ID **)&seq->clip);
392         seqclipboard_ptr_free((ID **)&seq->mask);
393         seqclipboard_ptr_free((ID **)&seq->sound);
394 }
395 void BKE_sequence_clipboard_pointers_store(Sequence *seq)
396 {
397         seqclipboard_ptr_store((ID **)&seq->scene);
398         seqclipboard_ptr_store((ID **)&seq->scene_camera);
399         seqclipboard_ptr_store((ID **)&seq->clip);
400         seqclipboard_ptr_store((ID **)&seq->mask);
401         seqclipboard_ptr_store((ID **)&seq->sound);
402 }
403 void BKE_sequence_clipboard_pointers_restore(Sequence *seq, Main *bmain)
404 {
405         seqclipboard_ptr_restore(bmain, (ID **)&seq->scene);
406         seqclipboard_ptr_restore(bmain, (ID **)&seq->scene_camera);
407         seqclipboard_ptr_restore(bmain, (ID **)&seq->clip);
408         seqclipboard_ptr_restore(bmain, (ID **)&seq->mask);
409         seqclipboard_ptr_restore(bmain, (ID **)&seq->sound);
410 }
411
412 /* recursive versions of funcions above */
413 void BKE_sequencer_base_clipboard_pointers_free(ListBase *seqbase)
414 {
415         Sequence *seq;
416         for (seq = seqbase->first; seq; seq = seq->next) {
417                 BKE_sequence_clipboard_pointers_free(seq);
418                 BKE_sequencer_base_clipboard_pointers_free(&seq->seqbase);
419         }
420 }
421 void BKE_sequencer_base_clipboard_pointers_store(ListBase *seqbase)
422 {
423         Sequence *seq;
424         for (seq = seqbase->first; seq; seq = seq->next) {
425                 BKE_sequence_clipboard_pointers_store(seq);
426                 BKE_sequencer_base_clipboard_pointers_store(&seq->seqbase);
427         }
428 }
429 void BKE_sequencer_base_clipboard_pointers_restore(ListBase *seqbase, Main *bmain)
430 {
431         Sequence *seq;
432         for (seq = seqbase->first; seq; seq = seq->next) {
433                 BKE_sequence_clipboard_pointers_restore(seq, bmain);
434                 BKE_sequencer_base_clipboard_pointers_restore(&seq->seqbase, bmain);
435         }
436 }
437
438 /* end clipboard pointer mess */
439
440
441 Editing *BKE_sequencer_editing_ensure(Scene *scene)
442 {
443         if (scene->ed == NULL) {
444                 Editing *ed;
445
446                 ed = scene->ed = MEM_callocN(sizeof(Editing), "addseq");
447                 ed->seqbasep = &ed->seqbase;
448         }
449
450         return scene->ed;
451 }
452
453 void BKE_sequencer_editing_free(Scene *scene)
454 {
455         Editing *ed = scene->ed;
456         Sequence *seq;
457
458         if (ed == NULL)
459                 return;
460
461         /* this may not be the active scene!, could be smarter about this */
462         BKE_sequencer_cache_cleanup();
463
464         SEQ_BEGIN (ed, seq)
465         {
466                 /* handle cache freeing above */
467                 BKE_sequence_free_ex(scene, seq, false);
468         }
469         SEQ_END
470
471         BLI_freelistN(&ed->metastack);
472
473         MEM_freeN(ed);
474
475         scene->ed = NULL;
476 }
477
478 /*********************** Sequencer color space functions  *************************/
479
480 static void sequencer_imbuf_assign_spaces(Scene *scene, ImBuf *ibuf)
481 {
482         if (ibuf->rect_float) {
483                 IMB_colormanagement_assign_float_colorspace(ibuf, scene->sequencer_colorspace_settings.name);
484         }
485 }
486
487 void BKE_sequencer_imbuf_to_sequencer_space(Scene *scene, ImBuf *ibuf, bool make_float)
488 {
489         const char *from_colorspace = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_SCENE_LINEAR);
490         const char *to_colorspace = scene->sequencer_colorspace_settings.name;
491         const char *float_colorspace = IMB_colormanagement_get_float_colorspace(ibuf);
492
493         if (!ibuf->rect_float) {
494                 if (ibuf->rect) {
495                         const char *byte_colorspace = IMB_colormanagement_get_rect_colorspace(ibuf);
496                         if (make_float || !STREQ(to_colorspace, byte_colorspace)) {
497                                 /* If byte space is not in sequencer's working space, we deliver float color space,
498                                  * this is to to prevent data loss.
499                                  */
500
501                                 /* when converting byte buffer to float in sequencer we need to make float
502                                  * buffer be in sequencer's working space, which is currently only doable
503                                  * from linear space.
504                                  */
505
506                                 /*
507                                  * OCIO_TODO: would be nice to support direct single transform from byte to sequencer's
508                                  */
509
510                                 IMB_float_from_rect(ibuf);
511                         }
512                         else {
513                                 return;
514                         }
515                 }
516                 else {
517                         return;
518                 }
519         }
520
521         if (from_colorspace && from_colorspace[0] != '\0') {
522                 if (ibuf->rect)
523                         imb_freerectImBuf(ibuf);
524
525                 if (!STREQ(float_colorspace, to_colorspace)) {
526                         IMB_colormanagement_transform_threaded(ibuf->rect_float, ibuf->x, ibuf->y, ibuf->channels,
527                                                                from_colorspace, to_colorspace, true);
528                         sequencer_imbuf_assign_spaces(scene, ibuf);
529                 }
530         }
531 }
532
533 void BKE_sequencer_imbuf_from_sequencer_space(Scene *scene, ImBuf *ibuf)
534 {
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);
537
538         if (!ibuf->rect_float)
539                 return;
540
541         if (to_colorspace && to_colorspace[0] != '\0') {
542                 IMB_colormanagement_transform_threaded(ibuf->rect_float, ibuf->x, ibuf->y, ibuf->channels,
543                                                        from_colorspace, to_colorspace, true);
544                 IMB_colormanagement_assign_float_colorspace(ibuf, to_colorspace);
545         }
546 }
547
548 void BKE_sequencer_pixel_from_sequencer_space_v4(struct Scene *scene, float pixel[4])
549 {
550         const char *from_colorspace = scene->sequencer_colorspace_settings.name;
551         const char *to_colorspace = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_SCENE_LINEAR);
552
553         if (to_colorspace && to_colorspace[0] != '\0') {
554                 IMB_colormanagement_transform_v4(pixel, from_colorspace, to_colorspace);
555         }
556         else {
557                 /* if no color management enables fallback to legacy conversion */
558                 srgb_to_linearrgb_v4(pixel, pixel);
559         }
560 }
561
562 /*********************** sequencer pipeline functions *************************/
563
564 void BKE_sequencer_new_render_data(
565         EvaluationContext *eval_ctx,
566         Main *bmain, Scene *scene, int rectx, int recty,
567         int preview_render_size,
568         SeqRenderData *r_context)
569 {
570         r_context->eval_ctx = eval_ctx;
571         r_context->bmain = bmain;
572         r_context->scene = scene;
573         r_context->rectx = rectx;
574         r_context->recty = recty;
575         r_context->preview_render_size = preview_render_size;
576         r_context->motion_blur_samples = 0;
577         r_context->motion_blur_shutter = 0;
578         r_context->skip_cache = false;
579         r_context->is_proxy_render = false;
580         r_context->view_id = 0;
581         r_context->gpu_offscreen = NULL;
582         r_context->gpu_samples = (scene->r.mode & R_OSA) ? scene->r.osa : 0;
583         r_context->gpu_full_samples = (r_context->gpu_samples) && (scene->r.scemode & R_FULL_SAMPLE);
584 }
585
586 /* ************************* iterator ************************** */
587 /* *************** (replaces old WHILE_SEQ) ********************* */
588 /* **************** use now SEQ_BEGIN () SEQ_END ***************** */
589
590 /* sequence strip iterator:
591  * - builds a full array, recursively into meta strips
592  */
593
594 static void seq_count(ListBase *seqbase, int *tot)
595 {
596         Sequence *seq;
597
598         for (seq = seqbase->first; seq; seq = seq->next) {
599                 (*tot)++;
600
601                 if (seq->seqbase.first)
602                         seq_count(&seq->seqbase, tot);
603         }
604 }
605
606 static void seq_build_array(ListBase *seqbase, Sequence ***array, int depth)
607 {
608         Sequence *seq;
609
610         for (seq = seqbase->first; seq; seq = seq->next) {
611                 seq->depth = depth;
612
613                 if (seq->seqbase.first)
614                         seq_build_array(&seq->seqbase, array, depth + 1);
615
616                 **array = seq;
617                 (*array)++;
618         }
619 }
620
621 static void seq_array(Editing *ed, Sequence ***seqarray, int *tot, bool use_pointer)
622 {
623         Sequence **array;
624
625         *seqarray = NULL;
626         *tot = 0;
627
628         if (ed == NULL)
629                 return;
630
631         if (use_pointer)
632                 seq_count(ed->seqbasep, tot);
633         else
634                 seq_count(&ed->seqbase, tot);
635
636         if (*tot == 0)
637                 return;
638
639         *seqarray = array = MEM_mallocN(sizeof(Sequence *) * (*tot), "SeqArray");
640         if (use_pointer)
641                 seq_build_array(ed->seqbasep, &array, 0);
642         else
643                 seq_build_array(&ed->seqbase, &array, 0);
644 }
645
646 void BKE_sequence_iterator_begin(Editing *ed, SeqIterator *iter, bool use_pointer)
647 {
648         memset(iter, 0, sizeof(*iter));
649         seq_array(ed, &iter->array, &iter->tot, use_pointer);
650
651         if (iter->tot) {
652                 iter->cur = 0;
653                 iter->seq = iter->array[iter->cur];
654                 iter->valid = 1;
655         }
656 }
657
658 void BKE_sequence_iterator_next(SeqIterator *iter)
659 {
660         if (++iter->cur < iter->tot)
661                 iter->seq = iter->array[iter->cur];
662         else
663                 iter->valid = 0;
664 }
665
666 void BKE_sequence_iterator_end(SeqIterator *iter)
667 {
668         if (iter->array)
669                 MEM_freeN(iter->array);
670
671         iter->valid = 0;
672 }
673
674 static int metaseq_start(Sequence *metaseq)
675 {
676         return metaseq->start + metaseq->startofs;
677 }
678
679 static int metaseq_end(Sequence *metaseq)
680 {
681         return metaseq->start + metaseq->len - metaseq->endofs;
682 }
683
684 static void seq_update_sound_bounds_recursive_rec(Scene *scene, Sequence *metaseq, int start, int end)
685 {
686         Sequence *seq;
687
688         /* for sound we go over full meta tree to update bounds of the sound strips,
689          * since sound is played outside of evaluating the imbufs, */
690         for (seq = metaseq->seqbase.first; seq; seq = seq->next) {
691                 if (seq->type == SEQ_TYPE_META) {
692                         seq_update_sound_bounds_recursive_rec(scene, seq, max_ii(start, metaseq_start(seq)),
693                                                               min_ii(end, metaseq_end(seq)));
694                 }
695                 else if (ELEM(seq->type, SEQ_TYPE_SOUND_RAM, SEQ_TYPE_SCENE)) {
696                         if (seq->scene_sound) {
697                                 int startofs = seq->startofs;
698                                 int endofs = seq->endofs;
699                                 if (seq->startofs + seq->start < start)
700                                         startofs = start - seq->start;
701
702                                 if (seq->start + seq->len - seq->endofs > end)
703                                         endofs = seq->start + seq->len - end;
704
705                                 BKE_sound_move_scene_sound(scene, seq->scene_sound, seq->start + startofs,
706                                                        seq->start + seq->len - endofs, startofs + seq->anim_startofs);
707                         }
708                 }
709         }
710 }
711
712 static void seq_update_sound_bounds_recursive(Scene *scene, Sequence *metaseq)
713 {
714         seq_update_sound_bounds_recursive_rec(scene, metaseq, metaseq_start(metaseq), metaseq_end(metaseq));
715 }
716
717 void BKE_sequence_calc_disp(Scene *scene, Sequence *seq)
718 {
719         if (seq->startofs && seq->startstill)
720                 seq->startstill = 0;
721         if (seq->endofs && seq->endstill)
722                 seq->endstill = 0;
723         
724         seq->startdisp = seq->start + seq->startofs - seq->startstill;
725         seq->enddisp = seq->start + seq->len - seq->endofs + seq->endstill;
726         
727         seq->handsize = 10.0;  /* 10 frames */
728         if (seq->enddisp - seq->startdisp < 10) {
729                 seq->handsize = (float)(0.5 * (seq->enddisp - seq->startdisp));
730         }
731         else if (seq->enddisp - seq->startdisp > 250) {
732                 seq->handsize = (float)((seq->enddisp - seq->startdisp) / 25);
733         }
734
735         if (ELEM(seq->type, SEQ_TYPE_SOUND_RAM, SEQ_TYPE_SCENE)) {
736                 BKE_sequencer_update_sound_bounds(scene, seq);
737         }
738         else if (seq->type == SEQ_TYPE_META) {
739                 seq_update_sound_bounds_recursive(scene, seq);
740         }
741 }
742
743 void BKE_sequence_calc(Scene *scene, Sequence *seq)
744 {
745         Sequence *seqm;
746         int min, max;
747
748         /* check all metas recursively */
749         seqm = seq->seqbase.first;
750         while (seqm) {
751                 if (seqm->seqbase.first) BKE_sequence_calc(scene, seqm);
752                 seqm = seqm->next;
753         }
754
755         /* effects and meta: automatic start and end */
756
757         if (seq->type & SEQ_TYPE_EFFECT) {
758                 /* pointers */
759                 if (seq->seq2 == NULL)
760                         seq->seq2 = seq->seq1;
761                 if (seq->seq3 == NULL)
762                         seq->seq3 = seq->seq1;
763
764                 /* effecten go from seq1 -> seq2: test */
765
766                 /* we take the largest start and smallest end */
767
768                 // seq->start = seq->startdisp = MAX2(seq->seq1->startdisp, seq->seq2->startdisp);
769                 // seq->enddisp = MIN2(seq->seq1->enddisp, seq->seq2->enddisp);
770
771                 if (seq->seq1) {
772                         /* XXX These resets should not be necessary, but users used to be able to
773                          *     edit effect's length, leading to strange results. See [#29190] */
774                         seq->startofs = seq->endofs = seq->startstill = seq->endstill = 0;
775                         seq->start = seq->startdisp = max_iii(seq->seq1->startdisp, seq->seq2->startdisp, seq->seq3->startdisp);
776                         seq->enddisp                = min_iii(seq->seq1->enddisp,   seq->seq2->enddisp,   seq->seq3->enddisp);
777                         /* we cant help if strips don't overlap, it wont give useful results.
778                          * but at least ensure 'len' is never negative which causes bad bugs elsewhere. */
779                         if (seq->enddisp < seq->startdisp) {
780                                 /* simple start/end swap */
781                                 seq->start = seq->enddisp;
782                                 seq->enddisp = seq->startdisp;
783                                 seq->startdisp = seq->start;
784                                 seq->flag |= SEQ_INVALID_EFFECT;
785                         }
786                         else {
787                                 seq->flag &= ~SEQ_INVALID_EFFECT;
788                         }
789
790                         seq->len = seq->enddisp - seq->startdisp;
791                 }
792                 else {
793                         BKE_sequence_calc_disp(scene, seq);
794                 }
795         }
796         else {
797                 if (seq->type == SEQ_TYPE_META) {
798                         seqm = seq->seqbase.first;
799                         if (seqm) {
800                                 min =  MAXFRAME * 2;
801                                 max = -MAXFRAME * 2;
802                                 while (seqm) {
803                                         if (seqm->startdisp < min) min = seqm->startdisp;
804                                         if (seqm->enddisp > max) max = seqm->enddisp;
805                                         seqm = seqm->next;
806                                 }
807                                 seq->start = min + seq->anim_startofs;
808                                 seq->len = max - min;
809                                 seq->len -= seq->anim_startofs;
810                                 seq->len -= seq->anim_endofs;
811                         }
812                         seq_update_sound_bounds_recursive(scene, seq);
813                 }
814                 BKE_sequence_calc_disp(scene, seq);
815         }
816 }
817
818 static void seq_multiview_name(
819         Scene *scene, const int view_id, const char *prefix, const char *ext,
820         char *r_path, size_t r_size)
821 {
822         const char *suffix = BKE_scene_multiview_view_id_suffix_get(&scene->r, view_id);
823         BLI_snprintf(r_path, r_size, "%s%s%s", prefix, suffix, ext);
824 }
825
826 /* note: caller should run BKE_sequence_calc(scene, seq) after */
827 void BKE_sequence_reload_new_file(Scene *scene, Sequence *seq, const bool lock_range)
828 {
829         char path[FILE_MAX];
830         int prev_startdisp = 0, prev_enddisp = 0;
831         /* note: don't rename the strip, will break animation curves */
832
833         if (ELEM(seq->type,
834                   SEQ_TYPE_MOVIE, SEQ_TYPE_IMAGE, SEQ_TYPE_SOUND_RAM,
835                   SEQ_TYPE_SCENE, SEQ_TYPE_META, SEQ_TYPE_MOVIECLIP, SEQ_TYPE_MASK) == 0)
836         {
837                 return;
838         }
839
840         if (lock_range) {
841                 /* keep so we don't have to move the actual start and end points (only the data) */
842                 BKE_sequence_calc_disp(scene, seq);
843                 prev_startdisp = seq->startdisp;
844                 prev_enddisp = seq->enddisp;
845         }
846
847         switch (seq->type) {
848                 case SEQ_TYPE_IMAGE:
849                 {
850                         /* Hack? */
851                         size_t olen = MEM_allocN_len(seq->strip->stripdata) / sizeof(StripElem);
852
853                         seq->len = olen;
854                         seq->len -= seq->anim_startofs;
855                         seq->len -= seq->anim_endofs;
856                         if (seq->len < 0) {
857                                 seq->len = 0;
858                         }
859                         break;
860                 }
861                 case SEQ_TYPE_MOVIE:
862                 {
863                         StripAnim *sanim;
864                         bool is_multiview_loaded = false;
865                         const bool is_multiview = (seq->flag & SEQ_USE_VIEWS) != 0 &&
866                                                   (scene->r.scemode & R_MULTIVIEW) != 0;
867
868                         BLI_join_dirfile(path, sizeof(path), seq->strip->dir,
869                                          seq->strip->stripdata->name);
870                         BLI_path_abs(path, G.main->name);
871
872                         BKE_sequence_free_anim(seq);
873
874                         if (is_multiview && (seq->views_format == R_IMF_VIEWS_INDIVIDUAL)) {
875                                 char prefix[FILE_MAX];
876                                 const char *ext = NULL;
877                                 const int totfiles = seq_num_files(scene, seq->views_format, true);
878                                 int i = 0;
879
880                                 BKE_scene_multiview_view_prefix_get(scene, path, prefix, &ext);
881
882                                 if (prefix[0] != '\0') {
883                                         for (i = 0; i < totfiles; i++) {
884                                                 struct anim *anim;
885                                                 char str[FILE_MAX];
886
887                                                 seq_multiview_name(scene, i, prefix, ext, str, FILE_MAX);
888                                                 anim = openanim(str, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
889                                                                 seq->streamindex, seq->strip->colorspace_settings.name);
890
891                                                 if (anim) {
892                                                         seq_anim_add_suffix(scene, anim, i);
893                                                         sanim = MEM_mallocN(sizeof(StripAnim), "Strip Anim");
894                                                         BLI_addtail(&seq->anims, sanim);
895                                                         sanim->anim = anim;
896                                                 }
897                                         }
898                                         is_multiview_loaded = true;
899                                 }
900                         }
901
902                         if (is_multiview_loaded == false) {
903                                 struct anim *anim;
904                                 anim = openanim(path, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
905                                                 seq->streamindex, seq->strip->colorspace_settings.name);
906                                 if (anim) {
907                                         sanim = MEM_mallocN(sizeof(StripAnim), "Strip Anim");
908                                         BLI_addtail(&seq->anims, sanim);
909                                         sanim->anim = anim;
910                                 }
911                         }
912
913                         /* use the first video as reference for everything */
914                         sanim = seq->anims.first;
915
916                         if ((!sanim) || (!sanim->anim)) {
917                                 return;
918                         }
919
920                         seq->len = IMB_anim_get_duration(sanim->anim, seq->strip->proxy ? seq->strip->proxy->tc : IMB_TC_RECORD_RUN);
921
922                         seq->anim_preseek = IMB_anim_get_preseek(sanim->anim);
923
924                         seq->len -= seq->anim_startofs;
925                         seq->len -= seq->anim_endofs;
926                         if (seq->len < 0) {
927                                 seq->len = 0;
928                         }
929                         break;
930                 }
931                 case SEQ_TYPE_MOVIECLIP:
932                         if (seq->clip == NULL)
933                                 return;
934
935                         seq->len = BKE_movieclip_get_duration(seq->clip);
936
937                         seq->len -= seq->anim_startofs;
938                         seq->len -= seq->anim_endofs;
939                         if (seq->len < 0) {
940                                 seq->len = 0;
941                         }
942                         break;
943                 case SEQ_TYPE_MASK:
944                         if (seq->mask == NULL)
945                                 return;
946                         seq->len = BKE_mask_get_duration(seq->mask);
947                         seq->len -= seq->anim_startofs;
948                         seq->len -= seq->anim_endofs;
949                         if (seq->len < 0) {
950                                 seq->len = 0;
951                         }
952                         break;
953                 case SEQ_TYPE_SOUND_RAM:
954 #ifdef WITH_AUDASPACE
955                         if (!seq->sound)
956                                 return;
957                         seq->len = ceil((double)AUD_getInfo(seq->sound->playback_handle).length * FPS);
958                         seq->len -= seq->anim_startofs;
959                         seq->len -= seq->anim_endofs;
960                         if (seq->len < 0) {
961                                 seq->len = 0;
962                         }
963 #else
964                         return;
965 #endif
966                         break;
967                 case SEQ_TYPE_SCENE:
968                 {
969                         seq->len = (seq->scene) ? seq->scene->r.efra - seq->scene->r.sfra + 1 : 0;
970                         seq->len -= seq->anim_startofs;
971                         seq->len -= seq->anim_endofs;
972                         if (seq->len < 0) {
973                                 seq->len = 0;
974                         }
975                         break;
976                 }
977         }
978
979         free_proxy_seq(seq);
980
981         if (lock_range) {
982                 BKE_sequence_tx_set_final_left(seq, prev_startdisp);
983                 BKE_sequence_tx_set_final_right(seq, prev_enddisp);
984                 BKE_sequence_single_fix(seq);
985         }
986         
987         BKE_sequence_calc(scene, seq);
988 }
989
990 void BKE_sequencer_sort(Scene *scene)
991 {
992         /* all strips together per kind, and in order of y location ("machine") */
993         ListBase seqbase, effbase;
994         Editing *ed = BKE_sequencer_editing_get(scene, false);
995         Sequence *seq, *seqt;
996
997         if (ed == NULL)
998                 return;
999
1000         BLI_listbase_clear(&seqbase);
1001         BLI_listbase_clear(&effbase);
1002
1003         while ((seq = BLI_pophead(ed->seqbasep))) {
1004
1005                 if (seq->type & SEQ_TYPE_EFFECT) {
1006                         seqt = effbase.first;
1007                         while (seqt) {
1008                                 if (seqt->machine >= seq->machine) {
1009                                         BLI_insertlinkbefore(&effbase, seqt, seq);
1010                                         break;
1011                                 }
1012                                 seqt = seqt->next;
1013                         }
1014                         if (seqt == NULL)
1015                                 BLI_addtail(&effbase, seq);
1016                 }
1017                 else {
1018                         seqt = seqbase.first;
1019                         while (seqt) {
1020                                 if (seqt->machine >= seq->machine) {
1021                                         BLI_insertlinkbefore(&seqbase, seqt, seq);
1022                                         break;
1023                                 }
1024                                 seqt = seqt->next;
1025                         }
1026                         if (seqt == NULL)
1027                                 BLI_addtail(&seqbase, seq);
1028                 }
1029         }
1030
1031         BLI_movelisttolist(&seqbase, &effbase);
1032         *(ed->seqbasep) = seqbase;
1033 }
1034
1035 /** Comparision function suitable to be used with BLI_listbase_sort()... */
1036 int BKE_sequencer_cmp_time_startdisp(const void *a, const void *b)
1037 {
1038         const Sequence *seq_a = a;
1039         const Sequence *seq_b = b;
1040
1041         return (seq_a->startdisp > seq_b->startdisp);
1042 }
1043
1044 static int clear_scene_in_allseqs_cb(Sequence *seq, void *arg_pt)
1045 {
1046         if (seq->scene == (Scene *)arg_pt)
1047                 seq->scene = NULL;
1048         return 1;
1049 }
1050
1051 void BKE_sequencer_clear_scene_in_allseqs(Main *bmain, Scene *scene)
1052 {
1053         Scene *scene_iter;
1054
1055         /* when a scene is deleted: test all seqs */
1056         for (scene_iter = bmain->scene.first; scene_iter; scene_iter = scene_iter->id.next) {
1057                 if (scene_iter != scene && scene_iter->ed) {
1058                         BKE_sequencer_base_recursive_apply(&scene_iter->ed->seqbase, clear_scene_in_allseqs_cb, scene);
1059                 }
1060         }
1061 }
1062
1063 typedef struct SeqUniqueInfo {
1064         Sequence *seq;
1065         char name_src[SEQ_NAME_MAXSTR];
1066         char name_dest[SEQ_NAME_MAXSTR];
1067         int count;
1068         int match;
1069 } SeqUniqueInfo;
1070
1071 static void seqbase_unique_name(ListBase *seqbasep, SeqUniqueInfo *sui)
1072 {
1073         Sequence *seq;
1074         for (seq = seqbasep->first; seq; seq = seq->next) {
1075                 if ((sui->seq != seq) && STREQ(sui->name_dest, seq->name + 2)) {
1076                         /* SEQ_NAME_MAXSTR -4 for the number, -1 for \0, - 2 for prefix */
1077                         BLI_snprintf(sui->name_dest, sizeof(sui->name_dest), "%.*s.%03d", SEQ_NAME_MAXSTR - 4 - 1 - 2,
1078                                      sui->name_src, sui->count++);
1079                         sui->match = 1; /* be sure to re-scan */
1080                 }
1081         }
1082 }
1083
1084 static int seqbase_unique_name_recursive_cb(Sequence *seq, void *arg_pt)
1085 {
1086         if (seq->seqbase.first)
1087                 seqbase_unique_name(&seq->seqbase, (SeqUniqueInfo *)arg_pt);
1088         return 1;
1089 }
1090
1091 void BKE_sequence_base_unique_name_recursive(ListBase *seqbasep, Sequence *seq)
1092 {
1093         SeqUniqueInfo sui;
1094         char *dot;
1095         sui.seq = seq;
1096         BLI_strncpy(sui.name_src, seq->name + 2, sizeof(sui.name_src));
1097         BLI_strncpy(sui.name_dest, seq->name + 2, sizeof(sui.name_dest));
1098
1099         sui.count = 1;
1100         sui.match = 1; /* assume the worst to start the loop */
1101
1102         /* Strip off the suffix */
1103         if ((dot = strrchr(sui.name_src, '.'))) {
1104                 *dot = '\0';
1105                 dot++;
1106
1107                 if (*dot)
1108                         sui.count = atoi(dot) + 1;
1109         }
1110
1111         while (sui.match) {
1112                 sui.match = 0;
1113                 seqbase_unique_name(seqbasep, &sui);
1114                 BKE_sequencer_base_recursive_apply(seqbasep, seqbase_unique_name_recursive_cb, &sui);
1115         }
1116
1117         BLI_strncpy(seq->name + 2, sui.name_dest, sizeof(seq->name) - 2);
1118 }
1119
1120 static const char *give_seqname_by_type(int type)
1121 {
1122         switch (type) {
1123                 case SEQ_TYPE_META:          return "Meta";
1124                 case SEQ_TYPE_IMAGE:         return "Image";
1125                 case SEQ_TYPE_SCENE:         return "Scene";
1126                 case SEQ_TYPE_MOVIE:         return "Movie";
1127                 case SEQ_TYPE_MOVIECLIP:     return "Clip";
1128                 case SEQ_TYPE_MASK:          return "Mask";
1129                 case SEQ_TYPE_SOUND_RAM:     return "Audio";
1130                 case SEQ_TYPE_CROSS:         return "Cross";
1131                 case SEQ_TYPE_GAMCROSS:      return "Gamma Cross";
1132                 case SEQ_TYPE_ADD:           return "Add";
1133                 case SEQ_TYPE_SUB:           return "Sub";
1134                 case SEQ_TYPE_MUL:           return "Mul";
1135                 case SEQ_TYPE_ALPHAOVER:     return "Alpha Over";
1136                 case SEQ_TYPE_ALPHAUNDER:    return "Alpha Under";
1137                 case SEQ_TYPE_OVERDROP:      return "Over Drop";
1138                 case SEQ_TYPE_WIPE:          return "Wipe";
1139                 case SEQ_TYPE_GLOW:          return "Glow";
1140                 case SEQ_TYPE_TRANSFORM:     return "Transform";
1141                 case SEQ_TYPE_COLOR:         return "Color";
1142                 case SEQ_TYPE_MULTICAM:      return "Multicam";
1143                 case SEQ_TYPE_ADJUSTMENT:    return "Adjustment";
1144                 case SEQ_TYPE_SPEED:         return "Speed";
1145                 case SEQ_TYPE_GAUSSIAN_BLUR: return "Gaussian Blur";
1146                 case SEQ_TYPE_TEXT:          return "Text";
1147                 default:
1148                         return NULL;
1149         }
1150 }
1151
1152 const char *BKE_sequence_give_name(Sequence *seq)
1153 {
1154         const char *name = give_seqname_by_type(seq->type);
1155
1156         if (!name) {
1157                 if (!(seq->type & SEQ_TYPE_EFFECT)) {
1158                         return seq->strip->dir;
1159                 }
1160                 else {
1161                         return "Effect";
1162                 }
1163         }
1164         return name;
1165 }
1166
1167 ListBase *BKE_sequence_seqbase_get(Sequence *seq, int *r_offset)
1168 {
1169         ListBase *seqbase = NULL;
1170
1171         switch (seq->type) {
1172                 case SEQ_TYPE_META:
1173                 {
1174                         seqbase = &seq->seqbase;
1175                         *r_offset = seq->start;
1176                         break;
1177                 }
1178                 case SEQ_TYPE_SCENE:
1179                 {
1180                         if (seq->flag & SEQ_SCENE_STRIPS) {
1181                                 Editing *ed = BKE_sequencer_editing_get(seq->scene, false);
1182                                 if (ed) {
1183                                         seqbase = &ed->seqbase;
1184                                         *r_offset = seq->scene->r.sfra;
1185                                 }
1186                         }
1187                         break;
1188                 }
1189         }
1190
1191         return seqbase;
1192 }
1193
1194 /*********************** DO THE SEQUENCE *************************/
1195
1196 static void make_black_ibuf(ImBuf *ibuf)
1197 {
1198         unsigned int *rect;
1199         float *rect_float;
1200         int tot;
1201
1202         if (ibuf == NULL || (ibuf->rect == NULL && ibuf->rect_float == NULL)) {
1203                 return;
1204         }
1205
1206         tot = ibuf->x * ibuf->y;
1207
1208         rect = ibuf->rect;
1209         rect_float = ibuf->rect_float;
1210
1211         if (rect) {
1212                 memset(rect, 0, tot * sizeof(char) * 4);
1213         }
1214
1215         if (rect_float) {
1216                 memset(rect_float, 0, tot * sizeof(float) * 4);
1217         }
1218 }
1219
1220 static void multibuf(ImBuf *ibuf, const float fmul)
1221 {
1222         char *rt;
1223         float *rt_float;
1224
1225         int a;
1226
1227         rt = (char *)ibuf->rect;
1228         rt_float = ibuf->rect_float;
1229
1230         if (rt) {
1231                 const int imul = (int)(256.0f * fmul);
1232                 a = ibuf->x * ibuf->y;
1233                 while (a--) {
1234                         rt[0] = min_ii((imul * rt[0]) >> 8, 255);
1235                         rt[1] = min_ii((imul * rt[1]) >> 8, 255);
1236                         rt[2] = min_ii((imul * rt[2]) >> 8, 255);
1237                         rt[3] = min_ii((imul * rt[3]) >> 8, 255);
1238
1239                         rt += 4;
1240                 }
1241         }
1242         if (rt_float) {
1243                 a = ibuf->x * ibuf->y;
1244                 while (a--) {
1245                         rt_float[0] *= fmul;
1246                         rt_float[1] *= fmul;
1247                         rt_float[2] *= fmul;
1248                         rt_float[3] *= fmul;
1249                         
1250                         rt_float += 4;
1251                 }
1252         }
1253 }
1254
1255 static float give_stripelem_index(Sequence *seq, float cfra)
1256 {
1257         float nr;
1258         int sta = seq->start;
1259         int end = seq->start + seq->len - 1;
1260
1261         if (seq->type & SEQ_TYPE_EFFECT) {
1262                 end = seq->enddisp;
1263         }
1264
1265         if (end < sta) {
1266                 return -1;
1267         }
1268
1269         if (seq->flag & SEQ_REVERSE_FRAMES) {
1270                 /*reverse frame in this sequence */
1271                 if (cfra <= sta) nr = end - sta;
1272                 else if (cfra >= end) nr = 0;
1273                 else nr = end - cfra;
1274         }
1275         else {
1276                 if (cfra <= sta) nr = 0;
1277                 else if (cfra >= end) nr = end - sta;
1278                 else nr = cfra - sta;
1279         }
1280         
1281         if (seq->strobe < 1.0f) seq->strobe = 1.0f;
1282         
1283         if (seq->strobe > 1.0f) {
1284                 nr -= fmodf((double)nr, (double)seq->strobe);
1285         }
1286
1287         return nr;
1288 }
1289
1290 StripElem *BKE_sequencer_give_stripelem(Sequence *seq, int cfra)
1291 {
1292         StripElem *se = seq->strip->stripdata;
1293
1294         if (seq->type == SEQ_TYPE_IMAGE) {
1295                 /* only IMAGE strips use the whole array, MOVIE strips use only the first element,
1296                  * all other strips don't use this...
1297                  */
1298
1299                 int nr = (int) give_stripelem_index(seq, cfra);
1300
1301                 if (nr == -1 || se == NULL)
1302                         return NULL;
1303         
1304                 se += nr + seq->anim_startofs;
1305         }
1306         return se;
1307 }
1308
1309 static int evaluate_seq_frame_gen(Sequence **seq_arr, ListBase *seqbase, int cfra, int chanshown)
1310 {
1311         /* Use arbitrary sized linked list, the size could be over MAXSEQ. */
1312         LinkNodePair effect_inputs = {NULL, NULL};
1313         int totseq = 0;
1314
1315         memset(seq_arr, 0, sizeof(Sequence *) * (MAXSEQ + 1));
1316
1317         for (Sequence *seq = seqbase->first; seq; seq = seq->next) {
1318                 if ((seq->startdisp <= cfra) && (seq->enddisp > cfra)) {
1319                         if ((seq->type & SEQ_TYPE_EFFECT) && !(seq->flag & SEQ_MUTE)) {
1320
1321                                 if (seq->seq1) {
1322                                         BLI_linklist_append_alloca(&effect_inputs, seq->seq1);
1323                                 }
1324
1325                                 if (seq->seq2) {
1326                                         BLI_linklist_append_alloca(&effect_inputs, seq->seq2);
1327                                 }
1328
1329                                 if (seq->seq3) {
1330                                         BLI_linklist_append_alloca(&effect_inputs, seq->seq3);
1331                                 }
1332                         }
1333
1334                         seq_arr[seq->machine] = seq;
1335                         totseq++;
1336                 }
1337         }
1338
1339         /* Drop strips which are used for effect inputs, we don't want
1340          * them to blend into render stack in any other way than effect
1341          * string rendering.
1342          */
1343         for (LinkNode *seq_item = effect_inputs.list; seq_item; seq_item = seq_item->next) {
1344                 Sequence *seq = seq_item->link;
1345                 /* It's possible that effetc strip would be placed to the same
1346                  * 'machine' as it's inputs. We don't want to clear such strips
1347                  * from the stack.
1348                  */
1349                 if (seq_arr[seq->machine] && seq_arr[seq->machine]->type & SEQ_TYPE_EFFECT) {
1350                         continue;
1351                 }
1352                 /* If we're shown a specified channel, then we want to see the stirps
1353                  * which belongs to this machine.
1354                  */
1355                 if (chanshown != 0 && chanshown <= seq->machine) {
1356                         continue;
1357                 }
1358                 seq_arr[seq->machine] = NULL;
1359         }
1360
1361         return totseq;
1362 }
1363
1364 int BKE_sequencer_evaluate_frame(Scene *scene, int cfra)
1365 {
1366         Editing *ed = BKE_sequencer_editing_get(scene, false);
1367         Sequence *seq_arr[MAXSEQ + 1];
1368
1369         if (ed == NULL)
1370                 return 0;
1371
1372         return evaluate_seq_frame_gen(seq_arr, ed->seqbasep, cfra, 0);
1373 }
1374
1375 static bool video_seq_is_rendered(Sequence *seq)
1376 {
1377         return (seq && !(seq->flag & SEQ_MUTE) && seq->type != SEQ_TYPE_SOUND_RAM);
1378 }
1379
1380 static int get_shown_sequences(ListBase *seqbasep, int cfra, int chanshown, Sequence **seq_arr_out)
1381 {
1382         Sequence *seq_arr[MAXSEQ + 1];
1383         int b = chanshown;
1384         int cnt = 0;
1385
1386         if (b > MAXSEQ) {
1387                 return 0;
1388         }
1389
1390         if (evaluate_seq_frame_gen(seq_arr, seqbasep, cfra, chanshown)) {
1391                 if (b == 0) {
1392                         b = MAXSEQ;
1393                 }
1394                 for (; b > 0; b--) {
1395                         if (video_seq_is_rendered(seq_arr[b])) {
1396                                 break;
1397                         }
1398                 }
1399         }
1400         
1401         chanshown = b;
1402
1403         for (; b > 0; b--) {
1404                 if (video_seq_is_rendered(seq_arr[b])) {
1405                         if (seq_arr[b]->blend_mode == SEQ_BLEND_REPLACE) {
1406                                 break;
1407                         }
1408                 }
1409         }
1410
1411         for (; b <= chanshown && b >= 0; b++) {
1412                 if (video_seq_is_rendered(seq_arr[b])) {
1413                         seq_arr_out[cnt++] = seq_arr[b];
1414                 }
1415         }
1416
1417         return cnt;
1418 }
1419
1420 /*********************** proxy management *************************/
1421
1422 typedef struct SeqIndexBuildContext {
1423         struct IndexBuildContext *index_context;
1424
1425         int tc_flags;
1426         int size_flags;
1427         int quality;
1428         bool overwrite;
1429         int view_id;
1430
1431         Main *bmain;
1432         Scene *scene;
1433         Sequence *seq, *orig_seq;
1434 } SeqIndexBuildContext;
1435
1436 #define PROXY_MAXFILE (2 * FILE_MAXDIR + FILE_MAXFILE)
1437
1438 static IMB_Proxy_Size seq_rendersize_to_proxysize(int size)
1439 {
1440         if (size >= 100) {
1441                 return IMB_PROXY_NONE;
1442         }
1443         if (size >= 99) {
1444                 return IMB_PROXY_100;
1445         }
1446         if (size >= 75) {
1447                 return IMB_PROXY_75;
1448         }
1449         if (size >= 50) {
1450                 return IMB_PROXY_50;
1451         }
1452         return IMB_PROXY_25;
1453 }
1454
1455 static double seq_rendersize_to_scale_factor(int size)
1456 {
1457         if (size >= 99) {
1458                 return 1.0;
1459         }
1460         if (size >= 75) {
1461                 return 0.75;
1462         }
1463         if (size >= 50) {
1464                 return 0.50;
1465         }
1466         return 0.25;
1467 }
1468
1469 /* the number of files will vary according to the stereo format */
1470 static int seq_num_files(Scene *scene, char views_format, const bool is_multiview)
1471 {
1472         if (!is_multiview) {
1473                 return 1;
1474         }
1475         else if (views_format == R_IMF_VIEWS_STEREO_3D) {
1476                 return 1;
1477         }
1478         /* R_IMF_VIEWS_INDIVIDUAL */
1479         else {
1480                 return BKE_scene_multiview_num_views_get(&scene->r);
1481         }
1482 }
1483
1484 static void seq_proxy_index_dir_set(struct anim *anim, const char *base_dir)
1485 {
1486         char dir[FILE_MAX];
1487         char fname[FILE_MAXFILE];
1488
1489         IMB_anim_get_fname(anim, fname, FILE_MAXFILE);
1490         BLI_strncpy(dir, base_dir, sizeof(dir));
1491         BLI_path_append(dir, sizeof(dir), fname);
1492         IMB_anim_set_index_dir(anim, dir);
1493 }
1494
1495 static void seq_open_anim_file(Scene *scene, Sequence *seq, bool openfile)
1496 {
1497         char dir[FILE_MAX];
1498         char name[FILE_MAX];
1499         StripProxy *proxy;
1500         bool use_proxy;
1501         bool is_multiview_loaded = false;
1502         Editing *ed = scene->ed;
1503         const bool is_multiview = (seq->flag & SEQ_USE_VIEWS) != 0 && (scene->r.scemode & R_MULTIVIEW) != 0;
1504
1505         if ((seq->anims.first != NULL) && (((StripAnim *)seq->anims.first)->anim != NULL)) {
1506                 return;
1507         }
1508
1509         /* reset all the previously created anims */
1510         BKE_sequence_free_anim(seq);
1511
1512         BLI_join_dirfile(name, sizeof(name),
1513                          seq->strip->dir, seq->strip->stripdata->name);
1514         BLI_path_abs(name, G.main->name);
1515
1516         proxy = seq->strip->proxy;
1517
1518         use_proxy = proxy && ((proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_DIR) != 0 ||
1519                               (ed->proxy_storage == SEQ_EDIT_PROXY_DIR_STORAGE));
1520
1521         if (use_proxy) {
1522                 if (ed->proxy_storage == SEQ_EDIT_PROXY_DIR_STORAGE) {
1523                         if (ed->proxy_dir[0] == 0)
1524                                 BLI_strncpy(dir, "//BL_proxy", sizeof(dir));
1525                         else
1526                                 BLI_strncpy(dir, ed->proxy_dir, sizeof(dir));
1527                 }
1528                 else {
1529                         BLI_strncpy(dir, seq->strip->proxy->dir, sizeof(dir));
1530                 }
1531                 BLI_path_abs(dir, G.main->name);
1532         }
1533
1534         if (is_multiview && seq->views_format == R_IMF_VIEWS_INDIVIDUAL) {
1535                 int totfiles = seq_num_files(scene, seq->views_format, true);
1536                 char prefix[FILE_MAX];
1537                 const char *ext = NULL;
1538                 int i;
1539
1540                 BKE_scene_multiview_view_prefix_get(scene, name, prefix, &ext);
1541
1542                 if (prefix[0] != '\0') {
1543                         for (i = 0; i < totfiles; i++) {
1544                                 const char *suffix = BKE_scene_multiview_view_id_suffix_get(&scene->r, i);
1545                                 char str[FILE_MAX];
1546                                 StripAnim *sanim = MEM_mallocN(sizeof(StripAnim), "Strip Anim");
1547
1548                                 BLI_addtail(&seq->anims, sanim);
1549
1550                                 BLI_snprintf(str, sizeof(str), "%s%s%s", prefix, suffix, ext);
1551
1552                                 if (openfile) {
1553                                         sanim->anim = openanim(
1554                                                 str, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
1555                                                 seq->streamindex, seq->strip->colorspace_settings.name);
1556                                 }
1557                                 else {
1558                                         sanim->anim = openanim_noload(
1559                                                 str, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
1560                                                 seq->streamindex, seq->strip->colorspace_settings.name);
1561                                 }
1562
1563                                 if (sanim->anim) {
1564 #if 0
1565                                         seq_anim_add_suffix(scene, sanim->anim, i);
1566 #else
1567                                         /* we already have the suffix */
1568                                         IMB_suffix_anim(sanim->anim, suffix);
1569 #endif
1570                                 }
1571                                 else {
1572                                         if (openfile) {
1573                                                 sanim->anim = openanim(
1574                                                         name, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
1575                                                         seq->streamindex, seq->strip->colorspace_settings.name);
1576                                         }
1577                                         else {
1578                                                 sanim->anim = openanim_noload(
1579                                                         name, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
1580                                                         seq->streamindex, seq->strip->colorspace_settings.name);
1581                                         }
1582
1583                                         /* no individual view files - monoscopic, stereo 3d or exr multiview */
1584                                         totfiles = 1;
1585                                 }
1586
1587                                 if (sanim->anim && use_proxy) {
1588                                         seq_proxy_index_dir_set(sanim->anim, dir);
1589                                 }
1590                         }
1591                         is_multiview_loaded = true;
1592                 }
1593         }
1594
1595         if (is_multiview_loaded == false) {
1596                 StripAnim *sanim;
1597
1598                 sanim = MEM_mallocN(sizeof(StripAnim), "Strip Anim");
1599                 BLI_addtail(&seq->anims, sanim);
1600
1601                 if (openfile) {
1602                         sanim->anim = openanim(
1603                                 name, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
1604                                 seq->streamindex, seq->strip->colorspace_settings.name);
1605                 }
1606                 else {
1607                         sanim->anim = openanim_noload(
1608                                 name, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
1609                                 seq->streamindex, seq->strip->colorspace_settings.name);
1610                 }
1611
1612                 if (sanim->anim && use_proxy) {
1613                         seq_proxy_index_dir_set(sanim->anim, dir);
1614                 }
1615         }
1616 }
1617
1618 static bool seq_proxy_get_fname(Editing *ed, Sequence *seq, int cfra, int render_size, char *name, const int view_id)
1619 {
1620         int frameno;
1621         char dir[PROXY_MAXFILE];
1622         StripAnim *sanim;
1623         char suffix[24] = {'\0'};
1624
1625         StripProxy *proxy = seq->strip->proxy;
1626         if (!proxy) {
1627                 return false;
1628         }
1629
1630         /* MOVIE tracks (only exception: custom files) are now handled 
1631          * internally by ImBuf module for various reasons: proper time code
1632          * support, quicker index build, using one file instead
1633          * of a full directory of jpeg files, etc. Trying to support old
1634          * and new method at once could lead to funny effects, if people
1635          * have both, a directory full of jpeg files and proxy avis, so
1636          * sorry folks, please rebuild your proxies... */
1637
1638         sanim = BLI_findlink(&seq->anims, view_id);
1639
1640         if (ed->proxy_storage == SEQ_EDIT_PROXY_DIR_STORAGE) {
1641                 char fname[FILE_MAXFILE];
1642                 if (ed->proxy_dir[0] == 0)
1643                         BLI_strncpy(dir, "//BL_proxy", sizeof(dir));
1644                 else
1645                         BLI_strncpy(dir, ed->proxy_dir, sizeof(dir));
1646
1647                 if (sanim && sanim->anim) {
1648                         IMB_anim_get_fname(sanim->anim, fname, FILE_MAXFILE);
1649                 }
1650                 else if (seq->type == SEQ_TYPE_IMAGE) {
1651                         fname[0] = 0;
1652                 }
1653                 BLI_path_append(dir, sizeof(dir), fname);
1654                 BLI_path_abs(name, G.main->name);
1655         }
1656         else if ((proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_DIR) && (proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_FILE)) {
1657                 BLI_strncpy(dir, seq->strip->proxy->dir, sizeof(dir));
1658         }
1659         else if (sanim && sanim->anim && (proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_DIR)) {
1660                 char fname[FILE_MAXFILE];
1661                 BLI_strncpy(dir, seq->strip->proxy->dir, sizeof(dir));
1662                 IMB_anim_get_fname(sanim->anim, fname, FILE_MAXFILE);
1663                 BLI_path_append(dir, sizeof(dir), fname);
1664         }
1665         else if (seq->type == SEQ_TYPE_IMAGE) {
1666                 if (proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_DIR)
1667                         BLI_strncpy(dir, seq->strip->proxy->dir, sizeof(dir));
1668                 else
1669                         BLI_snprintf(dir, PROXY_MAXFILE, "%s/BL_proxy", seq->strip->dir);
1670         }
1671         else {
1672                 return false;
1673         }
1674
1675         if (view_id > 0)
1676                 BLI_snprintf(suffix, sizeof(suffix), "_%d", view_id);
1677
1678         if (proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_FILE && sanim && sanim->anim &&
1679             ed->proxy_storage != SEQ_EDIT_PROXY_DIR_STORAGE)
1680         {
1681                 BLI_join_dirfile(name, PROXY_MAXFILE,
1682                                  dir, proxy->file);
1683                 BLI_path_abs(name, G.main->name);
1684                 BLI_snprintf(name, PROXY_MAXFILE, "%s_%s", name, suffix);
1685
1686                 return true;
1687         }
1688
1689         /* generate a separate proxy directory for each preview size */
1690
1691         if (seq->type == SEQ_TYPE_IMAGE) {
1692                 BLI_snprintf(name, PROXY_MAXFILE, "%s/images/%d/%s_proxy%s", dir, render_size,
1693                              BKE_sequencer_give_stripelem(seq, cfra)->name, suffix);
1694                 frameno = 1;
1695         }
1696         else {
1697                 frameno = (int)give_stripelem_index(seq, cfra) + seq->anim_startofs;
1698                 BLI_snprintf(name, PROXY_MAXFILE, "%s/proxy_misc/%d/####%s", dir, render_size, suffix);
1699         }
1700
1701         BLI_path_abs(name, G.main->name);
1702         BLI_path_frame(name, frameno, 0);
1703
1704         strcat(name, ".jpg");
1705
1706         return true;
1707 }
1708
1709 static ImBuf *seq_proxy_fetch(const SeqRenderData *context, Sequence *seq, int cfra)
1710 {
1711         char name[PROXY_MAXFILE];
1712         IMB_Proxy_Size psize = seq_rendersize_to_proxysize(context->preview_render_size);
1713         int size_flags;
1714         int render_size = context->preview_render_size;
1715         StripProxy *proxy = seq->strip->proxy;
1716         Editing *ed = context->scene->ed;
1717         StripAnim *sanim;
1718
1719         if (!(seq->flag & SEQ_USE_PROXY)) {
1720                 return NULL;
1721         }
1722
1723         /* dirty hack to distinguish 100% render size from PROXY_100 */
1724         if (render_size == 99) {
1725                 render_size = 100;
1726         }
1727
1728         size_flags = proxy->build_size_flags;
1729
1730         /* only use proxies, if they are enabled (even if present!) */
1731         if (psize == IMB_PROXY_NONE || ((size_flags & psize) != psize)) {
1732                 return NULL;
1733         }
1734
1735         if (proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_FILE) {
1736                 int frameno = (int)give_stripelem_index(seq, cfra) + seq->anim_startofs;
1737                 if (proxy->anim == NULL) {
1738                         if (seq_proxy_get_fname(ed, seq, cfra, render_size, name, context->view_id) == 0) {
1739                                 return NULL;
1740                         }
1741
1742                         proxy->anim = openanim(name, IB_rect, 0, seq->strip->colorspace_settings.name);
1743                 }
1744                 if (proxy->anim == NULL) {
1745                         return NULL;
1746                 }
1747  
1748                 seq_open_anim_file(context->scene, seq, true);
1749                 sanim = seq->anims.first;
1750
1751                 frameno = IMB_anim_index_get_frame_index(sanim ? sanim->anim : NULL, seq->strip->proxy->tc, frameno);
1752
1753                 return IMB_anim_absolute(proxy->anim, frameno, IMB_TC_NONE, IMB_PROXY_NONE);
1754         }
1755  
1756         if (seq_proxy_get_fname(ed, seq, cfra, render_size, name, context->view_id) == 0) {
1757                 return NULL;
1758         }
1759
1760         if (BLI_exists(name)) {
1761                 ImBuf *ibuf = IMB_loadiffname(name, IB_rect, NULL);
1762
1763                 if (ibuf)
1764                         sequencer_imbuf_assign_spaces(context->scene, ibuf);
1765
1766                 return ibuf;
1767         }
1768         else {
1769                 return NULL;
1770         }
1771 }
1772
1773 static void seq_proxy_build_frame(
1774         const SeqRenderData *context, SeqRenderState *state,
1775         Sequence *seq, int cfra,
1776         int proxy_render_size, const bool overwrite)
1777 {
1778         char name[PROXY_MAXFILE];
1779         int quality;
1780         int rectx, recty;
1781         int ok;
1782         ImBuf *ibuf_tmp, *ibuf;
1783         Editing *ed = context->scene->ed;
1784
1785         if (!seq_proxy_get_fname(ed, seq, cfra, proxy_render_size, name, context->view_id)) {
1786                 return;
1787         }
1788
1789         if (!overwrite && BLI_exists(name)) {
1790                 return;
1791         }
1792
1793         ibuf_tmp = seq_render_strip(context, state, seq, cfra);
1794
1795         rectx = (proxy_render_size * ibuf_tmp->x) / 100;
1796         recty = (proxy_render_size * ibuf_tmp->y) / 100;
1797
1798         if (ibuf_tmp->x != rectx || ibuf_tmp->y != recty) {
1799                 ibuf = IMB_dupImBuf(ibuf_tmp);
1800                 IMB_metadata_copy(ibuf, ibuf_tmp);
1801                 IMB_freeImBuf(ibuf_tmp);
1802                 IMB_scalefastImBuf(ibuf, (short)rectx, (short)recty);
1803         }
1804         else {
1805                 ibuf = ibuf_tmp;
1806         }
1807
1808         /* depth = 32 is intentionally left in, otherwise ALPHA channels
1809          * won't work... */
1810         quality = seq->strip->proxy->quality;
1811         ibuf->ftype = IMB_FTYPE_JPG;
1812         ibuf->foptions.quality = quality;
1813
1814         /* unsupported feature only confuses other s/w */
1815         if (ibuf->planes == 32)
1816                 ibuf->planes = 24;
1817
1818         BLI_make_existing_file(name);
1819
1820         ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
1821         if (ok == 0) {
1822                 perror(name);
1823         }
1824
1825         IMB_freeImBuf(ibuf);
1826 }
1827
1828 /**
1829  * Returns whether the file this context would read from even exist,
1830  * if not, don't create the context
1831  */
1832 static bool seq_proxy_multiview_context_invalid(Sequence *seq, Scene *scene, const int view_id)
1833 {
1834         if ((scene->r.scemode & R_MULTIVIEW) == 0)
1835                 return false;
1836
1837         if ((seq->type == SEQ_TYPE_IMAGE) && (seq->views_format == R_IMF_VIEWS_INDIVIDUAL)) {
1838                 static char prefix[FILE_MAX];
1839                 static const char *ext = NULL;
1840                 char str[FILE_MAX];
1841
1842                 if (view_id == 0) {
1843                         char path[FILE_MAX];
1844                         BLI_join_dirfile(path, sizeof(path), seq->strip->dir,
1845                                          seq->strip->stripdata->name);
1846                         BLI_path_abs(path, G.main->name);
1847                         BKE_scene_multiview_view_prefix_get(scene, path, prefix, &ext);
1848                 }
1849                 else {
1850                         prefix[0] = '\0';
1851                 }
1852
1853                 if (prefix[0] == '\0')
1854                         return view_id != 0;
1855
1856                 seq_multiview_name(scene, view_id, prefix, ext, str, FILE_MAX);
1857
1858                 if (BLI_access(str, R_OK) == 0)
1859                         return false;
1860                 else
1861                         return view_id != 0;
1862         }
1863         return false;
1864 }
1865
1866 /**
1867  * This returns the maximum possible number of required contexts
1868  */
1869 static int seq_proxy_context_count(Sequence *seq, Scene *scene)
1870 {
1871         int num_views = 1;
1872
1873         if ((scene->r.scemode & R_MULTIVIEW) == 0)
1874                 return 1;
1875
1876         switch (seq->type) {
1877                 case SEQ_TYPE_MOVIE:
1878                 {
1879                         num_views = BLI_listbase_count(&seq->anims);
1880                         break;
1881                 }
1882                 case SEQ_TYPE_IMAGE:
1883                 {
1884                         switch (seq->views_format) {
1885                                 case R_IMF_VIEWS_INDIVIDUAL:
1886                                         num_views = BKE_scene_multiview_num_views_get(&scene->r);
1887                                         break;
1888                                 case R_IMF_VIEWS_STEREO_3D:
1889                                         num_views = 2;
1890                                         break;
1891                                 case R_IMF_VIEWS_MULTIVIEW:
1892                                         /* not supported at the moment */
1893                                         /* pass through */
1894                                 default:
1895                                         num_views = 1;
1896                         }
1897                         break;
1898                 }
1899         }
1900
1901         return num_views;
1902 }
1903
1904 void BKE_sequencer_proxy_rebuild_context(Main *bmain, Scene *scene, Sequence *seq, struct GSet *file_list, ListBase *queue)
1905 {
1906         SeqIndexBuildContext *context;
1907         Sequence *nseq;
1908         LinkData *link;
1909         int num_files;
1910         int i;
1911
1912         if (!seq->strip || !seq->strip->proxy) {
1913                 return;
1914         }
1915
1916         if (!(seq->flag & SEQ_USE_PROXY)) {
1917                 return;
1918         }
1919
1920         num_files = seq_proxy_context_count(seq, scene);
1921
1922         for (i = 0; i < num_files; i++) {
1923                 if (seq_proxy_multiview_context_invalid(seq, scene, i))
1924                         continue;
1925
1926                 context = MEM_callocN(sizeof(SeqIndexBuildContext), "seq proxy rebuild context");
1927
1928                 nseq = BKE_sequence_dupli_recursive(scene, scene, seq, 0);
1929
1930                 context->tc_flags   = nseq->strip->proxy->build_tc_flags;
1931                 context->size_flags = nseq->strip->proxy->build_size_flags;
1932                 context->quality    = nseq->strip->proxy->quality;
1933                 context->overwrite = (nseq->strip->proxy->build_flags & SEQ_PROXY_SKIP_EXISTING) == 0;
1934
1935                 context->bmain = bmain;
1936                 context->scene = scene;
1937                 context->orig_seq = seq;
1938                 context->seq = nseq;
1939
1940                 context->view_id = i; /* only for images */
1941
1942                 link = BLI_genericNodeN(context);
1943                 BLI_addtail(queue, link);
1944
1945                 if (nseq->type == SEQ_TYPE_MOVIE) {
1946                         StripAnim *sanim;
1947
1948                         seq_open_anim_file(scene, nseq, true);
1949                         sanim = BLI_findlink(&nseq->anims,  i);
1950
1951                         if (sanim->anim) {
1952                                 context->index_context = IMB_anim_index_rebuild_context(sanim->anim,
1953                                         context->tc_flags, context->size_flags, context->quality,
1954                                         context->overwrite, file_list);
1955                         }
1956                 }
1957         }
1958 }
1959
1960 void BKE_sequencer_proxy_rebuild(SeqIndexBuildContext *context, short *stop, short *do_update, float *progress)
1961 {
1962         const bool overwrite = context->overwrite;
1963         SeqRenderData render_context;
1964         Sequence *seq = context->seq;
1965         Scene *scene = context->scene;
1966         Main *bmain = context->bmain;
1967         int cfra;
1968
1969         if (seq->type == SEQ_TYPE_MOVIE) {
1970                 if (context->index_context) {
1971                         IMB_anim_index_rebuild(context->index_context, stop, do_update, progress);
1972                 }
1973
1974                 return;
1975         }
1976
1977         if (!(seq->flag & SEQ_USE_PROXY)) {
1978                 return;
1979         }
1980
1981         /* that's why it is called custom... */
1982         if (seq->strip->proxy && seq->strip->proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_FILE) {
1983                 return;
1984         }
1985
1986         /* fail safe code */
1987
1988         BKE_sequencer_new_render_data(
1989                 bmain->eval_ctx, bmain, context->scene,
1990                 (scene->r.size * (float) scene->r.xsch) / 100.0f + 0.5f,
1991                 (scene->r.size * (float) scene->r.ysch) / 100.0f + 0.5f, 100,
1992                 &render_context);
1993
1994         render_context.skip_cache = true;
1995         render_context.is_proxy_render = true;
1996         render_context.view_id = context->view_id;
1997
1998         SeqRenderState state;
1999         sequencer_state_init(&state);
2000
2001         for (cfra = seq->startdisp + seq->startstill;  cfra < seq->enddisp - seq->endstill; cfra++) {
2002                 if (context->size_flags & IMB_PROXY_25) {
2003                         seq_proxy_build_frame(&render_context, &state, seq, cfra, 25, overwrite);
2004                 }
2005                 if (context->size_flags & IMB_PROXY_50) {
2006                         seq_proxy_build_frame(&render_context, &state, seq, cfra, 50, overwrite);
2007                 }
2008                 if (context->size_flags & IMB_PROXY_75) {
2009                         seq_proxy_build_frame(&render_context, &state, seq, cfra, 75, overwrite);
2010                 }
2011                 if (context->size_flags & IMB_PROXY_100) {
2012                         seq_proxy_build_frame(&render_context, &state, seq, cfra, 100, overwrite);
2013                 }
2014
2015                 *progress = (float) (cfra - seq->startdisp - seq->startstill) / (seq->enddisp - seq->endstill - seq->startdisp - seq->startstill);
2016                 *do_update = true;
2017
2018                 if (*stop || G.is_break)
2019                         break;
2020         }
2021 }
2022
2023 void BKE_sequencer_proxy_rebuild_finish(SeqIndexBuildContext *context, bool stop)
2024 {
2025         if (context->index_context) {
2026                 StripAnim *sanim;
2027
2028                 for (sanim = context->seq->anims.first; sanim; sanim = sanim->next)
2029                         IMB_close_anim_proxies(sanim->anim);
2030
2031                 for (sanim = context->orig_seq->anims.first; sanim; sanim = sanim->next)
2032                         IMB_close_anim_proxies(sanim->anim);
2033
2034                 IMB_anim_index_rebuild_finish(context->index_context, stop);
2035         }
2036
2037         seq_free_sequence_recurse(NULL, context->seq);
2038
2039         MEM_freeN(context);
2040 }
2041
2042 void BKE_sequencer_proxy_set(struct Sequence *seq, bool value)
2043 {
2044         if (value) {
2045                 seq->flag |= SEQ_USE_PROXY;
2046                 if (seq->strip->proxy == NULL) {
2047                         seq->strip->proxy = MEM_callocN(sizeof(struct StripProxy), "StripProxy");
2048                         seq->strip->proxy->quality = 90;
2049                         seq->strip->proxy->build_tc_flags = SEQ_PROXY_TC_ALL;
2050                         seq->strip->proxy->build_size_flags = SEQ_PROXY_IMAGE_SIZE_25;
2051                 }
2052         }
2053         else {
2054                 seq->flag ^= SEQ_USE_PROXY;
2055         }       
2056 }
2057
2058 /*********************** color balance *************************/
2059
2060 static StripColorBalance calc_cb(StripColorBalance *cb_)
2061 {
2062         StripColorBalance cb = *cb_;
2063         int c;
2064
2065         for (c = 0; c < 3; c++) {
2066                 cb.lift[c] = 2.0f - cb.lift[c];
2067         }
2068
2069         if (cb.flag & SEQ_COLOR_BALANCE_INVERSE_LIFT) {
2070                 for (c = 0; c < 3; c++) {
2071                         /* tweak to give more subtle results
2072                          * values above 1.0 are scaled */
2073                         if (cb.lift[c] > 1.0f)
2074                                 cb.lift[c] = pow(cb.lift[c] - 1.0f, 2.0) + 1.0;
2075
2076                         cb.lift[c] = 2.0f - cb.lift[c];
2077                 }
2078         }
2079
2080         if (cb.flag & SEQ_COLOR_BALANCE_INVERSE_GAIN) {
2081                 for (c = 0; c < 3; c++) {
2082                         if (cb.gain[c] != 0.0f) {
2083                                 cb.gain[c] = 1.0f / cb.gain[c];
2084                         }
2085                         else {
2086                                 cb.gain[c] = 1000000; /* should be enough :) */
2087                         }
2088                 }
2089         }
2090
2091         if (!(cb.flag & SEQ_COLOR_BALANCE_INVERSE_GAMMA)) {
2092                 for (c = 0; c < 3; c++) {
2093                         if (cb.gamma[c] != 0.0f) {
2094                                 cb.gamma[c] = 1.0f / cb.gamma[c];
2095                         }
2096                         else {
2097                                 cb.gamma[c] = 1000000; /* should be enough :) */
2098                         }
2099                 }
2100         }
2101
2102         return cb;
2103 }
2104
2105 /* note: lift is actually 2-lift */
2106 MINLINE float color_balance_fl(float in, const float lift, const float gain, const float gamma, const float mul)
2107 {
2108         float x = (((in - 1.0f) * lift) + 1.0f) * gain;
2109
2110         /* prevent NaN */
2111         if (x < 0.f)
2112                 x = 0.f;
2113
2114         return powf(x, gamma) * mul;
2115 }
2116
2117 static void make_cb_table_float(float lift, float gain, float gamma,
2118                                 float *table, float mul)
2119 {
2120         int y;
2121
2122         for (y = 0; y < 256; y++) {
2123                 float v = color_balance_fl((float)y * (1.0f / 255.0f), lift, gain, gamma, mul);
2124
2125                 table[y] = v;
2126         }
2127 }
2128
2129 static void color_balance_byte_byte(StripColorBalance *cb_, unsigned char *rect, unsigned char *mask_rect, int width, int height, float mul)
2130 {
2131         //unsigned char cb_tab[3][256];
2132         unsigned char *cp = rect;
2133         unsigned char *e = cp + width * 4 * height;
2134         unsigned char *m = mask_rect;
2135
2136         StripColorBalance cb = calc_cb(cb_);
2137
2138         while (cp < e) {
2139                 float p[4];
2140                 int c;
2141
2142                 straight_uchar_to_premul_float(p, cp);
2143
2144                 for (c = 0; c < 3; c++) {
2145                         float t = color_balance_fl(p[c], cb.lift[c], cb.gain[c], cb.gamma[c], mul);
2146
2147                         if (m) {
2148                                 float m_normal = (float) m[c] / 255.0f;
2149
2150                                 p[c] = p[c] * (1.0f - m_normal) + t * m_normal;
2151                         }
2152                         else
2153                                 p[c] = t;
2154                 }
2155                 
2156                 premul_float_to_straight_uchar(cp, p);
2157
2158                 cp += 4;
2159                 if (m)
2160                         m += 4;
2161         }
2162 }
2163
2164 static void color_balance_byte_float(StripColorBalance *cb_, unsigned char *rect, float *rect_float, unsigned char *mask_rect, int width, int height, float mul)
2165 {
2166         float cb_tab[4][256];
2167         int c, i;
2168         unsigned char *p = rect;
2169         unsigned char *e = p + width * 4 * height;
2170         unsigned char *m = mask_rect;
2171         float *o;
2172         StripColorBalance cb;
2173
2174         o = rect_float;
2175
2176         cb = calc_cb(cb_);
2177
2178         for (c = 0; c < 3; c++) {
2179                 make_cb_table_float(cb.lift[c], cb.gain[c], cb.gamma[c], cb_tab[c], mul);
2180         }
2181
2182         for (i = 0; i < 256; i++) {
2183                 cb_tab[3][i] = ((float)i) * (1.0f / 255.0f);
2184         }
2185
2186         while (p < e) {
2187                 if (m) {
2188                         float t[3] = {m[0] / 255.0f, m[1] / 255.0f, m[2] / 255.0f};
2189
2190                         p[0] = p[0] * (1.0f - t[0]) + t[0] * cb_tab[0][p[0]];
2191                         p[1] = p[1] * (1.0f - t[1]) + t[1] * cb_tab[1][p[1]];
2192                         p[2] = p[2] * (1.0f - t[2]) + t[2] * cb_tab[2][p[2]];
2193
2194                         m += 4;
2195                 }
2196                 else {
2197                         o[0] = cb_tab[0][p[0]];
2198                         o[1] = cb_tab[1][p[1]];
2199                         o[2] = cb_tab[2][p[2]];
2200                 }
2201
2202                 o[3] = cb_tab[3][p[3]];
2203
2204                 p += 4; o += 4;
2205         }
2206 }
2207
2208 static void color_balance_float_float(StripColorBalance *cb_, float *rect_float, float *mask_rect_float, int width, int height, float mul)
2209 {
2210         float *p = rect_float;
2211         const float *e = rect_float + width * 4 * height;
2212         const float *m = mask_rect_float;
2213         StripColorBalance cb = calc_cb(cb_);
2214
2215         while (p < e) {
2216                 int c;
2217                 for (c = 0; c < 3; c++) {
2218                         float t = color_balance_fl(p[c], cb.lift[c], cb.gain[c], cb.gamma[c], mul);
2219
2220                         if (m)
2221                                 p[c] = p[c] * (1.0f - m[c]) + t * m[c];
2222                         else
2223                                 p[c] = t;
2224                 }
2225
2226                 p += 4;
2227                 if (m)
2228                         m += 4;
2229         }
2230 }
2231
2232 typedef struct ColorBalanceInitData {
2233         StripColorBalance *cb;
2234         ImBuf *ibuf;
2235         float mul;
2236         ImBuf *mask;
2237         bool make_float;
2238 } ColorBalanceInitData;
2239
2240 typedef struct ColorBalanceThread {
2241         StripColorBalance *cb;
2242         float mul;
2243
2244         int width, height;
2245
2246         unsigned char *rect, *mask_rect;
2247         float *rect_float, *mask_rect_float;
2248
2249         bool make_float;
2250 } ColorBalanceThread;
2251
2252 static void color_balance_init_handle(void *handle_v, int start_line, int tot_line, void *init_data_v)
2253 {
2254         ColorBalanceThread *handle = (ColorBalanceThread *) handle_v;
2255         ColorBalanceInitData *init_data = (ColorBalanceInitData *) init_data_v;
2256         ImBuf *ibuf = init_data->ibuf;
2257         ImBuf *mask = init_data->mask;
2258
2259         int offset = 4 * start_line * ibuf->x;
2260
2261         memset(handle, 0, sizeof(ColorBalanceThread));
2262
2263         handle->cb = init_data->cb;
2264         handle->mul = init_data->mul;
2265         handle->width = ibuf->x;
2266         handle->height = tot_line;
2267         handle->make_float = init_data->make_float;
2268
2269         if (ibuf->rect)
2270                 handle->rect = (unsigned char *) ibuf->rect + offset;
2271
2272         if (ibuf->rect_float)
2273                 handle->rect_float = ibuf->rect_float + offset;
2274
2275         if (mask) {
2276                 if (mask->rect)
2277                         handle->mask_rect = (unsigned char *) mask->rect + offset;
2278
2279                 if (mask->rect_float)
2280                         handle->mask_rect_float = mask->rect_float + offset;
2281         }
2282         else {
2283                 handle->mask_rect = NULL;
2284                 handle->mask_rect_float = NULL;
2285         }
2286 }
2287
2288 static void *color_balance_do_thread(void *thread_data_v)
2289 {
2290         ColorBalanceThread *thread_data = (ColorBalanceThread *) thread_data_v;
2291         StripColorBalance *cb = thread_data->cb;
2292         int width = thread_data->width, height = thread_data->height;
2293         unsigned char *rect = thread_data->rect;
2294         unsigned char *mask_rect = thread_data->mask_rect;
2295         float *rect_float = thread_data->rect_float;
2296         float *mask_rect_float = thread_data->mask_rect_float;
2297         float mul = thread_data->mul;
2298
2299         if (rect_float) {
2300                 color_balance_float_float(cb, rect_float, mask_rect_float, width, height, mul);
2301         }
2302         else if (thread_data->make_float) {
2303                 color_balance_byte_float(cb, rect, rect_float, mask_rect, width, height, mul);
2304         }
2305         else {
2306                 color_balance_byte_byte(cb, rect, mask_rect, width, height, mul);
2307         }
2308
2309         return NULL;
2310 }
2311
2312 /* cfra is offset by fra_offset only in case we are using a real mask. */
2313 ImBuf *BKE_sequencer_render_mask_input(
2314         const SeqRenderData *context, int mask_input_type, Sequence *mask_sequence, Mask *mask_id,
2315         int cfra, int fra_offset, bool make_float)
2316 {
2317         ImBuf *mask_input = NULL;
2318
2319         if (mask_input_type == SEQUENCE_MASK_INPUT_STRIP) {
2320                 if (mask_sequence) {
2321                         SeqRenderState state;
2322                         sequencer_state_init(&state);
2323
2324                         mask_input = seq_render_strip(context, &state, mask_sequence, cfra);
2325
2326                         if (make_float) {
2327                                 if (!mask_input->rect_float)
2328                                         IMB_float_from_rect(mask_input);
2329                         }
2330                         else {
2331                                 if (!mask_input->rect)
2332                                         IMB_rect_from_float(mask_input);
2333                         }
2334                 }
2335         }
2336         else if (mask_input_type == SEQUENCE_MASK_INPUT_ID) {
2337                 mask_input = seq_render_mask(context, mask_id, cfra - fra_offset, make_float);
2338         }
2339
2340         return mask_input;
2341 }
2342
2343 void BKE_sequencer_color_balance_apply(StripColorBalance *cb, ImBuf *ibuf, float mul, bool make_float, ImBuf *mask_input)
2344 {
2345         ColorBalanceInitData init_data;
2346
2347         if (!ibuf->rect_float && make_float)
2348                 imb_addrectfloatImBuf(ibuf);
2349
2350         init_data.cb = cb;
2351         init_data.ibuf = ibuf;
2352         init_data.mul = mul;
2353         init_data.make_float = make_float;
2354         init_data.mask = mask_input;
2355
2356         IMB_processor_apply_threaded(ibuf->y, sizeof(ColorBalanceThread), &init_data,
2357                                      color_balance_init_handle, color_balance_do_thread);
2358
2359         /* color balance either happens on float buffer or byte buffer, but never on both,
2360          * free byte buffer if there's float buffer since float buffer would be used for
2361          * color balance in favor of byte buffer
2362          */
2363         if (ibuf->rect_float && ibuf->rect)
2364                 imb_freerectImBuf(ibuf);
2365 }
2366
2367 /*
2368  *  input preprocessing for SEQ_TYPE_IMAGE, SEQ_TYPE_MOVIE, SEQ_TYPE_MOVIECLIP and SEQ_TYPE_SCENE
2369  *
2370  *  Do all the things you can't really do afterwards using sequence effects
2371  *  (read: before rescaling to render resolution has been done)
2372  *
2373  *  Order is important!
2374  *
2375  *  - Deinterlace
2376  *  - Crop and transform in image source coordinate space
2377  *  - Flip X + Flip Y (could be done afterwards, backward compatibility)
2378  *  - Promote image to float data (affects pipeline operations afterwards)
2379  *  - Color balance (is most efficient in the byte -> float
2380  *    (future: half -> float should also work fine!)
2381  *    case, if done on load, since we can use lookup tables)
2382  *  - Premultiply
2383  */
2384
2385 bool BKE_sequencer_input_have_to_preprocess(const SeqRenderData *context, Sequence *seq, float UNUSED(cfra))
2386 {
2387         float mul;
2388
2389         if (context->is_proxy_render) {
2390                 return false;
2391         }
2392
2393         if (seq->flag & (SEQ_FILTERY | SEQ_USE_CROP | SEQ_USE_TRANSFORM | SEQ_FLIPX | SEQ_FLIPY | SEQ_MAKE_FLOAT)) {
2394                 return true;
2395         }
2396
2397         mul = seq->mul;
2398
2399         if (seq->blend_mode == SEQ_BLEND_REPLACE) {
2400                 mul *= seq->blend_opacity / 100.0f;
2401         }
2402
2403         if (mul != 1.0f) {
2404                 return true;
2405         }
2406
2407         if (seq->sat != 1.0f) {
2408                 return true;
2409         }
2410
2411         if (seq->modifiers.first) {
2412                 return true;
2413         }
2414
2415         return false;
2416 }
2417
2418 static ImBuf *input_preprocess(const SeqRenderData *context, Sequence *seq, float cfra, ImBuf *ibuf,
2419                                const bool is_proxy_image, const bool is_preprocessed)
2420 {
2421         Scene *scene = context->scene;
2422         float mul;
2423
2424         ibuf = IMB_makeSingleUser(ibuf);
2425
2426         if ((seq->flag & SEQ_FILTERY) &&
2427             !ELEM(seq->type, SEQ_TYPE_MOVIE, SEQ_TYPE_MOVIECLIP))
2428         {
2429                 IMB_filtery(ibuf);
2430         }
2431
2432         if (seq->flag & (SEQ_USE_CROP | SEQ_USE_TRANSFORM)) {
2433                 StripCrop c = {0};
2434                 StripTransform t = {0};
2435                 int sx, sy, dx, dy;
2436
2437                 if (is_proxy_image) {
2438                         double f = seq_rendersize_to_scale_factor(context->preview_render_size);
2439
2440                         if (f != 1.0) {
2441                                 IMB_scalefastImBuf(ibuf, ibuf->x / f, ibuf->y / f);
2442                         }
2443                 }
2444
2445                 if (seq->flag & SEQ_USE_CROP && seq->strip->crop) {
2446                         c = *seq->strip->crop;
2447                 }
2448                 if (seq->flag & SEQ_USE_TRANSFORM && seq->strip->transform) {
2449                         t = *seq->strip->transform;
2450                 }
2451
2452                 if (is_preprocessed) {
2453                         double xscale = scene->r.xsch ? ((double)context->rectx / (double)scene->r.xsch) : 1.0;
2454                         double yscale = scene->r.ysch ? ((double)context->recty / (double)scene->r.ysch) : 1.0;
2455                         if (seq->flag & SEQ_USE_TRANSFORM) {
2456                                 t.xofs *= xscale;
2457                                 t.yofs *= yscale;
2458                         }
2459                         if (seq->flag & SEQ_USE_CROP) {
2460                                 c.left *= xscale;
2461                                 c.right *= xscale;
2462                                 c.top *= yscale;
2463                                 c.bottom *= yscale;
2464                         }
2465                 }
2466
2467                 sx = ibuf->x - c.left - c.right;
2468                 sy = ibuf->y - c.top - c.bottom;
2469
2470                 if (seq->flag & SEQ_USE_TRANSFORM) {
2471                         if (is_preprocessed) {
2472                                 dx = context->rectx;
2473                                 dy = context->recty;
2474                         }
2475                         else {
2476                                 dx = scene->r.xsch;
2477                                 dy = scene->r.ysch;
2478                         }
2479                 }
2480                 else {
2481                         dx = sx;
2482                         dy = sy;
2483                 }
2484
2485                 if (c.top  + c.bottom >= ibuf->y ||
2486                     c.left + c.right  >= ibuf->x ||
2487                     t.xofs >= dx || t.yofs >= dy)
2488                 {
2489                         make_black_ibuf(ibuf);
2490                 }
2491                 else {
2492                         ImBuf *i = IMB_allocImBuf(dx, dy, 32, ibuf->rect_float ? IB_rectfloat : IB_rect);
2493
2494                         IMB_rectcpy(i, ibuf, t.xofs, t.yofs, c.left, c.bottom, sx, sy);
2495                         sequencer_imbuf_assign_spaces(scene, i);
2496
2497                         IMB_metadata_copy(i, ibuf);
2498                         IMB_freeImBuf(ibuf);
2499
2500                         ibuf = i;
2501                 }
2502         }
2503
2504         if (seq->flag & SEQ_FLIPX) {
2505                 IMB_flipx(ibuf);
2506         }
2507         
2508         if (seq->flag & SEQ_FLIPY) {
2509                 IMB_flipy(ibuf);
2510         }
2511
2512         if (seq->sat != 1.0f) {
2513                 IMB_saturation(ibuf, seq->sat);
2514         }
2515
2516         mul = seq->mul;
2517
2518         if (seq->blend_mode == SEQ_BLEND_REPLACE) {
2519                 mul *= seq->blend_opacity / 100.0f;
2520         }
2521
2522         if (seq->flag & SEQ_MAKE_FLOAT) {
2523                 if (!ibuf->rect_float) {
2524                         BKE_sequencer_imbuf_to_sequencer_space(scene, ibuf, true);
2525                 }
2526
2527                 if (ibuf->rect) {
2528                         imb_freerectImBuf(ibuf);
2529                 }
2530         }
2531
2532         if (mul != 1.0f) {
2533                 multibuf(ibuf, mul);
2534         }
2535
2536         if (ibuf->x != context->rectx || ibuf->y != context->recty) {
2537                 if (scene->r.mode & R_OSA) {
2538                         IMB_scaleImBuf(ibuf, (short)context->rectx, (short)context->recty);
2539                 }
2540                 else {
2541                         IMB_scalefastImBuf(ibuf, (short)context->rectx, (short)context->recty);
2542                 }
2543         }
2544
2545         if (seq->modifiers.first) {
2546                 ImBuf *ibuf_new = BKE_sequence_modifier_apply_stack(context, seq, ibuf, cfra);
2547
2548                 if (ibuf_new != ibuf) {
2549                         IMB_metadata_copy(ibuf_new, ibuf);
2550                         IMB_freeImBuf(ibuf);
2551                         ibuf = ibuf_new;
2552                 }
2553         }
2554
2555         return ibuf;
2556 }
2557
2558 static ImBuf *copy_from_ibuf_still(const SeqRenderData *context, Sequence *seq, float nr)
2559 {
2560         ImBuf *rval = NULL;
2561         ImBuf *ibuf = NULL;
2562
2563         if (nr == 0) {
2564                 ibuf = BKE_sequencer_cache_get(context, seq, seq->start, SEQ_STRIPELEM_IBUF_STARTSTILL);
2565         }
2566         else if (nr == seq->len - 1) {
2567                 ibuf = BKE_sequencer_cache_get(context, seq, seq->start, SEQ_STRIPELEM_IBUF_ENDSTILL);
2568         }
2569
2570         if (ibuf) {
2571                 rval = IMB_dupImBuf(ibuf);
2572                 IMB_metadata_copy(rval, ibuf);
2573                 IMB_freeImBuf(ibuf);
2574         }
2575
2576         return rval;
2577 }
2578
2579 static void copy_to_ibuf_still(const SeqRenderData *context, Sequence *seq, float nr, ImBuf *ibuf)
2580 {
2581         /* warning: ibuf may be NULL if the video fails to load */
2582         if (nr == 0 || nr == seq->len - 1) {
2583                 /* we have to store a copy, since the passed ibuf
2584                  * could be preprocessed afterwards (thereby silently
2585                  * changing the cached image... */
2586                 ImBuf *oibuf = ibuf;
2587                 ibuf = IMB_dupImBuf(oibuf);
2588
2589                 if (ibuf) {
2590                         IMB_metadata_copy(ibuf, oibuf);
2591                         sequencer_imbuf_assign_spaces(context->scene, ibuf);
2592                 }
2593
2594                 if (nr == 0) {
2595                         BKE_sequencer_cache_put(context, seq, seq->start, SEQ_STRIPELEM_IBUF_STARTSTILL, ibuf);
2596                 }
2597
2598                 if (nr == seq->len - 1) {
2599                         BKE_sequencer_cache_put(context, seq, seq->start, SEQ_STRIPELEM_IBUF_ENDSTILL, ibuf);
2600                 }
2601
2602                 IMB_freeImBuf(ibuf);
2603         }
2604 }
2605
2606 /*********************** strip rendering functions  *************************/
2607
2608 typedef struct RenderEffectInitData {
2609         struct SeqEffectHandle *sh;
2610         const SeqRenderData *context;
2611         Sequence *seq;
2612         float cfra, facf0, facf1;
2613         ImBuf *ibuf1, *ibuf2, *ibuf3;
2614
2615         ImBuf *out;
2616 } RenderEffectInitData;
2617
2618 typedef struct RenderEffectThread {
2619         struct SeqEffectHandle *sh;
2620         const SeqRenderData *context;
2621         Sequence *seq;
2622         float cfra, facf0, facf1;
2623         ImBuf *ibuf1, *ibuf2, *ibuf3;
2624
2625         ImBuf *out;
2626         int start_line, tot_line;
2627 } RenderEffectThread;
2628
2629 static void render_effect_execute_init_handle(void *handle_v, int start_line, int tot_line, void *init_data_v)
2630 {
2631         RenderEffectThread *handle = (RenderEffectThread *) handle_v;
2632         RenderEffectInitData *init_data = (RenderEffectInitData *) init_data_v;
2633
2634         handle->sh = init_data->sh;
2635         handle->context = init_data->context;
2636         handle->seq = init_data->seq;
2637         handle->cfra = init_data->cfra;
2638         handle->facf0 = init_data->facf0;
2639         handle->facf1 = init_data->facf1;
2640         handle->ibuf1 = init_data->ibuf1;
2641         handle->ibuf2 = init_data->ibuf2;
2642         handle->ibuf3 = init_data->ibuf3;
2643         handle->out = init_data->out;
2644
2645         handle->start_line = start_line;
2646         handle->tot_line = tot_line;
2647 }
2648
2649 static void *render_effect_execute_do_thread(void *thread_data_v)
2650 {
2651         RenderEffectThread *thread_data = (RenderEffectThread *) thread_data_v;
2652
2653         thread_data->sh->execute_slice(thread_data->context, thread_data->seq, thread_data->cfra,
2654                                        thread_data->facf0, thread_data->facf1, thread_data->ibuf1,
2655                                        thread_data->ibuf2, thread_data->ibuf3, thread_data->start_line,
2656                                        thread_data->tot_line, thread_data->out);
2657
2658         return NULL;
2659 }
2660
2661 static ImBuf *seq_render_effect_execute_threaded(struct SeqEffectHandle *sh, const SeqRenderData *context, Sequence *seq,
2662                                                  float cfra, float facf0, float facf1,
2663                                                  ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *ibuf3)
2664 {
2665         RenderEffectInitData init_data;
2666         ImBuf *out = sh->init_execution(context, ibuf1, ibuf2, ibuf3);
2667
2668         init_data.sh = sh;
2669         init_data.context = context;
2670         init_data.seq = seq;
2671         init_data.cfra = cfra;
2672         init_data.facf0 = facf0;
2673         init_data.facf1 = facf1;
2674         init_data.ibuf1 = ibuf1;
2675         init_data.ibuf2 = ibuf2;
2676         init_data.ibuf3 = ibuf3;
2677         init_data.out = out;
2678
2679         IMB_processor_apply_threaded(out->y, sizeof(RenderEffectThread), &init_data,
2680                                      render_effect_execute_init_handle, render_effect_execute_do_thread);
2681
2682         return out;
2683 }
2684
2685 static ImBuf *seq_render_effect_strip_impl(
2686         const SeqRenderData *context, SeqRenderState *state,
2687         Sequence *seq, float cfra)
2688 {
2689         Scene *scene = context->scene;
2690         float fac, facf;
2691         int early_out;
2692         int i;
2693         struct SeqEffectHandle sh = BKE_sequence_get_effect(seq);
2694         FCurve *fcu = NULL;
2695         ImBuf *ibuf[3];
2696         Sequence *input[3];
2697         ImBuf *out = NULL;
2698
2699         ibuf[0] = ibuf[1] = ibuf[2] = NULL;
2700
2701         input[0] = seq->seq1; input[1] = seq->seq2; input[2] = seq->seq3;
2702
2703         if (!sh.execute && !(sh.execute_slice && sh.init_execution)) {
2704                 /* effect not supported in this version... */
2705                 out = IMB_allocImBuf(context->rectx, context->recty, 32, IB_rect);
2706                 return out;
2707         }
2708
2709         if (seq->flag & SEQ_USE_EFFECT_DEFAULT_FADE) {
2710                 sh.get_default_fac(seq, cfra, &fac, &facf);
2711                 
2712                 if ((scene->r.mode & R_FIELDS) == 0)
2713                         facf = fac;
2714         }
2715         else {
2716                 fcu = id_data_find_fcurve(&scene->id, seq, &RNA_Sequence, "effect_fader", 0, NULL);
2717                 if (fcu) {
2718                         fac = facf = evaluate_fcurve(fcu, cfra);
2719                         if (scene->r.mode & R_FIELDS) {
2720                                 facf = evaluate_fcurve(fcu, cfra + 0.5f);
2721                         }
2722                 }
2723                 else {
2724                         fac = facf = seq->effect_fader;
2725                 }
2726         }
2727
2728         early_out = sh.early_out(seq, fac, facf);
2729
2730         switch (early_out) {
2731                 case EARLY_NO_INPUT:
2732                         out = sh.execute(context, seq, cfra, fac, facf, NULL, NULL, NULL);
2733                         break;
2734                 case EARLY_DO_EFFECT:
2735                         for (i = 0; i < 3; i++) {
2736                                 if (input[i])
2737                                         ibuf[i] = seq_render_strip(context, state, input[i], cfra);
2738                         }
2739
2740                         if (ibuf[0] && ibuf[1]) {
2741                                 if (sh.multithreaded)
2742                                         out = seq_render_effect_execute_threaded(&sh, context, seq, cfra, fac, facf, ibuf[0], ibuf[1], ibuf[2]);
2743                                 else
2744                                         out = sh.execute(context, seq, cfra, fac, facf, ibuf[0], ibuf[1], ibuf[2]);
2745                         }
2746                         break;
2747                 case EARLY_USE_INPUT_1:
2748                         if (input[0]) {
2749                                 ibuf[0] = seq_render_strip(context, state, input[0], cfra);
2750                         }
2751                         if (ibuf[0]) {
2752                                 if (BKE_sequencer_input_have_to_preprocess(context, seq, cfra)) {
2753                                         out = IMB_dupImBuf(ibuf[0]);
2754                                 }
2755                                 else {
2756                                         out = ibuf[0];
2757                                         IMB_refImBuf(out);
2758                                 }
2759                         }
2760                         break;
2761                 case EARLY_USE_INPUT_2:
2762                         if (input[1]) {
2763                                 ibuf[1] = seq_render_strip(context, state, input[1], cfra);
2764                         }
2765                         if (ibuf[1]) {
2766                                 if (BKE_sequencer_input_have_to_preprocess(context, seq, cfra)) {
2767                                         out = IMB_dupImBuf(ibuf[1]);
2768                                 }
2769                                 else {
2770                                         out = ibuf[1];
2771                                         IMB_refImBuf(out);
2772                                 }
2773                         }
2774                         break;
2775         }
2776
2777         for (i = 0; i < 3; i++) {
2778                 IMB_freeImBuf(ibuf[i]);
2779         }
2780
2781         if (out == NULL) {
2782                 out = IMB_allocImBuf(context->rectx, context->recty, 32, IB_rect);
2783         }
2784
2785         return out;
2786 }
2787
2788 static ImBuf *seq_render_image_strip(const SeqRenderData *context, Sequence *seq, float nr, float cfra)
2789 {
2790         ImBuf *ibuf = NULL;
2791         char name[FILE_MAX];
2792         bool is_multiview = (seq->flag & SEQ_USE_VIEWS) != 0 &&
2793                             (context->scene->r.scemode & R_MULTIVIEW) != 0;
2794         StripElem *s_elem = BKE_sequencer_give_stripelem(seq, cfra);
2795         int flag;
2796
2797         if (s_elem) {
2798                 BLI_join_dirfile(name, sizeof(name), seq->strip->dir, s_elem->name);
2799                 BLI_path_abs(name, G.main->name);
2800         }
2801
2802         flag = IB_rect | IB_metadata;
2803         if (seq->alpha_mode == SEQ_ALPHA_PREMUL)
2804                 flag |= IB_alphamode_premul;
2805
2806         if (!s_elem) {
2807                 /* don't do anything */
2808         }
2809         else if (is_multiview) {
2810                 const int totfiles = seq_num_files(context->scene, seq->views_format, true);
2811                 int totviews;
2812                 struct ImBuf **ibufs_arr;
2813                 char prefix[FILE_MAX];
2814                 const char *ext = NULL;
2815                 int i;
2816
2817                 if (totfiles > 1) {
2818                         BKE_scene_multiview_view_prefix_get(context->scene, name, prefix, &ext);
2819                         if (prefix[0] == '\0') {
2820                                 goto monoview_image;
2821                         }
2822                 }
2823                 else {
2824                         prefix[0] = '\0';
2825                 }
2826
2827                 totviews = BKE_scene_multiview_num_views_get(&context->scene->r);
2828                 ibufs_arr = MEM_callocN(sizeof(ImBuf *) * totviews, "Sequence Image Views Imbufs");
2829
2830                 for (i = 0; i < totfiles; i++) {
2831
2832                         if (prefix[0] == '\0') {
2833                                 ibufs_arr[i] = IMB_loadiffname(name, flag, seq->strip->colorspace_settings.name);
2834                         }
2835                         else {
2836                                 char str[FILE_MAX];
2837                                 seq_multiview_name(context->scene, i, prefix, ext, str, FILE_MAX);
2838                                 ibufs_arr[i] = IMB_loadiffname(str, flag, seq->strip->colorspace_settings.name);
2839                         }
2840
2841                         if (ibufs_arr[i]) {
2842                                 /* we don't need both (speed reasons)! */
2843                                 if (ibufs_arr[i]->rect_float && ibufs_arr[i]->rect)
2844                                         imb_freerectImBuf(ibufs_arr[i]);
2845                         }
2846                 }
2847
2848                 if (seq->views_format == R_IMF_VIEWS_STEREO_3D && ibufs_arr[0])
2849                         IMB_ImBufFromStereo3d(seq->stereo3d_format, ibufs_arr[0], &ibufs_arr[0], &ibufs_arr[1]);
2850
2851                 for (i = 0; i < totviews; i++) {
2852                         if (ibufs_arr[i]) {
2853                                 SeqRenderData localcontext = *context;
2854                                 localcontext.view_id = i;
2855
2856                                 /* all sequencer color is done in SRGB space, linear gives odd crossfades */
2857                                 BKE_sequencer_imbuf_to_sequencer_space(context->scene, ibufs_arr[i], false);
2858
2859                                 if (i != context->view_id) {
2860                                         copy_to_ibuf_still(&localcontext, seq, nr, ibufs_arr[i]);
2861                                         BKE_sequencer_cache_put(&localcontext, seq, cfra, SEQ_STRIPELEM_IBUF, ibufs_arr[i]);
2862                                 }
2863                         }
2864                 }
2865
2866                 /* return the original requested ImBuf */
2867                 ibuf = ibufs_arr[context->view_id];
2868                 if (ibuf) {
2869                         s_elem->orig_width  = ibufs_arr[0]->x;
2870                         s_elem->orig_height = ibufs_arr[0]->y;
2871                 }
2872
2873                 /* "remove" the others (decrease their refcount) */
2874                 for (i = 0; i < totviews; i++) {
2875                         if (ibufs_arr[i] != ibuf) {
2876                                 IMB_freeImBuf(ibufs_arr[i]);
2877                         }
2878                 }
2879
2880                 MEM_freeN(ibufs_arr);
2881         }
2882         else {
2883 monoview_image:
2884                 if ((ibuf = IMB_loadiffname(name, flag, seq->strip->colorspace_settings.name))) {
2885                         /* we don't need both (speed reasons)! */
2886                         if (ibuf->rect_float && ibuf->rect)
2887                                 imb_freerectImBuf(ibuf);
2888
2889                         /* all sequencer color is done in SRGB space, linear gives odd crossfades */
2890                         BKE_sequencer_imbuf_to_sequencer_space(context->scene, ibuf, false);
2891
2892                         s_elem->orig_width  = ibuf->x;
2893                         s_elem->orig_height = ibuf->y;
2894                 }
2895         }
2896
2897         return ibuf;
2898 }
2899
2900 static ImBuf *seq_render_movie_strip(const SeqRenderData *context, Sequence *seq, float nr, float cfra)
2901 {
2902         ImBuf *ibuf = NULL;
2903         StripAnim *sanim;
2904         bool is_multiview = (seq->flag & SEQ_USE_VIEWS) != 0 &&
2905                             (context->scene->r.scemode & R_MULTIVIEW) != 0;
2906
2907         /* load all the videos */
2908         seq_open_anim_file(context->scene, seq, false);
2909
2910         if (is_multiview) {
2911                 ImBuf **ibuf_arr;
2912                 const int totfiles = seq_num_files(context->scene, seq->views_format, true);
2913                 int totviews;
2914                 int i;
2915
2916                 if (totfiles != BLI_listbase_count_ex(&seq->anims, totfiles + 1))
2917                         goto monoview_movie;
2918
2919                 totviews = BKE_scene_multiview_num_views_get(&context->scene->r);
2920                 ibuf_arr = MEM_callocN(sizeof(ImBuf *) * totviews, "Sequence Image Views Imbufs");
2921
2922                 for (i = 0, sanim = seq->anims.first; sanim; sanim = sanim->next, i++) {
2923                         if (sanim->anim) {
2924                                 IMB_Proxy_Size proxy_size = seq_rendersize_to_proxysize(context->preview_render_size);
2925                                 IMB_anim_set_preseek(sanim->anim, seq->anim_preseek);
2926
2927                                 ibuf_arr[i] = IMB_anim_absolute(sanim->anim, nr + seq->anim_startofs,
2928                                                                 seq->strip->proxy ? seq->strip->proxy->tc : IMB_TC_RECORD_RUN,
2929                                                                 proxy_size);
2930
2931                                 /* fetching for requested proxy size failed, try fetching the original instead */
2932                                 if (!ibuf_arr[i] && proxy_size != IMB_PROXY_NONE) {
2933                                         ibuf_arr[i] = IMB_anim_absolute(sanim->anim, nr + seq->anim_startofs,
2934                                                                         seq->strip->proxy ? seq->strip->proxy->tc : IMB_TC_RECORD_RUN,
2935                                                                         IMB_PROXY_NONE);
2936                                 }
2937                                 if (ibuf_arr[i]) {
2938                                         /* we don't need both (speed reasons)! */
2939                                         if (ibuf_arr[i]->rect_float && ibuf_arr[i]->rect)
2940                                                 imb_freerectImBuf(ibuf_arr[i]);
2941                                 }
2942                         }
2943                 }
2944
2945                 if (seq->views_format == R_IMF_VIEWS_STEREO_3D) {
2946                         if (ibuf_arr[0]) {
2947                                 IMB_ImBufFromStereo3d(seq->stereo3d_format, ibuf_arr[0], &ibuf_arr[0], &ibuf_arr[1]);
2948                         }
2949                         else {
2950                                 /* probably proxy hasn't been created yet */
2951                                 MEM_freeN(ibuf_arr);
2952                                 return NULL;
2953                         }
2954                 }
2955
2956                 for (i = 0; i < totviews; i++) {
2957                         SeqRenderData localcontext = *context;
2958                         localcontext.view_id = i;
2959
2960                         if (ibuf_arr[i]) {
2961                                 /* all sequencer color is done in SRGB space, linear gives odd crossfades */
2962                                 BKE_sequencer_imbuf_to_sequencer_space(context->scene, ibuf_arr[i], false);
2963                         }
2964                         if (i != context->view_id) {
2965                                 copy_to_ibuf_still(&localcontext, seq, nr, ibuf_arr[i]);
2966                                 BKE_sequencer_cache_put(&localcontext, seq, cfra, SEQ_STRIPELEM_IBUF, ibuf_arr[i]);
2967                         }
2968                 }
2969
2970                 /* return the original requested ImBuf */
2971                 ibuf = ibuf_arr[context->view_id];
2972                 if (ibuf) {
2973                         seq->strip->stripdata->orig_width = ibuf->x;
2974                         seq->strip->stripdata->orig_height = ibuf->y;
2975                 }
2976
2977                 /* "remove" the others (decrease their refcount) */
2978                 for (i = 0; i < totviews; i++) {
2979                         if (ibuf_arr[i] != ibuf) {
2980                                 IMB_freeImBuf(ibuf_arr[i]);
2981                         }
2982                 }
2983
2984                 MEM_freeN(ibuf_arr);
2985         }
2986         else {
2987 monoview_movie:
2988                 sanim = seq->anims.first;
2989                 if (sanim && sanim->anim) {
2990                         IMB_Proxy_Size proxy_size = seq_rendersize_to_proxysize(context->preview_render_size);
2991                         IMB_anim_set_preseek(sanim->anim, seq->anim_preseek);
2992
2993                         ibuf = IMB_anim_absolute(sanim->anim, nr + seq->anim_startofs,
2994                                                  seq->strip->proxy ? seq->strip->proxy->tc : IMB_TC_RECORD_RUN,
2995                                                  proxy_size);
2996
2997                         /* fetching for requested proxy size failed, try fetching the original instead */
2998                         if (!ibuf && proxy_size != IMB_PROXY_NONE) {
2999                                 ibuf = IMB_anim_absolute(sanim->anim, nr + seq->anim_startofs,
3000                                                          seq->strip->proxy ? seq->strip->proxy->tc : IMB_TC_RECORD_RUN,
3001                                                          IMB_PROXY_NONE);
3002                         }
3003                         if (ibuf) {
3004                                 BKE_sequencer_imbuf_to_sequencer_space(context->scene, ibuf, false);
3005
3006                                 /* we don't need both (speed reasons)! */
3007                                 if (ibuf->rect_float && ibuf->rect) {
3008                                         imb_freerectImBuf(ibuf);
3009                                 }
3010
3011                                 seq->strip->stripdata->orig_width = ibuf->x;
3012                                 seq->strip->stripdata->orig_height = ibuf->y;
3013                         }
3014                 }
3015         }
3016         return ibuf;
3017 }
3018
3019 static ImBuf *seq_render_movieclip_strip(const SeqRenderData *context, Sequence *seq, float nr)
3020 {
3021         ImBuf *ibuf = NULL;
3022         MovieClipUser user;
3023         float tloc[2], tscale, tangle;
3024
3025         if (!seq->clip) {
3026                 return NULL;
3027         }
3028
3029         memset(&user, 0, sizeof(MovieClipUser));
3030         
3031         BKE_movieclip_user_set_frame(&user, nr + seq->anim_startofs + seq->clip->start_frame);
3032
3033         user.render_size = MCLIP_PROXY_RENDER_SIZE_FULL;
3034
3035         switch (seq_rendersize_to_proxysize(context->preview_render_size)) {
3036                 case IMB_PROXY_NONE:
3037                         user.render_size = MCLIP_PROXY_RENDER_SIZE_FULL;
3038                         break;
3039                 case IMB_PROXY_100:
3040                         user.render_size = MCLIP_PROXY_RENDER_SIZE_100;
3041                         break;
3042                 case IMB_PROXY_75:
3043                         user.render_size = MCLIP_PROXY_RENDER_SIZE_75;
3044                         break;
3045                 case IMB_PROXY_50:
3046                         user.render_size = MCLIP_PROXY_RENDER_SIZE_50;
3047                         break;
3048                 case IMB_PROXY_25:
3049                         user.render_size = MCLIP_PROXY_RENDER_SIZE_25;
3050                         break;
3051         }
3052
3053         if (seq->clip_flag & SEQ_MOVIECLIP_RENDER_UNDISTORTED) {
3054                 user.render_flag = MCLIP_PROXY_RENDER_UNDISTORT;
3055         }
3056
3057         if (seq->clip_flag & SEQ_MOVIECLIP_RENDER_STABILIZED) {
3058                 ibuf = BKE_movieclip_get_stable_ibuf(seq->clip, &user, tloc, &tscale, &tangle, 0);
3059         }
3060         else {
3061                 ibuf = BKE_movieclip_get_ibuf_flag(seq->clip, &user, 0, MOVIECLIP_CACHE_SKIP);
3062         }
3063
3064         return ibuf;
3065 }
3066
3067
3068 static ImBuf *seq_render_mask(const SeqRenderData *context, Mask *mask, float nr, bool make_float)
3069 {
3070         /* TODO - add option to rasterize to alpha imbuf? */
3071         ImBuf *ibuf = NULL;
3072         float *maskbuf;
3073         int i;
3074
3075         if (!mask) {
3076                 return NULL;
3077         }
3078         else {
3079                 AnimData *adt;
3080                 Mask *mask_temp;
3081                 MaskRasterHandle *mr_handle;
3082
3083                 mask_temp = BKE_mask_copy_nolib(mask);
3084
3085                 BKE_mask_evaluate(mask_temp, mask->sfra + nr, true);
3086
3087                 /* anim-data */
3088                 adt = BKE_animdata_from_id(&mask->id);
3089                 BKE_animsys_evaluate_animdata(context->scene, &mask_temp->id, adt, nr, ADT_RECALC_ANIM);
3090
3091                 maskbuf = MEM_mallocN(sizeof(float) * context->rectx * context->recty, __func__);
3092
3093                 mr_handle = BKE_maskrasterize_handle_new();
3094
3095                 BKE_maskrasterize_handle_init(mr_handle, mask_temp, context->rectx, context->recty, true, true, true);
3096
3097                 BKE_mask_free(mask_temp);
3098                 MEM_freeN(mask_temp);
3099
3100                 BKE_maskrasterize_buffer(mr_handle, context->rectx, context->recty, maskbuf);
3101
3102                 BKE_maskrasterize_handle_free(mr_handle);
3103         }
3104
3105
3106         if (make_float) {
3107                 /* pixels */
3108                 const float *fp_src;
3109                 float *fp_dst;
3110
3111                 ibuf = IMB_allocImBuf(context->rectx, context->recty, 32, IB_rectfloat);
3112
3113                 fp_src = maskbuf;
3114                 fp_dst = ibuf->rect_float;
3115                 i = context->rectx * context->recty;
3116                 while (--i) {
3117                         fp_dst[0] = fp_dst[1] = fp_dst[2] = *fp_src;
3118                         fp_dst[3] = 1.0f;
3119
3120                         fp_src += 1;
3121                         fp_dst += 4;
3122                 }
3123         }
3124         else {
3125                 /* pixels */
3126                 const float *fp_src;
3127                 unsigned char *ub_dst;
3128
3129                 ibuf = IMB_allocImBuf(context->rectx, context->recty, 32, IB_rect);
3130
3131                 fp_src = maskbuf;
3132                 ub_dst = (unsigned char *)ibuf->rect;
3133                 i = context->rectx * context->recty;
3134                 while (--i) {
3135                         ub_dst[0] = ub_dst[1] = ub_dst[2] = (unsigned char)(*fp_src * 255.0f); /* already clamped */
3136                         ub_dst[3] = 255;
3137
3138                         fp_src += 1;
3139                         ub_dst += 4;
3140                 }
3141         }
3142
3143         MEM_freeN(maskbuf);
3144
3145         return ibuf;
3146 }
3147
3148 static ImBuf *seq_render_mask_strip(const SeqRenderData *context, Sequence *seq, float nr)
3149 {
3150         bool make_float = (seq->flag & SEQ_MAKE_FLOAT) != 0;
3151
3152         return seq_render_mask(context, seq->mask, nr, make_float);
3153 }
3154
3155 static ImBuf *seq_render_scene_strip(const SeqRenderData *context, Sequence *seq, float nr, float cfra)
3156 {
3157         ImBuf *ibuf = NULL;
3158         double frame;
3159         Object *camera;
3160
3161         struct {
3162                 int scemode;
3163                 int cfra;
3164                 float subframe;
3165 #ifdef DURIAN_CAMERA_SWITCH
3166                 ListBase markers;
3167 #endif
3168         } orig_data;
3169         
3170         /* Old info:
3171          * Hack! This function can be called from do_render_seq(), in that case
3172          * the seq->scene can already have a Render initialized with same name,
3173          * so we have to use a default name. (compositor uses scene name to
3174          * find render).
3175          * However, when called from within the UI (image preview in sequencer)
3176          * we do want to use scene Render, that way the render result is defined
3177          * for display in render/imagewindow
3178          *
3179          * Hmm, don't see, why we can't do that all the time,
3180          * and since G.is_rendering is uhm, gone... (Peter)
3181          */
3182
3183         /* New info:
3184          * Using the same name for the renders works just fine as the do_render_seq()
3185          * render is not used while the scene strips are rendered.
3186          *
3187          * However rendering from UI (through sequencer_preview_area_draw) can crash in
3188          * very many cases since other renders (material preview, an actual render etc.)
3189          * can be started while this sequence preview render is running. The only proper
3190          * solution is to make the sequencer preview render a proper job, which&nb