9a94f16712c69c54e21d1cd9247a3dd4d60a3812
[blender.git] / source / blender / src / sequence.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * Contributor(s): Peter Schlaile <peter [at] schlaile [dot] de> 2005/2006
27  *
28  * ***** END GPL/BL DUAL LICENSE BLOCK *****
29  */
30
31 #include <string.h>
32 #include <math.h>
33 #include <stdlib.h>
34
35 #include "MEM_guardedalloc.h"
36 #include "MEM_CacheLimiterC-Api.h"
37
38 #include "BLI_blenlib.h"
39 #include "BLI_arithb.h"
40
41 #include "IMB_imbuf_types.h"
42 #include "IMB_imbuf.h"
43
44 #include "DNA_ipo_types.h"
45 #include "DNA_scene_types.h"
46 #include "DNA_sequence_types.h"
47 #include "DNA_view3d_types.h"
48
49 #include "BKE_global.h"
50 #include "BKE_image.h"
51 #include "BKE_ipo.h"
52 #include "BKE_main.h"
53 #include "BKE_scene.h"
54 #include "BKE_texture.h"
55 #include "BKE_utildefines.h"
56
57 #include "BIF_editsound.h"
58 #include "BIF_editseq.h"
59 #include "BSE_filesel.h"
60 #include "BSE_headerbuttons.h"
61 #include "BIF_interface.h"
62 #include "BIF_renderwin.h"
63 #include "BIF_screen.h"
64 #include "BIF_space.h"
65 #include "BIF_toolbox.h"
66
67 #include "BSE_sequence.h"
68 #include "BSE_seqeffects.h"
69
70 #include "RE_pipeline.h"                // talks to entire render API
71
72 #include "blendef.h"
73
74 #include "BLI_threads.h"
75 #include <pthread.h>
76
77 int seqrectx, seqrecty;
78
79 void free_tstripdata(int len, TStripElem *se)
80 {
81         TStripElem *seo;
82         int a;
83
84         seo= se;
85         if (!se) {
86                 return;
87         }
88
89         for(a=0; a<len; a++, se++) {
90                 if(se->ibuf && se->ok != STRIPELEM_META) {
91                         IMB_freeImBuf(se->ibuf);
92                         se->ibuf = 0;
93                 }
94         }
95
96         MEM_freeN(seo);
97
98 }
99
100 void seq_proxy_free(StripProxy * proxy)
101 {
102         MEM_freeN(proxy);
103 }
104
105 void free_strip(Strip *strip)
106 {
107         strip->us--;
108         if(strip->us>0) return;
109         if(strip->us<0) {
110                 printf("error: negative users in strip\n");
111                 return;
112         }
113
114         if(strip->stripdata) {
115                 MEM_freeN(strip->stripdata);
116         }
117         if (strip->crop) {
118                 MEM_freeN(strip->crop);
119         }
120         if (strip->transform) {
121                 MEM_freeN(strip->transform);
122         }
123         if (strip->proxy) {
124                 seq_proxy_free(strip->proxy);
125         }
126
127         free_tstripdata(strip->len, strip->tstripdata);
128
129         MEM_freeN(strip);
130 }
131
132 void new_tstripdata(Sequence *seq)
133 {
134         if(seq->strip) {
135                 free_tstripdata(seq->strip->len, seq->strip->tstripdata);
136                 seq->strip->tstripdata= 0;
137                 seq->strip->len= seq->len;
138         }
139 }
140
141 void free_sequence(Sequence *seq)
142 {
143         Sequence *last_seq = get_last_seq();
144
145         if(seq->strip) free_strip(seq->strip);
146
147         if(seq->anim) IMB_free_anim(seq->anim);
148         if(seq->hdaudio) sound_close_hdaudio(seq->hdaudio);
149
150         if (seq->type & SEQ_EFFECT) {
151                 struct SeqEffectHandle sh = get_sequence_effect(seq);
152
153                 sh.free(seq);
154         }
155
156         if(seq==last_seq) set_last_seq(NULL);
157
158         MEM_freeN(seq);
159 }
160
161 /*
162   **********************************************************************
163   * build_seqar
164   **********************************************************************
165   * Build a complete array of _all_ sequencies (including those
166   * in metastrips!)
167   **********************************************************************
168 */
169
170 static void do_seq_count(ListBase *seqbase, int *totseq)
171 {
172         Sequence *seq;
173
174         seq= seqbase->first;
175         while(seq) {
176                 (*totseq)++;
177                 if(seq->seqbase.first) do_seq_count(&seq->seqbase, totseq);
178                 seq= seq->next;
179         }
180 }
181
182 static void do_build_seqar(ListBase *seqbase, Sequence ***seqar, int depth)
183 {
184         Sequence *seq;
185
186         seq= seqbase->first;
187         while(seq) {
188                 seq->depth= depth;
189                 if(seq->seqbase.first) do_build_seqar(&seq->seqbase, seqar, depth+1);
190                 **seqar= seq;
191                 (*seqar)++;
192                 seq= seq->next;
193         }
194 }
195
196 void build_seqar(ListBase *seqbase, Sequence  ***seqar, int *totseq)
197 {
198         Sequence **tseqar;
199
200         *totseq= 0;
201         do_seq_count(seqbase, totseq);
202
203         if(*totseq==0) {
204                 *seqar= 0;
205                 return;
206         }
207         *seqar= MEM_mallocN(sizeof(void *)* *totseq, "seqar");
208         tseqar= *seqar;
209
210         do_build_seqar(seqbase, seqar, 0);
211         *seqar= tseqar;
212 }
213
214 void free_editing(Editing *ed)
215 {
216         MetaStack *ms;
217         Sequence *seq;
218
219         if(ed==NULL) return;
220         set_last_seq(NULL);     /* clear_last_seq doesnt work, it screws up free_sequence */
221
222         WHILE_SEQ(&ed->seqbase) {
223                 free_sequence(seq);
224         }
225         END_SEQ
226
227         while( (ms= ed->metastack.first) ) {
228                 BLI_remlink(&ed->metastack, ms);
229                 MEM_freeN(ms);
230         }
231
232         MEM_freeN(ed);
233
234 }
235
236 void calc_sequence_disp(Sequence *seq)
237 {
238         if(seq->startofs && seq->startstill) seq->startstill= 0;
239         if(seq->endofs && seq->endstill) seq->endstill= 0;
240         
241         seq->startdisp= seq->start + seq->startofs - seq->startstill;
242         seq->enddisp= seq->start+seq->len - seq->endofs + seq->endstill;
243         
244         seq->handsize= 10.0;    /* 10 frames */
245         if( seq->enddisp-seq->startdisp < 10 ) {
246                 seq->handsize= (float)(0.5*(seq->enddisp-seq->startdisp));
247         }
248         else if(seq->enddisp-seq->startdisp > 250) {
249                 seq->handsize= (float)((seq->enddisp-seq->startdisp)/25);
250         }
251 }
252
253 void calc_sequence(Sequence *seq)
254 {
255         Sequence *seqm;
256         int min, max;
257
258         /* check all metas recursively */
259         seqm= seq->seqbase.first;
260         while(seqm) {
261                 if(seqm->seqbase.first) calc_sequence(seqm);
262                 seqm= seqm->next;
263         }
264
265         /* effects and meta: automatic start and end */
266
267         if(seq->type & SEQ_EFFECT) {
268                 /* pointers */
269                 if(seq->seq2==0) seq->seq2= seq->seq1;
270                 if(seq->seq3==0) seq->seq3= seq->seq1;
271
272                 /* effecten go from seq1 -> seq2: test */
273
274                 /* we take the largest start and smallest end */
275
276                 // seq->start= seq->startdisp= MAX2(seq->seq1->startdisp, seq->seq2->startdisp);
277                 // seq->enddisp= MIN2(seq->seq1->enddisp, seq->seq2->enddisp);
278
279                 if (seq->seq1) {
280                         seq->start= seq->startdisp= MAX3(seq->seq1->startdisp, seq->seq2->startdisp, seq->seq3->startdisp);
281                         seq->enddisp= MIN3(seq->seq1->enddisp, seq->seq2->enddisp, seq->seq3->enddisp);
282                         seq->len= seq->enddisp - seq->startdisp;
283                 } else {
284                         calc_sequence_disp(seq);
285                 }
286
287                 if(seq->strip && seq->len!=seq->strip->len) {
288                         new_tstripdata(seq);
289                 }
290
291         }
292         else {
293                 if(seq->type==SEQ_META) {
294                         seqm= seq->seqbase.first;
295                         if(seqm) {
296                                 min= 1000000;
297                                 max= -1000000;
298                                 while(seqm) {
299                                         if(seqm->startdisp < min) min= seqm->startdisp;
300                                         if(seqm->enddisp > max) max= seqm->enddisp;
301                                         seqm= seqm->next;
302                                 }
303                                 seq->start= min;
304                                 seq->len= max-min;
305
306                                 if(seq->strip && seq->len!=seq->strip->len) {
307                                         new_tstripdata(seq);
308                                 }
309                         }
310                 }
311                 calc_sequence_disp(seq);
312         }
313 }
314
315 void sort_seq()
316 {
317         /* all strips together per kind, and in order of y location ("machine") */
318         ListBase seqbase, effbase;
319         Editing *ed;
320         Sequence *seq, *seqt;
321
322         ed= G.scene->ed;
323         if(ed==0) return;
324
325         seqbase.first= seqbase.last= 0;
326         effbase.first= effbase.last= 0;
327
328         while( (seq= ed->seqbasep->first) ) {
329                 BLI_remlink(ed->seqbasep, seq);
330
331                 if(seq->type & SEQ_EFFECT) {
332                         seqt= effbase.first;
333                         while(seqt) {
334                                 if(seqt->machine>=seq->machine) {
335                                         BLI_insertlinkbefore(&effbase, seqt, seq);
336                                         break;
337                                 }
338                                 seqt= seqt->next;
339                         }
340                         if(seqt==0) BLI_addtail(&effbase, seq);
341                 }
342                 else {
343                         seqt= seqbase.first;
344                         while(seqt) {
345                                 if(seqt->machine>=seq->machine) {
346                                         BLI_insertlinkbefore(&seqbase, seqt, seq);
347                                         break;
348                                 }
349                                 seqt= seqt->next;
350                         }
351                         if(seqt==0) BLI_addtail(&seqbase, seq);
352                 }
353         }
354
355         addlisttolist(&seqbase, &effbase);
356         *(ed->seqbasep)= seqbase;
357 }
358
359
360 void clear_scene_in_allseqs(Scene *sce)
361 {
362         Scene *sce1;
363         Editing *ed;
364         Sequence *seq;
365
366         /* when a scene is deleted: test all seqs */
367
368         sce1= G.main->scene.first;
369         while(sce1) {
370                 if(sce1!=sce && sce1->ed) {
371                         ed= sce1->ed;
372
373                         WHILE_SEQ(&ed->seqbase) {
374
375                                 if(seq->scene==sce) seq->scene= 0;
376
377                         }
378                         END_SEQ
379                 }
380
381                 sce1= sce1->id.next;
382         }
383 }
384
385 /* ***************** DO THE SEQUENCE ***************** */
386
387 static void make_black_ibuf(ImBuf *ibuf)
388 {
389         unsigned int *rect;
390         float *rect_float;
391         int tot;
392
393         if(ibuf==0 || (ibuf->rect==0 && ibuf->rect_float==0)) return;
394
395         tot= ibuf->x*ibuf->y;
396
397         rect= ibuf->rect;
398         rect_float = ibuf->rect_float;
399
400         if (rect) {
401                 memset(rect,       0, tot * sizeof(char) * 4);
402         }
403
404         if (rect_float) {
405                 memset(rect_float, 0, tot * sizeof(float) * 4);
406         }
407 }
408
409 static void multibuf(ImBuf *ibuf, float fmul)
410 {
411         char *rt;
412         float *rt_float;
413
414         int a, mul, icol;
415
416         mul= (int)(256.0*fmul);
417
418         a= ibuf->x*ibuf->y;
419         rt= (char *)ibuf->rect;
420         rt_float = ibuf->rect_float;
421
422         if (rt) {
423                 while(a--) {
424
425                         icol= (mul*rt[0])>>8;
426                         if(icol>254) rt[0]= 255; else rt[0]= icol;
427                         icol= (mul*rt[1])>>8;
428                         if(icol>254) rt[1]= 255; else rt[1]= icol;
429                         icol= (mul*rt[2])>>8;
430                         if(icol>254) rt[2]= 255; else rt[2]= icol;
431                         icol= (mul*rt[3])>>8;
432                         if(icol>254) rt[3]= 255; else rt[3]= icol;
433                         
434                         rt+= 4;
435                 }
436         }
437         if (rt_float) {
438                 while(a--) {
439                         rt_float[0] *= fmul;
440                         rt_float[1] *= fmul;
441                         rt_float[2] *= fmul;
442                         rt_float[3] *= fmul;
443                         
444                         rt_float += 4;
445                 }
446         }
447 }
448
449 static void do_effect(int cfra, Sequence *seq, TStripElem *se)
450 {
451         TStripElem *se1, *se2, *se3;
452         float fac, facf;
453         int x, y;
454         int early_out;
455         struct SeqEffectHandle sh = get_sequence_effect(seq);
456
457         if (!sh.execute) { /* effect not supported in this version... */
458                 make_black_ibuf(se->ibuf);
459                 return;
460         }
461
462         if(seq->ipo && seq->ipo->curve.first) {
463                 do_seq_ipo(seq, cfra);
464                 fac= seq->facf0;
465                 facf= seq->facf1;
466         } else {
467                 sh.get_default_fac(seq, cfra, &fac, &facf);
468         }
469
470         if( !(G.scene->r.mode & R_FIELDS) ) facf = fac;
471
472         early_out = sh.early_out(seq, fac, facf);
473
474         if (early_out == -1) { /* no input needed */
475                 sh.execute(seq, cfra, fac, facf, se->ibuf->x, se->ibuf->y, 
476                            0, 0, 0, se->ibuf);
477                 return;
478         }
479
480         switch (early_out) {
481         case 0:
482                 if (se->se1==0 || se->se2==0 || se->se3==0) {
483                         make_black_ibuf(se->ibuf);
484                         return;
485                 }
486
487                 if(se->se1->ok == STRIPELEM_META) se1= se->se1->se1;
488                 else se1= se->se1;
489                 
490                 if(se->se2->ok == STRIPELEM_META) se2= se->se2->se1;
491                 else se2= se->se2;
492                 
493                 if(se->se3->ok == STRIPELEM_META) se3= se->se3->se1;
494                 else se3= se->se3;
495
496                 if (   (se1==0 || se2==0 || se3==0)
497                     || (se1->ibuf==0 || se2->ibuf==0 || se3->ibuf==0)) {
498                         make_black_ibuf(se->ibuf);
499                         return;
500                 }
501
502                 break;
503         case 1:
504                 if (se->se1 == 0) {
505                         make_black_ibuf(se->ibuf);
506                         return;
507                 }
508
509                 if(se->se1->ok == STRIPELEM_META) se1= se->se1->se1;
510                 else se1= se->se1;
511
512                 if (se1 == 0 || se1->ibuf == 0) {
513                         make_black_ibuf(se->ibuf);
514                         return;
515                 }
516
517                 if (se->ibuf != se1->ibuf) {
518                         IMB_freeImBuf(se->ibuf);
519                         se->ibuf = se1->ibuf;
520                         IMB_refImBuf(se->ibuf);
521                 }
522                 return;
523         case 2:
524                 if (se->se2 == 0) {
525                         make_black_ibuf(se->ibuf);
526                         return;
527                 }
528
529                 if(se->se2->ok == STRIPELEM_META) se2= se->se2->se1;
530                 else se2= se->se2;
531
532                 if (se2 == 0 || se2->ibuf == 0) {
533                         make_black_ibuf(se->ibuf);
534                         return;
535                 }
536                 if (se->ibuf != se2->ibuf) {
537                         IMB_freeImBuf(se->ibuf);
538                         se->ibuf = se2->ibuf;
539                         IMB_refImBuf(se->ibuf);
540                 }
541                 return;
542         default:
543                 make_black_ibuf(se->ibuf);
544                 return;
545         }
546
547         x= se2->ibuf->x;
548         y= se2->ibuf->y;
549
550         if (!se1->ibuf->rect_float && se->ibuf->rect_float) {
551                 IMB_float_from_rect(se1->ibuf);
552         }
553         if (!se2->ibuf->rect_float && se->ibuf->rect_float) {
554                 IMB_float_from_rect(se2->ibuf);
555         }
556
557         if (!se1->ibuf->rect && !se->ibuf->rect_float) {
558                 IMB_rect_from_float(se1->ibuf);
559         }
560         if (!se2->ibuf->rect && !se->ibuf->rect_float) {
561                 IMB_rect_from_float(se2->ibuf);
562         }
563
564         sh.execute(seq, cfra, fac, facf, x, y, se1->ibuf, se2->ibuf, se3->ibuf,
565                    se->ibuf);
566 }
567
568 static int give_stripelem_index(Sequence *seq, int cfra)
569 {
570         int nr;
571
572         if(seq->startdisp >cfra || seq->enddisp <= cfra) return -1;
573
574         if(seq->flag&SEQ_REVERSE_FRAMES)        {       
575                 /*reverse frame in this sequence */
576                 if(cfra <= seq->start) nr= seq->len-1;
577                 else if(cfra >= seq->start+seq->len-1) nr= 0;
578                 else nr= (seq->start + seq->len) - cfra;
579         } else {
580                 if(cfra <= seq->start) nr= 0;
581                 else if(cfra >= seq->start+seq->len-1) nr= seq->len-1;
582                 else nr= cfra-seq->start;
583         }
584         if (seq->strobe < 1.0) seq->strobe = 1.0;
585         if (seq->strobe > 1.0) {
586                 nr -= (int)fmod((double)nr, (double)seq->strobe);
587         }
588
589         return nr;
590 }
591
592 TStripElem *give_tstripelem(Sequence *seq, int cfra)
593 {
594         TStripElem *se;
595         int nr;
596
597         se = seq->strip->tstripdata;
598         if (se == 0 && seq->len > 0) {
599                 int i;
600                 se = seq->strip->tstripdata = MEM_callocN(
601                         seq->len*sizeof(TStripElem), "tstripelems");
602                 for (i = 0; i < seq->len; i++) {
603                         se[i].ok = STRIPELEM_OK;
604                 }
605         }
606         nr = give_stripelem_index(seq, cfra);
607
608         if (nr == -1) return 0;
609         if (se == 0) return 0;
610         
611         se+= nr; 
612         se->nr= nr;
613         
614         return se;
615 }
616
617 StripElem *give_stripelem(Sequence *seq, int cfra)
618 {
619         StripElem *se;
620         int nr;
621
622         se = seq->strip->stripdata;
623         nr = give_stripelem_index(seq, cfra);
624
625         if (nr == -1) return 0;
626         if (se == 0) return 0;
627
628         se += nr; 
629         
630         return se;
631 }
632
633 static int evaluate_seq_frame_gen(
634         Sequence ** seq_arr, ListBase *seqbase, int cfra)
635 {
636         Sequence *seq;
637         int totseq=0;
638
639         memset(seq_arr, 0, sizeof(Sequence*) * MAXSEQ);
640
641         seq= seqbase->first;
642         while(seq) {
643                 if(seq->startdisp <=cfra && seq->enddisp > cfra) {
644                         seq_arr[seq->machine]= seq;
645                         totseq++;
646                 }
647                 seq= seq->next;
648         }
649
650         return totseq;
651 }
652
653 int evaluate_seq_frame(int cfra)
654 {
655        Editing *ed;
656        Sequence *seq_arr[MAXSEQ+1];
657
658        ed= G.scene->ed;
659        if(ed==0) return 0;
660         
661        return evaluate_seq_frame_gen(seq_arr, ed->seqbasep, cfra);
662
663 }
664
665 Sequence *get_shown_sequence(ListBase * seqbasep, int cfra, int chanshown)
666 {
667         Sequence *seq, *seqim, *seqeff;
668         Sequence *seq_arr[MAXSEQ+1];
669         int b;
670
671         seq = 0;
672
673         if (chanshown > MAXSEQ) {
674                 return 0;
675         }
676
677         if(evaluate_seq_frame_gen(seq_arr, seqbasep, cfra)) {
678                 if (chanshown > 0) {
679                         return seq_arr[chanshown];
680                 }
681
682                 /* we take the upper effect strip or 
683                    the lowest imagestrip/metastrip */
684                 seqim= seqeff= 0;
685
686                 for(b=1; b<MAXSEQ; b++) {
687                         if(seq_arr[b]) {
688                                 seq= seq_arr[b];
689                                 if(seq->flag & SEQ_MUTE) {
690                                         /* skip */
691                                 } else if(seq->type & SEQ_EFFECT) {
692                                         if(seqeff==0) seqeff= seq;
693                                         else if(seqeff->machine < seq->machine)
694                                                 seqeff= seq;
695                                 } else if (seq->type != SEQ_RAM_SOUND && seq->type != SEQ_HD_SOUND) {
696                                         if(seqim==0) seqim= seq;
697                                         else if(seqim->machine > seq->machine)
698                                                 seqim= seq;
699                                 }
700                         }
701                 }
702                 if(seqeff) seq= seqeff;
703                 else if(seqim) seq= seqim;
704                 else seq= 0;
705         }
706         
707         return seq;
708 }
709  
710 static Sequence * get_shown_seq_from_metastrip(Sequence * seqm, int cfra)
711 {
712         return get_shown_sequence(&seqm->seqbase, cfra, 0);
713 }
714
715 void set_meta_stripdata(Sequence *seqm)
716 {
717         Sequence *seq;
718         TStripElem *se;
719         int a, cfra;
720
721         se= seqm->strip->tstripdata;
722
723         if (se == 0 && seqm->len > 0) {
724                 int i;
725                 se = seqm->strip->tstripdata = MEM_callocN(
726                         seqm->len*sizeof(TStripElem), "tstripelems");
727                 for (i = 0; i < seqm->len; i++) {
728                         se[i].ok = STRIPELEM_META;
729                 }
730         }
731
732         /* sets all ->se1 pointers in stripdata, to read the ibuf from it */
733
734         for(a=0; a<seqm->len; a++, se++) {
735                 cfra= a+seqm->start;
736                 seq = get_shown_seq_from_metastrip(seqm, cfra);
737                 if (seq) {
738                         se->se1= give_tstripelem(seq, cfra);
739                 } else { 
740                         se->se1= 0;
741                 }
742         }
743 }
744
745 /*
746   input preprocessing for SEQ_IMAGE, SEQ_MOVIE and SEQ_SCENE
747
748   Do all the things you can't really do afterwards using sequence effects
749   (read: before rescaling to render resolution has been done)
750
751   Order is important!
752
753   - Deinterlace
754   - Crop and transform in image source coordinate space
755   - Flip X + Flip Y (could be done afterwards, backward compatibility)
756   - Promote image to float data (affects pipeline operations afterwards)
757   - Premultiply
758
759 */
760
761 static void input_preprocess(Sequence * seq, TStripElem* se, int cfra)
762 {
763         seq->strip->orx= se->ibuf->x;
764         seq->strip->ory= se->ibuf->y;
765
766         if(seq->flag & SEQ_FILTERY) {
767                 IMB_filtery(se->ibuf);
768         }
769
770         if(seq->flag & SEQ_USE_CROP || seq->flag & SEQ_USE_TRANSFORM) {
771                 StripCrop c;
772                 StripTransform t;
773
774                 memset(&c, 0, sizeof(StripCrop));
775                 memset(&t, 0, sizeof(StripTransform));
776
777                 if(seq->flag & SEQ_USE_CROP && seq->strip->crop) {
778                         c = *seq->strip->crop;
779                 }
780                 if(seq->flag & SEQ_USE_TRANSFORM && seq->strip->transform) {
781                         t = *seq->strip->transform;
782                 }
783
784                 if (c.top + c.bottom >= se->ibuf->y ||
785                     c.left + c.right >= se->ibuf->x ||
786                     t.xofs >= se->ibuf->x ||
787                     t.yofs >= se->ibuf->y) {
788                         make_black_ibuf(se->ibuf);
789                 } else {
790                         ImBuf * i;
791                         int sx = se->ibuf->x - c.left - c.right;
792                         int sy = se->ibuf->y - c.top - c.bottom;
793                         int dx = sx;
794                         int dy = sy;
795
796                         if (seq->flag & SEQ_USE_TRANSFORM) {
797                                 dx = seqrectx;
798                                 dy = seqrecty;
799                         }
800
801                         if (se->ibuf->rect_float) {
802                                 i = IMB_allocImBuf(dx, dy,32, IB_rectfloat, 0);
803                         } else {
804                                 i = IMB_allocImBuf(dx, dy,32, IB_rect, 0);
805                         }
806
807                         IMB_rectcpy(i, se->ibuf, 
808                                     t.xofs, t.yofs, 
809                                     c.left, c.bottom, 
810                                     sx, sy);
811
812                         IMB_freeImBuf(se->ibuf);
813
814                         se->ibuf = i;
815                 }
816         } 
817
818         if(seq->flag & SEQ_FLIPX) {
819                 IMB_flipx(se->ibuf);
820         }
821         if(seq->flag & SEQ_FLIPY) {
822                 IMB_flipy(se->ibuf);
823         }
824
825         if(seq->flag & SEQ_MAKE_FLOAT) {
826                 if (!se->ibuf->rect_float) {
827                         IMB_float_from_rect(se->ibuf);
828                 }
829         }
830
831         if(seq->mul == 0.0) {
832                 seq->mul = 1.0;
833         }
834         if(seq->mul != 1.0) {
835                 multibuf(se->ibuf, seq->mul);
836         }
837
838         if(seq->flag & SEQ_MAKE_PREMUL) {
839                 if(se->ibuf->depth == 32 && se->ibuf->zbuf == 0) {
840                         converttopremul(se->ibuf);
841                 }
842         }
843
844
845         if(se->ibuf->x != seqrectx || se->ibuf->y != seqrecty ) {
846                 if(G.scene->r.mode & R_OSA) {
847                         IMB_scaleImBuf(se->ibuf, 
848                                        (short)seqrectx, (short)seqrecty);
849                 } else {
850                         IMB_scalefastImBuf(se->ibuf, 
851                                            (short)seqrectx, (short)seqrecty);
852                 }
853         }
854 }
855
856 static TStripElem* do_build_seq_recursively(Sequence * seq, int cfra);
857
858 static void do_build_seq_ibuf(Sequence * seq, TStripElem *se, int cfra)
859 {
860         char name[FILE_MAXDIR+FILE_MAXFILE];
861
862         if (seq->type != SEQ_META && se->ibuf) {
863                 /* test if image too small 
864                    or discarded from cache: reload */
865                 if(se->ibuf->x != seqrectx || se->ibuf->y != seqrecty 
866                    || !(se->ibuf->rect || se->ibuf->rect_float)) {
867                         IMB_freeImBuf(se->ibuf);
868                         se->ibuf= 0;
869                         se->ok= STRIPELEM_OK;
870                 }
871         }
872
873         if(seq->type == SEQ_META) {
874                 if(seq->seqbase.first) {
875                         Sequence * seqmshown= 
876                                 get_shown_seq_from_metastrip(seq, cfra);
877                         if (seqmshown) {
878                                 if(cfra< seq->start) 
879                                         do_build_seq_recursively(seqmshown, seq->start);
880                                 else if(cfra> seq->start+seq->len-1) 
881                                         do_build_seq_recursively(seqmshown, seq->start + seq->len-1);
882                                 else do_build_seq_recursively(seqmshown, cfra);
883                         }
884                 }
885
886                 se->ok = STRIPELEM_META;
887                 if(se->se1 == 0) set_meta_stripdata(seq);
888                 if(se->se1) {
889                         se->ibuf= se->se1->ibuf;
890                 }
891         } else if(seq->type & SEQ_EFFECT) {
892                 /* should the effect be recalculated? */
893                 
894                 if(se->ibuf == 0) {
895                         /* if one of two first inputs are rectfloat, output is float too */
896                         if((se->se1 && se->se1->ibuf && se->se1->ibuf->rect_float) ||
897                            (se->se2 && se->se2->ibuf && se->se2->ibuf->rect_float))
898                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
899                         else
900                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
901                         
902                         do_effect(cfra, seq, se);
903                 }
904
905         } else if(seq->type < SEQ_EFFECT) {
906                 
907                 if(seq->type==SEQ_IMAGE) {
908                         if(se->ok == STRIPELEM_OK && se->ibuf==0) {
909                                 StripElem * s_elem = give_stripelem(seq, cfra);
910
911                                 strncpy(name, seq->strip->dir, FILE_MAXDIR-1);
912                                 strncat(name, s_elem->name, FILE_MAXFILE);
913                                 BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
914                                 se->ibuf= IMB_loadiffname(name, IB_rect);
915                                 
916                                 if(se->ibuf == 0) {
917                                         se->ok = STRIPELEM_FAILED;
918                                 } else {
919                                         input_preprocess(seq, se, cfra);
920                                 }
921                         }
922                 }
923                 else if(seq->type==SEQ_MOVIE) {
924                         if(se->ok == STRIPELEM_OK && se->ibuf==0) {
925                                 if(seq->anim==0) {
926                                         strncpy(name, seq->strip->dir, FILE_MAXDIR-1);
927                                         strncat(name, seq->strip->stripdata->name, FILE_MAXFILE-1);
928                                         BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
929                                         
930                                         seq->anim = openanim(name, IB_rect);
931                                 }
932                                 if(seq->anim) {
933                                         IMB_anim_set_preseek(seq->anim, seq->anim_preseek);
934                                         se->ibuf = IMB_anim_absolute(seq->anim, se->nr);
935                                 }
936                                 
937                                 if(se->ibuf == 0) {
938                                         se->ok = STRIPELEM_FAILED;
939                                 } else {
940                                         input_preprocess(seq, se, cfra);
941                                 }
942                         }
943                 } else if(seq->type==SEQ_SCENE && se->ibuf==NULL && seq->scene) {       // scene can be NULL after deletions
944                         int oldcfra = CFRA;
945                         Scene *sce= seq->scene, *oldsce= G.scene;
946                         Render *re;
947                         RenderResult rres;
948                         int doseq, rendering= G.rendering;
949                         char scenename[64];
950                         
951                         waitcursor(1);
952                         
953                         /* Hack! This function can be called from do_render_seq(), in that case
954                            the seq->scene can already have a Render initialized with same name, 
955                            so we have to use a default name. (compositor uses G.scene name to
956                            find render).
957                            However, when called from within the UI (image preview in sequencer)
958                            we do want to use scene Render, that way the render result is defined
959                            for display in render/imagewindow */
960                         if(rendering) {
961                                 BLI_strncpy(scenename, sce->id.name+2, 64);
962                                 strcpy(sce->id.name+2, " do_build_seq_ibuf");
963                         }
964                         re= RE_NewRender(sce->id.name);
965                         
966                         /* prevent eternal loop */
967                         doseq= G.scene->r.scemode & R_DOSEQ;
968                         G.scene->r.scemode &= ~R_DOSEQ;
969                         
970                         BIF_init_render_callbacks(re, 0);       /* 0= no display callbacks */
971                         
972                         /* hrms, set_scene still needed? work on that... */
973                         if(sce!=oldsce) set_scene_bg(sce);
974                         RE_BlenderFrame(re, sce, seq->sfra + se->nr);
975                         if(sce!=oldsce) set_scene_bg(oldsce);
976                         
977                         /* UGLY WARNING, it is set to zero in  RE_BlenderFrame */
978                         G.rendering= rendering;
979                         if(rendering)
980                                 BLI_strncpy(sce->id.name+2, scenename, 64);
981                         
982                         RE_GetResultImage(re, &rres);
983                         
984                         if(rres.rectf) {
985                                 se->ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rectfloat, 0);
986                                 memcpy(se->ibuf->rect_float, rres.rectf, 4*sizeof(float)*rres.rectx*rres.recty);
987                                 if(rres.rectz) {
988                                         addzbuffloatImBuf(se->ibuf);
989                                         memcpy(se->ibuf->zbuf_float, rres.rectz, sizeof(float)*rres.rectx*rres.recty);
990                                 }
991                         } else if (rres.rect32) {
992                                 se->ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rect, 0);
993                                 memcpy(se->ibuf->rect, rres.rect32, 4*rres.rectx*rres.recty);
994                         }
995                         
996                         BIF_end_render_callbacks();
997                         
998                         /* restore */
999                         G.scene->r.scemode |= doseq;
1000                         
1001                         if((G.f & G_PLAYANIM)==0) /* bad, is set on do_render_seq */
1002                                 waitcursor(0);
1003                         CFRA = oldcfra;
1004
1005                         input_preprocess(seq, se, cfra);
1006                 }       
1007         }
1008         if (se->ibuf && seq->type != SEQ_META) {
1009                 IMB_cache_limiter_insert(se->ibuf);
1010                 IMB_cache_limiter_ref(se->ibuf);
1011                 IMB_cache_limiter_touch(se->ibuf);
1012         }
1013 }
1014
1015 static void do_effect_seq_recursively(Sequence * seq, TStripElem *se, int cfra)
1016 {
1017         float fac, facf;
1018         struct SeqEffectHandle sh = get_sequence_effect(seq);
1019         int early_out;
1020
1021         se->se1 = 0;
1022         se->se2 = 0;
1023         se->se3 = 0;
1024
1025         if(seq->ipo && seq->ipo->curve.first) {
1026                 do_seq_ipo(seq, cfra);
1027                 fac= seq->facf0;
1028                 facf= seq->facf1;
1029         } else {
1030                 sh.get_default_fac(seq, cfra, &fac, &facf);
1031         } 
1032
1033         if( G.scene->r.mode & R_FIELDS ); else facf= fac;
1034         
1035         early_out = sh.early_out(seq, fac, facf);
1036         switch (early_out) {
1037         case -1:
1038                 /* no input needed */
1039                 break;
1040         case 0:
1041                 se->se1 = do_build_seq_recursively(seq->seq1, cfra);
1042                 se->se2 = do_build_seq_recursively(seq->seq2, cfra);
1043                 if (seq->seq3) {
1044                         se->se3 = do_build_seq_recursively(seq->seq3, cfra);
1045                 }
1046                 break;
1047         case 1:
1048                 se->se1 = do_build_seq_recursively(seq->seq1, cfra);
1049                 break;
1050         case 2:
1051                 se->se2 = do_build_seq_recursively(seq->seq2, cfra);
1052                 break;
1053         }
1054
1055
1056         do_build_seq_ibuf(seq, se, cfra);
1057
1058         /* children are not needed anymore ... */
1059
1060         if (se->se1 && se->se1->ibuf) {
1061                 IMB_cache_limiter_unref(se->se1->ibuf);
1062         }
1063         if (se->se2 && se->se2->ibuf) {
1064                 IMB_cache_limiter_unref(se->se2->ibuf);
1065         }
1066         if (se->se3 && se->se3->ibuf) {
1067                 IMB_cache_limiter_unref(se->se3->ibuf);
1068         }
1069 }
1070
1071 static TStripElem* do_build_seq_recursively_impl(Sequence * seq, int cfra)
1072 {
1073         TStripElem *se;
1074
1075         se = give_tstripelem(seq, cfra);
1076
1077         if(se) {
1078                 if (seq->type & SEQ_EFFECT) {
1079                         do_effect_seq_recursively(seq, se, cfra);
1080                 } else {
1081                         do_build_seq_ibuf(seq, se, cfra);
1082                 }
1083         }
1084         return se;
1085 }
1086
1087 /* FIXME:
1088    
1089 If cfra was float throughout blender (especially in the render
1090 pipeline) one could even _render_ with subframe precision
1091 instead of faking using the blend code below...
1092
1093 */
1094
1095 static TStripElem* do_handle_speed_effect(Sequence * seq, int cfra)
1096 {
1097         SpeedControlVars * s = (SpeedControlVars *)seq->effectdata;
1098         int nr = cfra - seq->start;
1099         float f_cfra;
1100         int cfra_left;
1101         int cfra_right;
1102         TStripElem * se = 0;
1103         TStripElem * se1 = 0;
1104         TStripElem * se2 = 0;
1105         
1106         sequence_effect_speed_rebuild_map(seq, 0);
1107         
1108         f_cfra = seq->start + s->frameMap[nr];
1109         
1110         cfra_left = (int) floor(f_cfra);
1111         cfra_right = (int) ceil(f_cfra);
1112
1113         se = give_tstripelem(seq, cfra);
1114
1115         if (cfra_left == cfra_right || 
1116             (s->flags & SEQ_SPEED_BLEND) == 0) {
1117                 if(se->ibuf) {
1118                         if(se->ibuf->x < seqrectx || se->ibuf->y < seqrecty 
1119                            || !(se->ibuf->rect || se->ibuf->rect_float)) {
1120                                 IMB_freeImBuf(se->ibuf);
1121                                 se->ibuf= 0;
1122                         }
1123                 }
1124
1125                 if (se->ibuf == NULL) {
1126                         se1 = do_build_seq_recursively_impl(
1127                                 seq->seq1, cfra_left);
1128
1129                         if((se1 && se1->ibuf && se1->ibuf->rect_float))
1130                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
1131                         else
1132                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
1133
1134                         if (se1 == 0 || se1->ibuf == 0) {
1135                                 make_black_ibuf(se->ibuf);
1136                         } else {
1137                                 if (se->ibuf != se1->ibuf) {
1138                                         if (se->ibuf) {
1139                                                 IMB_freeImBuf(se->ibuf);
1140                                         }
1141
1142                                         se->ibuf = se1->ibuf;
1143                                         IMB_refImBuf(se->ibuf);
1144                                 }
1145                         }
1146                 }
1147         } else {
1148                 struct SeqEffectHandle sh;
1149
1150                 if(se->ibuf) {
1151                         if(se->ibuf->x < seqrectx || se->ibuf->y < seqrecty 
1152                            || !(se->ibuf->rect || se->ibuf->rect_float)) {
1153                                 IMB_freeImBuf(se->ibuf);
1154                                 se->ibuf= 0;
1155                         }
1156                 }
1157
1158                 if (se->ibuf == NULL) {
1159                         se1 = do_build_seq_recursively_impl(
1160                                 seq->seq1, cfra_left);
1161                         se2 = do_build_seq_recursively_impl(
1162                                 seq->seq1, cfra_right);
1163
1164                         if((se1 && se1->ibuf && se1->ibuf->rect_float))
1165                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rectfloat, 0);
1166                         else
1167                                 se->ibuf= IMB_allocImBuf((short)seqrectx, (short)seqrecty, 32, IB_rect, 0);
1168                         
1169                         if (!se1 || !se2) {
1170                                 make_black_ibuf(se->ibuf);
1171                         } else {
1172                                 sh = get_sequence_effect(seq);
1173
1174                                 sh.execute(seq, cfra, 
1175                                            f_cfra - (float) cfra_left, 
1176                                            f_cfra - (float) cfra_left, 
1177                                            se->ibuf->x, se->ibuf->y, 
1178                                            se1->ibuf, se2->ibuf, 0, se->ibuf);
1179                         }
1180                 }
1181
1182         }
1183
1184         /* caller expects this to be referenced, so do it! */
1185         if (se->ibuf) {
1186                 IMB_cache_limiter_insert(se->ibuf);
1187                 IMB_cache_limiter_ref(se->ibuf);
1188                 IMB_cache_limiter_touch(se->ibuf);
1189         }
1190
1191         /* children are no longer needed */
1192         if (se1 && se1->ibuf)
1193                 IMB_cache_limiter_unref(se1->ibuf);
1194         if (se2 && se2->ibuf)
1195                 IMB_cache_limiter_unref(se2->ibuf);
1196
1197         return se;
1198 }
1199
1200 /* 
1201  * build all ibufs recursively
1202  * 
1203  * if successfull, the returned TStripElem contains the (referenced!) imbuf
1204  * that means: you _must_ call 
1205  *
1206  * IMB_cache_limiter_unref(rval);
1207  * 
1208  * if rval != 0
1209  * 
1210  */
1211
1212 static TStripElem* do_build_seq_recursively(Sequence * seq, int cfra)
1213 {
1214         if (seq->type == SEQ_SPEED) {
1215                 return do_handle_speed_effect(seq, cfra);
1216         } else {
1217                 return do_build_seq_recursively_impl(seq, cfra);
1218         }
1219 }
1220
1221 /*
1222  * returned ImBuf is refed!
1223  * you have to unref after usage!
1224  */
1225
1226 static ImBuf *give_ibuf_seq_impl(int rectx, int recty, int cfra, int chanshown)
1227 {
1228         Sequence *seqfirst=0;
1229         Editing *ed;
1230         int count;
1231         ListBase *seqbasep;
1232         TStripElem *se;
1233
1234         ed= G.scene->ed;
1235         if(ed==0) return 0;
1236
1237         count = BLI_countlist(&ed->metastack);
1238         if((chanshown < 0) && (count > 0)) {
1239                 count = MAX2(count + chanshown, 0);
1240                 seqbasep= ((MetaStack*)BLI_findlink(&ed->metastack, count))->oldbasep;
1241         } else {
1242                 seqbasep= ed->seqbasep;
1243         }
1244
1245         seqrectx= rectx;        /* bad bad global! */
1246         seqrecty= recty;
1247
1248         seqfirst = get_shown_sequence(seqbasep, cfra, chanshown);
1249
1250         if (!seqfirst) {
1251                 return 0;
1252         }
1253
1254         se = do_build_seq_recursively(seqfirst, cfra);
1255
1256         if(!se) { 
1257                 return 0;
1258         }
1259
1260         return se->ibuf;
1261 }
1262
1263 ImBuf *give_ibuf_seq_direct(int rectx, int recty, int cfra,
1264                             Sequence * seq)
1265 {
1266         TStripElem* se;
1267
1268         seqrectx= rectx;        /* bad bad global! */
1269         seqrecty= recty;
1270
1271         se = do_build_seq_recursively(seq, cfra);
1272
1273         if(!se) { 
1274                 return 0;
1275         }
1276
1277         if (se->ibuf) {
1278                 IMB_cache_limiter_unref(se->ibuf);
1279         }
1280
1281         return se->ibuf;
1282 }
1283
1284 ImBuf *give_ibuf_seq(int rectx, int recty, int cfra, int chanshown)
1285 {
1286         ImBuf* i = give_ibuf_seq_impl(rectx, recty, cfra, chanshown);
1287
1288         if (i) {
1289                 IMB_cache_limiter_unref(i);
1290         }
1291         return i;
1292 }
1293
1294 /* threading api */
1295
1296 static ListBase running_threads;
1297 static ListBase prefetch_wait;
1298 static ListBase prefetch_done;
1299
1300 static pthread_mutex_t queue_lock          = PTHREAD_MUTEX_INITIALIZER;
1301 static pthread_mutex_t wakeup_lock         = PTHREAD_MUTEX_INITIALIZER;
1302 static pthread_cond_t  wakeup_cond         = PTHREAD_COND_INITIALIZER;
1303
1304 static pthread_mutex_t prefetch_ready_lock = PTHREAD_MUTEX_INITIALIZER;
1305 static pthread_cond_t  prefetch_ready_cond = PTHREAD_COND_INITIALIZER;
1306
1307 static pthread_mutex_t frame_done_lock     = PTHREAD_MUTEX_INITIALIZER;
1308 static pthread_cond_t  frame_done_cond     = PTHREAD_COND_INITIALIZER;
1309
1310 static volatile int seq_thread_shutdown = FALSE;
1311 static volatile int seq_last_given_monoton_cfra = 0;
1312 static int monoton_cfra = 0;
1313
1314 typedef struct PrefetchThread {
1315         struct PrefetchThread *next, *prev;
1316         struct PrefetchQueueElem *current;
1317         pthread_t pthread;
1318         int running;
1319 } PrefetchThread;
1320
1321 typedef struct PrefetchQueueElem {
1322         struct PrefetchQueueElem *next, *prev;
1323         
1324         int rectx;
1325         int recty;
1326         int cfra;
1327         int chanshown;
1328
1329         int monoton_cfra;
1330
1331         struct ImBuf * ibuf;
1332 } PrefetchQueueElem;
1333
1334 static void * seq_prefetch_thread(void * This_)
1335 {
1336         PrefetchThread * This = This_;
1337
1338         while (!seq_thread_shutdown) {
1339                 PrefetchQueueElem * e;
1340                 int s_last;
1341
1342                 pthread_mutex_lock(&queue_lock);
1343                 e = prefetch_wait.first;
1344                 if (e) {
1345                         BLI_remlink(&prefetch_wait, e);
1346                 }
1347                 s_last = seq_last_given_monoton_cfra;
1348
1349                 This->current = e;
1350
1351                 pthread_mutex_unlock(&queue_lock);
1352
1353                 if (!e) {
1354                         pthread_mutex_lock(&prefetch_ready_lock);
1355
1356                         This->running = FALSE;
1357
1358                         pthread_cond_signal(&prefetch_ready_cond);
1359                         pthread_mutex_unlock(&prefetch_ready_lock);
1360
1361                         pthread_mutex_lock(&wakeup_lock);
1362                         if (!seq_thread_shutdown) {
1363                                 pthread_cond_wait(&wakeup_cond, &wakeup_lock);
1364                         }
1365                         pthread_mutex_unlock(&wakeup_lock);
1366                         continue;
1367                 }
1368
1369                 This->running = TRUE;
1370                 
1371                 if (e->cfra >= s_last) { 
1372                         e->ibuf = give_ibuf_seq_impl(
1373                                 e->rectx, e->recty, e->cfra, e->chanshown);
1374                 }
1375
1376                 pthread_mutex_lock(&queue_lock);
1377
1378                 BLI_addtail(&prefetch_done, e);
1379
1380                 for (e = prefetch_wait.first; e; e = e->next) {
1381                         if (s_last > e->monoton_cfra) {
1382                                 BLI_remlink(&prefetch_wait, e);
1383                                 MEM_freeN(e);
1384                         }
1385                 }
1386
1387                 for (e = prefetch_done.first; e; e = e->next) {
1388                         if (s_last > e->monoton_cfra) {
1389                                 if (e->ibuf) {
1390                                         IMB_cache_limiter_unref(e->ibuf);
1391                                 }
1392                                 BLI_remlink(&prefetch_done, e);
1393                                 MEM_freeN(e);
1394                         }
1395                 }
1396
1397                 pthread_mutex_unlock(&queue_lock);
1398
1399                 pthread_mutex_lock(&frame_done_lock);
1400                 pthread_cond_signal(&frame_done_cond);
1401                 pthread_mutex_unlock(&frame_done_lock);
1402         }
1403         return 0;
1404 }
1405
1406 void seq_start_threads()
1407 {
1408         int i;
1409
1410         running_threads.first = running_threads.last = NULL;
1411         prefetch_wait.first = prefetch_wait.last = NULL;
1412         prefetch_done.first = prefetch_done.last = NULL;
1413
1414         seq_thread_shutdown = FALSE;
1415         seq_last_given_monoton_cfra = monoton_cfra = 0;
1416
1417         /* since global structures are modified during the processing
1418            of one frame, only one render thread is currently possible... 
1419
1420            (but we code, in the hope, that we can remove this restriction
1421            soon...)
1422         */
1423
1424         fprintf(stderr, "SEQ-THREAD: seq_start_threads\n");
1425
1426         for (i = 0; i < 1; i++) {
1427                 PrefetchThread *t = MEM_callocN(sizeof(PrefetchThread), 
1428                                                 "prefetch_thread");
1429                 t->running = TRUE;
1430                 BLI_addtail(&running_threads, t);
1431
1432                 pthread_create(&t->pthread, NULL, seq_prefetch_thread, t);
1433         }
1434
1435         /* init malloc mutex */
1436         BLI_init_threads(0, 0, 0);
1437 }
1438
1439 void seq_stop_threads()
1440 {
1441         PrefetchThread *tslot;
1442         PrefetchQueueElem * e;
1443
1444         fprintf(stderr, "SEQ-THREAD: seq_stop_threads()\n");
1445
1446         if (seq_thread_shutdown) {
1447                 fprintf(stderr, "SEQ-THREAD: ... already stopped\n");
1448                 return;
1449         }
1450         
1451         pthread_mutex_lock(&wakeup_lock);
1452
1453         seq_thread_shutdown = TRUE;
1454
1455         pthread_cond_broadcast(&wakeup_cond);
1456         pthread_mutex_unlock(&wakeup_lock);
1457
1458         for(tslot = running_threads.first; tslot; tslot= tslot->next) {
1459                 pthread_join(tslot->pthread, NULL);
1460         }
1461
1462
1463         for (e = prefetch_wait.first; e; e = e->next) {
1464                 BLI_remlink(&prefetch_wait, e);
1465                 MEM_freeN(e);
1466         }
1467
1468         for (e = prefetch_done.first; e; e = e->next) {
1469                 if (e->ibuf) {
1470                         IMB_cache_limiter_unref(e->ibuf);
1471                 }
1472                 BLI_remlink(&prefetch_done, e);
1473                 MEM_freeN(e);
1474         }
1475
1476         BLI_freelistN(&running_threads);
1477
1478         /* deinit malloc mutex */
1479         BLI_end_threads(0);
1480 }
1481
1482 void give_ibuf_prefetch_request(int rectx, int recty, int cfra, int chanshown)
1483 {
1484         PrefetchQueueElem * e;
1485         if (seq_thread_shutdown) {
1486                 return;
1487         }
1488
1489         e = MEM_callocN(sizeof(PrefetchQueueElem), "prefetch_queue_elem");
1490         e->rectx = rectx;
1491         e->recty = recty;
1492         e->cfra = cfra;
1493         e->chanshown = chanshown;
1494         e->monoton_cfra = monoton_cfra++;
1495
1496         pthread_mutex_lock(&queue_lock);
1497         BLI_addtail(&prefetch_wait, e);
1498         pthread_mutex_unlock(&queue_lock);
1499         
1500         pthread_mutex_lock(&wakeup_lock);
1501         pthread_cond_signal(&wakeup_cond);
1502         pthread_mutex_unlock(&wakeup_lock);
1503 }
1504
1505 void seq_wait_for_prefetch_ready()
1506 {
1507         PrefetchThread *tslot;
1508
1509         if (seq_thread_shutdown) {
1510                 return;
1511         }
1512
1513         fprintf(stderr, "SEQ-THREAD: rendering prefetch frames...\n");
1514
1515         pthread_mutex_lock(&prefetch_ready_lock);
1516
1517         for(;;) {
1518                 for(tslot = running_threads.first; tslot; tslot= tslot->next) {
1519                         if (tslot->running) {
1520                                 break;
1521                         }
1522                 }
1523                 if (!tslot) {
1524                         break;
1525                 }
1526                 pthread_cond_wait(&prefetch_ready_cond, &prefetch_ready_lock);
1527         }
1528
1529         pthread_mutex_unlock(&prefetch_ready_lock);
1530
1531         fprintf(stderr, "SEQ-THREAD: prefetch done\n");
1532 }
1533
1534 ImBuf * give_ibuf_seq_threaded(int rectx, int recty, int cfra, int chanshown)
1535 {
1536         PrefetchQueueElem * e = 0;
1537         int found_something = FALSE;
1538
1539         if (seq_thread_shutdown) {
1540                 return give_ibuf_seq(rectx, recty, cfra, chanshown);
1541         }
1542
1543         while (!e) {
1544                 int success = FALSE;
1545                 pthread_mutex_lock(&queue_lock);
1546
1547                 for (e = prefetch_done.first; e; e = e->next) {
1548                         if (cfra == e->cfra &&
1549                             chanshown == e->chanshown &&
1550                             rectx == e->rectx && 
1551                             recty == e->recty) {
1552                                 success = TRUE;
1553                                 found_something = TRUE;
1554                                 break;
1555                         }
1556                 }
1557
1558                 if (!e) {
1559                         for (e = prefetch_wait.first; e; e = e->next) {
1560                                 if (cfra == e->cfra &&
1561                                     chanshown == e->chanshown &&
1562                                     rectx == e->rectx && 
1563                                     recty == e->recty) {
1564                                         found_something = TRUE;
1565                                         break;
1566                                 }
1567                         }
1568                 }
1569
1570                 if (!e) {
1571                         PrefetchThread *tslot;
1572
1573                         for(tslot = running_threads.first; 
1574                             tslot; tslot= tslot->next) {
1575                                 if (tslot->current &&
1576                                     cfra == tslot->current->cfra &&
1577                                     chanshown == tslot->current->chanshown &&
1578                                     rectx == tslot->current->rectx && 
1579                                     recty == tslot->current->recty) {
1580                                         found_something = TRUE;
1581                                         break;
1582                                 }
1583                         }
1584                 }
1585
1586                 /* e->ibuf is unrefed by render thread on next round. */
1587
1588                 if (e) {
1589                         seq_last_given_monoton_cfra = e->monoton_cfra;
1590                 }
1591
1592                 pthread_mutex_unlock(&queue_lock);
1593
1594                 if (!success) {
1595                         e = NULL;
1596
1597                         if (!found_something) {
1598                                 fprintf(stderr, 
1599                                         "SEQ-THREAD: Requested frame "
1600                                         "not in queue ???\n");
1601                                 break;
1602                         }
1603                         pthread_mutex_lock(&frame_done_lock);
1604                         pthread_cond_wait(&frame_done_cond, &frame_done_lock);
1605                         pthread_mutex_unlock(&frame_done_lock);
1606                 }
1607         }
1608         
1609         return e ? e->ibuf : 0;
1610 }
1611
1612 /* Functions to free imbuf and anim data on changes */
1613
1614 static void free_imbuf_strip_elem(TStripElem *se)
1615 {
1616         if (se->ibuf) {
1617                 if (se->ok != STRIPELEM_META && se->ibuf != 0)
1618                         IMB_freeImBuf(se->ibuf);
1619                 se->ibuf= 0;
1620                 se->ok= STRIPELEM_OK;
1621                 se->se1= se->se2= se->se3= 0;
1622         }
1623 }
1624
1625 static void free_anim_seq(Sequence *seq)
1626 {
1627         if(seq->anim) {
1628                 IMB_free_anim(seq->anim);
1629                 seq->anim = 0;
1630         }
1631 }
1632
1633 void free_imbuf_seq_except(int cfra)
1634 {
1635         Editing *ed= G.scene->ed;
1636         Sequence *seq;
1637         TStripElem *se;
1638         int a;
1639
1640         if(ed==0) return;
1641
1642         WHILE_SEQ(&ed->seqbase) {
1643                 if(seq->strip) {
1644                         TStripElem * curelem = give_tstripelem(seq, cfra);
1645
1646                         for(a=0, se= seq->strip->tstripdata; a<seq->len; a++, se++)
1647                                 if(se != curelem)
1648                                         free_imbuf_strip_elem(se);
1649
1650                         if(seq->type==SEQ_MOVIE)
1651                                 if(seq->startdisp > cfra || seq->enddisp < cfra)
1652                                         free_anim_seq(seq);
1653                 }
1654         }
1655         END_SEQ
1656 }
1657
1658 void free_imbuf_seq()
1659 {
1660         Editing *ed= G.scene->ed;
1661         Sequence *seq;
1662         TStripElem *se;
1663         int a;
1664
1665         if(ed==0) return;
1666
1667         WHILE_SEQ(&ed->seqbase) {
1668                 if(seq->strip) {
1669                         if (seq->strip->tstripdata) {
1670                                 for(a=0, se= seq->strip->tstripdata; a<seq->len; a++, se++)
1671                                         free_imbuf_strip_elem(se);
1672                         }
1673
1674                         if(seq->type==SEQ_MOVIE)
1675                                 free_anim_seq(seq);
1676                         if(seq->type==SEQ_SPEED) {
1677                                 sequence_effect_speed_rebuild_map(seq, 1);
1678                         }
1679                 }
1680         }
1681         END_SEQ
1682 }
1683
1684 void free_imbuf_seq_with_ipo(struct Ipo *ipo)
1685 {
1686         /* force update of all sequences with this ipo, on ipo changes */
1687         Editing *ed= G.scene->ed;
1688         Sequence *seq;
1689
1690         if(ed==0) return;
1691
1692         WHILE_SEQ(&ed->seqbase) {
1693                 if(seq->ipo == ipo) {
1694                         update_changed_seq_and_deps(seq, 0, 1);
1695                         if(seq->type == SEQ_SPEED) {
1696                                 sequence_effect_speed_rebuild_map(seq, 1);
1697                         }
1698                 }
1699         }
1700         END_SEQ
1701 }
1702
1703 static int update_changed_seq_recurs(Sequence *seq, Sequence *changed_seq, int len_change, int ibuf_change)
1704 {
1705         Sequence *subseq;
1706         int a, free_imbuf = 0;
1707         TStripElem *se;
1708
1709         /* recurs downwards to see if this seq depends on the changed seq */
1710
1711         if(seq == NULL)
1712                 return 0;
1713
1714         if(seq == changed_seq)
1715                 free_imbuf = 1;
1716         
1717         for(subseq=seq->seqbase.first; subseq; subseq=subseq->next)
1718                 if(update_changed_seq_recurs(subseq, changed_seq, len_change, ibuf_change))
1719                         free_imbuf = TRUE;
1720         
1721         if(seq->seq1)
1722                 if(update_changed_seq_recurs(seq->seq1, changed_seq, len_change, ibuf_change))
1723                         free_imbuf = TRUE;
1724         if(seq->seq2 && (seq->seq2 != seq->seq1))
1725                 if(update_changed_seq_recurs(seq->seq2, changed_seq, len_change, ibuf_change))
1726                         free_imbuf = TRUE;
1727         if(seq->seq3 && (seq->seq3 != seq->seq1) && (seq->seq3 != seq->seq2))
1728                 if(update_changed_seq_recurs(seq->seq3, changed_seq, len_change, ibuf_change))
1729                         free_imbuf = TRUE;
1730         
1731         if(free_imbuf) {
1732                 if(ibuf_change) {
1733                         se= seq->strip->tstripdata;
1734                         if (se) {
1735                                 for(a=0; a<seq->len; a++, se++)
1736                                         free_imbuf_strip_elem(se);
1737                         }
1738                 
1739                         if(seq->type == SEQ_MOVIE)
1740                                 free_anim_seq(seq);
1741                         if(seq->type == SEQ_SPEED) {
1742                                 sequence_effect_speed_rebuild_map(seq, 1);
1743                         }
1744                 }
1745
1746                 if(len_change)
1747                         calc_sequence(seq);
1748         }
1749         
1750         return free_imbuf;
1751 }
1752
1753 void update_changed_seq_and_deps(Sequence *changed_seq, int len_change, int ibuf_change)
1754 {
1755         Editing *ed= G.scene->ed;
1756         Sequence *seq;
1757
1758         if (!ed) return;
1759
1760         for (seq=ed->seqbase.first; seq; seq=seq->next)
1761                 update_changed_seq_recurs(seq, changed_seq, len_change, ibuf_change);
1762 }
1763
1764 /* bad levell call... */
1765 void do_render_seq(RenderResult *rr, int cfra)
1766 {
1767         ImBuf *ibuf;
1768
1769         G.f |= G_PLAYANIM;      /* waitcursor patch */
1770
1771         ibuf= give_ibuf_seq(rr->rectx, rr->recty, cfra, 0);
1772         
1773         if(ibuf) {
1774                 if(ibuf->rect_float) {
1775                         if (!rr->rectf)
1776                                 rr->rectf= MEM_mallocN(4*sizeof(float)*rr->rectx*rr->recty, "render_seq rectf");
1777                         
1778                         memcpy(rr->rectf, ibuf->rect_float, 4*sizeof(float)*rr->rectx*rr->recty);
1779                         
1780                         /* TSK! Since sequence render doesn't free the *rr render result, the old rect32
1781                            can hang around when sequence render has rendered a 32 bits one before */
1782                         if(rr->rect32) {
1783                                 MEM_freeN(rr->rect32);
1784                                 rr->rect32= NULL;
1785                         }
1786                 }
1787                 else if(ibuf->rect) {
1788                         if (!rr->rect32)
1789                                 rr->rect32= MEM_mallocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
1790
1791                         memcpy(rr->rect32, ibuf->rect, 4*rr->rectx*rr->recty);
1792
1793                         /* if (ibuf->zbuf) { */
1794                         /*      if (R.rectz) freeN(R.rectz); */
1795                         /*      R.rectz = BLI_dupallocN(ibuf->zbuf); */
1796                         /* } */
1797                 }
1798                 
1799                 /* Let the cache limitor take care of this (schlaile) */
1800                 /* While render let's keep all memory available for render 
1801                    (ton)
1802                    At least if free memory is tight...
1803                    This can make a big difference in encoding speed
1804                    (it is around 4 times(!) faster, if we do not waste time
1805                    on freeing _all_ buffers every time on long timelines...)
1806                    (schlaile)
1807                 */
1808                 {
1809                         extern int mem_in_use;
1810                         extern int mmap_in_use;
1811
1812                         int max = MEM_CacheLimiter_get_maximum();
1813                         if (max != 0 && mem_in_use + mmap_in_use > max) {
1814                                 fprintf(stderr, "mem_in_use = %d, max = %d\n",
1815                                         mem_in_use + mmap_in_use, max);
1816                                 fprintf(stderr, "Cleaning up, please wait...\n"
1817                                         "If this happens very often,\n"
1818                                         "consider "
1819                                         "raising the memcache limit in the "
1820                                         "user preferences.\n");
1821                                 free_imbuf_seq();
1822                         }
1823                 }
1824         }
1825         else {
1826                 /* render result is delivered empty in most cases, nevertheless we handle all cases */
1827                 if (rr->rectf)
1828                         memset(rr->rectf, 0, 4*sizeof(float)*rr->rectx*rr->recty);
1829                 else if (rr->rect32)
1830                         memset(rr->rect32, 0, 4*rr->rectx*rr->recty);
1831                 else
1832                         rr->rect32= MEM_callocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
1833         }
1834         
1835         G.f &= ~G_PLAYANIM;
1836
1837 }