== Sequencer ==
[blender.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 #include "MEM_CacheLimiterC-Api.h"
37
38 #include "BLI_blenlib.h"
39 #include "BLI_arithb.h"
40
41 #include "IMB_imbuf_types.h"
42 #include "IMB_imbuf.h"
43
44 #include "DNA_ipo_types.h"
45 #include "DNA_scene_types.h"
46 #include "DNA_sequence_types.h"
47 #include "DNA_view3d_types.h"
48
49 #include "BKE_global.h"
50 #include "BKE_image.h"
51 #include "BKE_ipo.h"
52 #include "BKE_main.h"
53 #include "BKE_scene.h"
54 #include "BKE_texture.h"
55 #include "BKE_utildefines.h"
56
57 #include "BIF_editsound.h"
58 #include "BIF_editseq.h"
59 #include "BSE_filesel.h"
60 #include "BSE_headerbuttons.h"
61 #include "BIF_interface.h"
62 #include "BIF_renderwin.h"
63 #include "BIF_screen.h"
64 #include "BIF_space.h"
65 #include "BIF_toolbox.h"
66
67 #include "BSE_sequence.h"
68 #include "BSE_seqeffects.h"
69
70 #include "RE_pipeline.h"                // talks to entire render API
71
72 #include "blendef.h"
73
74 #include "BLI_threads.h"
75 #include <pthread.h>
76
77 #ifdef WIN32
78 #define snprintf _snprintf
79 #endif
80
81 int seqrectx, seqrecty;
82
83 /* **********************************************************************
84    alloc / free functions
85    ********************************************************************** */
86
87 void free_tstripdata(int len, TStripElem *se)
88 {
89         TStripElem *seo;
90         int a;
91
92         seo= se;
93         if (!se) {
94                 return;
95         }
96
97         for(a=0; a<len; a++, se++) {
98                 if(se->ibuf) {
99                         IMB_freeImBuf(se->ibuf);
100                         se->ibuf = 0;
101                 }
102                 if(se->ibuf_comp) {
103                         IMB_freeImBuf(se->ibuf_comp);
104                         se->ibuf_comp = 0;
105                 }
106         }
107
108         MEM_freeN(seo);
109
110 }
111
112 void free_strip(Strip *strip)
113 {
114         strip->us--;
115         if(strip->us>0) return;
116         if(strip->us<0) {
117                 printf("error: negative users in strip\n");
118                 return;
119         }
120
121         if (strip->stripdata) {
122                 MEM_freeN(strip->stripdata);
123         }
124
125         if (strip->proxy) {
126                 MEM_freeN(strip->proxy);
127         }
128         if (strip->crop) {
129                 MEM_freeN(strip->crop);
130         }
131         if (strip->transform) {
132                 MEM_freeN(strip->transform);
133         }
134         if (strip->color_balance) {
135                 MEM_freeN(strip->color_balance);
136         }
137
138         free_tstripdata(strip->len, strip->tstripdata);
139         free_tstripdata(strip->endstill, strip->tstripdata_endstill);
140         free_tstripdata(strip->startstill, strip->tstripdata_startstill);
141
142         MEM_freeN(strip);
143 }
144
145 void new_tstripdata(Sequence *seq)
146 {
147         if(seq->strip) {
148                 free_tstripdata(seq->strip->len, seq->strip->tstripdata);
149                 free_tstripdata(seq->strip->endstill, 
150                                 seq->strip->tstripdata_endstill);
151                 free_tstripdata(seq->strip->startstill, 
152                                 seq->strip->tstripdata_startstill);
153
154                 seq->strip->tstripdata= 0;
155                 seq->strip->tstripdata_endstill= 0;
156                 seq->strip->tstripdata_startstill= 0;
157
158                 seq->strip->len= seq->len;
159         }
160 }
161
162 void free_sequence(Sequence *seq)
163 {
164         Sequence *last_seq = get_last_seq();
165
166         if(seq->strip) free_strip(seq->strip);
167
168         if(seq->anim) IMB_free_anim(seq->anim);
169         if(seq->hdaudio) sound_close_hdaudio(seq->hdaudio);
170
171         if (seq->type & SEQ_EFFECT) {
172                 struct SeqEffectHandle sh = get_sequence_effect(seq);
173
174                 sh.free(seq);
175         }
176
177         if(seq==last_seq) set_last_seq(NULL);
178
179         MEM_freeN(seq);
180 }
181
182 /*
183   **********************************************************************
184   * build_seqar
185   **********************************************************************
186   * Build a complete array of _all_ sequencies (including those
187   * in metastrips!)
188   **********************************************************************
189 */
190
191 static void do_seq_count(ListBase *seqbase, int *totseq)
192 {
193         Sequence *seq;
194
195         seq= seqbase->first;
196         while(seq) {
197                 (*totseq)++;
198                 if(seq->seqbase.first) do_seq_count(&seq->seqbase, totseq);
199                 seq= seq->next;
200         }
201 }
202
203 static void do_build_seqar(ListBase *seqbase, Sequence ***seqar, int depth)
204 {
205         Sequence *seq;
206
207         seq= seqbase->first;
208         while(seq) {
209                 seq->depth= depth;
210                 if(seq->seqbase.first) do_build_seqar(&seq->seqbase, seqar, depth+1);
211                 **seqar= seq;
212                 (*seqar)++;
213                 seq= seq->next;
214         }
215 }
216
217 void build_seqar(ListBase *seqbase, Sequence  ***seqar, int *totseq)
218 {
219         Sequence **tseqar;
220
221         *totseq= 0;
222         do_seq_count(seqbase, totseq);
223
224         if(*totseq==0) {
225                 *seqar= 0;
226                 return;
227         }
228         *seqar= MEM_mallocN(sizeof(void *)* *totseq, "seqar");
229         tseqar= *seqar;
230
231         do_build_seqar(seqbase, seqar, 0);
232         *seqar= tseqar;
233 }
234
235 static void do_seq_count_cb(ListBase *seqbase, int *totseq,
236                             int (*test_func)(Sequence * seq))
237 {
238         Sequence *seq;
239
240         seq= seqbase->first;
241         while(seq) {
242                 int test = test_func(seq);
243                 if (test & BUILD_SEQAR_COUNT_CURRENT) {
244                         (*totseq)++;
245                 }
246                 if(seq->seqbase.first && (test & BUILD_SEQAR_COUNT_CHILDREN)) {
247                         do_seq_count_cb(&seq->seqbase, totseq, test_func);
248                 }
249                 seq= seq->next;
250         }
251 }
252
253 static void do_build_seqar_cb(ListBase *seqbase, Sequence ***seqar, int depth,
254                               int (*test_func)(Sequence * seq))
255 {
256         Sequence *seq;
257
258         seq= seqbase->first;
259         while(seq) {
260                 int test = test_func(seq);
261                 seq->depth= depth;
262
263                 if(seq->seqbase.first && (test & BUILD_SEQAR_COUNT_CHILDREN)) {
264                         do_build_seqar_cb(&seq->seqbase, seqar, depth+1, 
265                                           test_func);
266                 }
267                 if (test & BUILD_SEQAR_COUNT_CURRENT) {
268                         **seqar= seq;
269                         (*seqar)++;
270                 }
271                 seq= seq->next;
272         }
273 }
274
275 void build_seqar_cb(ListBase *seqbase, Sequence  ***seqar, int *totseq,
276                     int (*test_func)(Sequence * seq))
277 {
278         Sequence **tseqar;
279
280         *totseq= 0;
281         do_seq_count_cb(seqbase, totseq, test_func);
282
283         if(*totseq==0) {
284                 *seqar= 0;
285                 return;
286         }
287         *seqar= MEM_mallocN(sizeof(void *)* *totseq, "seqar");
288         tseqar= *seqar;
289
290         do_build_seqar_cb(seqbase, seqar, 0, test_func);
291         *seqar= tseqar;
292 }
293
294
295 void free_editing(Editing *ed)
296 {
297         MetaStack *ms;
298         Sequence *seq;
299
300         if(ed==NULL) return;
301         set_last_seq(NULL);     /* clear_last_seq doesnt work, it screws up free_sequence */
302
303         WHILE_SEQ(&ed->seqbase) {
304                 free_sequence(seq);
305         }
306         END_SEQ
307
308         while( (ms= ed->metastack.first) ) {
309                 BLI_remlink(&ed->metastack, ms);
310                 MEM_freeN(ms);
311         }
312
313         MEM_freeN(ed);
314
315 }
316
317 void calc_sequence_disp(Sequence *seq)
318 {
319         if(seq->startofs && seq->startstill) seq->startstill= 0;
320         if(seq->endofs && seq->endstill) seq->endstill= 0;
321         
322         seq->startdisp= seq->start + seq->startofs - seq->startstill;
323         seq->enddisp= seq->start+seq->len - seq->endofs + seq->endstill;
324         
325         seq->handsize= 10.0;    /* 10 frames */
326         if( seq->enddisp-seq->startdisp < 10 ) {
327                 seq->handsize= (float)(0.5*(seq->enddisp-seq->startdisp));
328         }
329         else if(seq->enddisp-seq->startdisp > 250) {
330                 seq->handsize= (float)((seq->enddisp-seq->startdisp)/25);
331         }
332 }
333
334 void calc_sequence(Sequence *seq)
335 {
336         Sequence *seqm;
337         int min, max;
338
339         /* check all metas recursively */
340         seqm= seq->seqbase.first;
341         while(seqm) {
342                 if(seqm->seqbase.first) calc_sequence(seqm);
343                 seqm= seqm->next;
344         }
345
346         /* effects and meta: automatic start and end */
347
348         if(seq->type & SEQ_EFFECT) {
349                 /* pointers */
350                 if(seq->seq2==0) seq->seq2= seq->seq1;
351                 if(seq->seq3==0) seq->seq3= seq->seq1;
352
353                 /* effecten go from seq1 -> seq2: test */
354
355                 /* we take the largest start and smallest end */
356
357                 // seq->start= seq->startdisp= MAX2(seq->seq1->startdisp, seq->seq2->startdisp);
358                 // seq->enddisp= MIN2(seq->seq1->enddisp, seq->seq2->enddisp);
359
360                 if (seq->seq1) {
361                         seq->start= seq->startdisp= MAX3(seq->seq1->startdisp, seq->seq2->startdisp, seq->seq3->startdisp);
362                         seq->enddisp= MIN3(seq->seq1->enddisp, seq->seq2->enddisp, seq->seq3->enddisp);
363                         seq->len= seq->enddisp - seq->startdisp;
364                 } else {
365                         calc_sequence_disp(seq);
366                 }
367
368                 if(seq->strip && seq->len!=seq->strip->len) {
369                         new_tstripdata(seq);
370                 }
371
372         }
373         else {
374                 if(seq->type==SEQ_META) {
375                         seqm= seq->seqbase.first;
376                         if(seqm) {
377                                 min= 1000000;
378                                 max= -1000000;
379                                 while(seqm) {
380                                         if(seqm->startdisp < min) min= seqm->startdisp;
381                                         if(seqm->enddisp > max) max= seqm->enddisp;
382                                         seqm= seqm->next;
383                                 }
384                                 seq->start= min;
385                                 seq->len= max-min;
386
387                                 if(seq->strip && seq->len!=seq->strip->len) {
388                                         new_tstripdata(seq);
389                                 }
390                         }
391                 }
392                 calc_sequence_disp(seq);
393         }
394 }
395
396 void reload_sequence_new_file(Sequence * seq)
397 {
398         char str[FILE_MAXDIR+FILE_MAXFILE];
399
400         if (!(seq->type == SEQ_MOVIE || seq->type == SEQ_IMAGE ||
401               seq->type == SEQ_HD_SOUND || seq->type == SEQ_SCENE)) {
402                 return;
403         }
404
405         new_tstripdata(seq);
406
407         if (seq->type == SEQ_IMAGE) {
408                 return;
409         }
410
411         if (seq->type != SEQ_SCENE) {
412                 strncpy(str, seq->strip->dir, FILE_MAXDIR-1);
413                 strncat(str, seq->strip->stripdata->name, FILE_MAXFILE-1);
414         }
415
416         if (seq->type == SEQ_MOVIE) {
417                 if(seq->anim) IMB_free_anim(seq->anim);
418                 seq->anim = openanim(str, IB_rect);
419
420                 if (!seq->anim) {
421                         return;
422                 }
423         
424                 seq->len = IMB_anim_get_duration(seq->anim);
425                 
426                 seq->anim_preseek = IMB_anim_get_preseek(seq->anim);
427
428                 seq->len -= seq->anim_startofs;
429                 seq->len -= seq->anim_endofs;
430                 if (seq->len < 0) {
431                         seq->len = 0;
432                 }
433                 seq->strip->len = seq->len;
434         } else if (seq->type == SEQ_HD_SOUND) {
435                 if(seq->hdaudio) sound_close_hdaudio(seq->hdaudio);
436                 seq->hdaudio = sound_open_hdaudio(str);
437
438                 if (!seq->hdaudio) {
439                         return;
440                 }
441
442                 seq->strip->len = seq->len 
443                         = sound_hdaudio_get_duration(seq->hdaudio, FPS);
444         } else if (seq->type == SEQ_SCENE) {
445                 Scene * sce = G.main->scene.first;
446                 int nr = 1;
447                 while(sce) {
448                         if(nr == seq->scenenr) {
449                                 break;
450                         }
451                         nr++;
452                         sce= sce->id.next;
453                 }
454
455                 if (sce) {
456                         seq->scene = sce;
457                 }
458
459                 strncpy(seq->name + 2, sce->id.name + 2, 
460                         sizeof(seq->name) - 2);
461
462                 seq->len= seq->scene->r.efra - seq->scene->r.sfra + 1;
463                 seq->len -= seq->anim_startofs;
464                 seq->len -= seq->anim_endofs;
465                 if (seq->len < 0) {
466                         seq->len = 0;
467                 }
468                 seq->strip->len = seq->len;
469         }
470
471
472         calc_sequence(seq);
473 }
474
475 void sort_seq()
476 {
477         /* all strips together per kind, and in order of y location ("machine") */
478         ListBase seqbase, effbase;
479         Editing *ed;
480         Sequence *seq, *seqt;
481
482         ed= G.scene->ed;
483         if(ed==0) return;
484
485         seqbase.first= seqbase.last= 0;
486         effbase.first= effbase.last= 0;
487
488         while( (seq= ed->seqbasep->first) ) {
489                 BLI_remlink(ed->seqbasep, seq);
490
491                 if(seq->type & SEQ_EFFECT) {
492                         seqt= effbase.first;
493                         while(seqt) {
494                                 if(seqt->machine>=seq->machine) {
495                                         BLI_insertlinkbefore(&effbase, seqt, seq);
496                                         break;
497                                 }
498                                 seqt= seqt->next;
499                         }
500                         if(seqt==0) BLI_addtail(&effbase, seq);
501                 }
502                 else {
503                         seqt= seqbase.first;
504                         while(seqt) {
505                                 if(seqt->machine>=seq->machine) {
506                                         BLI_insertlinkbefore(&seqbase, seqt, seq);
507                                         break;
508                                 }
509                                 seqt= seqt->next;
510                         }
511                         if(seqt==0) BLI_addtail(&seqbase, seq);
512                 }
513         }
514
515         addlisttolist(&seqbase, &effbase);
516         *(ed->seqbasep)= seqbase;
517 }
518
519
520 void clear_scene_in_allseqs(Scene *sce)
521 {
522         Scene *sce1;
523         Editing *ed;
524         Sequence *seq;
525
526         /* when a scene is deleted: test all seqs */
527
528         sce1= G.main->scene.first;
529         while(sce1) {
530                 if(sce1!=sce && sce1->ed) {
531                         ed= sce1->ed;
532
533                         WHILE_SEQ(&ed->seqbase) {
534
535                                 if(seq->scene==sce) seq->scene= 0;
536
537                         }
538                         END_SEQ
539                 }
540
541                 sce1= sce1->id.next;
542         }
543 }
544
545 char *give_seqname_by_type(int type)
546 {
547         switch(type) {
548         case SEQ_META:       return "Meta";
549         case SEQ_IMAGE:      return "Image";
550         case SEQ_SCENE:      return "Scene";
551         case SEQ_MOVIE:      return "Movie";
552         case SEQ_RAM_SOUND:  return "Audio (RAM)";
553         case SEQ_HD_SOUND:   return "Audio (HD)";
554         case SEQ_CROSS:      return "Cross";
555         case SEQ_GAMCROSS:   return "Gamma Cross";
556         case SEQ_ADD:        return "Add";
557         case SEQ_SUB:        return "Sub";
558         case SEQ_MUL:        return "Mul";
559         case SEQ_ALPHAOVER:  return "Alpha Over";
560         case SEQ_ALPHAUNDER: return "Alpha Under";
561         case SEQ_OVERDROP:   return "Over Drop";
562         case SEQ_WIPE:       return "Wipe";
563         case SEQ_GLOW:       return "Glow";
564         case SEQ_TRANSFORM:  return "Transform";
565         case SEQ_COLOR:      return "Color";
566         case SEQ_SPEED:      return "Speed";
567         default:
568                 return 0;
569         }
570 }
571
572 char *give_seqname(Sequence *seq)
573 {
574         char * name = give_seqname_by_type(seq->type);
575
576         if (!name) {
577                 if(seq->type<SEQ_EFFECT) {
578                         return seq->strip->dir;
579                 } else if(seq->type==SEQ_PLUGIN) {
580                         if(!(seq->flag & SEQ_EFFECT_NOT_LOADED) &&
581                            seq->plugin && seq->plugin->doit) {
582                                 return seq->plugin->pname;
583                         } else {
584                                 return "Plugin";
585                         }
586                 } else {
587                         return "Effect";
588                 }
589         }
590         return name;
591 }
592
593 /* ***************** DO THE SEQUENCE ***************** */
594
595 static void make_black_ibuf(ImBuf *ibuf)
596 {
597         unsigned int *rect;
598         float *rect_float;
599         int tot;
600
601         if(ibuf==0 || (ibuf->rect==0 && ibuf->rect_float==0)) return;
602
603         tot= ibuf->x*ibuf->y;
604
605         rect= ibuf->rect;
606         rect_float = ibuf->rect_float;
607
608         if (rect) {
609                 memset(rect,       0, tot * sizeof(char) * 4);
610         }
611
612         if (rect_float) {
613                 memset(rect_float, 0, tot * sizeof(float) * 4);
614         }
615 }
616
617 static void multibuf(ImBuf *ibuf, float fmul)
618 {
619         char *rt;
620         float *rt_float;
621
622         int a, mul, icol;
623
624         mul= (int)(256.0*fmul);
625
626         a= ibuf->x*ibuf->y;
627         rt= (char *)ibuf->rect;
628         rt_float = ibuf->rect_float;
629
630         if (rt) {
631                 while(a--) {
632
633                         icol= (mul*rt[0])>>8;
634                         if(icol>254) rt[0]= 255; else rt[0]= icol;
635                         icol= (mul*rt[1])>>8;
636                         if(icol>254) rt[1]= 255; else rt[1]= icol;
637                         icol= (mul*rt[2])>>8;
638                         if(icol>254) rt[2]= 255; else rt[2]= icol;
639                         icol= (mul*rt[3])>>8;
640                         if(icol>254) rt[3]= 255; else rt[3]= icol;
641                         
642                         rt+= 4;
643                 }
644         }
645         if (rt_float) {
646                 while(a--) {
647                         rt_float[0] *= fmul;
648                         rt_float[1] *= fmul;
649                         rt_float[2] *= fmul;
650                         rt_float[3] *= fmul;
651                         
652                         rt_float += 4;
653                 }
654         }
655 }
656
657 static void do_effect(int cfra, Sequence *seq, TStripElem * se)
658 {
659         TStripElem *se1, *se2, *se3;
660         float fac, facf;
661         int x, y;
662         int early_out;
663         struct SeqEffectHandle sh = get_sequence_effect(seq);
664
665         if (!sh.execute) { /* effect not supported in this version... */
666                 make_black_ibuf(se->ibuf);
667                 return;
668         }
669
670         if(seq->ipo && seq->ipo->curve.first) {
671                 do_seq_ipo(seq, cfra);
672                 fac= seq->facf0;
673                 facf= seq->facf1;
674         } else {
675                 sh.get_default_fac(seq, cfra, &fac, &facf);
676         }
677
678         if( !(G.scene->r.mode & R_FIELDS) ) facf = fac;
679
680         early_out = sh.early_out(seq, fac, facf);
681
682         if (early_out == -1) { /* no input needed */
683                 sh.execute(seq, cfra, fac, facf, 
684                            se->ibuf->x, se->ibuf->y, 
685                            0, 0, 0, se->ibuf);
686                 return;
687         }
688
689         switch (early_out) {
690         case 0:
691                 if (se->se1==0 || se->se2==0 || se->se3==0) {
692                         make_black_ibuf(se->ibuf);
693                         return;
694                 }
695
696                 if(se->se1->ok == STRIPELEM_META) se1= se->se1->se1;
697                 else se1= se->se1;
698                 
699                 if(se->se2->ok == STRIPELEM_META) se2= se->se2->se1;
700                 else se2= se->se2;
701                 
702                 if(se->se3->ok == STRIPELEM_META) se3= se->se3->se1;
703                 else se3= se->se3;
704
705                 if (   (se1==0 || se2==0 || se3==0)
706                     || (se1->ibuf==0 || se2->ibuf==0 || se3->ibuf==0)) {
707                         make_black_ibuf(se->ibuf);
708                         return;
709                 }
710
711                 break;
712         case 1:
713                 if (se->se1 == 0) {
714                         make_black_ibuf(se->ibuf);
715                         return;
716                 }
717
718                 if(se->se1->ok == STRIPELEM_META) se1= se->se1->se1;
719                 else se1= se->se1;
720
721                 if (se1 == 0 || se1->ibuf == 0) {
722                         make_black_ibuf(se->ibuf);
723                         return;
724                 }
725
726                 if (se->ibuf != se1->ibuf) {
727                         IMB_freeImBuf(se->ibuf);
728                         se->ibuf = se1->ibuf;
729                         IMB_refImBuf(se->ibuf);
730                 }
731                 return;
732         case 2:
733                 if (se->se2 == 0) {
734                         make_black_ibuf(se->ibuf);
735                         return;
736                 }
737
738                 if(se->se2->ok == STRIPELEM_META) se2= se->se2->se1;
739                 else se2= se->se2;
740
741                 if (se2 == 0 || se2->ibuf == 0) {
742                         make_black_ibuf(se->ibuf);
743                         return;
744                 }
745                 if (se->ibuf != se2->ibuf) {
746                         IMB_freeImBuf(se->ibuf);
747                         se->ibuf = se2->ibuf;
748                         IMB_refImBuf(se->ibuf);
749                 }
750                 return;
751         default:
752                 make_black_ibuf(se->ibuf);
753                 return;
754         }
755
756         x= se2->ibuf->x;
757         y= se2->ibuf->y;
758
759         if (!se1->ibuf->rect_float && se->ibuf->rect_float) {
760                 IMB_float_from_rect(se1->ibuf);
761         }
762         if (!se2->ibuf->rect_float && se->ibuf->rect_float) {
763                 IMB_float_from_rect(se2->ibuf);
764         }
765
766         if (!se1->ibuf->rect && !se->ibuf->rect_float) {
767                 IMB_rect_from_float(se1->ibuf);
768         }
769         if (!se2->ibuf->rect && !se->ibuf->rect_float) {
770                 IMB_rect_from_float(se2->ibuf);
771         }
772
773         sh.execute(seq, cfra, fac, facf, x, y, se1->ibuf, se2->ibuf, se3->ibuf,
774                    se->ibuf);
775 }
776
777 static int give_stripelem_index(Sequence *seq, int cfra)
778 {
779         int nr;
780
781         if(seq->startdisp >cfra || seq->enddisp <= cfra) return -1;
782
783         if(seq->flag&SEQ_REVERSE_FRAMES) {      
784                 /*reverse frame in this sequence */
785                 if(cfra <= seq->start) nr= seq->len-1;
786                 else if(cfra >= seq->start+seq->len-1) nr= 0;
787                 else nr= (seq->start + seq->len) - cfra;
788         } else {
789                 if(cfra <= seq->start) nr= 0;
790                 else if(cfra >= seq->start+seq->len-1) nr= seq->len-1;
791                 else nr= cfra-seq->start;
792         }
793         if (seq->strobe < 1.0) seq->strobe = 1.0;
794         if (seq->strobe > 1.0) {
795                 nr -= (int)fmod((double)nr, (double)seq->strobe);
796         }
797
798         return nr;
799 }
800
801 static TStripElem* alloc_tstripdata(int len, const char * name)
802 {
803         int i;
804         TStripElem *se = MEM_callocN(len * sizeof(TStripElem), name);
805         for (i = 0; i < len; i++) {
806                 se[i].ok = STRIPELEM_OK;
807         }
808         return se;
809 }
810
811 TStripElem *give_tstripelem(Sequence *seq, int cfra)
812 {
813         TStripElem *se;
814         int nr;
815
816         se = seq->strip->tstripdata;
817         if (se == 0 && seq->len > 0) {
818                 se = seq->strip->tstripdata = alloc_tstripdata(seq->len,
819                                                                "tstripelems");
820         }
821         nr = give_stripelem_index(seq, cfra);
822
823         if (nr == -1) return 0;
824         if (se == 0) return 0;
825
826         se += nr; 
827
828         /* if there are IPOs with blend modes active, one has to watch out
829            for startstill + endstill area: we can't use the same tstripelem
830            here for all ibufs, since then, blending with IPOs won't work!
831            
832            Rather common case, if you use a single image and try to fade
833            it in and out... or want to use your strip as a watermark in
834            alpha over mode...
835         */
836         if (seq->blend_mode != SEQ_BLEND_REPLACE ||
837             (seq->ipo && seq->ipo->curve.first && !(seq->type & SEQ_EFFECT))) {
838                 Strip * s = seq->strip;
839                 if (cfra < seq->start) {
840                         se = s->tstripdata_startstill;
841                         if (seq->startstill > s->startstill) {
842                                 free_tstripdata(s->startstill, 
843                                                 s->tstripdata_startstill);
844                                 se = 0;
845                         }
846
847                         if (se == 0) {
848                                 s->startstill = seq->startstill;
849                                 se = seq->strip->tstripdata_startstill
850                                         = alloc_tstripdata(
851                                                 s->startstill,
852                                                 "tstripelems_startstill");
853                         }
854                         se += seq->start - cfra - 1;
855
856                 } else if (cfra > seq->start + seq->len-1) {
857                         se = s->tstripdata_endstill;
858                         if (seq->endstill > s->endstill) {
859                                 free_tstripdata(s->endstill, 
860                                                 s->tstripdata_endstill);
861                                 se = 0;
862                         }
863
864                         if (se == 0) {
865                                 s->endstill = seq->endstill;
866                                 se = seq->strip->tstripdata_endstill
867                                         = alloc_tstripdata(
868                                                 s->endstill,
869                                                 "tstripelems_endstill");
870                         }
871                         se += cfra - (seq->start + seq->len-1) - 1;
872                 }
873         }
874
875         
876         se->nr= nr;
877
878         return se;
879 }
880
881 StripElem *give_stripelem(Sequence *seq, int cfra)
882 {
883         StripElem *se;
884         int nr;
885
886         se = seq->strip->stripdata;
887         nr = give_stripelem_index(seq, cfra);
888
889         if (nr == -1) return 0;
890         if (se == 0) return 0;
891
892         se += nr + seq->anim_startofs; 
893         
894         return se;
895 }
896
897 static int evaluate_seq_frame_gen(
898         Sequence ** seq_arr, ListBase *seqbase, int cfra)
899 {
900         Sequence *seq;
901         int totseq=0;
902
903         memset(seq_arr, 0, sizeof(Sequence*) * (MAXSEQ+1));
904
905         seq= seqbase->first;
906         while(seq) {
907                 if(seq->startdisp <=cfra && seq->enddisp > cfra) {
908                         seq_arr[seq->machine]= seq;
909                         totseq++;
910                 }
911                 seq= seq->next;
912         }
913
914         return totseq;
915 }
916
917 int evaluate_seq_frame(int cfra)
918 {
919        Editing *ed;
920        Sequence *seq_arr[MAXSEQ+1];
921
922        ed= G.scene->ed;
923        if(ed==0) return 0;
924         
925        return evaluate_seq_frame_gen(seq_arr, ed->seqbasep, cfra);
926
927 }
928
929 static int video_seq_is_rendered(Sequence * seq)
930 {
931         return (seq 
932                 && !(seq->flag & SEQ_MUTE) 
933                 && seq->type != SEQ_RAM_SOUND 
934                 && seq->type != SEQ_HD_SOUND);
935 }
936
937 static int get_shown_sequences(
938         ListBase * seqbasep, int cfra, int chanshown, Sequence ** seq_arr_out)
939 {
940         Sequence *seq_arr[MAXSEQ+1];
941         int b = chanshown;
942         int cnt = 0;
943
944         if (b > MAXSEQ) {
945                 return 0;
946         }
947
948         if(evaluate_seq_frame_gen(seq_arr, seqbasep, cfra)) {
949                 if (b > 0) {
950                         if (seq_arr[b] == 0) {
951                                 return 0;
952                         }
953                 } else {
954                         for (b = MAXSEQ; b > 0; b--) {
955                                 if (video_seq_is_rendered(seq_arr[b])) {
956                                         break;
957                                 }
958                         }
959                 }
960         }
961         
962         chanshown = b;
963
964         for (;b > 0; b--) {
965                 if (video_seq_is_rendered(seq_arr[b])) {
966                         if (seq_arr[b]->blend_mode == SEQ_BLEND_REPLACE) {
967                                 break;
968                         }
969                 }
970         }
971
972         for (;b <= chanshown; b++) {
973                 if (video_seq_is_rendered(seq_arr[b])) {
974                         seq_arr_out[cnt++] = seq_arr[b];
975                 }
976         }
977
978         return cnt;
979 }
980  
981 static int get_shown_seq_from_metastrip(Sequence * seqm, int cfra,
982                                         Sequence ** seq_arr_out)
983 {
984         return get_shown_sequences(&seqm->seqbase, cfra, 0, seq_arr_out);
985 }
986
987 void set_meta_stripdata(Sequence *seqm)
988 {
989         TStripElem *se;
990         int a, cfra;
991
992         se= seqm->strip->tstripdata;
993
994         if (se == 0 && seqm->len > 0) {
995                 int i;
996                 se = seqm->strip->tstripdata = MEM_callocN(
997                         seqm->len*sizeof(TStripElem), "tstripelems");
998                 for (i = 0; i < seqm->len; i++) {
999                         se[i].ok = STRIPELEM_META;
1000                 }
1001         }
1002
1003         /* sets all ->se1 pointers in stripdata, to read the ibuf from it */
1004
1005         for(a=0; a<seqm->len; a++, se++) {
1006                 int cnt;
1007                 Sequence *seq_arr[MAXSEQ+1];
1008
1009                 cfra= a+seqm->start;
1010                 cnt = get_shown_seq_from_metastrip(seqm, cfra, seq_arr);
1011                 if (cnt) {
1012                         se->se1= give_tstripelem(seq_arr[cnt-1], cfra);
1013                 } else { 
1014                         se->se1= 0;
1015                 }
1016         }
1017 }
1018
1019 /* **********************************************************************
1020    proxy management
1021    ********************************************************************** */
1022
1023 #define PROXY_MAXFILE (2*FILE_MAXDIR+FILE_MAXFILE)
1024
1025 static int seq_proxy_get_fname(Sequence * seq, int cfra, char * name)
1026 {
1027         int frameno;
1028         char dir[FILE_MAXDIR];
1029
1030         if (!seq->strip->proxy) {
1031                 return FALSE;
1032         }
1033
1034         if (seq->flag & SEQ_USE_PROXY_CUSTOM_DIR) {
1035                 strcpy(dir, seq->strip->proxy->dir);
1036         } else {
1037                 if (seq->type == SEQ_IMAGE || seq->type == SEQ_MOVIE) {
1038                         snprintf(dir, FILE_MAXDIR, "%s/BL_proxy", 
1039                                  seq->strip->dir);
1040                 } else {
1041                         return FALSE;
1042                 }
1043         }
1044
1045         /* generate a seperate proxy directory for each preview size */
1046
1047         if (seq->type == SEQ_IMAGE) {
1048                 StripElem * se = give_stripelem(seq, cfra);
1049                 snprintf(name, PROXY_MAXFILE, "%s/images/%d/%s_proxy",
1050                          dir, G.scene->r.size, se->name);
1051                 frameno = 1;
1052         } else if (seq->type == SEQ_MOVIE) {
1053                 TStripElem * tse = give_tstripelem(seq, cfra);
1054
1055                 frameno = tse->nr + seq->anim_startofs;
1056
1057                 snprintf(name, PROXY_MAXFILE, "%s/%s/%d/#", dir,
1058                          seq->strip->stripdata->name,
1059                          G.scene->r.size);
1060         } else {
1061                 TStripElem * tse = give_tstripelem(seq, cfra);
1062
1063                 frameno = tse->nr + seq->anim_startofs;
1064
1065                 snprintf(name, PROXY_MAXFILE, "%s/proxy_misc/%d/#", dir,
1066                          G.scene->r.size);
1067         }
1068
1069         BLI_convertstringcode(name, G.sce, frameno);
1070
1071         strcat(name, ".jpg");
1072
1073         return TRUE;
1074 }
1075
1076 static struct ImBuf * seq_proxy_fetch(Sequence * seq, int cfra)
1077 {
1078         char name[PROXY_MAXFILE];
1079
1080         if (!(seq->flag & SEQ_USE_PROXY)) {
1081                 return 0;
1082         }
1083
1084         /* rendering at 100% ? No real sense in proxy-ing, right? */
1085         if (G.scene->r.size == 100.0) {
1086                 return 0;
1087         }
1088
1089         if (!seq_proxy_get_fname(seq, cfra, name)) {
1090                 return 0;
1091         }
1092
1093         if (BLI_exists(name)) {
1094                 return IMB_loadiffname(name, IB_rect);
1095         } else {
1096                 return 0;
1097         }
1098 }
1099
1100 static void do_build_seq_ibuf(Sequence * seq, TStripElem *se, int cfra,
1101                               int build_proxy_run);
1102
1103 static void seq_proxy_build_frame(Sequence * seq, int cfra)
1104 {
1105         char name[PROXY_MAXFILE];
1106         int quality;
1107         TStripElem * se;
1108         int ok;
1109         int rectx, recty;
1110         struct ImBuf * ibuf;
1111
1112         if (!(seq->flag & SEQ_USE_PROXY)) {
1113                 return;
1114         }
1115
1116         /* rendering at 100% ? No real sense in proxy-ing, right? */
1117         if (G.scene->r.size == 100.0) {
1118                 return;
1119         }
1120
1121         if (!seq_proxy_get_fname(seq, cfra, name)) {
1122                 return;
1123         }
1124
1125         se = give_tstripelem(seq, cfra);
1126
1127         if(se->ibuf) {
1128                 IMB_freeImBuf(se->ibuf);
1129                 se->ibuf = 0;
1130         }
1131         
1132         do_build_seq_ibuf(seq, se, cfra, TRUE);
1133
1134         if (!se->ibuf) {
1135                 return;
1136         }
1137
1138         rectx= (G.scene->r.size*G.scene->r.xsch)/100;
1139         recty= (G.scene->r.size*G.scene->r.ysch)/100;
1140
1141         ibuf = se->ibuf;
1142
1143         if (ibuf->x != rectx || ibuf->y != recty) {
1144                 IMB_scalefastImBuf(ibuf, (short)rectx, (short)recty);
1145         }
1146
1147         /* quality is fixed, otherwise one has to generate seperate
1148            directories for every quality...
1149
1150            depth = 32 is intentionally left in, otherwise ALPHA channels
1151            won't work... */
1152         quality = 90;
1153         ibuf->ftype= JPG | quality;
1154
1155         BLI_make_existing_file(name);
1156         
1157         ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
1158         if (ok == 0) {
1159                 perror(name);
1160         }
1161
1162         IMB_freeImBuf(ibuf);
1163         se->ibuf = 0;
1164 }
1165
1166 void seq_proxy_rebuild(Sequence * seq)
1167 {
1168         int cfra;
1169
1170         waitcursor(1);
1171
1172         G.afbreek = 0;
1173
1174         /* flag management tries to account for strobe and 
1175            other "non-linearities", that might come in the future...
1176            better way would be to "touch" the files, so that _really_
1177            no one is rebuild twice.
1178          */
1179
1180         for (cfra = seq->startdisp; cfra < seq->enddisp; cfra++) {
1181                 TStripElem * tse = give_tstripelem(seq, cfra);
1182
1183                 tse->flag &= ~STRIPELEM_PREVIEW_DONE;
1184         }
1185
1186         /* a _lot_ faster for movie files, if we read frames in
1187            sequential order */
1188         if (seq->flag & SEQ_REVERSE_FRAMES) {
1189                 for (cfra = seq->enddisp-seq->endstill-1; 
1190                      cfra >= seq->startdisp + seq->startstill; cfra--) {
1191                         TStripElem * tse = give_tstripelem(seq, cfra);
1192
1193                         if (!(tse->flag & STRIPELEM_PREVIEW_DONE)) {
1194                                 seq_proxy_build_frame(seq, cfra);
1195                                 tse->flag |= STRIPELEM_PREVIEW_DONE;
1196                         }
1197                         if (blender_test_break()) {
1198                                 break;
1199                         }
1200                 }
1201         } else {
1202                 for (cfra = seq->startdisp + seq->startstill; 
1203                      cfra < seq->enddisp - seq->endstill; cfra++) {
1204                         TStripElem * tse = give_tstripelem(seq, cfra);
1205
1206                         if (!(tse->flag & STRIPELEM_PREVIEW_DONE)) {
1207                                 seq_proxy_build_frame(seq, cfra);
1208                                 tse->flag |= STRIPELEM_PREVIEW_DONE;
1209                         }
1210                         if (blender_test_break()) {
1211                                 break;
1212                         }
1213                 }
1214         }
1215         waitcursor(0);
1216 }
1217
1218
1219 /* **********************************************************************
1220    color balance 
1221    ********************************************************************** */
1222
1223 static StripColorBalance calc_cb(StripColorBalance * cb_)
1224 {
1225         StripColorBalance cb = *cb_;
1226         int c;
1227
1228         if (cb.flag & SEQ_COLOR_BALANCE_INVERSE_LIFT) {
1229                 for (c = 0; c < 3; c++) {
1230                         cb.lift[c] = 1.0 - cb.lift[c];
1231                 }
1232         } else {
1233                 for (c = 0; c < 3; c++) {
1234                         cb.lift[c] = -(1.0 - cb.lift[c]);
1235                 }
1236         }
1237         if (cb.flag & SEQ_COLOR_BALANCE_INVERSE_GAIN) {
1238                 for (c = 0; c < 3; c++) {
1239                         if (cb.gain[c] != 0.0) {
1240                                 cb.gain[c] = 1.0/cb.gain[c];
1241                         } else {
1242                                 cb.gain[c] = 1000000; /* should be enough :) */
1243                         }
1244                 }
1245         }
1246
1247         if (!(cb.flag & SEQ_COLOR_BALANCE_INVERSE_GAMMA)) {
1248                 for (c = 0; c < 3; c++) {
1249                         if (cb.gain[c] != 0.0) {
1250                                 cb.gamma[c] = 1.0/cb.gamma[c];
1251                         } else {
1252                                 cb.gamma[c] = 1000000; /* should be enough :) */
1253                         }
1254                 }
1255         }
1256
1257         return cb;
1258 }
1259
1260 static void make_cb_table_byte(float lift, float gain, float gamma,
1261                                unsigned char * table, float mul)
1262 {
1263         int y;
1264
1265         for (y = 0; y < 256; y++) {
1266                 float v = 1.0 * y / 255;
1267                 v += lift; 
1268                 v *= gain;
1269                 v = pow(v, gamma);
1270                 v *= mul;
1271                 if ( v > 1.0) {
1272                         v = 1.0;
1273                 } else if (v < 0.0) {
1274                         v = 0.0;
1275                 }
1276                 table[y] = v * 255;
1277         }
1278
1279 }
1280
1281 static void make_cb_table_float(float lift, float gain, float gamma,
1282                                 float * table, float mul)
1283 {
1284         int y;
1285
1286         for (y = 0; y < 256; y++) {
1287                 float v = (float) y * 1.0 / 255.0;
1288                 v += lift;
1289                 v *= gain;
1290                 v = pow(v, gamma);
1291                 v *= mul;
1292                 table[y] = v;
1293         }
1294 }
1295
1296 static void color_balance_byte_byte(Sequence * seq, TStripElem* se,
1297                                     float mul)
1298 {
1299         unsigned char cb_tab[3][256];
1300         int c;
1301         unsigned char * p = (unsigned char*) se->ibuf->rect;
1302         unsigned char * e = p + se->ibuf->x * 4 * se->ibuf->y;
1303
1304         StripColorBalance cb = calc_cb(seq->strip->color_balance);
1305
1306         for (c = 0; c < 3; c++) {
1307                 make_cb_table_byte(cb.lift[c], cb.gain[c], cb.gamma[c],
1308                                    cb_tab[c], mul);
1309         }
1310
1311         while (p < e) {
1312                 p[0] = cb_tab[0][p[0]];
1313                 p[1] = cb_tab[1][p[1]];
1314                 p[2] = cb_tab[2][p[2]];
1315                 
1316                 p += 4;
1317         }
1318 }
1319
1320 static void color_balance_byte_float(Sequence * seq, TStripElem* se,
1321                                      float mul)
1322 {
1323         float cb_tab[4][256];
1324         int c,i;
1325         unsigned char * p = (unsigned char*) se->ibuf->rect;
1326         unsigned char * e = p + se->ibuf->x * 4 * se->ibuf->y;
1327         float * o;
1328         StripColorBalance cb;
1329
1330         imb_addrectfloatImBuf(se->ibuf);
1331
1332         o = se->ibuf->rect_float;
1333
1334         cb = calc_cb(seq->strip->color_balance);
1335
1336         for (c = 0; c < 3; c++) {
1337                 make_cb_table_float(cb.lift[c], cb.gain[c], cb.gamma[c],
1338                                     cb_tab[c], mul);
1339         }
1340
1341         for (i = 0; i < 256; i++) {
1342                 cb_tab[3][i] = ((float)i)*(1.0f/255.0f);
1343         }
1344
1345         while (p < e) {
1346                 o[0] = cb_tab[0][p[0]];
1347                 o[1] = cb_tab[1][p[1]];
1348                 o[2] = cb_tab[2][p[2]];
1349                 o[3] = cb_tab[3][p[3]];
1350
1351                 p += 4; o += 4;
1352         }
1353 }
1354
1355 static void color_balance_float_float(Sequence * seq, TStripElem* se,
1356                                       float mul)
1357 {
1358         float * p = se->ibuf->rect_float;
1359         float * e = se->ibuf->rect_float + se->ibuf->x * 4* se->ibuf->y;
1360         StripColorBalance cb = calc_cb(seq->strip->color_balance);
1361
1362         while (p < e) {
1363                 int c;
1364                 for (c = 0; c < 3; c++) {
1365                         p[c] = pow((p[c] + cb.lift[c]) * cb.gain[c], 
1366                                    cb.gamma[c]) * mul;
1367                 }
1368                 p += 4;
1369         }
1370 }
1371
1372 static void color_balance(Sequence * seq, TStripElem* se, float mul)
1373 {
1374         if (se->ibuf->rect_float) {
1375                 color_balance_float_float(seq, se, mul);
1376         } else if(seq->flag & SEQ_MAKE_FLOAT) {
1377                 color_balance_byte_float(seq, se, mul);
1378         } else {
1379                 color_balance_byte_byte(seq, se, mul);
1380         }
1381 }
1382
1383 /*
1384   input preprocessing for SEQ_IMAGE, SEQ_MOVIE and SEQ_SCENE
1385
1386   Do all the things you can't really do afterwards using sequence effects
1387   (read: before rescaling to render resolution has been done)
1388
1389   Order is important!
1390
1391   - Deinterlace
1392   - Crop and transform in image source coordinate space
1393   - Flip X + Flip Y (could be done afterwards, backward compatibility)
1394   - Promote image to float data (affects pipeline operations afterwards)
1395   - Color balance (is most efficient in the byte -> float 
1396     (future: half -> float should also work fine!)
1397     case, if done on load, since we can use lookup tables)
1398   - Premultiply
1399
1400 */
1401
1402 static void input_preprocess(Sequence * seq, TStripElem* se, int cfra)
1403 {
1404         float mul;
1405
1406         seq->strip->orx= se->ibuf->x;
1407         seq->strip->ory= se->ibuf->y;
1408
1409         if(seq->flag & SEQ_FILTERY) {
1410                 IMB_filtery(se->ibuf);
1411         }
1412
1413         if(seq->flag & SEQ_USE_CROP || seq->flag & SEQ_USE_TRANSFORM) {
1414                 StripCrop c;
1415                 StripTransform t;
1416
1417                 memset(&c, 0, sizeof(StripCrop));
1418                 memset(&t, 0, sizeof(StripTransform));
1419
1420                 if(seq->flag & SEQ_USE_CROP && seq->strip->crop) {
1421                         c = *seq->strip->crop;
1422                 }
1423                 if(seq->flag & SEQ_USE_TRANSFORM && seq->strip->transform) {
1424                         t = *seq->strip->transform;
1425                 }
1426
1427                 if (c.top + c.bottom >= se->ibuf->y ||
1428                     c.left + c.right >= se->ibuf->x ||
1429                     t.xofs >= se->ibuf->x ||
1430                     t.yofs >= se->ibuf->y) {
1431                         make_black_ibuf(se->ibuf);
1432                 } else {
1433                         ImBuf * i;
1434                         int sx = se->ibuf->x - c.left - c.right;
1435                         int sy = se->ibuf->y - c.top - c.bottom;
1436                         int dx = sx;
1437                         int dy = sy;
1438
1439                         if (seq->flag & SEQ_USE_TRANSFORM) {
1440                                 dx = seqrectx;
1441                                 dy = seqrecty;
1442                         }
1443
1444                         if (se->ibuf->rect_float) {
1445                                 i = IMB_allocImBuf(dx, dy,32, IB_rectfloat, 0);
1446                         } else {
1447                                 i = IMB_allocImBuf(dx, dy,32, IB_rect, 0);
1448                         }
1449
1450                         IMB_rectcpy(i, se->ibuf, 
1451                                     t.xofs, t.yofs, 
1452                                     c.left, c.bottom, 
1453                                     sx, sy);
1454
1455                         IMB_freeImBuf(se->ibuf);
1456
1457                         se->ibuf = i;
1458                 }
1459         } 
1460
1461         if(seq->flag & SEQ_FLIPX) {
1462                 IMB_flipx(se->ibuf);
1463         }
1464         if(seq->flag & SEQ_FLIPY) {
1465                 IMB_flipy(se->ibuf);
1466         }
1467
1468         if(seq->mul == 0.0) {
1469                 seq->mul = 1.0;
1470         }
1471
1472         mul = seq->mul;
1473
1474         if(seq->blend_mode == SEQ_BLEND_REPLACE) {
1475                 if (seq->ipo && seq->ipo->curve.first) {
1476                         do_seq_ipo(seq, cfra);
1477                         mul *= seq->facf0;
1478                 }
1479                 mul *= seq->blend_opacity / 100.0;
1480         }
1481
1482         if(seq->flag & SEQ_USE_COLOR_BALANCE && seq->strip->color_balance) {
1483                 color_balance(seq, se, mul);
1484                 mul = 1.0;
1485         }
1486
1487         if(seq->flag & SEQ_MAKE_FLOAT) {
1488                 if (!se->ibuf->rect_float) {
1489                         IMB_float_from_rect(se->ibuf);
1490                 }
1491                 if (se->ibuf->rect) {
1492                         imb_freerectImBuf(se->ibuf);
1493                 }
1494         }
1495
1496         if(mul != 1.0) {
1497                 multibuf(se->ibuf, mul);
1498         }
1499
1500         if(seq->flag & SEQ_MAKE_PREMUL) {
1501                 if(se->ibuf->depth == 32 && se->ibuf->zbuf == 0) {
1502                         converttopremul(se->ibuf);
1503                 }
1504         }
1505
1506
1507         if(se->ibuf->x != seqrectx || se->ibuf->y != seqrecty ) {
1508                 if(G.scene->r.mode & R_OSA) {
1509                         IMB_scaleImBuf(se->ibuf, 
1510                                        (short)seqrectx, (short)seqrecty);
1511                 } else {
1512                         IMB_scalefastImBuf(se->ibuf, 
1513                                            (short)seqrectx, (short)seqrecty);
1514                 }
1515         }
1516 }
1517
1518 /* test if image too small or discarded from cache: reload */
1519
1520 static void test_and_auto_discard_ibuf(TStripElem * se)
1521 {
1522         if (se->ibuf) {
1523                 if(se->ibuf->x != seqrectx || se->ibuf->y != seqrecty 
1524                    || !(se->ibuf->rect || se->ibuf->rect_float)) {
1525                         IMB_freeImBuf(se->ibuf);
1526
1527                         se->ibuf= 0;
1528                         se->ok= STRIPELEM_OK;
1529                 }
1530         }
1531         if (se->ibuf_comp) {
1532                 if(se->ibuf_comp->x != seqrectx || se->ibuf_comp->y != seqrecty 
1533                    || !(se->ibuf_comp->rect || se->ibuf_comp->rect_float)) {
1534                         IMB_freeImBuf(se->ibuf_comp);
1535
1536                         se->ibuf_comp = 0;
1537                 }
1538         }
1539 }
1540
1541 static TStripElem* do_build_seq_array_recursively(
1542         ListBase *seqbasep, int cfra, int chanshown);
1543
1544 static void do_build_seq_ibuf(Sequence * seq, TStripElem *se, int cfra,
1545                               int build_proxy_run)
1546 {
1547         char name[FILE_MAXDIR+FILE_MAXFILE];
1548
1549
1550         if (seq->type != SEQ_META) {
1551                 test_and_auto_discard_ibuf(se);
1552         }
1553
1554         if(seq->type == SEQ_META) {
1555                 if(seq->seqbase.first) {
1556                         if(cfra < seq->start) {
1557                                 do_build_seq_array_recursively(
1558                                         &seq->seqbase, 
1559                                         seq->start, 0);
1560                         } else if(cfra > seq->start + seq->len - 1) {
1561                                 do_build_seq_array_recursively(
1562                                         &seq->seqbase, 
1563                                         seq->start + seq->len - 1, 0);
1564                         } else {
1565                                 do_build_seq_array_recursively(
1566                                         &seq->seqbase, 
1567                                         cfra, 0);
1568                         }
1569                 }
1570
1571                 se->ok = STRIPELEM_META;
1572                 if(se->se1 == 0) set_meta_stripdata(seq);
1573                 if(se->se1) {
1574                         if(se->ibuf) {
1575                                 IMB_freeImBuf(se->ibuf);
1576                         }
1577                         se->ibuf = se->se1->ibuf_comp;
1578                         if(se->ibuf) {
1579                                 IMB_refImBuf(se->ibuf);
1580                         }
1581                 }
1582         } else if(seq->type & SEQ_EFFECT) {
1583                 /* should the effect be recalculated? */
1584                 
1585                 if (!build_proxy_run && se->ibuf == 0) {
1586                         se->ibuf = seq_proxy_fetch(seq, cfra);
1587                 }
1588
1589                 if(se->ibuf == 0) {
1590                         /* if one of two first inputs are rectfloat, output is float too */
1591                         if((se->se1 && se->se1->ibuf && se->se1->ibuf->rect_float) ||
1592                            (se->se2 && se->se2->ibuf && se->se2->ibuf->rect_float))
1593                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
1594                         else
1595                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
1596                         
1597                         do_effect(cfra, seq, se);
1598                 }
1599         } else if(seq->type == SEQ_IMAGE) {
1600                 if(se->ok == STRIPELEM_OK && se->ibuf == 0) {
1601                         StripElem * s_elem = give_stripelem(seq, cfra);
1602                         
1603                         strncpy(name, seq->strip->dir, FILE_MAXDIR-1);
1604                         strncat(name, s_elem->name, FILE_MAXFILE);
1605                         BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
1606                         if (!build_proxy_run) {
1607                                 se->ibuf = seq_proxy_fetch(seq, cfra);
1608                         }
1609                         if (!se->ibuf) {
1610                                 se->ibuf= IMB_loadiffname(
1611                                         name, IB_rect);
1612                         }
1613                         
1614                         if(se->ibuf == 0) {
1615                                 se->ok = STRIPELEM_FAILED;
1616                         } else if (!build_proxy_run) {
1617                                 input_preprocess(seq, se, cfra);
1618                         }
1619                 }
1620         } else if(seq->type == SEQ_MOVIE) {
1621                 if(se->ok == STRIPELEM_OK && se->ibuf==0) {
1622                         if(!build_proxy_run) {
1623                                 se->ibuf = seq_proxy_fetch(seq, cfra);
1624                         }
1625
1626                         if (se->ibuf == 0) {
1627                                 if(seq->anim==0) {
1628                                         strncpy(name, seq->strip->dir, FILE_MAXDIR-1);
1629                                         strncat(name, seq->strip->stripdata->name, FILE_MAXFILE-1);
1630                                         BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
1631                                 
1632                                         seq->anim = openanim(name, IB_rect);
1633                                 }
1634                                 if(seq->anim) {
1635                                         IMB_anim_set_preseek(seq->anim, seq->anim_preseek);
1636                                         se->ibuf = IMB_anim_absolute(seq->anim, se->nr + seq->anim_startofs);
1637                                 }
1638                         }
1639                         
1640                         if(se->ibuf == 0) {
1641                                 se->ok = STRIPELEM_FAILED;
1642                         } else if (!build_proxy_run) {
1643                                 input_preprocess(seq, se, cfra);
1644                         }
1645                 }
1646         } else if(seq->type == SEQ_SCENE) {     // scene can be NULL after deletions
1647                 int oldcfra = CFRA;
1648                 Scene *sce= seq->scene, *oldsce= G.scene;
1649                 Render *re;
1650                 RenderResult rres;
1651                 int doseq, rendering= G.rendering;
1652                 char scenename[64];
1653                         
1654                 if (se->ibuf==NULL && seq->scene && !build_proxy_run) {
1655                         se->ibuf = seq_proxy_fetch(seq, cfra);
1656                         if (se->ibuf) {
1657                                 input_preprocess(seq, se, cfra);
1658                         }
1659                 }
1660
1661                 if (se->ibuf==NULL && seq->scene) {
1662                         waitcursor(1);
1663                         
1664                         /* Hack! This function can be called from do_render_seq(), in that case
1665                            the seq->scene can already have a Render initialized with same name, 
1666                            so we have to use a default name. (compositor uses G.scene name to
1667                            find render).
1668                            However, when called from within the UI (image preview in sequencer)
1669                            we do want to use scene Render, that way the render result is defined
1670                            for display in render/imagewindow */
1671                         if(rendering) {
1672                                 BLI_strncpy(scenename, sce->id.name+2, 64);
1673                                 strcpy(sce->id.name+2, " do_build_seq_ibuf");
1674                         }
1675                         re= RE_NewRender(sce->id.name);
1676                         
1677                         /* prevent eternal loop */
1678                         doseq= G.scene->r.scemode & R_DOSEQ;
1679                         G.scene->r.scemode &= ~R_DOSEQ;
1680                         
1681                         BIF_init_render_callbacks(re, 0);       /* 0= no display callbacks */
1682                         
1683                         /* hrms, set_scene still needed? work on that... */
1684                         if(sce!=oldsce) set_scene_bg(sce);
1685                         RE_BlenderFrame(re, sce,
1686                                         seq->sfra+se->nr+seq->anim_startofs);
1687                         if(sce!=oldsce) set_scene_bg(oldsce);
1688                         
1689                         /* UGLY WARNING, it is set to zero in  RE_BlenderFrame */
1690                         G.rendering= rendering;
1691                         if(rendering)
1692                                 BLI_strncpy(sce->id.name+2, scenename, 64);
1693                         
1694                         RE_GetResultImage(re, &rres);
1695                         
1696                         if(rres.rectf) {
1697                                 se->ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rectfloat, 0);
1698                                 memcpy(se->ibuf->rect_float, rres.rectf, 4*sizeof(float)*rres.rectx*rres.recty);
1699                                 if(rres.rectz) {
1700                                         addzbuffloatImBuf(se->ibuf);
1701                                         memcpy(se->ibuf->zbuf_float, rres.rectz, sizeof(float)*rres.rectx*rres.recty);
1702                                 }
1703                         } else if (rres.rect32) {
1704                                 se->ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rect, 0);
1705                                 memcpy(se->ibuf->rect, rres.rect32, 4*rres.rectx*rres.recty);
1706                         }
1707                         
1708                         BIF_end_render_callbacks();
1709                         
1710                         /* restore */
1711                         G.scene->r.scemode |= doseq;
1712                         
1713                         if((G.f & G_PLAYANIM)==0) /* bad, is set on do_render_seq */
1714                                 waitcursor(0);
1715                         CFRA = oldcfra;
1716
1717                         if (!build_proxy_run) {
1718                                 input_preprocess(seq, se, cfra);
1719                         }
1720
1721                 }       
1722         }
1723         if (!build_proxy_run) {
1724                 if (se->ibuf && seq->type != SEQ_META) {
1725                         IMB_cache_limiter_insert(se->ibuf);
1726                         IMB_cache_limiter_ref(se->ibuf);
1727                         IMB_cache_limiter_touch(se->ibuf);
1728                 }
1729         }
1730 }
1731
1732 static TStripElem* do_build_seq_recursively(Sequence * seq, int cfra);
1733
1734 static void do_effect_seq_recursively(Sequence * seq, TStripElem *se, int cfra)
1735 {
1736         float fac, facf;
1737         struct SeqEffectHandle sh = get_sequence_effect(seq);
1738         int early_out;
1739
1740         se->se1 = 0;
1741         se->se2 = 0;
1742         se->se3 = 0;
1743
1744         if(seq->ipo && seq->ipo->curve.first) {
1745                 do_seq_ipo(seq, cfra);
1746                 fac= seq->facf0;
1747                 facf= seq->facf1;
1748         } else {
1749                 sh.get_default_fac(seq, cfra, &fac, &facf);
1750         } 
1751
1752         if( G.scene->r.mode & R_FIELDS ); else facf= fac;
1753         
1754         early_out = sh.early_out(seq, fac, facf);
1755         switch (early_out) {
1756         case -1:
1757                 /* no input needed */
1758                 break;
1759         case 0:
1760                 se->se1 = do_build_seq_recursively(seq->seq1, cfra);
1761                 se->se2 = do_build_seq_recursively(seq->seq2, cfra);
1762                 if (seq->seq3) {
1763                         se->se3 = do_build_seq_recursively(seq->seq3, cfra);
1764                 }
1765                 break;
1766         case 1:
1767                 se->se1 = do_build_seq_recursively(seq->seq1, cfra);
1768                 break;
1769         case 2:
1770                 se->se2 = do_build_seq_recursively(seq->seq2, cfra);
1771                 break;
1772         }
1773
1774
1775         do_build_seq_ibuf(seq, se, cfra, FALSE);
1776
1777         /* children are not needed anymore ... */
1778
1779         if (se->se1 && se->se1->ibuf) {
1780                 IMB_cache_limiter_unref(se->se1->ibuf);
1781         }
1782         if (se->se2 && se->se2->ibuf) {
1783                 IMB_cache_limiter_unref(se->se2->ibuf);
1784         }
1785         if (se->se3 && se->se3->ibuf) {
1786                 IMB_cache_limiter_unref(se->se3->ibuf);
1787         }
1788 }
1789
1790 static TStripElem* do_build_seq_recursively_impl(Sequence * seq, int cfra)
1791 {
1792         TStripElem *se;
1793
1794         se = give_tstripelem(seq, cfra);
1795
1796         if(se) {
1797                 if (seq->type & SEQ_EFFECT) {
1798                         do_effect_seq_recursively(seq, se, cfra);
1799                 } else {
1800                         do_build_seq_ibuf(seq, se, cfra, FALSE);
1801                 }
1802         }
1803         return se;
1804 }
1805
1806 /* FIXME:
1807    
1808 If cfra was float throughout blender (especially in the render
1809 pipeline) one could even _render_ with subframe precision
1810 instead of faking using the blend code below...
1811
1812 */
1813
1814 static TStripElem* do_handle_speed_effect(Sequence * seq, int cfra)
1815 {
1816         SpeedControlVars * s = (SpeedControlVars *)seq->effectdata;
1817         int nr = cfra - seq->start;
1818         float f_cfra;
1819         int cfra_left;
1820         int cfra_right;
1821         TStripElem * se = 0;
1822         TStripElem * se1 = 0;
1823         TStripElem * se2 = 0;
1824         
1825         sequence_effect_speed_rebuild_map(seq, 0);
1826         
1827         f_cfra = seq->start + s->frameMap[nr];
1828         
1829         cfra_left = (int) floor(f_cfra);
1830         cfra_right = (int) ceil(f_cfra);
1831
1832         se = give_tstripelem(seq, cfra);
1833
1834         if (cfra_left == cfra_right || 
1835             (s->flags & SEQ_SPEED_BLEND) == 0) {
1836                 test_and_auto_discard_ibuf(se);
1837
1838                 if (se->ibuf == NULL) {
1839                         se1 = do_build_seq_recursively_impl(
1840                                 seq->seq1, cfra_left);
1841
1842                         if((se1 && se1->ibuf && se1->ibuf->rect_float))
1843                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
1844                         else
1845                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
1846
1847                         if (se1 == 0 || se1->ibuf == 0) {
1848                                 make_black_ibuf(se->ibuf);
1849                         } else {
1850                                 if (se->ibuf != se1->ibuf) {
1851                                         if (se->ibuf) {
1852                                                 IMB_freeImBuf(se->ibuf);
1853                                         }
1854
1855                                         se->ibuf = se1->ibuf;
1856                                         IMB_refImBuf(se->ibuf);
1857                                 }
1858                         }
1859                 }
1860         } else {
1861                 struct SeqEffectHandle sh;
1862
1863                 if(se->ibuf) {
1864                         if(se->ibuf->x < seqrectx || se->ibuf->y < seqrecty 
1865                            || !(se->ibuf->rect || se->ibuf->rect_float)) {
1866                                 IMB_freeImBuf(se->ibuf);
1867                                 se->ibuf= 0;
1868                         }
1869                 }
1870
1871                 if (se->ibuf == NULL) {
1872                         se1 = do_build_seq_recursively_impl(
1873                                 seq->seq1, cfra_left);
1874                         se2 = do_build_seq_recursively_impl(
1875                                 seq->seq1, cfra_right);
1876
1877                         if((se1 && se1->ibuf && se1->ibuf->rect_float))
1878                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
1879                         else
1880                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
1881                         
1882                         if (!se1 || !se2) {
1883                                 make_black_ibuf(se->ibuf);
1884                         } else {
1885                                 sh = get_sequence_effect(seq);
1886
1887                                 sh.execute(seq, cfra, 
1888                                            f_cfra - (float) cfra_left, 
1889                                            f_cfra - (float) cfra_left, 
1890                                            se->ibuf->x, se->ibuf->y, 
1891                                            se1->ibuf, se2->ibuf, 0, se->ibuf);
1892                         }
1893                 }
1894
1895         }
1896
1897         /* caller expects this to be referenced, so do it! */
1898         if (se->ibuf) {
1899                 IMB_cache_limiter_insert(se->ibuf);
1900                 IMB_cache_limiter_ref(se->ibuf);
1901                 IMB_cache_limiter_touch(se->ibuf);
1902         }
1903
1904         /* children are no longer needed */
1905         if (se1 && se1->ibuf)
1906                 IMB_cache_limiter_unref(se1->ibuf);
1907         if (se2 && se2->ibuf)
1908                 IMB_cache_limiter_unref(se2->ibuf);
1909
1910         return se;
1911 }
1912
1913 /* 
1914  * build all ibufs recursively
1915  * 
1916  * if successfull, the returned TStripElem contains the (referenced!) imbuf
1917  * that means: you _must_ call 
1918  *
1919  * IMB_cache_limiter_unref(rval);
1920  * 
1921  * if rval != 0
1922  * 
1923  */
1924
1925 static TStripElem* do_build_seq_recursively(Sequence * seq, int cfra)
1926 {
1927         if (seq->type == SEQ_SPEED) {
1928                 return do_handle_speed_effect(seq, cfra);
1929         } else {
1930                 return do_build_seq_recursively_impl(seq, cfra);
1931         }
1932 }
1933
1934 static TStripElem* do_build_seq_array_recursively(
1935         ListBase *seqbasep, int cfra, int chanshown)
1936 {
1937         Sequence* seq_arr[MAXSEQ+1];
1938         int count;
1939         int i;
1940         TStripElem* se = 0;
1941
1942         count = get_shown_sequences(seqbasep, cfra, chanshown, (Sequence **)&seq_arr);
1943
1944         if (!count) {
1945                 return 0;
1946         }
1947
1948         se = give_tstripelem(seq_arr[count - 1], cfra);
1949
1950         test_and_auto_discard_ibuf(se);
1951
1952         if (se->ibuf_comp != 0) {
1953                 IMB_cache_limiter_insert(se->ibuf_comp);
1954                 IMB_cache_limiter_ref(se->ibuf_comp);
1955                 IMB_cache_limiter_touch(se->ibuf_comp);
1956                 return se;
1957         }
1958
1959         
1960         if(count == 1) {
1961                 se = do_build_seq_recursively(seq_arr[0], cfra);
1962                 if (se->ibuf) {
1963                         se->ibuf_comp = se->ibuf;
1964                         IMB_refImBuf(se->ibuf_comp);
1965                 }
1966                 return se;
1967         }
1968
1969
1970         for (i = count - 1; i >= 0; i--) {
1971                 int early_out;
1972                 Sequence * seq = seq_arr[i];
1973                 struct SeqEffectHandle sh;
1974
1975                 se = give_tstripelem(seq, cfra);
1976
1977                 test_and_auto_discard_ibuf(se);
1978
1979                 if (se->ibuf_comp != 0) {
1980                         break;
1981                 }
1982                 if (seq->blend_mode == SEQ_BLEND_REPLACE) {
1983                         do_build_seq_recursively(seq, cfra);
1984                         if (se->ibuf) {
1985                                 se->ibuf_comp = se->ibuf;
1986                                 IMB_refImBuf(se->ibuf);
1987                         } else {
1988                                 se->ibuf_comp = IMB_allocImBuf(
1989                                         (short)seqrectx, (short)seqrecty, 
1990                                         32, IB_rect, 0);
1991                         }
1992                         break;
1993                 }
1994
1995                 sh = get_sequence_blend(seq);
1996
1997                 seq->facf0 = seq->facf1 = 1.0;
1998
1999                 if(seq->ipo && seq->ipo->curve.first) {
2000                         do_seq_ipo(seq, cfra);
2001                 } 
2002
2003                 if( G.scene->r.mode & R_FIELDS ); else seq->facf0 = seq->facf1;
2004
2005                 seq->facf0 *= seq->blend_opacity / 100.0;
2006                 seq->facf1 *= seq->blend_opacity / 100.0;
2007
2008                 early_out = sh.early_out(seq, seq->facf0, seq->facf1);
2009
2010                 switch (early_out) {
2011                 case -1:
2012                 case 2:
2013                         do_build_seq_recursively(seq, cfra);
2014                         if (se->ibuf) {
2015                                 se->ibuf_comp = se->ibuf;
2016                                 IMB_refImBuf(se->ibuf_comp);
2017                         } else {
2018                                 se->ibuf_comp = IMB_allocImBuf(
2019                                         (short)seqrectx, (short)seqrecty, 
2020                                         32, IB_rect, 0);
2021                         }
2022                         break;
2023                 case 1:
2024                         if (i == 0) {
2025                                 se->ibuf_comp = IMB_allocImBuf(
2026                                         (short)seqrectx, (short)seqrecty, 
2027                                         32, IB_rect, 0);
2028                                 IMB_cache_limiter_insert(se->ibuf_comp);
2029                                 IMB_cache_limiter_ref(se->ibuf_comp);
2030                                 IMB_cache_limiter_touch(se->ibuf_comp);
2031                         }
2032                         break;
2033                 case 0:
2034                         do_build_seq_recursively(seq, cfra);
2035                         if (!se->ibuf) {
2036                                 se->ibuf = IMB_allocImBuf(
2037                                         (short)seqrectx, (short)seqrecty, 
2038                                         32, IB_rect, 0);
2039                         }
2040                         if (i == 0) {
2041                                 se->ibuf_comp = se->ibuf;
2042                                 IMB_refImBuf(se->ibuf_comp);
2043                         }
2044                         break;
2045                 }
2046         
2047                 if (se->ibuf_comp) {
2048                         break;
2049                 }
2050         }
2051
2052         i++;
2053
2054         for (; i < count; i++) {
2055                 Sequence * seq = seq_arr[i];
2056                 struct SeqEffectHandle sh = get_sequence_blend(seq);
2057                 TStripElem* se1 = give_tstripelem(seq_arr[i-1], cfra);
2058                 TStripElem* se2 = give_tstripelem(seq_arr[i], cfra);
2059         
2060                 int early_out = sh.early_out(seq, seq->facf0, seq->facf1);
2061                 switch (early_out) {
2062                 case 0: {
2063                         int x= se2->ibuf->x;
2064                         int y= se2->ibuf->y;
2065                         int swap_input = FALSE;
2066
2067                         if (se1->ibuf_comp->rect_float ||
2068                             se2->ibuf->rect_float) {
2069                                 se2->ibuf_comp = IMB_allocImBuf(
2070                                         (short)seqrectx, (short)seqrecty, 
2071                                         32, IB_rectfloat, 0);
2072                         } else {
2073                                 se2->ibuf_comp = IMB_allocImBuf(
2074                                         (short)seqrectx, (short)seqrecty, 
2075                                         32, IB_rect, 0);
2076                         }
2077
2078
2079                         if (!se1->ibuf_comp->rect_float && 
2080                             se2->ibuf_comp->rect_float) {
2081                                 IMB_float_from_rect(se1->ibuf_comp);
2082                         }
2083                         if (!se2->ibuf->rect_float && 
2084                             se2->ibuf_comp->rect_float) {
2085                                 IMB_float_from_rect(se2->ibuf);
2086                         }
2087
2088                         if (!se1->ibuf_comp->rect && 
2089                             !se2->ibuf_comp->rect_float) {
2090                                 IMB_rect_from_float(se1->ibuf_comp);
2091                         }
2092                         if (!se2->ibuf->rect && 
2093                             !se2->ibuf_comp->rect_float) {
2094                                 IMB_rect_from_float(se2->ibuf);
2095                         }
2096
2097                         /* bad hack, to fix crazy input ordering of 
2098                            those two effects */
2099
2100                         if (seq->blend_mode == SEQ_ALPHAOVER ||
2101                             seq->blend_mode == SEQ_ALPHAUNDER ||
2102                             seq->blend_mode == SEQ_OVERDROP) {
2103                                 swap_input = TRUE;
2104                         }
2105
2106                         if (swap_input) {
2107                                 sh.execute(seq, cfra, 
2108                                            seq->facf0, seq->facf1, x, y, 
2109                                            se2->ibuf, se1->ibuf_comp, 0,
2110                                            se2->ibuf_comp);
2111                         } else {
2112                                 sh.execute(seq, cfra, 
2113                                            seq->facf0, seq->facf1, x, y, 
2114                                            se1->ibuf_comp, se2->ibuf, 0,
2115                                            se2->ibuf_comp);
2116                         }
2117                         
2118                         IMB_cache_limiter_insert(se2->ibuf_comp);
2119                         IMB_cache_limiter_ref(se2->ibuf_comp);
2120                         IMB_cache_limiter_touch(se2->ibuf_comp);
2121
2122                         IMB_cache_limiter_unref(se1->ibuf_comp);
2123                         IMB_cache_limiter_unref(se2->ibuf);
2124
2125                         break;
2126                 }
2127                 case 1: {
2128                         se2->ibuf_comp = se1->ibuf;
2129                         IMB_refImBuf(se2->ibuf_comp);
2130
2131                         break;
2132                 }
2133                 }
2134                 se = se2;
2135         }
2136
2137         return se;
2138 }
2139
2140 /*
2141  * returned ImBuf is refed!
2142  * you have to unref after usage!
2143  */
2144
2145 static ImBuf *give_ibuf_seq_impl(int rectx, int recty, int cfra, int chanshown)
2146 {
2147         Editing *ed;
2148         int count;
2149         ListBase *seqbasep;
2150         TStripElem *se;
2151
2152         ed= G.scene->ed;
2153         if(ed==0) return 0;
2154
2155         count = BLI_countlist(&ed->metastack);
2156         if((chanshown < 0) && (count > 0)) {
2157                 count = MAX2(count + chanshown, 0);
2158                 seqbasep= ((MetaStack*)BLI_findlink(&ed->metastack, count))->oldbasep;
2159         } else {
2160                 seqbasep= ed->seqbasep;
2161         }
2162
2163         seqrectx= rectx;        /* bad bad global! */
2164         seqrecty= recty;
2165
2166         se = do_build_seq_array_recursively(seqbasep, cfra, chanshown);
2167
2168         if(!se) { 
2169                 return 0;
2170         }
2171
2172         return se->ibuf_comp;
2173 }
2174
2175 ImBuf *give_ibuf_seq_direct(int rectx, int recty, int cfra,
2176                             Sequence * seq)
2177 {
2178         TStripElem* se;
2179
2180         seqrectx= rectx;        /* bad bad global! */
2181         seqrecty= recty;
2182
2183         se = do_build_seq_recursively(seq, cfra);
2184
2185         if(!se) { 
2186                 return 0;
2187         }
2188
2189         if (se->ibuf) {
2190                 IMB_cache_limiter_unref(se->ibuf);
2191         }
2192
2193         return se->ibuf;
2194 }
2195
2196 ImBuf *give_ibuf_seq(int rectx, int recty, int cfra, int chanshown)
2197 {
2198         ImBuf* i = give_ibuf_seq_impl(rectx, recty, cfra, chanshown);
2199
2200         if (i) {
2201                 IMB_cache_limiter_unref(i);
2202         }
2203         return i;
2204 }
2205
2206 /* threading api */
2207
2208 static ListBase running_threads;
2209 static ListBase prefetch_wait;
2210 static ListBase prefetch_done;
2211
2212 static pthread_mutex_t queue_lock          = PTHREAD_MUTEX_INITIALIZER;
2213 static pthread_mutex_t wakeup_lock         = PTHREAD_MUTEX_INITIALIZER;
2214 static pthread_cond_t  wakeup_cond         = PTHREAD_COND_INITIALIZER;
2215
2216 static pthread_mutex_t prefetch_ready_lock = PTHREAD_MUTEX_INITIALIZER;
2217 static pthread_cond_t  prefetch_ready_cond = PTHREAD_COND_INITIALIZER;
2218
2219 static pthread_mutex_t frame_done_lock     = PTHREAD_MUTEX_INITIALIZER;
2220 static pthread_cond_t  frame_done_cond     = PTHREAD_COND_INITIALIZER;
2221
2222 static volatile int seq_thread_shutdown = FALSE;
2223 static volatile int seq_last_given_monoton_cfra = 0;
2224 static int monoton_cfra = 0;
2225
2226 typedef struct PrefetchThread {
2227         struct PrefetchThread *next, *prev;
2228         struct PrefetchQueueElem *current;
2229         pthread_t pthread;
2230         int running;
2231 } PrefetchThread;
2232
2233 typedef struct PrefetchQueueElem {
2234         struct PrefetchQueueElem *next, *prev;
2235         
2236         int rectx;
2237         int recty;
2238         int cfra;
2239         int chanshown;
2240
2241         int monoton_cfra;
2242
2243         struct ImBuf * ibuf;
2244 } PrefetchQueueElem;
2245
2246 static void * seq_prefetch_thread(void * This_)
2247 {
2248         PrefetchThread * This = This_;
2249
2250         while (!seq_thread_shutdown) {
2251                 PrefetchQueueElem * e;
2252                 int s_last;
2253
2254                 pthread_mutex_lock(&queue_lock);
2255                 e = prefetch_wait.first;
2256                 if (e) {
2257                         BLI_remlink(&prefetch_wait, e);
2258                 }
2259                 s_last = seq_last_given_monoton_cfra;
2260
2261                 This->current = e;
2262
2263                 pthread_mutex_unlock(&queue_lock);
2264
2265                 if (!e) {
2266                         pthread_mutex_lock(&prefetch_ready_lock);
2267
2268                         This->running = FALSE;
2269
2270                         pthread_cond_signal(&prefetch_ready_cond);
2271                         pthread_mutex_unlock(&prefetch_ready_lock);
2272
2273                         pthread_mutex_lock(&wakeup_lock);
2274                         if (!seq_thread_shutdown) {
2275                                 pthread_cond_wait(&wakeup_cond, &wakeup_lock);
2276                         }
2277                         pthread_mutex_unlock(&wakeup_lock);
2278                         continue;
2279                 }
2280
2281                 This->running = TRUE;
2282                 
2283                 if (e->cfra >= s_last) { 
2284                         e->ibuf = give_ibuf_seq_impl(
2285                                 e->rectx, e->recty, e->cfra, e->chanshown);
2286                 }
2287
2288                 pthread_mutex_lock(&queue_lock);
2289
2290                 BLI_addtail(&prefetch_done, e);
2291
2292                 for (e = prefetch_wait.first; e; e = e->next) {
2293                         if (s_last > e->monoton_cfra) {
2294                                 BLI_remlink(&prefetch_wait, e);
2295                                 MEM_freeN(e);
2296                         }
2297                 }
2298
2299                 for (e = prefetch_done.first; e; e = e->next) {
2300                         if (s_last > e->monoton_cfra) {
2301                                 if (e->ibuf) {
2302                                         IMB_cache_limiter_unref(e->ibuf);
2303                                 }
2304                                 BLI_remlink(&prefetch_done, e);
2305                                 MEM_freeN(e);
2306                         }
2307                 }
2308
2309                 pthread_mutex_unlock(&queue_lock);
2310
2311                 pthread_mutex_lock(&frame_done_lock);
2312                 pthread_cond_signal(&frame_done_cond);
2313                 pthread_mutex_unlock(&frame_done_lock);
2314         }
2315         return 0;
2316 }
2317
2318 void seq_start_threads()
2319 {
2320         int i;
2321
2322         running_threads.first = running_threads.last = NULL;
2323         prefetch_wait.first = prefetch_wait.last = NULL;
2324         prefetch_done.first = prefetch_done.last = NULL;
2325
2326         seq_thread_shutdown = FALSE;
2327         seq_last_given_monoton_cfra = monoton_cfra = 0;
2328
2329         /* since global structures are modified during the processing
2330            of one frame, only one render thread is currently possible... 
2331
2332            (but we code, in the hope, that we can remove this restriction
2333            soon...)
2334         */
2335
2336         fprintf(stderr, "SEQ-THREAD: seq_start_threads\n");
2337
2338         for (i = 0; i < 1; i++) {
2339                 PrefetchThread *t = MEM_callocN(sizeof(PrefetchThread), 
2340                                                 "prefetch_thread");
2341                 t->running = TRUE;
2342                 BLI_addtail(&running_threads, t);
2343
2344                 pthread_create(&t->pthread, NULL, seq_prefetch_thread, t);
2345         }
2346
2347         /* init malloc mutex */
2348         BLI_init_threads(0, 0, 0);
2349 }
2350
2351 void seq_stop_threads()
2352 {
2353         PrefetchThread *tslot;
2354         PrefetchQueueElem * e;
2355
2356         fprintf(stderr, "SEQ-THREAD: seq_stop_threads()\n");
2357
2358         if (seq_thread_shutdown) {
2359                 fprintf(stderr, "SEQ-THREAD: ... already stopped\n");
2360                 return;
2361         }
2362         
2363         pthread_mutex_lock(&wakeup_lock);
2364
2365         seq_thread_shutdown = TRUE;
2366
2367         pthread_cond_broadcast(&wakeup_cond);
2368         pthread_mutex_unlock(&wakeup_lock);
2369
2370         for(tslot = running_threads.first; tslot; tslot= tslot->next) {
2371                 pthread_join(tslot->pthread, NULL);
2372         }
2373
2374
2375         for (e = prefetch_wait.first; e; e = e->next) {
2376                 BLI_remlink(&prefetch_wait, e);
2377                 MEM_freeN(e);
2378         }
2379
2380         for (e = prefetch_done.first; e; e = e->next) {
2381                 if (e->ibuf) {
2382                         IMB_cache_limiter_unref(e->ibuf);
2383                 }
2384                 BLI_remlink(&prefetch_done, e);
2385                 MEM_freeN(e);
2386         }
2387
2388         BLI_freelistN(&running_threads);
2389
2390         /* deinit malloc mutex */
2391         BLI_end_threads(0);
2392 }
2393
2394 void give_ibuf_prefetch_request(int rectx, int recty, int cfra, int chanshown)
2395 {
2396         PrefetchQueueElem * e;
2397         if (seq_thread_shutdown) {
2398                 return;
2399         }
2400
2401         e = MEM_callocN(sizeof(PrefetchQueueElem), "prefetch_queue_elem");
2402         e->rectx = rectx;
2403         e->recty = recty;
2404         e->cfra = cfra;
2405         e->chanshown = chanshown;
2406         e->monoton_cfra = monoton_cfra++;
2407
2408         pthread_mutex_lock(&queue_lock);
2409         BLI_addtail(&prefetch_wait, e);
2410         pthread_mutex_unlock(&queue_lock);
2411         
2412         pthread_mutex_lock(&wakeup_lock);
2413         pthread_cond_signal(&wakeup_cond);
2414         pthread_mutex_unlock(&wakeup_lock);
2415 }
2416
2417 void seq_wait_for_prefetch_ready()
2418 {
2419         PrefetchThread *tslot;
2420
2421         if (seq_thread_shutdown) {
2422                 return;
2423         }
2424
2425         fprintf(stderr, "SEQ-THREAD: rendering prefetch frames...\n");
2426
2427         pthread_mutex_lock(&prefetch_ready_lock);
2428
2429         for(;;) {
2430                 for(tslot = running_threads.first; tslot; tslot= tslot->next) {
2431                         if (tslot->running) {
2432                                 break;
2433                         }
2434                 }
2435                 if (!tslot) {
2436                         break;
2437                 }
2438                 pthread_cond_wait(&prefetch_ready_cond, &prefetch_ready_lock);
2439         }
2440
2441         pthread_mutex_unlock(&prefetch_ready_lock);
2442
2443         fprintf(stderr, "SEQ-THREAD: prefetch done\n");
2444 }
2445
2446 ImBuf * give_ibuf_seq_threaded(int rectx, int recty, int cfra, int chanshown)
2447 {
2448         PrefetchQueueElem * e = 0;
2449         int found_something = FALSE;
2450
2451         if (seq_thread_shutdown) {
2452                 return give_ibuf_seq(rectx, recty, cfra, chanshown);
2453         }
2454
2455         while (!e) {
2456                 int success = FALSE;
2457                 pthread_mutex_lock(&queue_lock);
2458
2459                 for (e = prefetch_done.first; e; e = e->next) {
2460                         if (cfra == e->cfra &&
2461                             chanshown == e->chanshown &&
2462                             rectx == e->rectx && 
2463                             recty == e->recty) {
2464                                 success = TRUE;
2465                                 found_something = TRUE;
2466                                 break;
2467                         }
2468                 }
2469
2470                 if (!e) {
2471                         for (e = prefetch_wait.first; e; e = e->next) {
2472                                 if (cfra == e->cfra &&
2473                                     chanshown == e->chanshown &&
2474                                     rectx == e->rectx && 
2475                                     recty == e->recty) {
2476                                         found_something = TRUE;
2477                                         break;
2478                                 }
2479                         }
2480                 }
2481
2482                 if (!e) {
2483                         PrefetchThread *tslot;
2484
2485                         for(tslot = running_threads.first; 
2486                             tslot; tslot= tslot->next) {
2487                                 if (tslot->current &&
2488                                     cfra == tslot->current->cfra &&
2489                                     chanshown == tslot->current->chanshown &&
2490                                     rectx == tslot->current->rectx && 
2491                                     recty == tslot->current->recty) {
2492                                         found_something = TRUE;
2493                                         break;
2494                                 }
2495                         }
2496                 }
2497
2498                 /* e->ibuf is unrefed by render thread on next round. */
2499
2500                 if (e) {
2501                         seq_last_given_monoton_cfra = e->monoton_cfra;
2502                 }
2503
2504                 pthread_mutex_unlock(&queue_lock);
2505
2506                 if (!success) {
2507                         e = NULL;
2508
2509                         if (!found_something) {
2510                                 fprintf(stderr, 
2511                                         "SEQ-THREAD: Requested frame "
2512                                         "not in queue ???\n");
2513                                 break;
2514                         }
2515                         pthread_mutex_lock(&frame_done_lock);
2516                         pthread_cond_wait(&frame_done_cond, &frame_done_lock);
2517                         pthread_mutex_unlock(&frame_done_lock);
2518                 }
2519         }
2520         
2521         return e ? e->ibuf : 0;
2522 }
2523
2524 /* Functions to free imbuf and anim data on changes */
2525
2526 static void free_imbuf_strip_elem(TStripElem *se)
2527 {
2528         if(se->ibuf) {
2529                 IMB_freeImBuf(se->ibuf);
2530         }
2531         if(se->ibuf_comp) {
2532                 IMB_freeImBuf(se->ibuf_comp);
2533         }
2534         se->ibuf_comp = 0;
2535         se->ibuf= 0;
2536         se->ok= STRIPELEM_OK;
2537         se->se1= se->se2= se->se3= 0;
2538 }
2539
2540 static void free_anim_seq(Sequence *seq)
2541 {
2542         if(seq->anim) {
2543                 IMB_free_anim(seq->anim);
2544                 seq->anim = 0;
2545         }
2546 }
2547
2548 void free_imbuf_seq_except(int cfra)
2549 {
2550         Editing *ed= G.scene->ed;
2551         Sequence *seq;
2552         TStripElem *se;
2553         int a;
2554
2555         if(ed==0) return;
2556
2557         WHILE_SEQ(&ed->seqbase) {
2558                 if(seq->strip) {
2559                         TStripElem * curelem = give_tstripelem(seq, cfra);
2560
2561                         for(a = 0, se = seq->strip->tstripdata; 
2562                             a < seq->strip->len && se; a++, se++) {
2563                                 if(se != curelem) {
2564                                         free_imbuf_strip_elem(se);
2565                                 }
2566                         }
2567                         for(a = 0, se = seq->strip->tstripdata_startstill;
2568                             a < seq->strip->startstill && se; a++, se++) {
2569                                 if(se != curelem) {
2570                                         free_imbuf_strip_elem(se);
2571                                 }
2572                         }
2573                         for(a = 0, se = seq->strip->tstripdata_endstill;
2574                             a < seq->strip->endstill && se; a++, se++) {
2575                                 if(se != curelem) {
2576                                         free_imbuf_strip_elem(se);
2577                                 }
2578                         }
2579
2580                         if(seq->type==SEQ_MOVIE)
2581                                 if(seq->startdisp > cfra || seq->enddisp < cfra)
2582                                         free_anim_seq(seq);
2583                 }
2584         }
2585         END_SEQ
2586 }
2587
2588 void free_imbuf_seq()
2589 {
2590         Editing *ed= G.scene->ed;
2591         Sequence *seq;
2592         TStripElem *se;
2593         int a;
2594
2595         if(ed==0) return;
2596
2597         WHILE_SEQ(&ed->seqbase) {
2598                 if(seq->strip) {
2599                         for(a = 0, se = seq->strip->tstripdata; 
2600                             a < seq->strip->len && se; a++, se++) {
2601                                 free_imbuf_strip_elem(se);
2602                         }
2603                         for(a = 0, se = seq->strip->tstripdata_startstill; 
2604                             a < seq->strip->startstill && se; a++, se++) {
2605                                 free_imbuf_strip_elem(se);
2606                         }
2607                         for(a = 0, se = seq->strip->tstripdata_endstill; 
2608                             a < seq->strip->endstill && se; a++, se++) {
2609                                 free_imbuf_strip_elem(se);
2610                         }
2611
2612                         if(seq->type==SEQ_MOVIE)
2613                                 free_anim_seq(seq);
2614                         if(seq->type==SEQ_SPEED) {
2615                                 sequence_effect_speed_rebuild_map(seq, 1);
2616                         }
2617                 }
2618         }
2619         END_SEQ
2620 }
2621
2622 void free_imbuf_seq_with_ipo(struct Ipo *ipo)
2623 {
2624         /* force update of all sequences with this ipo, on ipo changes */
2625         Editing *ed= G.scene->ed;
2626         Sequence *seq;
2627
2628         if(ed==0) return;
2629
2630         WHILE_SEQ(&ed->seqbase) {
2631                 if(seq->ipo == ipo) {
2632                         update_changed_seq_and_deps(seq, 0, 1);
2633                         if(seq->type == SEQ_SPEED) {
2634                                 sequence_effect_speed_rebuild_map(seq, 1);
2635                         }
2636                 }
2637         }
2638         END_SEQ
2639 }
2640
2641 static int update_changed_seq_recurs(Sequence *seq, Sequence *changed_seq, int len_change, int ibuf_change)
2642 {
2643         Sequence *subseq;
2644         int a, free_imbuf = 0;
2645         TStripElem *se;
2646
2647         /* recurs downwards to see if this seq depends on the changed seq */
2648
2649         if(seq == NULL)
2650                 return 0;
2651
2652         if(seq == changed_seq)
2653                 free_imbuf = 1;
2654         
2655         for(subseq=seq->seqbase.first; subseq; subseq=subseq->next)
2656                 if(update_changed_seq_recurs(subseq, changed_seq, len_change, ibuf_change))
2657                         free_imbuf = TRUE;
2658         
2659         if(seq->seq1)
2660                 if(update_changed_seq_recurs(seq->seq1, changed_seq, len_change, ibuf_change))
2661                         free_imbuf = TRUE;
2662         if(seq->seq2 && (seq->seq2 != seq->seq1))
2663                 if(update_changed_seq_recurs(seq->seq2, changed_seq, len_change, ibuf_change))
2664                         free_imbuf = TRUE;
2665         if(seq->seq3 && (seq->seq3 != seq->seq1) && (seq->seq3 != seq->seq2))
2666                 if(update_changed_seq_recurs(seq->seq3, changed_seq, len_change, ibuf_change))
2667                         free_imbuf = TRUE;
2668         
2669         if(free_imbuf) {
2670                 if(ibuf_change) {
2671                         se= seq->strip->tstripdata;
2672                         if (se) {
2673                                 for(a=0; a<seq->len; a++, se++)
2674                                         free_imbuf_strip_elem(se);
2675                         }
2676                 
2677                         if(seq->type == SEQ_MOVIE)
2678                                 free_anim_seq(seq);
2679                         if(seq->type == SEQ_SPEED) {
2680                                 sequence_effect_speed_rebuild_map(seq, 1);
2681                         }
2682                 }
2683
2684                 if(len_change)
2685                         calc_sequence(seq);
2686         }
2687         
2688         return free_imbuf;
2689 }
2690
2691 void update_changed_seq_and_deps(Sequence *changed_seq, int len_change, int ibuf_change)
2692 {
2693         Editing *ed= G.scene->ed;
2694         Sequence *seq;
2695
2696         if (!ed) return;
2697
2698         for (seq=ed->seqbase.first; seq; seq=seq->next)
2699                 update_changed_seq_recurs(seq, changed_seq, len_change, ibuf_change);
2700 }
2701
2702 /* bad levell call... */
2703 void do_render_seq(RenderResult *rr, int cfra)
2704 {
2705         ImBuf *ibuf;
2706
2707         G.f |= G_PLAYANIM;      /* waitcursor patch */
2708
2709         ibuf= give_ibuf_seq(rr->rectx, rr->recty, cfra, 0);
2710         
2711         if(ibuf) {
2712                 if(ibuf->rect_float) {
2713                         if (!rr->rectf)
2714                                 rr->rectf= MEM_mallocN(4*sizeof(float)*rr->rectx*rr->recty, "render_seq rectf");
2715                         
2716                         memcpy(rr->rectf, ibuf->rect_float, 4*sizeof(float)*rr->rectx*rr->recty);
2717                         
2718                         /* TSK! Since sequence render doesn't free the *rr render result, the old rect32
2719                            can hang around when sequence render has rendered a 32 bits one before */
2720                         if(rr->rect32) {
2721                                 MEM_freeN(rr->rect32);
2722                                 rr->rect32= NULL;
2723                         }
2724                 }
2725                 else if(ibuf->rect) {
2726                         if (!rr->rect32)
2727                                 rr->rect32= MEM_mallocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
2728
2729                         memcpy(rr->rect32, ibuf->rect, 4*rr->rectx*rr->recty);
2730
2731                         /* if (ibuf->zbuf) { */
2732                         /*      if (R.rectz) freeN(R.rectz); */
2733                         /*      R.rectz = BLI_dupallocN(ibuf->zbuf); */
2734                         /* } */
2735                 }
2736                 
2737                 /* Let the cache limitor take care of this (schlaile) */
2738                 /* While render let's keep all memory available for render 
2739                    (ton)
2740                    At least if free memory is tight...
2741                    This can make a big difference in encoding speed
2742                    (it is around 4 times(!) faster, if we do not waste time
2743                    on freeing _all_ buffers every time on long timelines...)
2744                    (schlaile)
2745                 */
2746                 {
2747                         extern int mem_in_use;
2748                         extern int mmap_in_use;
2749
2750                         int max = MEM_CacheLimiter_get_maximum();
2751                         if (max != 0 && mem_in_use + mmap_in_use > max) {
2752                                 fprintf(stderr, "mem_in_use = %d, max = %d\n",
2753                                         mem_in_use + mmap_in_use, max);
2754                                 fprintf(stderr, "Cleaning up, please wait...\n"
2755                                         "If this happens very often,\n"
2756                                         "consider "
2757                                         "raising the memcache limit in the "
2758                                         "user preferences.\n");
2759                                 free_imbuf_seq();
2760                         }
2761                 }
2762         }
2763         else {
2764                 /* render result is delivered empty in most cases, nevertheless we handle all cases */
2765                 if (rr->rectf)
2766                         memset(rr->rectf, 0, 4*sizeof(float)*rr->rectx*rr->recty);
2767                 else if (rr->rect32)
2768                         memset(rr->rect32, 0, 4*rr->rectx*rr->recty);
2769                 else
2770                         rr->rect32= MEM_callocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
2771         }
2772         
2773         G.f &= ~G_PLAYANIM;
2774
2775 }