== Sequencer ==
[blender-staging.git] / source / blender / src / sequence.c
1         /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * Contributor(s): Peter Schlaile <peter [at] schlaile [dot] de> 2005/2006
27  *
28  * ***** END GPL/BL DUAL LICENSE BLOCK *****
29  */
30
31 #include <string.h>
32 #include <math.h>
33 #include <stdlib.h>
34
35 #include "MEM_guardedalloc.h"
36 #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         }
1369 }
1370
1371 static void color_balance(Sequence * seq, TStripElem* se, float mul)
1372 {
1373         if (se->ibuf->rect_float) {
1374                 color_balance_float_float(seq, se, mul);
1375         } else if(seq->flag & SEQ_MAKE_FLOAT) {
1376                 color_balance_byte_float(seq, se, mul);
1377         } else {
1378                 color_balance_byte_byte(seq, se, mul);
1379         }
1380 }
1381
1382 /*
1383   input preprocessing for SEQ_IMAGE, SEQ_MOVIE and SEQ_SCENE
1384
1385   Do all the things you can't really do afterwards using sequence effects
1386   (read: before rescaling to render resolution has been done)
1387
1388   Order is important!
1389
1390   - Deinterlace
1391   - Crop and transform in image source coordinate space
1392   - Flip X + Flip Y (could be done afterwards, backward compatibility)
1393   - Promote image to float data (affects pipeline operations afterwards)
1394   - Color balance (is most efficient in the byte -> float 
1395     (future: half -> float should also work fine!)
1396     case, if done on load, since we can use lookup tables)
1397   - Premultiply
1398
1399 */
1400
1401 static void input_preprocess(Sequence * seq, TStripElem* se, int cfra)
1402 {
1403         float mul;
1404
1405         seq->strip->orx= se->ibuf->x;
1406         seq->strip->ory= se->ibuf->y;
1407
1408         if(seq->flag & SEQ_FILTERY) {
1409                 IMB_filtery(se->ibuf);
1410         }
1411
1412         if(seq->flag & SEQ_USE_CROP || seq->flag & SEQ_USE_TRANSFORM) {
1413                 StripCrop c;
1414                 StripTransform t;
1415
1416                 memset(&c, 0, sizeof(StripCrop));
1417                 memset(&t, 0, sizeof(StripTransform));
1418
1419                 if(seq->flag & SEQ_USE_CROP && seq->strip->crop) {
1420                         c = *seq->strip->crop;
1421                 }
1422                 if(seq->flag & SEQ_USE_TRANSFORM && seq->strip->transform) {
1423                         t = *seq->strip->transform;
1424                 }
1425
1426                 if (c.top + c.bottom >= se->ibuf->y ||
1427                     c.left + c.right >= se->ibuf->x ||
1428                     t.xofs >= se->ibuf->x ||
1429                     t.yofs >= se->ibuf->y) {
1430                         make_black_ibuf(se->ibuf);
1431                 } else {
1432                         ImBuf * i;
1433                         int sx = se->ibuf->x - c.left - c.right;
1434                         int sy = se->ibuf->y - c.top - c.bottom;
1435                         int dx = sx;
1436                         int dy = sy;
1437
1438                         if (seq->flag & SEQ_USE_TRANSFORM) {
1439                                 dx = seqrectx;
1440                                 dy = seqrecty;
1441                         }
1442
1443                         if (se->ibuf->rect_float) {
1444                                 i = IMB_allocImBuf(dx, dy,32, IB_rectfloat, 0);
1445                         } else {
1446                                 i = IMB_allocImBuf(dx, dy,32, IB_rect, 0);
1447                         }
1448
1449                         IMB_rectcpy(i, se->ibuf, 
1450                                     t.xofs, t.yofs, 
1451                                     c.left, c.bottom, 
1452                                     sx, sy);
1453
1454                         IMB_freeImBuf(se->ibuf);
1455
1456                         se->ibuf = i;
1457                 }
1458         } 
1459
1460         if(seq->flag & SEQ_FLIPX) {
1461                 IMB_flipx(se->ibuf);
1462         }
1463         if(seq->flag & SEQ_FLIPY) {
1464                 IMB_flipy(se->ibuf);
1465         }
1466
1467         if(seq->mul == 0.0) {
1468                 seq->mul = 1.0;
1469         }
1470
1471         mul = seq->mul;
1472
1473         if(seq->blend_mode == SEQ_BLEND_REPLACE) {
1474                 if (seq->ipo && seq->ipo->curve.first) {
1475                         do_seq_ipo(seq, cfra);
1476                         mul *= seq->facf0;
1477                 }
1478                 mul *= seq->blend_opacity / 100.0;
1479         }
1480
1481         if(seq->flag & SEQ_USE_COLOR_BALANCE && seq->strip->color_balance) {
1482                 color_balance(seq, se, mul);
1483                 mul = 1.0;
1484         }
1485
1486         if(seq->flag & SEQ_MAKE_FLOAT) {
1487                 if (!se->ibuf->rect_float) {
1488                         IMB_float_from_rect(se->ibuf);
1489                 }
1490                 if (se->ibuf->rect) {
1491                         imb_freerectImBuf(se->ibuf);
1492                 }
1493         }
1494
1495         if(mul != 1.0) {
1496                 multibuf(se->ibuf, mul);
1497         }
1498
1499         if(seq->flag & SEQ_MAKE_PREMUL) {
1500                 if(se->ibuf->depth == 32 && se->ibuf->zbuf == 0) {
1501                         converttopremul(se->ibuf);
1502                 }
1503         }
1504
1505
1506         if(se->ibuf->x != seqrectx || se->ibuf->y != seqrecty ) {
1507                 if(G.scene->r.mode & R_OSA) {
1508                         IMB_scaleImBuf(se->ibuf, 
1509                                        (short)seqrectx, (short)seqrecty);
1510                 } else {
1511                         IMB_scalefastImBuf(se->ibuf, 
1512                                            (short)seqrectx, (short)seqrecty);
1513                 }
1514         }
1515 }
1516
1517 /* test if image too small or discarded from cache: reload */
1518
1519 static void test_and_auto_discard_ibuf(TStripElem * se)
1520 {
1521         if (se->ibuf) {
1522                 if(se->ibuf->x != seqrectx || se->ibuf->y != seqrecty 
1523                    || !(se->ibuf->rect || se->ibuf->rect_float)) {
1524                         IMB_freeImBuf(se->ibuf);
1525
1526                         se->ibuf= 0;
1527                         se->ok= STRIPELEM_OK;
1528                 }
1529         }
1530         if (se->ibuf_comp) {
1531                 if(se->ibuf_comp->x != seqrectx || se->ibuf_comp->y != seqrecty 
1532                    || !(se->ibuf_comp->rect || se->ibuf_comp->rect_float)) {
1533                         IMB_freeImBuf(se->ibuf_comp);
1534
1535                         se->ibuf_comp = 0;
1536                 }
1537         }
1538 }
1539
1540 static TStripElem* do_build_seq_array_recursively(
1541         ListBase *seqbasep, int cfra, int chanshown);
1542
1543 static void do_build_seq_ibuf(Sequence * seq, TStripElem *se, int cfra,
1544                               int build_proxy_run)
1545 {
1546         char name[FILE_MAXDIR+FILE_MAXFILE];
1547
1548
1549         if (seq->type != SEQ_META) {
1550                 test_and_auto_discard_ibuf(se);
1551         }
1552
1553         if(seq->type == SEQ_META) {
1554                 if(seq->seqbase.first) {
1555                         if(cfra < seq->start) {
1556                                 do_build_seq_array_recursively(
1557                                         &seq->seqbase, 
1558                                         seq->start, 0);
1559                         } else if(cfra > seq->start + seq->len - 1) {
1560                                 do_build_seq_array_recursively(
1561                                         &seq->seqbase, 
1562                                         seq->start + seq->len - 1, 0);
1563                         } else {
1564                                 do_build_seq_array_recursively(
1565                                         &seq->seqbase, 
1566                                         cfra, 0);
1567                         }
1568                 }
1569
1570                 se->ok = STRIPELEM_META;
1571                 if(se->se1 == 0) set_meta_stripdata(seq);
1572                 if(se->se1) {
1573                         if(se->ibuf) {
1574                                 IMB_freeImBuf(se->ibuf);
1575                         }
1576                         se->ibuf = se->se1->ibuf_comp;
1577                         if(se->ibuf) {
1578                                 IMB_refImBuf(se->ibuf);
1579                         }
1580                 }
1581         } else if(seq->type & SEQ_EFFECT) {
1582                 /* should the effect be recalculated? */
1583                 
1584                 if (!build_proxy_run && se->ibuf == 0) {
1585                         se->ibuf = seq_proxy_fetch(seq, cfra);
1586                 }
1587
1588                 if(se->ibuf == 0) {
1589                         /* if one of two first inputs are rectfloat, output is float too */
1590                         if((se->se1 && se->se1->ibuf && se->se1->ibuf->rect_float) ||
1591                            (se->se2 && se->se2->ibuf && se->se2->ibuf->rect_float))
1592                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
1593                         else
1594                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
1595                         
1596                         do_effect(cfra, seq, se);
1597                 }
1598         } else if(seq->type == SEQ_IMAGE) {
1599                 if(se->ok == STRIPELEM_OK && se->ibuf == 0) {
1600                         StripElem * s_elem = give_stripelem(seq, cfra);
1601                         
1602                         strncpy(name, seq->strip->dir, FILE_MAXDIR-1);
1603                         strncat(name, s_elem->name, FILE_MAXFILE);
1604                         BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
1605                         if (!build_proxy_run) {
1606                                 se->ibuf = seq_proxy_fetch(seq, cfra);
1607                         }
1608                         if (!se->ibuf) {
1609                                 se->ibuf= IMB_loadiffname(
1610                                         name, IB_rect);
1611                         }
1612                         
1613                         if(se->ibuf == 0) {
1614                                 se->ok = STRIPELEM_FAILED;
1615                         } else if (!build_proxy_run) {
1616                                 input_preprocess(seq, se, cfra);
1617                         }
1618                 }
1619         } else if(seq->type == SEQ_MOVIE) {
1620                 if(se->ok == STRIPELEM_OK && se->ibuf==0) {
1621                         if(!build_proxy_run) {
1622                                 se->ibuf = seq_proxy_fetch(seq, cfra);
1623                         }
1624
1625                         if (se->ibuf == 0) {
1626                                 if(seq->anim==0) {
1627                                         strncpy(name, seq->strip->dir, FILE_MAXDIR-1);
1628                                         strncat(name, seq->strip->stripdata->name, FILE_MAXFILE-1);
1629                                         BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
1630                                 
1631                                         seq->anim = openanim(name, IB_rect);
1632                                 }
1633                                 if(seq->anim) {
1634                                         IMB_anim_set_preseek(seq->anim, seq->anim_preseek);
1635                                         se->ibuf = IMB_anim_absolute(seq->anim, se->nr + seq->anim_startofs);
1636                                 }
1637                         }
1638                         
1639                         if(se->ibuf == 0) {
1640                                 se->ok = STRIPELEM_FAILED;
1641                         } else if (!build_proxy_run) {
1642                                 input_preprocess(seq, se, cfra);
1643                         }
1644                 }
1645         } else if(seq->type == SEQ_SCENE) {     // scene can be NULL after deletions
1646                 int oldcfra = CFRA;
1647                 Scene *sce= seq->scene, *oldsce= G.scene;
1648                 Render *re;
1649                 RenderResult rres;
1650                 int doseq, rendering= G.rendering;
1651                 char scenename[64];
1652                         
1653                 if (se->ibuf==NULL && seq->scene && !build_proxy_run) {
1654                         se->ibuf = seq_proxy_fetch(seq, cfra);
1655                         if (se->ibuf) {
1656                                 input_preprocess(seq, se, cfra);
1657                         }
1658                 }
1659
1660                 if (se->ibuf==NULL && seq->scene) {
1661                         waitcursor(1);
1662                         
1663                         /* Hack! This function can be called from do_render_seq(), in that case
1664                            the seq->scene can already have a Render initialized with same name, 
1665                            so we have to use a default name. (compositor uses G.scene name to
1666                            find render).
1667                            However, when called from within the UI (image preview in sequencer)
1668                            we do want to use scene Render, that way the render result is defined
1669                            for display in render/imagewindow */
1670                         if(rendering) {
1671                                 BLI_strncpy(scenename, sce->id.name+2, 64);
1672                                 strcpy(sce->id.name+2, " do_build_seq_ibuf");
1673                         }
1674                         re= RE_NewRender(sce->id.name);
1675                         
1676                         /* prevent eternal loop */
1677                         doseq= G.scene->r.scemode & R_DOSEQ;
1678                         G.scene->r.scemode &= ~R_DOSEQ;
1679                         
1680                         BIF_init_render_callbacks(re, 0);       /* 0= no display callbacks */
1681                         
1682                         /* hrms, set_scene still needed? work on that... */
1683                         if(sce!=oldsce) set_scene_bg(sce);
1684                         RE_BlenderFrame(re, sce,
1685                                         seq->sfra+se->nr+seq->anim_startofs);
1686                         if(sce!=oldsce) set_scene_bg(oldsce);
1687                         
1688                         /* UGLY WARNING, it is set to zero in  RE_BlenderFrame */
1689                         G.rendering= rendering;
1690                         if(rendering)
1691                                 BLI_strncpy(sce->id.name+2, scenename, 64);
1692                         
1693                         RE_GetResultImage(re, &rres);
1694                         
1695                         if(rres.rectf) {
1696                                 se->ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rectfloat, 0);
1697                                 memcpy(se->ibuf->rect_float, rres.rectf, 4*sizeof(float)*rres.rectx*rres.recty);
1698                                 if(rres.rectz) {
1699                                         addzbuffloatImBuf(se->ibuf);
1700                                         memcpy(se->ibuf->zbuf_float, rres.rectz, sizeof(float)*rres.rectx*rres.recty);
1701                                 }
1702                         } else if (rres.rect32) {
1703                                 se->ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rect, 0);
1704                                 memcpy(se->ibuf->rect, rres.rect32, 4*rres.rectx*rres.recty);
1705                         }
1706                         
1707                         BIF_end_render_callbacks();
1708                         
1709                         /* restore */
1710                         G.scene->r.scemode |= doseq;
1711                         
1712                         if((G.f & G_PLAYANIM)==0) /* bad, is set on do_render_seq */
1713                                 waitcursor(0);
1714                         CFRA = oldcfra;
1715
1716                         if (!build_proxy_run) {
1717                                 input_preprocess(seq, se, cfra);
1718                         }
1719
1720                 }       
1721         }
1722         if (!build_proxy_run) {
1723                 if (se->ibuf && seq->type != SEQ_META) {
1724                         IMB_cache_limiter_insert(se->ibuf);
1725                         IMB_cache_limiter_ref(se->ibuf);
1726                         IMB_cache_limiter_touch(se->ibuf);
1727                 }
1728         }
1729 }
1730
1731 static TStripElem* do_build_seq_recursively(Sequence * seq, int cfra);
1732
1733 static void do_effect_seq_recursively(Sequence * seq, TStripElem *se, int cfra)
1734 {
1735         float fac, facf;
1736         struct SeqEffectHandle sh = get_sequence_effect(seq);
1737         int early_out;
1738
1739         se->se1 = 0;
1740         se->se2 = 0;
1741         se->se3 = 0;
1742
1743         if(seq->ipo && seq->ipo->curve.first) {
1744                 do_seq_ipo(seq, cfra);
1745                 fac= seq->facf0;
1746                 facf= seq->facf1;
1747         } else {
1748                 sh.get_default_fac(seq, cfra, &fac, &facf);
1749         } 
1750
1751         if( G.scene->r.mode & R_FIELDS ); else facf= fac;
1752         
1753         early_out = sh.early_out(seq, fac, facf);
1754         switch (early_out) {
1755         case -1:
1756                 /* no input needed */
1757                 break;
1758         case 0:
1759                 se->se1 = do_build_seq_recursively(seq->seq1, cfra);
1760                 se->se2 = do_build_seq_recursively(seq->seq2, cfra);
1761                 if (seq->seq3) {
1762                         se->se3 = do_build_seq_recursively(seq->seq3, cfra);
1763                 }
1764                 break;
1765         case 1:
1766                 se->se1 = do_build_seq_recursively(seq->seq1, cfra);
1767                 break;
1768         case 2:
1769                 se->se2 = do_build_seq_recursively(seq->seq2, cfra);
1770                 break;
1771         }
1772
1773
1774         do_build_seq_ibuf(seq, se, cfra, FALSE);
1775
1776         /* children are not needed anymore ... */
1777
1778         if (se->se1 && se->se1->ibuf) {
1779                 IMB_cache_limiter_unref(se->se1->ibuf);
1780         }
1781         if (se->se2 && se->se2->ibuf) {
1782                 IMB_cache_limiter_unref(se->se2->ibuf);
1783         }
1784         if (se->se3 && se->se3->ibuf) {
1785                 IMB_cache_limiter_unref(se->se3->ibuf);
1786         }
1787 }
1788
1789 static TStripElem* do_build_seq_recursively_impl(Sequence * seq, int cfra)
1790 {
1791         TStripElem *se;
1792
1793         se = give_tstripelem(seq, cfra);
1794
1795         if(se) {
1796                 if (seq->type & SEQ_EFFECT) {
1797                         do_effect_seq_recursively(seq, se, cfra);
1798                 } else {
1799                         do_build_seq_ibuf(seq, se, cfra, FALSE);
1800                 }
1801         }
1802         return se;
1803 }
1804
1805 /* FIXME:
1806    
1807 If cfra was float throughout blender (especially in the render
1808 pipeline) one could even _render_ with subframe precision
1809 instead of faking using the blend code below...
1810
1811 */
1812
1813 static TStripElem* do_handle_speed_effect(Sequence * seq, int cfra)
1814 {
1815         SpeedControlVars * s = (SpeedControlVars *)seq->effectdata;
1816         int nr = cfra - seq->start;
1817         float f_cfra;
1818         int cfra_left;
1819         int cfra_right;
1820         TStripElem * se = 0;
1821         TStripElem * se1 = 0;
1822         TStripElem * se2 = 0;
1823         
1824         sequence_effect_speed_rebuild_map(seq, 0);
1825         
1826         f_cfra = seq->start + s->frameMap[nr];
1827         
1828         cfra_left = (int) floor(f_cfra);
1829         cfra_right = (int) ceil(f_cfra);
1830
1831         se = give_tstripelem(seq, cfra);
1832
1833         if (cfra_left == cfra_right || 
1834             (s->flags & SEQ_SPEED_BLEND) == 0) {
1835                 test_and_auto_discard_ibuf(se);
1836
1837                 if (se->ibuf == NULL) {
1838                         se1 = do_build_seq_recursively_impl(
1839                                 seq->seq1, cfra_left);
1840
1841                         if((se1 && se1->ibuf && se1->ibuf->rect_float))
1842                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
1843                         else
1844                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
1845
1846                         if (se1 == 0 || se1->ibuf == 0) {
1847                                 make_black_ibuf(se->ibuf);
1848                         } else {
1849                                 if (se->ibuf != se1->ibuf) {
1850                                         if (se->ibuf) {
1851                                                 IMB_freeImBuf(se->ibuf);
1852                                         }
1853
1854                                         se->ibuf = se1->ibuf;
1855                                         IMB_refImBuf(se->ibuf);
1856                                 }
1857                         }
1858                 }
1859         } else {
1860                 struct SeqEffectHandle sh;
1861
1862                 if(se->ibuf) {
1863                         if(se->ibuf->x < seqrectx || se->ibuf->y < seqrecty 
1864                            || !(se->ibuf->rect || se->ibuf->rect_float)) {
1865                                 IMB_freeImBuf(se->ibuf);
1866                                 se->ibuf= 0;
1867                         }
1868                 }
1869
1870                 if (se->ibuf == NULL) {
1871                         se1 = do_build_seq_recursively_impl(
1872                                 seq->seq1, cfra_left);
1873                         se2 = do_build_seq_recursively_impl(
1874                                 seq->seq1, cfra_right);
1875
1876                         if((se1 && se1->ibuf && se1->ibuf->rect_float))
1877                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
1878                         else
1879                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
1880                         
1881                         if (!se1 || !se2) {
1882                                 make_black_ibuf(se->ibuf);
1883                         } else {
1884                                 sh = get_sequence_effect(seq);
1885
1886                                 sh.execute(seq, cfra, 
1887                                            f_cfra - (float) cfra_left, 
1888                                            f_cfra - (float) cfra_left, 
1889                                            se->ibuf->x, se->ibuf->y, 
1890                                            se1->ibuf, se2->ibuf, 0, se->ibuf);
1891                         }
1892                 }
1893
1894         }
1895
1896         /* caller expects this to be referenced, so do it! */
1897         if (se->ibuf) {
1898                 IMB_cache_limiter_insert(se->ibuf);
1899                 IMB_cache_limiter_ref(se->ibuf);
1900                 IMB_cache_limiter_touch(se->ibuf);
1901         }
1902
1903         /* children are no longer needed */
1904         if (se1 && se1->ibuf)
1905                 IMB_cache_limiter_unref(se1->ibuf);
1906         if (se2 && se2->ibuf)
1907                 IMB_cache_limiter_unref(se2->ibuf);
1908
1909         return se;
1910 }
1911
1912 /* 
1913  * build all ibufs recursively
1914  * 
1915  * if successfull, the returned TStripElem contains the (referenced!) imbuf
1916  * that means: you _must_ call 
1917  *
1918  * IMB_cache_limiter_unref(rval);
1919  * 
1920  * if rval != 0
1921  * 
1922  */
1923
1924 static TStripElem* do_build_seq_recursively(Sequence * seq, int cfra)
1925 {
1926         if (seq->type == SEQ_SPEED) {
1927                 return do_handle_speed_effect(seq, cfra);
1928         } else {
1929                 return do_build_seq_recursively_impl(seq, cfra);
1930         }
1931 }
1932
1933 static TStripElem* do_build_seq_array_recursively(
1934         ListBase *seqbasep, int cfra, int chanshown)
1935 {
1936         Sequence* seq_arr[MAXSEQ+1];
1937         int count;
1938         int i;
1939         TStripElem* se = 0;
1940
1941         count = get_shown_sequences(seqbasep, cfra, chanshown, (Sequence **)&seq_arr);
1942
1943         if (!count) {
1944                 return 0;
1945         }
1946
1947         se = give_tstripelem(seq_arr[count - 1], cfra);
1948
1949         test_and_auto_discard_ibuf(se);
1950
1951         if (se->ibuf_comp != 0) {
1952                 IMB_cache_limiter_insert(se->ibuf_comp);
1953                 IMB_cache_limiter_ref(se->ibuf_comp);
1954                 IMB_cache_limiter_touch(se->ibuf_comp);
1955                 return se;
1956         }
1957
1958         
1959         if(count == 1) {
1960                 se = do_build_seq_recursively(seq_arr[0], cfra);
1961                 if (se->ibuf) {
1962                         se->ibuf_comp = se->ibuf;
1963                         IMB_refImBuf(se->ibuf_comp);
1964                 }
1965                 return se;
1966         }
1967
1968
1969         for (i = count - 1; i >= 0; i--) {
1970                 int early_out;
1971                 Sequence * seq = seq_arr[i];
1972                 struct SeqEffectHandle sh;
1973
1974                 se = give_tstripelem(seq, cfra);
1975
1976                 test_and_auto_discard_ibuf(se);
1977
1978                 if (se->ibuf_comp != 0) {
1979                         break;
1980                 }
1981                 if (seq->blend_mode == SEQ_BLEND_REPLACE) {
1982                         do_build_seq_recursively(seq, cfra);
1983                         if (se->ibuf) {
1984                                 se->ibuf_comp = se->ibuf;
1985                                 IMB_refImBuf(se->ibuf);
1986                         } else {
1987                                 se->ibuf_comp = IMB_allocImBuf(
1988                                         (short)seqrectx, (short)seqrecty, 
1989                                         32, IB_rect, 0);
1990                         }
1991                         break;
1992                 }
1993
1994                 sh = get_sequence_blend(seq);
1995
1996                 seq->facf0 = seq->facf1 = 1.0;
1997
1998                 if(seq->ipo && seq->ipo->curve.first) {
1999                         do_seq_ipo(seq, cfra);
2000                 } 
2001
2002                 if( G.scene->r.mode & R_FIELDS ); else seq->facf0 = seq->facf1;
2003
2004                 seq->facf0 *= seq->blend_opacity / 100.0;
2005                 seq->facf1 *= seq->blend_opacity / 100.0;
2006
2007                 early_out = sh.early_out(seq, seq->facf0, seq->facf1);
2008
2009                 switch (early_out) {
2010                 case -1:
2011                 case 2:
2012                         do_build_seq_recursively(seq, cfra);
2013                         if (se->ibuf) {
2014                                 se->ibuf_comp = se->ibuf;
2015                                 IMB_refImBuf(se->ibuf_comp);
2016                         } else {
2017                                 se->ibuf_comp = IMB_allocImBuf(
2018                                         (short)seqrectx, (short)seqrecty, 
2019                                         32, IB_rect, 0);
2020                         }
2021                         break;
2022                 case 1:
2023                         if (i == 0) {
2024                                 se->ibuf_comp = IMB_allocImBuf(
2025                                         (short)seqrectx, (short)seqrecty, 
2026                                         32, IB_rect, 0);
2027                                 IMB_cache_limiter_insert(se->ibuf_comp);
2028                                 IMB_cache_limiter_ref(se->ibuf_comp);
2029                                 IMB_cache_limiter_touch(se->ibuf_comp);
2030                         }
2031                         break;
2032                 case 0:
2033                         do_build_seq_recursively(seq, cfra);
2034                         if (!se->ibuf) {
2035                                 se->ibuf = IMB_allocImBuf(
2036                                         (short)seqrectx, (short)seqrecty, 
2037                                         32, IB_rect, 0);
2038                         }
2039                         if (i == 0) {
2040                                 se->ibuf_comp = se->ibuf;
2041                                 IMB_refImBuf(se->ibuf_comp);
2042                         }
2043                         break;
2044                 }
2045         
2046                 if (se->ibuf_comp) {
2047                         break;
2048                 }
2049         }
2050
2051         i++;
2052
2053         for (; i < count; i++) {
2054                 Sequence * seq = seq_arr[i];
2055                 struct SeqEffectHandle sh = get_sequence_blend(seq);
2056                 TStripElem* se1 = give_tstripelem(seq_arr[i-1], cfra);
2057                 TStripElem* se2 = give_tstripelem(seq_arr[i], cfra);
2058         
2059                 int early_out = sh.early_out(seq, seq->facf0, seq->facf1);
2060                 switch (early_out) {
2061                 case 0: {
2062                         int x= se2->ibuf->x;
2063                         int y= se2->ibuf->y;
2064                         int swap_input = FALSE;
2065
2066                         if (se1->ibuf_comp->rect_float ||
2067                             se2->ibuf->rect_float) {
2068                                 se2->ibuf_comp = IMB_allocImBuf(
2069                                         (short)seqrectx, (short)seqrecty, 
2070                                         32, IB_rectfloat, 0);
2071                         } else {
2072                                 se2->ibuf_comp = IMB_allocImBuf(
2073                                         (short)seqrectx, (short)seqrecty, 
2074                                         32, IB_rect, 0);
2075                         }
2076
2077
2078                         if (!se1->ibuf_comp->rect_float && 
2079                             se2->ibuf_comp->rect_float) {
2080                                 IMB_float_from_rect(se1->ibuf_comp);
2081                         }
2082                         if (!se2->ibuf->rect_float && 
2083                             se2->ibuf_comp->rect_float) {
2084                                 IMB_float_from_rect(se2->ibuf);
2085                         }
2086
2087                         if (!se1->ibuf_comp->rect && 
2088                             !se2->ibuf_comp->rect_float) {
2089                                 IMB_rect_from_float(se1->ibuf_comp);
2090                         }
2091                         if (!se2->ibuf->rect && 
2092                             !se2->ibuf_comp->rect_float) {
2093                                 IMB_rect_from_float(se2->ibuf);
2094                         }
2095
2096                         /* bad hack, to fix crazy input ordering of 
2097                            those two effects */
2098
2099                         if (seq->blend_mode == SEQ_ALPHAOVER ||
2100                             seq->blend_mode == SEQ_ALPHAUNDER ||
2101                             seq->blend_mode == SEQ_OVERDROP) {
2102                                 swap_input = TRUE;
2103                         }
2104
2105                         if (swap_input) {
2106                                 sh.execute(seq, cfra, 
2107                                            seq->facf0, seq->facf1, x, y, 
2108                                            se2->ibuf, se1->ibuf_comp, 0,
2109                                            se2->ibuf_comp);
2110                         } else {
2111                                 sh.execute(seq, cfra, 
2112                                            seq->facf0, seq->facf1, x, y, 
2113                                            se1->ibuf_comp, se2->ibuf, 0,
2114                                            se2->ibuf_comp);
2115                         }
2116                         
2117                         IMB_cache_limiter_insert(se2->ibuf_comp);
2118                         IMB_cache_limiter_ref(se2->ibuf_comp);
2119                         IMB_cache_limiter_touch(se2->ibuf_comp);
2120
2121                         IMB_cache_limiter_unref(se1->ibuf_comp);
2122                         IMB_cache_limiter_unref(se2->ibuf);
2123
2124                         break;
2125                 }
2126                 case 1: {
2127                         se2->ibuf_comp = se1->ibuf;
2128                         IMB_refImBuf(se2->ibuf_comp);
2129
2130                         break;
2131                 }
2132                 }
2133                 se = se2;
2134         }
2135
2136         return se;
2137 }
2138
2139 /*
2140  * returned ImBuf is refed!
2141  * you have to unref after usage!
2142  */
2143
2144 static ImBuf *give_ibuf_seq_impl(int rectx, int recty, int cfra, int chanshown)
2145 {
2146         Editing *ed;
2147         int count;
2148         ListBase *seqbasep;
2149         TStripElem *se;
2150
2151         ed= G.scene->ed;
2152         if(ed==0) return 0;
2153
2154         count = BLI_countlist(&ed->metastack);
2155         if((chanshown < 0) && (count > 0)) {
2156                 count = MAX2(count + chanshown, 0);
2157                 seqbasep= ((MetaStack*)BLI_findlink(&ed->metastack, count))->oldbasep;
2158         } else {
2159                 seqbasep= ed->seqbasep;
2160         }
2161
2162         seqrectx= rectx;        /* bad bad global! */
2163         seqrecty= recty;
2164
2165         se = do_build_seq_array_recursively(seqbasep, cfra, chanshown);
2166
2167         if(!se) { 
2168                 return 0;
2169         }
2170
2171         return se->ibuf_comp;
2172 }
2173
2174 ImBuf *give_ibuf_seq_direct(int rectx, int recty, int cfra,
2175                             Sequence * seq)
2176 {
2177         TStripElem* se;
2178
2179         seqrectx= rectx;        /* bad bad global! */
2180         seqrecty= recty;
2181
2182         se = do_build_seq_recursively(seq, cfra);
2183
2184         if(!se) { 
2185                 return 0;
2186         }
2187
2188         if (se->ibuf) {
2189                 IMB_cache_limiter_unref(se->ibuf);
2190         }
2191
2192         return se->ibuf;
2193 }
2194
2195 ImBuf *give_ibuf_seq(int rectx, int recty, int cfra, int chanshown)
2196 {
2197         ImBuf* i = give_ibuf_seq_impl(rectx, recty, cfra, chanshown);
2198
2199         if (i) {
2200                 IMB_cache_limiter_unref(i);
2201         }
2202         return i;
2203 }
2204
2205 /* threading api */
2206
2207 static ListBase running_threads;
2208 static ListBase prefetch_wait;
2209 static ListBase prefetch_done;
2210
2211 static pthread_mutex_t queue_lock          = PTHREAD_MUTEX_INITIALIZER;
2212 static pthread_mutex_t wakeup_lock         = PTHREAD_MUTEX_INITIALIZER;
2213 static pthread_cond_t  wakeup_cond         = PTHREAD_COND_INITIALIZER;
2214
2215 static pthread_mutex_t prefetch_ready_lock = PTHREAD_MUTEX_INITIALIZER;
2216 static pthread_cond_t  prefetch_ready_cond = PTHREAD_COND_INITIALIZER;
2217
2218 static pthread_mutex_t frame_done_lock     = PTHREAD_MUTEX_INITIALIZER;
2219 static pthread_cond_t  frame_done_cond     = PTHREAD_COND_INITIALIZER;
2220
2221 static volatile int seq_thread_shutdown = FALSE;
2222 static volatile int seq_last_given_monoton_cfra = 0;
2223 static int monoton_cfra = 0;
2224
2225 typedef struct PrefetchThread {
2226         struct PrefetchThread *next, *prev;
2227         struct PrefetchQueueElem *current;
2228         pthread_t pthread;
2229         int running;
2230 } PrefetchThread;
2231
2232 typedef struct PrefetchQueueElem {
2233         struct PrefetchQueueElem *next, *prev;
2234         
2235         int rectx;
2236         int recty;
2237         int cfra;
2238         int chanshown;
2239
2240         int monoton_cfra;
2241
2242         struct ImBuf * ibuf;
2243 } PrefetchQueueElem;
2244
2245 static void * seq_prefetch_thread(void * This_)
2246 {
2247         PrefetchThread * This = This_;
2248
2249         while (!seq_thread_shutdown) {
2250                 PrefetchQueueElem * e;
2251                 int s_last;
2252
2253                 pthread_mutex_lock(&queue_lock);
2254                 e = prefetch_wait.first;
2255                 if (e) {
2256                         BLI_remlink(&prefetch_wait, e);
2257                 }
2258                 s_last = seq_last_given_monoton_cfra;
2259
2260                 This->current = e;
2261
2262                 pthread_mutex_unlock(&queue_lock);
2263
2264                 if (!e) {
2265                         pthread_mutex_lock(&prefetch_ready_lock);
2266
2267                         This->running = FALSE;
2268
2269                         pthread_cond_signal(&prefetch_ready_cond);
2270                         pthread_mutex_unlock(&prefetch_ready_lock);
2271
2272                         pthread_mutex_lock(&wakeup_lock);
2273                         if (!seq_thread_shutdown) {
2274                                 pthread_cond_wait(&wakeup_cond, &wakeup_lock);
2275                         }
2276                         pthread_mutex_unlock(&wakeup_lock);
2277                         continue;
2278                 }
2279
2280                 This->running = TRUE;
2281                 
2282                 if (e->cfra >= s_last) { 
2283                         e->ibuf = give_ibuf_seq_impl(
2284                                 e->rectx, e->recty, e->cfra, e->chanshown);
2285                 }
2286
2287                 pthread_mutex_lock(&queue_lock);
2288
2289                 BLI_addtail(&prefetch_done, e);
2290
2291                 for (e = prefetch_wait.first; e; e = e->next) {
2292                         if (s_last > e->monoton_cfra) {
2293                                 BLI_remlink(&prefetch_wait, e);
2294                                 MEM_freeN(e);
2295                         }
2296                 }
2297
2298                 for (e = prefetch_done.first; e; e = e->next) {
2299                         if (s_last > e->monoton_cfra) {
2300                                 if (e->ibuf) {
2301                                         IMB_cache_limiter_unref(e->ibuf);
2302                                 }
2303                                 BLI_remlink(&prefetch_done, e);
2304                                 MEM_freeN(e);
2305                         }
2306                 }
2307
2308                 pthread_mutex_unlock(&queue_lock);
2309
2310                 pthread_mutex_lock(&frame_done_lock);
2311                 pthread_cond_signal(&frame_done_cond);
2312                 pthread_mutex_unlock(&frame_done_lock);
2313         }
2314         return 0;
2315 }
2316
2317 void seq_start_threads()
2318 {
2319         int i;
2320
2321         running_threads.first = running_threads.last = NULL;
2322         prefetch_wait.first = prefetch_wait.last = NULL;
2323         prefetch_done.first = prefetch_done.last = NULL;
2324
2325         seq_thread_shutdown = FALSE;
2326         seq_last_given_monoton_cfra = monoton_cfra = 0;
2327
2328         /* since global structures are modified during the processing
2329            of one frame, only one render thread is currently possible... 
2330
2331            (but we code, in the hope, that we can remove this restriction
2332            soon...)
2333         */
2334
2335         fprintf(stderr, "SEQ-THREAD: seq_start_threads\n");
2336
2337         for (i = 0; i < 1; i++) {
2338                 PrefetchThread *t = MEM_callocN(sizeof(PrefetchThread), 
2339                                                 "prefetch_thread");
2340                 t->running = TRUE;
2341                 BLI_addtail(&running_threads, t);
2342
2343                 pthread_create(&t->pthread, NULL, seq_prefetch_thread, t);
2344         }
2345
2346         /* init malloc mutex */
2347         BLI_init_threads(0, 0, 0);
2348 }
2349
2350 void seq_stop_threads()
2351 {
2352         PrefetchThread *tslot;
2353         PrefetchQueueElem * e;
2354
2355         fprintf(stderr, "SEQ-THREAD: seq_stop_threads()\n");
2356
2357         if (seq_thread_shutdown) {
2358                 fprintf(stderr, "SEQ-THREAD: ... already stopped\n");
2359                 return;
2360         }
2361         
2362         pthread_mutex_lock(&wakeup_lock);
2363
2364         seq_thread_shutdown = TRUE;
2365
2366         pthread_cond_broadcast(&wakeup_cond);
2367         pthread_mutex_unlock(&wakeup_lock);
2368
2369         for(tslot = running_threads.first; tslot; tslot= tslot->next) {
2370                 pthread_join(tslot->pthread, NULL);
2371         }
2372
2373
2374         for (e = prefetch_wait.first; e; e = e->next) {
2375                 BLI_remlink(&prefetch_wait, e);
2376                 MEM_freeN(e);
2377         }
2378
2379         for (e = prefetch_done.first; e; e = e->next) {
2380                 if (e->ibuf) {
2381                         IMB_cache_limiter_unref(e->ibuf);
2382                 }
2383                 BLI_remlink(&prefetch_done, e);
2384                 MEM_freeN(e);
2385         }
2386
2387         BLI_freelistN(&running_threads);
2388
2389         /* deinit malloc mutex */
2390         BLI_end_threads(0);
2391 }
2392
2393 void give_ibuf_prefetch_request(int rectx, int recty, int cfra, int chanshown)
2394 {
2395         PrefetchQueueElem * e;
2396         if (seq_thread_shutdown) {
2397                 return;
2398         }
2399
2400         e = MEM_callocN(sizeof(PrefetchQueueElem), "prefetch_queue_elem");
2401         e->rectx = rectx;
2402         e->recty = recty;
2403         e->cfra = cfra;
2404         e->chanshown = chanshown;
2405         e->monoton_cfra = monoton_cfra++;
2406
2407         pthread_mutex_lock(&queue_lock);
2408         BLI_addtail(&prefetch_wait, e);
2409         pthread_mutex_unlock(&queue_lock);
2410         
2411         pthread_mutex_lock(&wakeup_lock);
2412         pthread_cond_signal(&wakeup_cond);
2413         pthread_mutex_unlock(&wakeup_lock);
2414 }
2415
2416 void seq_wait_for_prefetch_ready()
2417 {
2418         PrefetchThread *tslot;
2419
2420         if (seq_thread_shutdown) {
2421                 return;
2422         }
2423
2424         fprintf(stderr, "SEQ-THREAD: rendering prefetch frames...\n");
2425
2426         pthread_mutex_lock(&prefetch_ready_lock);
2427
2428         for(;;) {
2429                 for(tslot = running_threads.first; tslot; tslot= tslot->next) {
2430                         if (tslot->running) {
2431                                 break;
2432                         }
2433                 }
2434                 if (!tslot) {
2435                         break;
2436                 }
2437                 pthread_cond_wait(&prefetch_ready_cond, &prefetch_ready_lock);
2438         }
2439
2440         pthread_mutex_unlock(&prefetch_ready_lock);
2441
2442         fprintf(stderr, "SEQ-THREAD: prefetch done\n");
2443 }
2444
2445 ImBuf * give_ibuf_seq_threaded(int rectx, int recty, int cfra, int chanshown)
2446 {
2447         PrefetchQueueElem * e = 0;
2448         int found_something = FALSE;
2449
2450         if (seq_thread_shutdown) {
2451                 return give_ibuf_seq(rectx, recty, cfra, chanshown);
2452         }
2453
2454         while (!e) {
2455                 int success = FALSE;
2456                 pthread_mutex_lock(&queue_lock);
2457
2458                 for (e = prefetch_done.first; e; e = e->next) {
2459                         if (cfra == e->cfra &&
2460                             chanshown == e->chanshown &&
2461                             rectx == e->rectx && 
2462                             recty == e->recty) {
2463                                 success = TRUE;
2464                                 found_something = TRUE;
2465                                 break;
2466                         }
2467                 }
2468
2469                 if (!e) {
2470                         for (e = prefetch_wait.first; e; e = e->next) {
2471                                 if (cfra == e->cfra &&
2472                                     chanshown == e->chanshown &&
2473                                     rectx == e->rectx && 
2474                                     recty == e->recty) {
2475                                         found_something = TRUE;
2476                                         break;
2477                                 }
2478                         }
2479                 }
2480
2481                 if (!e) {
2482                         PrefetchThread *tslot;
2483
2484                         for(tslot = running_threads.first; 
2485                             tslot; tslot= tslot->next) {
2486                                 if (tslot->current &&
2487                                     cfra == tslot->current->cfra &&
2488                                     chanshown == tslot->current->chanshown &&
2489                                     rectx == tslot->current->rectx && 
2490                                     recty == tslot->current->recty) {
2491                                         found_something = TRUE;
2492                                         break;
2493                                 }
2494                         }
2495                 }
2496
2497                 /* e->ibuf is unrefed by render thread on next round. */
2498
2499                 if (e) {
2500                         seq_last_given_monoton_cfra = e->monoton_cfra;
2501                 }
2502
2503                 pthread_mutex_unlock(&queue_lock);
2504
2505                 if (!success) {
2506                         e = NULL;
2507
2508                         if (!found_something) {
2509                                 fprintf(stderr, 
2510                                         "SEQ-THREAD: Requested frame "
2511                                         "not in queue ???\n");
2512                                 break;
2513                         }
2514                         pthread_mutex_lock(&frame_done_lock);
2515                         pthread_cond_wait(&frame_done_cond, &frame_done_lock);
2516                         pthread_mutex_unlock(&frame_done_lock);
2517                 }
2518         }
2519         
2520         return e ? e->ibuf : 0;
2521 }
2522
2523 /* Functions to free imbuf and anim data on changes */
2524
2525 static void free_imbuf_strip_elem(TStripElem *se)
2526 {
2527         if(se->ibuf) {
2528                 IMB_freeImBuf(se->ibuf);
2529         }
2530         if(se->ibuf_comp) {
2531                 IMB_freeImBuf(se->ibuf_comp);
2532         }
2533         se->ibuf_comp = 0;
2534         se->ibuf= 0;
2535         se->ok= STRIPELEM_OK;
2536         se->se1= se->se2= se->se3= 0;
2537 }
2538
2539 static void free_anim_seq(Sequence *seq)
2540 {
2541         if(seq->anim) {
2542                 IMB_free_anim(seq->anim);
2543                 seq->anim = 0;
2544         }
2545 }
2546
2547 void free_imbuf_seq_except(int cfra)
2548 {
2549         Editing *ed= G.scene->ed;
2550         Sequence *seq;
2551         TStripElem *se;
2552         int a;
2553
2554         if(ed==0) return;
2555
2556         WHILE_SEQ(&ed->seqbase) {
2557                 if(seq->strip) {
2558                         TStripElem * curelem = give_tstripelem(seq, cfra);
2559
2560                         for(a = 0, se = seq->strip->tstripdata; 
2561                             a < seq->strip->len && se; a++, se++) {
2562                                 if(se != curelem) {
2563                                         free_imbuf_strip_elem(se);
2564                                 }
2565                         }
2566                         for(a = 0, se = seq->strip->tstripdata_startstill;
2567                             a < seq->strip->startstill && se; a++, se++) {
2568                                 if(se != curelem) {
2569                                         free_imbuf_strip_elem(se);
2570                                 }
2571                         }
2572                         for(a = 0, se = seq->strip->tstripdata_endstill;
2573                             a < seq->strip->endstill && se; a++, se++) {
2574                                 if(se != curelem) {
2575                                         free_imbuf_strip_elem(se);
2576                                 }
2577                         }
2578
2579                         if(seq->type==SEQ_MOVIE)
2580                                 if(seq->startdisp > cfra || seq->enddisp < cfra)
2581                                         free_anim_seq(seq);
2582                 }
2583         }
2584         END_SEQ
2585 }
2586
2587 void free_imbuf_seq()
2588 {
2589         Editing *ed= G.scene->ed;
2590         Sequence *seq;
2591         TStripElem *se;
2592         int a;
2593
2594         if(ed==0) return;
2595
2596         WHILE_SEQ(&ed->seqbase) {
2597                 if(seq->strip) {
2598                         for(a = 0, se = seq->strip->tstripdata; 
2599                             a < seq->strip->len && se; a++, se++) {
2600                                 free_imbuf_strip_elem(se);
2601                         }
2602                         for(a = 0, se = seq->strip->tstripdata_startstill; 
2603                             a < seq->strip->startstill && se; a++, se++) {
2604                                 free_imbuf_strip_elem(se);
2605                         }
2606                         for(a = 0, se = seq->strip->tstripdata_endstill; 
2607                             a < seq->strip->endstill && se; a++, se++) {
2608                                 free_imbuf_strip_elem(se);
2609                         }
2610
2611                         if(seq->type==SEQ_MOVIE)
2612                                 free_anim_seq(seq);
2613                         if(seq->type==SEQ_SPEED) {
2614                                 sequence_effect_speed_rebuild_map(seq, 1);
2615                         }
2616                 }
2617         }
2618         END_SEQ
2619 }
2620
2621 void free_imbuf_seq_with_ipo(struct Ipo *ipo)
2622 {
2623         /* force update of all sequences with this ipo, on ipo changes */
2624         Editing *ed= G.scene->ed;
2625         Sequence *seq;
2626
2627         if(ed==0) return;
2628
2629         WHILE_SEQ(&ed->seqbase) {
2630                 if(seq->ipo == ipo) {
2631                         update_changed_seq_and_deps(seq, 0, 1);
2632                         if(seq->type == SEQ_SPEED) {
2633                                 sequence_effect_speed_rebuild_map(seq, 1);
2634                         }
2635                 }
2636         }
2637         END_SEQ
2638 }
2639
2640 static int update_changed_seq_recurs(Sequence *seq, Sequence *changed_seq, int len_change, int ibuf_change)
2641 {
2642         Sequence *subseq;
2643         int a, free_imbuf = 0;
2644         TStripElem *se;
2645
2646         /* recurs downwards to see if this seq depends on the changed seq */
2647
2648         if(seq == NULL)
2649                 return 0;
2650
2651         if(seq == changed_seq)
2652                 free_imbuf = 1;
2653         
2654         for(subseq=seq->seqbase.first; subseq; subseq=subseq->next)
2655                 if(update_changed_seq_recurs(subseq, changed_seq, len_change, ibuf_change))
2656                         free_imbuf = TRUE;
2657         
2658         if(seq->seq1)
2659                 if(update_changed_seq_recurs(seq->seq1, changed_seq, len_change, ibuf_change))
2660                         free_imbuf = TRUE;
2661         if(seq->seq2 && (seq->seq2 != seq->seq1))
2662                 if(update_changed_seq_recurs(seq->seq2, changed_seq, len_change, ibuf_change))
2663                         free_imbuf = TRUE;
2664         if(seq->seq3 && (seq->seq3 != seq->seq1) && (seq->seq3 != seq->seq2))
2665                 if(update_changed_seq_recurs(seq->seq3, changed_seq, len_change, ibuf_change))
2666                         free_imbuf = TRUE;
2667         
2668         if(free_imbuf) {
2669                 if(ibuf_change) {
2670                         se= seq->strip->tstripdata;
2671                         if (se) {
2672                                 for(a=0; a<seq->len; a++, se++)
2673                                         free_imbuf_strip_elem(se);
2674                         }
2675                 
2676                         if(seq->type == SEQ_MOVIE)
2677                                 free_anim_seq(seq);
2678                         if(seq->type == SEQ_SPEED) {
2679                                 sequence_effect_speed_rebuild_map(seq, 1);
2680                         }
2681                 }
2682
2683                 if(len_change)
2684                         calc_sequence(seq);
2685         }
2686         
2687         return free_imbuf;
2688 }
2689
2690 void update_changed_seq_and_deps(Sequence *changed_seq, int len_change, int ibuf_change)
2691 {
2692         Editing *ed= G.scene->ed;
2693         Sequence *seq;
2694
2695         if (!ed) return;
2696
2697         for (seq=ed->seqbase.first; seq; seq=seq->next)
2698                 update_changed_seq_recurs(seq, changed_seq, len_change, ibuf_change);
2699 }
2700
2701 /* bad levell call... */
2702 void do_render_seq(RenderResult *rr, int cfra)
2703 {
2704         ImBuf *ibuf;
2705
2706         G.f |= G_PLAYANIM;      /* waitcursor patch */
2707
2708         ibuf= give_ibuf_seq(rr->rectx, rr->recty, cfra, 0);
2709         
2710         if(ibuf) {
2711                 if(ibuf->rect_float) {
2712                         if (!rr->rectf)
2713                                 rr->rectf= MEM_mallocN(4*sizeof(float)*rr->rectx*rr->recty, "render_seq rectf");
2714                         
2715                         memcpy(rr->rectf, ibuf->rect_float, 4*sizeof(float)*rr->rectx*rr->recty);
2716                         
2717                         /* TSK! Since sequence render doesn't free the *rr render result, the old rect32
2718                            can hang around when sequence render has rendered a 32 bits one before */
2719                         if(rr->rect32) {
2720                                 MEM_freeN(rr->rect32);
2721                                 rr->rect32= NULL;
2722                         }
2723                 }
2724                 else if(ibuf->rect) {
2725                         if (!rr->rect32)
2726                                 rr->rect32= MEM_mallocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
2727
2728                         memcpy(rr->rect32, ibuf->rect, 4*rr->rectx*rr->recty);
2729
2730                         /* if (ibuf->zbuf) { */
2731                         /*      if (R.rectz) freeN(R.rectz); */
2732                         /*      R.rectz = BLI_dupallocN(ibuf->zbuf); */
2733                         /* } */
2734                 }
2735                 
2736                 /* Let the cache limitor take care of this (schlaile) */
2737                 /* While render let's keep all memory available for render 
2738                    (ton)
2739                    At least if free memory is tight...
2740                    This can make a big difference in encoding speed
2741                    (it is around 4 times(!) faster, if we do not waste time
2742                    on freeing _all_ buffers every time on long timelines...)
2743                    (schlaile)
2744                 */
2745                 {
2746                         extern int mem_in_use;
2747                         extern int mmap_in_use;
2748
2749                         int max = MEM_CacheLimiter_get_maximum();
2750                         if (max != 0 && mem_in_use + mmap_in_use > max) {
2751                                 fprintf(stderr, "mem_in_use = %d, max = %d\n",
2752                                         mem_in_use + mmap_in_use, max);
2753                                 fprintf(stderr, "Cleaning up, please wait...\n"
2754                                         "If this happens very often,\n"
2755                                         "consider "
2756                                         "raising the memcache limit in the "
2757                                         "user preferences.\n");
2758                                 free_imbuf_seq();
2759                         }
2760                 }
2761         }
2762         else {
2763                 /* render result is delivered empty in most cases, nevertheless we handle all cases */
2764                 if (rr->rectf)
2765                         memset(rr->rectf, 0, 4*sizeof(float)*rr->rectx*rr->recty);
2766                 else if (rr->rect32)
2767                         memset(rr->rect32, 0, 4*rr->rectx*rr->recty);
2768                 else
2769                         rr->rect32= MEM_callocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
2770         }
2771         
2772         G.f &= ~G_PLAYANIM;
2773
2774 }