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