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