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