use 'bool' for BLI_/BKE_ functions.
[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, bool 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         /* also clear clipboard */
820         BKE_sequencer_base_recursive_apply(&seqbase_clipboard, clear_scene_in_allseqs_cb, scene);
821 }
822
823 static int clear_movieclip_in_clipboard_cb(Sequence *seq, void *arg_pt)
824 {
825         if (seq->clip == (MovieClip *)arg_pt)
826                 seq->clip = NULL;
827         return 1;
828 }
829
830 void BKE_sequencer_clear_movieclip_in_clipboard(MovieClip *clip)
831 {
832         BKE_sequencer_base_recursive_apply(&seqbase_clipboard, clear_movieclip_in_clipboard_cb, clip);
833 }
834
835 static int clear_mask_in_clipboard_cb(Sequence *seq, void *arg_pt)
836 {
837         if (seq->mask == (Mask *)arg_pt)
838                 seq->mask = NULL;
839         return 1;
840 }
841
842 void BKE_sequencer_clear_mask_in_clipboard(Mask *mask)
843 {
844         BKE_sequencer_base_recursive_apply(&seqbase_clipboard, clear_mask_in_clipboard_cb, mask);
845 }
846
847 typedef struct SeqUniqueInfo {
848         Sequence *seq;
849         char name_src[SEQ_NAME_MAXSTR];
850         char name_dest[SEQ_NAME_MAXSTR];
851         int count;
852         int match;
853 } SeqUniqueInfo;
854
855 static void seqbase_unique_name(ListBase *seqbasep, SeqUniqueInfo *sui)
856 {
857         Sequence *seq;
858         for (seq = seqbasep->first; seq; seq = seq->next) {
859                 if (sui->seq != seq && strcmp(sui->name_dest, seq->name + 2) == 0) {
860                         /* SEQ_NAME_MAXSTR - 2 for prefix, -1 for \0, -4 for the number */
861                         BLI_snprintf(sui->name_dest, sizeof(sui->name_dest), "%.59s.%03d",  sui->name_src, sui->count++);
862                         sui->match = 1; /* be sure to re-scan */
863                 }
864         }
865 }
866
867 static int seqbase_unique_name_recursive_cb(Sequence *seq, void *arg_pt)
868 {
869         if (seq->seqbase.first)
870                 seqbase_unique_name(&seq->seqbase, (SeqUniqueInfo *)arg_pt);
871         return 1;
872 }
873
874 void BKE_sequence_base_unique_name_recursive(ListBase *seqbasep, Sequence *seq)
875 {
876         SeqUniqueInfo sui;
877         char *dot;
878         sui.seq = seq;
879         BLI_strncpy(sui.name_src, seq->name + 2, sizeof(sui.name_src));
880         BLI_strncpy(sui.name_dest, seq->name + 2, sizeof(sui.name_dest));
881
882         sui.count = 1;
883         sui.match = 1; /* assume the worst to start the loop */
884
885         /* Strip off the suffix */
886         if ((dot = strrchr(sui.name_src, '.'))) {
887                 *dot = '\0';
888                 dot++;
889
890                 if (*dot)
891                         sui.count = atoi(dot) + 1;
892         }
893
894         while (sui.match) {
895                 sui.match = 0;
896                 seqbase_unique_name(seqbasep, &sui);
897                 BKE_sequencer_base_recursive_apply(seqbasep, seqbase_unique_name_recursive_cb, &sui);
898         }
899
900         BLI_strncpy(seq->name + 2, sui.name_dest, sizeof(seq->name) - 2);
901 }
902
903 static const char *give_seqname_by_type(int type)
904 {
905         switch (type) {
906                 case SEQ_TYPE_META:       return "Meta";
907                 case SEQ_TYPE_IMAGE:      return "Image";
908                 case SEQ_TYPE_SCENE:      return "Scene";
909                 case SEQ_TYPE_MOVIE:      return "Movie";
910                 case SEQ_TYPE_MOVIECLIP:  return "Clip";
911                 case SEQ_TYPE_MASK:       return "Mask";
912                 case SEQ_TYPE_SOUND_RAM:  return "Audio";
913                 case SEQ_TYPE_CROSS:      return "Cross";
914                 case SEQ_TYPE_GAMCROSS:   return "Gamma Cross";
915                 case SEQ_TYPE_ADD:        return "Add";
916                 case SEQ_TYPE_SUB:        return "Sub";
917                 case SEQ_TYPE_MUL:        return "Mul";
918                 case SEQ_TYPE_ALPHAOVER:  return "Alpha Over";
919                 case SEQ_TYPE_ALPHAUNDER: return "Alpha Under";
920                 case SEQ_TYPE_OVERDROP:   return "Over Drop";
921                 case SEQ_TYPE_WIPE:       return "Wipe";
922                 case SEQ_TYPE_GLOW:       return "Glow";
923                 case SEQ_TYPE_TRANSFORM:  return "Transform";
924                 case SEQ_TYPE_COLOR:      return "Color";
925                 case SEQ_TYPE_MULTICAM:   return "Multicam";
926                 case SEQ_TYPE_ADJUSTMENT: return "Adjustment";
927                 case SEQ_TYPE_SPEED:      return "Speed";
928                 default:
929                         return NULL;
930         }
931 }
932
933 const char *BKE_sequence_give_name(Sequence *seq)
934 {
935         const char *name = give_seqname_by_type(seq->type);
936
937         if (!name) {
938                 if (seq->type < SEQ_TYPE_EFFECT) {
939                         return seq->strip->dir;
940                 }
941                 else {
942                         return "Effect";
943                 }
944         }
945         return name;
946 }
947
948 /*********************** DO THE SEQUENCE *************************/
949
950 static void make_black_ibuf(ImBuf *ibuf)
951 {
952         unsigned int *rect;
953         float *rect_float;
954         int tot;
955
956         if (ibuf == NULL || (ibuf->rect == NULL && ibuf->rect_float == NULL)) {
957                 return;
958         }
959
960         tot = ibuf->x * ibuf->y;
961
962         rect = ibuf->rect;
963         rect_float = ibuf->rect_float;
964
965         if (rect) {
966                 memset(rect, 0, tot * sizeof(char) * 4);
967         }
968
969         if (rect_float) {
970                 memset(rect_float, 0, tot * sizeof(float) * 4);
971         }
972 }
973
974 static void multibuf(ImBuf *ibuf, float fmul)
975 {
976         char *rt;
977         float *rt_float;
978
979         int a, mul, icol;
980
981         mul = (int)(256.0f * fmul);
982         rt = (char *)ibuf->rect;
983         rt_float = ibuf->rect_float;
984
985         if (rt) {
986                 a = ibuf->x * ibuf->y;
987                 while (a--) {
988
989                         icol = (mul * rt[0]) >> 8;
990                         if (icol > 254) rt[0] = 255; else rt[0] = icol;
991                         icol = (mul * rt[1]) >> 8;
992                         if (icol > 254) rt[1] = 255; else rt[1] = icol;
993                         icol = (mul * rt[2]) >> 8;
994                         if (icol > 254) rt[2] = 255; else rt[2] = icol;
995                         icol = (mul * rt[3]) >> 8;
996                         if (icol > 254) rt[3] = 255; else rt[3] = icol;
997                         
998                         rt += 4;
999                 }
1000         }
1001         if (rt_float) {
1002                 a = ibuf->x * ibuf->y;
1003                 while (a--) {
1004                         rt_float[0] *= fmul;
1005                         rt_float[1] *= fmul;
1006                         rt_float[2] *= fmul;
1007                         rt_float[3] *= fmul;
1008                         
1009                         rt_float += 4;
1010                 }
1011         }
1012 }
1013
1014 static float give_stripelem_index(Sequence *seq, float cfra)
1015 {
1016         float nr;
1017         int sta = seq->start;
1018         int end = seq->start + seq->len - 1;
1019
1020         if (seq->type & SEQ_TYPE_EFFECT) {
1021                 end = seq->enddisp;
1022         }
1023
1024         if (end < sta) {
1025                 return -1;
1026         }
1027
1028         if (seq->flag & SEQ_REVERSE_FRAMES) {
1029                 /*reverse frame in this sequence */
1030                 if (cfra <= sta) nr = end - sta;
1031                 else if (cfra >= end) nr = 0;
1032                 else nr = end - cfra;
1033         }
1034         else {
1035                 if (cfra <= sta) nr = 0;
1036                 else if (cfra >= end) nr = end - sta;
1037                 else nr = cfra - sta;
1038         }
1039         
1040         if (seq->strobe < 1.0f) seq->strobe = 1.0f;
1041         
1042         if (seq->strobe > 1.0f) {
1043                 nr -= fmodf((double)nr, (double)seq->strobe);
1044         }
1045
1046         return nr;
1047 }
1048
1049 StripElem *BKE_sequencer_give_stripelem(Sequence *seq, int cfra)
1050 {
1051         StripElem *se = seq->strip->stripdata;
1052
1053         if (seq->type == SEQ_TYPE_IMAGE) {
1054                 /* only IMAGE strips use the whole array, MOVIE strips use only the first element,
1055                  * all other strips don't use this...
1056                  */
1057
1058                 int nr = (int) give_stripelem_index(seq, cfra);
1059
1060                 if (nr == -1 || se == NULL)
1061                         return NULL;
1062         
1063                 se += nr + seq->anim_startofs;
1064         }
1065         return se;
1066 }
1067
1068 static int evaluate_seq_frame_gen(Sequence **seq_arr, ListBase *seqbase, int cfra)
1069 {
1070         Sequence *seq;
1071         int totseq = 0;
1072
1073         memset(seq_arr, 0, sizeof(Sequence *) * (MAXSEQ + 1));
1074
1075         seq = seqbase->first;
1076         while (seq) {
1077                 if (seq->startdisp <= cfra && seq->enddisp > cfra) {
1078                         seq_arr[seq->machine] = seq;
1079                         totseq++;
1080                 }
1081                 seq = seq->next;
1082         }
1083
1084         return totseq;
1085 }
1086
1087 int BKE_sequencer_evaluate_frame(Scene *scene, int cfra)
1088 {
1089         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1090         Sequence *seq_arr[MAXSEQ + 1];
1091
1092         if (ed == NULL)
1093                 return 0;
1094
1095         return evaluate_seq_frame_gen(seq_arr, ed->seqbasep, cfra);
1096 }
1097
1098 static int video_seq_is_rendered(Sequence *seq)
1099 {
1100         return (seq && !(seq->flag & SEQ_MUTE) && seq->type != SEQ_TYPE_SOUND_RAM);
1101 }
1102
1103 static int get_shown_sequences(ListBase *seqbasep, int cfra, int chanshown, Sequence **seq_arr_out)
1104 {
1105         Sequence *seq_arr[MAXSEQ + 1];
1106         int b = chanshown;
1107         int cnt = 0;
1108
1109         if (b > MAXSEQ) {
1110                 return 0;
1111         }
1112
1113         if (evaluate_seq_frame_gen(seq_arr, seqbasep, cfra)) {
1114                 if (b == 0) {
1115                         b = MAXSEQ;
1116                 }
1117                 for (; b > 0; b--) {
1118                         if (video_seq_is_rendered(seq_arr[b])) {
1119                                 break;
1120                         }
1121                 }
1122         }
1123         
1124         chanshown = b;
1125
1126         for (; b > 0; b--) {
1127                 if (video_seq_is_rendered(seq_arr[b])) {
1128                         if (seq_arr[b]->blend_mode == SEQ_BLEND_REPLACE) {
1129                                 break;
1130                         }
1131                 }
1132         }
1133
1134         for (; b <= chanshown && b >= 0; b++) {
1135                 if (video_seq_is_rendered(seq_arr[b])) {
1136                         seq_arr_out[cnt++] = seq_arr[b];
1137                 }
1138         }
1139
1140         return cnt;
1141 }
1142
1143 /*********************** proxy management *************************/
1144
1145 typedef struct SeqIndexBuildContext {
1146         struct IndexBuildContext *index_context;
1147
1148         int tc_flags;
1149         int size_flags;
1150         int quality;
1151
1152         Main *bmain;
1153         Scene *scene;
1154         Sequence *seq, *orig_seq;
1155 } SeqIndexBuildContext;
1156
1157 #define PROXY_MAXFILE (2 * FILE_MAXDIR + FILE_MAXFILE)
1158
1159 static IMB_Proxy_Size seq_rendersize_to_proxysize(int size)
1160 {
1161         if (size >= 100) {
1162                 return IMB_PROXY_NONE;
1163         }
1164         if (size >= 99) {
1165                 return IMB_PROXY_100;
1166         }
1167         if (size >= 75) {
1168                 return IMB_PROXY_75;
1169         }
1170         if (size >= 50) {
1171                 return IMB_PROXY_50;
1172         }
1173         return IMB_PROXY_25;
1174 }
1175
1176 static double seq_rendersize_to_scale_factor(int size)
1177 {
1178         if (size >= 99) {
1179                 return 1.0;
1180         }
1181         if (size >= 75) {
1182                 return 0.75;
1183         }
1184         if (size >= 50) {
1185                 return 0.50;
1186         }
1187         return 0.25;
1188 }
1189
1190 static void seq_open_anim_file(Sequence *seq)
1191 {
1192         char name[FILE_MAX];
1193         StripProxy *proxy;
1194
1195         if (seq->anim != NULL) {
1196                 return;
1197         }
1198
1199         BLI_join_dirfile(name, sizeof(name),
1200                          seq->strip->dir, seq->strip->stripdata->name);
1201         BLI_path_abs(name, G.main->name);
1202         
1203         seq->anim = openanim(name, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
1204                              seq->streamindex, seq->strip->colorspace_settings.name);
1205
1206         if (seq->anim == NULL) {
1207                 return;
1208         }
1209
1210         proxy = seq->strip->proxy;
1211
1212         if (proxy == NULL) {
1213                 return;
1214         }
1215
1216         if (seq->flag & SEQ_USE_PROXY_CUSTOM_DIR) {
1217                 char dir[FILE_MAX];
1218                 BLI_strncpy(dir, seq->strip->proxy->dir, sizeof(dir));
1219                 BLI_path_abs(dir, G.main->name);
1220
1221                 IMB_anim_set_index_dir(seq->anim, dir);
1222         }
1223 }
1224
1225
1226 static int seq_proxy_get_fname(Sequence *seq, int cfra, int render_size, char *name)
1227 {
1228         int frameno;
1229         char dir[PROXY_MAXFILE];
1230
1231         if (!seq->strip->proxy) {
1232                 return FALSE;
1233         }
1234
1235         /* MOVIE tracks (only exception: custom files) are now handled 
1236          * internally by ImBuf module for various reasons: proper time code
1237          * support, quicker index build, using one file instead
1238          * of a full directory of jpeg files, etc. Trying to support old
1239          * and new method at once could lead to funny effects, if people
1240          * have both, a directory full of jpeg files and proxy avis, so
1241          * sorry folks, please rebuild your proxies... */
1242
1243         if (seq->flag & (SEQ_USE_PROXY_CUSTOM_DIR | SEQ_USE_PROXY_CUSTOM_FILE)) {
1244                 BLI_strncpy(dir, seq->strip->proxy->dir, sizeof(dir));
1245         }
1246         else if (seq->type == SEQ_TYPE_IMAGE) {
1247                 BLI_snprintf(dir, PROXY_MAXFILE, "%s/BL_proxy", seq->strip->dir);
1248         }
1249         else {
1250                 return FALSE;
1251         }
1252
1253         if (seq->flag & SEQ_USE_PROXY_CUSTOM_FILE) {
1254                 BLI_join_dirfile(name, PROXY_MAXFILE,
1255                                  dir, seq->strip->proxy->file);
1256                 BLI_path_abs(name, G.main->name);
1257
1258                 return TRUE;
1259         }
1260
1261         /* generate a separate proxy directory for each preview size */
1262
1263         if (seq->type == SEQ_TYPE_IMAGE) {
1264                 BLI_snprintf(name, PROXY_MAXFILE, "%s/images/%d/%s_proxy", dir, render_size,
1265                              BKE_sequencer_give_stripelem(seq, cfra)->name);
1266                 frameno = 1;
1267         }
1268         else {
1269                 frameno = (int)give_stripelem_index(seq, cfra) + seq->anim_startofs;
1270                 BLI_snprintf(name, PROXY_MAXFILE, "%s/proxy_misc/%d/####", dir, render_size);
1271         }
1272
1273         BLI_path_abs(name, G.main->name);
1274         BLI_path_frame(name, frameno, 0);
1275
1276         strcat(name, ".jpg");
1277
1278         return TRUE;
1279 }
1280
1281 static ImBuf *seq_proxy_fetch(SeqRenderData context, Sequence *seq, int cfra)
1282 {
1283         char name[PROXY_MAXFILE];
1284         IMB_Proxy_Size psize = seq_rendersize_to_proxysize(context.preview_render_size);
1285         int size_flags;
1286         int render_size = context.preview_render_size;
1287
1288         /* dirty hack to distinguish 100% render size from PROXY_100 */
1289         if (render_size == 99) {
1290                 render_size = 100;
1291         }
1292
1293         if (!(seq->flag & SEQ_USE_PROXY)) {
1294                 return NULL;
1295         }
1296
1297         size_flags = seq->strip->proxy->build_size_flags;
1298
1299         /* only use proxies, if they are enabled (even if present!) */
1300         if (psize == IMB_PROXY_NONE || ((size_flags & psize) != psize)) {
1301                 return NULL;
1302         }
1303
1304         if (seq->flag & SEQ_USE_PROXY_CUSTOM_FILE) {
1305                 int frameno = (int)give_stripelem_index(seq, cfra) + seq->anim_startofs;
1306                 if (seq->strip->proxy->anim == NULL) {
1307                         if (seq_proxy_get_fname(seq, cfra, render_size, name) == 0) {
1308                                 return NULL;
1309                         }
1310
1311                         /* proxies are generated in default color space */
1312                         seq->strip->proxy->anim = openanim(name, IB_rect, 0, NULL);
1313                 }
1314                 if (seq->strip->proxy->anim == NULL) {
1315                         return NULL;
1316                 }
1317  
1318                 seq_open_anim_file(seq);
1319
1320                 frameno = IMB_anim_index_get_frame_index(seq->anim, seq->strip->proxy->tc, frameno);
1321
1322                 return IMB_anim_absolute(seq->strip->proxy->anim, frameno, IMB_TC_NONE, IMB_PROXY_NONE);
1323         }
1324  
1325         if (seq_proxy_get_fname(seq, cfra, render_size, name) == 0) {
1326                 return NULL;
1327         }
1328
1329         if (BLI_exists(name)) {
1330                 ImBuf *ibuf = IMB_loadiffname(name, IB_rect, NULL);
1331
1332                 if (ibuf)
1333                         sequencer_imbuf_assign_spaces(context.scene, ibuf);
1334
1335                 return ibuf;
1336         }
1337         else {
1338                 return NULL;
1339         }
1340 }
1341
1342 static void seq_proxy_build_frame(SeqRenderData context, Sequence *seq, int cfra, int proxy_render_size)
1343 {
1344         char name[PROXY_MAXFILE];
1345         int quality;
1346         int rectx, recty;
1347         int ok;
1348         ImBuf *ibuf;
1349
1350         if (!seq_proxy_get_fname(seq, cfra, proxy_render_size, name)) {
1351                 return;
1352         }
1353
1354         ibuf = seq_render_strip(context, seq, cfra);
1355
1356         rectx = (proxy_render_size * context.scene->r.xsch) / 100;
1357         recty = (proxy_render_size * context.scene->r.ysch) / 100;
1358
1359         if (ibuf->x != rectx || ibuf->y != recty) {
1360                 IMB_scalefastImBuf(ibuf, (short)rectx, (short)recty);
1361         }
1362
1363         /* depth = 32 is intentionally left in, otherwise ALPHA channels
1364          * won't work... */
1365         quality = seq->strip->proxy->quality;
1366         ibuf->ftype = JPG | quality;
1367
1368         /* unsupported feature only confuses other s/w */
1369         if (ibuf->planes == 32)
1370                 ibuf->planes = 24;
1371
1372         BLI_make_existing_file(name);
1373
1374         ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
1375         if (ok == 0) {
1376                 perror(name);
1377         }
1378
1379         IMB_freeImBuf(ibuf);
1380 }
1381
1382 SeqIndexBuildContext *BKE_sequencer_proxy_rebuild_context(Main *bmain, Scene *scene, Sequence *seq)
1383 {
1384         SeqIndexBuildContext *context;
1385         Sequence *nseq;
1386
1387         if (!seq->strip || !seq->strip->proxy) {
1388                 return NULL;
1389         }
1390
1391         if (!(seq->flag & SEQ_USE_PROXY)) {
1392                 return NULL;
1393         }
1394
1395         context = MEM_callocN(sizeof(SeqIndexBuildContext), "seq proxy rebuild context");
1396
1397         nseq = BKE_sequence_dupli_recursive(scene, scene, seq, 0);
1398
1399         context->tc_flags   = nseq->strip->proxy->build_tc_flags;
1400         context->size_flags = nseq->strip->proxy->build_size_flags;
1401         context->quality    = nseq->strip->proxy->quality;
1402
1403         context->bmain = bmain;
1404         context->scene = scene;
1405         context->orig_seq = seq;
1406         context->seq = nseq;
1407
1408         if (nseq->type == SEQ_TYPE_MOVIE) {
1409                 seq_open_anim_file(nseq);
1410
1411                 if (nseq->anim) {
1412                         context->index_context = IMB_anim_index_rebuild_context(nseq->anim,
1413                                 context->tc_flags, context->size_flags, context->quality);
1414                 }
1415         }
1416
1417         return context;
1418 }
1419
1420 void BKE_sequencer_proxy_rebuild(SeqIndexBuildContext *context, short *stop, short *do_update, float *progress)
1421 {
1422         SeqRenderData render_context;
1423         Sequence *seq = context->seq;
1424         Scene *scene = context->scene;
1425         int cfra;
1426
1427         if (seq->type == SEQ_TYPE_MOVIE) {
1428                 if (context->index_context) {
1429                         IMB_anim_index_rebuild(context->index_context, stop, do_update, progress);
1430                 }
1431
1432                 return;
1433         }
1434
1435         if (!(seq->flag & SEQ_USE_PROXY)) {
1436                 return;
1437         }
1438
1439         /* that's why it is called custom... */
1440         if (seq->flag & SEQ_USE_PROXY_CUSTOM_FILE) {
1441                 return;
1442         }
1443
1444         /* fail safe code */
1445
1446         render_context = BKE_sequencer_new_render_data(context->bmain, context->scene,
1447                                             (scene->r.size * (float) scene->r.xsch) / 100.0f + 0.5f,
1448                                             (scene->r.size * (float) scene->r.ysch) / 100.0f + 0.5f, 100);
1449
1450         for (cfra = seq->startdisp + seq->startstill;  cfra < seq->enddisp - seq->endstill; cfra++) {
1451                 if (context->size_flags & IMB_PROXY_25) {
1452                         seq_proxy_build_frame(render_context, seq, cfra, 25);
1453                 }
1454                 if (context->size_flags & IMB_PROXY_50) {
1455                         seq_proxy_build_frame(render_context, seq, cfra, 50);
1456                 }
1457                 if (context->size_flags & IMB_PROXY_75) {
1458                         seq_proxy_build_frame(render_context, seq, cfra, 75);
1459                 }
1460                 if (context->size_flags & IMB_PROXY_100) {
1461                         seq_proxy_build_frame(render_context, seq, cfra, 100);
1462                 }
1463
1464                 *progress = (float) (cfra - seq->startdisp - seq->startstill) / (seq->enddisp - seq->endstill - seq->startdisp - seq->startstill);
1465                 *do_update = TRUE;
1466
1467                 if (*stop || G.is_break)
1468                         break;
1469         }
1470 }
1471
1472 void BKE_sequencer_proxy_rebuild_finish(SeqIndexBuildContext *context, short stop)
1473 {
1474         if (context->index_context) {
1475                 IMB_close_anim_proxies(context->seq->anim);
1476                 IMB_close_anim_proxies(context->orig_seq->anim);
1477                 IMB_anim_index_rebuild_finish(context->index_context, stop);
1478         }
1479
1480         seq_free_sequence_recurse(NULL, context->seq);
1481
1482         MEM_freeN(context);
1483 }
1484
1485 /*********************** color balance *************************/
1486
1487 static StripColorBalance calc_cb(StripColorBalance *cb_)
1488 {
1489         StripColorBalance cb = *cb_;
1490         int c;
1491
1492         for (c = 0; c < 3; c++) {
1493                 cb.lift[c] = 2.0f - cb.lift[c];
1494         }
1495
1496         if (cb.flag & SEQ_COLOR_BALANCE_INVERSE_LIFT) {
1497                 for (c = 0; c < 3; c++) {
1498                         /* tweak to give more subtle results
1499                          * values above 1.0 are scaled */
1500                         if (cb.lift[c] > 1.0f)
1501                                 cb.lift[c] = pow(cb.lift[c] - 1.0f, 2.0) + 1.0;
1502
1503                         cb.lift[c] = 2.0f - cb.lift[c];
1504                 }
1505         }
1506
1507         if (cb.flag & SEQ_COLOR_BALANCE_INVERSE_GAIN) {
1508                 for (c = 0; c < 3; c++) {
1509                         if (cb.gain[c] != 0.0f) {
1510                                 cb.gain[c] = 1.0f / cb.gain[c];
1511                         }
1512                         else {
1513                                 cb.gain[c] = 1000000; /* should be enough :) */
1514                         }
1515                 }
1516         }
1517
1518         if (!(cb.flag & SEQ_COLOR_BALANCE_INVERSE_GAMMA)) {
1519                 for (c = 0; c < 3; c++) {
1520                         if (cb.gamma[c] != 0.0f) {
1521                                 cb.gamma[c] = 1.0f / cb.gamma[c];
1522                         }
1523                         else {
1524                                 cb.gamma[c] = 1000000; /* should be enough :) */
1525                         }
1526                 }
1527         }
1528
1529         return cb;
1530 }
1531
1532 /* note: lift is actually 2-lift */
1533 MINLINE float color_balance_fl(float in, const float lift, const float gain, const float gamma, const float mul)
1534 {
1535         float x = (((in - 1.0f) * lift) + 1.0f) * gain;
1536
1537         /* prevent NaN */
1538         if (x < 0.f)
1539                 x = 0.f;
1540
1541         return powf(x, gamma) * mul;
1542 }
1543
1544 static void make_cb_table_float(float lift, float gain, float gamma,
1545                                 float *table, float mul)
1546 {
1547         int y;
1548
1549         for (y = 0; y < 256; y++) {
1550                 float v = color_balance_fl((float)y * (1.0f / 255.0f), lift, gain, gamma, mul);
1551
1552                 table[y] = v;
1553         }
1554 }
1555
1556 static void color_balance_byte_byte(StripColorBalance *cb_, unsigned char *rect, unsigned char *mask_rect, int width, int height, float mul)
1557 {
1558         //unsigned char cb_tab[3][256];
1559         unsigned char *cp = rect;
1560         unsigned char *e = cp + width * 4 * height;
1561         unsigned char *m = mask_rect;
1562
1563         StripColorBalance cb = calc_cb(cb_);
1564
1565         while (cp < e) {
1566                 float p[4];
1567                 int c;
1568
1569                 straight_uchar_to_premul_float(p, cp);
1570
1571                 for (c = 0; c < 3; c++) {
1572                         float t = color_balance_fl(p[c], cb.lift[c], cb.gain[c], cb.gamma[c], mul);
1573
1574                         if (m) {
1575                                 float m_normal = (float) m[c] / 255.0f;
1576
1577                                 p[c] = p[c] * (1.0f - m_normal) + t * m_normal;
1578                         }
1579                         else
1580                                 p[c] = t;
1581                 }
1582                 
1583                 premul_float_to_straight_uchar(cp, p);
1584
1585                 cp += 4;
1586                 if (m)
1587                         m += 4;
1588         }
1589 }
1590
1591 static void color_balance_byte_float(StripColorBalance *cb_, unsigned char *rect, float *rect_float, unsigned char *mask_rect, int width, int height, float mul)
1592 {
1593         float cb_tab[4][256];
1594         int c, i;
1595         unsigned char *p = rect;
1596         unsigned char *e = p + width * 4 * height;
1597         unsigned char *m = mask_rect;
1598         float *o;
1599         StripColorBalance cb;
1600
1601         o = rect_float;
1602
1603         cb = calc_cb(cb_);
1604
1605         for (c = 0; c < 3; c++) {
1606                 make_cb_table_float(cb.lift[c], cb.gain[c], cb.gamma[c], cb_tab[c], mul);
1607         }
1608
1609         for (i = 0; i < 256; i++) {
1610                 cb_tab[3][i] = ((float)i) * (1.0f / 255.0f);
1611         }
1612
1613         while (p < e) {
1614                 if (m) {
1615                         float t[3] = {m[0] / 255.0f, m[1] / 255.0f, m[2] / 255.0f};
1616
1617                         p[0] = p[0] * (1.0f - t[0]) + t[0] * cb_tab[0][p[0]];
1618                         p[1] = p[1] * (1.0f - t[1]) + t[1] * cb_tab[1][p[1]];
1619                         p[2] = p[2] * (1.0f - t[2]) + t[2] * cb_tab[2][p[2]];
1620
1621                         m += 4;
1622                 }
1623                 else {
1624                         o[0] = cb_tab[0][p[0]];
1625                         o[1] = cb_tab[1][p[1]];
1626                         o[2] = cb_tab[2][p[2]];
1627                 }
1628
1629                 o[3] = cb_tab[3][p[3]];
1630
1631                 p += 4; o += 4;
1632         }
1633 }
1634
1635 static void color_balance_float_float(StripColorBalance *cb_, float *rect_float, float *mask_rect_float, int width, int height, float mul)
1636 {
1637         float *p = rect_float;
1638         float *e = rect_float + width * 4 * height;
1639         float *m = mask_rect_float;
1640         StripColorBalance cb = calc_cb(cb_);
1641
1642         while (p < e) {
1643                 int c;
1644                 for (c = 0; c < 3; c++) {
1645                         float t = color_balance_fl(p[c], cb.lift[c], cb.gain[c], cb.gamma[c], mul);
1646
1647                         if (m)
1648                                 p[c] = p[c] * (1.0f - m[c]) + t * m[c];
1649                         else
1650                                 p[c] = t;
1651                 }
1652
1653                 p += 4;
1654                 if (m)
1655                         m += 4;
1656         }
1657 }
1658
1659 typedef struct ColorBalanceInitData {
1660         StripColorBalance *cb;
1661         ImBuf *ibuf;
1662         float mul;
1663         ImBuf *mask;
1664         short make_float;
1665 } ColorBalanceInitData;
1666
1667 typedef struct ColorBalanceThread {
1668         StripColorBalance *cb;
1669         float mul;
1670
1671         int width, height;
1672
1673         unsigned char *rect, *mask_rect;
1674         float *rect_float, *mask_rect_float;
1675
1676         short make_float;
1677 } ColorBalanceThread;
1678
1679 static void color_balance_init_handle(void *handle_v, int start_line, int tot_line, void *init_data_v)
1680 {
1681         ColorBalanceThread *handle = (ColorBalanceThread *) handle_v;
1682         ColorBalanceInitData *init_data = (ColorBalanceInitData *) init_data_v;
1683         ImBuf *ibuf = init_data->ibuf;
1684         ImBuf *mask = init_data->mask;
1685
1686         int offset = 4 * start_line * ibuf->x;
1687
1688         memset(handle, 0, sizeof(ColorBalanceThread));
1689
1690         handle->cb = init_data->cb;
1691         handle->mul = init_data->mul;
1692         handle->width = ibuf->x;
1693         handle->height = tot_line;
1694         handle->make_float = init_data->make_float;
1695
1696         if (ibuf->rect)
1697                 handle->rect = (unsigned char *) ibuf->rect + offset;
1698
1699         if (ibuf->rect_float)
1700                 handle->rect_float = ibuf->rect_float + offset;
1701
1702         if (mask) {
1703                 if (mask->rect)
1704                         handle->mask_rect = (unsigned char *) mask->rect + offset;
1705
1706                 if (mask->rect_float)
1707                         handle->mask_rect_float = mask->rect_float + offset;
1708         }
1709         else {
1710                 handle->mask_rect = NULL;
1711                 handle->mask_rect_float = NULL;
1712         }
1713 }
1714
1715 static void *color_balance_do_thread(void *thread_data_v)
1716 {
1717         ColorBalanceThread *thread_data = (ColorBalanceThread *) thread_data_v;
1718         StripColorBalance *cb = thread_data->cb;
1719         int width = thread_data->width, height = thread_data->height;
1720         unsigned char *rect = thread_data->rect;
1721         unsigned char *mask_rect = thread_data->mask_rect;
1722         float *rect_float = thread_data->rect_float;
1723         float *mask_rect_float = thread_data->mask_rect_float;
1724         float mul = thread_data->mul;
1725
1726         if (rect_float) {
1727                 color_balance_float_float(cb, rect_float, mask_rect_float, width, height, mul);
1728         }
1729         else if (thread_data->make_float) {
1730                 color_balance_byte_float(cb, rect, rect_float, mask_rect, width, height, mul);
1731         }
1732         else {
1733                 color_balance_byte_byte(cb, rect, mask_rect, width, height, mul);
1734         }
1735
1736         return NULL;
1737 }
1738
1739 ImBuf *BKE_sequencer_render_mask_input(SeqRenderData context, int mask_input_type, Sequence *mask_sequence, Mask *mask_id, int cfra, int make_float)
1740 {
1741         ImBuf *mask_input = NULL;
1742
1743         if (mask_input_type == SEQUENCE_MASK_INPUT_STRIP) {
1744                 if (mask_sequence) {
1745                         mask_input = seq_render_strip(context, mask_sequence, cfra);
1746
1747                         if (make_float) {
1748                                 if (!mask_input->rect_float)
1749                                         IMB_float_from_rect(mask_input);
1750                         }
1751                         else {
1752                                 if (!mask_input->rect)
1753                                         IMB_rect_from_float(mask_input);
1754                         }
1755                 }
1756         }
1757         else if (mask_input_type == SEQUENCE_MASK_INPUT_ID) {
1758                 mask_input = seq_render_mask(context, mask_id, cfra, make_float);
1759         }
1760
1761         return mask_input;
1762 }
1763
1764 void BKE_sequencer_color_balance_apply(StripColorBalance *cb, ImBuf *ibuf, float mul, short make_float, ImBuf *mask_input)
1765 {
1766         ColorBalanceInitData init_data;
1767
1768         if (!ibuf->rect_float && make_float)
1769                 imb_addrectfloatImBuf(ibuf);
1770
1771         init_data.cb = cb;
1772         init_data.ibuf = ibuf;
1773         init_data.mul = mul;
1774         init_data.mask = NULL;
1775         init_data.make_float = make_float;
1776         init_data.mask = mask_input;
1777
1778         IMB_processor_apply_threaded(ibuf->y, sizeof(ColorBalanceThread), &init_data,
1779                                      color_balance_init_handle, color_balance_do_thread);
1780
1781         /* color balance either happens on float buffer or byte buffer, but never on both,
1782          * free byte buffer if there's float buffer since float buffer would be used for
1783          * color balance in favor of byte buffer
1784          */
1785         if (ibuf->rect_float && ibuf->rect)
1786                 imb_freerectImBuf(ibuf);
1787 }
1788
1789 /*
1790  *  input preprocessing for SEQ_TYPE_IMAGE, SEQ_TYPE_MOVIE, SEQ_TYPE_MOVIECLIP and SEQ_TYPE_SCENE
1791  *
1792  *  Do all the things you can't really do afterwards using sequence effects
1793  *  (read: before rescaling to render resolution has been done)
1794  *
1795  *  Order is important!
1796  *
1797  *  - Deinterlace
1798  *  - Crop and transform in image source coordinate space
1799  *  - Flip X + Flip Y (could be done afterwards, backward compatibility)
1800  *  - Promote image to float data (affects pipeline operations afterwards)
1801  *  - Color balance (is most efficient in the byte -> float
1802  *    (future: half -> float should also work fine!)
1803  *    case, if done on load, since we can use lookup tables)
1804  *  - Premultiply
1805  */
1806
1807 int BKE_sequencer_input_have_to_preprocess(SeqRenderData UNUSED(context), Sequence *seq, float UNUSED(cfra))
1808 {
1809         float mul;
1810
1811         if (seq->flag & (SEQ_FILTERY | SEQ_USE_CROP | SEQ_USE_TRANSFORM | SEQ_FLIPX | SEQ_FLIPY | SEQ_MAKE_FLOAT)) {
1812                 return TRUE;
1813         }
1814
1815         mul = seq->mul;
1816
1817         if (seq->blend_mode == SEQ_BLEND_REPLACE) {
1818                 mul *= seq->blend_opacity / 100.0f;
1819         }
1820
1821         if (mul != 1.0f) {
1822                 return TRUE;
1823         }
1824
1825         if (seq->sat != 1.0f) {
1826                 return TRUE;
1827         }
1828
1829         if (seq->modifiers.first) {
1830                 return TRUE;
1831         }
1832                 
1833         return FALSE;
1834 }
1835
1836 static ImBuf *input_preprocess(SeqRenderData context, Sequence *seq, float cfra, ImBuf *ibuf,
1837                                int is_proxy_image, int is_preprocessed)
1838 {
1839         float mul;
1840
1841         ibuf = IMB_makeSingleUser(ibuf);
1842
1843         if ((seq->flag & SEQ_FILTERY) &&
1844             !ELEM(seq->type, SEQ_TYPE_MOVIE, SEQ_TYPE_MOVIECLIP))
1845         {
1846                 IMB_filtery(ibuf);
1847         }
1848
1849         if (seq->flag & (SEQ_USE_CROP | SEQ_USE_TRANSFORM)) {
1850                 StripCrop c = {0};
1851                 StripTransform t = {0};
1852                 int sx, sy, dx, dy;
1853
1854                 if (is_proxy_image) {
1855                         double f = seq_rendersize_to_scale_factor(context.preview_render_size);
1856
1857                         if (f != 1.0) {
1858                                 IMB_scalefastImBuf(ibuf, ibuf->x / f, ibuf->y / f);
1859                         }
1860                 }
1861
1862                 if (seq->flag & SEQ_USE_CROP && seq->strip->crop) {
1863                         c = *seq->strip->crop;
1864                 }
1865                 if (seq->flag & SEQ_USE_TRANSFORM && seq->strip->transform) {
1866                         t = *seq->strip->transform;
1867                 }
1868
1869                 if (is_preprocessed) {
1870                         double xscale = context.scene->r.xsch ? ((double)context.rectx / (double)context.scene->r.xsch) : 1.0;
1871                         double yscale = context.scene->r.ysch ? ((double)context.recty / (double)context.scene->r.ysch) : 1.0;
1872                         if (seq->flag & SEQ_USE_TRANSFORM) {
1873                                 t.xofs *= xscale;
1874                                 t.yofs *= yscale;
1875                         }
1876                         if (seq->flag & SEQ_USE_CROP) {
1877                                 c.left *= xscale;
1878                                 c.right *= xscale;
1879                                 c.top *= yscale;
1880                                 c.bottom *= yscale;
1881                         }
1882                 }
1883
1884                 sx = ibuf->x - c.left - c.right;
1885                 sy = ibuf->y - c.top - c.bottom;
1886
1887                 if (seq->flag & SEQ_USE_TRANSFORM) {
1888                         if (is_preprocessed) {
1889                                 dx = context.rectx;
1890                                 dy = context.recty;
1891                         }
1892                         else {
1893                                 dx = context.scene->r.xsch;
1894                                 dy = context.scene->r.ysch;
1895                         }
1896                 }
1897                 else {
1898                         dx = sx;
1899                         dy = sy;
1900                 }
1901
1902                 if (c.top  + c.bottom >= ibuf->y ||
1903                     c.left + c.right  >= ibuf->x ||
1904                     t.xofs >= dx || t.yofs >= dy)
1905                 {
1906                         make_black_ibuf(ibuf);
1907                 }
1908                 else {
1909                         ImBuf *i = IMB_allocImBuf(dx, dy, 32, ibuf->rect_float ? IB_rectfloat : IB_rect);
1910
1911                         IMB_rectcpy(i, ibuf, t.xofs, t.yofs, c.left, c.bottom, sx, sy);
1912                         sequencer_imbuf_assign_spaces(context.scene, i);
1913
1914                         IMB_freeImBuf(ibuf);
1915
1916                         ibuf = i;
1917                 }
1918         }
1919
1920         if (seq->flag & SEQ_FLIPX) {
1921                 IMB_flipx(ibuf);
1922         }
1923         
1924         if (seq->flag & SEQ_FLIPY) {
1925                 IMB_flipy(ibuf);
1926         }
1927
1928         if (seq->sat != 1.0f) {
1929                 IMB_saturation(ibuf, seq->sat);
1930         }
1931
1932         mul = seq->mul;
1933
1934         if (seq->blend_mode == SEQ_BLEND_REPLACE) {
1935                 mul *= seq->blend_opacity / 100.0f;
1936         }
1937
1938         if (seq->flag & SEQ_MAKE_FLOAT) {
1939                 if (!ibuf->rect_float) {
1940                         BKE_sequencer_imbuf_to_sequencer_space(context.scene, ibuf, TRUE);
1941                 }
1942
1943                 if (ibuf->rect) {
1944                         imb_freerectImBuf(ibuf);
1945                 }
1946         }
1947
1948         if (mul != 1.0f) {
1949                 multibuf(ibuf, mul);
1950         }
1951
1952         if (ibuf->x != context.rectx || ibuf->y != context.recty) {
1953                 if (context.scene->r.mode & R_OSA) {
1954                         IMB_scaleImBuf(ibuf, (short)context.rectx, (short)context.recty);
1955                 }
1956                 else {
1957                         IMB_scalefastImBuf(ibuf, (short)context.rectx, (short)context.recty);
1958                 }
1959         }
1960
1961         if (seq->modifiers.first) {
1962                 ImBuf *ibuf_new = BKE_sequence_modifier_apply_stack(context, seq, ibuf, cfra);
1963
1964                 if (ibuf_new != ibuf) {
1965                         IMB_freeImBuf(ibuf);
1966                         ibuf = ibuf_new;
1967                 }
1968         }
1969
1970         return ibuf;
1971 }
1972
1973 static ImBuf *copy_from_ibuf_still(SeqRenderData context, Sequence *seq, float nr)
1974 {
1975         ImBuf *rval = NULL;
1976         ImBuf *ibuf = NULL;
1977
1978         if (nr == 0) {
1979                 ibuf = BKE_sequencer_cache_get(context, seq, seq->start, SEQ_STRIPELEM_IBUF_STARTSTILL);
1980         }
1981         else if (nr == seq->len - 1) {
1982                 ibuf = BKE_sequencer_cache_get(context, seq, seq->start, SEQ_STRIPELEM_IBUF_ENDSTILL);
1983         }
1984
1985         if (ibuf) {
1986                 rval = IMB_dupImBuf(ibuf);
1987                 IMB_freeImBuf(ibuf);
1988         }
1989
1990         return rval;
1991 }
1992
1993 static void copy_to_ibuf_still(SeqRenderData context, Sequence *seq, float nr, ImBuf *ibuf)
1994 {
1995         /* warning: ibuf may be NULL if the video fails to load */
1996         if (nr == 0 || nr == seq->len - 1) {
1997                 /* we have to store a copy, since the passed ibuf
1998                  * could be preprocessed afterwards (thereby silently
1999                  * changing the cached image... */
2000                 ibuf = IMB_dupImBuf(ibuf);
2001
2002                 if (ibuf) {
2003                         sequencer_imbuf_assign_spaces(context.scene, ibuf);
2004                 }
2005
2006                 if (nr == 0) {
2007                         BKE_sequencer_cache_put(context, seq, seq->start, SEQ_STRIPELEM_IBUF_STARTSTILL, ibuf);
2008                 }
2009
2010                 if (nr == seq->len - 1) {
2011                         BKE_sequencer_cache_put(context, seq, seq->start, SEQ_STRIPELEM_IBUF_ENDSTILL, ibuf);
2012                 }
2013
2014                 IMB_freeImBuf(ibuf);
2015         }
2016 }
2017
2018 /*********************** strip rendering functions  *************************/
2019
2020 typedef struct RenderEffectInitData {
2021         struct SeqEffectHandle *sh;
2022         SeqRenderData context;
2023         Sequence *seq;
2024         float cfra, facf0, facf1;
2025         ImBuf *ibuf1, *ibuf2, *ibuf3;
2026
2027         ImBuf *out;
2028 } RenderEffectInitData;
2029
2030 typedef struct RenderEffectThread {
2031         struct SeqEffectHandle *sh;
2032         SeqRenderData context;
2033         Sequence *seq;
2034         float cfra, facf0, facf1;
2035         ImBuf *ibuf1, *ibuf2, *ibuf3;
2036
2037         ImBuf *out;
2038         int start_line, tot_line;
2039 } RenderEffectThread;
2040
2041 static void render_effect_execute_init_handle(void *handle_v, int start_line, int tot_line, void *init_data_v)
2042 {
2043         RenderEffectThread *handle = (RenderEffectThread *) handle_v;
2044         RenderEffectInitData *init_data = (RenderEffectInitData *) init_data_v;
2045
2046         handle->sh = init_data->sh;
2047         handle->context = init_data->context;
2048         handle->seq = init_data->seq;
2049         handle->cfra = init_data->cfra;
2050         handle->facf0 = init_data->facf0;
2051         handle->facf1 = init_data->facf1;
2052         handle->ibuf1 = init_data->ibuf1;
2053         handle->ibuf2 = init_data->ibuf2;
2054         handle->ibuf3 = init_data->ibuf3;
2055         handle->out = init_data->out;
2056
2057         handle->start_line = start_line;
2058         handle->tot_line = tot_line;
2059 }
2060
2061 static void *render_effect_execute_do_thread(void *thread_data_v)
2062 {
2063         RenderEffectThread *thread_data = (RenderEffectThread *) thread_data_v;
2064
2065         thread_data->sh->execute_slice(thread_data->context, thread_data->seq, thread_data->cfra,
2066                                        thread_data->facf0, thread_data->facf1, thread_data->ibuf1,
2067                                        thread_data->ibuf2, thread_data->ibuf3, thread_data->start_line,
2068                                        thread_data->tot_line, thread_data->out);
2069
2070         return NULL;
2071 }
2072
2073 static ImBuf *seq_render_effect_execute_threaded(struct SeqEffectHandle *sh, SeqRenderData context, Sequence *seq,
2074                                                  float cfra, float facf0, float facf1,
2075                                                  ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *ibuf3)
2076 {
2077         RenderEffectInitData init_data;
2078         ImBuf *out = sh->init_execution(context, ibuf1, ibuf2, ibuf3);
2079
2080         init_data.sh = sh;
2081         init_data.context = context;
2082         init_data.seq = seq;
2083         init_data.cfra = cfra;
2084         init_data.facf0 = facf0;
2085         init_data.facf1 = facf1;
2086         init_data.ibuf1 = ibuf1;
2087         init_data.ibuf2 = ibuf2;
2088         init_data.ibuf3 = ibuf3;
2089         init_data.out = out;
2090
2091         IMB_processor_apply_threaded(out->y, sizeof(RenderEffectThread), &init_data,
2092                                  render_effect_execute_init_handle, render_effect_execute_do_thread);
2093
2094         return out;
2095 }
2096
2097 static ImBuf *seq_render_effect_strip_impl(SeqRenderData context, Sequence *seq, float cfra)
2098 {
2099         float fac, facf;
2100         int early_out;
2101         int i;
2102         struct SeqEffectHandle sh = BKE_sequence_get_effect(seq);
2103         FCurve *fcu = NULL;
2104         ImBuf *ibuf[3];
2105         Sequence *input[3];
2106         ImBuf *out = NULL;
2107
2108         ibuf[0] = ibuf[1] = ibuf[2] = NULL;
2109
2110         input[0] = seq->seq1; input[1] = seq->seq2; input[2] = seq->seq3;
2111
2112         if (!sh.execute && !(sh.execute_slice && sh.init_execution)) {
2113                 /* effect not supported in this version... */
2114                 out = IMB_allocImBuf(context.rectx, context.recty, 32, IB_rect);
2115                 return out;
2116         }
2117
2118         if (seq->flag & SEQ_USE_EFFECT_DEFAULT_FADE) {
2119                 sh.get_default_fac(seq, cfra, &fac, &facf);
2120                 
2121                 if ((context.scene->r.mode & R_FIELDS) == 0)
2122                         facf = fac;
2123         }
2124         else {
2125                 fcu = id_data_find_fcurve(&context.scene->id, seq, &RNA_Sequence, "effect_fader", 0, NULL);
2126                 if (fcu) {
2127                         fac = facf = evaluate_fcurve(fcu, cfra);
2128                         if (context.scene->r.mode & R_FIELDS) {
2129                                 facf = evaluate_fcurve(fcu, cfra + 0.5f);
2130                         }
2131                 }
2132                 else {
2133                         fac = facf = seq->effect_fader;
2134                 }
2135         }
2136
2137         early_out = sh.early_out(seq, fac, facf);
2138
2139         switch (early_out) {
2140                 case EARLY_NO_INPUT:
2141                         out = sh.execute(context, seq, cfra, fac, facf, NULL, NULL, NULL);
2142                         break;
2143                 case EARLY_DO_EFFECT:
2144                         for (i = 0; i < 3; i++) {
2145                                 if (input[i])
2146                                         ibuf[i] = seq_render_strip(context, input[i], cfra);
2147                         }
2148
2149                         if (ibuf[0] && ibuf[1]) {
2150                                 if (sh.multithreaded)
2151                                         out = seq_render_effect_execute_threaded(&sh, context, seq, cfra, fac, facf, ibuf[0], ibuf[1], ibuf[2]);
2152                                 else
2153                                         out = sh.execute(context, seq, cfra, fac, facf, ibuf[0], ibuf[1], ibuf[2]);
2154                         }
2155                         break;
2156                 case EARLY_USE_INPUT_1:
2157                         if (input[0]) {
2158                                 ibuf[0] = seq_render_strip(context, input[0], cfra);
2159                         }
2160                         if (ibuf[0]) {
2161                                 if (BKE_sequencer_input_have_to_preprocess(context, seq, cfra)) {
2162                                         out = IMB_dupImBuf(ibuf[0]);
2163                                 }
2164                                 else {
2165                                         out = ibuf[0];
2166                                         IMB_refImBuf(out);
2167                                 }
2168                         }
2169                         break;
2170                 case EARLY_USE_INPUT_2:
2171                         if (input[1]) {
2172                                 ibuf[1] = seq_render_strip(context, input[1], cfra);
2173                         }
2174                         if (ibuf[1]) {
2175                                 if (BKE_sequencer_input_have_to_preprocess(context, seq, cfra)) {
2176                                         out = IMB_dupImBuf(ibuf[1]);
2177                                 }
2178                                 else {
2179                                         out = ibuf[1];
2180                                         IMB_refImBuf(out);
2181                                 }
2182                         }
2183                         break;
2184         }
2185
2186         for (i = 0; i < 3; i++) {
2187                 IMB_freeImBuf(ibuf[i]);
2188         }
2189
2190         if (out == NULL) {
2191                 out = IMB_allocImBuf(context.rectx, context.recty, 32, IB_rect);
2192         }
2193
2194         return out;
2195 }
2196
2197 static ImBuf *seq_render_movieclip_strip(SeqRenderData context, Sequence *seq, float nr)
2198 {
2199         ImBuf *ibuf = NULL;
2200         MovieClipUser user;
2201         float tloc[2], tscale, tangle;
2202
2203         if (!seq->clip) {
2204                 return NULL;
2205         }
2206
2207         memset(&user, 0, sizeof(MovieClipUser));
2208         
2209         BKE_movieclip_user_set_frame(&user, nr + seq->anim_startofs + seq->clip->start_frame);
2210
2211         user.render_size = MCLIP_PROXY_RENDER_SIZE_FULL;
2212
2213         switch (seq_rendersize_to_proxysize(context.preview_render_size)) {
2214                 case IMB_PROXY_NONE:
2215                         user.render_size = MCLIP_PROXY_RENDER_SIZE_FULL;
2216                         break;
2217                 case IMB_PROXY_100:
2218                         user.render_size = MCLIP_PROXY_RENDER_SIZE_100;
2219                         break;
2220                 case IMB_PROXY_75:
2221                         user.render_size = MCLIP_PROXY_RENDER_SIZE_75;
2222                         break;
2223                 case IMB_PROXY_50:
2224                         user.render_size = MCLIP_PROXY_RENDER_SIZE_50;
2225                         break;
2226                 case IMB_PROXY_25:
2227                         user.render_size = MCLIP_PROXY_RENDER_SIZE_25;
2228                         break;
2229         }
2230
2231         if (seq->clip_flag & SEQ_MOVIECLIP_RENDER_UNDISTORTED) {
2232                 user.render_flag = MCLIP_PROXY_RENDER_UNDISTORT;
2233         }
2234
2235         if (seq->clip_flag & SEQ_MOVIECLIP_RENDER_STABILIZED) {
2236                 ibuf = BKE_movieclip_get_stable_ibuf(seq->clip, &user, tloc, &tscale, &tangle, 0);
2237         }
2238         else {
2239                 ibuf = BKE_movieclip_get_ibuf_flag(seq->clip, &user, 0, MOVIECLIP_CACHE_SKIP);
2240         }
2241
2242         return ibuf;
2243 }
2244
2245
2246 static ImBuf *seq_render_mask(SeqRenderData context, Mask *mask, float nr, short make_float)
2247 {
2248         /* TODO - add option to rasterize to alpha imbuf? */
2249         ImBuf *ibuf = NULL;
2250         float *maskbuf;
2251         int i;
2252
2253         if (!mask) {
2254                 return NULL;
2255         }
2256         else {
2257                 Mask *mask_temp;
2258                 MaskRasterHandle *mr_handle;
2259
2260                 mask_temp = BKE_mask_copy_nolib(mask);
2261
2262                 BKE_mask_evaluate(mask_temp, mask->sfra + nr, TRUE);
2263
2264                 maskbuf = MEM_mallocN(sizeof(float) * context.rectx * context.recty, __func__);
2265
2266                 mr_handle = BKE_maskrasterize_handle_new();
2267
2268                 BKE_maskrasterize_handle_init(mr_handle, mask_temp, context.rectx, context.recty, TRUE, TRUE, TRUE);
2269
2270                 BKE_mask_free_nolib(mask_temp);
2271                 MEM_freeN(mask_temp);
2272
2273                 BKE_maskrasterize_buffer(mr_handle, context.rectx, context.recty, maskbuf);
2274
2275                 BKE_maskrasterize_handle_free(mr_handle);
2276         }
2277
2278
2279         if (make_float) {
2280                 /* pixels */
2281                 float *fp_src;
2282                 float *fp_dst;
2283
2284                 ibuf = IMB_allocImBuf(context.rectx, context.recty, 32, IB_rectfloat);
2285
2286                 fp_src = maskbuf;
2287                 fp_dst = ibuf->rect_float;
2288                 i = context.rectx * context.recty;
2289                 while (--i) {
2290                         fp_dst[0] = fp_dst[1] = fp_dst[2] = *fp_src;
2291                         fp_dst[3] = 1.0f;
2292
2293                         fp_src += 1;
2294                         fp_dst += 4;
2295                 }
2296         }
2297         else {
2298                 /* pixels */
2299                 float *fp_src;
2300                 unsigned char *ub_dst;
2301
2302                 ibuf = IMB_allocImBuf(context.rectx, context.recty, 32, IB_rect);
2303
2304                 fp_src = maskbuf;
2305                 ub_dst = (unsigned char *)ibuf->rect;
2306                 i = context.rectx * context.recty;
2307                 while (--i) {
2308                         ub_dst[0] = ub_dst[1] = ub_dst[2] = (unsigned char)(*fp_src * 255.0f); /* already clamped */
2309                         ub_dst[3] = 255;
2310
2311                         fp_src += 1;
2312                         ub_dst += 4;
2313                 }
2314         }
2315
2316         MEM_freeN(maskbuf);
2317
2318         return ibuf;
2319 }
2320
2321 static ImBuf *seq_render_mask_strip(SeqRenderData context, Sequence *seq, float nr)
2322 {
2323         short make_float = seq->flag & SEQ_MAKE_FLOAT;
2324
2325         return seq_render_mask(context, seq->mask, nr, make_float);
2326 }
2327
2328 static ImBuf *seq_render_scene_strip(SeqRenderData context, Sequence *seq, float nr)
2329 {
2330         ImBuf *ibuf = NULL;
2331         float frame;
2332         float oldcfra;
2333         Object *camera;
2334         ListBase oldmarkers;
2335         
2336         /* Old info:
2337          * Hack! This function can be called from do_render_seq(), in that case
2338          * the seq->scene can already have a Render initialized with same name,
2339          * so we have to use a default name. (compositor uses scene name to
2340          * find render).
2341          * However, when called from within the UI (image preview in sequencer)
2342          * we do want to use scene Render, that way the render result is defined
2343          * for display in render/imagewindow
2344          *
2345          * Hmm, don't see, why we can't do that all the time,
2346          * and since G.is_rendering is uhm, gone... (Peter)
2347          */
2348
2349         /* New info:
2350          * Using the same name for the renders works just fine as the do_render_seq()
2351          * render is not used while the scene strips are rendered.
2352          *
2353          * However rendering from UI (through sequencer_preview_area_draw) can crash in
2354          * very many cases since other renders (material preview, an actual render etc.)
2355          * can be started while this sequence preview render is running. The only proper
2356          * solution is to make the sequencer preview render a proper job, which can be
2357          * stopped when needed. This would also give a nice progress bar for the preview
2358          * space so that users know there's something happening.
2359          *
2360          * As a result the active scene now only uses OpenGL rendering for the sequencer
2361          * preview. This is far from nice, but is the only way to prevent crashes at this
2362          * time.
2363          *
2364          * -jahka
2365          */
2366
2367         const short is_rendering = G.is_rendering;
2368         const short is_background = G.background;
2369         const int do_seq_gl = G.is_rendering ?
2370                     0 /* (context.scene->r.seq_flag & R_SEQ_GL_REND) */ :
2371                     (context.scene->r.seq_flag & R_SEQ_GL_PREV);
2372         int do_seq;
2373         // int have_seq = FALSE;  /* UNUSED */
2374         int have_comp = FALSE;
2375         Scene *scene;
2376         int is_thread_main = BLI_thread_is_main();
2377
2378         /* don't refer to seq->scene above this point!, it can be NULL */
2379         if (seq->scene == NULL) {
2380                 return NULL;
2381         }
2382
2383         scene = seq->scene;
2384         frame = scene->r.sfra + nr + seq->anim_startofs;
2385
2386         // have_seq = (scene->r.scemode & R_DOSEQ) && scene->ed && scene->ed->seqbase.first;  /* UNUSED */
2387         have_comp = (scene->r.scemode & R_DOCOMP) && scene->use_nodes && scene->nodetree;
2388
2389         oldcfra = scene->r.cfra;
2390         scene->r.cfra = frame;
2391
2392         if (seq->scene_camera) {
2393                 camera = seq->scene_camera;
2394         }
2395         else {
2396                 BKE_scene_camera_switch_update(scene);
2397                 camera = scene->camera;
2398         }
2399
2400         if (have_comp == FALSE && camera == NULL) {
2401                 scene->r.cfra = oldcfra;
2402                 return NULL;
2403         }
2404
2405         /* prevent eternal loop */
2406         do_seq = context.scene->r.scemode & R_DOSEQ;
2407         scene->r.scemode &= ~R_DOSEQ;
2408         
2409 #ifdef DURIAN_CAMERA_SWITCH
2410         /* stooping to new low's in hackyness :( */
2411         oldmarkers = scene->markers;
2412         scene->markers.first = scene->markers.last = NULL;
2413 #else
2414         (void)oldmarkers;
2415 #endif
2416
2417         if ((sequencer_view3d_cb && do_seq_gl && camera) && is_thread_main) {
2418                 char err_out[256] = "unknown";
2419                 /* for old scened this can be uninitialized,
2420                  * should probably be added to do_versions at some point if the functionality stays */
2421                 if (context.scene->r.seq_prev_type == 0)
2422                         context.scene->r.seq_prev_type = 3 /* == OB_SOLID */;
2423
2424                 /* opengl offscreen render */
2425                 BKE_scene_update_for_newframe(context.bmain, scene, scene->lay);
2426                 ibuf = sequencer_view3d_cb(scene, camera, context.rectx, context.recty, IB_rect,
2427                                            context.scene->r.seq_prev_type, context.scene->r.seq_flag & R_SEQ_SOLID_TEX,
2428                                            TRUE, scene->r.alphamode, err_out);
2429                 if (ibuf == NULL) {
2430                         fprintf(stderr, "seq_render_scene_strip failed to get opengl buffer: %s\n", err_out);
2431                 }
2432         }
2433         else {
2434                 Render *re = RE_GetRender(scene->id.name);
2435                 RenderResult rres;
2436
2437                 /* XXX: this if can be removed when sequence preview rendering uses the job system
2438                  *
2439                  * disable rendered preview for sequencer while rendering -- it's very much possible
2440                  * that preview render will went into conflict with final render
2441                  *
2442                  * When rendering from command line renderer is called from main thread, in this
2443                  * case it's always safe to render scene here
2444                  */
2445                 if (!is_thread_main || is_rendering == FALSE || is_background) {
2446                         if (re == NULL)
2447                                 re = RE_NewRender(scene->id.name);
2448                         
2449                         BKE_scene_update_for_newframe(context.bmain, scene, scene->lay);
2450                         RE_BlenderFrame(re, context.bmain, scene, NULL, camera, scene->lay, frame, FALSE);
2451
2452                         /* restore previous state after it was toggled on & off by RE_BlenderFrame */
2453                         G.is_rendering = is_rendering;
2454                 }
2455                 
2456                 RE_AcquireResultImage(re, &rres);
2457                 
2458                 if (rres.rectf) {
2459                         ibuf = IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rectfloat);
2460                         memcpy(ibuf->rect_float, rres.rectf, 4 * sizeof(float) * rres.rectx * rres.recty);
2461                         if (rres.rectz) {
2462                                 addzbuffloatImBuf(ibuf);
2463                                 memcpy(ibuf->zbuf_float, rres.rectz, sizeof(float) * rres.rectx * rres.recty);
2464                         }
2465
2466                         /* float buffers in the sequencer are not linear */
2467                         BKE_sequencer_imbuf_to_sequencer_space(context.scene, ibuf, FALSE);
2468                 }
2469                 else if (rres.rect32) {
2470                         ibuf = IMB_allocImBuf(rres.rectx, rres.recty, 32, IB_rect);
2471                         memcpy(ibuf->rect, rres.rect32, 4 * rres.rectx * rres.recty);
2472                 }
2473                 
2474                 RE_ReleaseResultImage(re);
2475                 
2476                 // BIF_end_render_callbacks();
2477         }
2478         
2479         /* restore */
2480         scene->r.scemode |= do_seq;
2481         
2482         scene->r.cfra = oldcfra;
2483
2484         if (frame != oldcfra)
2485                 BKE_scene_update_for_newframe(context.bmain, scene, scene->lay);
2486         
2487 #ifdef DURIAN_CAMERA_SWITCH
2488         /* stooping to new low's in hackyness :( */
2489         scene->markers = oldmarkers;
2490 #endif
2491
2492         return ibuf;
2493 }
2494
2495 static ImBuf *do_render_strip_uncached(SeqRenderData context, Sequence *seq, float cfra)
2496 {
2497         ImBuf *ibuf = NULL;
2498         float nr = give_stripelem_index(seq, cfra);
2499         int type = (seq->type & SEQ_TYPE_EFFECT && seq->type != SEQ_TYPE_SPEED) ? SEQ_TYPE_EFFECT : seq->type;
2500         int use_preprocess = BKE_sequencer_input_have_to_preprocess(context, seq, cfra);
2501         char name[FILE_MAX];
2502
2503         switch (type) {
2504                 case SEQ_TYPE_META:
2505                 {
2506                         ImBuf *meta_ibuf = NULL;
2507
2508                         if (seq->seqbase.first)
2509                                 meta_ibuf = seq_render_strip_stack(context, &seq->seqbase, seq->start + nr, 0);
2510
2511                         if (meta_ibuf) {
2512                                 ibuf = meta_ibuf;
2513                                 if (ibuf && use_preprocess) {
2514                                         ImBuf *i = IMB_dupImBuf(ibuf);
2515
2516                                         IMB_freeImBuf(ibuf);
2517
2518                                         ibuf = i;
2519                                 }
2520                         }
2521
2522                         break;
2523                 }
2524
2525                 case SEQ_TYPE_SPEED:
2526                 {
2527                         ImBuf *child_ibuf = NULL;
2528
2529                         float f_cfra;
2530                         SpeedControlVars *s = (SpeedControlVars *)seq->effectdata;
2531
2532                         BKE_sequence_effect_speed_rebuild_map(context.scene, seq, 0);
2533
2534                         /* weeek! */
2535                         f_cfra = seq->start + s->frameMap[(int)nr];
2536
2537                         child_ibuf = seq_render_strip(context, seq->seq1, f_cfra);
2538
2539                         if (child_ibuf) {
2540                                 ibuf = child_ibuf;
2541                                 if (ibuf && use_preprocess) {
2542                                         ImBuf *i = IMB_dupImBuf(ibuf);
2543
2544                                         IMB_freeImBuf(ibuf);
2545
2546                                         ibuf = i;
2547                                 }
2548                         }
2549                         break;
2550                 }
2551
2552                 case SEQ_TYPE_EFFECT:
2553                 {
2554                         ibuf = seq_render_effect_strip_impl(context, seq, seq->start + nr);
2555                         break;
2556                 }
2557
2558                 case SEQ_TYPE_IMAGE:
2559                 {
2560                         StripElem *s_elem = BKE_sequencer_give_stripelem(seq, cfra);
2561                         int flag;
2562
2563                         if (s_elem) {
2564                                 BLI_join_dirfile(name, sizeof(name), seq->strip->dir, s_elem->name);
2565                                 BLI_path_abs(name, G.main->name);
2566                         }
2567
2568                         flag = IB_rect;
2569                         if (seq->alpha_mode == SEQ_ALPHA_PREMUL)
2570                                 flag |= IB_alphamode_premul;
2571
2572                         if (s_elem && (ibuf = IMB_loadiffname(name, flag, seq->strip->colorspace_settings.name))) {
2573                                 /* we don't need both (speed reasons)! */
2574                                 if (ibuf->rect_float && ibuf->rect)
2575                                         imb_freerectImBuf(ibuf);
2576
2577                                 /* all sequencer color is done in SRGB space, linear gives odd crossfades */
2578                                 BKE_sequencer_imbuf_to_sequencer_space(context.scene, ibuf, FALSE);
2579
2580                                 copy_to_ibuf_still(context, seq, nr, ibuf);
2581
2582                                 s_elem->orig_width  = ibuf->x;
2583                                 s_elem->orig_height = ibuf->y;
2584                         }
2585                         break;
2586                 }
2587
2588                 case SEQ_TYPE_MOVIE:
2589                 {
2590                         seq_open_anim_file(seq);
2591
2592                         if (seq->anim) {
2593                                 IMB_anim_set_preseek(seq->anim, seq->anim_preseek);
2594
2595                                 ibuf = IMB_anim_absolute(seq->anim, nr + seq->anim_startofs,
2596                                                          seq->strip->proxy ? seq->strip->proxy->tc : IMB_TC_RECORD_RUN,
2597                                                          seq_rendersize_to_proxysize(context.preview_render_size));
2598
2599                                 /* we don't need both (speed reasons)! */
2600                                 if (ibuf && ibuf->rect_float && ibuf->rect)
2601                                         imb_freerectImBuf(ibuf);
2602                                 if (ibuf) {
2603                                         seq->strip->stripdata->orig_width = ibuf->x;
2604                                         seq->strip->stripdata->orig_height = ibuf->y;
2605                                 }
2606                         }
2607                         copy_to_ibuf_still(context, seq, nr, ibuf);
2608                         break;
2609                 }
2610
2611                 case SEQ_TYPE_SCENE:
2612                 {
2613                         /* scene can be NULL after deletions */
2614                         ibuf = seq_render_scene_strip(context, seq, nr);
2615
2616                         /* Scene strips update all animation, so we need to restore original state.*/
2617                         BKE_animsys_evaluate_all_animation(context.bmain, context.scene, cfra);
2618
2619                         copy_to_ibuf_still(context, seq, nr, ibuf);
2620                         break;
2621                 }
2622
2623                 case SEQ_TYPE_MOVIECLIP:
2624                 {
2625                         ibuf = seq_render_movieclip_strip(context, seq, nr);
2626
2627                         if (ibuf) {
2628                                 /* duplicate frame so movie cache wouldn't be confused by sequencer's stuff */
2629                                 ImBuf *i = IMB_dupImBuf(ibuf);
2630                                 IMB_freeImBuf(ibuf);
2631                                 ibuf = i;
2632
2633                                 if (ibuf->rect_float)
2634                                         BKE_sequencer_imbuf_to_sequencer_space(context.scene, ibuf, FALSE);
2635
2636                                 copy_to_ibuf_still(context, seq, nr, ibuf);
2637                         }
2638
2639                         break;
2640                 }
2641
2642                 case SEQ_TYPE_MASK:
2643                 {
2644                         /* ibuf is alwats new */
2645                         ibuf = seq_render_mask_strip(context, seq, nr);
2646
2647                         copy_to_ibuf_still(context, seq, nr, ibuf);
2648                         break;
2649                 }
2650         }
2651
2652         if (ibuf)
2653                 sequencer_imbuf_assign_spaces(context.scene, ibuf);
2654
2655         return ibuf;
2656 }
2657
2658 static ImBuf *seq_render_strip(SeqRenderData context, Sequence *seq, float cfra)
2659 {
2660         ImBuf *ibuf = NULL;
2661         int use_preprocess = FALSE;
2662         int is_proxy_image = FALSE;
2663         float nr = give_stripelem_index(seq, cfra);
2664         /* all effects are handled similarly with the exception of speed effect */
2665         int type = (seq->type & SEQ_TYPE_EFFECT && seq->type != SEQ_TYPE_SPEED) ? SEQ_TYPE_EFFECT : seq->type;
2666         int is_preprocessed = !ELEM3(type, SEQ_TYPE_IMAGE, SEQ_TYPE_MOVIE, SEQ_TYPE_SCENE);
2667
2668         ibuf = BKE_sequencer_cache_get(context, seq, cfra, SEQ_STRIPELEM_IBUF);
2669
2670         if (ibuf == NULL) {
2671                 if (ibuf == NULL)
2672                         ibuf = copy_from_ibuf_still(context, seq, nr);
2673
2674                 if (ibuf == NULL) {
2675                         ibuf = BKE_sequencer_preprocessed_cache_get(context, seq, cfra, SEQ_STRIPELEM_IBUF);
2676
2677                         if (ibuf == NULL) {
2678                                 /* MOVIECLIPs have their own proxy management */
2679                                 if (ibuf == NULL && seq->type != SEQ_TYPE_MOVIECLIP) {
2680                                         ibuf = seq_proxy_fetch(context, seq, cfra);
2681                                         is_proxy_image = (ibuf != NULL);
2682                                 }
2683
2684                                 if (ibuf == NULL)
2685                                         ibuf = do_render_strip_uncached(context, seq, cfra);
2686
2687                                 if (ibuf)
2688                                         BKE_sequencer_preprocessed_cache_put(context, seq, cfra, SEQ_STRIPELEM_IBUF, ibuf);
2689                         }
2690                 }
2691
2692                 if (ibuf)
2693                         use_preprocess = BKE_sequencer_input_have_to_preprocess(context, seq, cfra);
2694         }
2695         else {
2696                 /* currently, we cache preprocessed images in SEQ_STRIPELEM_IBUF,
2697                  * but not(!) on SEQ_STRIPELEM_IBUF_ENDSTILL and ..._STARTSTILL
2698                  * so, no need in check for preprocess here
2699                  */
2700         }
2701
2702         if (ibuf == NULL) {
2703                 ibuf = IMB_allocImBuf(context.rectx, context.recty, 32, IB_rect);
2704                 sequencer_imbuf_assign_spaces(context.scene, ibuf);
2705         }
2706
2707         if (ibuf->x != context.rectx || ibuf->y != context.recty)
2708                 use_preprocess = TRUE;
2709
2710         if (use_preprocess)
2711                 ibuf = input_preprocess(context, seq, cfra, ibuf, is_proxy_image, is_preprocessed);
2712
2713         BKE_sequencer_cache_put(context, seq, cfra, SEQ_STRIPELEM_IBUF, ibuf);
2714
2715         return ibuf;
2716 }
2717
2718 /*********************** strip stack rendering functions *************************/
2719
2720 static int seq_must_swap_input_in_blend_mode(Sequence *seq)
2721 {
2722         int swap_input = FALSE;
2723
2724         /* bad hack, to fix crazy input ordering of 
2725          * those two effects */
2726
2727         if (ELEM3(seq->blend_mode, SEQ_TYPE_ALPHAOVER, SEQ_TYPE_ALPHAUNDER, SEQ_TYPE_OVERDROP)) {
2728                 swap_input = TRUE;
2729         }
2730         
2731         return swap_input;
2732 }
2733
2734 static int seq_get_early_out_for_blend_mode(Sequence *seq)
2735 {
2736         struct SeqEffectHandle sh = BKE_sequence_get_blend(seq);
2737         float facf = seq->blend_opacity / 100.0f;
2738         int early_out = sh.early_out(seq, facf, facf);
2739         
2740         if (ELEM(early_out, EARLY_DO_EFFECT, EARLY_NO_INPUT)) {
2741                 return early_out;
2742         }
2743
2744         if (seq_must_swap_input_in_blend_mode(seq)) {
2745                 if (early_out == EARLY_USE_INPUT_2) {
2746                         return EARLY_USE_INPUT_1;
2747                 }
2748                 else if (early_out == EARLY_USE_INPUT_1) {
2749                         return EARLY_USE_INPUT_2;
2750                 }
2751         }
2752         return early_out;
2753 }
2754
2755 static ImBuf *seq_render_strip_stack(SeqRenderData context, ListBase *seqbasep, float cfra, int chanshown)
2756 {
2757         Sequence *seq_arr[MAXSEQ + 1];
2758         int count;
2759         int i;
2760         ImBuf *out = NULL;
2761
2762         count = get_shown_sequences(seqbasep, cfra, chanshown, (Sequence **)&seq_arr);
2763
2764         if (count == 0) {
2765                 return NULL;
2766         }
2767
2768 #if 0 /* commentind since this breaks keyframing, since it resets the value on draw */
2769         if (scene->r.cfra != cfra) {
2770                 /* XXX for prefetch and overlay offset!..., very bad!!! */
2771                 AnimData *adt = BKE_animdata_from_id(&scene->id);
2772                 BKE_animsys_evaluate_animdata(scene, &scene->id, adt, cfra, ADT_RECALC_ANIM);
2773         }
2774 #endif
2775
2776         out = BKE_sequencer_cache_get(context, seq_arr[count - 1],  cfra, SEQ_STRIPELEM_IBUF_COMP);
2777
2778         if (out) {
2779                 return out;
2780         }
2781         
2782         if (count == 1) {
2783                 out = seq_render_strip(context, seq_arr[0], cfra);
2784
2785                 BKE_sequencer_cache_put(context, seq_arr[0], cfra, SEQ_STRIPELEM_IBUF_COMP, out);
2786
2787                 return out;
2788         }
2789
2790         for (i = count - 1; i >= 0; i--) {
2791                 int early_out;
2792                 Sequence *seq = seq_arr[i];
2793
2794                 out = BKE_sequencer_cache_get(context, seq, cfra, SEQ_STRIPELEM_IBUF_COMP);
2795
2796                 if (out) {
2797                         break;
2798                 }
2799                 if (seq->blend_mode == SEQ_BLEND_REPLACE) {
2800                         out = seq_render_strip(context, seq, cfra);
2801                         break;
2802                 }
2803
2804                 early_out = seq_get_early_out_for_blend_mode(seq);
2805
2806                 switch (early_out) {
2807                         case EARLY_NO_INPUT:
2808                         case EARLY_USE_INPUT_2:
2809                                 out = seq_render_strip(context, seq, cfra);
2810                                 break;
2811                         case EARLY_USE_INPUT_1:
2812                                 if (i == 0) {
2813                                         out = IMB_allocImBuf(context.rectx, context.recty, 32, IB_rect);
2814                                 }
2815                                 break;
2816                         case EARLY_DO_EFFECT:
2817                                 if (i == 0) {
2818                                         out = seq_render_strip(context, seq, cfra);
2819                                 }
2820
2821                                 break;
2822                 }
2823                 if (out) {
2824                         break;
2825                 }
2826         }
2827
2828         BKE_sequencer_cache_put(context, seq_arr[i], cfra, SEQ_STRIPELEM_IBUF_COMP, out);
2829
2830         i++;
2831
2832         for (; i < count; i++) {
2833                 Sequence *seq = seq_arr[i];
2834
2835                 if (seq_get_early_out_for_blend_mode(seq) == EARLY_DO_EFFECT) {
2836                         struct SeqEffectHandle sh = BKE_sequence_get_blend(seq);
2837                         ImBuf *ibuf1 = out;
2838                         ImBuf *ibuf2 = seq_render_strip(context, seq, cfra);
2839
2840                         float facf = seq->blend_opacity / 100.0f;
2841                         int swap_input = seq_must_swap_input_in_blend_mode(seq);
2842
2843                         if (swap_input) {
2844                                 if (sh.multithreaded)
2845                                         out = seq_render_effect_execute_threaded(&sh, context, seq, cfra, facf, facf, ibuf2, ibuf1, NULL);
2846                                 else
2847                                         out = sh.execute(context, seq, cfra, facf, facf, ibuf2, ibuf1, NULL);
2848                         }
2849                         else {
2850                                 if (sh.multithreaded)
2851                                         out = seq_render_effect_execute_threaded(&sh, context, seq, cfra, facf, facf, ibuf1, ibuf2, NULL);
2852                                 else
2853                                         out = sh.execute(context, seq, cfra, facf, facf, ibuf1, ibuf2, NULL);
2854                         }
2855                 
2856                         IMB_freeImBuf(ibuf1);
2857                         IMB_freeImBuf(ibuf2);
2858                 }
2859
2860                 BKE_sequencer_cache_put(context, seq_arr[i], cfra, SEQ_STRIPELEM_IBUF_COMP, out);
2861         }
2862
2863         return out;
2864 }
2865
2866 /*
2867  * returned ImBuf is refed!
2868  * you have to free after usage!
2869  */
2870
2871 ImBuf *BKE_sequencer_give_ibuf(SeqRenderData context, float cfra, int chanshown)
2872 {
2873         Editing *ed = BKE_sequencer_editing_get(context.scene, FALSE);
2874         int count;
2875         ListBase *seqbasep;
2876         
2877         if (ed == NULL) return NULL;
2878
2879         count = BLI_countlist(&ed->metastack);
2880         if ((chanshown < 0) && (count > 0)) {
2881                 count = max_ii(count + chanshown, 0);
2882                 seqbasep = ((MetaStack *)BLI_findlink(&ed->metastack, count))->oldbasep;
2883         }
2884         else {
2885                 seqbasep = ed->seqbasep;
2886         }
2887
2888         return seq_render_strip_stack(context, seqbasep, cfra, chanshown);
2889 }
2890
2891 ImBuf *BKE_sequencer_give_ibuf_seqbase(SeqRenderData context, float cfra, int chanshown, ListBase *seqbasep)
2892 {
2893         return seq_render_strip_stack(context, seqbasep, cfra, chanshown);
2894 }
2895
2896
2897 ImBuf *BKE_sequencer_give_ibuf_direct(SeqRenderData context, float cfra, Sequence *seq)
2898 {
2899         return seq_render_strip(context, seq, cfra);
2900 }
2901
2902 /* *********************** threading api ******************* */
2903
2904 static ListBase running_threads;
2905 static ListBase prefetch_wait;
2906 static ListBase prefetch_done;
2907
2908 static pthread_mutex_t queue_lock          = PTHREAD_MUTEX_INITIALIZER;
2909 static pthread_mutex_t wakeup_lock         = PTHREAD_MUTEX_INITIALIZER;
2910 static pthread_cond_t wakeup_cond          = PTHREAD_COND_INITIALIZER;
2911
2912 //static pthread_mutex_t prefetch_ready_lock = PTHREAD_MUTEX_INITIALIZER;
2913 //static pthread_cond_t  prefetch_ready_cond = PTHREAD_COND_INITIALIZER;
2914
2915 static pthread_mutex_t frame_done_lock     = PTHREAD_MUTEX_INITIALIZER;
2916 static pthread_cond_t frame_done_cond      = PTHREAD_COND_INITIALIZER;
2917
2918 static volatile int seq_thread_shutdown = TRUE; 
2919 static volatile int seq_last_given_monoton_cfra = 0;
2920 static int monoton_cfra = 0;
2921
2922 typedef struct PrefetchThread {
2923         struct PrefetchThread *next, *prev;
2924         
2925         Scene *scene;
2926         struct PrefetchQueueElem *current;
2927         pthread_t pthread;
2928         int running;
2929         
2930 } PrefetchThread;
2931
2932 typedef struct PrefetchQueueElem {
2933         struct PrefetchQueueElem *next, *prev;
2934         
2935         int rectx;
2936         int recty;
2937         float cfra;
2938         int chanshown;
2939         int preview_render_size;
2940
2941         int monoton_cfra;
2942
2943         ImBuf *ibuf;
2944 } PrefetchQueueElem;
2945
2946 void BKE_sequencer_give_ibuf_prefetch_request(SeqRenderData context, float cfra, int chanshown)
2947 {
2948         PrefetchQueueElem *e;
2949         if (seq_thread_shutdown) {
2950                 return;
2951         }
2952
2953         e = MEM_callocN(sizeof(PrefetchQueueElem), "prefetch_queue_elem");
2954         e->rectx = context.rectx;
2955         e->recty = context.recty;
2956         e->cfra = cfra;
2957         e->chanshown = chanshown;
2958         e->preview_render_size = context.preview_render_size;
2959         e->monoton_cfra = monoton_cfra++;
2960
2961         pthread_mutex_lock(&queue_lock);
2962         BLI_addtail(&prefetch_wait, e);
2963         pthread_mutex_unlock(&queue_lock);
2964         
2965         pthread_mutex_lock(&wakeup_lock);
2966         pthread_cond_signal(&wakeup_cond);
2967         pthread_mutex_unlock(&wakeup_lock);
2968 }
2969
2970 ImBuf *BKE_sequencer_give_ibuf_threaded(SeqRenderData context, float cfra, int chanshown)
2971 {
2972         PrefetchQueueElem *e = NULL;
2973         int found_something = FALSE;
2974
2975         if (seq_thread_shutdown) {
2976                 return BKE_sequencer_give_ibuf(context, cfra, chanshown);
2977         }
2978
2979         while (!e) {
2980                 int success = FALSE;
2981                 pthread_mutex_lock(&queue_lock);
2982
2983                 for (e = prefetch_done.first; e; e = e->next) {
2984                         if (cfra == e->cfra &&
2985                             chanshown == e->chanshown &&
2986                             context.rectx == e->rectx &&
2987                             context.recty == e->recty &&
2988                             context.preview_render_size == e->preview_render_size)
2989                         {
2990                                 success = TRUE;
2991                                 found_something = TRUE;
2992                                 break;
2993                         }
2994                 }
2995
2996                 if (!e) {
2997                         for (e = prefetch_wait.first; e; e = e->next) {
2998                                 if (cfra == e->cfra &&
2999                                     chanshown == e->chanshown &&
3000                                     context.rectx == e->rectx &&
3001                                     context.recty == e->recty &&
3002                                     context.preview_render_size == e->preview_render_size)
3003                                 {
3004                                         found_something = TRUE;
3005                                         break;
3006                                 }
3007                         }
3008                 }
3009
3010                 if (!e) {
3011                         PrefetchThread *tslot;
3012
3013                         for (tslot = running_threads.first;
3014                              tslot;
3015                              tslot = tslot->next)
3016                         {
3017                                 if (tslot->current &&
3018                                     cfra == tslot->current->cfra &&
3019                                     chanshown == tslot->current->chanshown &&
3020                                     context.rectx == tslot->current->rectx &&
3021                                     context.recty == tslot->current->recty &&
3022                                     context.preview_render_size == tslot->current->preview_render_size)
3023                                 {
3024                                         found_something = TRUE;
3025                                         break;
3026                                 }
3027                         }
3028                 }
3029
3030                 /* e->ibuf is unrefed by render thread on next round. */
3031
3032                 if (e) {
3033                         seq_last_given_monoton_cfra = e->monoton_cfra;
3034                 }
3035
3036                 pthread_mutex_unlock(&queue_lock);
3037
3038                 if (!success) {
3039                         e = NULL;
3040
3041                         if (!found_something) {
3042                                 fprintf(stderr, "SEQ-THREAD: Requested frame not in queue ???\n");
3043                                 break;
3044                         }
3045                         pthread_mutex_lock(&frame_done_lock);
3046                         pthread_cond_wait(&frame_done_cond, &frame_done_lock);
3047                         pthread_mutex_unlock(&frame_done_lock);
3048                 }
3049         }
3050         
3051         return e ? e->ibuf : NULL;
3052 }
3053
3054 /* Functions to free imbuf and anim data on changes */
3055
3056 static void free_anim_seq(Sequence *seq)
3057 {
3058         if (seq->anim) {
3059                 IMB_free_anim(seq->anim);
3060                 seq->anim = NULL;
3061         }
3062 }
3063
3064 /* check whether sequence cur depends on seq */
3065 bool BKE_sequence_check_depend(Sequence *seq, Sequence *cur)
3066 {
3067         if (cur->seq1 == seq || cur->seq2 == seq || cur->seq3 == seq)
3068                 return true;
3069
3070         /* sequences are not intersecting in time, assume no dependency exists between them */
3071         if (cur->enddisp < seq->startdisp || cur->startdisp > seq->enddisp)
3072                 return false;
3073
3074         /* checking sequence is below reference one, not dependent on it */
3075         if (cur->machine < seq->machine)
3076                 return false;
3077
3078         /* sequence is not blending with lower machines, no dependency here occurs
3079          * check for non-effects only since effect could use lower machines as input
3080          */
3081         if ((cur->type & SEQ_TYPE_EFFECT) == 0 &&
3082             ((cur->blend_mode == SEQ_BLEND_REPLACE) ||
3083              (cur->blend_mode == SEQ_TYPE_CROSS && cur->blend_opacity == 100.0f)))
3084         {
3085                 return false;
3086         }
3087
3088         return true;
3089 }
3090
3091 static void sequence_do_invalidate_dependent(Sequence *seq, ListBase *seqbase)
3092 {
3093         Sequence *cur;
3094
3095         for (cur = seqbase->first; cur; cur = cur->next) {
3096                 if (cur == seq)
3097                         continue;
3098
3099                 if (BKE_sequence_check_depend(seq, cur)) {
3100                         BKE_sequencer_cache_cleanup_sequence(cur);
3101                         BKE_sequencer_preprocessed_cache_cleanup_sequence(cur);
3102                 }
3103
3104                 if (cur->seqbase.first)
3105                         sequence_do_invalidate_dependent(seq, &cur->seqbase);
3106         }
3107 }
3108
3109 static void sequence_invalidate_cache(Scene *scene, Sequence *seq, int invalidate_self, int invalidate_preprocess)
3110 {
3111         Editing *ed = scene->ed;
3112
3113         /* invalidate cache for current sequence */
3114         if (invalidate_self)
3115                 BKE_sequencer_cache_cleanup_sequence(seq);
3116
3117         /* if invalidation is invoked from sequence free routine, effectdata would be NULL here */
3118         if (seq->effectdata && seq->type == SEQ_TYPE_SPEED)
3119                 BKE_sequence_effect_speed_rebuild_map(scene, seq, TRUE);
3120
3121         if (invalidate_preprocess)
3122                 BKE_sequencer_preprocessed_cache_cleanup_sequence(seq);
3123
3124         /* invalidate cache for all dependent sequences */
3125
3126         /* NOTE: can not use SEQ_BEGIN/SEQ_END here because that macro will change sequence's depth,
3127          *       which makes transformation routines work incorrect
3128          */
3129         sequence_do_invalidate_dependent(seq, &ed->seqbase);
3130 }
3131
3132 void BKE_sequence_invalidate_cache(Scene *scene, Sequence *seq)
3133 {
3134         sequence_invalidate_cache(scene, seq, TRUE, TRUE);
3135 }
3136
3137 void BKE_sequence_invalidate_dependent(Scene *scene, Sequence *seq)
3138 {
3139         sequence_invalidate_cache(scene, seq, FALSE, TRUE);
3140 }
3141
3142 void BKE_sequence_invalidate_cache_for_modifier(Scene *scene, Sequence *seq)
3143 {
3144         sequence_invalidate_cache(scene, seq, TRUE, FALSE);
3145 }
3146
3147 void BKE_sequencer_free_imbuf(Scene *scene, ListBase *seqbase, int for_render)
3148 {
3149         Sequence *seq;
3150
3151         BKE_sequencer_cache_cleanup();
3152
3153         for (seq = seqbase->first; seq; seq = seq->next) {
3154                 if (for_render && CFRA >= seq->startdisp && CFRA <= seq->enddisp) {
3155                         continue;
3156                 }
3157
3158                 if (seq->strip) {
3159                         if (seq->type == SEQ_TYPE_MOVIE) {
3160                                 free_anim_seq(seq);
3161                         }
3162                         if (seq->type == SEQ_TYPE_SPEED) {
3163                                 BKE_sequence_effect_speed_rebuild_map(scene, seq, 1);
3164                         }
3165                 }
3166                 if (seq->type == SEQ_TYPE_META) {
3167                         BKE_sequencer_free_imbuf(scene, &seq->seqbase, for_render);
3168                 }
3169                 if (seq->type == SEQ_TYPE_SCENE) {
3170                         /* FIXME: recurs downwards, 
3171                          * but do recurs protection somehow! */
3172                 }
3173         }
3174         
3175 }
3176
3177 static int update_changed_seq_recurs(Scene *scene, Sequence *seq, Sequence *changed_seq, int len_change, int ibuf_change)
3178 {
3179         Sequence *subseq;
3180         int free_imbuf = 0;
3181         
3182         /* recurs downwards to see if this seq depends on the changed seq */
3183         
3184         if (seq == NULL)
3185                 return 0;
3186         
3187         if (seq == changed_seq)
3188                 free_imbuf = 1;
3189         
3190         for (subseq = seq->seqbase.first; subseq; subseq = subseq->next)
3191                 if (update_changed_seq_recurs(scene, subseq, changed_seq, len_change, ibuf_change))
3192                         free_imbuf = TRUE;
3193         
3194         if (seq->seq1)
3195                 if (update_changed_seq_recurs(scene, seq->seq1, changed_seq, len_change, ibuf_change))
3196                         free_imbuf = TRUE;
3197         if (seq->seq2 && (seq->seq2 != seq->seq1))
3198                 if (update_changed_seq_recurs(scene, seq->seq2, changed_seq, len_change, ibuf_change))
3199                         free_imbuf = TRUE;
3200         if (seq->seq3 && (seq->seq3 != seq->seq1) && (seq->seq3 != seq->seq2))
3201                 if (update_changed_seq_recurs(scene, seq->seq3, changed_seq, len_change, ibuf_change))
3202                         free_imbuf = TRUE;
3203         
3204         if (free_imbuf) {
3205                 if (ibuf_change) {
3206                         if (seq->type == SEQ_TYPE_MOVIE)
3207                                 free_anim_seq(seq);
3208                         if (seq->type == SEQ_TYPE_SPEED) {
3209                                 BKE_sequence_effect_speed_rebuild_map(scene, seq, 1);
3210                         }
3211                 }
3212                 
3213                 if (len_change)
3214                         BKE_sequence_calc(scene, seq);
3215         }
3216         
3217         return free_imbuf;
3218 }
3219
3220 void BKE_sequencer_update_changed_seq_and_deps(Scene *scene, Sequence *changed_seq, int len_change, int ibuf_change)
3221 {
3222         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
3223         Sequence *seq;
3224         
3225         if (ed == NULL) return;
3226         
3227         for (seq = ed->seqbase.first; seq; seq = seq->next)
3228                 update_changed_seq_recurs(scene, seq, changed_seq, len_change, ibuf_change);
3229 }
3230
3231 /* seq funcs's for transforming internally
3232  * notice the difference between start/end and left/right.
3233  *
3234  * left and right are the bounds at which the sequence is rendered,
3235  * start and end are from the start and fixed length of the sequence.
3236  */
3237 static int seq_tx_get_start(Sequence *seq)
3238 {
3239         return seq->start;
3240 }
3241 static int seq_tx_get_end(Sequence *seq)
3242 {
3243         return seq->start + seq->len;
3244 }
3245
3246 int BKE_sequence_tx_get_final_left(Sequence *seq, int metaclip)
3247 {
3248         if (metaclip && seq->tmp) {
3249                 /* return the range clipped by the parents range */
3250                 return max_ii(BKE_sequence_tx_get_final_left(seq, 0), BKE_sequence_tx_get_final_left((Sequence *)seq->tmp, TRUE));
3251         }
3252         else {
3253                 return (seq->start - seq->startstill) + seq->startofs;
3254         }
3255
3256 }
3257 int BKE_sequence_tx_get_final_right(Sequence *seq, int metaclip)
3258 {
3259         if (metaclip && seq->tmp) {
3260                 /* return the range clipped by the parents range */
3261                 return min_ii(BKE_sequence_tx_get_final_right(seq, 0), BKE_sequence_tx_get_final_right((Sequence *)seq->tmp, TRUE));
3262         }
3263         else {
3264                 return ((seq->start + seq->len) + seq->endstill) - seq->endofs;
3265         }
3266 }
3267
3268 void BKE_sequence_tx_set_final_left(Sequence *seq, int val)
3269 {