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