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