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