=== Custom Transform Orientation ===
[blender.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);
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                         if (i == 0) {
1547                                 se->ibuf_comp = se->ibuf;
1548                                 IMB_refImBuf(se->ibuf_comp);
1549                         }
1550                         break;
1551                 }
1552         
1553                 if (se->ibuf_comp) {
1554                         break;
1555                 }
1556         }
1557
1558         i++;
1559
1560         for (; i < count; i++) {
1561                 Sequence * seq = seq_arr[i];
1562                 struct SeqEffectHandle sh = get_sequence_blend(seq);
1563                 TStripElem* se1 = give_tstripelem(seq_arr[i-1], cfra);
1564                 TStripElem* se2 = give_tstripelem(seq_arr[i], cfra);
1565         
1566                 int early_out = sh.early_out(seq, seq->facf0, seq->facf1);
1567                 switch (early_out) {
1568                 case 0: {
1569                         int x= se2->ibuf->x;
1570                         int y= se2->ibuf->y;
1571                         int swap_input = FALSE;
1572
1573                         if (se1->ibuf_comp->rect_float ||
1574                             se2->ibuf->rect_float) {
1575                                 se2->ibuf_comp = IMB_allocImBuf(
1576                                         (short)seqrectx, (short)seqrecty, 
1577                                         32, IB_rectfloat, 0);
1578                         } else {
1579                                 se2->ibuf_comp = IMB_allocImBuf(
1580                                         (short)seqrectx, (short)seqrecty, 
1581                                         32, IB_rect, 0);
1582                         }
1583
1584
1585                         if (!se1->ibuf_comp->rect_float && 
1586                             se2->ibuf_comp->rect_float) {
1587                                 IMB_float_from_rect(se1->ibuf_comp);
1588                         }
1589                         if (!se2->ibuf->rect_float && 
1590                             se2->ibuf_comp->rect_float) {
1591                                 IMB_float_from_rect(se2->ibuf);
1592                         }
1593
1594                         if (!se1->ibuf_comp->rect && 
1595                             !se2->ibuf_comp->rect_float) {
1596                                 IMB_rect_from_float(se1->ibuf_comp);
1597                         }
1598                         if (!se2->ibuf->rect && 
1599                             !se2->ibuf_comp->rect_float) {
1600                                 IMB_rect_from_float(se2->ibuf);
1601                         }
1602
1603                         /* bad hack, to fix crazy input ordering of 
1604                            those two effects */
1605
1606                         if (seq->blend_mode == SEQ_ALPHAOVER ||
1607                             seq->blend_mode == SEQ_ALPHAUNDER ||
1608                             seq->blend_mode == SEQ_OVERDROP) {
1609                                 swap_input = TRUE;
1610                         }
1611
1612                         if (swap_input) {
1613                                 sh.execute(seq, cfra, 
1614                                            seq->facf0, seq->facf1, x, y, 
1615                                            se2->ibuf, se1->ibuf_comp, 0,
1616                                            se2->ibuf_comp);
1617                         } else {
1618                                 sh.execute(seq, cfra, 
1619                                            seq->facf0, seq->facf1, x, y, 
1620                                            se1->ibuf_comp, se2->ibuf, 0,
1621                                            se2->ibuf_comp);
1622                         }
1623                         
1624                         IMB_cache_limiter_insert(se2->ibuf_comp);
1625                         IMB_cache_limiter_ref(se2->ibuf_comp);
1626                         IMB_cache_limiter_touch(se2->ibuf_comp);
1627
1628                         IMB_cache_limiter_unref(se1->ibuf_comp);
1629                         IMB_cache_limiter_unref(se2->ibuf);
1630
1631                         break;
1632                 }
1633                 case 1: {
1634                         se2->ibuf_comp = se1->ibuf;
1635                         IMB_refImBuf(se2->ibuf_comp);
1636
1637                         break;
1638                 }
1639                 }
1640                 se = se2;
1641         }
1642
1643         return se;
1644 }
1645
1646 /*
1647  * returned ImBuf is refed!
1648  * you have to unref after usage!
1649  */
1650
1651 static ImBuf *give_ibuf_seq_impl(int rectx, int recty, int cfra, int chanshown)
1652 {
1653         Editing *ed;
1654         int count;
1655         ListBase *seqbasep;
1656         TStripElem *se;
1657
1658         ed= G.scene->ed;
1659         if(ed==0) return 0;
1660
1661         count = BLI_countlist(&ed->metastack);
1662         if((chanshown < 0) && (count > 0)) {
1663                 count = MAX2(count + chanshown, 0);
1664                 seqbasep= ((MetaStack*)BLI_findlink(&ed->metastack, count))->oldbasep;
1665         } else {
1666                 seqbasep= ed->seqbasep;
1667         }
1668
1669         seqrectx= rectx;        /* bad bad global! */
1670         seqrecty= recty;
1671
1672         se = do_build_seq_array_recursively(seqbasep, cfra, chanshown);
1673
1674         if(!se) { 
1675                 return 0;
1676         }
1677
1678         return se->ibuf_comp;
1679 }
1680
1681 ImBuf *give_ibuf_seq_direct(int rectx, int recty, int cfra,
1682                             Sequence * seq)
1683 {
1684         TStripElem* se;
1685
1686         seqrectx= rectx;        /* bad bad global! */
1687         seqrecty= recty;
1688
1689         se = do_build_seq_recursively(seq, cfra);
1690
1691         if(!se) { 
1692                 return 0;
1693         }
1694
1695         if (se->ibuf) {
1696                 IMB_cache_limiter_unref(se->ibuf);
1697         }
1698
1699         return se->ibuf;
1700 }
1701
1702 ImBuf *give_ibuf_seq(int rectx, int recty, int cfra, int chanshown)
1703 {
1704         ImBuf* i = give_ibuf_seq_impl(rectx, recty, cfra, chanshown);
1705
1706         if (i) {
1707                 IMB_cache_limiter_unref(i);
1708         }
1709         return i;
1710 }
1711
1712 /* threading api */
1713
1714 static ListBase running_threads;
1715 static ListBase prefetch_wait;
1716 static ListBase prefetch_done;
1717
1718 static pthread_mutex_t queue_lock          = PTHREAD_MUTEX_INITIALIZER;
1719 static pthread_mutex_t wakeup_lock         = PTHREAD_MUTEX_INITIALIZER;
1720 static pthread_cond_t  wakeup_cond         = PTHREAD_COND_INITIALIZER;
1721
1722 static pthread_mutex_t prefetch_ready_lock = PTHREAD_MUTEX_INITIALIZER;
1723 static pthread_cond_t  prefetch_ready_cond = PTHREAD_COND_INITIALIZER;
1724
1725 static pthread_mutex_t frame_done_lock     = PTHREAD_MUTEX_INITIALIZER;
1726 static pthread_cond_t  frame_done_cond     = PTHREAD_COND_INITIALIZER;
1727
1728 static volatile int seq_thread_shutdown = FALSE;
1729 static volatile int seq_last_given_monoton_cfra = 0;
1730 static int monoton_cfra = 0;
1731
1732 typedef struct PrefetchThread {
1733         struct PrefetchThread *next, *prev;
1734         struct PrefetchQueueElem *current;
1735         pthread_t pthread;
1736         int running;
1737 } PrefetchThread;
1738
1739 typedef struct PrefetchQueueElem {
1740         struct PrefetchQueueElem *next, *prev;
1741         
1742         int rectx;
1743         int recty;
1744         int cfra;
1745         int chanshown;
1746
1747         int monoton_cfra;
1748
1749         struct ImBuf * ibuf;
1750 } PrefetchQueueElem;
1751
1752 static void * seq_prefetch_thread(void * This_)
1753 {
1754         PrefetchThread * This = This_;
1755
1756         while (!seq_thread_shutdown) {
1757                 PrefetchQueueElem * e;
1758                 int s_last;
1759
1760                 pthread_mutex_lock(&queue_lock);
1761                 e = prefetch_wait.first;
1762                 if (e) {
1763                         BLI_remlink(&prefetch_wait, e);
1764                 }
1765                 s_last = seq_last_given_monoton_cfra;
1766
1767                 This->current = e;
1768
1769                 pthread_mutex_unlock(&queue_lock);
1770
1771                 if (!e) {
1772                         pthread_mutex_lock(&prefetch_ready_lock);
1773
1774                         This->running = FALSE;
1775
1776                         pthread_cond_signal(&prefetch_ready_cond);
1777                         pthread_mutex_unlock(&prefetch_ready_lock);
1778
1779                         pthread_mutex_lock(&wakeup_lock);
1780                         if (!seq_thread_shutdown) {
1781                                 pthread_cond_wait(&wakeup_cond, &wakeup_lock);
1782                         }
1783                         pthread_mutex_unlock(&wakeup_lock);
1784                         continue;
1785                 }
1786
1787                 This->running = TRUE;
1788                 
1789                 if (e->cfra >= s_last) { 
1790                         e->ibuf = give_ibuf_seq_impl(
1791                                 e->rectx, e->recty, e->cfra, e->chanshown);
1792                 }
1793
1794                 pthread_mutex_lock(&queue_lock);
1795
1796                 BLI_addtail(&prefetch_done, e);
1797
1798                 for (e = prefetch_wait.first; e; e = e->next) {
1799                         if (s_last > e->monoton_cfra) {
1800                                 BLI_remlink(&prefetch_wait, e);
1801                                 MEM_freeN(e);
1802                         }
1803                 }
1804
1805                 for (e = prefetch_done.first; e; e = e->next) {
1806                         if (s_last > e->monoton_cfra) {
1807                                 if (e->ibuf) {
1808                                         IMB_cache_limiter_unref(e->ibuf);
1809                                 }
1810                                 BLI_remlink(&prefetch_done, e);
1811                                 MEM_freeN(e);
1812                         }
1813                 }
1814
1815                 pthread_mutex_unlock(&queue_lock);
1816
1817                 pthread_mutex_lock(&frame_done_lock);
1818                 pthread_cond_signal(&frame_done_cond);
1819                 pthread_mutex_unlock(&frame_done_lock);
1820         }
1821         return 0;
1822 }
1823
1824 void seq_start_threads()
1825 {
1826         int i;
1827
1828         running_threads.first = running_threads.last = NULL;
1829         prefetch_wait.first = prefetch_wait.last = NULL;
1830         prefetch_done.first = prefetch_done.last = NULL;
1831
1832         seq_thread_shutdown = FALSE;
1833         seq_last_given_monoton_cfra = monoton_cfra = 0;
1834
1835         /* since global structures are modified during the processing
1836            of one frame, only one render thread is currently possible... 
1837
1838            (but we code, in the hope, that we can remove this restriction
1839            soon...)
1840         */
1841
1842         fprintf(stderr, "SEQ-THREAD: seq_start_threads\n");
1843
1844         for (i = 0; i < 1; i++) {
1845                 PrefetchThread *t = MEM_callocN(sizeof(PrefetchThread), 
1846                                                 "prefetch_thread");
1847                 t->running = TRUE;
1848                 BLI_addtail(&running_threads, t);
1849
1850                 pthread_create(&t->pthread, NULL, seq_prefetch_thread, t);
1851         }
1852
1853         /* init malloc mutex */
1854         BLI_init_threads(0, 0, 0);
1855 }
1856
1857 void seq_stop_threads()
1858 {
1859         PrefetchThread *tslot;
1860         PrefetchQueueElem * e;
1861
1862         fprintf(stderr, "SEQ-THREAD: seq_stop_threads()\n");
1863
1864         if (seq_thread_shutdown) {
1865                 fprintf(stderr, "SEQ-THREAD: ... already stopped\n");
1866                 return;
1867         }
1868         
1869         pthread_mutex_lock(&wakeup_lock);
1870
1871         seq_thread_shutdown = TRUE;
1872
1873         pthread_cond_broadcast(&wakeup_cond);
1874         pthread_mutex_unlock(&wakeup_lock);
1875
1876         for(tslot = running_threads.first; tslot; tslot= tslot->next) {
1877                 pthread_join(tslot->pthread, NULL);
1878         }
1879
1880
1881         for (e = prefetch_wait.first; e; e = e->next) {
1882                 BLI_remlink(&prefetch_wait, e);
1883                 MEM_freeN(e);
1884         }
1885
1886         for (e = prefetch_done.first; e; e = e->next) {
1887                 if (e->ibuf) {
1888                         IMB_cache_limiter_unref(e->ibuf);
1889                 }
1890                 BLI_remlink(&prefetch_done, e);
1891                 MEM_freeN(e);
1892         }
1893
1894         BLI_freelistN(&running_threads);
1895
1896         /* deinit malloc mutex */
1897         BLI_end_threads(0);
1898 }
1899
1900 void give_ibuf_prefetch_request(int rectx, int recty, int cfra, int chanshown)
1901 {
1902         PrefetchQueueElem * e;
1903         if (seq_thread_shutdown) {
1904                 return;
1905         }
1906
1907         e = MEM_callocN(sizeof(PrefetchQueueElem), "prefetch_queue_elem");
1908         e->rectx = rectx;
1909         e->recty = recty;
1910         e->cfra = cfra;
1911         e->chanshown = chanshown;
1912         e->monoton_cfra = monoton_cfra++;
1913
1914         pthread_mutex_lock(&queue_lock);
1915         BLI_addtail(&prefetch_wait, e);
1916         pthread_mutex_unlock(&queue_lock);
1917         
1918         pthread_mutex_lock(&wakeup_lock);
1919         pthread_cond_signal(&wakeup_cond);
1920         pthread_mutex_unlock(&wakeup_lock);
1921 }
1922
1923 void seq_wait_for_prefetch_ready()
1924 {
1925         PrefetchThread *tslot;
1926
1927         if (seq_thread_shutdown) {
1928                 return;
1929         }
1930
1931         fprintf(stderr, "SEQ-THREAD: rendering prefetch frames...\n");
1932
1933         pthread_mutex_lock(&prefetch_ready_lock);
1934
1935         for(;;) {
1936                 for(tslot = running_threads.first; tslot; tslot= tslot->next) {
1937                         if (tslot->running) {
1938                                 break;
1939                         }
1940                 }
1941                 if (!tslot) {
1942                         break;
1943                 }
1944                 pthread_cond_wait(&prefetch_ready_cond, &prefetch_ready_lock);
1945         }
1946
1947         pthread_mutex_unlock(&prefetch_ready_lock);
1948
1949         fprintf(stderr, "SEQ-THREAD: prefetch done\n");
1950 }
1951
1952 ImBuf * give_ibuf_seq_threaded(int rectx, int recty, int cfra, int chanshown)
1953 {
1954         PrefetchQueueElem * e = 0;
1955         int found_something = FALSE;
1956
1957         if (seq_thread_shutdown) {
1958                 return give_ibuf_seq(rectx, recty, cfra, chanshown);
1959         }
1960
1961         while (!e) {
1962                 int success = FALSE;
1963                 pthread_mutex_lock(&queue_lock);
1964
1965                 for (e = prefetch_done.first; e; e = e->next) {
1966                         if (cfra == e->cfra &&
1967                             chanshown == e->chanshown &&
1968                             rectx == e->rectx && 
1969                             recty == e->recty) {
1970                                 success = TRUE;
1971                                 found_something = TRUE;
1972                                 break;
1973                         }
1974                 }
1975
1976                 if (!e) {
1977                         for (e = prefetch_wait.first; e; e = e->next) {
1978                                 if (cfra == e->cfra &&
1979                                     chanshown == e->chanshown &&
1980                                     rectx == e->rectx && 
1981                                     recty == e->recty) {
1982                                         found_something = TRUE;
1983                                         break;
1984                                 }
1985                         }
1986                 }
1987
1988                 if (!e) {
1989                         PrefetchThread *tslot;
1990
1991                         for(tslot = running_threads.first; 
1992                             tslot; tslot= tslot->next) {
1993                                 if (tslot->current &&
1994                                     cfra == tslot->current->cfra &&
1995                                     chanshown == tslot->current->chanshown &&
1996                                     rectx == tslot->current->rectx && 
1997                                     recty == tslot->current->recty) {
1998                                         found_something = TRUE;
1999                                         break;
2000                                 }
2001                         }
2002                 }
2003
2004                 /* e->ibuf is unrefed by render thread on next round. */
2005
2006                 if (e) {
2007                         seq_last_given_monoton_cfra = e->monoton_cfra;
2008                 }
2009
2010                 pthread_mutex_unlock(&queue_lock);
2011
2012                 if (!success) {
2013                         e = NULL;
2014
2015                         if (!found_something) {
2016                                 fprintf(stderr, 
2017                                         "SEQ-THREAD: Requested frame "
2018                                         "not in queue ???\n");
2019                                 break;
2020                         }
2021                         pthread_mutex_lock(&frame_done_lock);
2022                         pthread_cond_wait(&frame_done_cond, &frame_done_lock);
2023                         pthread_mutex_unlock(&frame_done_lock);
2024                 }
2025         }
2026         
2027         return e ? e->ibuf : 0;
2028 }
2029
2030 /* Functions to free imbuf and anim data on changes */
2031
2032 static void free_imbuf_strip_elem(TStripElem *se)
2033 {
2034         if(se->ibuf) {
2035                 IMB_freeImBuf(se->ibuf);
2036         }
2037         if(se->ibuf_comp) {
2038                 IMB_freeImBuf(se->ibuf_comp);
2039         }
2040         se->ibuf_comp = 0;
2041         se->ibuf= 0;
2042         se->ok= STRIPELEM_OK;
2043         se->se1= se->se2= se->se3= 0;
2044 }
2045
2046 static void free_anim_seq(Sequence *seq)
2047 {
2048         if(seq->anim) {
2049                 IMB_free_anim(seq->anim);
2050                 seq->anim = 0;
2051         }
2052 }
2053
2054 void free_imbuf_seq_except(int cfra)
2055 {
2056         Editing *ed= G.scene->ed;
2057         Sequence *seq;
2058         TStripElem *se;
2059         int a;
2060
2061         if(ed==0) return;
2062
2063         WHILE_SEQ(&ed->seqbase) {
2064                 if(seq->strip) {
2065                         TStripElem * curelem = give_tstripelem(seq, cfra);
2066
2067                         for(a=0, se= seq->strip->tstripdata; a<seq->len; a++, se++)
2068                                 if(se != curelem)
2069                                         free_imbuf_strip_elem(se);
2070
2071                         if(seq->type==SEQ_MOVIE)
2072                                 if(seq->startdisp > cfra || seq->enddisp < cfra)
2073                                         free_anim_seq(seq);
2074                 }
2075         }
2076         END_SEQ
2077 }
2078
2079 void free_imbuf_seq()
2080 {
2081         Editing *ed= G.scene->ed;
2082         Sequence *seq;
2083         TStripElem *se;
2084         int a;
2085
2086         if(ed==0) return;
2087
2088         WHILE_SEQ(&ed->seqbase) {
2089                 if(seq->strip) {
2090                         if (seq->strip->tstripdata) {
2091                                 for(a=0, se= seq->strip->tstripdata; a<seq->len; a++, se++)
2092                                         free_imbuf_strip_elem(se);
2093                         }
2094
2095                         if(seq->type==SEQ_MOVIE)
2096                                 free_anim_seq(seq);
2097                         if(seq->type==SEQ_SPEED) {
2098                                 sequence_effect_speed_rebuild_map(seq, 1);
2099                         }
2100                 }
2101         }
2102         END_SEQ
2103 }
2104
2105 void free_imbuf_seq_with_ipo(struct Ipo *ipo)
2106 {
2107         /* force update of all sequences with this ipo, on ipo changes */
2108         Editing *ed= G.scene->ed;
2109         Sequence *seq;
2110
2111         if(ed==0) return;
2112
2113         WHILE_SEQ(&ed->seqbase) {
2114                 if(seq->ipo == ipo) {
2115                         update_changed_seq_and_deps(seq, 0, 1);
2116                         if(seq->type == SEQ_SPEED) {
2117                                 sequence_effect_speed_rebuild_map(seq, 1);
2118                         }
2119                 }
2120         }
2121         END_SEQ
2122 }
2123
2124 static int update_changed_seq_recurs(Sequence *seq, Sequence *changed_seq, int len_change, int ibuf_change)
2125 {
2126         Sequence *subseq;
2127         int a, free_imbuf = 0;
2128         TStripElem *se;
2129
2130         /* recurs downwards to see if this seq depends on the changed seq */
2131
2132         if(seq == NULL)
2133                 return 0;
2134
2135         if(seq == changed_seq)
2136                 free_imbuf = 1;
2137         
2138         for(subseq=seq->seqbase.first; subseq; subseq=subseq->next)
2139                 if(update_changed_seq_recurs(subseq, changed_seq, len_change, ibuf_change))
2140                         free_imbuf = TRUE;
2141         
2142         if(seq->seq1)
2143                 if(update_changed_seq_recurs(seq->seq1, changed_seq, len_change, ibuf_change))
2144                         free_imbuf = TRUE;
2145         if(seq->seq2 && (seq->seq2 != seq->seq1))
2146                 if(update_changed_seq_recurs(seq->seq2, changed_seq, len_change, ibuf_change))
2147                         free_imbuf = TRUE;
2148         if(seq->seq3 && (seq->seq3 != seq->seq1) && (seq->seq3 != seq->seq2))
2149                 if(update_changed_seq_recurs(seq->seq3, changed_seq, len_change, ibuf_change))
2150                         free_imbuf = TRUE;
2151         
2152         if(free_imbuf) {
2153                 if(ibuf_change) {
2154                         se= seq->strip->tstripdata;
2155                         if (se) {
2156                                 for(a=0; a<seq->len; a++, se++)
2157                                         free_imbuf_strip_elem(se);
2158                         }
2159                 
2160                         if(seq->type == SEQ_MOVIE)
2161                                 free_anim_seq(seq);
2162                         if(seq->type == SEQ_SPEED) {
2163                                 sequence_effect_speed_rebuild_map(seq, 1);
2164                         }
2165                 }
2166
2167                 if(len_change)
2168                         calc_sequence(seq);
2169         }
2170         
2171         return free_imbuf;
2172 }
2173
2174 void update_changed_seq_and_deps(Sequence *changed_seq, int len_change, int ibuf_change)
2175 {
2176         Editing *ed= G.scene->ed;
2177         Sequence *seq;
2178
2179         if (!ed) return;
2180
2181         for (seq=ed->seqbase.first; seq; seq=seq->next)
2182                 update_changed_seq_recurs(seq, changed_seq, len_change, ibuf_change);
2183 }
2184
2185 /* bad levell call... */
2186 void do_render_seq(RenderResult *rr, int cfra)
2187 {
2188         ImBuf *ibuf;
2189
2190         G.f |= G_PLAYANIM;      /* waitcursor patch */
2191
2192         ibuf= give_ibuf_seq(rr->rectx, rr->recty, cfra, 0);
2193         
2194         if(ibuf) {
2195                 if(ibuf->rect_float) {
2196                         if (!rr->rectf)
2197                                 rr->rectf= MEM_mallocN(4*sizeof(float)*rr->rectx*rr->recty, "render_seq rectf");
2198                         
2199                         memcpy(rr->rectf, ibuf->rect_float, 4*sizeof(float)*rr->rectx*rr->recty);
2200                         
2201                         /* TSK! Since sequence render doesn't free the *rr render result, the old rect32
2202                            can hang around when sequence render has rendered a 32 bits one before */
2203                         if(rr->rect32) {
2204                                 MEM_freeN(rr->rect32);
2205                                 rr->rect32= NULL;
2206                         }
2207                 }
2208                 else if(ibuf->rect) {
2209                         if (!rr->rect32)
2210                                 rr->rect32= MEM_mallocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
2211
2212                         memcpy(rr->rect32, ibuf->rect, 4*rr->rectx*rr->recty);
2213
2214                         /* if (ibuf->zbuf) { */
2215                         /*      if (R.rectz) freeN(R.rectz); */
2216                         /*      R.rectz = BLI_dupallocN(ibuf->zbuf); */
2217                         /* } */
2218                 }
2219                 
2220                 /* Let the cache limitor take care of this (schlaile) */
2221                 /* While render let's keep all memory available for render 
2222                    (ton)
2223                    At least if free memory is tight...
2224                    This can make a big difference in encoding speed
2225                    (it is around 4 times(!) faster, if we do not waste time
2226                    on freeing _all_ buffers every time on long timelines...)
2227                    (schlaile)
2228                 */
2229                 {
2230                         extern int mem_in_use;
2231                         extern int mmap_in_use;
2232
2233                         int max = MEM_CacheLimiter_get_maximum();
2234                         if (max != 0 && mem_in_use + mmap_in_use > max) {
2235                                 fprintf(stderr, "mem_in_use = %d, max = %d\n",
2236                                         mem_in_use + mmap_in_use, max);
2237                                 fprintf(stderr, "Cleaning up, please wait...\n"
2238                                         "If this happens very often,\n"
2239                                         "consider "
2240                                         "raising the memcache limit in the "
2241                                         "user preferences.\n");
2242                                 free_imbuf_seq();
2243                         }
2244                 }
2245         }
2246         else {
2247                 /* render result is delivered empty in most cases, nevertheless we handle all cases */
2248                 if (rr->rectf)
2249                         memset(rr->rectf, 0, 4*sizeof(float)*rr->rectx*rr->recty);
2250                 else if (rr->rect32)
2251                         memset(rr->rect32, 0, 4*rr->rectx*rr->recty);
2252                 else
2253                         rr->rect32= MEM_callocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
2254         }
2255         
2256         G.f &= ~G_PLAYANIM;
2257
2258 }