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