== 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
37 #include "BLI_blenlib.h"
38 #include "BLI_arithb.h"
39
40 #include "IMB_imbuf_types.h"
41 #include "IMB_imbuf.h"
42
43 #include "DNA_ipo_types.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_sequence_types.h"
46 #include "DNA_view3d_types.h"
47
48 #include "BKE_global.h"
49 #include "BKE_image.h"
50 #include "BKE_ipo.h"
51 #include "BKE_main.h"
52 #include "BKE_scene.h"
53 #include "BKE_texture.h"
54 #include "BKE_utildefines.h"
55
56 #include "BIF_editsound.h"
57 #include "BIF_editseq.h"
58 #include "BSE_filesel.h"
59 #include "BSE_headerbuttons.h"
60 #include "BIF_interface.h"
61 #include "BIF_renderwin.h"
62 #include "BIF_screen.h"
63 #include "BIF_space.h"
64 #include "BIF_toolbox.h"
65
66 #include "BSE_sequence.h"
67 #include "BSE_seqeffects.h"
68
69 #include "RE_pipeline.h"                // talks to entire render API
70
71 #include "blendef.h"
72
73 int seqrectx, seqrecty;
74
75 void free_stripdata(int len, StripElem *se)
76 {
77         StripElem *seo;
78         int a;
79
80         seo= se;
81
82         for(a=0; a<len; a++, se++) {
83                 if(se->ibuf && se->ok!=2) {
84                         IMB_freeImBuf(se->ibuf);
85                         se->ibuf = 0;
86                 }
87         }
88
89         MEM_freeN(seo);
90
91 }
92
93 void free_strip(Strip *strip)
94 {
95         strip->us--;
96         if(strip->us>0) return;
97         if(strip->us<0) {
98                 printf("error: negative users in strip\n");
99                 return;
100         }
101
102         if(strip->stripdata) {
103                 free_stripdata(strip->len, strip->stripdata);
104         }
105         MEM_freeN(strip);
106 }
107
108 void new_stripdata(Sequence *seq)
109 {
110         if(seq->strip) {
111                 if(seq->strip->stripdata) free_stripdata(seq->strip->len, seq->strip->stripdata);
112                 seq->strip->stripdata= 0;
113                 seq->strip->len= seq->len;
114                 if(seq->len>0) seq->strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelems");
115         }
116 }
117
118 void free_sequence(Sequence *seq)
119 {
120         Sequence *last_seq = get_last_seq();
121
122         if(seq->strip) free_strip(seq->strip);
123
124         if(seq->anim) IMB_free_anim(seq->anim);
125         if(seq->hdaudio) sound_close_hdaudio(seq->hdaudio);
126
127         if (seq->type & SEQ_EFFECT) {
128                 struct SeqEffectHandle sh = get_sequence_effect(seq);
129
130                 sh.free(seq);
131         }
132
133         if(seq==last_seq) set_last_seq(NULL);
134
135         MEM_freeN(seq);
136 }
137
138 /*
139   **********************************************************************
140   * build_seqar
141   **********************************************************************
142   * Build a complete array of _all_ sequencies (including those
143   * in metastrips!)
144   **********************************************************************
145 */
146
147 static void do_seq_count(ListBase *seqbase, int *totseq)
148 {
149         Sequence *seq;
150
151         seq= seqbase->first;
152         while(seq) {
153                 (*totseq)++;
154                 if(seq->seqbase.first) do_seq_count(&seq->seqbase, totseq);
155                 seq= seq->next;
156         }
157 }
158
159 static void do_build_seqar(ListBase *seqbase, Sequence ***seqar, int depth)
160 {
161         Sequence *seq;
162
163         seq= seqbase->first;
164         while(seq) {
165                 seq->depth= depth;
166                 if(seq->seqbase.first) do_build_seqar(&seq->seqbase, seqar, depth+1);
167                 **seqar= seq;
168                 (*seqar)++;
169                 seq= seq->next;
170         }
171 }
172
173 void build_seqar(ListBase *seqbase, Sequence  ***seqar, int *totseq)
174 {
175         Sequence **tseqar;
176
177         *totseq= 0;
178         do_seq_count(seqbase, totseq);
179
180         if(*totseq==0) {
181                 *seqar= 0;
182                 return;
183         }
184         *seqar= MEM_mallocN(sizeof(void *)* *totseq, "seqar");
185         tseqar= *seqar;
186
187         do_build_seqar(seqbase, seqar, 0);
188         *seqar= tseqar;
189 }
190
191 void free_editing(Editing *ed)
192 {
193         MetaStack *ms;
194         Sequence *seq;
195
196         if(ed==0) return;
197
198         WHILE_SEQ(&ed->seqbase) {
199                 free_sequence(seq);
200         }
201         END_SEQ
202
203         while( (ms= ed->metastack.first) ) {
204                 BLI_remlink(&ed->metastack, ms);
205                 MEM_freeN(ms);
206         }
207
208         MEM_freeN(ed);
209
210         clear_last_seq();
211 }
212
213 void calc_sequence(Sequence *seq)
214 {
215         Sequence *seqm;
216         int min, max;
217
218         /* check all metas recursively */
219         seqm= seq->seqbase.first;
220         while(seqm) {
221                 if(seqm->seqbase.first) calc_sequence(seqm);
222                 seqm= seqm->next;
223         }
224
225         /* effects and meta: automatic start and end */
226
227         if(seq->type & SEQ_EFFECT) {
228                 /* pointers */
229                 if(seq->seq2==0) seq->seq2= seq->seq1;
230                 if(seq->seq3==0) seq->seq3= seq->seq1;
231
232                 /* effecten go from seq1 -> seq2: test */
233
234                 /* we take the largest start and smallest end */
235
236                 // seq->start= seq->startdisp= MAX2(seq->seq1->startdisp, seq->seq2->startdisp);
237                 // seq->enddisp= MIN2(seq->seq1->enddisp, seq->seq2->enddisp);
238
239                 if (seq->seq1) {
240                         seq->start= seq->startdisp= MAX3(seq->seq1->startdisp, seq->seq2->startdisp, seq->seq3->startdisp);
241                         seq->enddisp= MIN3(seq->seq1->enddisp, seq->seq2->enddisp, seq->seq3->enddisp);
242                         seq->len= seq->enddisp - seq->startdisp;
243                 } else {
244                         if(seq->startofs && seq->startstill) seq->startstill= 0;
245                         if(seq->endofs && seq->endstill) seq->endstill= 0;
246
247                         seq->startdisp= seq->start + seq->startofs - seq->startstill;
248                         seq->enddisp= seq->start+seq->len - seq->endofs + seq->endstill;
249
250                         seq->handsize= 10.0;    /* 10 frames */
251                         if( seq->enddisp-seq->startdisp < 20 ) {
252                                 seq->handsize= (float)(0.5*(seq->enddisp-seq->startdisp));
253                         }
254                         else if(seq->enddisp-seq->startdisp > 250) {
255                                 seq->handsize= (float)((seq->enddisp-seq->startdisp)/25);
256                         }
257                 }
258
259                 if(seq->strip && seq->len!=seq->strip->len) {
260                         new_stripdata(seq);
261                 }
262
263         }
264         else {
265                 if(seq->type==SEQ_META) {
266                         seqm= seq->seqbase.first;
267                         if(seqm) {
268                                 min= 1000000;
269                                 max= -1000000;
270                                 while(seqm) {
271                                         if(seqm->startdisp < min) min= seqm->startdisp;
272                                         if(seqm->enddisp > max) max= seqm->enddisp;
273                                         seqm= seqm->next;
274                                 }
275                                 seq->start= min;
276                                 seq->len= max-min;
277
278                                 if(seq->strip && seq->len!=seq->strip->len) {
279                                         new_stripdata(seq);
280                                 }
281                         }
282                 }
283
284
285                 if(seq->startofs && seq->startstill) seq->startstill= 0;
286                 if(seq->endofs && seq->endstill) seq->endstill= 0;
287
288                 seq->startdisp= seq->start + seq->startofs - seq->startstill;
289                 seq->enddisp= seq->start+seq->len - seq->endofs + seq->endstill;
290
291                 seq->handsize= 10.0;    /* 10 frames */
292                 if( seq->enddisp-seq->startdisp < 20 ) {
293                         seq->handsize= (float)(0.5*(seq->enddisp-seq->startdisp));
294                 }
295                 else if(seq->enddisp-seq->startdisp > 250) {
296                         seq->handsize= (float)((seq->enddisp-seq->startdisp)/25);
297                 }
298         }
299 }
300
301 void sort_seq()
302 {
303         /* all strips together per kind, and in order of y location ("machine") */
304         ListBase seqbase, effbase;
305         Editing *ed;
306         Sequence *seq, *seqt;
307
308         ed= G.scene->ed;
309         if(ed==0) return;
310
311         seqbase.first= seqbase.last= 0;
312         effbase.first= effbase.last= 0;
313
314         while( (seq= ed->seqbasep->first) ) {
315                 BLI_remlink(ed->seqbasep, seq);
316
317                 if(seq->type & SEQ_EFFECT) {
318                         seqt= effbase.first;
319                         while(seqt) {
320                                 if(seqt->machine>=seq->machine) {
321                                         BLI_insertlinkbefore(&effbase, seqt, seq);
322                                         break;
323                                 }
324                                 seqt= seqt->next;
325                         }
326                         if(seqt==0) BLI_addtail(&effbase, seq);
327                 }
328                 else {
329                         seqt= seqbase.first;
330                         while(seqt) {
331                                 if(seqt->machine>=seq->machine) {
332                                         BLI_insertlinkbefore(&seqbase, seqt, seq);
333                                         break;
334                                 }
335                                 seqt= seqt->next;
336                         }
337                         if(seqt==0) BLI_addtail(&seqbase, seq);
338                 }
339         }
340
341         addlisttolist(&seqbase, &effbase);
342         *(ed->seqbasep)= seqbase;
343 }
344
345
346 void clear_scene_in_allseqs(Scene *sce)
347 {
348         Scene *sce1;
349         Editing *ed;
350         Sequence *seq;
351
352         /* when a scene is deleted: test all seqs */
353
354         sce1= G.main->scene.first;
355         while(sce1) {
356                 if(sce1!=sce && sce1->ed) {
357                         ed= sce1->ed;
358
359                         WHILE_SEQ(&ed->seqbase) {
360
361                                 if(seq->scene==sce) seq->scene= 0;
362
363                         }
364                         END_SEQ
365                 }
366
367                 sce1= sce1->id.next;
368         }
369 }
370
371 /* ***************** DO THE SEQUENCE ***************** */
372
373 static void make_black_ibuf(ImBuf *ibuf)
374 {
375         unsigned int *rect;
376         float *rect_float;
377         int tot;
378
379         if(ibuf==0 || (ibuf->rect==0 && ibuf->rect_float==0)) return;
380
381         tot= ibuf->x*ibuf->y;
382
383         rect= ibuf->rect;
384         rect_float = ibuf->rect_float;
385
386         if (rect) {
387                 memset(rect,       0, tot * sizeof(char) * 4);
388         }
389
390         if (rect_float) {
391                 memset(rect_float, 0, tot * sizeof(float) * 4);
392         }
393 }
394
395 static void multibuf(ImBuf *ibuf, float fmul)
396 {
397         char *rt;
398         float *rt_float;
399
400         int a, mul, icol;
401
402         mul= (int)(256.0*fmul);
403
404         a= ibuf->x*ibuf->y;
405         rt= (char *)ibuf->rect;
406         rt_float = ibuf->rect_float;
407
408         if (rt) {
409                 while(a--) {
410
411                         icol= (mul*rt[0])>>8;
412                         if(icol>254) rt[0]= 255; else rt[0]= icol;
413                         icol= (mul*rt[1])>>8;
414                         if(icol>254) rt[1]= 255; else rt[1]= icol;
415                         icol= (mul*rt[2])>>8;
416                         if(icol>254) rt[2]= 255; else rt[2]= icol;
417                         icol= (mul*rt[3])>>8;
418                         if(icol>254) rt[3]= 255; else rt[3]= icol;
419                         
420                         rt+= 4;
421                 }
422         }
423         if (rt_float) {
424                 while(a--) {
425                         rt_float[0] *= fmul;
426                         rt_float[1] *= fmul;
427                         rt_float[2] *= fmul;
428                         rt_float[3] *= fmul;
429                         
430                         rt_float += 4;
431                 }
432         }
433 }
434
435 static void do_effect(int cfra, Sequence *seq, StripElem *se)
436 {
437         StripElem *se1, *se2, *se3;
438         float fac, facf;
439         int x, y;
440         int early_out;
441         struct SeqEffectHandle sh = get_sequence_effect(seq);
442
443         if (!sh.execute) { /* effect not supported in this version... */
444                 make_black_ibuf(se->ibuf);
445                 return;
446         }
447
448         if(seq->ipo && seq->ipo->curve.first) {
449                 do_seq_ipo(seq);
450                 fac= seq->facf0;
451                 facf= seq->facf1;
452         } else {
453                 sh.get_default_fac(seq, cfra, &fac, &facf);
454         }
455
456         if( !(G.scene->r.mode & R_FIELDS) ) facf = fac;
457
458         early_out = sh.early_out(seq, fac, facf);
459
460         if (early_out == -1) { /* no input needed */
461                 sh.execute(seq, cfra, fac, facf, se->ibuf->x, se->ibuf->y, 
462                            0, 0, 0, se->ibuf);
463                 return;
464         }
465
466         if(se->se1==0 || se->se2==0 || se->se3==0) {
467                 make_black_ibuf(se->ibuf);
468                 return;
469         }
470         
471         /* if metastrip: other se's */
472         if(se->se1->ok==2) se1= se->se1->se1;
473         else se1= se->se1;
474         
475         if(se->se2->ok==2) se2= se->se2->se1;
476         else se2= se->se2;
477         
478         if(se->se3->ok==2) se3= se->se3->se1;
479         else se3= se->se3;
480         
481         if(se1==0 || se2==0 || se3==0) {
482                 make_black_ibuf(se->ibuf);
483                 return;
484         }
485
486         switch (early_out) {
487         case 0:
488                 break;
489         case 1:
490                 if (se1->ibuf==0) {
491                         make_black_ibuf(se->ibuf);
492                         return;
493                 }
494                 if (se->ibuf != se1->ibuf) {
495                         IMB_freeImBuf(se->ibuf);
496                         se->ibuf = se1->ibuf;
497                         IMB_refImBuf(se->ibuf);
498                 }
499                 return;
500         case 2:
501                 if (se2->ibuf==0) {
502                         make_black_ibuf(se->ibuf);
503                         return;
504                 }
505                 if (se->ibuf != se2->ibuf) {
506                         IMB_freeImBuf(se->ibuf);
507                         se->ibuf = se2->ibuf;
508                         IMB_refImBuf(se->ibuf);
509                 }
510                 return;
511         }
512
513         if (se1->ibuf==0 || se2->ibuf==0 || se3->ibuf==0) {
514                 make_black_ibuf(se->ibuf);
515                 return;
516         }
517
518         x= se2->ibuf->x;
519         y= se2->ibuf->y;
520
521         if (!se1->ibuf->rect_float && se->ibuf->rect_float) {
522                 IMB_float_from_rect(se1->ibuf);
523         }
524         if (!se2->ibuf->rect_float && se->ibuf->rect_float) {
525                 IMB_float_from_rect(se2->ibuf);
526         }
527
528         if (!se1->ibuf->rect && !se->ibuf->rect_float) {
529                 IMB_rect_from_float(se1->ibuf);
530         }
531         if (!se2->ibuf->rect && !se->ibuf->rect_float) {
532                 IMB_rect_from_float(se2->ibuf);
533         }
534
535         sh.execute(seq, cfra, fac, facf, x, y, se1->ibuf, se2->ibuf, se3->ibuf,
536                    se->ibuf);
537 }
538
539 StripElem *give_stripelem(Sequence *seq, int cfra)
540 {
541         Strip *strip;
542         StripElem *se;
543         int nr;
544
545         strip= seq->strip;
546         se= strip->stripdata;
547
548         if(se==0) return 0;
549         if(seq->startdisp >cfra || seq->enddisp <= cfra) return 0;
550
551         if(seq->flag&SEQ_REVERSE_FRAMES)        {       
552                 /*reverse frame in this sequence */
553                 if(cfra <= seq->start) nr= seq->len-1;
554                 else if(cfra >= seq->start+seq->len-1) nr= 0;
555                 else nr= (seq->start + seq->len) - cfra;
556         } else {
557                 if(cfra <= seq->start) nr= 0;
558                 else if(cfra >= seq->start+seq->len-1) nr= seq->len-1;
559                 else nr= cfra-seq->start;
560         }
561         if (seq->strobe < 1.0) seq->strobe = 1.0;
562         if (seq->strobe > 1.0) {
563                 nr -= (int)fmod((double)nr, (double)seq->strobe);
564         }
565         
566         se+= nr; /* don't get confused by the increment, this is the same as strip->stripdata[nr], which works on some compilers...*/
567         se->nr= nr;
568         
569         return se;
570 }
571
572 static int evaluate_seq_frame_gen(
573         Sequence ** seq_arr, ListBase *seqbase, int cfra)
574 {
575         Sequence *seq;
576         int totseq=0;
577
578         memset(seq_arr, 0, sizeof(Sequence*) * MAXSEQ);
579
580         seq= seqbase->first;
581         while(seq) {
582                 if(seq->startdisp <=cfra && seq->enddisp > cfra) {
583                         seq_arr[seq->machine]= seq;
584                         totseq++;
585                 }
586                 seq= seq->next;
587         }
588
589         return totseq;
590 }
591
592 int evaluate_seq_frame(int cfra)
593 {
594        Editing *ed;
595        Sequence *seq_arr[MAXSEQ+1];
596
597        ed= G.scene->ed;
598        if(ed==0) return 0;
599         
600        return evaluate_seq_frame_gen(seq_arr, ed->seqbasep, cfra);
601
602 }
603
604 Sequence *get_shown_sequence(ListBase * seqbasep, int cfra, int chanshown)
605 {
606         Sequence *seq, *seqim, *seqeff;
607         Sequence *seq_arr[MAXSEQ+1];
608         int b;
609
610         seq = 0;
611
612         if (chanshown > MAXSEQ) {
613                 return 0;
614         }
615
616         if(evaluate_seq_frame_gen(seq_arr, seqbasep, cfra)) {
617                 if (chanshown > 0) {
618                         return seq_arr[chanshown];
619                 }
620
621                 /* we take the upper effect strip or 
622                    the lowest imagestrip/metastrip */
623                 seqim= seqeff= 0;
624
625                 for(b=1; b<MAXSEQ; b++) {
626                         if(seq_arr[b]) {
627                                 seq= seq_arr[b];
628                                 if(seq->type & SEQ_EFFECT) {
629                                         if(seqeff==0) seqeff= seq;
630                                         else if(seqeff->machine < seq->machine)
631                                                 seqeff= seq;
632                                 } else if (seq->type != SEQ_RAM_SOUND && seq->type != SEQ_HD_SOUND) {
633                                         if(seqim==0) seqim= seq;
634                                         else if(seqim->machine > seq->machine)
635                                                 seqim= seq;
636                                 }
637                         }
638                 }
639                 if(seqeff) seq= seqeff;
640                 else if(seqim) seq= seqim;
641                 else seq= 0;
642         }
643         
644         return seq;
645 }
646  
647 static Sequence * get_shown_seq_from_metastrip(Sequence * seqm, int cfra)
648 {
649         return get_shown_sequence(&seqm->seqbase, cfra, 0);
650 }
651
652 void set_meta_stripdata(Sequence *seqm)
653 {
654         Sequence *seq;
655         StripElem *se;
656         int a, cfra;
657
658         /* sets all ->se1 pointers in stripdata, to read the ibuf from it */
659
660         se= seqm->strip->stripdata;
661         for(a=0; a<seqm->len; a++, se++) {
662                 cfra= a+seqm->start;
663                 seq = get_shown_seq_from_metastrip(seqm, cfra);
664                 if (seq) {
665                         se->se1= give_stripelem(seq, cfra);
666                 } else { 
667                         se->se1= 0;
668                 }
669         }
670 }
671
672 static void do_build_seq_ibuf(Sequence * seq, int cfra)
673 {
674         StripElem *se = seq->curelem;
675         char name[FILE_MAXDIR+FILE_MAXFILE];
676
677         if(seq->type == SEQ_META) {
678                 se->ok= 2;
679                 if(se->se1==0) set_meta_stripdata(seq);
680                 if(se->se1) {
681                         se->ibuf= se->se1->ibuf;
682                 }
683         } else if(seq->type == SEQ_RAM_SOUND || seq->type == SEQ_HD_SOUND) {
684                 se->ok= 2;
685         } else if(seq->type & SEQ_EFFECT) {
686                         
687                 /* test if image is too small or discarded from cache: reload */
688                 if(se->ibuf) {
689                         if(se->ibuf->x < seqrectx || se->ibuf->y < seqrecty || !(se->ibuf->rect || se->ibuf->rect_float)) {
690                                 IMB_freeImBuf(se->ibuf);
691                                 se->ibuf= 0;
692                         }
693                 }
694                         
695                 /* should the effect be recalculated? */
696                 
697                 if(se->ibuf==0 
698                    || (seq->seq1 && se->se1 != seq->seq1->curelem) 
699                    || (seq->seq2 && se->se2 != seq->seq2->curelem) 
700                    || (seq->seq3 && se->se3 != seq->seq3->curelem)) {
701                         if (seq->seq1) se->se1= seq->seq1->curelem;
702                         if (seq->seq2) se->se2= seq->seq2->curelem;
703                         if (seq->seq3) se->se3= seq->seq3->curelem;
704                         
705                         if(se->ibuf==NULL) {
706                                 /* if one of two first inputs are rectfloat, output is float too */
707                                 if((se->se1 && se->se1->ibuf && se->se1->ibuf->rect_float) ||
708                                    (se->se2 && se->se2->ibuf && se->se2->ibuf->rect_float))
709                                         se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
710                                 else
711                                         se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
712                         }
713                         
714                         do_effect(cfra, seq, se);
715                 }
716                 
717                 /* test size */
718                 if(se->ibuf) {
719                         if(se->ibuf->x != seqrectx || se->ibuf->y != seqrecty ) {
720                                 if(G.scene->r.mode & R_OSA) {
721                                         IMB_scaleImBuf(se->ibuf, (short)seqrectx, (short)seqrecty);
722                                 } else {
723                                         IMB_scalefastImBuf(se->ibuf, (short)seqrectx, (short)seqrecty);
724                                 }
725                         }
726                 }
727         } else if(seq->type < SEQ_EFFECT) {
728                 if(se->ibuf) {
729                         /* test if image too small 
730                            or discarded from cache: reload */
731                         if(se->ibuf->x < seqrectx || se->ibuf->y < seqrecty || !(se->ibuf->rect || se->ibuf->rect_float)) {
732                                 IMB_freeImBuf(se->ibuf);
733                                 se->ibuf= 0;
734                                 se->ok= 1;
735                         }
736                 }
737                 
738                 if(seq->type==SEQ_IMAGE) {
739                         if(se->ok && se->ibuf==0) {
740                                 /* if playanim or render: 
741                                    no waitcursor */
742                                 if((G.f & G_PLAYANIM)==0) 
743                                         waitcursor(1);
744                                 
745                                 strncpy(name, seq->strip->dir, FILE_MAXDIR-1);
746                                 strncat(name, se->name, FILE_MAXFILE);
747                                 BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
748                                 se->ibuf= IMB_loadiffname(name, IB_rect);
749                                 
750                                 if((G.f & G_PLAYANIM)==0) 
751                                         waitcursor(0);
752                                 
753                                 if(se->ibuf==0) se->ok= 0;
754                                 else {
755                                         if(seq->flag & SEQ_MAKE_PREMUL) {
756                                                 if(se->ibuf->depth==32 && se->ibuf->zbuf==0) converttopremul(se->ibuf);
757                                         }
758                                         seq->strip->orx= se->ibuf->x;
759                                         seq->strip->ory= se->ibuf->y;
760                                         if(seq->flag & SEQ_FILTERY) IMB_filtery(se->ibuf);
761                                         if(seq->mul==0.0) seq->mul= 1.0;
762                                         if(seq->mul != 1.0) multibuf(se->ibuf, seq->mul);
763                                 }
764                         }
765                 }
766                 else if(seq->type==SEQ_MOVIE) {
767                         if(se->ok && se->ibuf==0) {
768                                 if(seq->anim==0) {
769                                         strncpy(name, seq->strip->dir, FILE_MAXDIR-1);
770                                         strncat(name, seq->strip->stripdata->name, FILE_MAXFILE-1);
771                                         BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
772                                         
773                                         seq->anim = openanim(name, IB_rect);
774                                 }
775                                 if(seq->anim) {
776                                         IMB_anim_set_preseek(seq->anim, seq->anim_preseek);
777                                         se->ibuf = IMB_anim_absolute(seq->anim, se->nr);
778                                 }
779                                 
780                                 if(se->ibuf==0) se->ok= 0;
781                                 else {
782                                         if(seq->flag & SEQ_MAKE_PREMUL) {
783                                                 if(se->ibuf->depth==32) converttopremul(se->ibuf);
784                                         }
785                                         seq->strip->orx= se->ibuf->x;
786                                         seq->strip->ory= se->ibuf->y;
787                                         if(seq->flag & SEQ_FILTERY) IMB_filtery(se->ibuf);
788                                         if(seq->mul==0.0) seq->mul= 1.0;
789                                         if(seq->mul != 1.0) multibuf(se->ibuf, seq->mul);
790                                 }
791                         }
792                 } else if(seq->type==SEQ_SCENE && se->ibuf==NULL && seq->scene) {       // scene can be NULL after deletions
793                         int oldcfra = CFRA;
794                         Scene *sce= seq->scene, *oldsce= G.scene;
795                         Render *re;
796                         RenderResult rres;
797                         int doseq, rendering= G.rendering;
798                         char scenename[64];
799                         
800                         waitcursor(1);
801                         
802                         /* Hack! This function can be called from do_render_seq(), in that case
803                            the seq->scene can already have a Render initialized with same name, 
804                            so we have to use a default name. (compositor uses G.scene name to
805                            find render).
806                            However, when called from within the UI (image preview in sequencer)
807                            we do want to use scene Render, that way the render result is defined
808                            for display in render/imagewindow */
809                         if(rendering) {
810                                 BLI_strncpy(scenename, sce->id.name+2, 64);
811                                 strcpy(sce->id.name+2, " do_build_seq_ibuf");
812                         }
813                         re= RE_NewRender(sce->id.name);
814                         
815                         /* prevent eternal loop */
816                         doseq= sce->r.scemode & R_DOSEQ;
817                         sce->r.scemode &= ~R_DOSEQ;
818                         
819                         BIF_init_render_callbacks(re, 0);       /* 0= no display callbacks */
820                         
821                         /* hrms, set_scene still needed? work on that... */
822                         if(sce!=oldsce) set_scene_bg(sce);
823                         RE_BlenderFrame(re, sce, seq->sfra + se->nr);
824                         if(sce!=oldsce) set_scene_bg(oldsce);
825                         
826                         /* UGLY WARNING, it is set to zero in  RE_BlenderFrame */
827                         G.rendering= rendering;
828                         if(rendering)
829                                 BLI_strncpy(sce->id.name+2, scenename, 64);
830                         
831                         RE_GetResultImage(re, &rres);
832                         
833                         if(rres.rectf) {
834                                 se->ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rectfloat, 0);
835                                 memcpy(se->ibuf->rect_float, rres.rectf, 4*sizeof(float)*rres.rectx*rres.recty);
836                                 if(rres.rectz) {
837                                         addzbuffloatImBuf(se->ibuf);
838                                         memcpy(se->ibuf->zbuf_float, rres.rectz, sizeof(float)*rres.rectx*rres.recty);
839                                 }
840                         }
841                         
842                         BIF_end_render_callbacks();
843                         
844                         /* restore */
845                         sce->r.scemode |= doseq;
846                         
847                         if((G.f & G_PLAYANIM)==0) /* bad, is set on do_render_seq */
848                                 waitcursor(0);
849                         CFRA = oldcfra;
850                 }
851                 
852                 /* size test */
853                 if(se->ibuf) {
854                         if(se->ibuf->x != seqrectx || se->ibuf->y != seqrecty ) {
855                                 
856                                 if (0) { // G.scene->r.mode & R_FIELDS) {
857                                         
858                                         if (seqrecty > 288) 
859                                                 IMB_scalefieldImBuf(se->ibuf, (short)seqrectx, (short)seqrecty);
860                                         else {
861                                                 IMB_de_interlace(se->ibuf);
862                                                 
863                                                 if(G.scene->r.mode & R_OSA)
864                                                         IMB_scaleImBuf(se->ibuf, (short)seqrectx, (short)seqrecty);
865                                                 else
866                                                         IMB_scalefastImBuf(se->ibuf, (short)seqrectx, (short)seqrecty);
867                                         }
868                                 }
869                                 else {
870                                         if(G.scene->r.mode & R_OSA)
871                                                 IMB_scaleImBuf(se->ibuf,(short)seqrectx, (short)seqrecty);
872                                         else
873                                                 IMB_scalefastImBuf(se->ibuf, (short)seqrectx, (short)seqrecty);
874                                 }
875                         }
876                         
877                 }
878         }
879         if (se->ibuf) {
880                 IMB_cache_limiter_insert(se->ibuf);
881                 IMB_cache_limiter_ref(se->ibuf);
882                 IMB_cache_limiter_touch(se->ibuf);
883         }
884 }
885
886 static void do_build_seq_recursively(Sequence * seq, int cfra);
887
888 static void do_effect_seq_recursively(int cfra, Sequence * seq, StripElem *se)
889 {
890         float fac, facf;
891         struct SeqEffectHandle sh = get_sequence_effect(seq);
892         int early_out;
893
894         if(seq->ipo && seq->ipo->curve.first) {
895                 do_seq_ipo(seq);
896                 fac= seq->facf0;
897                 facf= seq->facf1;
898         } else {
899                 sh.get_default_fac(seq, cfra, &fac, &facf);
900         } 
901
902         if( G.scene->r.mode & R_FIELDS ); else facf= fac;
903         
904         early_out = sh.early_out(seq, fac, facf);
905         switch (early_out) {
906         case -1:
907                 /* no input needed */
908                 break;
909         case 0:
910                 do_build_seq_recursively(seq->seq1, cfra);
911                 do_build_seq_recursively(seq->seq2, cfra);
912                 break;
913         case 1:
914                 do_build_seq_recursively(seq->seq1, cfra);
915                 break;
916         case 2:
917                 do_build_seq_recursively(seq->seq2, cfra);
918                 break;
919         }
920
921         if (seq->seq3) {
922                 do_build_seq_recursively(seq->seq3, cfra);
923         }
924
925         do_build_seq_ibuf(seq, cfra);
926
927         /* children are not needed anymore ... */
928
929         switch (early_out) {
930         case 0:
931                 if (seq->seq1->curelem && seq->seq1->curelem->ibuf)
932                         IMB_cache_limiter_unref(seq->seq1->curelem->ibuf);
933                 if (seq->seq2->curelem && seq->seq2->curelem->ibuf)
934                         IMB_cache_limiter_unref(seq->seq2->curelem->ibuf);
935                 break;
936         case 1:
937                 if (seq->seq1->curelem && seq->seq1->curelem->ibuf)
938                         IMB_cache_limiter_unref(seq->seq1->curelem->ibuf);
939                 break;
940         case 2:
941                 if (seq->seq2->curelem && seq->seq2->curelem->ibuf)
942                         IMB_cache_limiter_unref(seq->seq2->curelem->ibuf);
943                 break;
944         }
945         if (seq->seq3) {
946                 if (seq->seq3->curelem && seq->seq3->curelem->ibuf)
947                         IMB_cache_limiter_unref(seq->seq3->curelem->ibuf);
948         }
949 }
950
951 static void do_build_seq_recursively_impl(Sequence * seq, int cfra)
952 {
953         StripElem *se;
954
955         se = seq->curelem = give_stripelem(seq, cfra);
956
957         if(se) {
958                 if(seq->seqbase.first) {
959                         Sequence * seqmshown= get_shown_seq_from_metastrip(seq, cfra);
960                         if (seqmshown) {
961                                 if(cfra< seq->start) 
962                                         do_build_seq_recursively(seqmshown, seq->start);
963                                 else if(cfra> seq->start+seq->len-1) 
964                                         do_build_seq_recursively(seqmshown, seq->start + seq->len-1);
965                                 else do_build_seq_recursively(seqmshown, cfra);
966                         }
967                 }
968
969                 if (seq->type & SEQ_EFFECT) {
970                         do_effect_seq_recursively(cfra, seq, se);
971                 } else {
972                         do_build_seq_ibuf(seq, cfra);
973                 }
974         }
975 }
976
977 /* FIXME:
978    
979 If cfra was float throughout blender (especially in the render
980 pipeline) one could even _render_ with subframe precision
981 instead of faking using the blend code below...
982
983 */
984
985 static void do_handle_speed_effect(Sequence * seq, int cfra)
986 {
987         SpeedControlVars * s = (SpeedControlVars *)seq->effectdata;
988         int nr = cfra - seq->start;
989         float f_cfra;
990         int cfra_left;
991         int cfra_right;
992         StripElem * se = 0;
993         StripElem * se1 = 0;
994         StripElem * se2 = 0;
995         
996         sequence_effect_speed_rebuild_map(seq, 0);
997         
998         f_cfra = seq->start + s->frameMap[nr];
999         
1000         cfra_left = (int) floor(f_cfra);
1001         cfra_right = (int) ceil(f_cfra);
1002
1003         se = seq->curelem = give_stripelem(seq, cfra);
1004
1005         if (cfra_left == cfra_right || 
1006             (s->flags & SEQ_SPEED_BLEND) == 0) {
1007                 if(se->ibuf) {
1008                         if(se->ibuf->x < seqrectx || se->ibuf->y < seqrecty 
1009                            || !(se->ibuf->rect || se->ibuf->rect_float)) {
1010                                 IMB_freeImBuf(se->ibuf);
1011                                 se->ibuf= 0;
1012                         }
1013                 }
1014
1015                 if (se->ibuf == NULL) {
1016                         do_build_seq_recursively_impl(seq->seq1, cfra_left);
1017
1018                         se1 = seq->seq1->curelem;
1019
1020                         if((se1 && se1->ibuf && se1->ibuf->rect_float))
1021                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
1022                         else
1023                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
1024
1025                         if (se1 == 0 || se1->ibuf == 0) {
1026                                 make_black_ibuf(se->ibuf);
1027                         } else {
1028                                 if (se->ibuf != se1->ibuf) {
1029                                         if (se->ibuf) {
1030                                                 IMB_freeImBuf(se->ibuf);
1031                                         }
1032
1033                                         se->ibuf = se1->ibuf;
1034                                         IMB_refImBuf(se->ibuf);
1035                                 }
1036                         }
1037                 }
1038         } else {
1039                 struct SeqEffectHandle sh;
1040
1041                 if(se->ibuf) {
1042                         if(se->ibuf->x < seqrectx || se->ibuf->y < seqrecty 
1043                            || !(se->ibuf->rect || se->ibuf->rect_float)) {
1044                                 IMB_freeImBuf(se->ibuf);
1045                                 se->ibuf= 0;
1046                         }
1047                 }
1048
1049                 if (se->ibuf == NULL) {
1050                         do_build_seq_recursively_impl(seq->seq1, cfra_left);
1051                         se1 = seq->seq1->curelem;
1052                         do_build_seq_recursively_impl(seq->seq1, cfra_right);
1053                         se2 = seq->seq1->curelem;
1054
1055
1056                         if((se1 && se1->ibuf && se1->ibuf->rect_float))
1057                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
1058                         else
1059                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
1060                         
1061                         if (!se1 || !se2) {
1062                                 make_black_ibuf(se->ibuf);
1063                         } else {
1064                                 sh = get_sequence_effect(seq);
1065
1066                                 sh.execute(seq, cfra, 
1067                                            f_cfra - (float) cfra_left, 
1068                                            f_cfra - (float) cfra_left, 
1069                                            se->ibuf->x, se->ibuf->y, 
1070                                            se1->ibuf, se2->ibuf, 0, se->ibuf);
1071                         }
1072                 }
1073
1074         }
1075
1076         /* caller expects this to be referenced, so do it! */
1077         if (se->ibuf) {
1078                 IMB_cache_limiter_insert(se->ibuf);
1079                 IMB_cache_limiter_ref(se->ibuf);
1080                 IMB_cache_limiter_touch(se->ibuf);
1081         }
1082
1083         /* children are no longer needed */
1084         if (se1 && se1->ibuf)
1085                 IMB_cache_limiter_unref(se1->ibuf);
1086         if (se2 && se2->ibuf)
1087                 IMB_cache_limiter_unref(se2->ibuf);
1088 }
1089
1090 /* 
1091  * build all ibufs recursively
1092  * 
1093  * if successfull, seq->curelem->ibuf contains the (referenced!) imbuf
1094  * that means: you _must_ call 
1095  *
1096  * IMB_cache_limiter_unref(seq->curelem->ibuf);
1097  * 
1098  * if seq->curelem exists!
1099  * 
1100  */
1101
1102 static void do_build_seq_recursively(Sequence * seq, int cfra)
1103 {
1104         if (seq->type == SEQ_SPEED) {
1105                 do_handle_speed_effect(seq, cfra);
1106         } else {
1107                 do_build_seq_recursively_impl(seq, cfra);
1108         }
1109 }
1110
1111 ImBuf *give_ibuf_seq(int rectx, int recty, int cfra, int chanshown)
1112 {
1113         Sequence *seqfirst=0;
1114         Editing *ed;
1115         int count;
1116         ListBase *seqbasep;
1117
1118         ed= G.scene->ed;
1119         if(ed==0) return 0;
1120
1121         count = BLI_countlist(&ed->metastack);
1122         if((chanshown < 0) && (count > 0)) {
1123                 count = MAX2(count + chanshown, 0);
1124                 seqbasep= ((MetaStack*)BLI_findlink(&ed->metastack, count))->oldbasep;
1125         } else {
1126                 seqbasep= ed->seqbasep;
1127         }
1128
1129         seqrectx= rectx;        /* bad bad global! */
1130         seqrecty= recty;
1131
1132         seqfirst = get_shown_sequence(seqbasep, cfra, chanshown);
1133
1134         if (!seqfirst) {
1135                 return 0;
1136         }
1137
1138         do_build_seq_recursively(seqfirst, cfra);
1139
1140         if(!seqfirst->curelem) { 
1141                 return 0;
1142         }
1143
1144         IMB_cache_limiter_unref(seqfirst->curelem->ibuf);
1145
1146         return seqfirst->curelem->ibuf;
1147
1148 }
1149
1150 /* Functions to free imbuf and anim data on changes */
1151
1152 static void free_imbuf_strip_elem(StripElem *se)
1153 {
1154         if (se->ibuf) {
1155                 if (se->ok != 2)
1156                         IMB_freeImBuf(se->ibuf);
1157                 se->ibuf= 0;
1158                 se->ok= 1;
1159                 se->se1= se->se2= se->se3= 0;
1160         }
1161 }
1162
1163 static void free_anim_seq(Sequence *seq)
1164 {
1165         if(seq->anim) {
1166                 IMB_free_anim(seq->anim);
1167                 seq->anim = 0;
1168         }
1169 }
1170
1171 void free_imbuf_seq_except(int cfra)
1172 {
1173         Editing *ed= G.scene->ed;
1174         Sequence *seq;
1175         StripElem *se;
1176         int a;
1177
1178         if(ed==0) return;
1179
1180         WHILE_SEQ(&ed->seqbase) {
1181                 if(seq->strip) {
1182                         for(a=0, se= seq->strip->stripdata; a<seq->len; a++, se++)
1183                                 if(se!=seq->curelem)
1184                                         free_imbuf_strip_elem(se);
1185
1186                         if(seq->type==SEQ_MOVIE)
1187                                 if(seq->startdisp > cfra || seq->enddisp < cfra)
1188                                         free_anim_seq(seq);
1189                 }
1190         }
1191         END_SEQ
1192 }
1193
1194 void free_imbuf_seq()
1195 {
1196         Editing *ed= G.scene->ed;
1197         Sequence *seq;
1198         StripElem *se;
1199         int a;
1200
1201         if(ed==0) return;
1202
1203         WHILE_SEQ(&ed->seqbase) {
1204                 if(seq->strip) {
1205                         for(a=0, se= seq->strip->stripdata; a<seq->len; a++, se++)
1206                                 free_imbuf_strip_elem(se);
1207
1208                         if(seq->type==SEQ_MOVIE)
1209                                 free_anim_seq(seq);
1210                         if(seq->type==SEQ_SPEED) {
1211                                 sequence_effect_speed_rebuild_map(seq, 1);
1212                         }
1213                 }
1214         }
1215         END_SEQ
1216 }
1217
1218 void free_imbuf_seq_with_ipo(struct Ipo *ipo)
1219 {
1220         /* force update of all sequences with this ipo, on ipo changes */
1221         Editing *ed= G.scene->ed;
1222         Sequence *seq;
1223
1224         if(ed==0) return;
1225
1226         WHILE_SEQ(&ed->seqbase) {
1227                 if(seq->ipo == ipo) {
1228                         update_changed_seq_and_deps(seq, 0, 1);
1229                         if(seq->type == SEQ_SPEED) {
1230                                 sequence_effect_speed_rebuild_map(seq, 1);
1231                         }
1232                 }
1233         }
1234         END_SEQ
1235 }
1236
1237 static int update_changed_seq_recurs(Sequence *seq, Sequence *changed_seq, int len_change, int ibuf_change)
1238 {
1239         Sequence *subseq;
1240         int a, free_imbuf = 0;
1241         StripElem *se;
1242
1243         /* recurs downwards to see if this seq depends on the changed seq */
1244
1245         if(seq == NULL)
1246                 return 0;
1247
1248         if(seq == changed_seq)
1249                 free_imbuf = 1;
1250         
1251         for(subseq=seq->seqbase.first; subseq; subseq=subseq->next)
1252                 if(update_changed_seq_recurs(subseq, changed_seq, len_change, ibuf_change))
1253                         free_imbuf = 1;
1254         
1255         if(seq->seq1)
1256                 if(update_changed_seq_recurs(seq->seq1, changed_seq, len_change, ibuf_change))
1257                         free_imbuf = 1;
1258         if(seq->seq2 && (seq->seq2 != seq->seq1))
1259                 if(update_changed_seq_recurs(seq->seq2, changed_seq, len_change, ibuf_change))
1260                         free_imbuf = 1;
1261         if(seq->seq3 && (seq->seq3 != seq->seq1) && (seq->seq3 != seq->seq2))
1262                 if(update_changed_seq_recurs(seq->seq3, changed_seq, len_change, ibuf_change))
1263                         free_imbuf = 1;
1264         
1265         if(free_imbuf) {
1266                 if(ibuf_change) {
1267                         for(a=0, se= seq->strip->stripdata; a<seq->len; a++, se++)
1268                                 free_imbuf_strip_elem(se);
1269                 
1270                         if(seq->type==SEQ_MOVIE)
1271                                 free_anim_seq(seq);
1272                         if(seq->type == SEQ_SPEED) {
1273                                 sequence_effect_speed_rebuild_map(seq, 1);
1274                         }
1275                 }
1276
1277                 if(len_change)
1278                         calc_sequence(seq);
1279         }
1280         
1281         return free_imbuf;
1282 }
1283
1284 void update_changed_seq_and_deps(Sequence *changed_seq, int len_change, int ibuf_change)
1285 {
1286         Editing *ed= G.scene->ed;
1287         Sequence *seq;
1288
1289         if (!ed) return;
1290
1291         for (seq=ed->seqbase.first; seq; seq=seq->next)
1292                 update_changed_seq_recurs(seq, changed_seq, len_change, ibuf_change);
1293 }
1294
1295 /* bad levell call... */
1296 void do_render_seq(RenderResult *rr, int cfra)
1297 {
1298         ImBuf *ibuf;
1299
1300         G.f |= G_PLAYANIM;      /* waitcursor patch */
1301
1302         ibuf= give_ibuf_seq(rr->rectx, rr->recty, cfra, 0);
1303         
1304         if(ibuf) {
1305                 
1306                 if(ibuf->rect_float) {
1307                         if (!rr->rectf)
1308                                 rr->rectf= MEM_mallocN(4*sizeof(float)*rr->rectx*rr->recty, "render_seq rectf");
1309                         
1310                         memcpy(rr->rectf, ibuf->rect_float, 4*sizeof(float)*rr->rectx*rr->recty);
1311                         
1312                         /* TSK! Since sequence render doesn't free the *rr render result, the old rect32
1313                            can hang around when sequence render has rendered a 32 bits one before */
1314                         if(rr->rect32) {
1315                                 MEM_freeN(rr->rect32);
1316                                 rr->rect32= NULL;
1317                         }
1318                 }
1319                 else if(ibuf->rect) {
1320                         if (!rr->rect32)
1321                                 rr->rect32= MEM_mallocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
1322
1323                         memcpy(rr->rect32, ibuf->rect, 4*rr->rectx*rr->recty);
1324
1325                         /* if (ibuf->zbuf) { */
1326                         /*      if (R.rectz) freeN(R.rectz); */
1327                         /*      R.rectz = BLI_dupallocN(ibuf->zbuf); */
1328                         /* } */
1329                 }
1330                 
1331                 /* Let the cache limitor take care of this (schlaile) */
1332                 /* While render let's keep all memory available for render (ton) */
1333                 free_imbuf_seq_except(cfra);
1334         }
1335         else {
1336                 /* render result is delivered empty in most cases, nevertheless we handle all cases */
1337                 if (rr->rectf)
1338                         memset(rr->rectf, 0, 4*sizeof(float)*rr->rectx*rr->recty);
1339                 else if (rr->rect32)
1340                         memset(rr->rect32, 0, 4*rr->rectx*rr->recty);
1341                 else
1342                         rr->rect32= MEM_callocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
1343         }
1344         
1345         G.f &= ~G_PLAYANIM;
1346
1347 }