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