Two in one:
[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
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==NULL) return;
197         set_last_seq(NULL);     /* clear_last_seq doesnt work, it screws up free_sequence */
198
199         WHILE_SEQ(&ed->seqbase) {
200                 free_sequence(seq);
201         }
202         END_SEQ
203
204         while( (ms= ed->metastack.first) ) {
205                 BLI_remlink(&ed->metastack, ms);
206                 MEM_freeN(ms);
207         }
208
209         MEM_freeN(ed);
210
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                 int unref_meta = FALSE;
959                 if(seq->seqbase.first) {
960                         Sequence * seqmshown= get_shown_seq_from_metastrip(seq, cfra);
961                         if (seqmshown) {
962                                 if(cfra< seq->start) 
963                                         do_build_seq_recursively(seqmshown, seq->start);
964                                 else if(cfra> seq->start+seq->len-1) 
965                                         do_build_seq_recursively(seqmshown, seq->start + seq->len-1);
966                                 else do_build_seq_recursively(seqmshown, cfra);
967
968                                 unref_meta = TRUE;
969                         }
970                 }
971
972                 if (seq->type & SEQ_EFFECT) {
973                         do_effect_seq_recursively(cfra, seq, se);
974                 } else {
975                         do_build_seq_ibuf(seq, cfra);
976                 }
977
978                 if(unref_meta && seq->curelem->ibuf) {
979                         IMB_cache_limiter_unref(seq->curelem->ibuf);
980                 }
981         }
982 }
983
984 /* FIXME:
985    
986 If cfra was float throughout blender (especially in the render
987 pipeline) one could even _render_ with subframe precision
988 instead of faking using the blend code below...
989
990 */
991
992 static void do_handle_speed_effect(Sequence * seq, int cfra)
993 {
994         SpeedControlVars * s = (SpeedControlVars *)seq->effectdata;
995         int nr = cfra - seq->start;
996         float f_cfra;
997         int cfra_left;
998         int cfra_right;
999         StripElem * se = 0;
1000         StripElem * se1 = 0;
1001         StripElem * se2 = 0;
1002         
1003         sequence_effect_speed_rebuild_map(seq, 0);
1004         
1005         f_cfra = seq->start + s->frameMap[nr];
1006         
1007         cfra_left = (int) floor(f_cfra);
1008         cfra_right = (int) ceil(f_cfra);
1009
1010         se = seq->curelem = give_stripelem(seq, cfra);
1011
1012         if (cfra_left == cfra_right || 
1013             (s->flags & SEQ_SPEED_BLEND) == 0) {
1014                 if(se->ibuf) {
1015                         if(se->ibuf->x < seqrectx || se->ibuf->y < seqrecty 
1016                            || !(se->ibuf->rect || se->ibuf->rect_float)) {
1017                                 IMB_freeImBuf(se->ibuf);
1018                                 se->ibuf= 0;
1019                         }
1020                 }
1021
1022                 if (se->ibuf == NULL) {
1023                         do_build_seq_recursively_impl(seq->seq1, cfra_left);
1024
1025                         se1 = seq->seq1->curelem;
1026
1027                         if((se1 && se1->ibuf && se1->ibuf->rect_float))
1028                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
1029                         else
1030                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
1031
1032                         if (se1 == 0 || se1->ibuf == 0) {
1033                                 make_black_ibuf(se->ibuf);
1034                         } else {
1035                                 if (se->ibuf != se1->ibuf) {
1036                                         if (se->ibuf) {
1037                                                 IMB_freeImBuf(se->ibuf);
1038                                         }
1039
1040                                         se->ibuf = se1->ibuf;
1041                                         IMB_refImBuf(se->ibuf);
1042                                 }
1043                         }
1044                 }
1045         } else {
1046                 struct SeqEffectHandle sh;
1047
1048                 if(se->ibuf) {
1049                         if(se->ibuf->x < seqrectx || se->ibuf->y < seqrecty 
1050                            || !(se->ibuf->rect || se->ibuf->rect_float)) {
1051                                 IMB_freeImBuf(se->ibuf);
1052                                 se->ibuf= 0;
1053                         }
1054                 }
1055
1056                 if (se->ibuf == NULL) {
1057                         do_build_seq_recursively_impl(seq->seq1, cfra_left);
1058                         se1 = seq->seq1->curelem;
1059                         do_build_seq_recursively_impl(seq->seq1, cfra_right);
1060                         se2 = seq->seq1->curelem;
1061
1062
1063                         if((se1 && se1->ibuf && se1->ibuf->rect_float))
1064                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
1065                         else
1066                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
1067                         
1068                         if (!se1 || !se2) {
1069                                 make_black_ibuf(se->ibuf);
1070                         } else {
1071                                 sh = get_sequence_effect(seq);
1072
1073                                 sh.execute(seq, cfra, 
1074                                            f_cfra - (float) cfra_left, 
1075                                            f_cfra - (float) cfra_left, 
1076                                            se->ibuf->x, se->ibuf->y, 
1077                                            se1->ibuf, se2->ibuf, 0, se->ibuf);
1078                         }
1079                 }
1080
1081         }
1082
1083         /* caller expects this to be referenced, so do it! */
1084         if (se->ibuf) {
1085                 IMB_cache_limiter_insert(se->ibuf);
1086                 IMB_cache_limiter_ref(se->ibuf);
1087                 IMB_cache_limiter_touch(se->ibuf);
1088         }
1089
1090         /* children are no longer needed */
1091         if (se1 && se1->ibuf)
1092                 IMB_cache_limiter_unref(se1->ibuf);
1093         if (se2 && se2->ibuf)
1094                 IMB_cache_limiter_unref(se2->ibuf);
1095 }
1096
1097 /* 
1098  * build all ibufs recursively
1099  * 
1100  * if successfull, seq->curelem->ibuf contains the (referenced!) imbuf
1101  * that means: you _must_ call 
1102  *
1103  * IMB_cache_limiter_unref(seq->curelem->ibuf);
1104  * 
1105  * if seq->curelem exists!
1106  * 
1107  */
1108
1109 static void do_build_seq_recursively(Sequence * seq, int cfra)
1110 {
1111         if (seq->type == SEQ_SPEED) {
1112                 do_handle_speed_effect(seq, cfra);
1113         } else {
1114                 do_build_seq_recursively_impl(seq, cfra);
1115         }
1116 }
1117
1118 ImBuf *give_ibuf_seq(int rectx, int recty, int cfra, int chanshown)
1119 {
1120         Sequence *seqfirst=0;
1121         Editing *ed;
1122         int count;
1123         ListBase *seqbasep;
1124
1125         ed= G.scene->ed;
1126         if(ed==0) return 0;
1127
1128         count = BLI_countlist(&ed->metastack);
1129         if((chanshown < 0) && (count > 0)) {
1130                 count = MAX2(count + chanshown, 0);
1131                 seqbasep= ((MetaStack*)BLI_findlink(&ed->metastack, count))->oldbasep;
1132         } else {
1133                 seqbasep= ed->seqbasep;
1134         }
1135
1136         seqrectx= rectx;        /* bad bad global! */
1137         seqrecty= recty;
1138
1139         seqfirst = get_shown_sequence(seqbasep, cfra, chanshown);
1140
1141         if (!seqfirst) {
1142                 return 0;
1143         }
1144
1145         do_build_seq_recursively(seqfirst, cfra);
1146
1147         if(!seqfirst->curelem) { 
1148                 return 0;
1149         }
1150
1151         if (seqfirst->curelem->ibuf) {
1152                 IMB_cache_limiter_unref(seqfirst->curelem->ibuf);
1153         }
1154
1155         return seqfirst->curelem->ibuf;
1156
1157 }
1158
1159 /* Functions to free imbuf and anim data on changes */
1160
1161 static void free_imbuf_strip_elem(StripElem *se)
1162 {
1163         if (se->ibuf) {
1164                 if (se->ok != 2)
1165                         IMB_freeImBuf(se->ibuf);
1166                 se->ibuf= 0;
1167                 se->ok= 1;
1168                 se->se1= se->se2= se->se3= 0;
1169         }
1170 }
1171
1172 static void free_anim_seq(Sequence *seq)
1173 {
1174         if(seq->anim) {
1175                 IMB_free_anim(seq->anim);
1176                 seq->anim = 0;
1177         }
1178 }
1179
1180 void free_imbuf_seq_except(int cfra)
1181 {
1182         Editing *ed= G.scene->ed;
1183         Sequence *seq;
1184         StripElem *se;
1185         int a;
1186
1187         if(ed==0) return;
1188
1189         WHILE_SEQ(&ed->seqbase) {
1190                 if(seq->strip) {
1191                         for(a=0, se= seq->strip->stripdata; a<seq->len; a++, se++)
1192                                 if(se!=seq->curelem)
1193                                         free_imbuf_strip_elem(se);
1194
1195                         if(seq->type==SEQ_MOVIE)
1196                                 if(seq->startdisp > cfra || seq->enddisp < cfra)
1197                                         free_anim_seq(seq);
1198                 }
1199         }
1200         END_SEQ
1201 }
1202
1203 void free_imbuf_seq()
1204 {
1205         Editing *ed= G.scene->ed;
1206         Sequence *seq;
1207         StripElem *se;
1208         int a;
1209
1210         if(ed==0) return;
1211
1212         WHILE_SEQ(&ed->seqbase) {
1213                 if(seq->strip) {
1214                         for(a=0, se= seq->strip->stripdata; a<seq->len; a++, se++)
1215                                 free_imbuf_strip_elem(se);
1216
1217                         if(seq->type==SEQ_MOVIE)
1218                                 free_anim_seq(seq);
1219                         if(seq->type==SEQ_SPEED) {
1220                                 sequence_effect_speed_rebuild_map(seq, 1);
1221                         }
1222                 }
1223         }
1224         END_SEQ
1225 }
1226
1227 void free_imbuf_seq_with_ipo(struct Ipo *ipo)
1228 {
1229         /* force update of all sequences with this ipo, on ipo changes */
1230         Editing *ed= G.scene->ed;
1231         Sequence *seq;
1232
1233         if(ed==0) return;
1234
1235         WHILE_SEQ(&ed->seqbase) {
1236                 if(seq->ipo == ipo) {
1237                         update_changed_seq_and_deps(seq, 0, 1);
1238                         if(seq->type == SEQ_SPEED) {
1239                                 sequence_effect_speed_rebuild_map(seq, 1);
1240                         }
1241                 }
1242         }
1243         END_SEQ
1244 }
1245
1246 static int update_changed_seq_recurs(Sequence *seq, Sequence *changed_seq, int len_change, int ibuf_change)
1247 {
1248         Sequence *subseq;
1249         int a, free_imbuf = 0;
1250         StripElem *se;
1251
1252         /* recurs downwards to see if this seq depends on the changed seq */
1253
1254         if(seq == NULL)
1255                 return 0;
1256
1257         if(seq == changed_seq)
1258                 free_imbuf = 1;
1259         
1260         for(subseq=seq->seqbase.first; subseq; subseq=subseq->next)
1261                 if(update_changed_seq_recurs(subseq, changed_seq, len_change, ibuf_change))
1262                         free_imbuf = 1;
1263         
1264         if(seq->seq1)
1265                 if(update_changed_seq_recurs(seq->seq1, changed_seq, len_change, ibuf_change))
1266                         free_imbuf = 1;
1267         if(seq->seq2 && (seq->seq2 != seq->seq1))
1268                 if(update_changed_seq_recurs(seq->seq2, changed_seq, len_change, ibuf_change))
1269                         free_imbuf = 1;
1270         if(seq->seq3 && (seq->seq3 != seq->seq1) && (seq->seq3 != seq->seq2))
1271                 if(update_changed_seq_recurs(seq->seq3, changed_seq, len_change, ibuf_change))
1272                         free_imbuf = 1;
1273         
1274         if(free_imbuf) {
1275                 if(ibuf_change) {
1276                         for(a=0, se= seq->strip->stripdata; a<seq->len; a++, se++)
1277                                 free_imbuf_strip_elem(se);
1278                 
1279                         if(seq->type==SEQ_MOVIE)
1280                                 free_anim_seq(seq);
1281                         if(seq->type == SEQ_SPEED) {
1282                                 sequence_effect_speed_rebuild_map(seq, 1);
1283                         }
1284                 }
1285
1286                 if(len_change)
1287                         calc_sequence(seq);
1288         }
1289         
1290         return free_imbuf;
1291 }
1292
1293 void update_changed_seq_and_deps(Sequence *changed_seq, int len_change, int ibuf_change)
1294 {
1295         Editing *ed= G.scene->ed;
1296         Sequence *seq;
1297
1298         if (!ed) return;
1299
1300         for (seq=ed->seqbase.first; seq; seq=seq->next)
1301                 update_changed_seq_recurs(seq, changed_seq, len_change, ibuf_change);
1302 }
1303
1304 /* bad levell call... */
1305 void do_render_seq(RenderResult *rr, int cfra)
1306 {
1307         ImBuf *ibuf;
1308
1309         G.f |= G_PLAYANIM;      /* waitcursor patch */
1310
1311         ibuf= give_ibuf_seq(rr->rectx, rr->recty, cfra, 0);
1312         
1313         if(ibuf) {
1314                 
1315                 if(ibuf->rect_float) {
1316                         if (!rr->rectf)
1317                                 rr->rectf= MEM_mallocN(4*sizeof(float)*rr->rectx*rr->recty, "render_seq rectf");
1318                         
1319                         memcpy(rr->rectf, ibuf->rect_float, 4*sizeof(float)*rr->rectx*rr->recty);
1320                         
1321                         /* TSK! Since sequence render doesn't free the *rr render result, the old rect32
1322                            can hang around when sequence render has rendered a 32 bits one before */
1323                         if(rr->rect32) {
1324                                 MEM_freeN(rr->rect32);
1325                                 rr->rect32= NULL;
1326                         }
1327                 }
1328                 else if(ibuf->rect) {
1329                         if (!rr->rect32)
1330                                 rr->rect32= MEM_mallocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
1331
1332                         memcpy(rr->rect32, ibuf->rect, 4*rr->rectx*rr->recty);
1333
1334                         /* if (ibuf->zbuf) { */
1335                         /*      if (R.rectz) freeN(R.rectz); */
1336                         /*      R.rectz = BLI_dupallocN(ibuf->zbuf); */
1337                         /* } */
1338                 }
1339                 
1340                 /* Let the cache limitor take care of this (schlaile) */
1341                 /* While render let's keep all memory available for render (ton) */
1342                 free_imbuf_seq_except(cfra);
1343         }
1344         else {
1345                 /* render result is delivered empty in most cases, nevertheless we handle all cases */
1346                 if (rr->rectf)
1347                         memset(rr->rectf, 0, 4*sizeof(float)*rr->rectx*rr->recty);
1348                 else if (rr->rect32)
1349                         memset(rr->rect32, 0, 4*rr->rectx*rr->recty);
1350                 else
1351                         rr->rect32= MEM_callocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
1352         }
1353         
1354         G.f &= ~G_PLAYANIM;
1355
1356 }