Fix #4352
[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= RE_NewRender(" do_build_seq_ibuf");         /* do not use Scene name, can be the same */
893                                 RenderResult rres;
894                                 int doseq;
895                                 
896                                 waitcursor(1);
897                                 
898                                 /* prevent eternal loop */
899                                 doseq= sce->r.scemode & R_DOSEQ;
900                                 sce->r.scemode &= ~R_DOSEQ;
901                                 
902                                 BIF_init_render_callbacks(re);
903                                 
904                                 /* hrms, set_scene still needed? work on that... */
905                                 if(sce!=oldsce) set_scene_bg(sce);
906                                 RE_BlenderFrame(re, sce, seq->sfra + se->nr);
907                                 if(sce!=oldsce) set_scene_bg(oldsce);
908                                 
909                                 RE_GetResultImage(re, &rres);
910                                 
911                                 if(rres.rectf) {
912                                         se->ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rectfloat, 0);
913                                         memcpy(se->ibuf->rect_float, rres.rectf, 4*sizeof(float)*rres.rectx*rres.recty);
914                                         if(rres.rectz) {
915                                                 /* not yet */
916                                         }
917                                 }
918                                            
919                                 BIF_end_render_callbacks();
920
921                                 /* restore */
922                                 sce->r.scemode |= doseq;
923                                 
924                                 if((G.f & G_PLAYANIM)==0) /* bad, is set on do_render_seq */
925                                         waitcursor(0);
926                                 CFRA = oldcfra;
927                         }
928                         
929                         /* size test */
930                         if(se->ibuf) {
931                                 if(se->ibuf->x != seqrectx || se->ibuf->y != seqrecty ) {
932                                         
933                                         if (0) { // G.scene->r.mode & R_FIELDS) {
934                                                 
935                                                 if (seqrecty > 288) 
936                                                         IMB_scalefieldImBuf(se->ibuf, (short)seqrectx, (short)seqrecty);
937                                                 else {
938                                                         IMB_de_interlace(se->ibuf);
939                                                         
940                                                         if(G.scene->r.mode & R_OSA)
941                                                                 IMB_scaleImBuf(se->ibuf, (short)seqrectx, (short)seqrecty);
942                                                         else
943                                                                 IMB_scalefastImBuf(se->ibuf, (short)seqrectx, (short)seqrecty);
944                                                 }
945                                         }
946                                         else {
947                                                 if(G.scene->r.mode & R_OSA)
948                                                         IMB_scaleImBuf(se->ibuf,(short)seqrectx, (short)seqrecty);
949                                                 else
950                                                         IMB_scalefastImBuf(se->ibuf, (short)seqrectx, (short)seqrecty);
951                                         }
952                                 }
953                                 
954                         }
955                 }
956                 if (se->ibuf) {
957                         IMB_cache_limiter_insert(se->ibuf);
958                         IMB_cache_limiter_ref(se->ibuf);
959                         IMB_cache_limiter_touch(se->ibuf);
960                 }
961         }
962 }
963
964 static void do_build_seqar_cfra(ListBase *seqbase, Sequence ***seqar, int cfra)
965 {
966         Sequence *seq;
967         StripElem *se;
968
969         if(seqar==NULL) return;
970         
971         seq= seqbase->first;
972         while(seq) {
973
974                 /* set at zero because free_imbuf_seq... */
975                 seq->curelem= 0;
976
977                 if ((seq->type == SEQ_RAM_SOUND || seq->type == SEQ_HD_SOUND) && (seq->ipo)
978                     && (seq->startdisp <= cfra+2) && (seq->enddisp > cfra)) {
979                         do_seq_ipo(seq);
980                 }
981
982                 if(seq->startdisp <=cfra && seq->enddisp > cfra) {
983                         **seqar= seq;
984                         (*seqar)++;
985
986                         /* nobody is needed a priori */
987                         se = seq->curelem= give_stripelem(seq, cfra);
988         
989                         if (se) {
990                                 se->isneeded = 0;
991                         }
992                 }
993
994                 seq= seq->next;
995         }
996 }
997
998 static void do_build_seq_ibufs(ListBase *seqbase, int cfra)
999 {
1000         Sequence *seq;
1001
1002         seq= seqbase->first;
1003         while(seq) {
1004
1005                 /* set at zero because free_imbuf_seq... */
1006                 seq->curelem= 0;
1007
1008                 if ((seq->type == SEQ_RAM_SOUND || seq->type == SEQ_HD_SOUND) && (seq->ipo)
1009                     && (seq->startdisp <= cfra+2)  && (seq->enddisp > cfra)) {
1010                         do_seq_ipo(seq);
1011                 }
1012
1013                 if(seq->startdisp <=cfra && seq->enddisp > cfra) {
1014                         if(seq->seqbase.first) {
1015                                 if(cfra< seq->start) 
1016                                         do_build_seq_ibufs(&seq->seqbase, seq->start);
1017                                 else if(cfra> seq->start+seq->len-1) 
1018                                         do_build_seq_ibufs(&seq->seqbase, seq->start + seq->len-1);
1019                                 else do_build_seq_ibufs(&seq->seqbase, cfra);
1020                         }
1021
1022                         do_build_seq_ibuf(seq, cfra);
1023                 }
1024
1025                 seq= seq->next;
1026         }
1027 }
1028
1029 ImBuf *give_ibuf_seq(int rectx, int recty, int cfra, int chanshown)
1030 {
1031         Sequence **tseqar, **seqar;
1032         Sequence *seq, *seqfirst=0;/*  , *effirst=0; */
1033         Editing *ed;
1034         StripElem *se;
1035         int seqnr, totseq;
1036
1037         /* we make recursively a 'stack' of sequences, these are
1038          * sorted nicely as well.
1039          * this method has been developed especially for 
1040          * stills before or after metas
1041          */
1042
1043         totseq= 0;
1044         ed= G.scene->ed;
1045         if(ed==0) return 0;
1046         do_seq_count_cfra(ed->seqbasep, &totseq, cfra);
1047
1048         if(totseq==0) return 0;
1049
1050         seqrectx= rectx;        /* bad bad global! */
1051         seqrecty= recty;
1052
1053         /* tseqar is needed because in do_build_... the pointer changes */
1054         seqar= tseqar= MEM_callocN(sizeof(void *)*totseq, "seqar");
1055
1056         /* this call creates the sequence order array */
1057         do_build_seqar_cfra(ed->seqbasep, &seqar, cfra);
1058
1059         seqar= tseqar;
1060
1061         for(seqnr=0; seqnr<totseq; seqnr++) {
1062                 seq= seqar[seqnr];
1063
1064                 se= seq->curelem;
1065                 if((seq->type != SEQ_RAM_SOUND && seq->type != SEQ_HD_SOUND) 
1066                         && (se) && (chanshown == 0 || seq->machine == chanshown)) {
1067                         if(seq->type==SEQ_META) {
1068
1069                                 /* bottom strip! */
1070                                 if(seqfirst==0) seqfirst= seq;
1071                                 else if(seqfirst->depth > seq->depth) seqfirst= seq;
1072                                 else if(seqfirst->machine > seq->machine) seqfirst= seq;
1073
1074                         }
1075                         else if(seq->type & SEQ_EFFECT) {
1076
1077                                 /* top strip! */
1078                                 if(seqfirst==0) seqfirst= seq;
1079                                 else if(seqfirst->depth > seq->depth) seqfirst= seq;
1080                                 else if(seqfirst->machine < seq->machine) seqfirst= seq;
1081
1082
1083                         }
1084                         else if(seq->type < SEQ_EFFECT) {       /* images */
1085
1086                                 /* bottom strip! a feature that allows you to store junk in locations above */
1087
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         }
1095
1096         MEM_freeN(seqar);
1097
1098         /* we know, that we have to build the ibuf of seqfirst, 
1099            now build the dependencies and later the ibufs */
1100
1101         if (seqfirst) {
1102                 do_build_seq_depend(seqfirst, cfra);
1103                 do_build_seq_ibufs(ed->seqbasep, cfra);
1104                 do_seq_unref_cfra(ed->seqbasep, cfra);
1105                 do_seq_test_unref_cfra(ed->seqbasep, cfra);
1106         }
1107
1108         if(!seqfirst) return 0;
1109         if(!seqfirst->curelem) return 0;
1110         return seqfirst->curelem->ibuf;
1111
1112 }
1113
1114 void free_imbuf_effect_spec(int cfra)
1115 {
1116         Sequence *seq;
1117         StripElem *se;
1118         Editing *ed;
1119         int a;
1120
1121         ed= G.scene->ed;
1122         if(ed==0) return;
1123
1124         WHILE_SEQ(&ed->seqbase) {
1125
1126                 if(seq->strip) {
1127
1128                         if(seq->type & SEQ_EFFECT) {
1129                                 se= seq->strip->stripdata;
1130                                 for(a=0; a<seq->len; a++, se++) {
1131                                         if(se==seq->curelem && se->ibuf) {
1132                                                 IMB_freeImBuf(se->ibuf);
1133                                                 se->ibuf= 0;
1134                                                 se->ok= 1;
1135                                                 se->se1= se->se2= se->se3= 0;
1136                                         }
1137                                 }
1138                         }
1139                 }
1140         }
1141         END_SEQ
1142 }
1143
1144 void free_imbuf_seq_except(int cfra)
1145 {
1146         Sequence *seq;
1147         StripElem *se;
1148         Editing *ed;
1149         int a;
1150
1151         ed= G.scene->ed;
1152         if(ed==0) return;
1153
1154         WHILE_SEQ(&ed->seqbase) {
1155
1156                 if(seq->strip) {
1157
1158                         if( seq->type==SEQ_META ) {
1159                                 ;
1160                         }
1161                         else {
1162                                 se= seq->strip->stripdata;
1163                                 for(a=0; a<seq->len; a++, se++) {
1164                                         if(se!=seq->curelem && se->ibuf) {
1165                                                 IMB_freeImBuf(se->ibuf);
1166                                                 se->ibuf= 0;
1167                                                 se->ok= 1;
1168                                                 se->se1= se->se2= se->se3= 0;
1169                                         }
1170                                 }
1171                         }
1172
1173                         if(seq->type==SEQ_MOVIE) {
1174                                 if(seq->startdisp > cfra || seq->enddisp < cfra) {
1175                                         if(seq->anim) {
1176                                                 IMB_free_anim(seq->anim);
1177                                                 seq->anim = 0;
1178                                         }
1179                                 }
1180                         }
1181                 }
1182         }
1183         END_SEQ
1184 }
1185
1186 void free_imbuf_seq()
1187 {
1188         Sequence *seq;
1189         StripElem *se;
1190         Editing *ed;
1191         int a;
1192
1193         ed= G.scene->ed;
1194         if(ed==0) return;
1195
1196         WHILE_SEQ(&ed->seqbase) {
1197
1198                 if(seq->strip) {
1199
1200                         if( seq->type==SEQ_META ) {
1201                                 ;
1202                         }
1203                         else {
1204                                 se= seq->strip->stripdata;
1205                                 for(a=0; a<seq->len; a++, se++) {
1206                                         if(se->ibuf) {
1207                                                 IMB_freeImBuf(se->ibuf);
1208                                                 se->ibuf= 0;
1209                                                 se->ok= 1;
1210                                                 se->se1= se->se2= se->se3= 0;
1211                                         }
1212                                 }
1213                         }
1214
1215                         if(seq->type==SEQ_MOVIE) {
1216                                 if(seq->anim) {
1217                                         IMB_free_anim(seq->anim);
1218                                         seq->anim = 0;
1219                                 }
1220                         }
1221                 }
1222         }
1223         END_SEQ
1224 }
1225
1226 /* bad levell call... */
1227 void do_render_seq(RenderResult *rr, int cfra)
1228 {
1229         ImBuf *ibuf;
1230
1231         G.f |= G_PLAYANIM;      /* waitcursor patch */
1232
1233         ibuf= give_ibuf_seq(rr->rectx, rr->recty, cfra, 0);
1234         
1235         if(ibuf) {
1236                 
1237                 if(ibuf->rect_float) {
1238                         if (!rr->rectf)
1239                                 rr->rectf= MEM_mallocN(4*sizeof(float)*rr->rectx*rr->recty, "render_seq rectf");
1240                         
1241                         memcpy(rr->rectf, ibuf->rect_float, 4*sizeof(float)*rr->rectx*rr->recty);
1242                 }
1243                 else if(ibuf->rect) {
1244                         if (!rr->rect32)
1245                                 rr->rect32= MEM_mallocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
1246
1247                         memcpy(rr->rect32, ibuf->rect, 4*rr->rectx*rr->recty);
1248
1249                         /* if (ibuf->zbuf) { */
1250                         /*      if (R.rectz) freeN(R.rectz); */
1251                         /*      R.rectz = BLI_dupallocN(ibuf->zbuf); */
1252                         /* } */
1253                 }
1254                 
1255                 /* Let the cache limitor take care of this (schlaile) */
1256                 /* While render let's keep all memory available for render (ton) */
1257                 free_imbuf_seq_except(cfra);
1258         }
1259         else {
1260                 /* render result is delivered empty in most cases */
1261                 if (!rr->rect32)
1262                         rr->rect32= MEM_callocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
1263         }
1264         
1265         G.f &= ~G_PLAYANIM;
1266
1267 }