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