== 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                         IMB_refImBuf(se->ibuf);
1112                 }
1113         } else if(seq->type & SEQ_EFFECT) {
1114                 /* should the effect be recalculated? */
1115                 
1116                 if(se->ibuf == 0) {
1117                         /* if one of two first inputs are rectfloat, output is float too */
1118                         if((se->se1 && se->se1->ibuf && se->se1->ibuf->rect_float) ||
1119                            (se->se2 && se->se2->ibuf && se->se2->ibuf->rect_float))
1120                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
1121                         else
1122                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
1123                         
1124                         do_effect(cfra, seq, se);
1125                 }
1126
1127         } else if(seq->type < SEQ_EFFECT) {
1128                 
1129                 if(seq->type==SEQ_IMAGE) {
1130                         if(se->ok == STRIPELEM_OK && se->ibuf==0) {
1131                                 StripElem * s_elem = give_stripelem(seq, cfra);
1132
1133                                 strncpy(name, seq->strip->dir, FILE_MAXDIR-1);
1134                                 strncat(name, s_elem->name, FILE_MAXFILE);
1135                                 BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
1136                                 se->ibuf= IMB_loadiffname(name, IB_rect);
1137                                 
1138                                 if(se->ibuf == 0) {
1139                                         se->ok = STRIPELEM_FAILED;
1140                                 } else {
1141                                         input_preprocess(seq, se, cfra);
1142                                 }
1143                         }
1144                 }
1145                 else if(seq->type==SEQ_MOVIE) {
1146                         if(se->ok == STRIPELEM_OK && se->ibuf==0) {
1147                                 if(seq->anim==0) {
1148                                         strncpy(name, seq->strip->dir, FILE_MAXDIR-1);
1149                                         strncat(name, seq->strip->stripdata->name, FILE_MAXFILE-1);
1150                                         BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
1151                                         
1152                                         seq->anim = openanim(name, IB_rect);
1153                                 }
1154                                 if(seq->anim) {
1155                                         IMB_anim_set_preseek(seq->anim, seq->anim_preseek);
1156                                         se->ibuf = IMB_anim_absolute(seq->anim, se->nr + seq->anim_startofs);
1157                                 }
1158                                 
1159                                 if(se->ibuf == 0) {
1160                                         se->ok = STRIPELEM_FAILED;
1161                                 } else {
1162                                         input_preprocess(seq, se, cfra);
1163                                 }
1164                         }
1165                 } else if(seq->type==SEQ_SCENE && se->ibuf==NULL && seq->scene) {       // scene can be NULL after deletions
1166                         int oldcfra = CFRA;
1167                         Scene *sce= seq->scene, *oldsce= G.scene;
1168                         Render *re;
1169                         RenderResult rres;
1170                         int doseq, rendering= G.rendering;
1171                         char scenename[64];
1172                         
1173                         waitcursor(1);
1174                         
1175                         /* Hack! This function can be called from do_render_seq(), in that case
1176                            the seq->scene can already have a Render initialized with same name, 
1177                            so we have to use a default name. (compositor uses G.scene name to
1178                            find render).
1179                            However, when called from within the UI (image preview in sequencer)
1180                            we do want to use scene Render, that way the render result is defined
1181                            for display in render/imagewindow */
1182                         if(rendering) {
1183                                 BLI_strncpy(scenename, sce->id.name+2, 64);
1184                                 strcpy(sce->id.name+2, " do_build_seq_ibuf");
1185                         }
1186                         re= RE_NewRender(sce->id.name);
1187                         
1188                         /* prevent eternal loop */
1189                         doseq= G.scene->r.scemode & R_DOSEQ;
1190                         G.scene->r.scemode &= ~R_DOSEQ;
1191                         
1192                         BIF_init_render_callbacks(re, 0);       /* 0= no display callbacks */
1193                         
1194                         /* hrms, set_scene still needed? work on that... */
1195                         if(sce!=oldsce) set_scene_bg(sce);
1196                         RE_BlenderFrame(re, sce,
1197                                         seq->sfra+se->nr+seq->anim_startofs);
1198                         if(sce!=oldsce) set_scene_bg(oldsce);
1199                         
1200                         /* UGLY WARNING, it is set to zero in  RE_BlenderFrame */
1201                         G.rendering= rendering;
1202                         if(rendering)
1203                                 BLI_strncpy(sce->id.name+2, scenename, 64);
1204                         
1205                         RE_GetResultImage(re, &rres);
1206                         
1207                         if(rres.rectf) {
1208                                 se->ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rectfloat, 0);
1209                                 memcpy(se->ibuf->rect_float, rres.rectf, 4*sizeof(float)*rres.rectx*rres.recty);
1210                                 if(rres.rectz) {
1211                                         addzbuffloatImBuf(se->ibuf);
1212                                         memcpy(se->ibuf->zbuf_float, rres.rectz, sizeof(float)*rres.rectx*rres.recty);
1213                                 }
1214                         } else if (rres.rect32) {
1215                                 se->ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rect, 0);
1216                                 memcpy(se->ibuf->rect, rres.rect32, 4*rres.rectx*rres.recty);
1217                         }
1218                         
1219                         BIF_end_render_callbacks();
1220                         
1221                         /* restore */
1222                         G.scene->r.scemode |= doseq;
1223                         
1224                         if((G.f & G_PLAYANIM)==0) /* bad, is set on do_render_seq */
1225                                 waitcursor(0);
1226                         CFRA = oldcfra;
1227
1228                         input_preprocess(seq, se, cfra);
1229                 }       
1230         }
1231         if (se->ibuf && seq->type != SEQ_META) {
1232                 IMB_cache_limiter_insert(se->ibuf);
1233                 IMB_cache_limiter_ref(se->ibuf);
1234                 IMB_cache_limiter_touch(se->ibuf);
1235         }
1236 }
1237
1238 static TStripElem* do_build_seq_recursively(Sequence * seq, int cfra);
1239
1240 static void do_effect_seq_recursively(Sequence * seq, TStripElem *se, int cfra)
1241 {
1242         float fac, facf;
1243         struct SeqEffectHandle sh = get_sequence_effect(seq);
1244         int early_out;
1245
1246         se->se1 = 0;
1247         se->se2 = 0;
1248         se->se3 = 0;
1249
1250         if(seq->ipo && seq->ipo->curve.first) {
1251                 do_seq_ipo(seq, cfra);
1252                 fac= seq->facf0;
1253                 facf= seq->facf1;
1254         } else {
1255                 sh.get_default_fac(seq, cfra, &fac, &facf);
1256         } 
1257
1258         if( G.scene->r.mode & R_FIELDS ); else facf= fac;
1259         
1260         early_out = sh.early_out(seq, fac, facf);
1261         switch (early_out) {
1262         case -1:
1263                 /* no input needed */
1264                 break;
1265         case 0:
1266                 se->se1 = do_build_seq_recursively(seq->seq1, cfra);
1267                 se->se2 = do_build_seq_recursively(seq->seq2, cfra);
1268                 if (seq->seq3) {
1269                         se->se3 = do_build_seq_recursively(seq->seq3, cfra);
1270                 }
1271                 break;
1272         case 1:
1273                 se->se1 = do_build_seq_recursively(seq->seq1, cfra);
1274                 break;
1275         case 2:
1276                 se->se2 = do_build_seq_recursively(seq->seq2, cfra);
1277                 break;
1278         }
1279
1280
1281         do_build_seq_ibuf(seq, se, cfra);
1282
1283         /* children are not needed anymore ... */
1284
1285         if (se->se1 && se->se1->ibuf) {
1286                 IMB_cache_limiter_unref(se->se1->ibuf);
1287         }
1288         if (se->se2 && se->se2->ibuf) {
1289                 IMB_cache_limiter_unref(se->se2->ibuf);
1290         }
1291         if (se->se3 && se->se3->ibuf) {
1292                 IMB_cache_limiter_unref(se->se3->ibuf);
1293         }
1294 }
1295
1296 static TStripElem* do_build_seq_recursively_impl(Sequence * seq, int cfra)
1297 {
1298         TStripElem *se;
1299
1300         se = give_tstripelem(seq, cfra);
1301
1302         if(se) {
1303                 if (seq->type & SEQ_EFFECT) {
1304                         do_effect_seq_recursively(seq, se, cfra);
1305                 } else {
1306                         do_build_seq_ibuf(seq, se, cfra);
1307                 }
1308         }
1309         return se;
1310 }
1311
1312 /* FIXME:
1313    
1314 If cfra was float throughout blender (especially in the render
1315 pipeline) one could even _render_ with subframe precision
1316 instead of faking using the blend code below...
1317
1318 */
1319
1320 static TStripElem* do_handle_speed_effect(Sequence * seq, int cfra)
1321 {
1322         SpeedControlVars * s = (SpeedControlVars *)seq->effectdata;
1323         int nr = cfra - seq->start;
1324         float f_cfra;
1325         int cfra_left;
1326         int cfra_right;
1327         TStripElem * se = 0;
1328         TStripElem * se1 = 0;
1329         TStripElem * se2 = 0;
1330         
1331         sequence_effect_speed_rebuild_map(seq, 0);
1332         
1333         f_cfra = seq->start + s->frameMap[nr];
1334         
1335         cfra_left = (int) floor(f_cfra);
1336         cfra_right = (int) ceil(f_cfra);
1337
1338         se = give_tstripelem(seq, cfra);
1339
1340         if (cfra_left == cfra_right || 
1341             (s->flags & SEQ_SPEED_BLEND) == 0) {
1342                 test_and_auto_discard_ibuf(se);
1343
1344                 if (se->ibuf == NULL) {
1345                         se1 = do_build_seq_recursively_impl(
1346                                 seq->seq1, cfra_left);
1347
1348                         if((se1 && se1->ibuf && se1->ibuf->rect_float))
1349                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
1350                         else
1351                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
1352
1353                         if (se1 == 0 || se1->ibuf == 0) {
1354                                 make_black_ibuf(se->ibuf);
1355                         } else {
1356                                 if (se->ibuf != se1->ibuf) {
1357                                         if (se->ibuf) {
1358                                                 IMB_freeImBuf(se->ibuf);
1359                                         }
1360
1361                                         se->ibuf = se1->ibuf;
1362                                         IMB_refImBuf(se->ibuf);
1363                                 }
1364                         }
1365                 }
1366         } else {
1367                 struct SeqEffectHandle sh;
1368
1369                 if(se->ibuf) {
1370                         if(se->ibuf->x < seqrectx || se->ibuf->y < seqrecty 
1371                            || !(se->ibuf->rect || se->ibuf->rect_float)) {
1372                                 IMB_freeImBuf(se->ibuf);
1373                                 se->ibuf= 0;
1374                         }
1375                 }
1376
1377                 if (se->ibuf == NULL) {
1378                         se1 = do_build_seq_recursively_impl(
1379                                 seq->seq1, cfra_left);
1380                         se2 = do_build_seq_recursively_impl(
1381                                 seq->seq1, cfra_right);
1382
1383                         if((se1 && se1->ibuf && se1->ibuf->rect_float))
1384                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
1385                         else
1386                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
1387                         
1388                         if (!se1 || !se2) {
1389                                 make_black_ibuf(se->ibuf);
1390                         } else {
1391                                 sh = get_sequence_effect(seq);
1392
1393                                 sh.execute(seq, cfra, 
1394                                            f_cfra - (float) cfra_left, 
1395                                            f_cfra - (float) cfra_left, 
1396                                            se->ibuf->x, se->ibuf->y, 
1397                                            se1->ibuf, se2->ibuf, 0, se->ibuf);
1398                         }
1399                 }
1400
1401         }
1402
1403         /* caller expects this to be referenced, so do it! */
1404         if (se->ibuf) {
1405                 IMB_cache_limiter_insert(se->ibuf);
1406                 IMB_cache_limiter_ref(se->ibuf);
1407                 IMB_cache_limiter_touch(se->ibuf);
1408         }
1409
1410         /* children are no longer needed */
1411         if (se1 && se1->ibuf)
1412                 IMB_cache_limiter_unref(se1->ibuf);
1413         if (se2 && se2->ibuf)
1414                 IMB_cache_limiter_unref(se2->ibuf);
1415
1416         return se;
1417 }
1418
1419 /* 
1420  * build all ibufs recursively
1421  * 
1422  * if successfull, the returned TStripElem contains the (referenced!) imbuf
1423  * that means: you _must_ call 
1424  *
1425  * IMB_cache_limiter_unref(rval);
1426  * 
1427  * if rval != 0
1428  * 
1429  */
1430
1431 static TStripElem* do_build_seq_recursively(Sequence * seq, int cfra)
1432 {
1433         if (seq->type == SEQ_SPEED) {
1434                 return do_handle_speed_effect(seq, cfra);
1435         } else {
1436                 return do_build_seq_recursively_impl(seq, cfra);
1437         }
1438 }
1439
1440 static TStripElem* do_build_seq_array_recursively(
1441         ListBase *seqbasep, int cfra, int chanshown)
1442 {
1443         Sequence* seq_arr[MAXSEQ+1];
1444         int count;
1445         int i;
1446         TStripElem* se = 0;
1447
1448         count = get_shown_sequences(seqbasep, cfra, chanshown, &seq_arr);
1449
1450         if (!count) {
1451                 return 0;
1452         }
1453
1454         se = give_tstripelem(seq_arr[count - 1], cfra);
1455
1456         test_and_auto_discard_ibuf(se);
1457
1458         if (se->ibuf_comp != 0) {
1459                 IMB_cache_limiter_insert(se->ibuf_comp);
1460                 IMB_cache_limiter_ref(se->ibuf_comp);
1461                 IMB_cache_limiter_touch(se->ibuf_comp);
1462                 return se;
1463         }
1464
1465         
1466         if(count == 1) {
1467                 se = do_build_seq_recursively(seq_arr[0], cfra);
1468                 if (se->ibuf) {
1469                         se->ibuf_comp = se->ibuf;
1470                         IMB_refImBuf(se->ibuf_comp);
1471                 }
1472                 return se;
1473         }
1474
1475
1476         for (i = count - 1; i >= 0; i--) {
1477                 int early_out;
1478                 Sequence * seq = seq_arr[i];
1479                 struct SeqEffectHandle sh;
1480
1481                 se = give_tstripelem(seq, cfra);
1482
1483                 test_and_auto_discard_ibuf(se);
1484
1485                 if (se->ibuf_comp != 0) {
1486                         break;
1487                 }
1488                 if (seq->blend_mode == SEQ_BLEND_REPLACE) {
1489                         do_build_seq_recursively(seq, cfra);
1490                         if (se->ibuf) {
1491                                 se->ibuf_comp = se->ibuf;
1492                                 IMB_refImBuf(se->ibuf);
1493                         } else {
1494                                 se->ibuf_comp = IMB_allocImBuf(
1495                                         (short)seqrectx, (short)seqrecty, 
1496                                         32, IB_rect, 0);
1497                         }
1498                         break;
1499                 }
1500
1501                 sh = get_sequence_blend(seq_arr[i]);
1502
1503                 seq->facf0 = seq->facf1 = 1.0;
1504
1505                 if(seq->ipo && seq->ipo->curve.first) {
1506                         do_seq_ipo(seq, cfra);
1507                 } 
1508
1509                 if( G.scene->r.mode & R_FIELDS ); else seq->facf0 = seq->facf1;
1510
1511                 seq->facf0 *= seq->blend_opacity / 100.0;
1512                 seq->facf1 *= seq->blend_opacity / 100.0;
1513
1514                 early_out = sh.early_out(seq, seq->facf0, seq->facf1);
1515
1516                 switch (early_out) {
1517                 case -1:
1518                 case 2:
1519                         do_build_seq_recursively(seq, cfra);
1520                         if (se->ibuf) {
1521                                 se->ibuf_comp = se->ibuf;
1522                                 IMB_refImBuf(se->ibuf_comp);
1523                         } else {
1524                                 se->ibuf_comp = IMB_allocImBuf(
1525                                         (short)seqrectx, (short)seqrecty, 
1526                                         32, IB_rect, 0);
1527                         }
1528                         break;
1529                 case 1:
1530                         if (i == 0) {
1531                                 se->ibuf_comp = IMB_allocImBuf(
1532                                         (short)seqrectx, (short)seqrecty, 
1533                                         32, IB_rect, 0);
1534                                 IMB_cache_limiter_insert(se->ibuf_comp);
1535                                 IMB_cache_limiter_ref(se->ibuf_comp);
1536                                 IMB_cache_limiter_touch(se->ibuf_comp);
1537                         }
1538                         break;
1539                 case 0:
1540                         do_build_seq_recursively(seq, cfra);
1541                         if (!se->ibuf) {
1542                                 se->ibuf = IMB_allocImBuf(
1543                                         (short)seqrectx, (short)seqrecty, 
1544                                         32, IB_rect, 0);
1545                         }
1546                         break;
1547                 }
1548                 
1549                 if (se->ibuf_comp) {
1550                         break;
1551                 }
1552         }
1553
1554         i++;
1555
1556         for (; i < count; i++) {
1557                 Sequence * seq = seq_arr[i];
1558                 struct SeqEffectHandle sh = get_sequence_blend(seq);
1559                 TStripElem* se1 = give_tstripelem(seq_arr[i-1], cfra);
1560                 TStripElem* se2 = give_tstripelem(seq_arr[i], cfra);
1561         
1562                 int early_out = sh.early_out(seq, seq->facf0, seq->facf1);
1563                 switch (early_out) {
1564                 case 0: {
1565                         int x= se2->ibuf->x;
1566                         int y= se2->ibuf->y;
1567
1568                         if (se1->ibuf_comp->rect_float ||
1569                             se2->ibuf->rect_float) {
1570                                 se2->ibuf_comp = IMB_allocImBuf(
1571                                         (short)seqrectx, (short)seqrecty, 
1572                                         32, IB_rectfloat, 0);
1573                         } else {
1574                                 se2->ibuf_comp = IMB_allocImBuf(
1575                                         (short)seqrectx, (short)seqrecty, 
1576                                         32, IB_rect, 0);
1577                         }
1578
1579
1580                         if (!se1->ibuf_comp->rect_float && 
1581                             se2->ibuf_comp->rect_float) {
1582                                 IMB_float_from_rect(se1->ibuf_comp);
1583                         }
1584                         if (!se2->ibuf->rect_float && 
1585                             se2->ibuf_comp->rect_float) {
1586                                 IMB_float_from_rect(se2->ibuf);
1587                         }
1588
1589                         if (!se1->ibuf_comp->rect && 
1590                             !se2->ibuf_comp->rect_float) {
1591                                 IMB_rect_from_float(se1->ibuf);
1592                         }
1593                         if (!se2->ibuf->rect && 
1594                             !se2->ibuf_comp->rect_float) {
1595                                 IMB_rect_from_float(se2->ibuf);
1596                         }
1597
1598                         sh.execute(seq, cfra, seq->facf0, seq->facf1, x, y, 
1599                                    se1->ibuf_comp, se2->ibuf, 0,
1600                                    se2->ibuf_comp);
1601                         
1602                         IMB_cache_limiter_insert(se2->ibuf_comp);
1603                         IMB_cache_limiter_ref(se2->ibuf_comp);
1604                         IMB_cache_limiter_touch(se2->ibuf_comp);
1605
1606                         IMB_cache_limiter_unref(se1->ibuf_comp);
1607                         IMB_cache_limiter_unref(se2->ibuf);
1608
1609                         break;
1610                 }
1611                 case 1: {
1612                         se2->ibuf_comp = se1->ibuf;
1613                         IMB_refImBuf(se2->ibuf_comp);
1614
1615                         break;
1616                 }
1617                 }
1618                 se = se2;
1619         }
1620
1621         return se;
1622 }
1623
1624 /*
1625  * returned ImBuf is refed!
1626  * you have to unref after usage!
1627  */
1628
1629 static ImBuf *give_ibuf_seq_impl(int rectx, int recty, int cfra, int chanshown)
1630 {
1631         Editing *ed;
1632         int count;
1633         ListBase *seqbasep;
1634         TStripElem *se;
1635
1636         ed= G.scene->ed;
1637         if(ed==0) return 0;
1638
1639         count = BLI_countlist(&ed->metastack);
1640         if((chanshown < 0) && (count > 0)) {
1641                 count = MAX2(count + chanshown, 0);
1642                 seqbasep= ((MetaStack*)BLI_findlink(&ed->metastack, count))->oldbasep;
1643         } else {
1644                 seqbasep= ed->seqbasep;
1645         }
1646
1647         seqrectx= rectx;        /* bad bad global! */
1648         seqrecty= recty;
1649
1650         se = do_build_seq_array_recursively(seqbasep, cfra, chanshown);
1651
1652         if(!se) { 
1653                 return 0;
1654         }
1655
1656         return se->ibuf_comp;
1657 }
1658
1659 ImBuf *give_ibuf_seq_direct(int rectx, int recty, int cfra,
1660                             Sequence * seq)
1661 {
1662         TStripElem* se;
1663
1664         seqrectx= rectx;        /* bad bad global! */
1665         seqrecty= recty;
1666
1667         se = do_build_seq_recursively(seq, cfra);
1668
1669         if(!se) { 
1670                 return 0;
1671         }
1672
1673         if (se->ibuf) {
1674                 IMB_cache_limiter_unref(se->ibuf);
1675         }
1676
1677         return se->ibuf;
1678 }
1679
1680 ImBuf *give_ibuf_seq(int rectx, int recty, int cfra, int chanshown)
1681 {
1682         ImBuf* i = give_ibuf_seq_impl(rectx, recty, cfra, chanshown);
1683
1684         if (i) {
1685                 IMB_cache_limiter_unref(i);
1686         }
1687         return i;
1688 }
1689
1690 /* threading api */
1691
1692 static ListBase running_threads;
1693 static ListBase prefetch_wait;
1694 static ListBase prefetch_done;
1695
1696 static pthread_mutex_t queue_lock          = PTHREAD_MUTEX_INITIALIZER;
1697 static pthread_mutex_t wakeup_lock         = PTHREAD_MUTEX_INITIALIZER;
1698 static pthread_cond_t  wakeup_cond         = PTHREAD_COND_INITIALIZER;
1699
1700 static pthread_mutex_t prefetch_ready_lock = PTHREAD_MUTEX_INITIALIZER;
1701 static pthread_cond_t  prefetch_ready_cond = PTHREAD_COND_INITIALIZER;
1702
1703 static pthread_mutex_t frame_done_lock     = PTHREAD_MUTEX_INITIALIZER;
1704 static pthread_cond_t  frame_done_cond     = PTHREAD_COND_INITIALIZER;
1705
1706 static volatile int seq_thread_shutdown = FALSE;
1707 static volatile int seq_last_given_monoton_cfra = 0;
1708 static int monoton_cfra = 0;
1709
1710 typedef struct PrefetchThread {
1711         struct PrefetchThread *next, *prev;
1712         struct PrefetchQueueElem *current;
1713         pthread_t pthread;
1714         int running;
1715 } PrefetchThread;
1716
1717 typedef struct PrefetchQueueElem {
1718         struct PrefetchQueueElem *next, *prev;
1719         
1720         int rectx;
1721         int recty;
1722         int cfra;
1723         int chanshown;
1724
1725         int monoton_cfra;
1726
1727         struct ImBuf * ibuf;
1728 } PrefetchQueueElem;
1729
1730 static void * seq_prefetch_thread(void * This_)
1731 {
1732         PrefetchThread * This = This_;
1733
1734         while (!seq_thread_shutdown) {
1735                 PrefetchQueueElem * e;
1736                 int s_last;
1737
1738                 pthread_mutex_lock(&queue_lock);
1739                 e = prefetch_wait.first;
1740                 if (e) {
1741                         BLI_remlink(&prefetch_wait, e);
1742                 }
1743                 s_last = seq_last_given_monoton_cfra;
1744
1745                 This->current = e;
1746
1747                 pthread_mutex_unlock(&queue_lock);
1748
1749                 if (!e) {
1750                         pthread_mutex_lock(&prefetch_ready_lock);
1751
1752                         This->running = FALSE;
1753
1754                         pthread_cond_signal(&prefetch_ready_cond);
1755                         pthread_mutex_unlock(&prefetch_ready_lock);
1756
1757                         pthread_mutex_lock(&wakeup_lock);
1758                         if (!seq_thread_shutdown) {
1759                                 pthread_cond_wait(&wakeup_cond, &wakeup_lock);
1760                         }
1761                         pthread_mutex_unlock(&wakeup_lock);
1762                         continue;
1763                 }
1764
1765                 This->running = TRUE;
1766                 
1767                 if (e->cfra >= s_last) { 
1768                         e->ibuf = give_ibuf_seq_impl(
1769                                 e->rectx, e->recty, e->cfra, e->chanshown);
1770                 }
1771
1772                 pthread_mutex_lock(&queue_lock);
1773
1774                 BLI_addtail(&prefetch_done, e);
1775
1776                 for (e = prefetch_wait.first; e; e = e->next) {
1777                         if (s_last > e->monoton_cfra) {
1778                                 BLI_remlink(&prefetch_wait, e);
1779                                 MEM_freeN(e);
1780                         }
1781                 }
1782
1783                 for (e = prefetch_done.first; e; e = e->next) {
1784                         if (s_last > e->monoton_cfra) {
1785                                 if (e->ibuf) {
1786                                         IMB_cache_limiter_unref(e->ibuf);
1787                                 }
1788                                 BLI_remlink(&prefetch_done, e);
1789                                 MEM_freeN(e);
1790                         }
1791                 }
1792
1793                 pthread_mutex_unlock(&queue_lock);
1794
1795                 pthread_mutex_lock(&frame_done_lock);
1796                 pthread_cond_signal(&frame_done_cond);
1797                 pthread_mutex_unlock(&frame_done_lock);
1798         }
1799         return 0;
1800 }
1801
1802 void seq_start_threads()
1803 {
1804         int i;
1805
1806         running_threads.first = running_threads.last = NULL;
1807         prefetch_wait.first = prefetch_wait.last = NULL;
1808         prefetch_done.first = prefetch_done.last = NULL;
1809
1810         seq_thread_shutdown = FALSE;
1811         seq_last_given_monoton_cfra = monoton_cfra = 0;
1812
1813         /* since global structures are modified during the processing
1814            of one frame, only one render thread is currently possible... 
1815
1816            (but we code, in the hope, that we can remove this restriction
1817            soon...)
1818         */
1819
1820         fprintf(stderr, "SEQ-THREAD: seq_start_threads\n");
1821
1822         for (i = 0; i < 1; i++) {
1823                 PrefetchThread *t = MEM_callocN(sizeof(PrefetchThread), 
1824                                                 "prefetch_thread");
1825                 t->running = TRUE;
1826                 BLI_addtail(&running_threads, t);
1827
1828                 pthread_create(&t->pthread, NULL, seq_prefetch_thread, t);
1829         }
1830
1831         /* init malloc mutex */
1832         BLI_init_threads(0, 0, 0);
1833 }
1834
1835 void seq_stop_threads()
1836 {
1837         PrefetchThread *tslot;
1838         PrefetchQueueElem * e;
1839
1840         fprintf(stderr, "SEQ-THREAD: seq_stop_threads()\n");
1841
1842         if (seq_thread_shutdown) {
1843                 fprintf(stderr, "SEQ-THREAD: ... already stopped\n");
1844                 return;
1845         }
1846         
1847         pthread_mutex_lock(&wakeup_lock);
1848
1849         seq_thread_shutdown = TRUE;
1850
1851         pthread_cond_broadcast(&wakeup_cond);
1852         pthread_mutex_unlock(&wakeup_lock);
1853
1854         for(tslot = running_threads.first; tslot; tslot= tslot->next) {
1855                 pthread_join(tslot->pthread, NULL);
1856         }
1857
1858
1859         for (e = prefetch_wait.first; e; e = e->next) {
1860                 BLI_remlink(&prefetch_wait, e);
1861                 MEM_freeN(e);
1862         }
1863
1864         for (e = prefetch_done.first; e; e = e->next) {
1865                 if (e->ibuf) {
1866                         IMB_cache_limiter_unref(e->ibuf);
1867                 }
1868                 BLI_remlink(&prefetch_done, e);
1869                 MEM_freeN(e);
1870         }
1871
1872         BLI_freelistN(&running_threads);
1873
1874         /* deinit malloc mutex */
1875         BLI_end_threads(0);
1876 }
1877
1878 void give_ibuf_prefetch_request(int rectx, int recty, int cfra, int chanshown)
1879 {
1880         PrefetchQueueElem * e;
1881         if (seq_thread_shutdown) {
1882                 return;
1883         }
1884
1885         e = MEM_callocN(sizeof(PrefetchQueueElem), "prefetch_queue_elem");
1886         e->rectx = rectx;
1887         e->recty = recty;
1888         e->cfra = cfra;
1889         e->chanshown = chanshown;
1890         e->monoton_cfra = monoton_cfra++;
1891
1892         pthread_mutex_lock(&queue_lock);
1893         BLI_addtail(&prefetch_wait, e);
1894         pthread_mutex_unlock(&queue_lock);
1895         
1896         pthread_mutex_lock(&wakeup_lock);
1897         pthread_cond_signal(&wakeup_cond);
1898         pthread_mutex_unlock(&wakeup_lock);
1899 }
1900
1901 void seq_wait_for_prefetch_ready()
1902 {
1903         PrefetchThread *tslot;
1904
1905         if (seq_thread_shutdown) {
1906                 return;
1907         }
1908
1909         fprintf(stderr, "SEQ-THREAD: rendering prefetch frames...\n");
1910
1911         pthread_mutex_lock(&prefetch_ready_lock);
1912
1913         for(;;) {
1914                 for(tslot = running_threads.first; tslot; tslot= tslot->next) {
1915                         if (tslot->running) {
1916                                 break;
1917                         }
1918                 }
1919                 if (!tslot) {
1920                         break;
1921                 }
1922                 pthread_cond_wait(&prefetch_ready_cond, &prefetch_ready_lock);
1923         }
1924
1925         pthread_mutex_unlock(&prefetch_ready_lock);
1926
1927         fprintf(stderr, "SEQ-THREAD: prefetch done\n");
1928 }
1929
1930 ImBuf * give_ibuf_seq_threaded(int rectx, int recty, int cfra, int chanshown)
1931 {
1932         PrefetchQueueElem * e = 0;
1933         int found_something = FALSE;
1934
1935         if (seq_thread_shutdown) {
1936                 return give_ibuf_seq(rectx, recty, cfra, chanshown);
1937         }
1938
1939         while (!e) {
1940                 int success = FALSE;
1941                 pthread_mutex_lock(&queue_lock);
1942
1943                 for (e = prefetch_done.first; e; e = e->next) {
1944                         if (cfra == e->cfra &&
1945                             chanshown == e->chanshown &&
1946                             rectx == e->rectx && 
1947                             recty == e->recty) {
1948                                 success = TRUE;
1949                                 found_something = TRUE;
1950                                 break;
1951                         }
1952                 }
1953
1954                 if (!e) {
1955                         for (e = prefetch_wait.first; e; e = e->next) {
1956                                 if (cfra == e->cfra &&
1957                                     chanshown == e->chanshown &&
1958                                     rectx == e->rectx && 
1959                                     recty == e->recty) {
1960                                         found_something = TRUE;
1961                                         break;
1962                                 }
1963                         }
1964                 }
1965
1966                 if (!e) {
1967                         PrefetchThread *tslot;
1968
1969                         for(tslot = running_threads.first; 
1970                             tslot; tslot= tslot->next) {
1971                                 if (tslot->current &&
1972                                     cfra == tslot->current->cfra &&
1973                                     chanshown == tslot->current->chanshown &&
1974                                     rectx == tslot->current->rectx && 
1975                                     recty == tslot->current->recty) {
1976                                         found_something = TRUE;
1977                                         break;
1978                                 }
1979                         }
1980                 }
1981
1982                 /* e->ibuf is unrefed by render thread on next round. */
1983
1984                 if (e) {
1985                         seq_last_given_monoton_cfra = e->monoton_cfra;
1986                 }
1987
1988                 pthread_mutex_unlock(&queue_lock);
1989
1990                 if (!success) {
1991                         e = NULL;
1992
1993                         if (!found_something) {
1994                                 fprintf(stderr, 
1995                                         "SEQ-THREAD: Requested frame "
1996                                         "not in queue ???\n");
1997                                 break;
1998                         }
1999                         pthread_mutex_lock(&frame_done_lock);
2000                         pthread_cond_wait(&frame_done_cond, &frame_done_lock);
2001                         pthread_mutex_unlock(&frame_done_lock);
2002                 }
2003         }
2004         
2005         return e ? e->ibuf : 0;
2006 }
2007
2008 /* Functions to free imbuf and anim data on changes */
2009
2010 static void free_imbuf_strip_elem(TStripElem *se)
2011 {
2012         if(se->ibuf) {
2013                 IMB_freeImBuf(se->ibuf);
2014         }
2015         if(se->ibuf_comp) {
2016                 IMB_freeImBuf(se->ibuf_comp);
2017         }
2018         se->ibuf_comp = 0;
2019         se->ibuf= 0;
2020         se->ok= STRIPELEM_OK;
2021         se->se1= se->se2= se->se3= 0;
2022 }
2023
2024 static void free_anim_seq(Sequence *seq)
2025 {
2026         if(seq->anim) {
2027                 IMB_free_anim(seq->anim);
2028                 seq->anim = 0;
2029         }
2030 }
2031
2032 void free_imbuf_seq_except(int cfra)
2033 {
2034         Editing *ed= G.scene->ed;
2035         Sequence *seq;
2036         TStripElem *se;
2037         int a;
2038
2039         if(ed==0) return;
2040
2041         WHILE_SEQ(&ed->seqbase) {
2042                 if(seq->strip) {
2043                         TStripElem * curelem = give_tstripelem(seq, cfra);
2044
2045                         for(a=0, se= seq->strip->tstripdata; a<seq->len; a++, se++)
2046                                 if(se != curelem)
2047                                         free_imbuf_strip_elem(se);
2048
2049                         if(seq->type==SEQ_MOVIE)
2050                                 if(seq->startdisp > cfra || seq->enddisp < cfra)
2051                                         free_anim_seq(seq);
2052                 }
2053         }
2054         END_SEQ
2055 }
2056
2057 void free_imbuf_seq()
2058 {
2059         Editing *ed= G.scene->ed;
2060         Sequence *seq;
2061         TStripElem *se;
2062         int a;
2063
2064         if(ed==0) return;
2065
2066         WHILE_SEQ(&ed->seqbase) {
2067                 if(seq->strip) {
2068                         if (seq->strip->tstripdata) {
2069                                 for(a=0, se= seq->strip->tstripdata; a<seq->len; a++, se++)
2070                                         free_imbuf_strip_elem(se);
2071                         }
2072
2073                         if(seq->type==SEQ_MOVIE)
2074                                 free_anim_seq(seq);
2075                         if(seq->type==SEQ_SPEED) {
2076                                 sequence_effect_speed_rebuild_map(seq, 1);
2077                         }
2078                 }
2079         }
2080         END_SEQ
2081 }
2082
2083 void free_imbuf_seq_with_ipo(struct Ipo *ipo)
2084 {
2085         /* force update of all sequences with this ipo, on ipo changes */
2086         Editing *ed= G.scene->ed;
2087         Sequence *seq;
2088
2089         if(ed==0) return;
2090
2091         WHILE_SEQ(&ed->seqbase) {
2092                 if(seq->ipo == ipo) {
2093                         update_changed_seq_and_deps(seq, 0, 1);
2094                         if(seq->type == SEQ_SPEED) {
2095                                 sequence_effect_speed_rebuild_map(seq, 1);
2096                         }
2097                 }
2098         }
2099         END_SEQ
2100 }
2101
2102 static int update_changed_seq_recurs(Sequence *seq, Sequence *changed_seq, int len_change, int ibuf_change)
2103 {
2104         Sequence *subseq;
2105         int a, free_imbuf = 0;
2106         TStripElem *se;
2107
2108         /* recurs downwards to see if this seq depends on the changed seq */
2109
2110         if(seq == NULL)
2111                 return 0;
2112
2113         if(seq == changed_seq)
2114                 free_imbuf = 1;
2115         
2116         for(subseq=seq->seqbase.first; subseq; subseq=subseq->next)
2117                 if(update_changed_seq_recurs(subseq, changed_seq, len_change, ibuf_change))
2118                         free_imbuf = TRUE;
2119         
2120         if(seq->seq1)
2121                 if(update_changed_seq_recurs(seq->seq1, changed_seq, len_change, ibuf_change))
2122                         free_imbuf = TRUE;
2123         if(seq->seq2 && (seq->seq2 != seq->seq1))
2124                 if(update_changed_seq_recurs(seq->seq2, changed_seq, len_change, ibuf_change))
2125                         free_imbuf = TRUE;
2126         if(seq->seq3 && (seq->seq3 != seq->seq1) && (seq->seq3 != seq->seq2))
2127                 if(update_changed_seq_recurs(seq->seq3, changed_seq, len_change, ibuf_change))
2128                         free_imbuf = TRUE;
2129         
2130         if(free_imbuf) {
2131                 if(ibuf_change) {
2132                         se= seq->strip->tstripdata;
2133                         if (se) {
2134                                 for(a=0; a<seq->len; a++, se++)
2135                                         free_imbuf_strip_elem(se);
2136                         }
2137                 
2138                         if(seq->type == SEQ_MOVIE)
2139                                 free_anim_seq(seq);
2140                         if(seq->type == SEQ_SPEED) {
2141                                 sequence_effect_speed_rebuild_map(seq, 1);
2142                         }
2143                 }
2144
2145                 if(len_change)
2146                         calc_sequence(seq);
2147         }
2148         
2149         return free_imbuf;
2150 }
2151
2152 void update_changed_seq_and_deps(Sequence *changed_seq, int len_change, int ibuf_change)
2153 {
2154         Editing *ed= G.scene->ed;
2155         Sequence *seq;
2156
2157         if (!ed) return;
2158
2159         for (seq=ed->seqbase.first; seq; seq=seq->next)
2160                 update_changed_seq_recurs(seq, changed_seq, len_change, ibuf_change);
2161 }
2162
2163 /* bad levell call... */
2164 void do_render_seq(RenderResult *rr, int cfra)
2165 {
2166         ImBuf *ibuf;
2167
2168         G.f |= G_PLAYANIM;      /* waitcursor patch */
2169
2170         ibuf= give_ibuf_seq(rr->rectx, rr->recty, cfra, 0);
2171         
2172         if(ibuf) {
2173                 if(ibuf->rect_float) {
2174                         if (!rr->rectf)
2175                                 rr->rectf= MEM_mallocN(4*sizeof(float)*rr->rectx*rr->recty, "render_seq rectf");
2176                         
2177                         memcpy(rr->rectf, ibuf->rect_float, 4*sizeof(float)*rr->rectx*rr->recty);
2178                         
2179                         /* TSK! Since sequence render doesn't free the *rr render result, the old rect32
2180                            can hang around when sequence render has rendered a 32 bits one before */
2181                         if(rr->rect32) {
2182                                 MEM_freeN(rr->rect32);
2183                                 rr->rect32= NULL;
2184                         }
2185                 }
2186                 else if(ibuf->rect) {
2187                         if (!rr->rect32)
2188                                 rr->rect32= MEM_mallocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
2189
2190                         memcpy(rr->rect32, ibuf->rect, 4*rr->rectx*rr->recty);
2191
2192                         /* if (ibuf->zbuf) { */
2193                         /*      if (R.rectz) freeN(R.rectz); */
2194                         /*      R.rectz = BLI_dupallocN(ibuf->zbuf); */
2195                         /* } */
2196                 }
2197                 
2198                 /* Let the cache limitor take care of this (schlaile) */
2199                 /* While render let's keep all memory available for render 
2200                    (ton)
2201                    At least if free memory is tight...
2202                    This can make a big difference in encoding speed
2203                    (it is around 4 times(!) faster, if we do not waste time
2204                    on freeing _all_ buffers every time on long timelines...)
2205                    (schlaile)
2206                 */
2207                 {
2208                         extern int mem_in_use;
2209                         extern int mmap_in_use;
2210
2211                         int max = MEM_CacheLimiter_get_maximum();
2212                         if (max != 0 && mem_in_use + mmap_in_use > max) {
2213                                 fprintf(stderr, "mem_in_use = %d, max = %d\n",
2214                                         mem_in_use + mmap_in_use, max);
2215                                 fprintf(stderr, "Cleaning up, please wait...\n"
2216                                         "If this happens very often,\n"
2217                                         "consider "
2218                                         "raising the memcache limit in the "
2219                                         "user preferences.\n");
2220                                 free_imbuf_seq();
2221                         }
2222                 }
2223         }
2224         else {
2225                 /* render result is delivered empty in most cases, nevertheless we handle all cases */
2226                 if (rr->rectf)
2227                         memset(rr->rectf, 0, 4*sizeof(float)*rr->rectx*rr->recty);
2228                 else if (rr->rect32)
2229                         memset(rr->rect32, 0, 4*rr->rectx*rr->recty);
2230                 else
2231                         rr->rect32= MEM_callocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
2232         }
2233         
2234         G.f &= ~G_PLAYANIM;
2235
2236 }