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