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