Merging r40615 through r40652 from trunk into soc-2011-tomato
[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         /* unsupported feature only confuses other s/w */
1329         if(ibuf->depth==32)
1330                 ibuf->depth= 24;
1331
1332         BLI_make_existing_file(name);
1333         
1334         ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
1335         if (ok == 0) {
1336                 perror(name);
1337         }
1338
1339         IMB_freeImBuf(ibuf);
1340 }
1341
1342 void seq_proxy_rebuild(struct Main * bmain, Scene *scene, Sequence * seq,
1343                        short *stop, short *do_update, float *progress)
1344 {
1345         SeqRenderData context;
1346         int cfra;
1347         int tc_flags;
1348         int size_flags;
1349         int quality;
1350
1351         if (!seq->strip || !seq->strip->proxy) {
1352                 return;
1353         }
1354
1355         if (!(seq->flag & SEQ_USE_PROXY)) {
1356                 return;
1357         }
1358
1359         tc_flags   = seq->strip->proxy->build_tc_flags;
1360         size_flags = seq->strip->proxy->build_size_flags;
1361         quality    = seq->strip->proxy->quality;
1362
1363         if (seq->type == SEQ_MOVIE) {
1364                 seq_open_anim_file(seq);
1365
1366                 if (seq->anim) {
1367                         IMB_anim_index_rebuild(
1368                                 seq->anim, tc_flags, size_flags, quality,
1369                                 stop, do_update, progress);
1370                 }
1371                 return;
1372         }
1373
1374         if (!(seq->flag & SEQ_USE_PROXY)) {
1375                 return;
1376         }
1377
1378         /* that's why it is called custom... */
1379         if (seq->flag & SEQ_USE_PROXY_CUSTOM_FILE) {
1380                 return;
1381         }
1382
1383         /* fail safe code */
1384
1385         context = seq_new_render_data(
1386                 bmain, scene, 
1387                 (scene->r.size * (float) scene->r.xsch) / 100.0f + 0.5f, 
1388                 (scene->r.size * (float) scene->r.ysch) / 100.0f + 0.5f, 
1389                 100);
1390
1391         for (cfra = seq->startdisp + seq->startstill; 
1392              cfra < seq->enddisp - seq->endstill; cfra++) {
1393                 if (size_flags & IMB_PROXY_25) {
1394                         seq_proxy_build_frame(context, seq, cfra, 25);
1395                 }
1396                 if (size_flags & IMB_PROXY_50) {
1397                         seq_proxy_build_frame(context, seq, cfra, 50);
1398                 }
1399                 if (size_flags & IMB_PROXY_75) {
1400                         seq_proxy_build_frame(context, seq, cfra, 75);
1401                 }
1402                 if (size_flags & IMB_PROXY_100) {
1403                         seq_proxy_build_frame(context, seq, cfra, 100);
1404                 }
1405
1406                 *progress= (float)cfra/(seq->enddisp - seq->endstill 
1407                                         - seq->startdisp + seq->startstill);
1408                 *do_update= 1;
1409
1410                 if(*stop || G.afbreek)
1411                         break;
1412         }
1413 }
1414
1415
1416 /* **********************************************************************
1417    color balance 
1418    ********************************************************************** */
1419
1420 static StripColorBalance calc_cb(StripColorBalance * cb_)
1421 {
1422         StripColorBalance cb = *cb_;
1423         int c;
1424
1425         for (c = 0; c < 3; c++) {
1426                 cb.lift[c] = 2.0f - cb.lift[c];
1427         }
1428
1429         if(cb.flag & SEQ_COLOR_BALANCE_INVERSE_LIFT) {
1430                 for (c = 0; c < 3; c++) {
1431                         /* tweak to give more subtle results
1432                          * values above 1.0 are scaled */
1433                         if(cb.lift[c] > 1.0f)
1434                                 cb.lift[c] = pow(cb.lift[c] - 1.0f, 2.0) + 1.0;
1435
1436                         cb.lift[c] = 2.0f - cb.lift[c];
1437                 }
1438         }
1439
1440         if (cb.flag & SEQ_COLOR_BALANCE_INVERSE_GAIN) {
1441                 for (c = 0; c < 3; c++) {
1442                         if (cb.gain[c] != 0.0f) {
1443                                 cb.gain[c] = 1.0f / cb.gain[c];
1444                         } else {
1445                                 cb.gain[c] = 1000000; /* should be enough :) */
1446                         }
1447                 }
1448         }
1449
1450         if (!(cb.flag & SEQ_COLOR_BALANCE_INVERSE_GAMMA)) {
1451                 for (c = 0; c < 3; c++) {
1452                         if (cb.gamma[c] != 0.0f) {
1453                                 cb.gamma[c] = 1.0f/cb.gamma[c];
1454                         } else {
1455                                 cb.gamma[c] = 1000000; /* should be enough :) */
1456                         }
1457                 }
1458         }
1459
1460         return cb;
1461 }
1462
1463 /* note: lift is actually 2-lift */
1464 MINLINE float color_balance_fl(float in, const float lift, const float gain, const float gamma, const float mul)
1465 {
1466         float x= (((in - 1.0f) * lift) + 1.0f) * gain;
1467
1468         /* prevent NaN */
1469         if (x < 0.f) x = 0.f;
1470
1471         return powf(x, gamma) * mul;
1472 }
1473
1474 static void make_cb_table_byte(float lift, float gain, float gamma,
1475                                    unsigned char * table, float mul)
1476 {
1477         int y;
1478
1479         for (y = 0; y < 256; y++) {
1480                 float v= color_balance_fl((float)y * (1.0f / 255.0f), lift, gain, gamma, mul);
1481                 CLAMP(v, 0.0f, 1.0f);
1482                 table[y] = v * 255;
1483         }
1484 }
1485
1486 static void make_cb_table_float(float lift, float gain, float gamma,
1487                                 float * table, float mul)
1488 {
1489         int y;
1490
1491         for (y = 0; y < 256; y++) {
1492                 float v= color_balance_fl((float)y * (1.0f / 255.0f), lift, gain, gamma, mul);
1493                 table[y] = v;
1494         }
1495 }
1496
1497 static void color_balance_byte_byte(Sequence * seq, ImBuf* ibuf, float mul)
1498 {
1499         unsigned char cb_tab[3][256];
1500         int c;
1501         unsigned char * p = (unsigned char*) ibuf->rect;
1502         unsigned char * e = p + ibuf->x * 4 * ibuf->y;
1503
1504         StripColorBalance cb = calc_cb(seq->strip->color_balance);
1505
1506         for (c = 0; c < 3; c++) {
1507                 make_cb_table_byte(cb.lift[c], cb.gain[c], cb.gamma[c],
1508                                    cb_tab[c], mul);
1509         }
1510
1511         while (p < e) {
1512                 p[0] = cb_tab[0][p[0]];
1513                 p[1] = cb_tab[1][p[1]];
1514                 p[2] = cb_tab[2][p[2]];
1515                 
1516                 p += 4;
1517         }
1518 }
1519
1520 static void color_balance_byte_float(Sequence * seq, ImBuf* ibuf, float mul)
1521 {
1522         float cb_tab[4][256];
1523         int c,i;
1524         unsigned char * p = (unsigned char*) ibuf->rect;
1525         unsigned char * e = p + ibuf->x * 4 * ibuf->y;
1526         float * o;
1527         StripColorBalance cb;
1528
1529         imb_addrectfloatImBuf(ibuf);
1530
1531         o = ibuf->rect_float;
1532
1533         cb = calc_cb(seq->strip->color_balance);
1534
1535         for (c = 0; c < 3; c++) {
1536                 make_cb_table_float(cb.lift[c], cb.gain[c], cb.gamma[c], cb_tab[c], mul);
1537         }
1538
1539         for (i = 0; i < 256; i++) {
1540                 cb_tab[3][i] = ((float)i)*(1.0f/255.0f);
1541         }
1542
1543         while (p < e) {
1544                 o[0] = cb_tab[0][p[0]];
1545                 o[1] = cb_tab[1][p[1]];
1546                 o[2] = cb_tab[2][p[2]];
1547                 o[3] = cb_tab[3][p[3]];
1548
1549                 p += 4; o += 4;
1550         }
1551 }
1552
1553 static void color_balance_float_float(Sequence * seq, ImBuf* ibuf, float mul)
1554 {
1555         float * p = ibuf->rect_float;
1556         float * e = ibuf->rect_float + ibuf->x * 4* ibuf->y;
1557         StripColorBalance cb = calc_cb(seq->strip->color_balance);
1558
1559         while (p < e) {
1560                 int c;
1561                 for (c = 0; c < 3; c++) {
1562                         p[c]= color_balance_fl(p[c], cb.lift[c], cb.gain[c], cb.gamma[c], mul);
1563                 }
1564                 p += 4;
1565         }
1566 }
1567
1568 static void color_balance(Sequence * seq, ImBuf* ibuf, float mul)
1569 {
1570         if (ibuf->rect_float) {
1571                 color_balance_float_float(seq, ibuf, mul);
1572         } else if(seq->flag & SEQ_MAKE_FLOAT) {
1573                 color_balance_byte_float(seq, ibuf, mul);
1574         } else {
1575                 color_balance_byte_byte(seq, ibuf, mul);
1576         }
1577 }
1578
1579 /*
1580   input preprocessing for SEQ_IMAGE, SEQ_MOVIE and SEQ_SCENE
1581
1582   Do all the things you can't really do afterwards using sequence effects
1583   (read: before rescaling to render resolution has been done)
1584
1585   Order is important!
1586
1587   - Deinterlace
1588   - Crop and transform in image source coordinate space
1589   - Flip X + Flip Y (could be done afterwards, backward compatibility)
1590   - Promote image to float data (affects pipeline operations afterwards)
1591   - Color balance (is most efficient in the byte -> float 
1592         (future: half -> float should also work fine!)
1593         case, if done on load, since we can use lookup tables)
1594   - Premultiply
1595
1596 */
1597
1598 int input_have_to_preprocess(
1599         SeqRenderData UNUSED(context), Sequence * seq, float UNUSED(cfra))
1600 {
1601         float mul;
1602
1603         if (seq->flag & (SEQ_FILTERY|SEQ_USE_CROP|SEQ_USE_TRANSFORM|SEQ_FLIPX|
1604                          SEQ_FLIPY|SEQ_USE_COLOR_BALANCE|SEQ_MAKE_PREMUL)) {
1605                 return TRUE;
1606         }
1607
1608         mul = seq->mul;
1609
1610         if(seq->blend_mode == SEQ_BLEND_REPLACE) {
1611                 mul *= seq->blend_opacity / 100.0f;
1612         }
1613
1614         if (mul != 1.0f) {
1615                 return TRUE;
1616         }
1617
1618         if (seq->sat != 1.0f) {
1619                 return TRUE;
1620         }
1621                 
1622         return FALSE;
1623 }
1624
1625 static ImBuf * input_preprocess(
1626         SeqRenderData context, Sequence *seq, float UNUSED(cfra), ImBuf * ibuf)
1627 {
1628         float mul;
1629
1630         ibuf = IMB_makeSingleUser(ibuf);
1631
1632         if((seq->flag & SEQ_FILTERY) && seq->type != SEQ_MOVIE) {
1633                 IMB_filtery(ibuf);
1634         }
1635
1636         if(seq->flag & (SEQ_USE_CROP|SEQ_USE_TRANSFORM)) {
1637                 StripCrop c= {0};
1638                 StripTransform t= {0};
1639                 int sx,sy,dx,dy;
1640
1641                 if(seq->flag & SEQ_USE_CROP && seq->strip->crop) {
1642                         c = *seq->strip->crop;
1643                 }
1644                 if(seq->flag & SEQ_USE_TRANSFORM && seq->strip->transform) {
1645                         t = *seq->strip->transform;
1646                 }
1647
1648                 sx = ibuf->x - c.left - c.right;
1649                 sy = ibuf->y - c.top - c.bottom;
1650                 dx = sx;
1651                 dy = sy;
1652
1653                 if (seq->flag & SEQ_USE_TRANSFORM) {
1654                         dx = context.scene->r.xsch;
1655                         dy = context.scene->r.ysch;
1656                 }
1657
1658                 if (c.top + c.bottom >= ibuf->y || c.left + c.right >= ibuf->x ||
1659                                 t.xofs >= dx || t.yofs >= dy) {
1660                         make_black_ibuf(ibuf);
1661                 } else {
1662                         ImBuf * i = IMB_allocImBuf(dx, dy,32, ibuf->rect_float ? IB_rectfloat : IB_rect);
1663
1664                         IMB_rectcpy(i, ibuf, t.xofs, t.yofs, c.left, c.bottom, sx, sy);
1665                         
1666                         IMB_freeImBuf(ibuf);
1667
1668                         ibuf = i;
1669                 }
1670         } 
1671
1672         if(seq->flag & SEQ_FLIPX) {
1673                 IMB_flipx(ibuf);
1674         }
1675         
1676         if(seq->flag & SEQ_FLIPY) {
1677                 IMB_flipy(ibuf);
1678         }
1679
1680         if(seq->sat != 1.0f) {
1681                 /* inline for now, could become an imbuf function */
1682                 int i;
1683                 unsigned char *rct= (unsigned char *)ibuf->rect;
1684                 float *rctf= ibuf->rect_float;
1685                 const float sat= seq->sat;
1686                 float hsv[3];
1687
1688                 if(rct) {
1689                         float rgb[3];
1690                         for (i = ibuf->x * ibuf->y; i > 0; i--, rct+=4) {
1691                                 rgb_byte_to_float(rct, rgb);
1692                                 rgb_to_hsv(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
1693                                 hsv_to_rgb(hsv[0], hsv[1] * sat, hsv[2], rgb, rgb+1, rgb+2);
1694                                 rgb_float_to_byte(rgb, rct);
1695                         }
1696                 }
1697
1698                 if(rctf) {
1699                         for (i = ibuf->x * ibuf->y; i > 0; i--, rctf+=4) {
1700                                 rgb_to_hsv(rctf[0], rctf[1], rctf[2], hsv, hsv+1, hsv+2);
1701                                 hsv_to_rgb(hsv[0], hsv[1] * sat, hsv[2], rctf, rctf+1, rctf+2);
1702                         }
1703                 }
1704         }
1705
1706         mul = seq->mul;
1707
1708         if(seq->blend_mode == SEQ_BLEND_REPLACE) {
1709                 mul *= seq->blend_opacity / 100.0f;
1710         }
1711
1712         if(seq->flag & SEQ_USE_COLOR_BALANCE && seq->strip->color_balance) {
1713                 color_balance(seq, ibuf, mul);
1714                 mul = 1.0;
1715         }
1716
1717         if(seq->flag & SEQ_MAKE_FLOAT) {
1718                 if (!ibuf->rect_float)
1719                         IMB_float_from_rect_simple(ibuf);
1720
1721                 if (ibuf->rect) {
1722                         imb_freerectImBuf(ibuf);
1723                 }
1724         }
1725
1726         if(mul != 1.0f) {
1727                 multibuf(ibuf, mul);
1728         }
1729
1730         if(seq->flag & SEQ_MAKE_PREMUL) {
1731                 if(ibuf->depth == 32 && ibuf->zbuf == NULL) {
1732                         IMB_premultiply_alpha(ibuf);
1733                 }
1734         }
1735
1736
1737         if(ibuf->x != context.rectx || ibuf->y != context.recty ) {
1738                 if(context.scene->r.mode & R_OSA) {
1739                         IMB_scaleImBuf(ibuf, (short)context.rectx, (short)context.recty);
1740                 } else {
1741                         IMB_scalefastImBuf(ibuf, (short)context.rectx, (short)context.recty);
1742                 }
1743         }
1744         return ibuf;
1745 }
1746
1747 static ImBuf * copy_from_ibuf_still(SeqRenderData context, Sequence * seq, 
1748                                     float nr)
1749 {
1750         ImBuf * rval = NULL;
1751         ImBuf * ibuf = NULL;
1752
1753         if (nr == 0) {
1754                 ibuf = seq_stripelem_cache_get(
1755                         context, seq, seq->start, 
1756                         SEQ_STRIPELEM_IBUF_STARTSTILL);
1757         } else if (nr == seq->len - 1) {
1758                 ibuf = seq_stripelem_cache_get(
1759                         context, seq, seq->start, 
1760                         SEQ_STRIPELEM_IBUF_ENDSTILL);
1761         }
1762
1763         if (ibuf) {
1764                 rval = IMB_dupImBuf(ibuf);
1765                 IMB_freeImBuf(ibuf);
1766         }
1767
1768         return rval;
1769 }
1770
1771 static void copy_to_ibuf_still(SeqRenderData context, Sequence * seq, float nr,
1772                                ImBuf * ibuf)
1773 {
1774         if (nr == 0 || nr == seq->len - 1) {
1775                 /* we have to store a copy, since the passed ibuf
1776                    could be preprocessed afterwards (thereby silently
1777                    changing the cached image... */
1778                 ibuf = IMB_dupImBuf(ibuf);
1779
1780                 if (nr == 0) {
1781                         seq_stripelem_cache_put(
1782                                 context, seq, seq->start, 
1783                                 SEQ_STRIPELEM_IBUF_STARTSTILL, ibuf);
1784                 } 
1785
1786                 if (nr == seq->len - 1) {
1787                         seq_stripelem_cache_put(
1788                                 context, seq, seq->start, 
1789                                 SEQ_STRIPELEM_IBUF_ENDSTILL, ibuf);
1790                 }
1791
1792                 IMB_freeImBuf(ibuf);
1793         }
1794 }
1795
1796 /* **********************************************************************
1797    strip rendering functions
1798    ********************************************************************** */
1799
1800 static ImBuf* seq_render_strip_stack( 
1801         SeqRenderData context, ListBase *seqbasep, float cfra, int chanshown);
1802
1803 static ImBuf * seq_render_strip(
1804         SeqRenderData context, Sequence * seq, float cfra);
1805
1806
1807 static ImBuf* seq_render_effect_strip_impl(
1808         SeqRenderData context, Sequence *seq, float cfra)
1809 {
1810         float fac, facf;
1811         int early_out;
1812         int i;
1813         struct SeqEffectHandle sh = get_sequence_effect(seq);
1814         FCurve *fcu= NULL;
1815         ImBuf * ibuf[3];
1816         Sequence *input[3];
1817         ImBuf * out = NULL;
1818
1819         ibuf[0] = ibuf[1] = ibuf[2] = NULL;
1820
1821         input[0] = seq->seq1; input[1] = seq->seq2; input[2] = seq->seq3;
1822
1823         if (!sh.execute) { /* effect not supported in this version... */
1824                 out = IMB_allocImBuf((short)context.rectx, 
1825                                      (short)context.recty, 32, IB_rect);
1826                 return out;
1827         }
1828
1829         if (seq->flag & SEQ_USE_EFFECT_DEFAULT_FADE) {
1830                 sh.get_default_fac(seq, cfra, &fac, &facf);
1831                 
1832                 if ((context.scene->r.mode & R_FIELDS)==0)
1833                         facf= fac;
1834         }
1835         else {
1836                 fcu = id_data_find_fcurve(&context.scene->id, seq, &RNA_Sequence, "effect_fader", 0, NULL);
1837                 if (fcu) {
1838                         fac = facf = evaluate_fcurve(fcu, cfra);
1839                         if( context.scene->r.mode & R_FIELDS ) {
1840                                 facf = evaluate_fcurve(fcu, cfra + 0.5f);
1841                         }
1842                 } else {
1843                         fac = facf = seq->effect_fader;
1844                 }
1845         }
1846
1847         early_out = sh.early_out(seq, fac, facf);
1848
1849         switch (early_out) {
1850         case EARLY_NO_INPUT:
1851                 out = sh.execute(context, seq, cfra, fac, facf, 
1852                                  NULL, NULL, NULL);
1853                 break;
1854         case EARLY_DO_EFFECT:
1855                 for(i=0; i<3; i++) {
1856                         if(input[i])
1857                                 ibuf[i] = seq_render_strip(
1858                                         context, input[i], cfra);
1859                 }
1860
1861                 if (ibuf[0] && ibuf[1]) {
1862                         out = sh.execute(context, seq, cfra, fac, facf,  
1863                                          ibuf[0], ibuf[1], ibuf[2]);
1864                 }
1865                 break;
1866         case EARLY_USE_INPUT_1:
1867                 if (input[0]) {
1868                         ibuf[0] = seq_render_strip(context, input[0], cfra);
1869                 }
1870                 if (ibuf[0]) {
1871                         if (input_have_to_preprocess(context, seq, cfra)) {
1872                                 out = IMB_dupImBuf(ibuf[0]);
1873                         } else {
1874                                 out = ibuf[0];
1875                                 IMB_refImBuf(out);
1876                         }
1877                 }
1878                 break;
1879         case EARLY_USE_INPUT_2:
1880                 if (input[1]) {
1881                         ibuf[1] = seq_render_strip(context, input[1], cfra);
1882                 }
1883                 if (ibuf[1]) {
1884                         if (input_have_to_preprocess(context, seq, cfra)) {
1885                                 out = IMB_dupImBuf(ibuf[1]);
1886                         } else {
1887                                 out = ibuf[1];
1888                                 IMB_refImBuf(out);
1889                         }
1890                 }
1891                 break;
1892         }
1893
1894         for (i = 0; i < 3; i++) {
1895                 IMB_freeImBuf(ibuf[i]);
1896         }
1897
1898         if (out == NULL) {
1899                 out = IMB_allocImBuf((short)context.rectx, (short)context.recty, 32, IB_rect);
1900         }
1901
1902         return out;
1903 }
1904
1905
1906 static ImBuf * seq_render_scene_strip_impl(
1907         SeqRenderData context, Sequence * seq, float nr)
1908 {
1909         ImBuf * ibuf = NULL;
1910         float frame= seq->sfra + nr + seq->anim_startofs;
1911         float oldcfra;
1912         Object *camera;
1913         ListBase oldmarkers;
1914         
1915         /* Old info:
1916            Hack! This function can be called from do_render_seq(), in that case
1917            the seq->scene can already have a Render initialized with same name,
1918            so we have to use a default name. (compositor uses scene name to
1919            find render).
1920            However, when called from within the UI (image preview in sequencer)
1921            we do want to use scene Render, that way the render result is defined
1922            for display in render/imagewindow
1923            
1924            Hmm, don't see, why we can't do that all the time,
1925            and since G.rendering is uhm, gone... (Peter)
1926         */
1927
1928         /* New info:
1929            Using the same name for the renders works just fine as the do_render_seq()
1930            render is not used while the scene strips are rendered.
1931            
1932            However rendering from UI (through sequencer_preview_area_draw) can crash in
1933            very many cases since other renders (material preview, an actual render etc.)
1934            can be started while this sequence preview render is running. The only proper
1935            solution is to make the sequencer preview render a proper job, which can be
1936            stopped when needed. This would also give a nice progress bar for the preview
1937            space so that users know there's something happening.
1938
1939            As a result the active scene now only uses OpenGL rendering for the sequencer
1940            preview. This is far from nice, but is the only way to prevent crashes at this
1941            time. 
1942
1943            -jahka
1944         */
1945
1946         int rendering = G.rendering;
1947         int doseq;
1948         int doseq_gl= G.rendering ? /*(scene->r.seq_flag & R_SEQ_GL_REND)*/ 0 : /*(scene->r.seq_flag & R_SEQ_GL_PREV)*/ 1;
1949         int have_seq= FALSE;
1950         Scene *scene;
1951
1952         /* dont refer to seq->scene above this point!, it can be NULL */
1953         if(seq->scene == NULL) {
1954                 return NULL;
1955         }
1956
1957         scene= seq->scene;
1958
1959         have_seq= (scene->r.scemode & R_DOSEQ) && scene->ed && scene->ed->seqbase.first;
1960
1961         oldcfra= scene->r.cfra; 
1962         scene->r.cfra= frame;
1963
1964         if(seq->scene_camera)   
1965                 camera= seq->scene_camera;
1966         else {  
1967                 scene_camera_switch_update(scene);
1968                 camera= scene->camera;
1969         }
1970
1971         if(have_seq==FALSE && camera==NULL) {
1972                 scene->r.cfra= oldcfra;
1973                 return NULL;
1974         }
1975
1976         /* prevent eternal loop */
1977         doseq= context.scene->r.scemode & R_DOSEQ;
1978         context.scene->r.scemode &= ~R_DOSEQ;
1979         
1980 #ifdef DURIAN_CAMERA_SWITCH
1981         /* stooping to new low's in hackyness :( */
1982         oldmarkers= scene->markers;
1983         scene->markers.first= scene->markers.last= NULL;
1984 #endif
1985         
1986         if(sequencer_view3d_cb && BLI_thread_is_main() && doseq_gl && (scene == context.scene || have_seq==0) && camera) {
1987                 char err_out[256]= "unknown";
1988                 /* for old scened this can be uninitialized, should probably be added to do_versions at some point if the functionality stays */
1989                 if(context.scene->r.seq_prev_type==0)
1990                         context.scene->r.seq_prev_type = 3 /* ==OB_SOLID */; 
1991
1992                 /* opengl offscreen render */
1993                 scene_update_for_newframe(context.bmain, scene, scene->lay);
1994                 ibuf= sequencer_view3d_cb(scene, camera, context.rectx, context.recty, IB_rect, context.scene->r.seq_prev_type, err_out);
1995                 if(ibuf == NULL) {
1996                         fprintf(stderr, "seq_render_scene_strip_impl failed to get opengl buffer: %s\n", err_out);
1997                 }
1998         }
1999         else {
2000                 Render *re = RE_GetRender(scene->id.name);
2001                 RenderResult rres;
2002
2003                 /* XXX: this if can be removed when sequence preview rendering uses the job system */
2004                 if(rendering || context.scene != scene) {
2005                         if(re==NULL)
2006                                 re= RE_NewRender(scene->id.name);
2007                         
2008                         RE_BlenderFrame(re, context.bmain, scene, NULL, camera, scene->lay, frame, FALSE);
2009
2010                         /* restore previous state after it was toggled on & off by RE_BlenderFrame */
2011                         G.rendering = rendering;
2012                 }
2013                 
2014                 RE_AcquireResultImage(re, &rres);
2015                 
2016                 if(rres.rectf) {
2017                         ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rectfloat);
2018                         memcpy(ibuf->rect_float, rres.rectf, 4*sizeof(float)*rres.rectx*rres.recty);
2019                         if(rres.rectz) {
2020                                 addzbuffloatImBuf(ibuf);
2021                                 memcpy(ibuf->zbuf_float, rres.rectz, sizeof(float)*rres.rectx*rres.recty);
2022                         }
2023
2024                         /* float buffers in the sequencer are not linear */
2025                         ibuf->profile= IB_PROFILE_LINEAR_RGB;
2026                         IMB_convert_profile(ibuf, IB_PROFILE_SRGB);                     
2027                 }
2028                 else if (rres.rect32) {
2029                         ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rect);
2030                         memcpy(ibuf->rect, rres.rect32, 4*rres.rectx*rres.recty);
2031                 }
2032                 
2033                 RE_ReleaseResultImage(re);
2034                 
2035                 // BIF_end_render_callbacks();
2036         }
2037         
2038         /* restore */
2039         context.scene->r.scemode |= doseq;
2040         
2041         scene->r.cfra = oldcfra;
2042
2043         if(frame != oldcfra)
2044                 scene_update_for_newframe(context.bmain, scene, scene->lay);
2045         
2046 #ifdef DURIAN_CAMERA_SWITCH
2047         /* stooping to new low's in hackyness :( */
2048         scene->markers= oldmarkers;
2049 #endif
2050
2051         return ibuf;
2052 }
2053
2054 static ImBuf * seq_render_strip(SeqRenderData context, Sequence * seq, float cfra)
2055 {
2056         ImBuf * ibuf = NULL;
2057         char name[FILE_MAXDIR+FILE_MAXFILE];
2058         int use_preprocess = input_have_to_preprocess(context, seq, cfra);
2059         float nr = give_stripelem_index(seq, cfra);
2060         /* all effects are handled similarly with the exception of speed effect */
2061         int type = (seq->type & SEQ_EFFECT && seq->type != SEQ_SPEED) ? SEQ_EFFECT : seq->type;
2062
2063         ibuf = seq_stripelem_cache_get(context, seq, cfra, SEQ_STRIPELEM_IBUF);
2064
2065         /* currently, we cache preprocessed images in SEQ_STRIPELEM_IBUF,
2066            but not(!) on SEQ_STRIPELEM_IBUF_ENDSTILL and ..._STARTSTILL */
2067         if (ibuf)
2068                 use_preprocess = FALSE;
2069
2070         if (ibuf == NULL)
2071                 ibuf = copy_from_ibuf_still(context, seq, nr);
2072         
2073         if (ibuf == NULL)
2074                 ibuf = seq_proxy_fetch(context, seq, cfra);
2075
2076         if(ibuf == NULL) switch(type) {
2077                 case SEQ_META:
2078                 {
2079                         ImBuf * meta_ibuf = NULL;
2080
2081                         if(seq->seqbase.first)
2082                                 meta_ibuf = seq_render_strip_stack(
2083                                         context, &seq->seqbase,
2084                                         seq->start + nr, 0);
2085
2086                         if(meta_ibuf) {
2087                                 ibuf = meta_ibuf;
2088                                 if(ibuf && use_preprocess) {
2089                                         struct ImBuf * i = IMB_dupImBuf(ibuf);
2090
2091                                         IMB_freeImBuf(ibuf);
2092
2093                                         ibuf = i;
2094                                 }
2095                         }
2096                         break;
2097                 }
2098                 case SEQ_SPEED:
2099                 {
2100                         ImBuf * child_ibuf = NULL;
2101
2102                         float f_cfra;
2103                         SpeedControlVars * s = (SpeedControlVars *)seq->effectdata;
2104
2105                         sequence_effect_speed_rebuild_map(context.scene,seq, 0);
2106
2107                         /* weeek! */
2108                         f_cfra = seq->start + s->frameMap[(int) nr];
2109
2110                         child_ibuf = seq_render_strip(context,seq->seq1,f_cfra);
2111
2112                         if (child_ibuf) {
2113                                 ibuf = child_ibuf;
2114                                 if(ibuf && use_preprocess) {
2115                                         struct ImBuf * i = IMB_dupImBuf(ibuf);
2116
2117                                         IMB_freeImBuf(ibuf);
2118
2119                                         ibuf = i;
2120                                 }
2121                         }
2122                         break;
2123                 }
2124                 case SEQ_EFFECT:
2125                 {
2126                         ibuf = seq_render_effect_strip_impl(
2127                                 context, seq, seq->start + nr);
2128                         break;
2129                 }
2130                 case SEQ_IMAGE:
2131                 {
2132                         StripElem * s_elem = give_stripelem(seq, cfra);
2133
2134                         if (s_elem) {
2135                                 BLI_join_dirfile(name, sizeof(name), seq->strip->dir, s_elem->name);
2136                                 BLI_path_abs(name, G.main->name);
2137                         }
2138
2139                         if (s_elem && (ibuf = IMB_loadiffname(name, IB_rect))) {
2140                                 /* we don't need both (speed reasons)! */
2141                                 if (ibuf->rect_float && ibuf->rect)
2142                                         imb_freerectImBuf(ibuf);
2143
2144                                 /* all sequencer color is done in SRGB space, linear gives odd crossfades */
2145                                 if(ibuf->profile == IB_PROFILE_LINEAR_RGB)
2146                                         IMB_convert_profile(ibuf, IB_PROFILE_NONE);
2147
2148                                 copy_to_ibuf_still(context, seq, nr, ibuf);
2149
2150                                 s_elem->orig_width  = ibuf->x;
2151                                 s_elem->orig_height = ibuf->y;
2152                         }
2153                         break;
2154                 }
2155                 case SEQ_MOVIE:
2156                 {
2157                         seq_open_anim_file(seq);
2158
2159                         if(seq->anim) {
2160                                 IMB_anim_set_preseek(seq->anim,
2161                                                      seq->anim_preseek);
2162
2163                                 ibuf = IMB_anim_absolute(
2164                                         seq->anim, nr + seq->anim_startofs, 
2165                                         seq->strip->proxy ? 
2166                                         seq->strip->proxy->tc
2167                                         : IMB_TC_RECORD_RUN, 
2168                                         seq_rendersize_to_proxysize(
2169                                                 context.preview_render_size));
2170
2171                                 /* we don't need both (speed reasons)! */
2172                                 if (ibuf && ibuf->rect_float && ibuf->rect)
2173                                         imb_freerectImBuf(ibuf);
2174                                 if (ibuf) {
2175                                         seq->strip->stripdata->orig_width = ibuf->x;
2176                                         seq->strip->stripdata->orig_height = ibuf->y;
2177                                 }
2178                         }
2179                         copy_to_ibuf_still(context, seq, nr, ibuf);
2180                         break;
2181                 }
2182                 case SEQ_SCENE:
2183                 {       // scene can be NULL after deletions
2184                         ibuf = seq_render_scene_strip_impl(context, seq, nr);
2185
2186                         /* Scene strips update all animation, so we need to restore original state.*/
2187                         BKE_animsys_evaluate_all_animation(context.bmain, context.scene, cfra);
2188
2189                         copy_to_ibuf_still(context, seq, nr, ibuf);
2190                         break;
2191                 }
2192         }
2193
2194         if (ibuf == NULL)
2195                 ibuf = IMB_allocImBuf((short)context.rectx, (short)context.recty, 32, IB_rect);
2196
2197         if (ibuf->x != context.rectx || ibuf->y != context.recty)
2198                 use_preprocess = TRUE;
2199
2200         if (use_preprocess)
2201                 ibuf = input_preprocess(context, seq, cfra, ibuf);
2202
2203         seq_stripelem_cache_put(context, seq, cfra, SEQ_STRIPELEM_IBUF, ibuf);
2204
2205         return ibuf;
2206 }
2207
2208 /* **********************************************************************
2209    strip stack rendering functions
2210    ********************************************************************** */
2211
2212 static int seq_must_swap_input_in_blend_mode(Sequence * seq)
2213 {
2214         int swap_input = FALSE;
2215
2216         /* bad hack, to fix crazy input ordering of 
2217            those two effects */
2218
2219         if (ELEM3(seq->blend_mode, SEQ_ALPHAOVER, SEQ_ALPHAUNDER, SEQ_OVERDROP)) {
2220                 swap_input = TRUE;
2221         }
2222         
2223         return swap_input;
2224 }
2225
2226 static int seq_get_early_out_for_blend_mode(Sequence * seq)
2227 {
2228         struct SeqEffectHandle sh = get_sequence_blend(seq);
2229         float facf = seq->blend_opacity / 100.0f;
2230         int early_out = sh.early_out(seq, facf, facf);
2231         
2232         if (ELEM(early_out, EARLY_DO_EFFECT, EARLY_NO_INPUT)) {
2233                 return early_out;
2234         }
2235
2236         if (seq_must_swap_input_in_blend_mode(seq)) {
2237                 if (early_out == EARLY_USE_INPUT_2) {
2238                         return EARLY_USE_INPUT_1;
2239                 } else if (early_out == EARLY_USE_INPUT_1) {
2240                         return EARLY_USE_INPUT_2;
2241                 }
2242         }
2243         return early_out;
2244 }
2245
2246 static ImBuf* seq_render_strip_stack(
2247         SeqRenderData context, ListBase *seqbasep, float cfra, int chanshown)
2248 {
2249         Sequence* seq_arr[MAXSEQ+1];
2250         int count;
2251         int i;
2252         ImBuf* out = NULL;
2253
2254         count = get_shown_sequences(seqbasep, cfra, chanshown, (Sequence **)&seq_arr);
2255
2256         if (count == 0) {
2257                 return NULL;
2258         }
2259
2260 #if 0 /* commentind since this breaks keyframing, since it resets the value on draw */
2261         if(scene->r.cfra != cfra) {
2262                 // XXX for prefetch and overlay offset!..., very bad!!!
2263                 AnimData *adt= BKE_animdata_from_id(&scene->id);
2264                 BKE_animsys_evaluate_animdata(scene, &scene->id, adt, cfra, ADT_RECALC_ANIM);
2265         }
2266 #endif
2267
2268         out = seq_stripelem_cache_get(context, seq_arr[count - 1], 
2269                                       cfra, SEQ_STRIPELEM_IBUF_COMP);
2270
2271         if (out) {
2272                 return out;
2273         }
2274         
2275         if(count == 1) {
2276                 out = seq_render_strip(context, seq_arr[0], cfra);
2277                 seq_stripelem_cache_put(context, seq_arr[0], cfra, 
2278                                         SEQ_STRIPELEM_IBUF_COMP, out);
2279
2280                 return out;
2281         }
2282
2283
2284         for (i = count - 1; i >= 0; i--) {
2285                 int early_out;
2286                 Sequence *seq = seq_arr[i];
2287
2288                 out = seq_stripelem_cache_get(
2289                         context, seq, cfra, SEQ_STRIPELEM_IBUF_COMP);
2290
2291                 if (out) {
2292                         break;
2293                 }
2294                 if (seq->blend_mode == SEQ_BLEND_REPLACE) {
2295                         out = seq_render_strip(context, seq, cfra);
2296                         break;
2297                 }
2298
2299                 early_out = seq_get_early_out_for_blend_mode(seq);
2300
2301                 switch (early_out) {
2302                 case EARLY_NO_INPUT:
2303                 case EARLY_USE_INPUT_2:
2304                         out = seq_render_strip(context, seq, cfra);
2305                         break;
2306                 case EARLY_USE_INPUT_1:
2307                         if (i == 0) {
2308                                 out = IMB_allocImBuf((short)context.rectx, (short)context.recty, 32, IB_rect);
2309                         }
2310                         break;
2311                 case EARLY_DO_EFFECT:
2312                         if (i == 0) {
2313                                 out = seq_render_strip(context, seq, cfra);
2314                         }
2315
2316                         break;
2317                 }
2318                 if (out) {
2319                         break;
2320                 }
2321         }
2322
2323         seq_stripelem_cache_put(context, seq_arr[i], cfra, 
2324                                 SEQ_STRIPELEM_IBUF_COMP, out);
2325
2326
2327         i++;
2328
2329         for (; i < count; i++) {
2330                 Sequence * seq = seq_arr[i];
2331
2332                 if (seq_get_early_out_for_blend_mode(seq) == EARLY_DO_EFFECT) {
2333                         struct SeqEffectHandle sh = get_sequence_blend(seq);
2334                         ImBuf * ibuf1 = out;
2335                         ImBuf * ibuf2 = seq_render_strip(context, seq, cfra);
2336
2337                         float facf = seq->blend_opacity / 100.0f;
2338                         int swap_input = seq_must_swap_input_in_blend_mode(seq);
2339
2340                         if (swap_input) {
2341                                 out = sh.execute(context, seq, cfra, 
2342                                                  facf, facf, 
2343                                                  ibuf2, ibuf1, NULL);
2344                         } else {
2345                                 out = sh.execute(context, seq, cfra, 
2346                                                  facf, facf, 
2347                                                  ibuf1, ibuf2, NULL);
2348                         }
2349                 
2350                         IMB_freeImBuf(ibuf1);
2351                         IMB_freeImBuf(ibuf2);
2352                 }
2353
2354                 seq_stripelem_cache_put(context, seq_arr[i], cfra,
2355                                         SEQ_STRIPELEM_IBUF_COMP, out);
2356         }
2357
2358         return out;
2359 }
2360
2361 /*
2362  * returned ImBuf is refed!
2363  * you have to free after usage!
2364  */
2365
2366 ImBuf *give_ibuf_seq(SeqRenderData context, float cfra, int chanshown)
2367 {
2368         Editing *ed= seq_give_editing(context.scene, FALSE);
2369         int count;
2370         ListBase *seqbasep;
2371         
2372         if(ed==NULL) return NULL;
2373
2374         count = BLI_countlist(&ed->metastack);
2375         if((chanshown < 0) && (count > 0)) {
2376                 count = MAX2(count + chanshown, 0);
2377                 seqbasep= ((MetaStack*)BLI_findlink(&ed->metastack, count))->oldbasep;
2378         } else {
2379                 seqbasep= ed->seqbasep;
2380         }
2381
2382         return seq_render_strip_stack(context, seqbasep, cfra, chanshown);
2383 }
2384
2385 ImBuf *give_ibuf_seqbase(SeqRenderData context, float cfra, int chanshown, ListBase *seqbasep)
2386 {
2387         return seq_render_strip_stack(context, seqbasep, cfra, chanshown);
2388 }
2389
2390
2391 ImBuf *give_ibuf_seq_direct(SeqRenderData context, float cfra, Sequence *seq)
2392 {
2393         return seq_render_strip(context, seq, cfra);
2394 }
2395
2396 #if 0
2397 /* check used when we need to change seq->blend_mode but not to effect or audio strips */
2398 static int seq_can_blend(Sequence *seq)
2399 {
2400         if (ELEM4(seq->type, SEQ_IMAGE, SEQ_META, SEQ_SCENE, SEQ_MOVIE)) {
2401                 return 1;
2402         } else {
2403                 return 0;
2404         }
2405 }
2406 #endif
2407
2408 /* *********************** threading api ******************* */
2409
2410 static ListBase running_threads;
2411 static ListBase prefetch_wait;
2412 static ListBase prefetch_done;
2413
2414 static pthread_mutex_t queue_lock          = PTHREAD_MUTEX_INITIALIZER;
2415 static pthread_mutex_t wakeup_lock         = PTHREAD_MUTEX_INITIALIZER;
2416 static pthread_cond_t  wakeup_cond         = PTHREAD_COND_INITIALIZER;
2417
2418 //static pthread_mutex_t prefetch_ready_lock = PTHREAD_MUTEX_INITIALIZER;
2419 //static pthread_cond_t  prefetch_ready_cond = PTHREAD_COND_INITIALIZER;
2420
2421 static pthread_mutex_t frame_done_lock     = PTHREAD_MUTEX_INITIALIZER;
2422 static pthread_cond_t  frame_done_cond     = PTHREAD_COND_INITIALIZER;
2423
2424 static volatile int seq_thread_shutdown = TRUE; 
2425 static volatile int seq_last_given_monoton_cfra = 0;
2426 static int monoton_cfra = 0;
2427
2428 typedef struct PrefetchThread {
2429         struct PrefetchThread *next, *prev;
2430         
2431         Scene *scene;
2432         struct PrefetchQueueElem *current;
2433         pthread_t pthread;
2434         int running;
2435         
2436 } PrefetchThread;
2437
2438 typedef struct PrefetchQueueElem {
2439         struct PrefetchQueueElem *next, *prev;
2440         
2441         int rectx;
2442         int recty;
2443         float cfra;
2444         int chanshown;
2445         int preview_render_size;
2446
2447         int monoton_cfra;
2448
2449         struct ImBuf * ibuf;
2450 } PrefetchQueueElem;
2451
2452 #if 0
2453 static void *seq_prefetch_thread(void * This_)
2454 {
2455         PrefetchThread * This = This_;
2456
2457         while (!seq_thread_shutdown) {
2458                 PrefetchQueueElem *e;
2459                 int s_last;
2460
2461                 pthread_mutex_lock(&queue_lock);
2462                 e = prefetch_wait.first;
2463                 if (e) {
2464                         BLI_remlink(&prefetch_wait, e);
2465                 }
2466                 s_last = seq_last_given_monoton_cfra;
2467
2468                 This->current = e;
2469
2470                 pthread_mutex_unlock(&queue_lock);
2471
2472                 if (!e) {
2473                         pthread_mutex_lock(&prefetch_ready_lock);
2474
2475                         This->running = FALSE;
2476
2477                         pthread_cond_signal(&prefetch_ready_cond);
2478                         pthread_mutex_unlock(&prefetch_ready_lock);
2479
2480                         pthread_mutex_lock(&wakeup_lock);
2481                         if (!seq_thread_shutdown) {
2482                                 pthread_cond_wait(&wakeup_cond, &wakeup_lock);
2483                         }
2484                         pthread_mutex_unlock(&wakeup_lock);
2485                         continue;
2486                 }
2487
2488                 This->running = TRUE;
2489                 
2490                 if (e->cfra >= s_last) { 
2491                         e->ibuf = give_ibuf_seq_impl(This->scene, 
2492                                 e->rectx, e->recty, e->cfra, e->chanshown,
2493                                 e->preview_render_size);
2494                 }
2495
2496                 pthread_mutex_lock(&queue_lock);
2497
2498                 BLI_addtail(&prefetch_done, e);
2499
2500                 for (e = prefetch_wait.first; e; e = e->next) {
2501                         if (s_last > e->monoton_cfra) {
2502                                 BLI_remlink(&prefetch_wait, e);
2503                                 MEM_freeN(e);
2504                         }
2505                 }
2506
2507                 for (e = prefetch_done.first; e; e = e->next) {
2508                         if (s_last > e->monoton_cfra) {
2509                                 if (e->ibuf) {
2510                                         IMB_cache_limiter_unref(e->ibuf);
2511                                 }
2512                                 BLI_remlink(&prefetch_done, e);
2513                                 MEM_freeN(e);
2514                         }
2515                 }
2516
2517                 pthread_mutex_unlock(&queue_lock);
2518
2519                 pthread_mutex_lock(&frame_done_lock);
2520                 pthread_cond_signal(&frame_done_cond);
2521                 pthread_mutex_unlock(&frame_done_lock);
2522         }
2523         return 0;
2524 }
2525
2526 static void seq_start_threads(Scene *scene)
2527 {
2528         int i;
2529
2530         running_threads.first = running_threads.last = NULL;
2531         prefetch_wait.first = prefetch_wait.last = NULL;
2532         prefetch_done.first = prefetch_done.last = NULL;
2533
2534         seq_thread_shutdown = FALSE;
2535         seq_last_given_monoton_cfra = monoton_cfra = 0;
2536
2537         /* since global structures are modified during the processing
2538            of one frame, only one render thread is currently possible... 
2539
2540            (but we code, in the hope, that we can remove this restriction
2541            soon...)
2542         */
2543
2544         fprintf(stderr, "SEQ-THREAD: seq_start_threads\n");
2545
2546         for (i = 0; i < 1; i++) {
2547                 PrefetchThread *t = MEM_callocN(sizeof(PrefetchThread), "prefetch_thread");
2548                 t->scene= scene;
2549                 t->running = TRUE;
2550                 BLI_addtail(&running_threads, t);
2551
2552                 pthread_create(&t->pthread, NULL, seq_prefetch_thread, t);
2553         }
2554
2555         /* init malloc mutex */
2556         BLI_init_threads(0, 0, 0);
2557 }
2558
2559 static void seq_stop_threads()
2560 {
2561         PrefetchThread *tslot;
2562         PrefetchQueueElem *e;
2563
2564         fprintf(stderr, "SEQ-THREAD: seq_stop_threads()\n");
2565
2566         if (seq_thread_shutdown) {
2567                 fprintf(stderr, "SEQ-THREAD: ... already stopped\n");
2568                 return;
2569         }
2570         
2571         pthread_mutex_lock(&wakeup_lock);
2572
2573         seq_thread_shutdown = TRUE;
2574
2575                 pthread_cond_broadcast(&wakeup_cond);
2576                 pthread_mutex_unlock(&wakeup_lock);
2577
2578         for(tslot = running_threads.first; tslot; tslot= tslot->next) {
2579                 pthread_join(tslot->pthread, NULL);
2580         }
2581
2582
2583         for (e = prefetch_wait.first; e; e = e->next) {
2584                 BLI_remlink(&prefetch_wait, e);
2585                 MEM_freeN(e);
2586         }
2587
2588         for (e = prefetch_done.first; e; e = e->next) {
2589                 if (e->ibuf) {
2590                         IMB_cache_limiter_unref(e->ibuf);
2591                 }
2592                 BLI_remlink(&prefetch_done, e);
2593                 MEM_freeN(e);
2594         }
2595
2596         BLI_freelistN(&running_threads);
2597
2598         /* deinit malloc mutex */
2599         BLI_end_threads(0);
2600 }
2601 #endif
2602
2603 void give_ibuf_prefetch_request(SeqRenderData context, float cfra, int chanshown)
2604 {
2605         PrefetchQueueElem *e;
2606         if (seq_thread_shutdown) {
2607                 return;
2608         }
2609
2610         e = MEM_callocN(sizeof(PrefetchQueueElem), "prefetch_queue_elem");
2611         e->rectx = context.rectx;
2612         e->recty = context.recty;
2613         e->cfra = cfra;
2614         e->chanshown = chanshown;
2615         e->preview_render_size = context.preview_render_size;
2616         e->monoton_cfra = monoton_cfra++;
2617
2618         pthread_mutex_lock(&queue_lock);
2619         BLI_addtail(&prefetch_wait, e);
2620         pthread_mutex_unlock(&queue_lock);
2621         
2622         pthread_mutex_lock(&wakeup_lock);
2623         pthread_cond_signal(&wakeup_cond);
2624         pthread_mutex_unlock(&wakeup_lock);
2625 }
2626
2627 #if 0
2628 static void seq_wait_for_prefetch_ready()
2629 {
2630         PrefetchThread *tslot;
2631
2632         if (seq_thread_shutdown) {
2633                 return;
2634         }
2635
2636         fprintf(stderr, "SEQ-THREAD: rendering prefetch frames...\n");
2637
2638         pthread_mutex_lock(&prefetch_ready_lock);
2639
2640         for(;;) {
2641                 for(tslot = running_threads.first; tslot; tslot= tslot->next) {
2642                         if (tslot->running) {
2643                                 break;
2644                         }
2645                 }
2646                 if (!tslot) {
2647                         break;
2648                 }
2649                 pthread_cond_wait(&prefetch_ready_cond, &prefetch_ready_lock);
2650         }
2651
2652         pthread_mutex_unlock(&prefetch_ready_lock);
2653
2654         fprintf(stderr, "SEQ-THREAD: prefetch done\n");
2655 }
2656 #endif
2657
2658 ImBuf *give_ibuf_seq_threaded(SeqRenderData context, float cfra, int chanshown)
2659 {
2660         PrefetchQueueElem *e = NULL;
2661         int found_something = FALSE;
2662
2663         if (seq_thread_shutdown) {
2664                 return give_ibuf_seq(context, cfra, chanshown);
2665         }
2666
2667         while (!e) {
2668                 int success = FALSE;
2669                 pthread_mutex_lock(&queue_lock);
2670
2671                 for (e = prefetch_done.first; e; e = e->next) {
2672                         if (cfra == e->cfra &&
2673                                 chanshown == e->chanshown &&
2674                                 context.rectx == e->rectx && 
2675                                 context.recty == e->recty &&
2676                                 context.preview_render_size == e->preview_render_size) {
2677                                 success = TRUE;
2678                                 found_something = TRUE;
2679                                 break;
2680                         }
2681                 }
2682
2683                 if (!e) {
2684                         for (e = prefetch_wait.first; e; e = e->next) {
2685                                 if (cfra == e->cfra &&
2686                                         chanshown == e->chanshown &&
2687                                         context.rectx == e->rectx && 
2688                                         context.recty == e->recty &&
2689                                         context.preview_render_size == e->preview_render_size) {
2690                                         found_something = TRUE;
2691                                         break;
2692                                 }
2693                         }
2694                 }
2695
2696                 if (!e) {
2697                         PrefetchThread *tslot;
2698
2699                         for(tslot = running_threads.first; 
2700                                 tslot; tslot= tslot->next) {
2701                                 if (tslot->current &&
2702                                         cfra == tslot->current->cfra &&
2703                                         chanshown == tslot->current->chanshown &&
2704                                         context.rectx == tslot->current->rectx && 
2705                                         context.recty == tslot->current->recty &&
2706                                         context.preview_render_size== tslot->current->preview_render_size){
2707                                         found_something = TRUE;
2708                                         break;
2709                                 }
2710                         }
2711                 }
2712
2713                 /* e->ibuf is unrefed by render thread on next round. */
2714
2715                 if (e) {
2716                         seq_last_given_monoton_cfra = e->monoton_cfra;
2717                 }
2718
2719                 pthread_mutex_unlock(&queue_lock);
2720
2721                 if (!success) {
2722                         e = NULL;
2723
2724                         if (!found_something) {
2725                                 fprintf(stderr, 
2726                                         "SEQ-THREAD: Requested frame "
2727                                         "not in queue ???\n");
2728                                 break;
2729                         }
2730                         pthread_mutex_lock(&frame_done_lock);
2731                         pthread_cond_wait(&frame_done_cond, &frame_done_lock);
2732                         pthread_mutex_unlock(&frame_done_lock);
2733                 }
2734         }
2735         
2736         return e ? e->ibuf : NULL;
2737 }
2738
2739 /* Functions to free imbuf and anim data on changes */
2740
2741 static void free_anim_seq(Sequence *seq)
2742 {
2743         if(seq->anim) {
2744                 IMB_free_anim(seq->anim);
2745                 seq->anim = NULL;
2746         }
2747 }
2748
2749 void free_imbuf_seq(Scene *scene, ListBase * seqbase, int check_mem_usage,
2750                     int keep_file_handles)
2751 {
2752         Sequence *seq;
2753
2754         if (check_mem_usage) {
2755                 /* Let the cache limitor take care of this (schlaile) */
2756                 /* While render let's keep all memory available for render 
2757                    (ton)
2758                    At least if free memory is tight...
2759                    This can make a big difference in encoding speed
2760                    (it is around 4 times(!) faster, if we do not waste time
2761                    on freeing _all_ buffers every time on long timelines...)
2762                    (schlaile)
2763                 */
2764         
2765                 uintptr_t mem_in_use;
2766                 uintptr_t mmap_in_use;
2767                 uintptr_t max;
2768         
2769                 mem_in_use= MEM_get_memory_in_use();
2770                 mmap_in_use= MEM_get_mapped_memory_in_use();
2771                 max = MEM_CacheLimiter_get_maximum();
2772         
2773                 if (max == 0 || mem_in_use + mmap_in_use <= max) {
2774                         return;
2775                 }
2776         }
2777
2778         seq_stripelem_cache_cleanup();
2779         
2780         for(seq= seqbase->first; seq; seq= seq->next) {
2781                 if(seq->strip) {
2782                         if(seq->type==SEQ_MOVIE && !keep_file_handles)
2783                                 free_anim_seq(seq);
2784                         if(seq->type==SEQ_SPEED) {
2785                                 sequence_effect_speed_rebuild_map(scene, seq, 1);
2786                         }
2787                 }
2788                 if(seq->type==SEQ_META) {
2789                         free_imbuf_seq(scene, &seq->seqbase, FALSE, keep_file_handles);
2790                 }
2791                 if(seq->type==SEQ_SCENE) {
2792                         /* FIXME: recurs downwards, 
2793                            but do recurs protection somehow! */
2794                 }
2795         }
2796         
2797 }
2798
2799 static int update_changed_seq_recurs(Scene *scene, Sequence *seq, Sequence *changed_seq, int len_change, int ibuf_change)
2800 {
2801         Sequence *subseq;
2802         int free_imbuf = 0;
2803         
2804         /* recurs downwards to see if this seq depends on the changed seq */
2805         
2806         if(seq == NULL)
2807                 return 0;
2808         
2809         if(seq == changed_seq)
2810                 free_imbuf = 1;
2811         
2812         for(subseq=seq->seqbase.first; subseq; subseq=subseq->next)
2813                 if(update_changed_seq_recurs(scene, subseq, changed_seq, len_change, ibuf_change))
2814                         free_imbuf = TRUE;
2815         
2816         if(seq->seq1)
2817                 if(update_changed_seq_recurs(scene, seq->seq1, changed_seq, len_change, ibuf_change))
2818                         free_imbuf = TRUE;
2819         if(seq->seq2 && (seq->seq2 != seq->seq1))
2820                 if(update_changed_seq_recurs(scene, seq->seq2, changed_seq, len_change, ibuf_change))
2821                         free_imbuf = TRUE;
2822         if(seq->seq3 && (seq->seq3 != seq->seq1) && (seq->seq3 != seq->seq2))
2823                 if(update_changed_seq_recurs(scene, seq->seq3, changed_seq, len_change, ibuf_change))
2824                         free_imbuf = TRUE;
2825         
2826         if(free_imbuf) {
2827                 if(ibuf_change) {
2828                         if(seq->type == SEQ_MOVIE)
2829                                 free_anim_seq(seq);
2830                         if(seq->type == SEQ_SPEED) {
2831                                 sequence_effect_speed_rebuild_map(scene, seq, 1);
2832                         }
2833                 }
2834                 
2835                 if(len_change)
2836                         calc_sequence(scene, seq);
2837         }
2838         
2839         return free_imbuf;
2840 }
2841
2842 void update_changed_seq_and_deps(Scene *scene, Sequence *changed_seq, int len_change, int ibuf_change)
2843 {
2844         Editing *ed= seq_give_editing(scene, FALSE);
2845         Sequence *seq;
2846         
2847         if (ed==NULL) return;
2848         
2849         for (seq=ed->seqbase.first; seq; seq=seq->next)
2850                 update_changed_seq_recurs(scene, seq, changed_seq, len_change, ibuf_change);
2851 }
2852
2853 /* seq funcs's for transforming internally
2854  notice the difference between start/end and left/right.
2855
2856  left and right are the bounds at which the sequence is rendered,
2857 start and end are from the start and fixed length of the sequence.
2858 */
2859 int seq_tx_get_start(Sequence *seq)
2860 {
2861         return seq->start;
2862 }
2863 int seq_tx_get_end(Sequence *seq)
2864 {
2865         return seq->start+seq->len;
2866 }
2867
2868 int seq_tx_get_final_left(Sequence *seq, int metaclip)
2869 {
2870         if (metaclip && seq->tmp) {
2871                 /* return the range clipped by the parents range */
2872                 return MAX2( seq_tx_get_final_left(seq, 0), seq_tx_get_final_left((Sequence *)seq->tmp, 1) );
2873         } else {
2874                 return (seq->start - seq->startstill) + seq->startofs;
2875         }
2876
2877 }
2878 int seq_tx_get_final_right(Sequence *seq, int metaclip)
2879 {
2880         if (metaclip && seq->tmp) {
2881                 /* return the range clipped by the parents range */
2882                 return MIN2( seq_tx_get_final_right(seq, 0), seq_tx_get_final_right((Sequence *)seq->tmp, 1) );
2883         } else {
2884                 return ((seq->start+seq->len) + seq->endstill) - seq->endofs;
2885         }
2886 }
2887
2888 void seq_tx_set_final_left(Sequence *seq, int val)
2889 {
2890         if (val < (seq)->start) {
2891                 seq->startstill = abs(val - (seq)->start);
2892                 seq->startofs = 0;
2893         } else {
2894                 seq->startofs = abs(val - (seq)->start);
2895                 seq->startstill = 0;
2896         }
2897 }
2898
2899 void seq_tx_set_final_right(Sequence *seq, int val)
2900 {
2901         if (val > (seq)->start + (seq)->len) {
2902                 seq->endstill = abs(val - (seq->start + (seq)->len));
2903                 seq->endofs = 0;
2904         } else {
2905                 seq->endofs = abs(val - ((seq)->start + (seq)->len));
2906                 seq->endstill = 0;
2907         }
2908 }
2909
2910 /* used so we can do a quick check for single image seq
2911    since they work a bit differently to normal image seq's (during transform) */
2912 int seq_single_check(Sequence *seq)
2913 {
2914         return (seq->len==1 && (
2915                         seq->type == SEQ_IMAGE 
2916                         || ((seq->type & SEQ_EFFECT) && 
2917                             get_sequence_effect_num_inputs(seq->type) == 0)));
2918 }
2919
2920 /* check if the selected seq's reference unselected seq's */
2921 int seqbase_isolated_sel_check(ListBase *seqbase)
2922 {
2923         Sequence *seq;
2924         /* is there more than 1 select */
2925         int ok= FALSE;
2926
2927         for(seq= seqbase->first; seq; seq= seq->next) {
2928                 if(seq->flag & SELECT) {
2929                         ok= TRUE;
2930                         break;
2931                 }
2932         }
2933
2934         if(ok == FALSE)
2935                 return FALSE;
2936
2937         /* test relationships */
2938         for(seq= seqbase->first; seq; seq= seq->next) {
2939                 if((seq->type & SEQ_EFFECT)==0)
2940                         continue;
2941
2942                 if(seq->flag & SELECT) {
2943                         if( (seq->seq1 && (seq->seq1->flag & SELECT)==0) ||
2944                                 (seq->seq2 && (seq->seq2->flag & SELECT)==0) ||
2945                                 (seq->seq3 && (seq->seq3->flag & SELECT)==0) )
2946                                 return FALSE;
2947                 }
2948                 else {
2949                         if( (seq->seq1 && (seq->seq1->flag & SELECT)) ||
2950                                 (seq->seq2 && (seq->seq2->flag & SELECT)) ||
2951                                 (seq->seq3 && (seq->seq3->flag & SELECT)) )
2952                                 return FALSE;
2953                 }
2954         }
2955
2956         return TRUE;
2957 }
2958
2959 /* use to impose limits when dragging/extending - so impossible situations dont happen
2960  * Cant use the SEQ_LEFTSEL and SEQ_LEFTSEL directly because the strip may be in a metastrip */
2961 void seq_tx_handle_xlimits(Sequence *seq, int leftflag, int rightflag)
2962 {
2963         if(leftflag) {
2964                 if (seq_tx_get_final_left(seq, 0) >= seq_tx_get_final_right(seq, 0)) {
2965                         seq_tx_set_final_left(seq, seq_tx_get_final_right(seq, 0)-1);
2966                 }
2967
2968                 if (seq_single_check(seq)==0) {
2969                         if (seq_tx_get_final_left(seq, 0) >= seq_tx_get_end(seq)) {
2970                                 seq_tx_set_final_left(seq, seq_tx_get_end(seq)-1);
2971                         }
2972
2973                         /* dosnt work now - TODO */
2974                         /*
2975                         if (seq_tx_get_start(seq) >= seq_tx_get_final_right(seq, 0)) {
2976                                 int ofs;
2977                                 ofs = seq_tx_get_start(seq) - seq_tx_get_final_right(seq, 0);
2978                                 seq->start -= ofs;
2979                                 seq_tx_set_final_left(seq, seq_tx_get_final_left(seq, 0) + ofs );
2980                         }*/
2981
2982                 }
2983         }
2984
2985         if(rightflag) {
2986                 if (seq_tx_get_final_right(seq, 0) <=  seq_tx_get_final_left(seq, 0)) {
2987                         seq_tx_set_final_right(seq, seq_tx_get_final_left(seq, 0)+1);
2988                 }
2989
2990                 if (seq_single_check(seq)==0) {
2991                         if (seq_tx_get_final_right(seq, 0) <= seq_tx_get_start(seq)) {
2992                                 seq_tx_set_final_right(seq, seq_tx_get_start(seq)+1);
2993                         }
2994                 }
2995         }
2996
2997         /* sounds cannot be extended past their endpoints */
2998         if (seq->type == SEQ_SOUND) {
2999                 seq->startstill= 0;
3000                 seq->endstill= 0;
3001         }
3002 }
3003
3004 void seq_single_fix(Sequence *seq)
3005 {
3006         int left, start, offset;
3007         if (!seq_single_check(seq))
3008                 return;
3009
3010         /* make sure the image is always at the start since there is only one,
3011            adjusting its start should be ok */
3012         left = seq_tx_get_final_left(seq, 0);
3013         start = seq->start;
3014         if (start != left) {
3015                 offset = left - start;
3016                 seq_tx_set_final_left( seq, seq_tx_get_final_left(seq, 0) - offset );
3017                 seq_tx_set_final_right( seq, seq_tx_get_final_right(seq, 0) - offset );
3018                 seq->start += offset;
3019         }
3020 }
3021
3022 int seq_tx_test(Sequence * seq)
3023 {
3024         return (seq->type < SEQ_EFFECT) || (get_sequence_effect_num_inputs(seq->type) == 0);
3025 }
3026
3027 static int seq_overlap(Sequence *seq1, Sequence *seq2)
3028 {
3029         return (seq1 != seq2 && seq1->machine == seq2->machine &&
3030                         ((seq1->enddisp <= seq2->startdisp) || (seq1->startdisp >= seq2->enddisp))==0);
3031 }
3032
3033 int seq_test_overlap(ListBase * seqbasep, Sequence *test)
3034 {
3035         Sequence *seq;
3036
3037         seq= seqbasep->first;
3038         while(seq) {
3039                 if(seq_overlap(test, seq))
3040                         return 1;
3041
3042                 seq= seq->next;
3043         }
3044         return 0;
3045 }
3046
3047
3048 void seq_translate(Scene *evil_scene, Sequence *seq, int delta)
3049 {
3050         seq_offset_animdata(evil_scene, seq, delta);
3051         seq->start += delta;
3052
3053         if(seq->type==SEQ_META) {
3054                 Sequence *seq_child;
3055                 for(seq_child= seq->seqbase.first; seq_child; seq_child= seq_child->next) {
3056                         seq_translate(evil_scene, seq_child, delta);
3057                 }
3058         }
3059
3060         calc_sequence_disp(evil_scene, seq);
3061 }
3062
3063 void seq_sound_init(Scene *scene, Sequence *seq)
3064 {
3065         if(seq->type==SEQ_META) {
3066                 Sequence *seq_child;
3067                 for(seq_child= seq->seqbase.first; seq_child; seq_child= seq_child->next) {
3068                         seq_sound_init(scene, seq_child);
3069                 }
3070         }
3071         else {
3072                 if(seq->sound) {
3073                         seq->scene_sound = sound_add_scene_sound(scene, seq, seq->startdisp, seq->enddisp, seq->startofs + seq->anim_startofs);
3074                 }
3075                 if(seq->scene) {
3076                         sound_scene_add_scene_sound(scene, seq, seq->startdisp, seq->enddisp, seq->startofs + seq->anim_startofs);
3077                 }
3078         }
3079 }
3080
3081 Sequence *seq_foreground_frame_get(Scene *scene, int frame)
3082 {
3083         Editing *ed= seq_give_editing(scene, FALSE);
3084         Sequence *seq, *best_seq=NULL;
3085         int best_machine = -1;
3086         
3087         if(!ed) return NULL;
3088         
3089         for (seq=ed->seqbasep->first; seq; seq= seq->next) {
3090                 if(seq->flag & SEQ_MUTE || seq->startdisp > frame || seq->enddisp <= frame)
3091                         continue;
3092                 /* only use elements you can see - not */
3093                 if (ELEM5(seq->type, SEQ_IMAGE, SEQ_META, SEQ_SCENE, SEQ_MOVIE, SEQ_COLOR)) {
3094                         if (seq->machine > best_machine) {
3095                                 best_seq = seq;
3096                                 best_machine = seq->machine;
3097                         }
3098                 }
3099         }
3100         return best_seq;
3101 }
3102
3103 /* return 0 if there werent enough space */
3104 int shuffle_seq(ListBase * seqbasep, Sequence *test, Scene *evil_scene)
3105 {
3106         int orig_machine= test->machine;
3107         test->machine++;
3108         calc_sequence(evil_scene, test);
3109         while( seq_test_overlap(seqbasep, test) ) {
3110                 if(test->machine >= MAXSEQ) {
3111                         break;
3112                 }
3113                 test->machine++;
3114                 calc_sequence(evil_scene, test); // XXX - I dont think this is needed since were only moving vertically, Campbell.
3115         }
3116
3117         
3118         if(test->machine >= MAXSEQ) {
3119                 /* Blender 2.4x would remove the strip.
3120                  * nicer to move it to the end */
3121
3122                 Sequence *seq;
3123                 int new_frame= test->enddisp;
3124
3125                 for(seq= seqbasep->first; seq; seq= seq->next) {
3126                         if (seq->machine == orig_machine)
3127                                 new_frame = MAX2(new_frame, seq->enddisp);
3128                 }
3129
3130                 test->machine= orig_machine;
3131                 new_frame = new_frame + (test->start-test->startdisp); /* adjust by the startdisp */
3132                 seq_translate(evil_scene, test, new_frame - test->start);
3133
3134                 calc_sequence(evil_scene, test);
3135                 return 0;
3136         } else {
3137                 return 1;
3138         }
3139 }
3140
3141 static int shuffle_seq_time_offset_test(ListBase * seqbasep, char dir)
3142 {
3143         int offset= 0;
3144         Sequence *seq, *seq_other;
3145
3146         for(seq= seqbasep->first; seq; seq= seq->next) {
3147                 if(seq->tmp) {
3148                         for(seq_other= seqbasep->first; seq_other; seq_other= seq_other->next) {
3149                                 if(!seq_other->tmp && seq_overlap(seq, seq_other)) {
3150                                         if(dir=='L') {
3151                                                 offset= MIN2(offset, seq_other->startdisp - seq->enddisp);
3152                                         }
3153                                         else {
3154                                                 offset= MAX2(offset, seq_other->enddisp - seq->startdisp);
3155                                         }
3156                                 }
3157                         }
3158                 }
3159         }
3160         return offset;
3161 }
3162
3163 static int shuffle_seq_time_offset(Scene* scene, ListBase * seqbasep, char dir)
3164 {
3165         int ofs= 0;
3166         int tot_ofs= 0;
3167         Sequence *seq;
3168         while( (ofs= shuffle_seq_time_offset_test(seqbasep, dir)) ) {
3169                 for(seq= seqbasep->first; seq; seq= seq->next) {
3170                         if(seq->tmp) {
3171                                 /* seq_test_overlap only tests display values */
3172                                 seq->startdisp +=       ofs;
3173                                 seq->enddisp +=         ofs;
3174                         }
3175                 }
3176
3177                 tot_ofs+= ofs;
3178         }
3179
3180         for(seq= seqbasep->first; seq; seq= seq->next) {
3181                 if(seq->tmp)
3182                         calc_sequence_disp(scene, seq); /* corrects dummy startdisp/enddisp values */
3183         }
3184
3185         return tot_ofs;
3186 }
3187
3188 int shuffle_seq_time(ListBase * seqbasep, Scene *evil_scene)
3189 {
3190         /* note: seq->tmp is used to tag strips to move */
3191
3192         Sequence *seq;
3193
3194         int offset_l = shuffle_seq_time_offset(evil_scene, seqbasep, 'L');
3195         int offset_r = shuffle_seq_time_offset(evil_scene, seqbasep, 'R');
3196         int offset = (-offset_l < offset_r) ?  offset_l:offset_r;
3197
3198         if(offset) {
3199                 for(seq= seqbasep->first; seq; seq= seq->next) {
3200                         if(seq->tmp) {
3201                                 seq_translate(evil_scene, seq, offset);
3202                                 seq->flag &= ~SEQ_OVERLAP;
3203                         }
3204                 }
3205         }
3206
3207         return offset? 0:1;
3208 }
3209
3210 void seq_update_sound_bounds_all(Scene *scene)
3211 {
3212         Editing *ed = scene->ed;
3213
3214         if(ed) {
3215                 Sequence *seq;
3216
3217                 for(seq = ed->seqbase.first; seq; seq = seq->next) {
3218                         if(seq->type == SEQ_META) {
3219                                 seq_update_sound_bounds_recursive(scene, seq);
3220                         }
3221                         else if(ELEM(seq->type, SEQ_SOUND, SEQ_SCENE)) {
3222                                 seq_update_sound_bounds(scene, seq);
3223                         }
3224                 }
3225         }
3226 }
3227
3228 void seq_update_sound_bounds(Scene* scene, Sequence *seq)
3229 {
3230         if(seq->scene_sound) {
3231                 sound_move_scene_sound(scene, seq->scene_sound, seq->startdisp, seq->enddisp, seq->startofs + seq->anim_startofs);
3232                 /* mute is set in seq_update_muting_recursive */
3233         }
3234 }
3235
3236 static void seq_update_muting_recursive(ListBase *seqbasep, Sequence *metaseq, int mute)
3237 {
3238         Sequence *seq;
3239         int seqmute;
3240
3241         /* for sound we go over full meta tree to update muted state,
3242            since sound is played outside of evaluating the imbufs, */
3243         for(seq=seqbasep->first; seq; seq=seq->next) {
3244                 seqmute= (mute || (seq->flag & SEQ_MUTE));
3245
3246                 if(seq->type == SEQ_META) {
3247                         /* if this is the current meta sequence, unmute because
3248                            all sequences above this were set to mute */
3249                         if(seq == metaseq)
3250                                 seqmute= 0;
3251
3252                         seq_update_muting_recursive(&seq->seqbase, metaseq, seqmute);
3253                 }
3254                 else if(ELEM(seq->type, SEQ_SOUND, SEQ_SCENE)) {
3255                         if(seq->scene_sound) {
3256                                 sound_mute_scene_sound(seq->scene_sound, seqmute);
3257                         }
3258                 }
3259         }
3260 }
3261
3262 void seq_update_muting(Editing *ed)
3263 {
3264         if(ed) {
3265                 /* mute all sounds up to current metastack list */
3266                 MetaStack *ms= ed->metastack.last;
3267
3268                 if(ms)
3269                         seq_update_muting_recursive(&ed->seqbase, ms->parseq, 1);
3270                 else
3271                         seq_update_muting_recursive(&ed->seqbase, NULL, 0);
3272         }
3273 }
3274
3275 static void seq_update_sound_recursive(Scene *scene, ListBase *seqbasep, bSound *sound)
3276 {
3277         Sequence *seq;
3278
3279         for(seq=seqbasep->first; seq; seq=seq->next) {
3280                 if(seq->type == SEQ_META) {
3281                         seq_update_sound_recursive(scene, &seq->seqbase, sound);
3282                 }
3283                 else if(seq->type == SEQ_SOUND) {
3284                         if(seq->scene_sound && sound == seq->sound) {
3285                                 sound_update_scene_sound(seq->scene_sound, sound);
3286                         }
3287                 }
3288         }
3289 }
3290
3291 void seq_update_sound(struct Scene *scene, struct bSound *sound)
3292 {
3293         if(scene->ed) {
3294     &n