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