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