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