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