svn merge ^/trunk/blender -r48333:48370
[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                 char dir[FILE_MAX];
1224                 BLI_strncpy(dir, seq->strip->proxy->dir, sizeof(dir));
1225                 BLI_path_abs(dir, G.main->name);
1226
1227                 IMB_anim_set_index_dir(seq->anim, dir);
1228         }
1229 }
1230
1231
1232 static int seq_proxy_get_fname(Sequence *seq, int cfra, int render_size, char *name)
1233 {
1234         int frameno;
1235         char dir[PROXY_MAXFILE];
1236
1237         if (!seq->strip->proxy) {
1238                 return FALSE;
1239         }
1240
1241         /* MOVIE tracks (only exception: custom files) are now handled 
1242          * internally by ImBuf module for various reasons: proper time code
1243          * support, quicker index build, using one file instead
1244          * of a full directory of jpeg files, etc. Trying to support old
1245          * and new method at once could lead to funny effects, if people
1246          * have both, a directory full of jpeg files and proxy avis, so
1247          * sorry folks, please rebuild your proxies... */
1248
1249         if (seq->flag & (SEQ_USE_PROXY_CUSTOM_DIR | SEQ_USE_PROXY_CUSTOM_FILE)) {
1250                 BLI_strncpy(dir, seq->strip->proxy->dir, sizeof(dir));
1251         }
1252         else if (seq->type == SEQ_TYPE_IMAGE) {
1253                 BLI_snprintf(dir, PROXY_MAXFILE, "%s/BL_proxy", seq->strip->dir);
1254         }
1255         else {
1256                 return FALSE;
1257         }
1258
1259         if (seq->flag & SEQ_USE_PROXY_CUSTOM_FILE) {
1260                 BLI_join_dirfile(name, PROXY_MAXFILE,
1261                                  dir, seq->strip->proxy->file);
1262                 BLI_path_abs(name, G.main->name);
1263
1264                 return TRUE;
1265         }
1266
1267         /* generate a separate proxy directory for each preview size */
1268
1269         if (seq->type == SEQ_TYPE_IMAGE) {
1270                 BLI_snprintf(name, PROXY_MAXFILE, "%s/images/%d/%s_proxy", dir,
1271                              render_size,
1272                              give_stripelem(seq, cfra)->name);
1273                 frameno = 1;
1274         }
1275         else {
1276                 frameno = (int)give_stripelem_index(seq, cfra) + seq->anim_startofs;
1277                 BLI_snprintf(name, PROXY_MAXFILE, "%s/proxy_misc/%d/####", dir, 
1278                              render_size);
1279         }
1280
1281         BLI_path_abs(name, G.main->name);
1282         BLI_path_frame(name, frameno, 0);
1283
1284         strcat(name, ".jpg");
1285
1286         return TRUE;
1287 }
1288
1289 static ImBuf *seq_proxy_fetch(SeqRenderData context, Sequence *seq, int cfra)
1290 {
1291         char name[PROXY_MAXFILE];
1292         IMB_Proxy_Size psize = seq_rendersize_to_proxysize(context.preview_render_size);
1293         int size_flags;
1294         int render_size = context.preview_render_size;
1295
1296         /* dirty hack to distinguish 100% render size from PROXY_100 */
1297         if (render_size == 99) {
1298                 render_size = 100;
1299         }
1300
1301         if (!(seq->flag & SEQ_USE_PROXY)) {
1302                 return NULL;
1303         }
1304
1305         size_flags = seq->strip->proxy->build_size_flags;
1306
1307         /* only use proxies, if they are enabled (even if present!) */
1308         if (psize == IMB_PROXY_NONE || ((size_flags & psize) != psize)) {
1309                 return NULL;
1310         }
1311
1312         if (seq->flag & SEQ_USE_PROXY_CUSTOM_FILE) {
1313                 int frameno = (int)give_stripelem_index(seq, cfra) + seq->anim_startofs;
1314                 if (seq->strip->proxy->anim == NULL) {
1315                         if (seq_proxy_get_fname(seq, cfra, render_size, name) == 0) {
1316                                 return NULL;
1317                         }
1318  
1319                         seq->strip->proxy->anim = openanim(name, IB_rect, 0);
1320                 }
1321                 if (seq->strip->proxy->anim == NULL) {
1322                         return NULL;
1323                 }
1324  
1325                 seq_open_anim_file(seq);
1326
1327                 frameno = IMB_anim_index_get_frame_index(seq->anim, seq->strip->proxy->tc,
1328                                                          frameno);
1329
1330                 return IMB_anim_absolute(seq->strip->proxy->anim, frameno,
1331                                          IMB_TC_NONE, IMB_PROXY_NONE);
1332         }
1333  
1334         if (seq_proxy_get_fname(seq, cfra, render_size, name) == 0) {
1335                 return NULL;
1336         }
1337
1338         if (BLI_exists(name)) {
1339                 return IMB_loadiffname(name, IB_rect);
1340         }
1341         else {
1342                 return NULL;
1343         }
1344 }
1345
1346 static void seq_proxy_build_frame(SeqRenderData context,
1347                                   Sequence *seq, int cfra,
1348                                   int proxy_render_size)
1349 {
1350         char name[PROXY_MAXFILE];
1351         int quality;
1352         int rectx, recty;
1353         int ok;
1354         ImBuf *ibuf;
1355
1356         if (!seq_proxy_get_fname(seq, cfra, proxy_render_size, name)) {
1357                 return;
1358         }
1359
1360         ibuf = seq_render_strip(context, seq, cfra);
1361
1362         rectx = (proxy_render_size * context.scene->r.xsch) / 100;
1363         recty = (proxy_render_size * context.scene->r.ysch) / 100;
1364
1365         if (ibuf->x != rectx || ibuf->y != recty) {
1366                 IMB_scalefastImBuf(ibuf, (short)rectx, (short)recty);
1367         }
1368
1369         /* depth = 32 is intentionally left in, otherwise ALPHA channels
1370          * won't work... */
1371         quality = seq->strip->proxy->quality;
1372         ibuf->ftype = JPG | quality;
1373
1374         /* unsupported feature only confuses other s/w */
1375         if (ibuf->planes == 32)
1376                 ibuf->planes = 24;
1377
1378         BLI_make_existing_file(name);
1379         
1380         ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
1381         if (ok == 0) {
1382                 perror(name);
1383         }
1384
1385         IMB_freeImBuf(ibuf);
1386 }
1387
1388 struct SeqIndexBuildContext *seq_proxy_rebuild_context(Main *bmain, Scene *scene, Sequence *seq)
1389 {
1390         SeqIndexBuildContext *context;
1391         Sequence *nseq;
1392
1393         if (!seq->strip || !seq->strip->proxy) {
1394                 return NULL;
1395         }
1396
1397         if (!(seq->flag & SEQ_USE_PROXY)) {
1398                 return NULL;
1399         }
1400
1401         context = MEM_callocN(sizeof(SeqIndexBuildContext), "seq proxy rebuild context");
1402
1403         nseq = seq_dupli_recursive(scene, scene, seq, 0);
1404
1405         context->tc_flags   = nseq->strip->proxy->build_tc_flags;
1406         context->size_flags = nseq->strip->proxy->build_size_flags;
1407         context->quality    = nseq->strip->proxy->quality;
1408
1409         context->bmain = bmain;
1410         context->scene = scene;
1411         context->orig_seq = seq;
1412         context->seq = nseq;
1413
1414         if (nseq->type == SEQ_TYPE_MOVIE) {
1415                 seq_open_anim_file(nseq);
1416
1417                 if (nseq->anim) {
1418                         context->index_context = IMB_anim_index_rebuild_context(nseq->anim,
1419                                 context->tc_flags, context->size_flags, context->quality);
1420                 }
1421         }
1422
1423         return context;
1424 }
1425
1426 void seq_proxy_rebuild(SeqIndexBuildContext *context, short *stop, short *do_update, float *progress)
1427 {
1428         SeqRenderData render_context;
1429         Sequence *seq = context->seq;
1430         Scene *scene = context->scene;
1431         int cfra;
1432
1433         if (seq->type == SEQ_TYPE_MOVIE) {
1434                 if (context->index_context) {
1435                         IMB_anim_index_rebuild(context->index_context, stop, do_update, progress);
1436                 }
1437
1438                 return;
1439         }
1440
1441         if (!(seq->flag & SEQ_USE_PROXY)) {
1442                 return;
1443         }
1444
1445         /* that's why it is called custom... */
1446         if (seq->flag & SEQ_USE_PROXY_CUSTOM_FILE) {
1447                 return;
1448         }
1449
1450         /* fail safe code */
1451
1452         render_context = seq_new_render_data(
1453                 context->bmain, context->scene,
1454                 (scene->r.size * (float)scene->r.xsch) / 100.0f + 0.5f,
1455                 (scene->r.size * (float)scene->r.ysch) / 100.0f + 0.5f,
1456                 100);
1457
1458         for (cfra = seq->startdisp + seq->startstill; 
1459              cfra < seq->enddisp - seq->endstill; cfra++)
1460         {
1461                 if (context->size_flags & IMB_PROXY_25) {
1462                         seq_proxy_build_frame(render_context, seq, cfra, 25);
1463                 }
1464                 if (context->size_flags & IMB_PROXY_50) {
1465                         seq_proxy_build_frame(render_context, seq, cfra, 50);
1466                 }
1467                 if (context->size_flags & IMB_PROXY_75) {
1468                         seq_proxy_build_frame(render_context, seq, cfra, 75);
1469                 }
1470                 if (context->size_flags & IMB_PROXY_100) {
1471                         seq_proxy_build_frame(render_context, seq, cfra, 100);
1472                 }
1473
1474                 *progress = (float)cfra / (seq->enddisp - seq->endstill -
1475                                            seq->startdisp + seq->startstill);
1476                 *do_update = TRUE;
1477
1478                 if (*stop || G.afbreek)
1479                         break;
1480         }
1481 }
1482
1483 void seq_proxy_rebuild_finish(SeqIndexBuildContext *context, short stop)
1484 {
1485         if (context->index_context) {
1486                 IMB_close_anim_proxies(context->seq->anim);
1487                 IMB_close_anim_proxies(context->orig_seq->anim);
1488                 IMB_anim_index_rebuild_finish(context->index_context, stop);
1489         }
1490
1491         seq_free_sequence_recurse(context->scene, context->seq);
1492
1493         MEM_freeN(context);
1494 }
1495
1496 /* **********************************************************************
1497  * color balance
1498  * ********************************************************************** */
1499
1500 static StripColorBalance calc_cb(StripColorBalance *cb_)
1501 {
1502         StripColorBalance cb = *cb_;
1503         int c;
1504
1505         for (c = 0; c < 3; c++) {
1506                 cb.lift[c] = 2.0f - cb.lift[c];
1507         }
1508
1509         if (cb.flag & SEQ_COLOR_BALANCE_INVERSE_LIFT) {
1510                 for (c = 0; c < 3; c++) {
1511                         /* tweak to give more subtle results
1512                          * values above 1.0 are scaled */
1513                         if (cb.lift[c] > 1.0f)
1514                                 cb.lift[c] = pow(cb.lift[c] - 1.0f, 2.0) + 1.0;
1515
1516                         cb.lift[c] = 2.0f - cb.lift[c];
1517                 }
1518         }
1519
1520         if (cb.flag & SEQ_COLOR_BALANCE_INVERSE_GAIN) {
1521                 for (c = 0; c < 3; c++) {
1522                         if (cb.gain[c] != 0.0f) {
1523                                 cb.gain[c] = 1.0f / cb.gain[c];
1524                         }
1525                         else {
1526                                 cb.gain[c] = 1000000; /* should be enough :) */
1527                         }
1528                 }
1529         }
1530
1531         if (!(cb.flag & SEQ_COLOR_BALANCE_INVERSE_GAMMA)) {
1532                 for (c = 0; c < 3; c++) {
1533                         if (cb.gamma[c] != 0.0f) {
1534                                 cb.gamma[c] = 1.0f / cb.gamma[c];
1535                         }
1536                         else {
1537                                 cb.gamma[c] = 1000000; /* should be enough :) */
1538                         }
1539                 }
1540         }
1541
1542         return cb;
1543 }
1544
1545 /* note: lift is actually 2-lift */
1546 MINLINE float color_balance_fl(float in, const float lift, const float gain, const float gamma, const float mul)
1547 {
1548         float x = (((in - 1.0f) * lift) + 1.0f) * gain;
1549
1550         /* prevent NaN */
1551         if (x < 0.f) x = 0.f;
1552
1553         return powf(x, gamma) * mul;
1554 }
1555
1556 static void make_cb_table_byte(float lift, float gain, float gamma,
1557                                unsigned char *table, float mul)
1558 {
1559         int y;
1560
1561         for (y = 0; y < 256; y++) {
1562                 float v = color_balance_fl((float)y * (1.0f / 255.0f), lift, gain, gamma, mul);
1563                 table[y] = FTOCHAR(v);
1564         }
1565 }
1566
1567 static void make_cb_table_float(float lift, float gain, float gamma,
1568                                 float *table, float mul)
1569 {
1570         int y;
1571
1572         for (y = 0; y < 256; y++) {
1573                 float v = color_balance_fl((float)y * (1.0f / 255.0f), lift, gain, gamma, mul);
1574                 table[y] = v;
1575         }
1576 }
1577
1578 static void color_balance_byte_byte(Sequence *seq, ImBuf *ibuf, float mul)
1579 {
1580         unsigned char cb_tab[3][256];
1581         int c;
1582         unsigned char *p = (unsigned char *) ibuf->rect;
1583         unsigned char *e = p + ibuf->x * 4 * ibuf->y;
1584
1585         StripColorBalance cb = calc_cb(seq->strip->color_balance);
1586
1587         for (c = 0; c < 3; c++) {
1588                 make_cb_table_byte(cb.lift[c], cb.gain[c], cb.gamma[c],
1589                                    cb_tab[c], mul);
1590         }
1591
1592         while (p < e) {
1593                 p[0] = cb_tab[0][p[0]];
1594                 p[1] = cb_tab[1][p[1]];
1595                 p[2] = cb_tab[2][p[2]];
1596                 
1597                 p += 4;
1598         }
1599 }
1600
1601 static void color_balance_byte_float(Sequence *seq, ImBuf *ibuf, float mul)
1602 {
1603         float cb_tab[4][256];
1604         int c, i;
1605         unsigned char *p = (unsigned char *) ibuf->rect;
1606         unsigned char *e = p + ibuf->x * 4 * ibuf->y;
1607         float *o;
1608         StripColorBalance cb;
1609
1610         imb_addrectfloatImBuf(ibuf);
1611
1612         o = ibuf->rect_float;
1613
1614         cb = calc_cb(seq->strip->color_balance);
1615
1616         for (c = 0; c < 3; c++) {
1617                 make_cb_table_float(cb.lift[c], cb.gain[c], cb.gamma[c], cb_tab[c], mul);
1618         }
1619
1620         for (i = 0; i < 256; i++) {
1621                 cb_tab[3][i] = ((float)i) * (1.0f / 255.0f);
1622         }
1623
1624         while (p < e) {
1625                 o[0] = cb_tab[0][p[0]];
1626                 o[1] = cb_tab[1][p[1]];
1627                 o[2] = cb_tab[2][p[2]];
1628                 o[3] = cb_tab[3][p[3]];
1629
1630                 p += 4; o += 4;
1631         }
1632 }
1633
1634 static void color_balance_float_float(Sequence *seq, ImBuf *ibuf, float mul)
1635 {
1636         float *p = ibuf->rect_float;
1637         float *e = ibuf->rect_float + ibuf->x * 4 * ibuf->y;
1638         StripColorBalance cb = calc_cb(seq->strip->color_balance);
1639
1640         while (p < e) {
1641                 int c;
1642                 for (c = 0; c < 3; c++) {
1643                         p[c] = color_balance_fl(p[c], cb.lift[c], cb.gain[c], cb.gamma[c], mul);
1644                 }
1645                 p += 4;
1646         }
1647 }
1648
1649 static void color_balance(Sequence *seq, ImBuf *ibuf, float mul)
1650 {
1651         if (ibuf->rect_float) {
1652                 color_balance_float_float(seq, ibuf, mul);
1653         }
1654         else if (seq->flag & SEQ_MAKE_FLOAT) {
1655                 color_balance_byte_float(seq, ibuf, mul);
1656         }
1657         else {
1658                 color_balance_byte_byte(seq, ibuf, mul);
1659         }
1660 }
1661
1662 /*
1663  *  input preprocessing for SEQ_TYPE_IMAGE, SEQ_TYPE_MOVIE, SEQ_TYPE_MOVIECLIP and SEQ_TYPE_SCENE
1664  *
1665  *  Do all the things you can't really do afterwards using sequence effects
1666  *  (read: before rescaling to render resolution has been done)
1667  *
1668  *  Order is important!
1669  *
1670  *  - Deinterlace
1671  *  - Crop and transform in image source coordinate space
1672  *  - Flip X + Flip Y (could be done afterwards, backward compatibility)
1673  *  - Promote image to float data (affects pipeline operations afterwards)
1674  *  - Color balance (is most efficient in the byte -> float
1675  *    (future: half -> float should also work fine!)
1676  *    case, if done on load, since we can use lookup tables)
1677  *  - Premultiply
1678  */
1679
1680 int input_have_to_preprocess(
1681         SeqRenderData UNUSED(context), Sequence *seq, float UNUSED(cfra))
1682 {
1683         float mul;
1684
1685         if (seq->flag & (SEQ_FILTERY | SEQ_USE_CROP | SEQ_USE_TRANSFORM | SEQ_FLIPX |
1686                          SEQ_FLIPY | SEQ_USE_COLOR_BALANCE | SEQ_MAKE_PREMUL))
1687         {
1688                 return TRUE;
1689         }
1690
1691         mul = seq->mul;
1692
1693         if (seq->blend_mode == SEQ_BLEND_REPLACE) {
1694                 mul *= seq->blend_opacity / 100.0f;
1695         }
1696
1697         if (mul != 1.0f) {
1698                 return TRUE;
1699         }
1700
1701         if (seq->sat != 1.0f) {
1702                 return TRUE;
1703         }
1704                 
1705         return FALSE;
1706 }
1707
1708 static ImBuf *input_preprocess(
1709         SeqRenderData context, Sequence *seq, float UNUSED(cfra), ImBuf *ibuf,
1710         int is_proxy_image, int is_preprocessed)
1711 {
1712         float mul;
1713
1714         ibuf = IMB_makeSingleUser(ibuf);
1715
1716         if ((seq->flag & SEQ_FILTERY) &&
1717             !ELEM(seq->type, SEQ_TYPE_MOVIE, SEQ_TYPE_MOVIECLIP))
1718         {
1719                 IMB_filtery(ibuf);
1720         }
1721
1722         if (seq->flag & (SEQ_USE_CROP | SEQ_USE_TRANSFORM)) {
1723                 StripCrop c = {0};
1724                 StripTransform t = {0};
1725                 int sx, sy, dx, dy;
1726                 double xscale = 1.0;
1727                 double yscale = 1.0;
1728
1729                 if (is_proxy_image) {
1730                         double f = seq_rendersize_to_scale_factor(
1731                                 context.preview_render_size);
1732
1733                         if (f != 1.0) {
1734                                 IMB_scalefastImBuf(
1735                                         ibuf, ibuf->x / f, ibuf->y / f);
1736                         }
1737                 }
1738
1739                 if (seq->flag & SEQ_USE_CROP && seq->strip->crop) {
1740                         c = *seq->strip->crop;
1741                 }
1742                 if (seq->flag & SEQ_USE_TRANSFORM && seq->strip->transform) {
1743                         t = *seq->strip->transform;
1744                 }
1745
1746                 xscale = context.scene->r.xsch ? 
1747                          ((double) context.rectx /
1748                           (double) context.scene->r.xsch) : 1.0;
1749                 yscale = context.scene->r.ysch ? 
1750                          ((double) context.recty /
1751                           (double) context.scene->r.ysch) : 1.0;
1752
1753                 c.left *= xscale; c.right *= xscale;
1754                 c.top *= yscale; c.bottom *= yscale;
1755
1756                 t.xofs *= xscale; t.yofs *= yscale;
1757
1758                 sx = ibuf->x - c.left - c.right;
1759                 sy = ibuf->y - c.top - c.bottom;
1760                 dx = sx;
1761                 dy = sy;
1762
1763                 if (seq->flag & SEQ_USE_TRANSFORM) {
1764                         if (is_preprocessed) {
1765                                 dx = context.rectx;
1766                                 dy = context.recty;
1767                         }
1768                         else {
1769                                 dx = context.scene->r.xsch;
1770                                 dy = context.scene->r.ysch;
1771                         }
1772                 }
1773
1774                 if (c.top  + c.bottom >= ibuf->y ||
1775                     c.left + c.right  >= ibuf->x ||
1776                     t.xofs >= dx || t.yofs >= dy)
1777                 {
1778                         make_black_ibuf(ibuf);
1779                 }
1780                 else {
1781                         ImBuf *i = IMB_allocImBuf(
1782                                 dx, dy, 32,
1783                                 ibuf->rect_float ? IB_rectfloat : IB_rect);
1784
1785                         IMB_rectcpy(i, ibuf, 
1786                                     t.xofs, t.yofs, c.left, c.bottom, sx, sy);
1787                         
1788                         IMB_freeImBuf(ibuf);
1789
1790                         ibuf = i;
1791                 }
1792         } 
1793
1794         if (seq->flag & SEQ_FLIPX) {
1795                 IMB_flipx(ibuf);
1796         }
1797         
1798         if (seq->flag & SEQ_FLIPY) {
1799                 IMB_flipy(ibuf);
1800         }
1801
1802         if (seq->sat != 1.0f) {
1803                 IMB_saturation(ibuf, seq->sat);
1804         }
1805
1806         mul = seq->mul;
1807
1808         if (seq->blend_mode == SEQ_BLEND_REPLACE) {
1809                 mul *= seq->blend_opacity / 100.0f;
1810         }
1811
1812         if (seq->flag & SEQ_USE_COLOR_BALANCE && seq->strip->color_balance) {
1813                 color_balance(seq, ibuf, mul);
1814                 mul = 1.0;
1815         }
1816
1817         if (seq->flag & SEQ_MAKE_FLOAT) {
1818                 if (!ibuf->rect_float)
1819                         IMB_float_from_rect_simple(ibuf);
1820
1821                 if (ibuf->rect) {
1822                         imb_freerectImBuf(ibuf);
1823                 }
1824         }
1825
1826         if (mul != 1.0f) {
1827                 multibuf(ibuf, mul);
1828         }
1829
1830         if (seq->flag & SEQ_MAKE_PREMUL) {
1831                 if (ibuf->planes == 32 && ibuf->zbuf == NULL) {
1832                         IMB_premultiply_alpha(ibuf);
1833                 }
1834         }
1835
1836
1837         if (ibuf->x != context.rectx || ibuf->y != context.recty) {
1838                 if (context.scene->r.mode & R_OSA) {
1839                         IMB_scaleImBuf(ibuf, (short)context.rectx, (short)context.recty);
1840                 }
1841                 else {
1842                         IMB_scalefastImBuf(ibuf, (short)context.rectx, (short)context.recty);
1843                 }
1844         }
1845         return ibuf;
1846 }
1847
1848 static ImBuf *copy_from_ibuf_still(SeqRenderData context, Sequence *seq,
1849                                    float nr)
1850 {
1851         ImBuf *rval = NULL;
1852         ImBuf *ibuf = NULL;
1853
1854         if (nr == 0) {
1855                 ibuf = seq_stripelem_cache_get(
1856                         context, seq, seq->start,
1857                         SEQ_STRIPELEM_IBUF_STARTSTILL);
1858         }
1859         else if (nr == seq->len - 1) {
1860                 ibuf = seq_stripelem_cache_get(
1861                         context, seq, seq->start,
1862                         SEQ_STRIPELEM_IBUF_ENDSTILL);
1863         }
1864
1865         if (ibuf) {
1866                 rval = IMB_dupImBuf(ibuf);
1867                 IMB_freeImBuf(ibuf);
1868         }
1869
1870         return rval;
1871 }
1872
1873 static void copy_to_ibuf_still(SeqRenderData context, Sequence *seq, float nr,
1874                                ImBuf *ibuf)
1875 {
1876         if (nr == 0 || nr == seq->len - 1) {
1877                 /* we have to store a copy, since the passed ibuf
1878                  * could be preprocessed afterwards (thereby silently
1879                  * changing the cached image... */
1880                 ibuf = IMB_dupImBuf(ibuf);
1881
1882                 if (nr == 0) {
1883                         seq_stripelem_cache_put(
1884                                 context, seq, seq->start,
1885                                 SEQ_STRIPELEM_IBUF_STARTSTILL, ibuf);
1886                 } 
1887
1888                 if (nr == seq->len - 1) {
1889                         seq_stripelem_cache_put(
1890                                 context, seq, seq->start,
1891                                 SEQ_STRIPELEM_IBUF_ENDSTILL, ibuf);
1892                 }
1893
1894                 IMB_freeImBuf(ibuf);
1895         }
1896 }
1897
1898 /* **********************************************************************
1899  * strip rendering functions
1900  * ********************************************************************** */
1901
1902 static ImBuf *seq_render_strip_stack(
1903         SeqRenderData context, ListBase *seqbasep, float cfra, int chanshown);
1904
1905 static ImBuf *seq_render_strip(
1906         SeqRenderData context, Sequence *seq, float cfra);
1907
1908
1909 static ImBuf *seq_render_effect_strip_impl(
1910         SeqRenderData context, Sequence *seq, float cfra)
1911 {
1912         float fac, facf;
1913         int early_out;
1914         int i;
1915         struct SeqEffectHandle sh = get_sequence_effect(seq);
1916         FCurve *fcu = NULL;
1917         ImBuf *ibuf[3];
1918         Sequence *input[3];
1919         ImBuf *out = NULL;
1920
1921         ibuf[0] = ibuf[1] = ibuf[2] = NULL;
1922
1923         input[0] = seq->seq1; input[1] = seq->seq2; input[2] = seq->seq3;
1924
1925         if (!sh.execute) { /* effect not supported in this version... */
1926                 out = IMB_allocImBuf(context.rectx, context.recty, 32, IB_rect);
1927                 return out;
1928         }
1929
1930         if (seq->flag & SEQ_USE_EFFECT_DEFAULT_FADE) {
1931                 sh.get_default_fac(seq, cfra, &fac, &facf);
1932                 
1933                 if ((context.scene->r.mode & R_FIELDS) == 0)
1934                         facf = fac;
1935         }
1936         else {
1937                 fcu = id_data_find_fcurve(&context.scene->id, seq, &RNA_Sequence, "effect_fader", 0, NULL);
1938                 if (fcu) {
1939                         fac = facf = evaluate_fcurve(fcu, cfra);
1940                         if (context.scene->r.mode & R_FIELDS) {
1941                                 facf = evaluate_fcurve(fcu, cfra + 0.5f);
1942                         }
1943                 }
1944                 else {
1945                         fac = facf = seq->effect_fader;
1946                 }
1947         }
1948
1949         early_out = sh.early_out(seq, fac, facf);
1950
1951         switch (early_out) {
1952                 case EARLY_NO_INPUT:
1953                         out = sh.execute(context, seq, cfra, fac, facf,  
1954                                          NULL, NULL, NULL);
1955                         break;
1956                 case EARLY_DO_EFFECT:
1957                         for (i = 0; i < 3; i++) {
1958                                 if (input[i])
1959                                         ibuf[i] = seq_render_strip(
1960                                                 context, input[i], cfra);
1961                         }
1962
1963                         if (ibuf[0] && ibuf[1]) {
1964                                 out = sh.execute(context, seq, cfra, fac, facf,
1965                                                  ibuf[0], ibuf[1], ibuf[2]);
1966                         }
1967                         break;
1968                 case EARLY_USE_INPUT_1:
1969                         if (input[0]) {
1970                                 ibuf[0] = seq_render_strip(context, input[0], cfra);
1971                         }
1972                         if (ibuf[0]) {
1973                                 if (input_have_to_preprocess(context, seq, cfra)) {
1974                                         out = IMB_dupImBuf(ibuf[0]);
1975                                 }
1976                                 else {
1977                                         out = ibuf[0];
1978                                         IMB_refImBuf(out);
1979                                 }
1980                         }
1981                         break;
1982                 case EARLY_USE_INPUT_2:
1983                         if (input[1]) {
1984                                 ibuf[1] = seq_render_strip(context, input[1], cfra);
1985                         }
1986                         if (ibuf[1]) {
1987                                 if (input_have_to_preprocess(context, seq, cfra)) {
1988                                         out = IMB_dupImBuf(ibuf[1]);
1989                                 }
1990                                 else {
1991                                         out = ibuf[1];
1992                                         IMB_refImBuf(out);
1993                                 }
1994                         }
1995                         break;
1996         }
1997
1998         for (i = 0; i < 3; i++) {
1999                 IMB_freeImBuf(ibuf[i]);
2000         }
2001
2002         if (out == NULL) {
2003                 out = IMB_allocImBuf(context.rectx, context.recty, 32, IB_rect);
2004         }
2005
2006         return out;
2007 }
2008
2009 static ImBuf *seq_render_movieclip_strip(
2010         SeqRenderData context, Sequence *seq, float nr)
2011 {
2012         ImBuf *ibuf = NULL;
2013         MovieClipUser user;
2014         float tloc[2], tscale, tangle;
2015
2016         if (!seq->clip) {
2017                 return NULL;
2018         }
2019
2020         memset(&user, 0, sizeof(MovieClipUser));
2021         
2022         BKE_movieclip_user_set_frame(&user, nr + seq->anim_startofs);
2023
2024         user.render_size = MCLIP_PROXY_RENDER_SIZE_FULL;
2025
2026         switch (seq_rendersize_to_proxysize(context.preview_render_size)) {
2027                 case IMB_PROXY_NONE:
2028                         user.render_size = MCLIP_PROXY_RENDER_SIZE_FULL;
2029                         break;
2030                 case IMB_PROXY_100:
2031                         user.render_size = MCLIP_PROXY_RENDER_SIZE_100;
2032                         break;
2033                 case IMB_PROXY_75:
2034                         user.render_size = MCLIP_PROXY_RENDER_SIZE_75;
2035                         break;
2036                 case IMB_PROXY_50:
2037                         user.render_size = MCLIP_PROXY_RENDER_SIZE_50;
2038                         break;
2039                 case IMB_PROXY_25:
2040                         user.render_size = MCLIP_PROXY_RENDER_SIZE_25;
2041                         break;
2042         }
2043
2044         if (seq->clip_flag & SEQ_MOVIECLIP_RENDER_UNDISTORTED) {
2045                 user.render_flag = MCLIP_PROXY_RENDER_UNDISTORT;
2046         }
2047
2048         if (seq->clip_flag & SEQ_MOVIECLIP_RENDER_STABILIZED) {
2049                 ibuf = BKE_movieclip_get_stable_ibuf(
2050                         seq->clip, &user, tloc, &tscale, &tangle,
2051                         0);
2052         }
2053         else {
2054                 ibuf = BKE_movieclip_get_ibuf_flag(
2055                         seq->clip, &user, 0, MOVIECLIP_CACHE_SKIP);
2056         }
2057
2058         return ibuf;
2059 }
2060
2061
2062 static ImBuf *seq_render_mask_strip(
2063         SeqRenderData context, Sequence *seq, float nr)
2064 {
2065         /* TODO - add option to rasterize to alpha imbuf? */
2066         ImBuf *ibuf = NULL;
2067         float *maskbuf;
2068         int i;
2069
2070         if (!seq->mask) {
2071                 return NULL;
2072         }
2073
2074         BKE_mask_evaluate(seq->mask, seq->mask->sfra + nr, TRUE);
2075
2076         maskbuf = MEM_callocN(sizeof(float) * context.rectx * context.recty, __func__);
2077
2078         if (seq->flag & SEQ_MAKE_FLOAT) {
2079                 /* pixels */
2080                 float *fp_src;
2081                 float *fp_dst;
2082
2083                 ibuf = IMB_allocImBuf(context.rectx, context.recty, 32, IB_rectfloat);
2084
2085                 BKE_mask_rasterize(seq->mask,
2086                                    context.rectx, context.recty,
2087                                    maskbuf,
2088                                    TRUE,
2089                                    FALSE, /*XXX- TODO: make on/off for anti-aliasing */
2090                                    TRUE   /*XXX- TODO: make on/off for feather */
2091                                    );
2092
2093                 fp_src = maskbuf;
2094                 fp_dst = ibuf->rect_float;
2095                 i = context.rectx * context.recty;
2096                 while (--i) {
2097                         fp_dst[0] = fp_dst[1] = fp_dst[2] = *fp_src;
2098                         fp_dst[3] = 1.0f;
2099
2100                         fp_src += 1;
2101                         fp_dst += 4;
2102                 }
2103         }
2104         else {
2105                 /* pixels */
2106                 float *fp_src;
2107                 unsigned char *ub_dst;
2108
2109                 ibuf = IMB_allocImBuf(context.rectx, context.recty, 32, IB_rect);
2110
2111                 BKE_mask_rasterize(seq->mask,
2112                                    context.rectx, context.recty,
2113                                    maskbuf,
2114                                    TRUE,
2115                                    FALSE, /*XXX- TODO: make on/off for anti-aliasing */
2116                                    TRUE   /*XXX- TODO: make on/off for feather */
2117                                    );
2118
2119                 fp_src = maskbuf;
2120                 ub_dst = (unsigned char *)ibuf->rect;
2121                 i = context.rectx * context.recty;
2122                 while (--i) {
2123                         ub_dst[0] = ub_dst[1] = ub_dst[2] = (unsigned char)(*fp_src * 255.0f); /* already clamped */
2124                         ub_dst[3] = 255;
2125
2126                         fp_src += 1;
2127                         ub_dst += 4;
2128                 }
2129         }
2130
2131         MEM_freeN(maskbuf);
2132
2133         return ibuf;
2134 }
2135
2136 static ImBuf *seq_render_scene_strip(
2137         SeqRenderData context, Sequence *seq, float nr)
2138 {
2139         ImBuf *ibuf = NULL;
2140         float frame;
2141         float oldcfra;
2142         Object *camera;
2143         ListBase oldmarkers;
2144         
2145         /* Old info:
2146          * Hack! This function can be called from do_render_seq(), in that case
2147          * the seq->scene can already have a Render initialized with same name,
2148          * so we have to use a default name. (compositor uses scene name to
2149          * find render).
2150          * However, when called from within the UI (image preview in sequencer)
2151          * we do want to use scene Render, that way the render result is defined
2152          * for display in render/imagewindow
2153          *
2154          * Hmm, don't see, why we can't do that all the time,
2155          * and since G.rendering is uhm, gone... (Peter)
2156          */
2157
2158         /* New info:
2159          * Using the same name for the renders works just fine as the do_render_seq()
2160          * render is not used while the scene strips are rendered.
2161          *
2162          * However rendering from UI (through sequencer_preview_area_draw) can crash in
2163          * very many cases since other renders (material preview, an actual render etc.)
2164          * can be started while this sequence preview render is running. The only proper
2165          * solution is to make the sequencer preview render a proper job, which can be
2166          * stopped when needed. This would also give a nice progress bar for the preview
2167          * space so that users know there's something happening.
2168          *
2169          * As a result the active scene now only uses OpenGL rendering for the sequencer
2170          * preview. This is far from nice, but is the only way to prevent crashes at this
2171          * time.
2172          *
2173          * -jahka
2174          */
2175
2176         int rendering = G.rendering;
2177         int doseq;
2178         int doseq_gl = G.rendering ? /*(scene->r.seq_flag & R_SEQ_GL_REND)*/ 0 : /*(scene->r.seq_flag & R_SEQ_GL_PREV)*/ 1;
2179         int have_seq = FALSE;
2180         Scene *scene;
2181
2182         /* don't refer to seq->scene above this point!, it can be NULL */
2183         if (seq->scene == NULL) {
2184                 return NULL;
2185         }
2186
2187         scene = seq->scene;
2188         frame = scene->r.sfra + nr + seq->anim_startofs;
2189
2190         have_seq = (scene->r.scemode & R_DOSEQ) && scene->ed && scene->ed->seqbase.first;
2191
2192         oldcfra = scene->r.cfra;
2193         scene->r.cfra = frame;
2194
2195         if (seq->scene_camera)  
2196                 camera = seq->scene_camera;
2197         else {  
2198                 BKE_scene_camera_switch_update(scene);
2199                 camera = scene->camera;
2200         }
2201
2202         if (have_seq == FALSE && camera == NULL) {
2203                 scene->r.cfra = oldcfra;
2204                 return NULL;
2205         }
2206
2207         /* prevent eternal loop */
2208         doseq = context.scene->r.scemode & R_DOSEQ;
2209         context.scene->r.scemode &= ~R_DOSEQ;
2210         
2211 #ifdef DURIAN_CAMERA_SWITCH
2212         /* stooping to new low's in hackyness :( */
2213         oldmarkers = scene->markers;
2214         scene->markers.first = scene->markers.last = NULL;
2215 #else
2216         (void)oldmarkers;
2217 #endif
2218         
2219         if (sequencer_view3d_cb && BLI_thread_is_main() && doseq_gl && (scene == context.scene || have_seq == 0) && camera) {
2220                 char err_out[256] = "unknown";
2221                 /* for old scened this can be uninitialized,
2222                  * should probably be added to do_versions at some point if the functionality stays */
2223                 if (context.scene->r.seq_prev_type == 0)
2224                         context.scene->r.seq_prev_type = 3 /* ==OB_SOLID */; 
2225
2226                 /* opengl offscreen render */
2227                 BKE_scene_update_for_newframe(context.bmain, scene, scene->lay);
2228                 ibuf = sequencer_view3d_cb(scene, camera, context.rectx, context.recty,
2229                                            IB_rect, context.scene->r.seq_prev_type, TRUE, err_out);
2230                 if (ibuf == NULL) {
2231                         fprintf(stderr, "seq_render_scene_strip failed to get opengl buffer: %s\n", err_out);
2232                 }
2233         }
2234         else {
2235                 Render *re = RE_GetRender(scene->id.name);
2236                 RenderResult rres;
2237
2238                 /* XXX: this if can be removed when sequence preview rendering uses the job system */
2239                 if (rendering || context.scene != scene) {
2240                         if (re == NULL)
2241                                 re = RE_NewRender(scene->id.name);
2242                         
2243                         RE_BlenderFrame(re, context.bmain, scene, NULL, camera, scene->lay, frame, FALSE);
2244
2245                         /* restore previous state after it was toggled on & off by RE_BlenderFrame */
2246                         G.rendering = rendering;
2247                 }
2248                 
2249                 RE_AcquireResultImage(re, &rres);
2250                 
2251                 if (rres.rectf) {
2252                         ibuf = IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rectfloat);
2253                         memcpy(ibuf->rect_float, rres.rectf, 4 * sizeof(float) * rres.rectx * rres.recty);
2254                         if (rres.rectz) {
2255                                 addzbuffloatImBuf(ibuf);
2256                                 memcpy(ibuf->zbuf_float, rres.rectz, sizeof(float) * rres.rectx * rres.recty);
2257                         }
2258
2259                         /* float buffers in the sequencer are not linear */
2260                         if (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
2261                                 ibuf->profile = IB_PROFILE_LINEAR_RGB;
2262                         else
2263                                 ibuf->profile = IB_PROFILE_NONE;
2264                         IMB_convert_profile(ibuf, IB_PROFILE_SRGB);                     
2265                 }
2266                 else if (rres.rect32) {
2267                         ibuf = IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rect);
2268                         memcpy(ibuf->rect, rres.rect32, 4 * rres.rectx * rres.recty);
2269                 }
2270                 
2271                 RE_ReleaseResultImage(re);
2272                 
2273                 // BIF_end_render_callbacks();
2274         }
2275         
2276         /* restore */
2277         context.scene->r.scemode |= doseq;
2278         
2279         scene->r.cfra = oldcfra;
2280
2281         if (frame != oldcfra)
2282                 BKE_scene_update_for_newframe(context.bmain, scene, scene->lay);
2283         
2284 #ifdef DURIAN_CAMERA_SWITCH
2285         /* stooping to new low's in hackyness :( */
2286         scene->markers = oldmarkers;
2287 #endif
2288
2289         return ibuf;
2290 }
2291
2292 static ImBuf *seq_render_strip(SeqRenderData context, Sequence *seq, float cfra)
2293 {
2294         ImBuf *ibuf = NULL;
2295         char name[FILE_MAX];
2296         int use_preprocess = input_have_to_preprocess(context, seq, cfra);
2297         int is_proxy_image = FALSE;
2298         float nr = give_stripelem_index(seq, cfra);
2299         /* all effects are handled similarly with the exception of speed effect */
2300         int type = (seq->type & SEQ_TYPE_EFFECT && seq->type != SEQ_TYPE_SPEED) ? SEQ_TYPE_EFFECT : seq->type;
2301         int is_preprocessed = !ELEM3(type, SEQ_TYPE_IMAGE, SEQ_TYPE_MOVIE, SEQ_TYPE_SCENE);
2302
2303         ibuf = seq_stripelem_cache_get(context, seq, cfra, SEQ_STRIPELEM_IBUF);
2304
2305         /* currently, we cache preprocessed images in SEQ_STRIPELEM_IBUF,
2306          * but not(!) on SEQ_STRIPELEM_IBUF_ENDSTILL and ..._STARTSTILL */
2307         if (ibuf)
2308                 use_preprocess = FALSE;
2309
2310         if (ibuf == NULL)
2311                 ibuf = copy_from_ibuf_still(context, seq, nr);
2312         
2313         /* MOVIECLIPs have their own proxy management */
2314         if (ibuf == NULL && seq->type != SEQ_TYPE_MOVIECLIP) {
2315                 ibuf = seq_proxy_fetch(context, seq, cfra);
2316                 is_proxy_image = (ibuf != NULL);
2317         }
2318
2319         if (ibuf == NULL) switch (type) {
2320                         case SEQ_TYPE_META:
2321                         {
2322                                 ImBuf *meta_ibuf = NULL;
2323
2324                                 if (seq->seqbase.first)
2325                                         meta_ibuf = seq_render_strip_stack(
2326                                                 context, &seq->seqbase,
2327                                                 seq->start + nr, 0);
2328
2329                                 if (meta_ibuf) {
2330                                         ibuf = meta_ibuf;
2331                                         if (ibuf && use_preprocess) {
2332                                                 ImBuf *i = IMB_dupImBuf(ibuf);
2333
2334                                                 IMB_freeImBuf(ibuf);
2335
2336                                                 ibuf = i;
2337                                         }
2338                                 }
2339
2340                                 break;
2341                         }
2342                         case SEQ_TYPE_SPEED:
2343                         {
2344                                 ImBuf *child_ibuf = NULL;
2345
2346                                 float f_cfra;
2347                                 SpeedControlVars *s = (SpeedControlVars *)seq->effectdata;
2348
2349                                 sequence_effect_speed_rebuild_map(context.scene, seq, 0);
2350
2351                                 /* weeek! */
2352                                 f_cfra = seq->start + s->frameMap[(int)nr];
2353
2354                                 child_ibuf = seq_render_strip(context, seq->seq1, f_cfra);
2355
2356                                 if (child_ibuf) {
2357                                         ibuf = child_ibuf;
2358                                         if (ibuf && use_preprocess) {
2359                                                 ImBuf *i = IMB_dupImBuf(ibuf);
2360
2361                                                 IMB_freeImBuf(ibuf);
2362
2363                                                 ibuf = i;
2364                                         }
2365                                 }
2366                                 break;
2367                         }
2368                         case SEQ_TYPE_EFFECT:
2369                         {
2370                                 ibuf = seq_render_effect_strip_impl(
2371                                         context, seq, seq->start + nr);
2372                                 break;
2373                         }
2374                         case SEQ_TYPE_IMAGE:
2375                         {
2376                                 StripElem *s_elem = give_stripelem(seq, cfra);
2377
2378                                 if (s_elem) {
2379                                         BLI_join_dirfile(name, sizeof(name), seq->strip->dir, s_elem->name);
2380                                         BLI_path_abs(name, G.main->name);
2381                                 }
2382
2383                                 if (s_elem && (ibuf = IMB_loadiffname(name, IB_rect))) {
2384                                         /* we don't need both (speed reasons)! */
2385                                         if (ibuf->rect_float && ibuf->rect)
2386                                                 imb_freerectImBuf(ibuf);
2387
2388                                         /* all sequencer color is done in SRGB space, linear gives odd crossfades */
2389                                         if (ibuf->profile == IB_PROFILE_LINEAR_RGB)
2390                                                 IMB_convert_profile(ibuf, IB_PROFILE_NONE);
2391
2392                                         copy_to_ibuf_still(context, seq, nr, ibuf);
2393
2394                                         s_elem->orig_width  = ibuf->x;
2395                                         s_elem->orig_height = ibuf->y;
2396                                 }
2397                                 break;
2398                         }
2399                         case SEQ_TYPE_MOVIE:
2400                         {
2401                                 seq_open_anim_file(seq);
2402
2403                                 if (seq->anim) {
2404                                         IMB_anim_set_preseek(seq->anim,
2405                                                              seq->anim_preseek);
2406
2407                                         ibuf = IMB_anim_absolute(
2408                                                 seq->anim, nr + seq->anim_startofs,
2409                                                 seq->strip->proxy ?
2410                                                     seq->strip->proxy->tc :
2411                                                     IMB_TC_RECORD_RUN,
2412                                                 seq_rendersize_to_proxysize(
2413                                                     context.preview_render_size));
2414
2415                                         /* we don't need both (speed reasons)! */
2416                                         if (ibuf && ibuf->rect_float && ibuf->rect)
2417                                                 imb_freerectImBuf(ibuf);
2418                                         if (ibuf) {
2419                                                 seq->strip->stripdata->orig_width = ibuf->x;
2420                                                 seq->strip->stripdata->orig_height = ibuf->y;
2421                                         }
2422                                 }
2423                                 copy_to_ibuf_still(context, seq, nr, ibuf);
2424                                 break;
2425                         }
2426                         case SEQ_TYPE_SCENE:
2427                         { // scene can be NULL after deletions
2428                                 ibuf = seq_render_scene_strip(context, seq, nr);
2429
2430                                 /* Scene strips update all animation, so we need to restore original state.*/
2431                                 BKE_animsys_evaluate_all_animation(context.bmain, context.scene, cfra);
2432
2433                                 copy_to_ibuf_still(context, seq, nr, ibuf);
2434                                 break;
2435                         }
2436                         case SEQ_TYPE_MOVIECLIP:
2437                         {
2438                                 ibuf = seq_render_movieclip_strip(context, seq, nr);
2439
2440                                 if (ibuf && use_preprocess) {
2441                                         ImBuf *i = IMB_dupImBuf(ibuf);
2442
2443                                         IMB_freeImBuf(ibuf);
2444
2445                                         ibuf = i;
2446                                 }
2447
2448                                 copy_to_ibuf_still(context, seq, nr, ibuf);
2449                                 break;
2450                         }
2451                 case SEQ_TYPE_MASK:
2452                 {
2453                         /* ibuf is alwats new */
2454                         ibuf = seq_render_mask_strip(context, seq, nr);
2455
2456                         copy_to_ibuf_still(context, seq, nr, ibuf);
2457                         break;
2458                 }
2459                 }
2460
2461         if (ibuf == NULL)
2462                 ibuf = IMB_allocImBuf(context.rectx, context.recty, 32, IB_rect);
2463
2464         if (ibuf->x != context.rectx || ibuf->y != context.recty)
2465                 use_preprocess = TRUE;
2466
2467         if (use_preprocess)
2468                 ibuf = input_preprocess(context, seq, cfra, ibuf, 
2469                                         is_proxy_image, is_preprocessed);
2470
2471         seq_stripelem_cache_put(context, seq, cfra, SEQ_STRIPELEM_IBUF, ibuf);
2472
2473         return ibuf;
2474 }
2475
2476 /* **********************************************************************
2477  * strip stack rendering functions
2478  * ********************************************************************** */
2479
2480 static int seq_must_swap_input_in_blend_mode(Sequence *seq)
2481 {
2482         int swap_input = FALSE;
2483
2484         /* bad hack, to fix crazy input ordering of 
2485          * those two effects */
2486
2487         if (ELEM3(seq->blend_mode, SEQ_TYPE_ALPHAOVER, SEQ_TYPE_ALPHAUNDER, SEQ_TYPE_OVERDROP)) {
2488                 swap_input = TRUE;
2489         }
2490         
2491         return swap_input;
2492 }
2493
2494 static int seq_get_early_out_for_blend_mode(Sequence *seq)
2495 {
2496         struct SeqEffectHandle sh = get_sequence_blend(seq);
2497         float facf = seq->blend_opacity / 100.0f;
2498         int early_out = sh.early_out(seq, facf, facf);
2499         
2500         if (ELEM(early_out, EARLY_DO_EFFECT, EARLY_NO_INPUT)) {
2501                 return early_out;
2502         }
2503
2504         if (seq_must_swap_input_in_blend_mode(seq)) {
2505                 if (early_out == EARLY_USE_INPUT_2) {
2506                         return EARLY_USE_INPUT_1;
2507                 }
2508                 else if (early_out == EARLY_USE_INPUT_1) {
2509                         return EARLY_USE_INPUT_2;
2510                 }
2511         }
2512         return early_out;
2513 }
2514
2515 static ImBuf *seq_render_strip_stack(
2516         SeqRenderData context, ListBase *seqbasep, float cfra, int chanshown)
2517 {
2518         Sequence *seq_arr[MAXSEQ + 1];
2519         int count;
2520         int i;
2521         ImBuf *out = NULL;
2522
2523         count = get_shown_sequences(seqbasep, cfra, chanshown, (Sequence **)&seq_arr);
2524
2525         if (count == 0) {
2526                 return NULL;
2527         }
2528
2529 #if 0 /* commentind since this breaks keyframing, since it resets the value on draw */
2530         if (scene->r.cfra != cfra) {
2531                 // XXX for prefetch and overlay offset!..., very bad!!!
2532                 AnimData *adt = BKE_animdata_from_id(&scene->id);
2533                 BKE_animsys_evaluate_animdata(scene, &scene->id, adt, cfra, ADT_RECALC_ANIM);
2534         }
2535 #endif
2536
2537         out = seq_stripelem_cache_get(context, seq_arr[count - 1], 
2538                                       cfra, SEQ_STRIPELEM_IBUF_COMP);
2539
2540         if (out) {
2541                 return out;
2542         }
2543         
2544         if (count == 1) {
2545                 out = seq_render_strip(context, seq_arr[0], cfra);
2546                 seq_stripelem_cache_put(context, seq_arr[0], cfra, 
2547                                         SEQ_STRIPELEM_IBUF_COMP, out);
2548
2549                 return out;
2550         }
2551
2552
2553         for (i = count - 1; i >= 0; i--) {
2554                 int early_out;
2555                 Sequence *seq = seq_arr[i];
2556
2557                 out = seq_stripelem_cache_get(
2558                         context, seq, cfra, SEQ_STRIPELEM_IBUF_COMP);
2559
2560                 if (out) {
2561                         break;
2562                 }
2563                 if (seq->blend_mode == SEQ_BLEND_REPLACE) {
2564                         out = seq_render_strip(context, seq, cfra);
2565                         break;
2566                 }
2567
2568                 early_out = seq_get_early_out_for_blend_mode(seq);
2569
2570                 switch (early_out) {
2571                         case EARLY_NO_INPUT:
2572                         case EARLY_USE_INPUT_2:
2573                                 out = seq_render_strip(context, seq, cfra);
2574                                 break;
2575                         case EARLY_USE_INPUT_1:
2576                                 if (i == 0) {
2577                                         out = IMB_allocImBuf(context.rectx, context.recty, 32, IB_rect);
2578                                 }
2579                                 break;
2580                         case EARLY_DO_EFFECT:
2581                                 if (i == 0) {
2582                                         out = seq_render_strip(context, seq, cfra);
2583                                 }
2584
2585                                 break;
2586                 }
2587                 if (out) {
2588                         break;
2589                 }
2590         }
2591
2592         seq_stripelem_cache_put(context, seq_arr[i], cfra, 
2593                                 SEQ_STRIPELEM_IBUF_COMP, out);
2594
2595
2596         i++;
2597
2598         for (; i < count; i++) {
2599                 Sequence *seq = seq_arr[i];
2600
2601                 if (seq_get_early_out_for_blend_mode(seq) == EARLY_DO_EFFECT) {
2602                         struct SeqEffectHandle sh = get_sequence_blend(seq);
2603                         ImBuf *ibuf1 = out;
2604                         ImBuf *ibuf2 = seq_render_strip(context, seq, cfra);
2605
2606                         float facf = seq->blend_opacity / 100.0f;
2607                         int swap_input = seq_must_swap_input_in_blend_mode(seq);
2608
2609                         if (swap_input) {
2610                                 out = sh.execute(context, seq, cfra, 
2611                                                  facf, facf,
2612                                                  ibuf2, ibuf1, NULL);
2613                         }
2614                         else {
2615                                 out = sh.execute(context, seq, cfra, 
2616                                                  facf, facf,
2617                                                  ibuf1, ibuf2, NULL);
2618                         }
2619                 
2620                         IMB_freeImBuf(ibuf1);
2621                         IMB_freeImBuf(ibuf2);
2622                 }
2623
2624                 seq_stripelem_cache_put(context, seq_arr[i], cfra,
2625                                         SEQ_STRIPELEM_IBUF_COMP, out);
2626         }
2627
2628         return out;
2629 }
2630
2631 /*
2632  * returned ImBuf is refed!
2633  * you have to free after usage!
2634  */
2635
2636 ImBuf *give_ibuf_seq(SeqRenderData context, float cfra, int chanshown)
2637 {
2638         Editing *ed = BKE_sequencer_editing_get(context.scene, FALSE);
2639         int count;
2640         ListBase *seqbasep;
2641         
2642         if (ed == NULL) return NULL;
2643
2644         count = BLI_countlist(&ed->metastack);
2645         if ((chanshown < 0) && (count > 0)) {
2646                 count = MAX2(count + chanshown, 0);
2647                 seqbasep = ((MetaStack *)BLI_findlink(&ed->metastack, count))->oldbasep;
2648         }
2649         else {
2650                 seqbasep = ed->seqbasep;
2651         }
2652
2653         return seq_render_strip_stack(context, seqbasep, cfra, chanshown);
2654 }
2655
2656 ImBuf *give_ibuf_seqbase(SeqRenderData context, float cfra, int chanshown, ListBase *seqbasep)
2657 {
2658         return seq_render_strip_stack(context, seqbasep, cfra, chanshown);
2659 }
2660
2661
2662 ImBuf *give_ibuf_seq_direct(SeqRenderData context, float cfra, Sequence *seq)
2663 {
2664         return seq_render_strip(context, seq, cfra);
2665 }
2666
2667 #if 0
2668 /* check used when we need to change seq->blend_mode but not to effect or audio strips */
2669 static int seq_can_blend(Sequence *seq)
2670 {
2671         if (ELEM4(seq->type, SEQ_TYPE_IMAGE, SEQ_TYPE_META, SEQ_TYPE_SCENE, SEQ_TYPE_MOVIE)) {
2672                 return 1;
2673         }
2674         else {
2675                 return 0;
2676         }
2677 }
2678 #endif
2679
2680 /* *********************** threading api ******************* */
2681
2682 static ListBase running_threads;
2683 static ListBase prefetch_wait;
2684 static ListBase prefetch_done;
2685
2686 static pthread_mutex_t queue_lock          = PTHREAD_MUTEX_INITIALIZER;
2687 static pthread_mutex_t wakeup_lock         = PTHREAD_MUTEX_INITIALIZER;
2688 static pthread_cond_t wakeup_cond          = PTHREAD_COND_INITIALIZER;
2689
2690 //static pthread_mutex_t prefetch_ready_lock = PTHREAD_MUTEX_INITIALIZER;
2691 //static pthread_cond_t  prefetch_ready_cond = PTHREAD_COND_INITIALIZER;
2692
2693 static pthread_mutex_t frame_done_lock     = PTHREAD_MUTEX_INITIALIZER;
2694 static pthread_cond_t frame_done_cond      = PTHREAD_COND_INITIALIZER;
2695
2696 static volatile int seq_thread_shutdown = TRUE; 
2697 static volatile int seq_last_given_monoton_cfra = 0;
2698 static int monoton_cfra = 0;
2699
2700 typedef struct PrefetchThread {
2701         struct PrefetchThread *next, *prev;
2702         
2703         Scene *scene;
2704         struct PrefetchQueueElem *current;
2705         pthread_t pthread;
2706         int running;
2707         
2708 } PrefetchThread;
2709
2710 typedef struct PrefetchQueueElem {
2711         struct PrefetchQueueElem *next, *prev;
2712         
2713         int rectx;
2714         int recty;
2715         float cfra;
2716         int chanshown;
2717         int preview_render_size;
2718
2719         int monoton_cfra;
2720
2721         ImBuf *ibuf;
2722 } PrefetchQueueElem;
2723
2724 #if 0
2725 static void *seq_prefetch_thread(void *This_)
2726 {
2727         PrefetchThread *This = This_;
2728
2729         while (!seq_thread_shutdown) {
2730                 PrefetchQueueElem *e;
2731                 int s_last;
2732
2733                 pthread_mutex_lock(&queue_lock);
2734                 e = prefetch_wait.first;
2735                 if (e) {
2736                         BLI_remlink(&prefetch_wait, e);
2737                 }
2738                 s_last = seq_last_given_monoton_cfra;
2739
2740                 This->current = e;
2741
2742                 pthread_mutex_unlock(&queue_lock);
2743
2744                 if (!e) {
2745                         pthread_mutex_lock(&prefetch_ready_lock);
2746
2747                         This->running = FALSE;
2748
2749                         pthread_cond_signal(&prefetch_ready_cond);
2750                         pthread_mutex_unlock(&prefetch_ready_lock);
2751
2752                         pthread_mutex_lock(&wakeup_lock);
2753                         if (!seq_thread_shutdown) {
2754                                 pthread_cond_wait(&wakeup_cond, &wakeup_lock);
2755                         }
2756                         pthread_mutex_unlock(&wakeup_lock);
2757                         continue;
2758                 }
2759
2760                 This->running = TRUE;
2761                 
2762                 if (e->cfra >= s_last) { 
2763                         e->ibuf = give_ibuf_seq_impl(This->scene, 
2764                                                      e->rectx, e->recty, e->cfra, e->chanshown,
2765                                                      e->preview_render_size);
2766                 }
2767
2768                 pthread_mutex_lock(&queue_lock);
2769
2770                 BLI_addtail(&prefetch_done, e);
2771
2772                 for (e = prefetch_wait.first; e; e = e->next) {
2773                         if (s_last > e->monoton_cfra) {
2774                                 BLI_remlink(&prefetch_wait, e);
2775                                 MEM_freeN(e);
2776                         }
2777                 }
2778
2779                 for (e = prefetch_done.first; e; e = e->next) {
2780                         if (s_last > e->monoton_cfra) {
2781                                 BLI_remlink(&prefetch_done, e);
2782                                 MEM_freeN(e);
2783                         }
2784                 }
2785
2786                 pthread_mutex_unlock(&queue_lock);
2787
2788                 pthread_mutex_lock(&frame_done_lock);
2789                 pthread_cond_signal(&frame_done_cond);
2790                 pthread_mutex_unlock(&frame_done_lock);
2791         }
2792         return 0;
2793 }
2794
2795 static void seq_start_threads(Scene *scene)
2796 {
2797         int i;
2798
2799         running_threads.first = running_threads.last = NULL;
2800         prefetch_wait.first = prefetch_wait.last = NULL;
2801         prefetch_done.first = prefetch_done.last = NULL;
2802
2803         seq_thread_shutdown = FALSE;
2804         seq_last_given_monoton_cfra = monoton_cfra = 0;
2805
2806         /* since global structures are modified during the processing
2807          * of one frame, only one render thread is currently possible...
2808          *
2809          * (but we code, in the hope, that we can remove this restriction
2810          * soon...)
2811          */
2812
2813         fprintf(stderr, "SEQ-THREAD: seq_start_threads\n");
2814
2815         for (i = 0; i < 1; i++) {
2816                 PrefetchThread *t = MEM_callocN(sizeof(PrefetchThread), "prefetch_thread");
2817                 t->scene = scene;
2818                 t->running = TRUE;
2819                 BLI_addtail(&running_threads, t);
2820
2821                 pthread_create(&t->pthread, NULL, seq_prefetch_thread, t);
2822         }
2823
2824         /* init malloc mutex */
2825         BLI_init_threads(0, 0, 0);
2826 }
2827
2828 static void seq_stop_threads()
2829 {
2830         PrefetchThread *tslot;
2831         PrefetchQueueElem *e;
2832
2833         fprintf(stderr, "SEQ-THREAD: seq_stop_threads()\n");
2834
2835         if (seq_thread_shutdown) {
2836                 fprintf(stderr, "SEQ-THREAD: ... already stopped\n");
2837                 return;
2838         }
2839         
2840         pthread_mutex_lock(&wakeup_lock);
2841
2842         seq_thread_shutdown = TRUE;
2843
2844         pthread_cond_broadcast(&wakeup_cond);
2845         pthread_mutex_unlock(&wakeup_lock);
2846
2847         for (tslot = running_threads.first; tslot; tslot = tslot->next) {
2848                 pthread_join(tslot->pthread, NULL);
2849         }
2850
2851
2852         for (e = prefetch_wait.first; e; e = e->next) {
2853                 BLI_remlink(&prefetch_wait, e);
2854                 MEM_freeN(e);
2855         }
2856
2857         for (e = prefetch_done.first; e; e = e->next) {
2858                 BLI_remlink(&prefetch_done, e);
2859                 MEM_freeN(e);
2860         }
2861
2862         BLI_freelistN(&running_threads);
2863
2864         /* deinit malloc mutex */
2865         BLI_end_threads(0);
2866 }
2867 #endif
2868
2869 void give_ibuf_prefetch_request(SeqRenderData context, float cfra, int chanshown)
2870 {
2871         PrefetchQueueElem *e;
2872         if (seq_thread_shutdown) {
2873                 return;
2874         }
2875
2876         e = MEM_callocN(sizeof(PrefetchQueueElem), "prefetch_queue_elem");
2877         e->rectx = context.rectx;
2878         e->recty = context.recty;
2879         e->cfra = cfra;
2880         e->chanshown = chanshown;
2881         e->preview_render_size = context.preview_render_size;
2882         e->monoton_cfra = monoton_cfra++;
2883
2884         pthread_mutex_lock(&queue_lock);
2885         BLI_addtail(&prefetch_wait, e);
2886         pthread_mutex_unlock(&queue_lock);
2887         
2888         pthread_mutex_lock(&wakeup_lock);
2889         pthread_cond_signal(&wakeup_cond);
2890         pthread_mutex_unlock(&wakeup_lock);
2891 }
2892
2893 #if 0
2894 static void seq_wait_for_prefetch_ready()
2895 {
2896         PrefetchThread *tslot;
2897
2898         if (seq_thread_shutdown) {
2899                 return;
2900         }
2901
2902         fprintf(stderr, "SEQ-THREAD: rendering prefetch frames...\n");
2903
2904         pthread_mutex_lock(&prefetch_ready_lock);
2905
2906         for (;; ) {
2907                 for (tslot = running_threads.first; tslot; tslot = tslot->next) {
2908                         if (tslot->running) {
2909                                 break;
2910                         }
2911                 }
2912                 if (!tslot) {
2913                         break;
2914                 }
2915                 pthread_cond_wait(&prefetch_ready_cond, &prefetch_ready_lock);
2916         }
2917
2918         pthread_mutex_unlock(&prefetch_ready_lock);
2919
2920         fprintf(stderr, "SEQ-THREAD: prefetch done\n");
2921 }
2922 #endif
2923
2924 ImBuf *give_ibuf_seq_threaded(SeqRenderData context, float cfra, int chanshown)
2925 {
2926         PrefetchQueueElem *e = NULL;
2927         int found_something = FALSE;
2928
2929         if (seq_thread_shutdown) {
2930                 return give_ibuf_seq(context, cfra, chanshown);
2931         }
2932
2933         while (!e) {
2934                 int success = FALSE;
2935                 pthread_mutex_lock(&queue_lock);
2936
2937                 for (e = prefetch_done.first; e; e = e->next) {
2938                         if (cfra == e->cfra &&
2939                             chanshown == e->chanshown &&
2940                             context.rectx == e->rectx &&
2941                             context.recty == e->recty &&
2942                             context.preview_render_size == e->preview_render_size)
2943                         {
2944                                 success = TRUE;
2945                                 found_something = TRUE;
2946                                 break;
2947                         }
2948                 }
2949
2950                 if (!e) {
2951                         for (e = prefetch_wait.first; e; e = e->next) {
2952                                 if (cfra == e->cfra &&
2953                                     chanshown == e->chanshown &&
2954                                     context.rectx == e->rectx &&
2955                                     context.recty == e->recty &&
2956                                     context.preview_render_size == e->preview_render_size)
2957                                 {
2958                                         found_something = TRUE;
2959                                         break;
2960                                 }
2961                         }
2962                 }
2963
2964                 if (!e) {
2965                         PrefetchThread *tslot;
2966
2967                         for (tslot = running_threads.first;
2968                              tslot;
2969                              tslot = tslot->next)
2970                         {
2971                                 if (tslot->current &&
2972                                     cfra == tslot->current->cfra &&
2973                                     chanshown == tslot->current->chanshown &&
2974                                     context.rectx == tslot->current->rectx &&
2975                                     context.recty == tslot->current->recty &&
2976                                     context.preview_render_size == tslot->current->preview_render_size)
2977                                 {
2978                                         found_something = TRUE;
2979                                         break;
2980                                 }
2981                         }
2982                 }
2983
2984                 /* e->ibuf is unrefed by render thread on next round. */
2985
2986                 if (e) {
2987                         seq_last_given_monoton_cfra = e->monoton_cfra;
2988                 }
2989
2990                 pthread_mutex_unlock(&queue_lock);
2991
2992                 if (!success) {
2993                         e = NULL;
2994
2995                         if (!found_something) {
2996                                 fprintf(stderr, 
2997                                         "SEQ-THREAD: Requested frame "
2998                                         "not in queue ???\n");
2999                                 break;
3000                         }
3001                         pthread_mutex_lock(&frame_done_lock);
3002                         pthread_cond_wait(&frame_done_cond, &frame_done_lock);
3003                         pthread_mutex_unlock(&frame_done_lock);
3004                 }
3005         }
3006         
3007         return e ? e->ibuf : NULL;
3008 }
3009
3010 /* Functions to free imbuf and anim data on changes */
3011
3012 static void free_anim_seq(Sequence *seq)
3013 {
3014         if (seq->anim) {
3015                 IMB_free_anim(seq->anim);
3016                 seq->anim = NULL;
3017         }
3018 }
3019
3020 void free_imbuf_seq(Scene *scene, ListBase *seqbase, int check_mem_usage,
3021                     int keep_file_handles)
3022 {
3023         Sequence *seq;
3024
3025         if (check_mem_usage) {
3026                 /* Let the cache limitor take care of this (schlaile) */
3027                 /* While render let's keep all memory available for render 
3028                  * (ton)
3029                  * At least if free memory is tight...
3030                  * This can make a big difference in encoding speed
3031                  * (it is around 4 times(!) faster, if we do not waste time
3032                  * on freeing _all_ buffers every time on long timelines...)
3033                  * (schlaile)
3034                  */
3035         
3036                 uintptr_t mem_in_use;
3037                 uintptr_t mmap_in_use;
3038                 uintptr_t max;
3039         
3040                 mem_in_use = MEM_get_memory_in_use();
3041                 mmap_in_use = MEM_get_mapped_memory_in_use();
3042                 max = MEM_CacheLimiter_get_maximum();
3043         
3044                 if (max == 0 || mem_in_use + mmap_in_use <= max) {
3045                         return;
3046                 }
3047         }
3048
3049         seq_stripelem_cache_cleanup();
3050         
3051         for (seq = seqbase->first; seq; seq = seq->next) {
3052                 if (seq->strip) {
3053                         if (seq->type == SEQ_TYPE_MOVIE && !keep_file_handles)
3054                                 free_anim_seq(seq);
3055                         if (seq->type == SEQ_TYPE_SPEED) {
3056                                 sequence_effect_speed_rebuild_map(scene, seq, 1);
3057                         }
3058                 }
3059                 if (seq->type == SEQ_TYPE_META) {
3060                         free_imbuf_seq(scene, &seq->seqbase, FALSE, keep_file_handles);
3061                 }
3062                 if (seq->type == SEQ_TYPE_SCENE) {
3063                         /* FIXME: recurs downwards, 
3064                          * but do recurs protection somehow! */
3065                 }
3066         }
3067         
3068 }
3069
3070 static int update_changed_seq_recurs(Scene *scene, Sequence *seq, Sequence *changed_seq, int len_change, int ibuf_change)
3071 {
3072         Sequence *subseq;
3073         int free_imbuf = 0;
3074         
3075         /* recurs downwards to see if this seq depends on the changed seq */
3076         
3077         if (seq == NULL)
3078                 return 0;
3079         
3080         if (seq == changed_seq)
3081                 free_imbuf = 1;
3082         
3083         for (subseq = seq->seqbase.first; subseq; subseq = subseq->next)
3084                 if (update_changed_seq_recurs(scene, subseq, changed_seq, len_change, ibuf_change))
3085                         free_imbuf = TRUE;
3086         
3087         if (seq->seq1)
3088                 if (update_changed_seq_recurs(scene, seq->seq1, changed_seq, len_change, ibuf_change))
3089                         free_imbuf = TRUE;
3090         if (seq->seq2 && (seq->seq2 != seq->seq1))
3091                 if (update_changed_seq_recurs(scene, seq->seq2, changed_seq, len_change, ibuf_change))
3092                         free_imbuf = TRUE;
3093         if (seq->seq3 && (seq->seq3 != seq->seq1) && (seq->seq3 != seq->seq2))
3094                 if (update_changed_seq_recurs(scene, seq->seq3, changed_seq, len_change, ibuf_change))
3095                         free_imbuf = TRUE;
3096         
3097         if (free_imbuf) {
3098                 if (ibuf_change) {
3099                         if (seq->type == SEQ_TYPE_MOVIE)
3100                                 free_anim_seq(seq);
3101                         if (seq->type == SEQ_TYPE_SPEED) {
3102                                 sequence_effect_speed_rebuild_map(scene, seq, 1);
3103                         }
3104                 }
3105                 
3106                 if (len_change)
3107                         calc_sequence(scene, seq);
3108         }
3109         
3110         return free_imbuf;
3111 }
3112
3113 void update_changed_seq_and_deps(Scene *scene, Sequence *changed_seq, int len_change, int ibuf_change)
3114 {
3115         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
3116         Sequence *seq;
3117         
3118         if (ed == NULL) return;
3119         
3120         for (seq = ed->seqbase.first; seq; seq = seq->next)
3121                 update_changed_seq_recurs(scene, seq, changed_seq, len_change, ibuf_change);
3122 }
3123
3124 /* seq funcs's for transforming internally
3125  * notice the difference between start/end and left/right.
3126  *
3127  * left and right are the bounds at which the sequence is rendered,
3128  * start and end are from the start and fixed length of the sequence.
3129  */
3130 int seq_tx_get_start(Sequence *seq)
3131 {
3132         return seq->start;
3133 }
3134 int seq_tx_get_end(Sequence *seq)
3135 {
3136         return seq->start + seq->len;
3137 }
3138
3139 int seq_tx_get_final_left(Sequence *seq, int metaclip)
3140 {
3141         if (metaclip && seq->tmp) {
3142                 /* return the range clipped by the parents range */
3143                 return MAX2(seq_tx_get_final_left(seq, 0), seq_tx_get_final_left((Sequence *)seq->tmp, 1) );
3144         }
3145         else {
3146                 return (seq->start - seq->startstill) + seq->startofs;
3147         }
3148
3149 }
3150 int seq_tx_get_final_right(Sequence *seq, int metaclip)
3151 {
3152         if (metaclip && seq->tmp) {
3153                 /* return the range clipped by the parents range */
3154                 return MIN2(seq_tx_get_final_right(seq, 0), seq_tx_get_final_right((Sequence *)seq->tmp, 1) );
3155         }
3156         else {
3157                 return ((seq->start + seq->len) + seq->endstill) - seq->endofs;
3158         }
3159 }
3160
3161 void seq_tx_set_final_left(Sequence *seq, int val)
3162 {
3163         if (val < (seq)->start) {
3164                 seq->startstill = abs(val - (seq)->start);
3165                 seq->startofs = 0;
3166         }
3167         else {
3168                 seq->startofs = abs(val - (seq)->start);
3169                 seq->startstill = 0;
3170         }
3171 }
3172
3173 void seq_tx_set_final_right(Sequence *seq, int val)
3174 {
3175         if (val > (seq)->start + (seq)->len) {
3176                 seq->endstill = abs(val - (seq->start + (seq)->len));
3177                 seq->endofs = 0;
3178         }
3179         else {
3180                 seq->endofs = abs(val - ((seq)->start + (seq)->len));
3181                 seq->endstill = 0;
3182         }
3183 }
3184
3185 /* used so we can do a quick check for single image seq
3186  * since they work a bit differently to normal image seq's (during transform) */
3187 int seq_single_check(Sequence *seq)
3188 {
3189         return ((seq->len == 1) &&
3190                 (seq->type == SEQ_TYPE_IMAGE ||
3191                  ((seq->type & SEQ_TYPE_EFFECT) &&
3192                   get_sequence_effect_num_inputs(seq->type) == 0)));
3193 }
3194
3195 /* check if the selected seq's reference unselected seq's */
3196 int seqbase_isolated_sel_check(ListBase *seqbase)
3197 {
3198         Sequence *seq;
3199         /* is there more than 1 select */
3200         int ok = FALSE;
3201
3202         for (seq = seqbase->first; seq; seq = seq->next) {
3203                 if (seq->flag & SELECT) {
3204                         ok = TRUE;
3205                         break;
3206                 }
3207         }
3208
3209         if (ok == FALSE)
3210                 return FALSE;
3211
3212         /* test relationships */
3213         for (seq = seqbase->first; seq; seq = seq->next) {
3214                 if ((seq->type & SEQ_TYPE_EFFECT) == 0)
3215                         continue;
3216
3217                 if (seq->flag & SELECT) {
3218                         if ( (seq->seq1 && (seq->seq1->flag & SELECT) == 0) ||
3219                              (seq->seq2 && (seq->seq2->flag & SELECT) == 0) ||
3220                              (seq->seq3 && (seq->seq3->flag & SELECT) == 0) )
3221                         {
3222                                 return FALSE;
3223                         }
3224                 }
3225                 else {
3226                         if ( (seq->seq1 && (seq->seq1->flag & SELECT)) ||
3227                              (seq->seq2 && (seq->seq2->flag & SELECT)) ||
3228                              (seq->seq3 && (seq->seq3->flag & SELECT)) )
3229                         {
3230                                 return FALSE;
3231                         }
3232                 }
3233         }
3234
3235         return TRUE;
3236 }
3237
3238 /* use to impose limits when dragging/extending - so impossible situations don't happen
3239  * Cant use the SEQ_LEFTSEL and SEQ_LEFTSEL directly because the strip may be in a metastrip */
3240 void seq_tx_handle_xlimits(Sequence *seq, int leftflag, int rightflag)
3241 {
3242         if (leftflag) {
3243                 if (seq_tx_get_final_left(seq, 0) >= seq_tx_get_final_right(seq, 0)) {
3244                         seq_tx_set_final_left(seq, seq_tx_get_final_right(seq, 0) - 1);
3245                 }
3246
3247                 if (seq_single_check(seq) == 0) {
3248                         if (seq_tx_get_final_left(seq, 0) >= seq_tx_get_end(seq)) {
3249                                 seq_tx_set_final_left(seq, seq_tx_get_end(seq) - 1);
3250                         }
3251
3252                         /* dosnt work now - TODO */
3253 #if 0
3254