22bedab6beebc9fe3b8d5083aa66c1b8c6294383
[blender-staging.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                 strncpy(str, seq->strip->dir, FILE_MAXDIR-1);
430                 strncat(str, seq->strip->stripdata->name, FILE_MAXFILE-1);
431
432                 BLI_convertstringcode(str, G.sce, G.scene->r.cfra);
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, frameno);
1072
1073         strcat(name, ".jpg");
1074
1075         return TRUE;
1076 }
1077
1078 static struct ImBuf * seq_proxy_fetch(Sequence * seq, int cfra)
1079 {
1080         char name[PROXY_MAXFILE];
1081
1082         if (!(seq->flag & SEQ_USE_PROXY)) {
1083                 return 0;
1084         }
1085
1086         /* rendering at 100% ? No real sense in proxy-ing, right? */
1087         if (G.scene->r.size == 100.0) {
1088                 return 0;
1089         }
1090
1091         if (!seq_proxy_get_fname(seq, cfra, name)) {
1092                 return 0;
1093         }
1094
1095         if (BLI_exists(name)) {
1096                 return IMB_loadiffname(name, IB_rect);
1097         } else {
1098                 return 0;
1099         }
1100 }
1101
1102 static void do_build_seq_ibuf(Sequence * seq, TStripElem *se, int cfra,
1103                               int build_proxy_run);
1104
1105 static void seq_proxy_build_frame(Sequence * seq, int cfra)
1106 {
1107         char name[PROXY_MAXFILE];
1108         int quality;
1109         TStripElem * se;
1110         int ok;
1111         int rectx, recty;
1112         struct ImBuf * ibuf;
1113
1114         if (!(seq->flag & SEQ_USE_PROXY)) {
1115                 return;
1116         }
1117
1118         /* rendering at 100% ? No real sense in proxy-ing, right? */
1119         if (G.scene->r.size == 100.0) {
1120                 return;
1121         }
1122
1123         if (!seq_proxy_get_fname(seq, cfra, name)) {
1124                 return;
1125         }
1126
1127         se = give_tstripelem(seq, cfra);
1128         if (!se) {
1129                 return;
1130         }
1131
1132         if(se->ibuf) {
1133                 IMB_freeImBuf(se->ibuf);
1134                 se->ibuf = 0;
1135         }
1136         
1137         do_build_seq_ibuf(seq, se, cfra, TRUE);
1138
1139         if (!se->ibuf) {
1140                 return;
1141         }
1142
1143         rectx= (G.scene->r.size*G.scene->r.xsch)/100;
1144         recty= (G.scene->r.size*G.scene->r.ysch)/100;
1145
1146         ibuf = se->ibuf;
1147
1148         if (ibuf->x != rectx || ibuf->y != recty) {
1149                 IMB_scalefastImBuf(ibuf, (short)rectx, (short)recty);
1150         }
1151
1152         /* quality is fixed, otherwise one has to generate seperate
1153            directories for every quality...
1154
1155            depth = 32 is intentionally left in, otherwise ALPHA channels
1156            won't work... */
1157         quality = 90;
1158         ibuf->ftype= JPG | quality;
1159
1160         BLI_make_existing_file(name);
1161         
1162         ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
1163         if (ok == 0) {
1164                 perror(name);
1165         }
1166
1167         IMB_freeImBuf(ibuf);
1168         se->ibuf = 0;
1169 }
1170
1171 void seq_proxy_rebuild(Sequence * seq)
1172 {
1173         int cfra;
1174
1175         waitcursor(1);
1176
1177         G.afbreek = 0;
1178
1179         /* flag management tries to account for strobe and 
1180            other "non-linearities", that might come in the future...
1181            better way would be to "touch" the files, so that _really_
1182            no one is rebuild twice.
1183          */
1184
1185         for (cfra = seq->startdisp; cfra < seq->enddisp; cfra++) {
1186                 TStripElem * tse = give_tstripelem(seq, cfra);
1187
1188                 tse->flag &= ~STRIPELEM_PREVIEW_DONE;
1189         }
1190
1191         /* a _lot_ faster for movie files, if we read frames in
1192            sequential order */
1193         if (seq->flag & SEQ_REVERSE_FRAMES) {
1194                 for (cfra = seq->enddisp-seq->endstill-1; 
1195                      cfra >= seq->startdisp + seq->startstill; cfra--) {
1196                         TStripElem * tse = give_tstripelem(seq, cfra);
1197
1198                         if (!(tse->flag & STRIPELEM_PREVIEW_DONE)) {
1199                                 seq_proxy_build_frame(seq, cfra);
1200                                 tse->flag |= STRIPELEM_PREVIEW_DONE;
1201                         }
1202                         if (blender_test_break()) {
1203                                 break;
1204                         }
1205                 }
1206         } else {
1207                 for (cfra = seq->startdisp + seq->startstill; 
1208                      cfra < seq->enddisp - seq->endstill; cfra++) {
1209                         TStripElem * tse = give_tstripelem(seq, cfra);
1210
1211                         if (!(tse->flag & STRIPELEM_PREVIEW_DONE)) {
1212                                 seq_proxy_build_frame(seq, cfra);
1213                                 tse->flag |= STRIPELEM_PREVIEW_DONE;
1214                         }
1215                         if (blender_test_break()) {
1216                                 break;
1217                         }
1218                 }
1219         }
1220         waitcursor(0);
1221 }
1222
1223
1224 /* **********************************************************************
1225    color balance 
1226    ********************************************************************** */
1227
1228 static StripColorBalance calc_cb(StripColorBalance * cb_)
1229 {
1230         StripColorBalance cb = *cb_;
1231         int c;
1232
1233         if (cb.flag & SEQ_COLOR_BALANCE_INVERSE_LIFT) {
1234                 for (c = 0; c < 3; c++) {
1235                         cb.lift[c] = 1.0 - cb.lift[c];
1236                 }
1237         } else {
1238                 for (c = 0; c < 3; c++) {
1239                         cb.lift[c] = -(1.0 - cb.lift[c]);
1240                 }
1241         }
1242         if (cb.flag & SEQ_COLOR_BALANCE_INVERSE_GAIN) {
1243                 for (c = 0; c < 3; c++) {
1244                         if (cb.gain[c] != 0.0) {
1245                                 cb.gain[c] = 1.0/cb.gain[c];
1246                         } else {
1247                                 cb.gain[c] = 1000000; /* should be enough :) */
1248                         }
1249                 }
1250         }
1251
1252         if (!(cb.flag & SEQ_COLOR_BALANCE_INVERSE_GAMMA)) {
1253                 for (c = 0; c < 3; c++) {
1254                         if (cb.gamma[c] != 0.0) {
1255                                 cb.gamma[c] = 1.0/cb.gamma[c];
1256                         } else {
1257                                 cb.gamma[c] = 1000000; /* should be enough :) */
1258                         }
1259                 }
1260         }
1261
1262         return cb;
1263 }
1264
1265 static void make_cb_table_byte(float lift, float gain, float gamma,
1266                                unsigned char * table, float mul)
1267 {
1268         int y;
1269
1270         for (y = 0; y < 256; y++) {
1271                 float v = 1.0 * y / 255;
1272                 v += lift; 
1273                 v *= gain;
1274                 v = pow(v, gamma);
1275                 v *= mul;
1276                 if ( v > 1.0) {
1277                         v = 1.0;
1278                 } else if (v < 0.0) {
1279                         v = 0.0;
1280                 }
1281                 table[y] = v * 255;
1282         }
1283
1284 }
1285
1286 static void make_cb_table_float(float lift, float gain, float gamma,
1287                                 float * table, float mul)
1288 {
1289         int y;
1290
1291         for (y = 0; y < 256; y++) {
1292                 float v = (float) y * 1.0 / 255.0;
1293                 v += lift;
1294                 v *= gain;
1295                 v = pow(v, gamma);
1296                 v *= mul;
1297                 table[y] = v;
1298         }
1299 }
1300
1301 static void color_balance_byte_byte(Sequence * seq, TStripElem* se,
1302                                     float mul)
1303 {
1304         unsigned char cb_tab[3][256];
1305         int c;
1306         unsigned char * p = (unsigned char*) se->ibuf->rect;
1307         unsigned char * e = p + se->ibuf->x * 4 * se->ibuf->y;
1308
1309         StripColorBalance cb = calc_cb(seq->strip->color_balance);
1310
1311         for (c = 0; c < 3; c++) {
1312                 make_cb_table_byte(cb.lift[c], cb.gain[c], cb.gamma[c],
1313                                    cb_tab[c], mul);
1314         }
1315
1316         while (p < e) {
1317                 p[0] = cb_tab[0][p[0]];
1318                 p[1] = cb_tab[1][p[1]];
1319                 p[2] = cb_tab[2][p[2]];
1320                 
1321                 p += 4;
1322         }
1323 }
1324
1325 static void color_balance_byte_float(Sequence * seq, TStripElem* se,
1326                                      float mul)
1327 {
1328         float cb_tab[4][256];
1329         int c,i;
1330         unsigned char * p = (unsigned char*) se->ibuf->rect;
1331         unsigned char * e = p + se->ibuf->x * 4 * se->ibuf->y;
1332         float * o;
1333         StripColorBalance cb;
1334
1335         imb_addrectfloatImBuf(se->ibuf);
1336
1337         o = se->ibuf->rect_float;
1338
1339         cb = calc_cb(seq->strip->color_balance);
1340
1341         for (c = 0; c < 3; c++) {
1342                 make_cb_table_float(cb.lift[c], cb.gain[c], cb.gamma[c],
1343                                     cb_tab[c], mul);
1344         }
1345
1346         for (i = 0; i < 256; i++) {
1347                 cb_tab[3][i] = ((float)i)*(1.0f/255.0f);
1348         }
1349
1350         while (p < e) {
1351                 o[0] = cb_tab[0][p[0]];
1352                 o[1] = cb_tab[1][p[1]];
1353                 o[2] = cb_tab[2][p[2]];
1354                 o[3] = cb_tab[3][p[3]];
1355
1356                 p += 4; o += 4;
1357         }
1358 }
1359
1360 static void color_balance_float_float(Sequence * seq, TStripElem* se,
1361                                       float mul)
1362 {
1363         float * p = se->ibuf->rect_float;
1364         float * e = se->ibuf->rect_float + se->ibuf->x * 4* se->ibuf->y;
1365         StripColorBalance cb = calc_cb(seq->strip->color_balance);
1366
1367         while (p < e) {
1368                 int c;
1369                 for (c = 0; c < 3; c++) {
1370                         p[c] = pow((p[c] + cb.lift[c]) * cb.gain[c], 
1371                                    cb.gamma[c]) * mul;
1372                 }
1373                 p += 4;
1374         }
1375 }
1376
1377 static void color_balance(Sequence * seq, TStripElem* se, float mul)
1378 {
1379         if (se->ibuf->rect_float) {
1380                 color_balance_float_float(seq, se, mul);
1381         } else if(seq->flag & SEQ_MAKE_FLOAT) {
1382                 color_balance_byte_float(seq, se, mul);
1383         } else {
1384                 color_balance_byte_byte(seq, se, mul);
1385         }
1386 }
1387
1388 /*
1389   input preprocessing for SEQ_IMAGE, SEQ_MOVIE and SEQ_SCENE
1390
1391   Do all the things you can't really do afterwards using sequence effects
1392   (read: before rescaling to render resolution has been done)
1393
1394   Order is important!
1395
1396   - Deinterlace
1397   - Crop and transform in image source coordinate space
1398   - Flip X + Flip Y (could be done afterwards, backward compatibility)
1399   - Promote image to float data (affects pipeline operations afterwards)
1400   - Color balance (is most efficient in the byte -> float 
1401     (future: half -> float should also work fine!)
1402     case, if done on load, since we can use lookup tables)
1403   - Premultiply
1404
1405 */
1406
1407 static int input_have_to_preprocess(Sequence * seq, TStripElem* se, int cfra)
1408 {
1409         float mul;
1410
1411         if ((seq->flag & SEQ_FILTERY) || 
1412             (seq->flag & SEQ_USE_CROP) ||
1413             (seq->flag & SEQ_USE_TRANSFORM) ||
1414             (seq->flag & SEQ_FLIPX) ||
1415             (seq->flag & SEQ_FLIPY) ||
1416             (seq->flag & SEQ_USE_COLOR_BALANCE) ||
1417             (seq->flag & SEQ_MAKE_PREMUL) ||
1418             (se->ibuf->x != seqrectx || se->ibuf->y != seqrecty)) {
1419                 return TRUE;
1420         }
1421
1422         mul = seq->mul;
1423
1424         if(seq->blend_mode == SEQ_BLEND_REPLACE) {
1425                 if (seq->ipo && seq->ipo->curve.first) {
1426                         do_seq_ipo(seq, cfra);
1427                         mul *= seq->facf0;
1428                 }
1429                 mul *= seq->blend_opacity / 100.0;
1430         }
1431
1432         if (mul != 1.0) {
1433                 return TRUE;
1434         }
1435                 
1436         return FALSE;
1437 }
1438
1439 static void input_preprocess(Sequence * seq, TStripElem* se, int cfra)
1440 {
1441         float mul;
1442
1443         seq->strip->orx= se->ibuf->x;
1444         seq->strip->ory= se->ibuf->y;
1445
1446         if(seq->flag & SEQ_FILTERY) {
1447                 IMB_filtery(se->ibuf);
1448         }
1449
1450         if(seq->flag & SEQ_USE_CROP || seq->flag & SEQ_USE_TRANSFORM) {
1451                 StripCrop c;
1452                 StripTransform t;
1453
1454                 memset(&c, 0, sizeof(StripCrop));
1455                 memset(&t, 0, sizeof(StripTransform));
1456
1457                 if(seq->flag & SEQ_USE_CROP && seq->strip->crop) {
1458                         c = *seq->strip->crop;
1459                 }
1460                 if(seq->flag & SEQ_USE_TRANSFORM && seq->strip->transform) {
1461                         t = *seq->strip->transform;
1462                 }
1463
1464                 if (c.top + c.bottom >= se->ibuf->y ||
1465                     c.left + c.right >= se->ibuf->x ||
1466                     t.xofs >= se->ibuf->x ||
1467                     t.yofs >= se->ibuf->y) {
1468                         make_black_ibuf(se->ibuf);
1469                 } else {
1470                         ImBuf * i;
1471                         int sx = se->ibuf->x - c.left - c.right;
1472                         int sy = se->ibuf->y - c.top - c.bottom;
1473                         int dx = sx;
1474                         int dy = sy;
1475
1476                         if (seq->flag & SEQ_USE_TRANSFORM) {
1477                                 dx = seqrectx;
1478                                 dy = seqrecty;
1479                         }
1480
1481                         if (se->ibuf->rect_float) {
1482                                 i = IMB_allocImBuf(dx, dy,32, IB_rectfloat, 0);
1483                         } else {
1484                                 i = IMB_allocImBuf(dx, dy,32, IB_rect, 0);
1485                         }
1486
1487                         IMB_rectcpy(i, se->ibuf, 
1488                                     t.xofs, t.yofs, 
1489                                     c.left, c.bottom, 
1490                                     sx, sy);
1491
1492                         IMB_freeImBuf(se->ibuf);
1493
1494                         se->ibuf = i;
1495                 }
1496         } 
1497
1498         if(seq->flag & SEQ_FLIPX) {
1499                 IMB_flipx(se->ibuf);
1500         }
1501         if(seq->flag & SEQ_FLIPY) {
1502                 IMB_flipy(se->ibuf);
1503         }
1504
1505         if(seq->mul == 0.0) {
1506                 seq->mul = 1.0;
1507         }
1508
1509         mul = seq->mul;
1510
1511         if(seq->blend_mode == SEQ_BLEND_REPLACE) {
1512                 if (seq->ipo && seq->ipo->curve.first) {
1513                         do_seq_ipo(seq, cfra);
1514                         mul *= seq->facf0;
1515                 }
1516                 mul *= seq->blend_opacity / 100.0;
1517         }
1518
1519         if(seq->flag & SEQ_USE_COLOR_BALANCE && seq->strip->color_balance) {
1520                 color_balance(seq, se, mul);
1521                 mul = 1.0;
1522         }
1523
1524         if(seq->flag & SEQ_MAKE_FLOAT) {
1525                 if (!se->ibuf->rect_float) {
1526                         IMB_float_from_rect(se->ibuf);
1527                 }
1528                 if (se->ibuf->rect) {
1529                         imb_freerectImBuf(se->ibuf);
1530                 }
1531         }
1532
1533         if(mul != 1.0) {
1534                 multibuf(se->ibuf, mul);
1535         }
1536
1537         if(seq->flag & SEQ_MAKE_PREMUL) {
1538                 if(se->ibuf->depth == 32 && se->ibuf->zbuf == 0) {
1539                         converttopremul(se->ibuf);
1540                 }
1541         }
1542
1543
1544         if(se->ibuf->x != seqrectx || se->ibuf->y != seqrecty ) {
1545                 if(G.scene->r.mode & R_OSA) {
1546                         IMB_scaleImBuf(se->ibuf, 
1547                                        (short)seqrectx, (short)seqrecty);
1548                 } else {
1549                         IMB_scalefastImBuf(se->ibuf, 
1550                                            (short)seqrectx, (short)seqrecty);
1551                 }
1552         }
1553 }
1554
1555 /* test if image too small or discarded from cache: reload */
1556
1557 static void test_and_auto_discard_ibuf(TStripElem * se)
1558 {
1559         if (se->ibuf) {
1560                 if(se->ibuf->x != seqrectx || se->ibuf->y != seqrecty 
1561                    || !(se->ibuf->rect || se->ibuf->rect_float)) {
1562                         IMB_freeImBuf(se->ibuf);
1563
1564                         se->ibuf= 0;
1565                         se->ok= STRIPELEM_OK;
1566                 }
1567         }
1568         if (se->ibuf_comp) {
1569                 if(se->ibuf_comp->x != seqrectx || se->ibuf_comp->y != seqrecty 
1570                    || !(se->ibuf_comp->rect || se->ibuf_comp->rect_float)) {
1571                         IMB_freeImBuf(se->ibuf_comp);
1572
1573                         se->ibuf_comp = 0;
1574                 }
1575         }
1576 }
1577
1578 static void test_and_auto_discard_ibuf_stills(Strip * strip)
1579 {
1580         if (strip->ibuf_startstill) {
1581                 if (!strip->ibuf_startstill->rect &&
1582                     !strip->ibuf_startstill->rect_float) {
1583                         IMB_freeImBuf(strip->ibuf_startstill);
1584                         strip->ibuf_startstill = 0;
1585                 }
1586         }
1587         if (strip->ibuf_endstill) {
1588                 if (!strip->ibuf_endstill->rect &&
1589                     !strip->ibuf_endstill->rect_float) {
1590                         IMB_freeImBuf(strip->ibuf_endstill);
1591                         strip->ibuf_endstill = 0;
1592                 }
1593         }
1594 }
1595
1596 static void copy_from_ibuf_still(Sequence * seq, TStripElem * se)
1597 {
1598         if (!se->ibuf) {
1599                 if (se->nr == 0 && seq->strip->ibuf_startstill) {
1600                         IMB_cache_limiter_touch(seq->strip->ibuf_startstill);
1601
1602                         se->ibuf = IMB_dupImBuf(seq->strip->ibuf_startstill);
1603                 }
1604                 if (se->nr == seq->len - 1 
1605                     && (seq->len != 1)
1606                     && seq->strip->ibuf_endstill) {
1607                         IMB_cache_limiter_touch(seq->strip->ibuf_endstill);
1608
1609                         se->ibuf = IMB_dupImBuf(seq->strip->ibuf_endstill);
1610                 }
1611         }
1612 }
1613
1614 static void copy_to_ibuf_still(Sequence * seq, TStripElem * se)
1615 {
1616         if (se->ibuf) {
1617                 if (se->nr == 0) {
1618                         seq->strip->ibuf_startstill = IMB_dupImBuf(se->ibuf);
1619
1620                         IMB_cache_limiter_insert(seq->strip->ibuf_startstill);
1621                         IMB_cache_limiter_touch(seq->strip->ibuf_startstill);
1622                 }
1623                 if (se->nr == seq->len - 1 && seq->len != 1) {
1624                         seq->strip->ibuf_endstill = IMB_dupImBuf(se->ibuf);
1625
1626                         IMB_cache_limiter_insert(seq->strip->ibuf_endstill);
1627                         IMB_cache_limiter_touch(seq->strip->ibuf_endstill);
1628                 }
1629         }
1630 }
1631
1632
1633 static TStripElem* do_build_seq_array_recursively(
1634         ListBase *seqbasep, int cfra, int chanshown);
1635
1636 static void do_build_seq_ibuf(Sequence * seq, TStripElem *se, int cfra,
1637                               int build_proxy_run)
1638 {
1639         char name[FILE_MAXDIR+FILE_MAXFILE];
1640         int use_limiter = TRUE;
1641
1642         test_and_auto_discard_ibuf(se);
1643         test_and_auto_discard_ibuf_stills(seq->strip);
1644
1645         if(seq->type == SEQ_META) {
1646                 TStripElem * meta_se = 0;
1647                 use_limiter = FALSE;
1648
1649                 if (!build_proxy_run && se->ibuf == 0) {
1650                         se->ibuf = seq_proxy_fetch(seq, cfra);
1651                         if (se->ibuf) {
1652                                 use_limiter = TRUE;
1653                         }
1654                 }
1655
1656                 if(!se->ibuf && seq->seqbase.first) {
1657                         meta_se = do_build_seq_array_recursively(
1658                                 &seq->seqbase, seq->start + se->nr, 0);
1659                 }
1660
1661                 se->ok = STRIPELEM_OK;
1662
1663                 if(!se->ibuf && meta_se) {
1664                         se->ibuf = meta_se->ibuf_comp;
1665                         if(se->ibuf &&
1666                            (!input_have_to_preprocess(seq, se, cfra) ||
1667                             build_proxy_run)) {
1668                                 IMB_refImBuf(se->ibuf);
1669                                 if (build_proxy_run) {
1670                                         IMB_cache_limiter_unref(se->ibuf);
1671                                 }
1672                         } else if (se->ibuf) {
1673                                 struct ImBuf * i = IMB_dupImBuf(se->ibuf);
1674
1675                                 IMB_cache_limiter_unref(se->ibuf);
1676
1677                                 se->ibuf = i;
1678
1679                                 use_limiter = TRUE;
1680                         }
1681                 }
1682
1683                 if (use_limiter) {
1684                         input_preprocess(seq, se, cfra);
1685                 }
1686         } else if(seq->type & SEQ_EFFECT) {
1687                 /* should the effect be recalculated? */
1688                 
1689                 if (!build_proxy_run && se->ibuf == 0) {
1690                         se->ibuf = seq_proxy_fetch(seq, cfra);
1691                 }
1692
1693                 if(se->ibuf == 0) {
1694                         /* if one of two first inputs are rectfloat, output is float too */
1695                         if((se->se1 && se->se1->ibuf && se->se1->ibuf->rect_float) ||
1696                            (se->se2 && se->se2->ibuf && se->se2->ibuf->rect_float))
1697                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
1698                         else
1699                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
1700                         
1701                         do_effect(cfra, seq, se);
1702                 }
1703         } else if(seq->type == SEQ_IMAGE) {
1704                 if(se->ok == STRIPELEM_OK && se->ibuf == 0) {
1705                         StripElem * s_elem = give_stripelem(seq, cfra);
1706                         
1707                         strncpy(name, seq->strip->dir, FILE_MAXDIR-1);
1708                         strncat(name, s_elem->name, FILE_MAXFILE);
1709                         BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
1710                         if (!build_proxy_run) {
1711                                 se->ibuf = seq_proxy_fetch(seq, cfra);
1712                         }
1713                         copy_from_ibuf_still(seq, se);
1714
1715                         if (!se->ibuf) {
1716                                 se->ibuf= IMB_loadiffname(
1717                                         name, IB_rect);
1718                                 copy_to_ibuf_still(seq, se);
1719                         }
1720                         
1721                         if(se->ibuf == 0) {
1722                                 se->ok = STRIPELEM_FAILED;
1723                         } else if (!build_proxy_run) {
1724                                 input_preprocess(seq, se, cfra);
1725                         }
1726                 }
1727         } else if(seq->type == SEQ_MOVIE) {
1728                 if(se->ok == STRIPELEM_OK && se->ibuf==0) {
1729                         if(!build_proxy_run) {
1730                                 se->ibuf = seq_proxy_fetch(seq, cfra);
1731                         }
1732                         copy_from_ibuf_still(seq, se);
1733
1734                         if (se->ibuf == 0) {
1735                                 if(seq->anim==0) {
1736                                         strncpy(name, seq->strip->dir, FILE_MAXDIR-1);
1737                                         strncat(name, seq->strip->stripdata->name, FILE_MAXFILE-1);
1738                                         BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
1739                                 
1740                                         seq->anim = openanim(name, IB_rect);
1741                                 }
1742                                 if(seq->anim) {
1743                                         IMB_anim_set_preseek(seq->anim, seq->anim_preseek);
1744                                         se->ibuf = IMB_anim_absolute(seq->anim, se->nr + seq->anim_startofs);
1745                                 }
1746                                 copy_to_ibuf_still(seq, se);
1747                         }
1748                         
1749                         if(se->ibuf == 0) {
1750                                 se->ok = STRIPELEM_FAILED;
1751                         } else if (!build_proxy_run) {
1752                                 input_preprocess(seq, se, cfra);
1753                         }
1754                 }
1755         } else if(seq->type == SEQ_SCENE) {     // scene can be NULL after deletions
1756                 int oldcfra = CFRA;
1757                 Scene *sce= seq->scene, *oldsce= G.scene;
1758                 Render *re;
1759                 RenderResult rres;
1760                 int doseq, rendering= G.rendering;
1761                 char scenename[64];
1762                 int sce_valid =sce&& (sce->camera || sce->r.scemode & R_DOSEQ);
1763                         
1764                 if (se->ibuf == NULL && sce_valid && !build_proxy_run) {
1765                         se->ibuf = seq_proxy_fetch(seq, cfra);
1766                         if (se->ibuf) {
1767                                 input_preprocess(seq, se, cfra);
1768                         }
1769                 }
1770
1771                 if (se->ibuf == NULL && sce_valid) {
1772                         copy_from_ibuf_still(seq, se);
1773                         if (se->ibuf) {
1774                                 input_preprocess(seq, se, cfra);
1775                         }
1776                 }
1777                 
1778                 if (!sce_valid) {
1779                         se->ok = STRIPELEM_FAILED;
1780                 } else if (se->ibuf==NULL && sce_valid) {
1781                         waitcursor(1);
1782                         
1783                         /* Hack! This function can be called from do_render_seq(), in that case
1784                            the seq->scene can already have a Render initialized with same name, 
1785                            so we have to use a default name. (compositor uses G.scene name to
1786                            find render).
1787                            However, when called from within the UI (image preview in sequencer)
1788                            we do want to use scene Render, that way the render result is defined
1789                            for display in render/imagewindow */
1790                         if(rendering) {
1791                                 BLI_strncpy(scenename, sce->id.name+2, 64);
1792                                 strcpy(sce->id.name+2, " do_build_seq_ibuf");
1793                         }
1794                         re= RE_NewRender(sce->id.name);
1795                         
1796                         /* prevent eternal loop */
1797                         doseq= G.scene->r.scemode & R_DOSEQ;
1798                         G.scene->r.scemode &= ~R_DOSEQ;
1799                         
1800                         BIF_init_render_callbacks(re, 0);       /* 0= no display callbacks */
1801                         
1802                         /* hrms, set_scene still needed? work on that... */
1803                         if(sce!=oldsce) set_scene_bg(sce);
1804                         RE_BlenderFrame(re, sce,
1805                                         seq->sfra+se->nr+seq->anim_startofs);
1806                         if(sce!=oldsce) set_scene_bg(oldsce);
1807                         
1808                         /* UGLY WARNING, it is set to zero in  RE_BlenderFrame */
1809                         G.rendering= rendering;
1810                         if(rendering)
1811                                 BLI_strncpy(sce->id.name+2, scenename, 64);
1812                         
1813                         RE_GetResultImage(re, &rres);
1814                         
1815                         if(rres.rectf) {
1816                                 se->ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rectfloat, 0);
1817                                 memcpy(se->ibuf->rect_float, rres.rectf, 4*sizeof(float)*rres.rectx*rres.recty);
1818                                 if(rres.rectz) {
1819                                         addzbuffloatImBuf(se->ibuf);
1820                                         memcpy(se->ibuf->zbuf_float, rres.rectz, sizeof(float)*rres.rectx*rres.recty);
1821                                 }
1822                         } else if (rres.rect32) {
1823                                 se->ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rect, 0);
1824                                 memcpy(se->ibuf->rect, rres.rect32, 4*rres.rectx*rres.recty);
1825                         }
1826                         
1827                         BIF_end_render_callbacks();
1828                         
1829                         /* restore */
1830                         G.scene->r.scemode |= doseq;
1831                         
1832                         if((G.f & G_PLAYANIM)==0) /* bad, is set on do_render_seq */
1833                                 waitcursor(0);
1834                         CFRA = oldcfra;
1835
1836                         copy_to_ibuf_still(seq, se);
1837
1838                         if (!build_proxy_run) {
1839                                 if(se->ibuf == NULL) {
1840                                         se->ok = STRIPELEM_FAILED;
1841                                 } else {
1842                                         input_preprocess(seq, se, cfra);
1843                                 }
1844                         }
1845
1846                 }       
1847         }
1848         if (!build_proxy_run) {
1849                 if (se->ibuf && use_limiter) {
1850                         IMB_cache_limiter_insert(se->ibuf);
1851                         IMB_cache_limiter_ref(se->ibuf);
1852                         IMB_cache_limiter_touch(se->ibuf);
1853                 }
1854         }
1855 }
1856
1857 static TStripElem* do_build_seq_recursively(Sequence * seq, int cfra);
1858
1859 static void do_effect_seq_recursively(Sequence * seq, TStripElem *se, int cfra)
1860 {
1861         float fac, facf;
1862         struct SeqEffectHandle sh = get_sequence_effect(seq);
1863         int early_out;
1864
1865         se->se1 = 0;
1866         se->se2 = 0;
1867         se->se3 = 0;
1868
1869         if(seq->ipo && seq->ipo->curve.first) {
1870                 do_seq_ipo(seq, cfra);
1871                 fac= seq->facf0;
1872                 facf= seq->facf1;
1873         } else {
1874                 sh.get_default_fac(seq, cfra, &fac, &facf);
1875         } 
1876
1877         if( G.scene->r.mode & R_FIELDS ); else facf= fac;
1878         
1879         early_out = sh.early_out(seq, fac, facf);
1880         switch (early_out) {
1881         case -1:
1882                 /* no input needed */
1883                 break;
1884         case 0:
1885                 se->se1 = do_build_seq_recursively(seq->seq1, cfra);
1886                 se->se2 = do_build_seq_recursively(seq->seq2, cfra);
1887                 if (seq->seq3) {
1888                         se->se3 = do_build_seq_recursively(seq->seq3, cfra);
1889                 }
1890                 break;
1891         case 1:
1892                 se->se1 = do_build_seq_recursively(seq->seq1, cfra);
1893                 break;
1894         case 2:
1895                 se->se2 = do_build_seq_recursively(seq->seq2, cfra);
1896                 break;
1897         }
1898
1899
1900         do_build_seq_ibuf(seq, se, cfra, FALSE);
1901
1902         /* children are not needed anymore ... */
1903
1904         if (se->se1 && se->se1->ibuf) {
1905                 IMB_cache_limiter_unref(se->se1->ibuf);
1906         }
1907         if (se->se2 && se->se2->ibuf) {
1908                 IMB_cache_limiter_unref(se->se2->ibuf);
1909         }
1910         if (se->se3 && se->se3->ibuf) {
1911                 IMB_cache_limiter_unref(se->se3->ibuf);
1912         }
1913 }
1914
1915 static TStripElem* do_build_seq_recursively_impl(Sequence * seq, int cfra)
1916 {
1917         TStripElem *se;
1918
1919         se = give_tstripelem(seq, cfra);
1920
1921         if(se) {
1922                 if (seq->type & SEQ_EFFECT) {
1923                         do_effect_seq_recursively(seq, se, cfra);
1924                 } else {
1925                         do_build_seq_ibuf(seq, se, cfra, FALSE);
1926                 }
1927         }
1928         return se;
1929 }
1930
1931 /* FIXME:
1932    
1933 If cfra was float throughout blender (especially in the render
1934 pipeline) one could even _render_ with subframe precision
1935 instead of faking using the blend code below...
1936
1937 */
1938
1939 static TStripElem* do_handle_speed_effect(Sequence * seq, int cfra)
1940 {
1941         SpeedControlVars * s = (SpeedControlVars *)seq->effectdata;
1942         int nr = cfra - seq->start;
1943         float f_cfra;
1944         int cfra_left;
1945         int cfra_right;
1946         TStripElem * se = 0;
1947         TStripElem * se1 = 0;
1948         TStripElem * se2 = 0;
1949         
1950         sequence_effect_speed_rebuild_map(seq, 0);
1951         
1952         f_cfra = seq->start + s->frameMap[nr];
1953         
1954         cfra_left = (int) floor(f_cfra);
1955         cfra_right = (int) ceil(f_cfra);
1956
1957         se = give_tstripelem(seq, cfra);
1958
1959         if (cfra_left == cfra_right || 
1960             (s->flags & SEQ_SPEED_BLEND) == 0) {
1961                 test_and_auto_discard_ibuf(se);
1962
1963                 if (se->ibuf == NULL) {
1964                         se1 = do_build_seq_recursively_impl(
1965                                 seq->seq1, cfra_left);
1966
1967                         if((se1 && se1->ibuf && se1->ibuf->rect_float))
1968                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
1969                         else
1970                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
1971
1972                         if (se1 == 0 || se1->ibuf == 0) {
1973                                 make_black_ibuf(se->ibuf);
1974                         } else {
1975                                 if (se->ibuf != se1->ibuf) {
1976                                         if (se->ibuf) {
1977                                                 IMB_freeImBuf(se->ibuf);
1978                                         }
1979
1980                                         se->ibuf = se1->ibuf;
1981                                         IMB_refImBuf(se->ibuf);
1982                                 }
1983                         }
1984                 }
1985         } else {
1986                 struct SeqEffectHandle sh;
1987
1988                 if(se->ibuf) {
1989                         if(se->ibuf->x < seqrectx || se->ibuf->y < seqrecty 
1990                            || !(se->ibuf->rect || se->ibuf->rect_float)) {
1991                                 IMB_freeImBuf(se->ibuf);
1992                                 se->ibuf= 0;
1993                         }
1994                 }
1995
1996                 if (se->ibuf == NULL) {
1997                         se1 = do_build_seq_recursively_impl(
1998                                 seq->seq1, cfra_left);
1999                         se2 = do_build_seq_recursively_impl(
2000                                 seq->seq1, cfra_right);
2001
2002                         if((se1 && se1->ibuf && se1->ibuf->rect_float))
2003                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
2004                         else
2005                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
2006                         
2007                         if (!se1 || !se2) {
2008                                 make_black_ibuf(se->ibuf);
2009                         } else {
2010                                 sh = get_sequence_effect(seq);
2011
2012                                 sh.execute(seq, cfra, 
2013                                            f_cfra - (float) cfra_left, 
2014                                            f_cfra - (float) cfra_left, 
2015                                            se->ibuf->x, se->ibuf->y, 
2016                                            se1->ibuf, se2->ibuf, 0, se->ibuf);
2017                         }
2018                 }
2019
2020         }
2021
2022         /* caller expects this to be referenced, so do it! */
2023         if (se->ibuf) {
2024                 IMB_cache_limiter_insert(se->ibuf);
2025                 IMB_cache_limiter_ref(se->ibuf);
2026                 IMB_cache_limiter_touch(se->ibuf);
2027         }
2028
2029         /* children are no longer needed */
2030         if (se1 && se1->ibuf)
2031                 IMB_cache_limiter_unref(se1->ibuf);
2032         if (se2 && se2->ibuf)
2033                 IMB_cache_limiter_unref(se2->ibuf);
2034
2035         return se;
2036 }
2037
2038 /* 
2039  * build all ibufs recursively
2040  * 
2041  * if successfull, the returned TStripElem contains the (referenced!) imbuf
2042  * that means: you _must_ call 
2043  *
2044  * IMB_cache_limiter_unref(rval);
2045  * 
2046  * if rval != 0
2047  * 
2048  */
2049
2050 static TStripElem* do_build_seq_recursively(Sequence * seq, int cfra)
2051 {
2052         if (seq->type == SEQ_SPEED) {
2053                 return do_handle_speed_effect(seq, cfra);
2054         } else {
2055                 return do_build_seq_recursively_impl(seq, cfra);
2056         }
2057 }
2058
2059 static TStripElem* do_build_seq_array_recursively(
2060         ListBase *seqbasep, int cfra, int chanshown)
2061 {
2062         Sequence* seq_arr[MAXSEQ+1];
2063         int count;
2064         int i;
2065         TStripElem* se = 0;
2066
2067         count = get_shown_sequences(seqbasep, cfra, chanshown, (Sequence **)&seq_arr);
2068
2069         if (!count) {
2070                 return 0;
2071         }
2072
2073         se = give_tstripelem(seq_arr[count - 1], cfra);
2074
2075         if (!se) {
2076                 return 0;
2077         }
2078
2079         test_and_auto_discard_ibuf(se);
2080
2081         if (se->ibuf_comp != 0) {
2082                 IMB_cache_limiter_insert(se->ibuf_comp);
2083                 IMB_cache_limiter_ref(se->ibuf_comp);
2084                 IMB_cache_limiter_touch(se->ibuf_comp);
2085                 return se;
2086         }
2087
2088         
2089         if(count == 1) {
2090                 se = do_build_seq_recursively(seq_arr[0], cfra);
2091                 if (se->ibuf) {
2092                         se->ibuf_comp = se->ibuf;
2093                         IMB_refImBuf(se->ibuf_comp);
2094                 }
2095                 return se;
2096         }
2097
2098
2099         for (i = count - 1; i >= 0; i--) {
2100                 int early_out;
2101                 Sequence * seq = seq_arr[i];
2102                 struct SeqEffectHandle sh;
2103
2104                 se = give_tstripelem(seq, cfra);
2105
2106                 test_and_auto_discard_ibuf(se);
2107
2108                 if (se->ibuf_comp != 0) {
2109                         break;
2110                 }
2111                 if (seq->blend_mode == SEQ_BLEND_REPLACE) {
2112                         do_build_seq_recursively(seq, cfra);
2113                         if (se->ibuf) {
2114                                 se->ibuf_comp = se->ibuf;
2115                                 IMB_refImBuf(se->ibuf);
2116                         } else {
2117                                 se->ibuf_comp = IMB_allocImBuf(
2118                                         (short)seqrectx, (short)seqrecty, 
2119                                         32, IB_rect, 0);
2120                         }
2121                         break;
2122                 }
2123
2124                 sh = get_sequence_blend(seq);
2125
2126                 seq->facf0 = seq->facf1 = 1.0;
2127
2128                 if(seq->ipo && seq->ipo->curve.first) {
2129                         do_seq_ipo(seq, cfra);
2130                 } 
2131
2132                 if( G.scene->r.mode & R_FIELDS ); else seq->facf0 = seq->facf1;
2133
2134                 seq->facf0 *= seq->blend_opacity / 100.0;
2135                 seq->facf1 *= seq->blend_opacity / 100.0;
2136
2137                 early_out = sh.early_out(seq, seq->facf0, seq->facf1);
2138
2139                 switch (early_out) {
2140                 case -1:
2141                 case 2:
2142                         do_build_seq_recursively(seq, cfra);
2143                         if (se->ibuf) {
2144                                 se->ibuf_comp = se->ibuf;
2145                                 IMB_refImBuf(se->ibuf_comp);
2146                         } else {
2147                                 se->ibuf_comp = IMB_allocImBuf(
2148                                         (short)seqrectx, (short)seqrecty, 
2149                                         32, IB_rect, 0);
2150                         }
2151                         break;
2152                 case 1:
2153                         if (i == 0) {
2154                                 se->ibuf_comp = IMB_allocImBuf(
2155                                         (short)seqrectx, (short)seqrecty, 
2156                                         32, IB_rect, 0);
2157                                 IMB_cache_limiter_insert(se->ibuf_comp);
2158                                 IMB_cache_limiter_ref(se->ibuf_comp);
2159                                 IMB_cache_limiter_touch(se->ibuf_comp);
2160                         }
2161                         break;
2162                 case 0:
2163                         do_build_seq_recursively(seq, cfra);
2164                         if (!se->ibuf) {
2165                                 se->ibuf = IMB_allocImBuf(
2166                                         (short)seqrectx, (short)seqrecty, 
2167                                         32, IB_rect, 0);
2168                         }
2169                         if (i == 0) {
2170                                 se->ibuf_comp = se->ibuf;
2171                                 IMB_refImBuf(se->ibuf_comp);
2172                         }
2173                         break;
2174                 }
2175         
2176                 if (se->ibuf_comp) {
2177                         break;
2178                 }
2179         }
2180
2181         i++;
2182
2183         for (; i < count; i++) {
2184                 Sequence * seq = seq_arr[i];
2185                 struct SeqEffectHandle sh = get_sequence_blend(seq);
2186                 TStripElem* se1 = give_tstripelem(seq_arr[i-1], cfra);
2187                 TStripElem* se2 = give_tstripelem(seq_arr[i], cfra);
2188         
2189                 int early_out = sh.early_out(seq, seq->facf0, seq->facf1);
2190                 switch (early_out) {
2191                 case 0: {
2192                         int x= se2->ibuf->x;
2193                         int y= se2->ibuf->y;
2194                         int swap_input = FALSE;
2195
2196                         if (se1->ibuf_comp->rect_float ||
2197                             se2->ibuf->rect_float) {
2198                                 se2->ibuf_comp = IMB_allocImBuf(
2199                                         (short)seqrectx, (short)seqrecty, 
2200                                         32, IB_rectfloat, 0);
2201                         } else {
2202                                 se2->ibuf_comp = IMB_allocImBuf(
2203                                         (short)seqrectx, (short)seqrecty, 
2204                                         32, IB_rect, 0);
2205                         }
2206
2207
2208                         if (!se1->ibuf_comp->rect_float && 
2209                             se2->ibuf_comp->rect_float) {
2210                                 IMB_float_from_rect(se1->ibuf_comp);
2211                         }
2212                         if (!se2->ibuf->rect_float && 
2213                             se2->ibuf_comp->rect_float) {
2214                                 IMB_float_from_rect(se2->ibuf);
2215                         }
2216
2217                         if (!se1->ibuf_comp->rect && 
2218                             !se2->ibuf_comp->rect_float) {
2219                                 IMB_rect_from_float(se1->ibuf_comp);
2220                         }
2221                         if (!se2->ibuf->rect && 
2222                             !se2->ibuf_comp->rect_float) {
2223                                 IMB_rect_from_float(se2->ibuf);
2224                         }
2225
2226                         /* bad hack, to fix crazy input ordering of 
2227                            those two effects */
2228
2229                         if (seq->blend_mode == SEQ_ALPHAOVER ||
2230                             seq->blend_mode == SEQ_ALPHAUNDER ||
2231                             seq->blend_mode == SEQ_OVERDROP) {
2232                                 swap_input = TRUE;
2233                         }
2234
2235                         if (swap_input) {
2236                                 sh.execute(seq, cfra, 
2237                                            seq->facf0, seq->facf1, x, y, 
2238                                            se2->ibuf, se1->ibuf_comp, 0,
2239                                            se2->ibuf_comp);
2240                         } else {
2241                                 sh.execute(seq, cfra, 
2242                                            seq->facf0, seq->facf1, x, y, 
2243                                            se1->ibuf_comp, se2->ibuf, 0,
2244                                            se2->ibuf_comp);
2245                         }
2246                         
2247                         IMB_cache_limiter_insert(se2->ibuf_comp);
2248                         IMB_cache_limiter_ref(se2->ibuf_comp);
2249                         IMB_cache_limiter_touch(se2->ibuf_comp);
2250
2251                         IMB_cache_limiter_unref(se1->ibuf_comp);
2252                         IMB_cache_limiter_unref(se2->ibuf);
2253
2254                         break;
2255                 }
2256                 case 1: {
2257                         se2->ibuf_comp = se1->ibuf;
2258                         IMB_refImBuf(se2->ibuf_comp);
2259
2260                         break;
2261                 }
2262                 }
2263                 se = se2;
2264         }
2265
2266         return se;
2267 }
2268
2269 /*
2270  * returned ImBuf is refed!
2271  * you have to unref after usage!
2272  */
2273
2274 static ImBuf *give_ibuf_seq_impl(int rectx, int recty, int cfra, int chanshown)
2275 {
2276         Editing *ed;
2277         int count;
2278         ListBase *seqbasep;
2279         TStripElem *se;
2280
2281         ed= G.scene->ed;
2282         if(ed==0) return 0;
2283
2284         count = BLI_countlist(&ed->metastack);
2285         if((chanshown < 0) && (count > 0)) {
2286                 count = MAX2(count + chanshown, 0);
2287                 seqbasep= ((MetaStack*)BLI_findlink(&ed->metastack, count))->oldbasep;
2288         } else {
2289                 seqbasep= ed->seqbasep;
2290         }
2291
2292         seqrectx= rectx;        /* bad bad global! */
2293         seqrecty= recty;
2294
2295         se = do_build_seq_array_recursively(seqbasep, cfra, chanshown);
2296
2297         if(!se) { 
2298                 return 0;
2299         }
2300
2301         return se->ibuf_comp;
2302 }
2303
2304 ImBuf *give_ibuf_seq_direct(int rectx, int recty, int cfra,
2305                             Sequence * seq)
2306 {
2307         TStripElem* se;
2308
2309         seqrectx= rectx;        /* bad bad global! */
2310         seqrecty= recty;
2311
2312         se = do_build_seq_recursively(seq, cfra);
2313
2314         if(!se) { 
2315                 return 0;
2316         }
2317
2318         if (se->ibuf) {
2319                 IMB_cache_limiter_unref(se->ibuf);
2320         }
2321
2322         return se->ibuf;
2323 }
2324
2325 ImBuf *give_ibuf_seq(int rectx, int recty, int cfra, int chanshown)
2326 {
2327         ImBuf* i = give_ibuf_seq_impl(rectx, recty, cfra, chanshown);
2328
2329         if (i) {
2330                 IMB_cache_limiter_unref(i);
2331         }
2332         return i;
2333 }
2334
2335 /* threading api */
2336
2337 static ListBase running_threads;
2338 static ListBase prefetch_wait;
2339 static ListBase prefetch_done;
2340
2341 static pthread_mutex_t queue_lock          = PTHREAD_MUTEX_INITIALIZER;
2342 static pthread_mutex_t wakeup_lock         = PTHREAD_MUTEX_INITIALIZER;
2343 static pthread_cond_t  wakeup_cond         = PTHREAD_COND_INITIALIZER;
2344
2345 static pthread_mutex_t prefetch_ready_lock = PTHREAD_MUTEX_INITIALIZER;
2346 static pthread_cond_t  prefetch_ready_cond = PTHREAD_COND_INITIALIZER;
2347
2348 static pthread_mutex_t frame_done_lock     = PTHREAD_MUTEX_INITIALIZER;
2349 static pthread_cond_t  frame_done_cond     = PTHREAD_COND_INITIALIZER;
2350
2351 static volatile int seq_thread_shutdown = FALSE;
2352 static volatile int seq_last_given_monoton_cfra = 0;
2353 static int monoton_cfra = 0;
2354
2355 typedef struct PrefetchThread {
2356         struct PrefetchThread *next, *prev;
2357         struct PrefetchQueueElem *current;
2358         pthread_t pthread;
2359         int running;
2360 } PrefetchThread;
2361
2362 typedef struct PrefetchQueueElem {
2363         struct PrefetchQueueElem *next, *prev;
2364         
2365         int rectx;
2366         int recty;
2367         int cfra;
2368         int chanshown;
2369
2370         int monoton_cfra;
2371
2372         struct ImBuf * ibuf;
2373 } PrefetchQueueElem;
2374
2375 static void * seq_prefetch_thread(void * This_)
2376 {
2377         PrefetchThread * This = This_;
2378
2379         while (!seq_thread_shutdown) {
2380                 PrefetchQueueElem * e;
2381                 int s_last;
2382
2383                 pthread_mutex_lock(&queue_lock);
2384                 e = prefetch_wait.first;
2385                 if (e) {
2386                         BLI_remlink(&prefetch_wait, e);
2387                 }
2388                 s_last = seq_last_given_monoton_cfra;
2389
2390                 This->current = e;
2391
2392                 pthread_mutex_unlock(&queue_lock);
2393
2394                 if (!e) {
2395                         pthread_mutex_lock(&prefetch_ready_lock);
2396
2397                         This->running = FALSE;
2398
2399                         pthread_cond_signal(&prefetch_ready_cond);
2400                         pthread_mutex_unlock(&prefetch_ready_lock);
2401
2402                         pthread_mutex_lock(&wakeup_lock);
2403                         if (!seq_thread_shutdown) {
2404                                 pthread_cond_wait(&wakeup_cond, &wakeup_lock);
2405                         }
2406                         pthread_mutex_unlock(&wakeup_lock);
2407                         continue;
2408                 }
2409
2410                 This->running = TRUE;
2411                 
2412                 if (e->cfra >= s_last) { 
2413                         e->ibuf = give_ibuf_seq_impl(
2414                                 e->rectx, e->recty, e->cfra, e->chanshown);
2415                 }
2416
2417                 pthread_mutex_lock(&queue_lock);
2418
2419                 BLI_addtail(&prefetch_done, e);
2420
2421                 for (e = prefetch_wait.first; e; e = e->next) {
2422                         if (s_last > e->monoton_cfra) {
2423                                 BLI_remlink(&prefetch_wait, e);
2424                                 MEM_freeN(e);
2425                         }
2426                 }
2427
2428                 for (e = prefetch_done.first; e; e = e->next) {
2429                         if (s_last > e->monoton_cfra) {
2430                                 if (e->ibuf) {
2431                                         IMB_cache_limiter_unref(e->ibuf);
2432                                 }
2433                                 BLI_remlink(&prefetch_done, e);
2434                                 MEM_freeN(e);
2435                         }
2436                 }
2437
2438                 pthread_mutex_unlock(&queue_lock);
2439
2440                 pthread_mutex_lock(&frame_done_lock);
2441                 pthread_cond_signal(&frame_done_cond);
2442                 pthread_mutex_unlock(&frame_done_lock);
2443         }
2444         return 0;
2445 }
2446
2447 void seq_start_threads()
2448 {
2449         int i;
2450
2451         running_threads.first = running_threads.last = NULL;
2452         prefetch_wait.first = prefetch_wait.last = NULL;
2453         prefetch_done.first = prefetch_done.last = NULL;
2454
2455         seq_thread_shutdown = FALSE;
2456         seq_last_given_monoton_cfra = monoton_cfra = 0;
2457
2458         /* since global structures are modified during the processing
2459            of one frame, only one render thread is currently possible... 
2460
2461            (but we code, in the hope, that we can remove this restriction
2462            soon...)
2463         */
2464
2465         fprintf(stderr, "SEQ-THREAD: seq_start_threads\n");
2466
2467         for (i = 0; i < 1; i++) {
2468                 PrefetchThread *t = MEM_callocN(sizeof(PrefetchThread), 
2469                                                 "prefetch_thread");
2470                 t->running = TRUE;
2471                 BLI_addtail(&running_threads, t);
2472
2473                 pthread_create(&t->pthread, NULL, seq_prefetch_thread, t);
2474         }
2475
2476         /* init malloc mutex */
2477         BLI_init_threads(0, 0, 0);
2478 }
2479
2480 void seq_stop_threads()
2481 {
2482         PrefetchThread *tslot;
2483         PrefetchQueueElem * e;
2484
2485         fprintf(stderr, "SEQ-THREAD: seq_stop_threads()\n");
2486
2487         if (seq_thread_shutdown) {
2488                 fprintf(stderr, "SEQ-THREAD: ... already stopped\n");
2489                 return;
2490         }
2491         
2492         pthread_mutex_lock(&wakeup_lock);
2493
2494         seq_thread_shutdown = TRUE;
2495
2496         pthread_cond_broadcast(&wakeup_cond);
2497         pthread_mutex_unlock(&wakeup_lock);
2498
2499         for(tslot = running_threads.first; tslot; tslot= tslot->next) {
2500                 pthread_join(tslot->pthread, NULL);
2501         }
2502
2503
2504         for (e = prefetch_wait.first; e; e = e->next) {
2505                 BLI_remlink(&prefetch_wait, e);
2506                 MEM_freeN(e);
2507         }
2508
2509         for (e = prefetch_done.first; e; e = e->next) {
2510                 if (e->ibuf) {
2511                         IMB_cache_limiter_unref(e->ibuf);
2512                 }
2513                 BLI_remlink(&prefetch_done, e);
2514                 MEM_freeN(e);
2515         }
2516
2517         BLI_freelistN(&running_threads);
2518
2519         /* deinit malloc mutex */
2520         BLI_end_threads(0);
2521 }
2522
2523 void give_ibuf_prefetch_request(int rectx, int recty, int cfra, int chanshown)
2524 {
2525         PrefetchQueueElem * e;
2526         if (seq_thread_shutdown) {
2527                 return;
2528         }
2529
2530         e = MEM_callocN(sizeof(PrefetchQueueElem), "prefetch_queue_elem");
2531         e->rectx = rectx;
2532         e->recty = recty;
2533         e->cfra = cfra;
2534         e->chanshown = chanshown;
2535         e->monoton_cfra = monoton_cfra++;
2536
2537         pthread_mutex_lock(&queue_lock);
2538         BLI_addtail(&prefetch_wait, e);
2539         pthread_mutex_unlock(&queue_lock);
2540         
2541         pthread_mutex_lock(&wakeup_lock);
2542         pthread_cond_signal(&wakeup_cond);
2543         pthread_mutex_unlock(&wakeup_lock);
2544 }
2545
2546 void seq_wait_for_prefetch_ready()
2547 {
2548         PrefetchThread *tslot;
2549
2550         if (seq_thread_shutdown) {
2551                 return;
2552         }
2553
2554         fprintf(stderr, "SEQ-THREAD: rendering prefetch frames...\n");
2555
2556         pthread_mutex_lock(&prefetch_ready_lock);
2557
2558         for(;;) {
2559                 for(tslot = running_threads.first; tslot; tslot= tslot->next) {
2560                         if (tslot->running) {
2561                                 break;
2562                         }
2563                 }
2564                 if (!tslot) {
2565                         break;
2566                 }
2567                 pthread_cond_wait(&prefetch_ready_cond, &prefetch_ready_lock);
2568         }
2569
2570         pthread_mutex_unlock(&prefetch_ready_lock);
2571
2572         fprintf(stderr, "SEQ-THREAD: prefetch done\n");
2573 }
2574
2575 ImBuf * give_ibuf_seq_threaded(int rectx, int recty, int cfra, int chanshown)
2576 {
2577         PrefetchQueueElem * e = 0;
2578         int found_something = FALSE;
2579
2580         if (seq_thread_shutdown) {
2581                 return give_ibuf_seq(rectx, recty, cfra, chanshown);
2582         }
2583
2584         while (!e) {
2585                 int success = FALSE;
2586                 pthread_mutex_lock(&queue_lock);
2587
2588                 for (e = prefetch_done.first; e; e = e->next) {
2589                         if (cfra == e->cfra &&
2590                             chanshown == e->chanshown &&
2591                             rectx == e->rectx && 
2592                             recty == e->recty) {
2593                                 success = TRUE;
2594                                 found_something = TRUE;
2595                                 break;
2596                         }
2597                 }
2598
2599                 if (!e) {
2600                         for (e = prefetch_wait.first; e; e = e->next) {
2601                                 if (cfra == e->cfra &&
2602                                     chanshown == e->chanshown &&
2603                                     rectx == e->rectx && 
2604                                     recty == e->recty) {
2605                                         found_something = TRUE;
2606                                         break;
2607                                 }
2608                         }
2609                 }
2610
2611                 if (!e) {
2612                         PrefetchThread *tslot;
2613
2614                         for(tslot = running_threads.first; 
2615                             tslot; tslot= tslot->next) {
2616                                 if (tslot->current &&
2617                                     cfra == tslot->current->cfra &&
2618                                     chanshown == tslot->current->chanshown &&
2619                                     rectx == tslot->current->rectx && 
2620                                     recty == tslot->current->recty) {
2621                                         found_something = TRUE;
2622                                         break;
2623                                 }
2624                         }
2625                 }
2626
2627                 /* e->ibuf is unrefed by render thread on next round. */
2628
2629                 if (e) {
2630                         seq_last_given_monoton_cfra = e->monoton_cfra;
2631                 }
2632
2633                 pthread_mutex_unlock(&queue_lock);
2634
2635                 if (!success) {
2636                         e = NULL;
2637
2638                         if (!found_something) {
2639                                 fprintf(stderr, 
2640                                         "SEQ-THREAD: Requested frame "
2641                                         "not in queue ???\n");
2642                                 break;
2643                         }
2644                         pthread_mutex_lock(&frame_done_lock);
2645                         pthread_cond_wait(&frame_done_cond, &frame_done_lock);
2646                         pthread_mutex_unlock(&frame_done_lock);
2647                 }
2648         }
2649         
2650         return e ? e->ibuf : 0;
2651 }
2652
2653 /* Functions to free imbuf and anim data on changes */
2654
2655 static void free_imbuf_strip_elem(TStripElem *se)
2656 {
2657         if(se->ibuf) {
2658                 IMB_freeImBuf(se->ibuf);
2659         }
2660         if(se->ibuf_comp) {
2661                 IMB_freeImBuf(se->ibuf_comp);
2662         }
2663         se->ibuf_comp = 0;
2664         se->ibuf= 0;
2665         se->ok= STRIPELEM_OK;
2666         se->se1= se->se2= se->se3= 0;
2667 }
2668
2669 static void free_anim_seq(Sequence *seq)
2670 {
2671         if(seq->anim) {
2672                 IMB_free_anim(seq->anim);
2673                 seq->anim = 0;
2674         }
2675 }
2676
2677 void free_imbuf_seq_except(int cfra)
2678 {
2679         Editing *ed= G.scene->ed;
2680         Sequence *seq;
2681         TStripElem *se;
2682         int a;
2683
2684         if(ed==0) return;
2685
2686         WHILE_SEQ(&ed->seqbase) {
2687                 if(seq->strip) {
2688                         TStripElem * curelem = give_tstripelem(seq, cfra);
2689
2690                         for(a = 0, se = seq->strip->tstripdata; 
2691                             a < seq->strip->len && se; a++, se++) {
2692                                 if(se != curelem) {
2693                                         free_imbuf_strip_elem(se);
2694                                 }
2695                         }
2696                         for(a = 0, se = seq->strip->tstripdata_startstill;
2697                             a < seq->strip->startstill && se; a++, se++) {
2698                                 if(se != curelem) {
2699                                         free_imbuf_strip_elem(se);
2700                                 }
2701                         }
2702                         for(a = 0, se = seq->strip->tstripdata_endstill;
2703                             a < seq->strip->endstill && se; a++, se++) {
2704                                 if(se != curelem) {
2705                                         free_imbuf_strip_elem(se);
2706                                 }
2707                         }
2708                         if(seq->strip->ibuf_startstill) {
2709                                 IMB_freeImBuf(seq->strip->ibuf_startstill);
2710                                 seq->strip->ibuf_startstill = 0;
2711                         }
2712
2713                         if(seq->strip->ibuf_endstill) {
2714                                 IMB_freeImBuf(seq->strip->ibuf_endstill);
2715                                 seq->strip->ibuf_endstill = 0;
2716                         }
2717
2718                         if(seq->type==SEQ_MOVIE)
2719                                 if(seq->startdisp > cfra || seq->enddisp < cfra)
2720                                         free_anim_seq(seq);
2721                 }
2722         }
2723         END_SEQ
2724 }
2725
2726 void free_imbuf_seq()
2727 {
2728         Editing *ed= G.scene->ed;
2729         Sequence *seq;
2730         TStripElem *se;
2731         int a;
2732
2733         if(ed==0) return;
2734
2735         WHILE_SEQ(&ed->seqbase) {
2736                 if(seq->strip) {
2737                         for(a = 0, se = seq->strip->tstripdata; 
2738                             a < seq->strip->len && se; a++, se++) {
2739                                 free_imbuf_strip_elem(se);
2740                         }
2741                         for(a = 0, se = seq->strip->tstripdata_startstill; 
2742                             a < seq->strip->startstill && se; a++, se++) {
2743                                 free_imbuf_strip_elem(se);
2744                         }
2745                         for(a = 0, se = seq->strip->tstripdata_endstill; 
2746                             a < seq->strip->endstill && se; a++, se++) {
2747                                 free_imbuf_strip_elem(se);
2748                         }
2749                         if(seq->strip->ibuf_startstill) {
2750                                 IMB_freeImBuf(seq->strip->ibuf_startstill);
2751                                 seq->strip->ibuf_startstill = 0;
2752                         }
2753
2754                         if(seq->strip->ibuf_endstill) {
2755                                 IMB_freeImBuf(seq->strip->ibuf_endstill);
2756                                 seq->strip->ibuf_endstill = 0;
2757                         }
2758
2759                         if(seq->type==SEQ_MOVIE)
2760                                 free_anim_seq(seq);
2761                         if(seq->type==SEQ_SPEED) {
2762                                 sequence_effect_speed_rebuild_map(seq, 1);
2763                         }
2764                 }
2765         }
2766         END_SEQ
2767 }
2768
2769 void free_imbuf_seq_with_ipo(struct Ipo *ipo)
2770 {
2771         /* force update of all sequences with this ipo, on ipo changes */
2772         Editing *ed= G.scene->ed;
2773         Sequence *seq;
2774
2775         if(ed==0) return;
2776
2777         WHILE_SEQ(&ed->seqbase) {
2778                 if(seq->ipo == ipo) {
2779                         update_changed_seq_and_deps(seq, 0, 1);
2780                         if(seq->type == SEQ_SPEED) {
2781                                 sequence_effect_speed_rebuild_map(seq, 1);
2782                         }
2783                 }
2784         }
2785         END_SEQ
2786 }
2787
2788 static int update_changed_seq_recurs(Sequence *seq, Sequence *changed_seq, int len_change, int ibuf_change)
2789 {
2790         Sequence *subseq;
2791         int a, free_imbuf = 0;
2792         TStripElem *se;
2793
2794         /* recurs downwards to see if this seq depends on the changed seq */
2795
2796         if(seq == NULL)
2797                 return 0;
2798
2799         if(seq == changed_seq)
2800                 free_imbuf = 1;
2801         
2802         for(subseq=seq->seqbase.first; subseq; subseq=subseq->next)
2803                 if(update_changed_seq_recurs(subseq, changed_seq, len_change, ibuf_change))
2804                         free_imbuf = TRUE;
2805         
2806         if(seq->seq1)
2807                 if(update_changed_seq_recurs(seq->seq1, changed_seq, len_change, ibuf_change))
2808                         free_imbuf = TRUE;
2809         if(seq->seq2 && (seq->seq2 != seq->seq1))
2810                 if(update_changed_seq_recurs(seq->seq2, changed_seq, len_change, ibuf_change))
2811                         free_imbuf = TRUE;
2812         if(seq->seq3 && (seq->seq3 != seq->seq1) && (seq->seq3 != seq->seq2))
2813                 if(update_changed_seq_recurs(seq->seq3, changed_seq, len_change, ibuf_change))
2814                         free_imbuf = TRUE;
2815         
2816         if(free_imbuf) {
2817                 if(ibuf_change) {
2818                         se= seq->strip->tstripdata;
2819                         if (se) {
2820                                 for(a=0; a<seq->len; a++, se++)
2821                                         free_imbuf_strip_elem(se);
2822                         }
2823                 
2824                         if(seq->type == SEQ_MOVIE)
2825                                 free_anim_seq(seq);
2826                         if(seq->type == SEQ_SPEED) {
2827                                 sequence_effect_speed_rebuild_map(seq, 1);
2828                         }
2829                 }
2830
2831                 if(len_change)
2832                         calc_sequence(seq);
2833         }
2834         
2835         return free_imbuf;
2836 }
2837
2838 void update_changed_seq_and_deps(Sequence *changed_seq, int len_change, int ibuf_change)
2839 {
2840         Editing *ed= G.scene->ed;
2841         Sequence *seq;
2842
2843         if (!ed) return;
2844
2845         for (seq=ed->seqbase.first; seq; seq=seq->next)
2846                 update_changed_seq_recurs(seq, changed_seq, len_change, ibuf_change);
2847 }
2848
2849 /* bad levell call... */
2850 void do_render_seq(RenderResult *rr, int cfra)
2851 {
2852         ImBuf *ibuf;
2853
2854         G.f |= G_PLAYANIM;      /* waitcursor patch */
2855
2856         ibuf= give_ibuf_seq(rr->rectx, rr->recty, cfra, 0);
2857         
2858         if(ibuf) {
2859                 if(ibuf->rect_float) {
2860                         if (!rr->rectf)
2861                                 rr->rectf= MEM_mallocN(4*sizeof(float)*rr->rectx*rr->recty, "render_seq rectf");
2862                         
2863                         memcpy(rr->rectf, ibuf->rect_float, 4*sizeof(float)*rr->rectx*rr->recty);
2864                         
2865                         /* TSK! Since sequence render doesn't free the *rr render result, the old rect32
2866                            can hang around when sequence render has rendered a 32 bits one before */
2867                         if(rr->rect32) {
2868                                 MEM_freeN(rr->rect32);
2869                                 rr->rect32= NULL;
2870                         }
2871                 }
2872                 else if(ibuf->rect) {
2873                         if (!rr->rect32)
2874                                 rr->rect32= MEM_mallocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
2875
2876                         memcpy(rr->rect32, ibuf->rect, 4*rr->rectx*rr->recty);
2877
2878                         /* if (ibuf->zbuf) { */
2879                         /*      if (R.rectz) freeN(R.rectz); */
2880                         /*      R.rectz = BLI_dupallocN(ibuf->zbuf); */
2881                         /* } */
2882                 }
2883                 
2884                 /* Let the cache limitor take care of this (schlaile) */
2885                 /* While render let's keep all memory available for render 
2886                    (ton)
2887                    At least if free memory is tight...
2888                    This can make a big difference in encoding speed
2889                    (it is around 4 times(!) faster, if we do not waste time
2890                    on freeing _all_ buffers every time on long timelines...)
2891                    (schlaile)
2892                 */
2893                 {
2894                         extern int mem_in_use;
2895                         extern int mmap_in_use;
2896
2897                         int max = MEM_CacheLimiter_get_maximum();
2898                         if (max != 0 && mem_in_use + mmap_in_use > max) {
2899                                 fprintf(stderr, "mem_in_use = %d, max = %d\n",
2900                                         mem_in_use + mmap_in_use, max);
2901                                 fprintf(stderr, "Cleaning up, please wait...\n"
2902                                         "If this happens very often,\n"
2903                                         "consider "
2904                                         "raising the memcache limit in the "
2905                                         "user preferences.\n");
2906                                 free_imbuf_seq();
2907                         }
2908                 }
2909         }
2910         else {
2911                 /* render result is delivered empty in most cases, nevertheless we handle all cases */
2912                 if (rr->rectf)
2913                         memset(rr->rectf, 0, 4*sizeof(float)*rr->rectx*rr->recty);
2914                 else if (rr->rect32)
2915                         memset(rr->rect32, 0, 4*rr->rectx*rr->recty);
2916                 else
2917                         rr->rect32= MEM_callocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
2918         }
2919         
2920         G.f &= ~G_PLAYANIM;
2921
2922 }