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