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