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