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