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