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