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