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