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