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