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