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