== Sequencer ==
[blender-staging.git] / source / blender / src / sequence.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * Contributor(s): Peter Schlaile <peter [at] schlaile [dot] de> 2005/2006
27  *
28  * ***** END GPL/BL DUAL LICENSE BLOCK *****
29  */
30
31 #include <string.h>
32 #include <math.h>
33 #include <stdlib.h>
34
35 #include "MEM_guardedalloc.h"
36 #include "MEM_CacheLimiterC-Api.h"
37
38 #include "BLI_blenlib.h"
39 #include "BLI_arithb.h"
40
41 #include "IMB_imbuf_types.h"
42 #include "IMB_imbuf.h"
43
44 #include "DNA_ipo_types.h"
45 #include "DNA_scene_types.h"
46 #include "DNA_sequence_types.h"
47 #include "DNA_view3d_types.h"
48
49 #include "BKE_global.h"
50 #include "BKE_image.h"
51 #include "BKE_ipo.h"
52 #include "BKE_main.h"
53 #include "BKE_scene.h"
54 #include "BKE_texture.h"
55 #include "BKE_utildefines.h"
56
57 #include "BIF_editsound.h"
58 #include "BIF_editseq.h"
59 #include "BSE_filesel.h"
60 #include "BSE_headerbuttons.h"
61 #include "BIF_interface.h"
62 #include "BIF_renderwin.h"
63 #include "BIF_screen.h"
64 #include "BIF_space.h"
65 #include "BIF_toolbox.h"
66
67 #include "BSE_sequence.h"
68 #include "BSE_seqeffects.h"
69
70 #include "RE_pipeline.h"                // talks to entire render API
71
72 #include "blendef.h"
73
74 #include "BLI_threads.h"
75 #include <pthread.h>
76
77 int seqrectx, seqrecty;
78
79 void free_tstripdata(int len, TStripElem *se)
80 {
81         TStripElem *seo;
82         int a;
83
84         seo= se;
85         if (!se) {
86                 return;
87         }
88
89         for(a=0; a<len; a++, se++) {
90                 if(se->ibuf) {
91                         IMB_freeImBuf(se->ibuf);
92                         se->ibuf = 0;
93                 }
94                 if(se->ibuf_comp) {
95                         IMB_freeImBuf(se->ibuf_comp);
96                         se->ibuf_comp = 0;
97                 }
98         }
99
100         MEM_freeN(seo);
101
102 }
103
104 void seq_proxy_free(StripProxy * proxy)
105 {
106         MEM_freeN(proxy);
107 }
108
109 void free_strip(Strip *strip)
110 {
111         strip->us--;
112         if(strip->us>0) return;
113         if(strip->us<0) {
114                 printf("error: negative users in strip\n");
115                 return;
116         }
117
118         if(strip->stripdata) {
119                 MEM_freeN(strip->stripdata);
120         }
121         if (strip->crop) {
122                 MEM_freeN(strip->crop);
123         }
124         if (strip->transform) {
125                 MEM_freeN(strip->transform);
126         }
127         if (strip->proxy) {
128                 seq_proxy_free(strip->proxy);
129         }
130
131         free_tstripdata(strip->len, strip->tstripdata);
132
133         MEM_freeN(strip);
134 }
135
136 void new_tstripdata(Sequence *seq)
137 {
138         if(seq->strip) {
139                 if (seq->strip->tstripdata) {
140                         free_tstripdata(seq->strip->len, 
141                                         seq->strip->tstripdata);
142                 }
143                 seq->strip->tstripdata= 0;
144                 seq->strip->len= seq->len;
145         }
146 }
147
148 void free_sequence(Sequence *seq)
149 {
150         Sequence *last_seq = get_last_seq();
151
152         if(seq->strip) free_strip(seq->strip);
153
154         if(seq->anim) IMB_free_anim(seq->anim);
155         if(seq->hdaudio) sound_close_hdaudio(seq->hdaudio);
156
157         if (seq->type & SEQ_EFFECT) {
158                 struct SeqEffectHandle sh = get_sequence_effect(seq);
159
160                 sh.free(seq);
161         }
162
163         if(seq==last_seq) set_last_seq(NULL);
164
165         MEM_freeN(seq);
166 }
167
168 /*
169   **********************************************************************
170   * build_seqar
171   **********************************************************************
172   * Build a complete array of _all_ sequencies (including those
173   * in metastrips!)
174   **********************************************************************
175 */
176
177 static void do_seq_count(ListBase *seqbase, int *totseq)
178 {
179         Sequence *seq;
180
181         seq= seqbase->first;
182         while(seq) {
183                 (*totseq)++;
184                 if(seq->seqbase.first) do_seq_count(&seq->seqbase, totseq);
185                 seq= seq->next;
186         }
187 }
188
189 static void do_build_seqar(ListBase *seqbase, Sequence ***seqar, int depth)
190 {
191         Sequence *seq;
192
193         seq= seqbase->first;
194         while(seq) {
195                 seq->depth= depth;
196                 if(seq->seqbase.first) do_build_seqar(&seq->seqbase, seqar, depth+1);
197                 **seqar= seq;
198                 (*seqar)++;
199                 seq= seq->next;
200         }
201 }
202
203 void build_seqar(ListBase *seqbase, Sequence  ***seqar, int *totseq)
204 {
205         Sequence **tseqar;
206
207         *totseq= 0;
208         do_seq_count(seqbase, totseq);
209
210         if(*totseq==0) {
211                 *seqar= 0;
212                 return;
213         }
214         *seqar= MEM_mallocN(sizeof(void *)* *totseq, "seqar");
215         tseqar= *seqar;
216
217         do_build_seqar(seqbase, seqar, 0);
218         *seqar= tseqar;
219 }
220
221 static void do_seq_count_cb(ListBase *seqbase, int *totseq,
222                             int (*test_func)(Sequence * seq))
223 {
224         Sequence *seq;
225
226         seq= seqbase->first;
227         while(seq) {
228                 int test = test_func(seq);
229                 if (test & BUILD_SEQAR_COUNT_CURRENT) {
230                         (*totseq)++;
231                 }
232                 if(seq->seqbase.first && (test & BUILD_SEQAR_COUNT_CHILDREN)) {
233                         do_seq_count_cb(&seq->seqbase, totseq, test_func);
234                 }
235                 seq= seq->next;
236         }
237 }
238
239 static void do_build_seqar_cb(ListBase *seqbase, Sequence ***seqar, int depth,
240                               int (*test_func)(Sequence * seq))
241 {
242         Sequence *seq;
243
244         seq= seqbase->first;
245         while(seq) {
246                 int test = test_func(seq);
247                 seq->depth= depth;
248
249                 if(seq->seqbase.first && (test & BUILD_SEQAR_COUNT_CHILDREN)) {
250                         do_build_seqar_cb(&seq->seqbase, seqar, depth+1, 
251                                           test_func);
252                 }
253                 if (test & BUILD_SEQAR_COUNT_CURRENT) {
254                         **seqar= seq;
255                         (*seqar)++;
256                 }
257                 seq= seq->next;
258         }
259 }
260
261 void build_seqar_cb(ListBase *seqbase, Sequence  ***seqar, int *totseq,
262                     int (*test_func)(Sequence * seq))
263 {
264         Sequence **tseqar;
265
266         *totseq= 0;
267         do_seq_count_cb(seqbase, totseq, test_func);
268
269         if(*totseq==0) {
270                 *seqar= 0;
271                 return;
272         }
273         *seqar= MEM_mallocN(sizeof(void *)* *totseq, "seqar");
274         tseqar= *seqar;
275
276         do_build_seqar_cb(seqbase, seqar, 0, test_func);
277         *seqar= tseqar;
278 }
279
280
281 void free_editing(Editing *ed)
282 {
283         MetaStack *ms;
284         Sequence *seq;
285
286         if(ed==NULL) return;
287         set_last_seq(NULL);     /* clear_last_seq doesnt work, it screws up free_sequence */
288
289         WHILE_SEQ(&ed->seqbase) {
290                 free_sequence(seq);
291         }
292         END_SEQ
293
294         while( (ms= ed->metastack.first) ) {
295                 BLI_remlink(&ed->metastack, ms);
296                 MEM_freeN(ms);
297         }
298
299         MEM_freeN(ed);
300
301 }
302
303 void calc_sequence_disp(Sequence *seq)
304 {
305         if(seq->startofs && seq->startstill) seq->startstill= 0;
306         if(seq->endofs && seq->endstill) seq->endstill= 0;
307         
308         seq->startdisp= seq->start + seq->startofs - seq->startstill;
309         seq->enddisp= seq->start+seq->len - seq->endofs + seq->endstill;
310         
311         seq->handsize= 10.0;    /* 10 frames */
312         if( seq->enddisp-seq->startdisp < 10 ) {
313                 seq->handsize= (float)(0.5*(seq->enddisp-seq->startdisp));
314         }
315         else if(seq->enddisp-seq->startdisp > 250) {
316                 seq->handsize= (float)((seq->enddisp-seq->startdisp)/25);
317         }
318 }
319
320 void calc_sequence(Sequence *seq)
321 {
322         Sequence *seqm;
323         int min, max;
324
325         /* check all metas recursively */
326         seqm= seq->seqbase.first;
327         while(seqm) {
328                 if(seqm->seqbase.first) calc_sequence(seqm);
329                 seqm= seqm->next;
330         }
331
332         /* effects and meta: automatic start and end */
333
334         if(seq->type & SEQ_EFFECT) {
335                 /* pointers */
336                 if(seq->seq2==0) seq->seq2= seq->seq1;
337                 if(seq->seq3==0) seq->seq3= seq->seq1;
338
339                 /* effecten go from seq1 -> seq2: test */
340
341                 /* we take the largest start and smallest end */
342
343                 // seq->start= seq->startdisp= MAX2(seq->seq1->startdisp, seq->seq2->startdisp);
344                 // seq->enddisp= MIN2(seq->seq1->enddisp, seq->seq2->enddisp);
345
346                 if (seq->seq1) {
347                         seq->start= seq->startdisp= MAX3(seq->seq1->startdisp, seq->seq2->startdisp, seq->seq3->startdisp);
348                         seq->enddisp= MIN3(seq->seq1->enddisp, seq->seq2->enddisp, seq->seq3->enddisp);
349                         seq->len= seq->enddisp - seq->startdisp;
350                 } else {
351                         calc_sequence_disp(seq);
352                 }
353
354                 if(seq->strip && seq->len!=seq->strip->len) {
355                         new_tstripdata(seq);
356                 }
357
358         }
359         else {
360                 if(seq->type==SEQ_META) {
361                         seqm= seq->seqbase.first;
362                         if(seqm) {
363                                 min= 1000000;
364                                 max= -1000000;
365                                 while(seqm) {
366                                         if(seqm->startdisp < min) min= seqm->startdisp;
367                                         if(seqm->enddisp > max) max= seqm->enddisp;
368                                         seqm= seqm->next;
369                                 }
370                                 seq->start= min;
371                                 seq->len= max-min;
372
373                                 if(seq->strip && seq->len!=seq->strip->len) {
374                                         new_tstripdata(seq);
375                                 }
376                         }
377                 }
378                 calc_sequence_disp(seq);
379         }
380 }
381
382 void reload_sequence_new_file(Sequence * seq)
383 {
384         char str[FILE_MAXDIR+FILE_MAXFILE];
385
386         if (!(seq->type == SEQ_MOVIE || seq->type == SEQ_IMAGE ||
387               seq->type == SEQ_HD_SOUND || seq->type == SEQ_SCENE)) {
388                 return;
389         }
390
391         new_tstripdata(seq);
392
393         if (seq->type == SEQ_IMAGE) {
394                 return;
395         }
396
397         strncpy(str, seq->strip->dir, FILE_MAXDIR-1);
398         strncat(str, seq->strip->stripdata->name, FILE_MAXFILE-1);
399
400         if (seq->type == SEQ_MOVIE) {
401                 if(seq->anim) IMB_free_anim(seq->anim);
402                 seq->anim = openanim(str, IB_rect);
403
404                 if (!seq->anim) {
405                         return;
406                 }
407         
408                 seq->len = IMB_anim_get_duration(seq->anim);
409                 
410                 seq->anim_preseek = IMB_anim_get_preseek(seq->anim);
411
412                 seq->len -= seq->anim_startofs;
413                 seq->len -= seq->anim_endofs;
414                 if (seq->len < 0) {
415                         seq->len = 0;
416                 }
417                 seq->strip->len = seq->len;
418         } else if (seq->type == SEQ_HD_SOUND) {
419                 if(seq->hdaudio) sound_close_hdaudio(seq->hdaudio);
420                 seq->hdaudio = sound_open_hdaudio(str);
421
422                 if (!seq->hdaudio) {
423                         return;
424                 }
425
426                 seq->strip->len = seq->len 
427                         = sound_hdaudio_get_duration(seq->hdaudio, FPS);
428         } else if (seq->type == SEQ_SCENE) {
429                 seq->len= seq->scene->r.efra - seq->scene->r.sfra + 1;
430                 seq->len -= seq->anim_startofs;
431                 seq->len -= seq->anim_endofs;
432                 if (seq->len < 0) {
433                         seq->len = 0;
434                 }
435                 seq->strip->len = seq->len;
436         }
437
438
439         calc_sequence(seq);
440 }
441
442 void sort_seq()
443 {
444         /* all strips together per kind, and in order of y location ("machine") */
445         ListBase seqbase, effbase;
446         Editing *ed;
447         Sequence *seq, *seqt;
448
449         ed= G.scene->ed;
450         if(ed==0) return;
451
452         seqbase.first= seqbase.last= 0;
453         effbase.first= effbase.last= 0;
454
455         while( (seq= ed->seqbasep->first) ) {
456                 BLI_remlink(ed->seqbasep, seq);
457
458                 if(seq->type & SEQ_EFFECT) {
459                         seqt= effbase.first;
460                         while(seqt) {
461                                 if(seqt->machine>=seq->machine) {
462                                         BLI_insertlinkbefore(&effbase, seqt, seq);
463                                         break;
464                                 }
465                                 seqt= seqt->next;
466                         }
467                         if(seqt==0) BLI_addtail(&effbase, seq);
468                 }
469                 else {
470                         seqt= seqbase.first;
471                         while(seqt) {
472                                 if(seqt->machine>=seq->machine) {
473                                         BLI_insertlinkbefore(&seqbase, seqt, seq);
474                                         break;
475                                 }
476                                 seqt= seqt->next;
477                         }
478                         if(seqt==0) BLI_addtail(&seqbase, seq);
479                 }
480         }
481
482         addlisttolist(&seqbase, &effbase);
483         *(ed->seqbasep)= seqbase;
484 }
485
486
487 void clear_scene_in_allseqs(Scene *sce)
488 {
489         Scene *sce1;
490         Editing *ed;
491         Sequence *seq;
492
493         /* when a scene is deleted: test all seqs */
494
495         sce1= G.main->scene.first;
496         while(sce1) {
497                 if(sce1!=sce && sce1->ed) {
498                         ed= sce1->ed;
499
500                         WHILE_SEQ(&ed->seqbase) {
501
502                                 if(seq->scene==sce) seq->scene= 0;
503
504                         }
505                         END_SEQ
506                 }
507
508                 sce1= sce1->id.next;
509         }
510 }
511
512 char *give_seqname_by_type(int type)
513 {
514         switch(type) {
515         case SEQ_META:       return "Meta";
516         case SEQ_IMAGE:      return "Image";
517         case SEQ_SCENE:      return "Scene";
518         case SEQ_MOVIE:      return "Movie";
519         case SEQ_RAM_SOUND:  return "Audio (RAM)";
520         case SEQ_HD_SOUND:   return "Audio (HD)";
521         case SEQ_CROSS:      return "Cross";
522         case SEQ_GAMCROSS:   return "Gamma Cross";
523         case SEQ_ADD:        return "Add";
524         case SEQ_SUB:        return "Sub";
525         case SEQ_MUL:        return "Mul";
526         case SEQ_ALPHAOVER:  return "Alpha Over";
527         case SEQ_ALPHAUNDER: return "Alpha Under";
528         case SEQ_OVERDROP:   return "Over Drop";
529         case SEQ_WIPE:       return "Wipe";
530         case SEQ_GLOW:       return "Glow";
531         case SEQ_TRANSFORM:  return "Transform";
532         case SEQ_COLOR:      return "Color";
533         case SEQ_SPEED:      return "Speed";
534         default:
535                 return 0;
536         }
537 }
538
539 char *give_seqname(Sequence *seq)
540 {
541         char * name = give_seqname_by_type(seq->type);
542
543         if (!name) {
544                 if(seq->type<SEQ_EFFECT) {
545                         return seq->strip->dir;
546                 } else if(seq->type==SEQ_PLUGIN) {
547                         if(!(seq->flag & SEQ_EFFECT_NOT_LOADED) &&
548                            seq->plugin && seq->plugin->doit) {
549                                 return seq->plugin->pname;
550                         } else {
551                                 return "Plugin";
552                         }
553                 } else {
554                         return "Effect";
555                 }
556         }
557         return name;
558 }
559
560 /* ***************** DO THE SEQUENCE ***************** */
561
562 static void make_black_ibuf(ImBuf *ibuf)
563 {
564         unsigned int *rect;
565         float *rect_float;
566         int tot;
567
568         if(ibuf==0 || (ibuf->rect==0 && ibuf->rect_float==0)) return;
569
570         tot= ibuf->x*ibuf->y;
571
572         rect= ibuf->rect;
573         rect_float = ibuf->rect_float;
574
575         if (rect) {
576                 memset(rect,       0, tot * sizeof(char) * 4);
577         }
578
579         if (rect_float) {
580                 memset(rect_float, 0, tot * sizeof(float) * 4);
581         }
582 }
583
584 static void multibuf(ImBuf *ibuf, float fmul)
585 {
586         char *rt;
587         float *rt_float;
588
589         int a, mul, icol;
590
591         mul= (int)(256.0*fmul);
592
593         a= ibuf->x*ibuf->y;
594         rt= (char *)ibuf->rect;
595         rt_float = ibuf->rect_float;
596
597         if (rt) {
598                 while(a--) {
599
600                         icol= (mul*rt[0])>>8;
601                         if(icol>254) rt[0]= 255; else rt[0]= icol;
602                         icol= (mul*rt[1])>>8;
603                         if(icol>254) rt[1]= 255; else rt[1]= icol;
604                         icol= (mul*rt[2])>>8;
605                         if(icol>254) rt[2]= 255; else rt[2]= icol;
606                         icol= (mul*rt[3])>>8;
607                         if(icol>254) rt[3]= 255; else rt[3]= icol;
608                         
609                         rt+= 4;
610                 }
611         }
612         if (rt_float) {
613                 while(a--) {
614                         rt_float[0] *= fmul;
615                         rt_float[1] *= fmul;
616                         rt_float[2] *= fmul;
617                         rt_float[3] *= fmul;
618                         
619                         rt_float += 4;
620                 }
621         }
622 }
623
624 static void do_effect(int cfra, Sequence *seq, TStripElem * se)
625 {
626         TStripElem *se1, *se2, *se3;
627         float fac, facf;
628         int x, y;
629         int early_out;
630         struct SeqEffectHandle sh = get_sequence_effect(seq);
631
632         if (!sh.execute) { /* effect not supported in this version... */
633                 make_black_ibuf(se->ibuf);
634                 return;
635         }
636
637         if(seq->ipo && seq->ipo->curve.first) {
638                 do_seq_ipo(seq, cfra);
639                 fac= seq->facf0;
640                 facf= seq->facf1;
641         } else {
642                 sh.get_default_fac(seq, cfra, &fac, &facf);
643         }
644
645         if( !(G.scene->r.mode & R_FIELDS) ) facf = fac;
646
647         early_out = sh.early_out(seq, fac, facf);
648
649         if (early_out == -1) { /* no input needed */
650                 sh.execute(seq, cfra, fac, facf, 
651                            se->ibuf->x, se->ibuf->y, 
652                            0, 0, 0, se->ibuf);
653                 return;
654         }
655
656         switch (early_out) {
657         case 0:
658                 if (se->se1==0 || se->se2==0 || se->se3==0) {
659                         make_black_ibuf(se->ibuf);
660                         return;
661                 }
662
663                 if(se->se1->ok == STRIPELEM_META) se1= se->se1->se1;
664                 else se1= se->se1;
665                 
666                 if(se->se2->ok == STRIPELEM_META) se2= se->se2->se1;
667                 else se2= se->se2;
668                 
669                 if(se->se3->ok == STRIPELEM_META) se3= se->se3->se1;
670                 else se3= se->se3;
671
672                 if (   (se1==0 || se2==0 || se3==0)
673                     || (se1->ibuf==0 || se2->ibuf==0 || se3->ibuf==0)) {
674                         make_black_ibuf(se->ibuf);
675                         return;
676                 }
677
678                 break;
679         case 1:
680                 if (se->se1 == 0) {
681                         make_black_ibuf(se->ibuf);
682                         return;
683                 }
684
685                 if(se->se1->ok == STRIPELEM_META) se1= se->se1->se1;
686                 else se1= se->se1;
687
688                 if (se1 == 0 || se1->ibuf == 0) {
689                         make_black_ibuf(se->ibuf);
690                         return;
691                 }
692
693                 if (se->ibuf != se1->ibuf) {
694                         IMB_freeImBuf(se->ibuf);
695                         se->ibuf = se1->ibuf;
696                         IMB_refImBuf(se->ibuf);
697                 }
698                 return;
699         case 2:
700                 if (se->se2 == 0) {
701                         make_black_ibuf(se->ibuf);
702                         return;
703                 }
704
705                 if(se->se2->ok == STRIPELEM_META) se2= se->se2->se1;
706                 else se2= se->se2;
707
708                 if (se2 == 0 || se2->ibuf == 0) {
709                         make_black_ibuf(se->ibuf);
710                         return;
711                 }
712                 if (se->ibuf != se2->ibuf) {
713                         IMB_freeImBuf(se->ibuf);
714                         se->ibuf = se2->ibuf;
715                         IMB_refImBuf(se->ibuf);
716                 }
717                 return;
718         default:
719                 make_black_ibuf(se->ibuf);
720                 return;
721         }
722
723         x= se2->ibuf->x;
724         y= se2->ibuf->y;
725
726         if (!se1->ibuf->rect_float && se->ibuf->rect_float) {
727                 IMB_float_from_rect(se1->ibuf);
728         }
729         if (!se2->ibuf->rect_float && se->ibuf->rect_float) {
730                 IMB_float_from_rect(se2->ibuf);
731         }
732
733         if (!se1->ibuf->rect && !se->ibuf->rect_float) {
734                 IMB_rect_from_float(se1->ibuf);
735         }
736         if (!se2->ibuf->rect && !se->ibuf->rect_float) {
737                 IMB_rect_from_float(se2->ibuf);
738         }
739
740         sh.execute(seq, cfra, fac, facf, x, y, se1->ibuf, se2->ibuf, se3->ibuf,
741                    se->ibuf);
742 }
743
744 static int give_stripelem_index(Sequence *seq, int cfra)
745 {
746         int nr;
747
748         if(seq->startdisp >cfra || seq->enddisp <= cfra) return -1;
749
750         if(seq->flag&SEQ_REVERSE_FRAMES)        {       
751                 /*reverse frame in this sequence */
752                 if(cfra <= seq->start) nr= seq->len-1;
753                 else if(cfra >= seq->start+seq->len-1) nr= 0;
754                 else nr= (seq->start + seq->len) - cfra;
755         } else {
756                 if(cfra <= seq->start) nr= 0;
757                 else if(cfra >= seq->start+seq->len-1) nr= seq->len-1;
758                 else nr= cfra-seq->start;
759         }
760         if (seq->strobe < 1.0) seq->strobe = 1.0;
761         if (seq->strobe > 1.0) {
762                 nr -= (int)fmod((double)nr, (double)seq->strobe);
763         }
764
765         return nr;
766 }
767
768 TStripElem *give_tstripelem(Sequence *seq, int cfra)
769 {
770         TStripElem *se;
771         int nr;
772
773         se = seq->strip->tstripdata;
774         if (se == 0 && seq->len > 0) {
775                 int i;
776                 se = seq->strip->tstripdata = MEM_callocN(
777                         seq->len*sizeof(TStripElem), "tstripelems");
778                 for (i = 0; i < seq->len; i++) {
779                         se[i].ok = STRIPELEM_OK;
780                 }
781         }
782         nr = give_stripelem_index(seq, cfra);
783
784         if (nr == -1) return 0;
785         if (se == 0) return 0;
786         
787         se+= nr; 
788         se->nr= nr;
789         
790         return se;
791 }
792
793 StripElem *give_stripelem(Sequence *seq, int cfra)
794 {
795         StripElem *se;
796         int nr;
797
798         se = seq->strip->stripdata;
799         nr = give_stripelem_index(seq, cfra);
800
801         if (nr == -1) return 0;
802         if (se == 0) return 0;
803
804         se += nr + seq->anim_startofs; 
805         
806         return se;
807 }
808
809 static int evaluate_seq_frame_gen(
810         Sequence ** seq_arr, ListBase *seqbase, int cfra)
811 {
812         Sequence *seq;
813         int totseq=0;
814
815         memset(seq_arr, 0, sizeof(Sequence*) * (MAXSEQ+1));
816
817         seq= seqbase->first;
818         while(seq) {
819                 if(seq->startdisp <=cfra && seq->enddisp > cfra) {
820                         seq_arr[seq->machine]= seq;
821                         totseq++;
822                 }
823                 seq= seq->next;
824         }
825
826         return totseq;
827 }
828
829 int evaluate_seq_frame(int cfra)
830 {
831        Editing *ed;
832        Sequence *seq_arr[MAXSEQ+1];
833
834        ed= G.scene->ed;
835        if(ed==0) return 0;
836         
837        return evaluate_seq_frame_gen(seq_arr, ed->seqbasep, cfra);
838
839 }
840
841 static int video_seq_is_rendered(Sequence * seq)
842 {
843         return (seq 
844                 && !(seq->flag & SEQ_MUTE) 
845                 && seq->type != SEQ_RAM_SOUND 
846                 && seq->type != SEQ_HD_SOUND);
847 }
848
849 static int get_shown_sequences(
850         ListBase * seqbasep, int cfra, int chanshown, Sequence ** seq_arr_out)
851 {
852         Sequence *seq_arr[MAXSEQ+1];
853         int b = chanshown;
854         int cnt = 0;
855
856         if (b > MAXSEQ) {
857                 return 0;
858         }
859
860         if(evaluate_seq_frame_gen(seq_arr, seqbasep, cfra)) {
861                 if (b > 0) {
862                         if (seq_arr[b] == 0) {
863                                 return 0;
864                         }
865                 } else {
866                         for (b = MAXSEQ; b > 0; b--) {
867                                 if (video_seq_is_rendered(seq_arr[b])) {
868                                         break;
869                                 }
870                         }
871                 }
872         }
873         
874         chanshown = b;
875
876         for (;b > 0; b--) {
877                 if (video_seq_is_rendered(seq_arr[b])) {
878                         if (seq_arr[b]->blend_mode == SEQ_BLEND_REPLACE) {
879                                 break;
880                         }
881                 }
882         }
883
884         for (;b <= chanshown; b++) {
885                 if (video_seq_is_rendered(seq_arr[b])) {
886                         seq_arr_out[cnt++] = seq_arr[b];
887                 }
888         }
889
890         return cnt;
891 }
892  
893 static int get_shown_seq_from_metastrip(Sequence * seqm, int cfra,
894                                         Sequence ** seq_arr_out)
895 {
896         return get_shown_sequences(&seqm->seqbase, cfra, 0, seq_arr_out);
897 }
898
899 void set_meta_stripdata(Sequence *seqm)
900 {
901         TStripElem *se;
902         int a, cfra;
903
904         se= seqm->strip->tstripdata;
905
906         if (se == 0 && seqm->len > 0) {
907                 int i;
908                 se = seqm->strip->tstripdata = MEM_callocN(
909                         seqm->len*sizeof(TStripElem), "tstripelems");
910                 for (i = 0; i < seqm->len; i++) {
911                         se[i].ok = STRIPELEM_META;
912                 }
913         }
914
915         /* sets all ->se1 pointers in stripdata, to read the ibuf from it */
916
917         for(a=0; a<seqm->len; a++, se++) {
918                 int cnt;
919                 Sequence *seq_arr[MAXSEQ+1];
920
921                 cfra= a+seqm->start;
922                 cnt = get_shown_seq_from_metastrip(seqm, cfra, seq_arr);
923                 if (cnt) {
924                         se->se1= give_tstripelem(seq_arr[cnt-1], cfra);
925                 } else { 
926                         se->se1= 0;
927                 }
928         }
929 }
930
931 /*
932   input preprocessing for SEQ_IMAGE, SEQ_MOVIE and SEQ_SCENE
933
934   Do all the things you can't really do afterwards using sequence effects
935   (read: before rescaling to render resolution has been done)
936
937   Order is important!
938
939   - Deinterlace
940   - Crop and transform in image source coordinate space
941   - Flip X + Flip Y (could be done afterwards, backward compatibility)
942   - Promote image to float data (affects pipeline operations afterwards)
943   - Premultiply
944
945 */
946
947 static void input_preprocess(Sequence * seq, TStripElem* se, int cfra)
948 {
949         float mul;
950
951         seq->strip->orx= se->ibuf->x;
952         seq->strip->ory= se->ibuf->y;
953
954         if(seq->flag & SEQ_FILTERY) {
955                 IMB_filtery(se->ibuf);
956         }
957
958         if(seq->flag & SEQ_USE_CROP || seq->flag & SEQ_USE_TRANSFORM) {
959                 StripCrop c;
960                 StripTransform t;
961
962                 memset(&c, 0, sizeof(StripCrop));
963                 memset(&t, 0, sizeof(StripTransform));
964
965                 if(seq->flag & SEQ_USE_CROP && seq->strip->crop) {
966                         c = *seq->strip->crop;
967                 }
968                 if(seq->flag & SEQ_USE_TRANSFORM && seq->strip->transform) {
969                         t = *seq->strip->transform;
970                 }
971
972                 if (c.top + c.bottom >= se->ibuf->y ||
973                     c.left + c.right >= se->ibuf->x ||
974                     t.xofs >= se->ibuf->x ||
975                     t.yofs >= se->ibuf->y) {
976                         make_black_ibuf(se->ibuf);
977                 } else {
978                         ImBuf * i;
979                         int sx = se->ibuf->x - c.left - c.right;
980                         int sy = se->ibuf->y - c.top - c.bottom;
981                         int dx = sx;
982                         int dy = sy;
983
984                         if (seq->flag & SEQ_USE_TRANSFORM) {
985                                 dx = seqrectx;
986                                 dy = seqrecty;
987                         }
988
989                         if (se->ibuf->rect_float) {
990                                 i = IMB_allocImBuf(dx, dy,32, IB_rectfloat, 0);
991                         } else {
992                                 i = IMB_allocImBuf(dx, dy,32, IB_rect, 0);
993                         }
994
995                         IMB_rectcpy(i, se->ibuf, 
996                                     t.xofs, t.yofs, 
997                                     c.left, c.bottom, 
998                                     sx, sy);
999
1000                         IMB_freeImBuf(se->ibuf);
1001
1002                         se->ibuf = i;
1003                 }
1004         } 
1005
1006         if(seq->flag & SEQ_FLIPX) {
1007                 IMB_flipx(se->ibuf);
1008         }
1009         if(seq->flag & SEQ_FLIPY) {
1010                 IMB_flipy(se->ibuf);
1011         }
1012
1013         if(seq->flag & SEQ_MAKE_FLOAT) {
1014                 if (!se->ibuf->rect_float) {
1015                         IMB_float_from_rect(se->ibuf);
1016                 }
1017         }
1018
1019         if(seq->mul == 0.0) {
1020                 seq->mul = 1.0;
1021         }
1022
1023         mul = seq->mul;
1024
1025         if(seq->blend_mode == SEQ_BLEND_REPLACE 
1026            && seq->ipo && seq->ipo->curve.first) {
1027                 do_seq_ipo(seq, cfra);
1028                 mul *= seq->facf0;
1029         }
1030         if(mul != 1.0) {
1031                 multibuf(se->ibuf, mul);
1032         }
1033
1034         if(seq->flag & SEQ_MAKE_PREMUL) {
1035                 if(se->ibuf->depth == 32 && se->ibuf->zbuf == 0) {
1036                         converttopremul(se->ibuf);
1037                 }
1038         }
1039
1040
1041         if(se->ibuf->x != seqrectx || se->ibuf->y != seqrecty ) {
1042                 if(G.scene->r.mode & R_OSA) {
1043                         IMB_scaleImBuf(se->ibuf, 
1044                                        (short)seqrectx, (short)seqrecty);
1045                 } else {
1046                         IMB_scalefastImBuf(se->ibuf, 
1047                                            (short)seqrectx, (short)seqrecty);
1048                 }
1049         }
1050 }
1051
1052 /* test if image too small or discarded from cache: reload */
1053
1054 static void test_and_auto_discard_ibuf(TStripElem * se)
1055 {
1056         if (se->ibuf) {
1057                 if(se->ibuf->x != seqrectx || se->ibuf->y != seqrecty 
1058                    || !(se->ibuf->rect || se->ibuf->rect_float)) {
1059                         IMB_freeImBuf(se->ibuf);
1060
1061                         se->ibuf= 0;
1062                         se->ok= STRIPELEM_OK;
1063                 }
1064         }
1065         if (se->ibuf_comp) {
1066                 if(se->ibuf_comp->x != seqrectx || se->ibuf_comp->y != seqrecty 
1067                    || !(se->ibuf_comp->rect || se->ibuf_comp->rect_float)) {
1068                         IMB_freeImBuf(se->ibuf_comp);
1069
1070                         se->ibuf_comp = 0;
1071                 }
1072         }
1073 }
1074
1075 static TStripElem* do_build_seq_array_recursively(
1076         ListBase *seqbasep, int cfra, int chanshown);
1077
1078 static void do_build_seq_ibuf(Sequence * seq, TStripElem *se, int cfra)
1079 {
1080         char name[FILE_MAXDIR+FILE_MAXFILE];
1081
1082
1083         if (seq->type != SEQ_META) {
1084                 test_and_auto_discard_ibuf(se);
1085         }
1086
1087         if(seq->type == SEQ_META) {
1088                 if(seq->seqbase.first) {
1089                         if(cfra < seq->start) {
1090                                 do_build_seq_array_recursively(
1091                                         &seq->seqbase, 
1092                                         seq->start, 0);
1093                         } else if(cfra > seq->start + seq->len - 1) {
1094                                 do_build_seq_array_recursively(
1095                                         &seq->seqbase, 
1096                                         seq->start + seq->len - 1, 0);
1097                         } else {
1098                                 do_build_seq_array_recursively(
1099                                         &seq->seqbase, 
1100                                         cfra, 0);
1101                         }
1102                 }
1103
1104                 se->ok = STRIPELEM_META;
1105                 if(se->se1 == 0) set_meta_stripdata(seq);
1106                 if(se->se1) {
1107                         if(se->ibuf) {
1108                                 IMB_freeImBuf(se->ibuf);
1109                         }
1110                         se->ibuf = se->se1->ibuf_comp;
1111                         if(se->ibuf) {
1112                                 IMB_refImBuf(se->ibuf);
1113                         }
1114                 }
1115         } else if(seq->type & SEQ_EFFECT) {
1116                 /* should the effect be recalculated? */
1117                 
1118                 if(se->ibuf == 0) {
1119                         /* if one of two first inputs are rectfloat, output is float too */
1120                         if((se->se1 && se->se1->ibuf && se->se1->ibuf->rect_float) ||
1121                            (se->se2 && se->se2->ibuf && se->se2->ibuf->rect_float))
1122                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
1123                         else
1124                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
1125                         
1126                         do_effect(cfra, seq, se);
1127                 }
1128
1129         } else if(seq->type < SEQ_EFFECT) {
1130                 
1131                 if(seq->type==SEQ_IMAGE) {
1132                         if(se->ok == STRIPELEM_OK && se->ibuf==0) {
1133                                 StripElem * s_elem = give_stripelem(seq, cfra);
1134
1135                                 strncpy(name, seq->strip->dir, FILE_MAXDIR-1);
1136                                 strncat(name, s_elem->name, FILE_MAXFILE);
1137                                 BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
1138                                 se->ibuf= IMB_loadiffname(name, IB_rect);
1139                                 
1140                                 if(se->ibuf == 0) {
1141                                         se->ok = STRIPELEM_FAILED;
1142                                 } else {
1143                                         input_preprocess(seq, se, cfra);
1144                                 }
1145                         }
1146                 }
1147                 else if(seq->type==SEQ_MOVIE) {
1148                         if(se->ok == STRIPELEM_OK && se->ibuf==0) {
1149                                 if(seq->anim==0) {
1150                                         strncpy(name, seq->strip->dir, FILE_MAXDIR-1);
1151                                         strncat(name, seq->strip->stripdata->name, FILE_MAXFILE-1);
1152                                         BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
1153                                         
1154                                         seq->anim = openanim(name, IB_rect);
1155                                 }
1156                                 if(seq->anim) {
1157                                         IMB_anim_set_preseek(seq->anim, seq->anim_preseek);
1158                                         se->ibuf = IMB_anim_absolute(seq->anim, se->nr + seq->anim_startofs);
1159                                 }
1160                                 
1161                                 if(se->ibuf == 0) {
1162                                         se->ok = STRIPELEM_FAILED;
1163                                 } else {
1164                                         input_preprocess(seq, se, cfra);
1165                                 }
1166                         }
1167                 } else if(seq->type==SEQ_SCENE && se->ibuf==NULL && seq->scene) {       // scene can be NULL after deletions
1168                         int oldcfra = CFRA;
1169                         Scene *sce= seq->scene, *oldsce= G.scene;
1170                         Render *re;
1171                         RenderResult rres;
1172                         int doseq, rendering= G.rendering;
1173                         char scenename[64];
1174                         
1175                         waitcursor(1);
1176                         
1177                         /* Hack! This function can be called from do_render_seq(), in that case
1178                            the seq->scene can already have a Render initialized with same name, 
1179                            so we have to use a default name. (compositor uses G.scene name to
1180                            find render).
1181                            However, when called from within the UI (image preview in sequencer)
1182                            we do want to use scene Render, that way the render result is defined
1183                            for display in render/imagewindow */
1184                         if(rendering) {
1185                                 BLI_strncpy(scenename, sce->id.name+2, 64);
1186                                 strcpy(sce->id.name+2, " do_build_seq_ibuf");
1187                         }
1188                         re= RE_NewRender(sce->id.name);
1189                         
1190                         /* prevent eternal loop */
1191                         doseq= G.scene->r.scemode & R_DOSEQ;
1192                         G.scene->r.scemode &= ~R_DOSEQ;
1193                         
1194                         BIF_init_render_callbacks(re, 0);       /* 0= no display callbacks */
1195                         
1196                         /* hrms, set_scene still needed? work on that... */
1197                         if(sce!=oldsce) set_scene_bg(sce);
1198                         RE_BlenderFrame(re, sce,
1199                                         seq->sfra+se->nr+seq->anim_startofs);
1200                         if(sce!=oldsce) set_scene_bg(oldsce);
1201                         
1202                         /* UGLY WARNING, it is set to zero in  RE_BlenderFrame */
1203                         G.rendering= rendering;
1204                         if(rendering)
1205                                 BLI_strncpy(sce->id.name+2, scenename, 64);
1206                         
1207                         RE_GetResultImage(re, &rres);
1208                         
1209                         if(rres.rectf) {
1210                                 se->ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rectfloat, 0);
1211                                 memcpy(se->ibuf->rect_float, rres.rectf, 4*sizeof(float)*rres.rectx*rres.recty);
1212                                 if(rres.rectz) {
1213                                         addzbuffloatImBuf(se->ibuf);
1214                                         memcpy(se->ibuf->zbuf_float, rres.rectz, sizeof(float)*rres.rectx*rres.recty);
1215                                 }
1216                         } else if (rres.rect32) {
1217                                 se->ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rect, 0);
1218                                 memcpy(se->ibuf->rect, rres.rect32, 4*rres.rectx*rres.recty);
1219                         }
1220                         
1221                         BIF_end_render_callbacks();
1222                         
1223                         /* restore */
1224                         G.scene->r.scemode |= doseq;
1225                         
1226                         if((G.f & G_PLAYANIM)==0) /* bad, is set on do_render_seq */
1227                                 waitcursor(0);
1228                         CFRA = oldcfra;
1229
1230                         input_preprocess(seq, se, cfra);
1231                 }       
1232         }
1233         if (se->ibuf && seq->type != SEQ_META) {
1234                 IMB_cache_limiter_insert(se->ibuf);
1235                 IMB_cache_limiter_ref(se->ibuf);
1236                 IMB_cache_limiter_touch(se->ibuf);
1237         }
1238 }
1239
1240 static TStripElem* do_build_seq_recursively(Sequence * seq, int cfra);
1241
1242 static void do_effect_seq_recursively(Sequence * seq, TStripElem *se, int cfra)
1243 {
1244         float fac, facf;
1245         struct SeqEffectHandle sh = get_sequence_effect(seq);
1246         int early_out;
1247
1248         se->se1 = 0;
1249         se->se2 = 0;
1250         se->se3 = 0;
1251
1252         if(seq->ipo && seq->ipo->curve.first) {
1253                 do_seq_ipo(seq, cfra);
1254                 fac= seq->facf0;
1255                 facf= seq->facf1;
1256         } else {
1257                 sh.get_default_fac(seq, cfra, &fac, &facf);
1258         } 
1259
1260         if( G.scene->r.mode & R_FIELDS ); else facf= fac;
1261         
1262         early_out = sh.early_out(seq, fac, facf);
1263         switch (early_out) {
1264         case -1:
1265                 /* no input needed */
1266                 break;
1267         case 0:
1268                 se->se1 = do_build_seq_recursively(seq->seq1, cfra);
1269                 se->se2 = do_build_seq_recursively(seq->seq2, cfra);
1270                 if (seq->seq3) {
1271                         se->se3 = do_build_seq_recursively(seq->seq3, cfra);
1272                 }
1273                 break;
1274         case 1:
1275                 se->se1 = do_build_seq_recursively(seq->seq1, cfra);
1276                 break;
1277         case 2:
1278                 se->se2 = do_build_seq_recursively(seq->seq2, cfra);
1279                 break;
1280         }
1281
1282
1283         do_build_seq_ibuf(seq, se, cfra);
1284
1285         /* children are not needed anymore ... */
1286
1287         if (se->se1 && se->se1->ibuf) {
1288                 IMB_cache_limiter_unref(se->se1->ibuf);
1289         }
1290         if (se->se2 && se->se2->ibuf) {
1291                 IMB_cache_limiter_unref(se->se2->ibuf);
1292         }
1293         if (se->se3 && se->se3->ibuf) {
1294                 IMB_cache_limiter_unref(se->se3->ibuf);
1295         }
1296 }
1297
1298 static TStripElem* do_build_seq_recursively_impl(Sequence * seq, int cfra)
1299 {
1300         TStripElem *se;
1301
1302         se = give_tstripelem(seq, cfra);
1303
1304         if(se) {
1305                 if (seq->type & SEQ_EFFECT) {
1306                         do_effect_seq_recursively(seq, se, cfra);
1307                 } else {
1308                         do_build_seq_ibuf(seq, se, cfra);
1309                 }
1310         }
1311         return se;
1312 }
1313
1314 /* FIXME:
1315    
1316 If cfra was float throughout blender (especially in the render
1317 pipeline) one could even _render_ with subframe precision
1318 instead of faking using the blend code below...
1319
1320 */
1321
1322 static TStripElem* do_handle_speed_effect(Sequence * seq, int cfra)
1323 {
1324         SpeedControlVars * s = (SpeedControlVars *)seq->effectdata;
1325         int nr = cfra - seq->start;
1326         float f_cfra;
1327         int cfra_left;
1328         int cfra_right;
1329         TStripElem * se = 0;
1330         TStripElem * se1 = 0;
1331         TStripElem * se2 = 0;
1332         
1333         sequence_effect_speed_rebuild_map(seq, 0);
1334         
1335         f_cfra = seq->start + s->frameMap[nr];
1336         
1337         cfra_left = (int) floor(f_cfra);
1338         cfra_right = (int) ceil(f_cfra);
1339
1340         se = give_tstripelem(seq, cfra);
1341
1342         if (cfra_left == cfra_right || 
1343             (s->flags & SEQ_SPEED_BLEND) == 0) {
1344                 test_and_auto_discard_ibuf(se);
1345
1346                 if (se->ibuf == NULL) {
1347                         se1 = do_build_seq_recursively_impl(
1348                                 seq->seq1, cfra_left);
1349
1350                         if((se1 && se1->ibuf && se1->ibuf->rect_float))
1351                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
1352                         else
1353                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
1354
1355                         if (se1 == 0 || se1->ibuf == 0) {
1356                                 make_black_ibuf(se->ibuf);
1357                         } else {
1358                                 if (se->ibuf != se1->ibuf) {
1359                                         if (se->ibuf) {
1360                                                 IMB_freeImBuf(se->ibuf);
1361                                         }
1362
1363                                         se->ibuf = se1->ibuf;
1364                                         IMB_refImBuf(se->ibuf);
1365                                 }
1366                         }
1367                 }
1368         } else {
1369                 struct SeqEffectHandle sh;
1370
1371                 if(se->ibuf) {
1372                         if(se->ibuf->x < seqrectx || se->ibuf->y < seqrecty 
1373                            || !(se->ibuf->rect || se->ibuf->rect_float)) {
1374                                 IMB_freeImBuf(se->ibuf);
1375                                 se->ibuf= 0;
1376                         }
1377                 }
1378
1379                 if (se->ibuf == NULL) {
1380                         se1 = do_build_seq_recursively_impl(
1381                                 seq->seq1, cfra_left);
1382                         se2 = do_build_seq_recursively_impl(
1383                                 seq->seq1, cfra_right);
1384
1385                         if((se1 && se1->ibuf && se1->ibuf->rect_float))
1386                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
1387                         else
1388                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
1389                         
1390                         if (!se1 || !se2) {
1391                                 make_black_ibuf(se->ibuf);
1392                         } else {
1393                                 sh = get_sequence_effect(seq);
1394
1395                                 sh.execute(seq, cfra, 
1396                                            f_cfra - (float) cfra_left, 
1397                                            f_cfra - (float) cfra_left, 
1398                                            se->ibuf->x, se->ibuf->y, 
1399                                            se1->ibuf, se2->ibuf, 0, se->ibuf);
1400                         }
1401                 }
1402
1403         }
1404
1405         /* caller expects this to be referenced, so do it! */
1406         if (se->ibuf) {
1407                 IMB_cache_limiter_insert(se->ibuf);
1408                 IMB_cache_limiter_ref(se->ibuf);
1409                 IMB_cache_limiter_touch(se->ibuf);
1410         }
1411
1412         /* children are no longer needed */
1413         if (se1 && se1->ibuf)
1414                 IMB_cache_limiter_unref(se1->ibuf);
1415         if (se2 && se2->ibuf)
1416                 IMB_cache_limiter_unref(se2->ibuf);
1417
1418         return se;
1419 }
1420
1421 /* 
1422  * build all ibufs recursively
1423  * 
1424  * if successfull, the returned TStripElem contains the (referenced!) imbuf
1425  * that means: you _must_ call 
1426  *
1427  * IMB_cache_limiter_unref(rval);
1428  * 
1429  * if rval != 0
1430  * 
1431  */
1432
1433 static TStripElem* do_build_seq_recursively(Sequence * seq, int cfra)
1434 {
1435         if (seq->type == SEQ_SPEED) {
1436                 return do_handle_speed_effect(seq, cfra);
1437         } else {
1438                 return do_build_seq_recursively_impl(seq, cfra);
1439         }
1440 }
1441
1442 static TStripElem* do_build_seq_array_recursively(
1443         ListBase *seqbasep, int cfra, int chanshown)
1444 {
1445         Sequence* seq_arr[MAXSEQ+1];
1446         int count;
1447         int i;
1448         TStripElem* se = 0;
1449
1450         count = get_shown_sequences(seqbasep, cfra, chanshown, &seq_arr);
1451
1452         if (!count) {
1453                 return 0;
1454         }
1455
1456         se = give_tstripelem(seq_arr[count - 1], cfra);
1457
1458         test_and_auto_discard_ibuf(se);
1459
1460         if (se->ibuf_comp != 0) {
1461                 IMB_cache_limiter_insert(se->ibuf_comp);
1462                 IMB_cache_limiter_ref(se->ibuf_comp);
1463                 IMB_cache_limiter_touch(se->ibuf_comp);
1464                 return se;
1465         }
1466
1467         
1468         if(count == 1) {
1469                 se = do_build_seq_recursively(seq_arr[0], cfra);
1470                 if (se->ibuf) {
1471                         se->ibuf_comp = se->ibuf;
1472                         IMB_refImBuf(se->ibuf_comp);
1473                 }
1474                 return se;
1475         }
1476
1477
1478         for (i = count - 1; i >= 0; i--) {
1479                 int early_out;
1480                 Sequence * seq = seq_arr[i];
1481                 struct SeqEffectHandle sh;
1482
1483                 se = give_tstripelem(seq, cfra);
1484
1485                 test_and_auto_discard_ibuf(se);
1486
1487                 if (se->ibuf_comp != 0) {
1488                         break;
1489                 }
1490                 if (seq->blend_mode == SEQ_BLEND_REPLACE) {
1491                         do_build_seq_recursively(seq, cfra);
1492                         if (se->ibuf) {
1493                                 se->ibuf_comp = se->ibuf;
1494                                 IMB_refImBuf(se->ibuf);
1495                         } else {
1496                                 se->ibuf_comp = IMB_allocImBuf(
1497                                         (short)seqrectx, (short)seqrecty, 
1498                                         32, IB_rect, 0);
1499                         }
1500                         break;
1501                 }
1502
1503                 sh = get_sequence_blend(seq);
1504
1505                 seq->facf0 = seq->facf1 = 1.0;
1506
1507                 if(seq->ipo && seq->ipo->curve.first) {
1508                         do_seq_ipo(seq, cfra);
1509                 } 
1510
1511                 if( G.scene->r.mode & R_FIELDS ); else seq->facf0 = seq->facf1;
1512
1513                 seq->facf0 *= seq->blend_opacity / 100.0;
1514                 seq->facf1 *= seq->blend_opacity / 100.0;
1515
1516                 early_out = sh.early_out(seq, seq->facf0, seq->facf1);
1517
1518                 switch (early_out) {
1519                 case -1:
1520                 case 2:
1521                         do_build_seq_recursively(seq, cfra);
1522                         if (se->ibuf) {
1523                                 se->ibuf_comp = se->ibuf;
1524                                 IMB_refImBuf(se->ibuf_comp);
1525                         } else {
1526                                 se->ibuf_comp = IMB_allocImBuf(
1527                                         (short)seqrectx, (short)seqrecty, 
1528                                         32, IB_rect, 0);
1529                         }
1530                         break;
1531                 case 1:
1532                         if (i == 0) {
1533                                 se->ibuf_comp = IMB_allocImBuf(
1534                                         (short)seqrectx, (short)seqrecty, 
1535                                         32, IB_rect, 0);
1536                                 IMB_cache_limiter_insert(se->ibuf_comp);
1537                                 IMB_cache_limiter_ref(se->ibuf_comp);
1538                                 IMB_cache_limiter_touch(se->ibuf_comp);
1539                         }
1540                         break;
1541                 case 0:
1542                         do_build_seq_recursively(seq, cfra);
1543                         if (!se->ibuf) {
1544                                 se->ibuf = IMB_allocImBuf(
1545                                         (short)seqrectx, (short)seqrecty, 
1546                                         32, IB_rect, 0);
1547                         }
1548                         if (i == 0) {
1549                                 se->ibuf_comp = se->ibuf;
1550                                 IMB_refImBuf(se->ibuf_comp);
1551                         }
1552                         break;
1553                 }
1554         
1555                 if (se->ibuf_comp) {
1556                         break;
1557                 }
1558         }
1559
1560         i++;
1561
1562         for (; i < count; i++) {
1563                 Sequence * seq = seq_arr[i];
1564                 struct SeqEffectHandle sh = get_sequence_blend(seq);
1565                 TStripElem* se1 = give_tstripelem(seq_arr[i-1], cfra);
1566                 TStripElem* se2 = give_tstripelem(seq_arr[i], cfra);
1567         
1568                 int early_out = sh.early_out(seq, seq->facf0, seq->facf1);
1569                 switch (early_out) {
1570                 case 0: {
1571                         int x= se2->ibuf->x;
1572                         int y= se2->ibuf->y;
1573                         int swap_input = FALSE;
1574
1575                         if (se1->ibuf_comp->rect_float ||
1576                             se2->ibuf->rect_float) {
1577                                 se2->ibuf_comp = IMB_allocImBuf(
1578                                         (short)seqrectx, (short)seqrecty, 
1579                                         32, IB_rectfloat, 0);
1580                         } else {
1581                                 se2->ibuf_comp = IMB_allocImBuf(
1582                                         (short)seqrectx, (short)seqrecty, 
1583                                         32, IB_rect, 0);
1584                         }
1585
1586
1587                         if (!se1->ibuf_comp->rect_float && 
1588                             se2->ibuf_comp->rect_float) {
1589                                 IMB_float_from_rect(se1->ibuf_comp);
1590                         }
1591                         if (!se2->ibuf->rect_float && 
1592                             se2->ibuf_comp->rect_float) {
1593                                 IMB_float_from_rect(se2->ibuf);
1594                         }
1595
1596                         if (!se1->ibuf_comp->rect && 
1597                             !se2->ibuf_comp->rect_float) {
1598                                 IMB_rect_from_float(se1->ibuf_comp);
1599                         }
1600                         if (!se2->ibuf->rect && 
1601                             !se2->ibuf_comp->rect_float) {
1602                                 IMB_rect_from_float(se2->ibuf);
1603                         }
1604
1605                         /* bad hack, to fix crazy input ordering of 
1606                            those two effects */
1607
1608                         if (seq->blend_mode == SEQ_ALPHAOVER ||
1609                             seq->blend_mode == SEQ_ALPHAUNDER ||
1610                             seq->blend_mode == SEQ_OVERDROP) {
1611                                 swap_input = TRUE;
1612                         }
1613
1614                         if (swap_input) {
1615                                 sh.execute(seq, cfra, 
1616                                            seq->facf0, seq->facf1, x, y, 
1617                                            se2->ibuf, se1->ibuf_comp, 0,
1618                                            se2->ibuf_comp);
1619                         } else {
1620                                 sh.execute(seq, cfra, 
1621                                            seq->facf0, seq->facf1, x, y, 
1622                                            se1->ibuf_comp, se2->ibuf, 0,
1623                                            se2->ibuf_comp);
1624                         }
1625                         
1626                         IMB_cache_limiter_insert(se2->ibuf_comp);
1627                         IMB_cache_limiter_ref(se2->ibuf_comp);
1628                         IMB_cache_limiter_touch(se2->ibuf_comp);
1629
1630                         IMB_cache_limiter_unref(se1->ibuf_comp);
1631                         IMB_cache_limiter_unref(se2->ibuf);
1632
1633                         break;
1634                 }
1635                 case 1: {
1636                         se2->ibuf_comp = se1->ibuf;
1637                         IMB_refImBuf(se2->ibuf_comp);
1638
1639                         break;
1640                 }
1641                 }
1642                 se = se2;
1643         }
1644
1645         return se;
1646 }
1647
1648 /*
1649  * returned ImBuf is refed!
1650  * you have to unref after usage!
1651  */
1652
1653 static ImBuf *give_ibuf_seq_impl(int rectx, int recty, int cfra, int chanshown)
1654 {
1655         Editing *ed;
1656         int count;
1657         ListBase *seqbasep;
1658         TStripElem *se;
1659
1660         ed= G.scene->ed;
1661         if(ed==0) return 0;
1662
1663         count = BLI_countlist(&ed->metastack);
1664         if((chanshown < 0) && (count > 0)) {
1665                 count = MAX2(count + chanshown, 0);
1666                 seqbasep= ((MetaStack*)BLI_findlink(&ed->metastack, count))->oldbasep;
1667         } else {
1668                 seqbasep= ed->seqbasep;
1669         }
1670
1671         seqrectx= rectx;        /* bad bad global! */
1672         seqrecty= recty;
1673
1674         se = do_build_seq_array_recursively(seqbasep, cfra, chanshown);
1675
1676         if(!se) { 
1677                 return 0;
1678         }
1679
1680         return se->ibuf_comp;
1681 }
1682
1683 ImBuf *give_ibuf_seq_direct(int rectx, int recty, int cfra,
1684                             Sequence * seq)
1685 {
1686         TStripElem* se;
1687
1688         seqrectx= rectx;        /* bad bad global! */
1689         seqrecty= recty;
1690
1691         se = do_build_seq_recursively(seq, cfra);
1692
1693         if(!se) { 
1694                 return 0;
1695         }
1696
1697         if (se->ibuf) {
1698                 IMB_cache_limiter_unref(se->ibuf);
1699         }
1700
1701         return se->ibuf;
1702 }
1703
1704 ImBuf *give_ibuf_seq(int rectx, int recty, int cfra, int chanshown)
1705 {
1706         ImBuf* i = give_ibuf_seq_impl(rectx, recty, cfra, chanshown);
1707
1708         if (i) {
1709                 IMB_cache_limiter_unref(i);
1710         }
1711         return i;
1712 }
1713
1714 /* threading api */
1715
1716 static ListBase running_threads;
1717 static ListBase prefetch_wait;
1718 static ListBase prefetch_done;
1719
1720 static pthread_mutex_t queue_lock          = PTHREAD_MUTEX_INITIALIZER;
1721 static pthread_mutex_t wakeup_lock         = PTHREAD_MUTEX_INITIALIZER;
1722 static pthread_cond_t  wakeup_cond         = PTHREAD_COND_INITIALIZER;
1723
1724 static pthread_mutex_t prefetch_ready_lock = PTHREAD_MUTEX_INITIALIZER;
1725 static pthread_cond_t  prefetch_ready_cond = PTHREAD_COND_INITIALIZER;
1726
1727 static pthread_mutex_t frame_done_lock     = PTHREAD_MUTEX_INITIALIZER;
1728 static pthread_cond_t  frame_done_cond     = PTHREAD_COND_INITIALIZER;
1729
1730 static volatile int seq_thread_shutdown = FALSE;
1731 static volatile int seq_last_given_monoton_cfra = 0;
1732 static int monoton_cfra = 0;
1733
1734 typedef struct PrefetchThread {
1735         struct PrefetchThread *next, *prev;
1736         struct PrefetchQueueElem *current;
1737         pthread_t pthread;
1738         int running;
1739 } PrefetchThread;
1740
1741 typedef struct PrefetchQueueElem {
1742         struct PrefetchQueueElem *next, *prev;
1743         
1744         int rectx;
1745         int recty;
1746         int cfra;
1747         int chanshown;
1748
1749         int monoton_cfra;
1750
1751         struct ImBuf * ibuf;
1752 } PrefetchQueueElem;
1753
1754 static void * seq_prefetch_thread(void * This_)
1755 {
1756         PrefetchThread * This = This_;
1757
1758         while (!seq_thread_shutdown) {
1759                 PrefetchQueueElem * e;
1760                 int s_last;
1761
1762                 pthread_mutex_lock(&queue_lock);
1763                 e = prefetch_wait.first;
1764                 if (e) {
1765                         BLI_remlink(&prefetch_wait, e);
1766                 }
1767                 s_last = seq_last_given_monoton_cfra;
1768
1769                 This->current = e;
1770
1771                 pthread_mutex_unlock(&queue_lock);
1772
1773                 if (!e) {
1774                         pthread_mutex_lock(&prefetch_ready_lock);
1775
1776                         This->running = FALSE;
1777
1778                         pthread_cond_signal(&prefetch_ready_cond);
1779                         pthread_mutex_unlock(&prefetch_ready_lock);
1780
1781                         pthread_mutex_lock(&wakeup_lock);
1782                         if (!seq_thread_shutdown) {
1783                                 pthread_cond_wait(&wakeup_cond, &wakeup_lock);
1784                         }
1785                         pthread_mutex_unlock(&wakeup_lock);
1786                         continue;
1787                 }
1788
1789                 This->running = TRUE;
1790                 
1791                 if (e->cfra >= s_last) { 
1792                         e->ibuf = give_ibuf_seq_impl(
1793                                 e->rectx, e->recty, e->cfra, e->chanshown);
1794                 }
1795
1796                 pthread_mutex_lock(&queue_lock);
1797
1798                 BLI_addtail(&prefetch_done, e);
1799
1800                 for (e = prefetch_wait.first; e; e = e->next) {
1801                         if (s_last > e->monoton_cfra) {
1802                                 BLI_remlink(&prefetch_wait, e);
1803                                 MEM_freeN(e);
1804                         }
1805                 }
1806
1807                 for (e = prefetch_done.first; e; e = e->next) {
1808                         if (s_last > e->monoton_cfra) {
1809                                 if (e->ibuf) {
1810                                         IMB_cache_limiter_unref(e->ibuf);
1811                                 }
1812                                 BLI_remlink(&prefetch_done, e);
1813                                 MEM_freeN(e);
1814                         }
1815                 }
1816
1817                 pthread_mutex_unlock(&queue_lock);
1818
1819                 pthread_mutex_lock(&frame_done_lock);
1820                 pthread_cond_signal(&frame_done_cond);
1821                 pthread_mutex_unlock(&frame_done_lock);
1822         }
1823         return 0;
1824 }
1825
1826 void seq_start_threads()
1827 {
1828         int i;
1829
1830         running_threads.first = running_threads.last = NULL;
1831         prefetch_wait.first = prefetch_wait.last = NULL;
1832         prefetch_done.first = prefetch_done.last = NULL;
1833
1834         seq_thread_shutdown = FALSE;
1835         seq_last_given_monoton_cfra = monoton_cfra = 0;
1836
1837         /* since global structures are modified during the processing
1838            of one frame, only one render thread is currently possible... 
1839
1840            (but we code, in the hope, that we can remove this restriction
1841            soon...)
1842         */
1843
1844         fprintf(stderr, "SEQ-THREAD: seq_start_threads\n");
1845
1846         for (i = 0; i < 1; i++) {
1847                 PrefetchThread *t = MEM_callocN(sizeof(PrefetchThread), 
1848                                                 "prefetch_thread");
1849                 t->running = TRUE;
1850                 BLI_addtail(&running_threads, t);
1851
1852                 pthread_create(&t->pthread, NULL, seq_prefetch_thread, t);
1853         }
1854
1855         /* init malloc mutex */
1856         BLI_init_threads(0, 0, 0);
1857 }
1858
1859 void seq_stop_threads()
1860 {
1861         PrefetchThread *tslot;
1862         PrefetchQueueElem * e;
1863
1864         fprintf(stderr, "SEQ-THREAD: seq_stop_threads()\n");
1865
1866         if (seq_thread_shutdown) {
1867                 fprintf(stderr, "SEQ-THREAD: ... already stopped\n");
1868                 return;
1869         }
1870         
1871         pthread_mutex_lock(&wakeup_lock);
1872
1873         seq_thread_shutdown = TRUE;
1874
1875         pthread_cond_broadcast(&wakeup_cond);
1876         pthread_mutex_unlock(&wakeup_lock);
1877
1878         for(tslot = running_threads.first; tslot; tslot= tslot->next) {
1879                 pthread_join(tslot->pthread, NULL);
1880         }
1881
1882
1883         for (e = prefetch_wait.first; e; e = e->next) {
1884                 BLI_remlink(&prefetch_wait, e);
1885                 MEM_freeN(e);
1886         }
1887
1888         for (e = prefetch_done.first; e; e = e->next) {
1889                 if (e->ibuf) {
1890                         IMB_cache_limiter_unref(e->ibuf);
1891                 }
1892                 BLI_remlink(&prefetch_done, e);
1893                 MEM_freeN(e);
1894         }
1895
1896         BLI_freelistN(&running_threads);
1897
1898         /* deinit malloc mutex */
1899         BLI_end_threads(0);
1900 }
1901
1902 void give_ibuf_prefetch_request(int rectx, int recty, int cfra, int chanshown)
1903 {
1904         PrefetchQueueElem * e;
1905         if (seq_thread_shutdown) {
1906                 return;
1907         }
1908
1909         e = MEM_callocN(sizeof(PrefetchQueueElem), "prefetch_queue_elem");
1910         e->rectx = rectx;
1911         e->recty = recty;
1912         e->cfra = cfra;
1913         e->chanshown = chanshown;
1914         e->monoton_cfra = monoton_cfra++;
1915
1916         pthread_mutex_lock(&queue_lock);
1917         BLI_addtail(&prefetch_wait, e);
1918         pthread_mutex_unlock(&queue_lock);
1919         
1920         pthread_mutex_lock(&wakeup_lock);
1921         pthread_cond_signal(&wakeup_cond);
1922         pthread_mutex_unlock(&wakeup_lock);
1923 }
1924
1925 void seq_wait_for_prefetch_ready()
1926 {
1927         PrefetchThread *tslot;
1928
1929         if (seq_thread_shutdown) {
1930                 return;
1931         }
1932
1933         fprintf(stderr, "SEQ-THREAD: rendering prefetch frames...\n");
1934
1935         pthread_mutex_lock(&prefetch_ready_lock);
1936
1937         for(;;) {
1938                 for(tslot = running_threads.first; tslot; tslot= tslot->next) {
1939                         if (tslot->running) {
1940                                 break;
1941                         }
1942                 }
1943                 if (!tslot) {
1944                         break;
1945                 }
1946                 pthread_cond_wait(&prefetch_ready_cond, &prefetch_ready_lock);
1947         }
1948
1949         pthread_mutex_unlock(&prefetch_ready_lock);
1950
1951         fprintf(stderr, "SEQ-THREAD: prefetch done\n");
1952 }
1953
1954 ImBuf * give_ibuf_seq_threaded(int rectx, int recty, int cfra, int chanshown)
1955 {
1956         PrefetchQueueElem * e = 0;
1957         int found_something = FALSE;
1958
1959         if (seq_thread_shutdown) {
1960                 return give_ibuf_seq(rectx, recty, cfra, chanshown);
1961         }
1962
1963         while (!e) {
1964                 int success = FALSE;
1965                 pthread_mutex_lock(&queue_lock);
1966
1967                 for (e = prefetch_done.first; e; e = e->next) {
1968                         if (cfra == e->cfra &&
1969                             chanshown == e->chanshown &&
1970                             rectx == e->rectx && 
1971                             recty == e->recty) {
1972                                 success = TRUE;
1973                                 found_something = TRUE;
1974                                 break;
1975                         }
1976                 }
1977
1978                 if (!e) {
1979                         for (e = prefetch_wait.first; e; e = e->next) {
1980                                 if (cfra == e->cfra &&
1981                                     chanshown == e->chanshown &&
1982                                     rectx == e->rectx && 
1983                                     recty == e->recty) {
1984                                         found_something = TRUE;
1985                                         break;
1986                                 }
1987                         }
1988                 }
1989
1990                 if (!e) {
1991                         PrefetchThread *tslot;
1992
1993                         for(tslot = running_threads.first; 
1994                             tslot; tslot= tslot->next) {
1995                                 if (tslot->current &&
1996                                     cfra == tslot->current->cfra &&
1997                                     chanshown == tslot->current->chanshown &&
1998                                     rectx == tslot->current->rectx && 
1999                                     recty == tslot->current->recty) {
2000                                         found_something = TRUE;
2001                                         break;
2002                                 }
2003                         }
2004                 }
2005
2006                 /* e->ibuf is unrefed by render thread on next round. */
2007
2008                 if (e) {
2009                         seq_last_given_monoton_cfra = e->monoton_cfra;
2010                 }
2011
2012                 pthread_mutex_unlock(&queue_lock);
2013
2014                 if (!success) {
2015                         e = NULL;
2016
2017                         if (!found_something) {
2018                                 fprintf(stderr, 
2019                                         "SEQ-THREAD: Requested frame "
2020                                         "not in queue ???\n");
2021                                 break;
2022                         }
2023                         pthread_mutex_lock(&frame_done_lock);
2024                         pthread_cond_wait(&frame_done_cond, &frame_done_lock);
2025                         pthread_mutex_unlock(&frame_done_lock);
2026                 }
2027         }
2028         
2029         return e ? e->ibuf : 0;
2030 }
2031
2032 /* Functions to free imbuf and anim data on changes */
2033
2034 static void free_imbuf_strip_elem(TStripElem *se)
2035 {
2036         if(se->ibuf) {
2037                 IMB_freeImBuf(se->ibuf);
2038         }
2039         if(se->ibuf_comp) {
2040                 IMB_freeImBuf(se->ibuf_comp);
2041         }
2042         se->ibuf_comp = 0;
2043         se->ibuf= 0;
2044         se->ok= STRIPELEM_OK;
2045         se->se1= se->se2= se->se3= 0;
2046 }
2047
2048 static void free_anim_seq(Sequence *seq)
2049 {
2050         if(seq->anim) {
2051                 IMB_free_anim(seq->anim);
2052                 seq->anim = 0;
2053         }
2054 }
2055
2056 void free_imbuf_seq_except(int cfra)
2057 {
2058         Editing *ed= G.scene->ed;
2059         Sequence *seq;
2060         TStripElem *se;
2061         int a;
2062
2063         if(ed==0) return;
2064
2065         WHILE_SEQ(&ed->seqbase) {
2066                 if(seq->strip) {
2067                         TStripElem * curelem = give_tstripelem(seq, cfra);
2068
2069                         for(a=0, se= seq->strip->tstripdata; a<seq->len; a++, se++)
2070                                 if(se != curelem)
2071                                         free_imbuf_strip_elem(se);
2072
2073                         if(seq->type==SEQ_MOVIE)
2074                                 if(seq->startdisp > cfra || seq->enddisp < cfra)
2075                                         free_anim_seq(seq);
2076                 }
2077         }
2078         END_SEQ
2079 }
2080
2081 void free_imbuf_seq()
2082 {
2083         Editing *ed= G.scene->ed;
2084         Sequence *seq;
2085         TStripElem *se;
2086         int a;
2087
2088         if(ed==0) return;
2089
2090         WHILE_SEQ(&ed->seqbase) {
2091                 if(seq->strip) {
2092                         if (seq->strip->tstripdata) {
2093                                 for(a=0, se= seq->strip->tstripdata; a<seq->len; a++, se++)
2094                                         free_imbuf_strip_elem(se);
2095                         }
2096
2097                         if(seq->type==SEQ_MOVIE)
2098                                 free_anim_seq(seq);
2099                         if(seq->type==SEQ_SPEED) {
2100                                 sequence_effect_speed_rebuild_map(seq, 1);
2101                         }
2102                 }
2103         }
2104         END_SEQ
2105 }
2106
2107 void free_imbuf_seq_with_ipo(struct Ipo *ipo)
2108 {
2109         /* force update of all sequences with this ipo, on ipo changes */
2110         Editing *ed= G.scene->ed;
2111         Sequence *seq;
2112
2113         if(ed==0) return;
2114
2115         WHILE_SEQ(&ed->seqbase) {
2116                 if(seq->ipo == ipo) {
2117                         update_changed_seq_and_deps(seq, 0, 1);
2118                         if(seq->type == SEQ_SPEED) {
2119                                 sequence_effect_speed_rebuild_map(seq, 1);
2120                         }
2121                 }
2122         }
2123         END_SEQ
2124 }
2125
2126 static int update_changed_seq_recurs(Sequence *seq, Sequence *changed_seq, int len_change, int ibuf_change)
2127 {
2128         Sequence *subseq;
2129         int a, free_imbuf = 0;
2130         TStripElem *se;
2131
2132         /* recurs downwards to see if this seq depends on the changed seq */
2133
2134         if(seq == NULL)
2135                 return 0;
2136
2137         if(seq == changed_seq)
2138                 free_imbuf = 1;
2139         
2140         for(subseq=seq->seqbase.first; subseq; subseq=subseq->next)
2141                 if(update_changed_seq_recurs(subseq, changed_seq, len_change, ibuf_change))
2142                         free_imbuf = TRUE;
2143         
2144         if(seq->seq1)
2145                 if(update_changed_seq_recurs(seq->seq1, changed_seq, len_change, ibuf_change))
2146                         free_imbuf = TRUE;
2147         if(seq->seq2 && (seq->seq2 != seq->seq1))
2148                 if(update_changed_seq_recurs(seq->seq2, changed_seq, len_change, ibuf_change))
2149                         free_imbuf = TRUE;
2150         if(seq->seq3 && (seq->seq3 != seq->seq1) && (seq->seq3 != seq->seq2))
2151                 if(update_changed_seq_recurs(seq->seq3, changed_seq, len_change, ibuf_change))
2152                         free_imbuf = TRUE;
2153         
2154         if(free_imbuf) {
2155                 if(ibuf_change) {
2156                         se= seq->strip->tstripdata;
2157                         if (se) {
2158                                 for(a=0; a<seq->len; a++, se++)
2159                                         free_imbuf_strip_elem(se);
2160                         }
2161                 
2162                         if(seq->type == SEQ_MOVIE)
2163                                 free_anim_seq(seq);
2164                         if(seq->type == SEQ_SPEED) {
2165                                 sequence_effect_speed_rebuild_map(seq, 1);
2166                         }
2167                 }
2168
2169                 if(len_change)
2170                         calc_sequence(seq);
2171         }
2172         
2173         return free_imbuf;
2174 }
2175
2176 void update_changed_seq_and_deps(Sequence *changed_seq, int len_change, int ibuf_change)
2177 {
2178         Editing *ed= G.scene->ed;
2179         Sequence *seq;
2180
2181         if (!ed) return;
2182
2183         for (seq=ed->seqbase.first; seq; seq=seq->next)
2184                 update_changed_seq_recurs(seq, changed_seq, len_change, ibuf_change);
2185 }
2186
2187 /* bad levell call... */
2188 void do_render_seq(RenderResult *rr, int cfra)
2189 {
2190         ImBuf *ibuf;
2191
2192         G.f |= G_PLAYANIM;      /* waitcursor patch */
2193
2194         ibuf= give_ibuf_seq(rr->rectx, rr->recty, cfra, 0);
2195         
2196         if(ibuf) {
2197                 if(ibuf->rect_float) {
2198                         if (!rr->rectf)
2199                                 rr->rectf= MEM_mallocN(4*sizeof(float)*rr->rectx*rr->recty, "render_seq rectf");
2200                         
2201                         memcpy(rr->rectf, ibuf->rect_float, 4*sizeof(float)*rr->rectx*rr->recty);
2202                         
2203                         /* TSK! Since sequence render doesn't free the *rr render result, the old rect32
2204                            can hang around when sequence render has rendered a 32 bits one before */
2205                         if(rr->rect32) {
2206                                 MEM_freeN(rr->rect32);
2207                                 rr->rect32= NULL;
2208                         }
2209                 }
2210                 else if(ibuf->rect) {
2211                         if (!rr->rect32)
2212                                 rr->rect32= MEM_mallocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
2213
2214                         memcpy(rr->rect32, ibuf->rect, 4*rr->rectx*rr->recty);
2215
2216                         /* if (ibuf->zbuf) { */
2217                         /*      if (R.rectz) freeN(R.rectz); */
2218                         /*      R.rectz = BLI_dupallocN(ibuf->zbuf); */
2219                         /* } */
2220                 }
2221                 
2222                 /* Let the cache limitor take care of this (schlaile) */
2223                 /* While render let's keep all memory available for render 
2224                    (ton)
2225                    At least if free memory is tight...
2226                    This can make a big difference in encoding speed
2227                    (it is around 4 times(!) faster, if we do not waste time
2228                    on freeing _all_ buffers every time on long timelines...)
2229                    (schlaile)
2230                 */
2231                 {
2232                         extern int mem_in_use;
2233                         extern int mmap_in_use;
2234
2235                         int max = MEM_CacheLimiter_get_maximum();
2236                         if (max != 0 && mem_in_use + mmap_in_use > max) {
2237                                 fprintf(stderr, "mem_in_use = %d, max = %d\n",
2238                                         mem_in_use + mmap_in_use, max);
2239                                 fprintf(stderr, "Cleaning up, please wait...\n"
2240                                         "If this happens very often,\n"
2241                                         "consider "
2242                                         "raising the memcache limit in the "
2243                                         "user preferences.\n");
2244                                 free_imbuf_seq();
2245                         }
2246                 }
2247         }
2248         else {
2249                 /* render result is delivered empty in most cases, nevertheless we handle all cases */
2250                 if (rr->rectf)
2251                         memset(rr->rectf, 0, 4*sizeof(float)*rr->rectx*rr->recty);
2252                 else if (rr->rect32)
2253                         memset(rr->rect32, 0, 4*rr->rectx*rr->recty);
2254                 else
2255                         rr->rect32= MEM_callocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
2256         }
2257         
2258         G.f &= ~G_PLAYANIM;
2259
2260 }