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