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