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