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