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