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