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