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