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