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