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