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