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