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