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