Cleanup
[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                                                 seq_anim_add_suffix(scene, anim, i);
872
873                                                 if (anim) {
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, float fmul)
1165 {
1166         char *rt;
1167         float *rt_float;
1168
1169         int a, mul, icol;
1170
1171         mul = (int)(256.0f * fmul);
1172         rt = (char *)ibuf->rect;
1173         rt_float = ibuf->rect_float;
1174
1175         if (rt) {
1176                 a = ibuf->x * ibuf->y;
1177                 while (a--) {
1178
1179                         icol = (mul * rt[0]) >> 8;
1180                         if (icol > 254) rt[0] = 255; else rt[0] = icol;
1181                         icol = (mul * rt[1]) >> 8;
1182                         if (icol > 254) rt[1] = 255; else rt[1] = icol;
1183                         icol = (mul * rt[2]) >> 8;
1184                         if (icol > 254) rt[2] = 255; else rt[2] = icol;
1185                         icol = (mul * rt[3]) >> 8;
1186                         if (icol > 254) rt[3] = 255; else rt[3] = icol;
1187                         
1188                         rt += 4;
1189                 }
1190         }
1191         if (rt_float) {
1192                 a = ibuf->x * ibuf->y;
1193                 while (a--) {
1194                         rt_float[0] *= fmul;
1195                         rt_float[1] *= fmul;
1196                         rt_float[2] *= fmul;
1197                         rt_float[3] *= fmul;
1198                         
1199                         rt_float += 4;
1200                 }
1201         }
1202 }
1203
1204 static float give_stripelem_index(Sequence *seq, float cfra)
1205 {
1206         float nr;
1207         int sta = seq->start;
1208         int end = seq->start + seq->len - 1;
1209
1210         if (seq->type & SEQ_TYPE_EFFECT) {
1211                 end = seq->enddisp;
1212         }
1213
1214         if (end < sta) {
1215                 return -1;
1216         }
1217
1218         if (seq->flag & SEQ_REVERSE_FRAMES) {
1219                 /*reverse frame in this sequence */
1220                 if (cfra <= sta) nr = end - sta;
1221                 else if (cfra >= end) nr = 0;
1222                 else nr = end - cfra;
1223         }
1224         else {
1225                 if (cfra <= sta) nr = 0;
1226                 else if (cfra >= end) nr = end - sta;
1227                 else nr = cfra - sta;
1228         }
1229         
1230         if (seq->strobe < 1.0f) seq->strobe = 1.0f;
1231         
1232         if (seq->strobe > 1.0f) {
1233                 nr -= fmodf((double)nr, (double)seq->strobe);
1234         }
1235
1236         return nr;
1237 }
1238
1239 StripElem *BKE_sequencer_give_stripelem(Sequence *seq, int cfra)
1240 {
1241         StripElem *se = seq->strip->stripdata;
1242
1243         if (seq->type == SEQ_TYPE_IMAGE) {
1244                 /* only IMAGE strips use the whole array, MOVIE strips use only the first element,
1245                  * all other strips don't use this...
1246                  */
1247
1248                 int nr = (int) give_stripelem_index(seq, cfra);
1249
1250                 if (nr == -1 || se == NULL)
1251                         return NULL;
1252         
1253                 se += nr + seq->anim_startofs;
1254         }
1255         return se;
1256 }
1257
1258 static int evaluate_seq_frame_gen(Sequence **seq_arr, ListBase *seqbase, int cfra, int chanshown)
1259 {
1260         Sequence *seq;
1261         Sequence *effect_inputs[MAXSEQ + 1];
1262         int i, totseq = 0, num_effect_inputs = 0;
1263
1264         memset(seq_arr, 0, sizeof(Sequence *) * (MAXSEQ + 1));
1265
1266         seq = seqbase->first;
1267         while (seq) {
1268                 if (seq->startdisp <= cfra && seq->enddisp > cfra) {
1269                         if ((seq->type & SEQ_TYPE_EFFECT)) {
1270                                 if (seq->seq1) {
1271                                         effect_inputs[num_effect_inputs++] = seq->seq1;
1272                                 }
1273
1274                                 if (seq->seq2) {
1275                                         effect_inputs[num_effect_inputs++] = seq->seq2;
1276                                 }
1277
1278                                 if (seq->seq3) {
1279                                         effect_inputs[num_effect_inputs++] = seq->seq3;
1280                                 }
1281                         }
1282
1283                         seq_arr[seq->machine] = seq;
1284                         totseq++;
1285                 }
1286                 seq = seq->next;
1287         }
1288
1289         /* Drop strips which are used for effect inputs, we don't want
1290          * them to blend into render stack in any other way than effect
1291          * string rendering.
1292          */
1293         for (i = 0; i < num_effect_inputs; i++) {
1294                 seq = effect_inputs[i];
1295                 /* It's possible that effetc strip would be placed to the same
1296                  * 'machine' as it's inputs. We don't want to clear such strips
1297                  * from the stack.
1298                  */
1299                 if (seq_arr[seq->machine] && seq_arr[seq->machine]->type & SEQ_TYPE_EFFECT) {
1300                         continue;
1301                 }
1302                 /* If we're shown a specified channel, then we want to see the stirps
1303                  * which belongs to this machine.
1304                  */
1305                 if (chanshown != 0 && chanshown <= seq->machine) {
1306                         continue;
1307                 }
1308                 seq_arr[seq->machine] = NULL;
1309         }
1310
1311         return totseq;
1312 }
1313
1314 int BKE_sequencer_evaluate_frame(Scene *scene, int cfra)
1315 {
1316         Editing *ed = BKE_sequencer_editing_get(scene, false);
1317         Sequence *seq_arr[MAXSEQ + 1];
1318
1319         if (ed == NULL)
1320                 return 0;
1321
1322         return evaluate_seq_frame_gen(seq_arr, ed->seqbasep, cfra, 0);
1323 }
1324
1325 static bool video_seq_is_rendered(Sequence *seq)
1326 {
1327         return (seq && !(seq->flag & SEQ_MUTE) && seq->type != SEQ_TYPE_SOUND_RAM);
1328 }
1329
1330 static int get_shown_sequences(ListBase *seqbasep, int cfra, int chanshown, Sequence **seq_arr_out)
1331 {
1332         Sequence *seq_arr[MAXSEQ + 1];
1333         int b = chanshown;
1334         int cnt = 0;
1335
1336         if (b > MAXSEQ) {
1337                 return 0;
1338         }
1339
1340         if (evaluate_seq_frame_gen(seq_arr, seqbasep, cfra, chanshown)) {
1341                 if (b == 0) {
1342                         b = MAXSEQ;
1343                 }
1344                 for (; b > 0; b--) {
1345                         if (video_seq_is_rendered(seq_arr[b])) {
1346                                 break;
1347                         }
1348                 }
1349         }
1350         
1351         chanshown = b;
1352
1353         for (; b > 0; b--) {
1354                 if (video_seq_is_rendered(seq_arr[b])) {
1355                         if (seq_arr[b]->blend_mode == SEQ_BLEND_REPLACE) {
1356                                 break;
1357                         }
1358                 }
1359         }
1360
1361         for (; b <= chanshown && b >= 0; b++) {
1362                 if (video_seq_is_rendered(seq_arr[b])) {
1363                         seq_arr_out[cnt++] = seq_arr[b];
1364                 }
1365         }
1366
1367         return cnt;
1368 }
1369
1370 /*********************** proxy management *************************/
1371
1372 typedef struct SeqIndexBuildContext {
1373         struct IndexBuildContext *index_context;
1374
1375         int tc_flags;
1376         int size_flags;
1377         int quality;
1378         bool overwrite;
1379         size_t view_id;
1380
1381         Main *bmain;
1382         Scene *scene;
1383         Sequence *seq, *orig_seq;
1384 } SeqIndexBuildContext;
1385
1386 #define PROXY_MAXFILE (2 * FILE_MAXDIR + FILE_MAXFILE)
1387
1388 static IMB_Proxy_Size seq_rendersize_to_proxysize(int size)
1389 {
1390         if (size >= 100) {
1391                 return IMB_PROXY_NONE;
1392         }
1393         if (size >= 99) {
1394                 return IMB_PROXY_100;
1395         }
1396         if (size >= 75) {
1397                 return IMB_PROXY_75;
1398         }
1399         if (size >= 50) {
1400                 return IMB_PROXY_50;
1401         }
1402         return IMB_PROXY_25;
1403 }
1404
1405 static double seq_rendersize_to_scale_factor(int size)
1406 {
1407         if (size >= 99) {
1408                 return 1.0;
1409         }
1410         if (size >= 75) {
1411                 return 0.75;
1412         }
1413         if (size >= 50) {
1414                 return 0.50;
1415         }
1416         return 0.25;
1417 }
1418
1419 /* the number of files will vary according to the stereo format */
1420 static size_t seq_num_files(Scene *scene, char views_format, const bool is_multiview)
1421 {
1422         if (!is_multiview) {
1423                 return 1;
1424         }
1425         else if (views_format == R_IMF_VIEWS_STEREO_3D) {
1426                 return 1;
1427         }
1428         /* R_IMF_VIEWS_INDIVIDUAL */
1429         else {
1430                 return BKE_scene_multiview_num_views_get(&scene->r);
1431         }
1432 }
1433
1434 static void seq_proxy_index_dir_set(struct anim *anim, const char *base_dir)
1435 {
1436         char dir[FILE_MAX];
1437         char fname[FILE_MAXFILE];
1438
1439         IMB_anim_get_fname(anim, fname, FILE_MAXFILE);
1440         BLI_strncpy(dir, base_dir, sizeof(dir));
1441         BLI_path_append(dir, sizeof(dir), fname);
1442         IMB_anim_set_index_dir(anim, dir);
1443 }
1444
1445 static void seq_open_anim_file(Scene *scene, Sequence *seq, bool openfile)
1446 {
1447         char dir[FILE_MAX];
1448         char name[FILE_MAX];
1449         StripProxy *proxy;
1450         bool use_proxy;
1451         bool is_multiview_loaded = false;
1452         Editing *ed = scene->ed;
1453         const bool is_multiview = (seq->flag & SEQ_USE_VIEWS) != 0 && (scene->r.scemode & R_MULTIVIEW) != 0;
1454
1455         if ((seq->anims.first != NULL) && (((StripAnim *)seq->anims.first)->anim != NULL)) {
1456                 return;
1457         }
1458
1459         /* reset all the previously created anims */
1460         BKE_sequence_free_anim(seq);
1461
1462         BLI_join_dirfile(name, sizeof(name),
1463                          seq->strip->dir, seq->strip->stripdata->name);
1464         BLI_path_abs(name, G.main->name);
1465
1466         proxy = seq->strip->proxy;
1467
1468         use_proxy = proxy && ((proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_DIR) != 0 ||
1469                               (ed->proxy_storage == SEQ_EDIT_PROXY_DIR_STORAGE));
1470
1471         if (use_proxy) {
1472                 if (ed->proxy_storage == SEQ_EDIT_PROXY_DIR_STORAGE) {
1473                         if (ed->proxy_dir[0] == 0)
1474                                 BLI_strncpy(dir, "//BL_proxy", sizeof(dir));
1475                         else
1476                                 BLI_strncpy(dir, ed->proxy_dir, sizeof(dir));
1477                 }
1478                 else {
1479                         BLI_strncpy(dir, seq->strip->proxy->dir, sizeof(dir));
1480                 }
1481                 BLI_path_abs(dir, G.main->name);
1482         }
1483
1484         if (is_multiview && seq->views_format == R_IMF_VIEWS_INDIVIDUAL) {
1485                 size_t totfiles = seq_num_files(scene, seq->views_format, true);
1486                 char prefix[FILE_MAX];
1487                 char *ext = NULL;
1488                 int i;
1489
1490                 BKE_scene_multiview_view_prefix_get(scene, name, prefix, &ext);
1491
1492                 if (prefix[0] != '\0') {
1493                         for (i = 0; i < totfiles; i++) {
1494                                 const char *suffix = BKE_scene_multiview_view_id_suffix_get(&scene->r, i);
1495                                 char str[FILE_MAX];
1496                                 StripAnim *sanim = MEM_mallocN(sizeof(StripAnim), "Strip Anim");
1497
1498                                 BLI_addtail(&seq->anims, sanim);
1499
1500                                 BLI_snprintf(str, sizeof(str), "%s%s%s", prefix, suffix, ext);
1501
1502                                 if (openfile) {
1503                                         sanim->anim = openanim(
1504                                                 str, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
1505                                                 seq->streamindex, seq->strip->colorspace_settings.name);
1506                                 }
1507                                 else {
1508                                         sanim->anim = openanim_noload(
1509                                                 str, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
1510                                                 seq->streamindex, seq->strip->colorspace_settings.name);
1511                                 }
1512
1513                                 seq_anim_add_suffix(scene, sanim->anim, i);
1514
1515                                 if (sanim->anim == NULL) {
1516                                         if (openfile) {
1517                                                 sanim->anim = openanim(
1518                                                         name, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
1519                                                         seq->streamindex, seq->strip->colorspace_settings.name);
1520                                         }
1521                                         else {
1522                                                 sanim->anim = openanim_noload(
1523                                                         name, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
1524                                                         seq->streamindex, seq->strip->colorspace_settings.name);
1525                                         }
1526
1527                                         /* no individual view files - monoscopic, stereo 3d or exr multiview */
1528                                         totfiles = 1;
1529                                 }
1530
1531                                 if (sanim->anim && use_proxy) {
1532                                         seq_proxy_index_dir_set(sanim->anim, dir);
1533                                 }
1534                         }
1535                         is_multiview_loaded = true;
1536                 }
1537         }
1538
1539         if (is_multiview_loaded == false) {
1540                 StripAnim *sanim;
1541
1542                 sanim = MEM_mallocN(sizeof(StripAnim), "Strip Anim");
1543                 BLI_addtail(&seq->anims, sanim);
1544
1545                 if (openfile) {
1546                         sanim->anim = openanim(
1547                                 name, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
1548                                 seq->streamindex, seq->strip->colorspace_settings.name);
1549                 }
1550                 else {
1551                         sanim->anim = openanim_noload(
1552                                 name, IB_rect | ((seq->flag & SEQ_FILTERY) ? IB_animdeinterlace : 0),
1553                                 seq->streamindex, seq->strip->colorspace_settings.name);
1554                 }
1555
1556                 if (sanim->anim && use_proxy) {
1557                         seq_proxy_index_dir_set(sanim->anim, dir);
1558                 }
1559         }
1560 }
1561
1562 static bool seq_proxy_get_fname(Editing *ed, Sequence *seq, int cfra, int render_size, char *name, const size_t view_id)
1563 {
1564         int frameno;
1565         char dir[PROXY_MAXFILE];
1566         StripAnim *sanim;
1567         char suffix[24] = {'\0'};
1568
1569         StripProxy *proxy = seq->strip->proxy;
1570         if (!proxy) {
1571                 return false;
1572         }
1573
1574         /* MOVIE tracks (only exception: custom files) are now handled 
1575          * internally by ImBuf module for various reasons: proper time code
1576          * support, quicker index build, using one file instead
1577          * of a full directory of jpeg files, etc. Trying to support old
1578          * and new method at once could lead to funny effects, if people
1579          * have both, a directory full of jpeg files and proxy avis, so
1580          * sorry folks, please rebuild your proxies... */
1581
1582         sanim = BLI_findlink(&seq->anims, view_id);
1583
1584         if (sanim && sanim->anim && ed->proxy_storage == SEQ_EDIT_PROXY_DIR_STORAGE) {
1585                 char fname[FILE_MAXFILE];
1586                 if (ed->proxy_dir[0] == 0)
1587                         BLI_strncpy(dir, "//BL_proxy", sizeof(dir));
1588                 else
1589                         BLI_strncpy(dir, ed->proxy_dir, sizeof(dir));
1590                 IMB_anim_get_fname(sanim->anim, fname, FILE_MAXFILE);
1591                 BLI_path_append(dir, sizeof(dir), fname);
1592                 BLI_path_abs(name, G.main->name);
1593         }
1594         else if ((proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_DIR) && (proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_FILE)) {
1595                 BLI_strncpy(dir, seq->strip->proxy->dir, sizeof(dir));
1596         }
1597         else if (sanim && sanim->anim && (proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_DIR)) {
1598                 char fname[FILE_MAXFILE];
1599                 BLI_strncpy(dir, seq->strip->proxy->dir, sizeof(dir));
1600                 IMB_anim_get_fname(sanim->anim, fname, FILE_MAXFILE);
1601                 BLI_path_append(dir, sizeof(dir), fname);
1602         }
1603         else if (seq->type == SEQ_TYPE_IMAGE) {
1604                 BLI_snprintf(dir, PROXY_MAXFILE, "%s/BL_proxy", seq->strip->dir);
1605         }
1606         else {
1607                 return false;
1608         }
1609
1610         if (view_id > 0)
1611                 BLI_snprintf(suffix, sizeof(suffix), "_%zu", view_id);
1612
1613         if (proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_FILE && sanim && sanim->anim &&
1614             ed->proxy_storage != SEQ_EDIT_PROXY_DIR_STORAGE)
1615         {
1616                 BLI_join_dirfile(name, PROXY_MAXFILE,
1617                                  dir, proxy->file);
1618                 BLI_path_abs(name, G.main->name);
1619                 BLI_snprintf(name, PROXY_MAXFILE, "%s_%s", name, suffix);
1620
1621                 return true;
1622         }
1623
1624         /* generate a separate proxy directory for each preview size */
1625
1626         if (seq->type == SEQ_TYPE_IMAGE) {
1627                 BLI_snprintf(name, PROXY_MAXFILE, "%s/images/%d/%s_proxy%s", dir, render_size,
1628                              BKE_sequencer_give_stripelem(seq, cfra)->name, suffix);
1629                 frameno = 1;
1630         }
1631         else {
1632                 frameno = (int)give_stripelem_index(seq, cfra) + seq->anim_startofs;
1633                 BLI_snprintf(name, PROXY_MAXFILE, "%s/proxy_misc/%d/####%s", dir, render_size, suffix);
1634         }
1635
1636         BLI_path_abs(name, G.main->name);
1637         BLI_path_frame(name, frameno, 0);
1638
1639         strcat(name, ".jpg");
1640
1641         return true;
1642 }
1643
1644 static ImBuf *seq_proxy_fetch(const SeqRenderData *context, Sequence *seq, int cfra)
1645 {
1646         char name[PROXY_MAXFILE];
1647         IMB_Proxy_Size psize = seq_rendersize_to_proxysize(context->preview_render_size);
1648         int size_flags;
1649         int render_size = context->preview_render_size;
1650         StripProxy *proxy = seq->strip->proxy;
1651         Editing *ed = context->scene->ed;
1652         StripAnim *sanim;
1653
1654         if (!(seq->flag & SEQ_USE_PROXY)) {
1655                 return NULL;
1656         }
1657
1658         /* dirty hack to distinguish 100% render size from PROXY_100 */
1659         if (render_size == 99) {
1660                 render_size = 100;
1661         }
1662
1663         size_flags = proxy->build_size_flags;
1664
1665         /* only use proxies, if they are enabled (even if present!) */
1666         if (psize == IMB_PROXY_NONE || ((size_flags & psize) != psize)) {
1667                 return NULL;
1668         }
1669
1670         if (proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_FILE) {
1671                 int frameno = (int)give_stripelem_index(seq, cfra) + seq->anim_startofs;
1672                 if (proxy->anim == NULL) {
1673                         if (seq_proxy_get_fname(ed, seq, cfra, render_size, name, context->view_id) == 0) {
1674                                 return NULL;
1675                         }
1676
1677                         proxy->anim = openanim(name, IB_rect, 0, seq->strip->colorspace_settings.name);
1678                 }
1679                 if (proxy->anim == NULL) {
1680                         return NULL;
1681                 }
1682  
1683                 seq_open_anim_file(context->scene, seq, true);
1684                 sanim = seq->anims.first;
1685
1686                 frameno = IMB_anim_index_get_frame_index(sanim ? sanim->anim : NULL, seq->strip->proxy->tc, frameno);
1687
1688                 return IMB_anim_absolute(proxy->anim, frameno, IMB_TC_NONE, IMB_PROXY_NONE);
1689         }
1690  
1691         if (seq_proxy_get_fname(ed, seq, cfra, render_size, name, context->view_id) == 0) {
1692                 return NULL;
1693         }
1694
1695         if (BLI_exists(name)) {
1696                 ImBuf *ibuf = IMB_loadiffname(name, IB_rect, NULL);
1697
1698                 if (ibuf)
1699                         sequencer_imbuf_assign_spaces(context->scene, ibuf);
1700
1701                 return ibuf;
1702         }
1703         else {
1704                 return NULL;
1705         }
1706 }
1707
1708 static void seq_proxy_build_frame(const SeqRenderData *context, Sequence *seq, int cfra,
1709                                   int proxy_render_size, const bool overwrite)
1710 {
1711         char name[PROXY_MAXFILE];
1712         int quality;
1713         int rectx, recty;
1714         int ok;
1715         ImBuf *ibuf_tmp, *ibuf;
1716         Editing *ed = context->scene->ed;
1717
1718         if (!seq_proxy_get_fname(ed, seq, cfra, proxy_render_size, name, context->view_id)) {
1719                 return;
1720         }
1721
1722         if (!overwrite && BLI_exists(name)) {
1723                 return;
1724         }
1725
1726         ibuf_tmp = seq_render_strip(context, seq, cfra);
1727
1728         rectx = (proxy_render_size * ibuf_tmp->x) / 100;
1729         recty = (proxy_render_size * ibuf_tmp->y) / 100;
1730
1731         if (ibuf_tmp->x != rectx || ibuf_tmp->y != recty) {
1732                 ibuf = IMB_dupImBuf(ibuf_tmp);
1733                 IMB_freeImBuf(ibuf_tmp);
1734                 IMB_scalefastImBuf(ibuf, (short)rectx, (short)recty);
1735         }
1736         else {
1737                 ibuf = ibuf_tmp;
1738         }
1739
1740         /* depth = 32 is intentionally left in, otherwise ALPHA channels
1741          * won't work... */
1742         quality = seq->strip->proxy->quality;
1743         ibuf->ftype = JPG | quality;
1744
1745         /* unsupported feature only confuses other s/w */
1746         if (ibuf->planes == 32)
1747                 ibuf->planes = 24;
1748
1749         BLI_make_existing_file(name);
1750
1751         ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
1752         if (ok == 0) {
1753                 perror(name);
1754         }
1755
1756         IMB_freeImBuf(ibuf);
1757 }
1758
1759 /* returns whether the file this context would read from even exist, if not, don't create the context
1760 */
1761 static bool seq_proxy_multiview_context_invalid(Sequence *seq, Scene *scene, const size_t view_id)
1762 {
1763         if ((scene->r.scemode & R_MULTIVIEW) == 0)
1764                 return false;
1765
1766         if ((seq->type == SEQ_TYPE_IMAGE) && (seq->views_format == R_IMF_VIEWS_INDIVIDUAL)) {
1767                 static char prefix[FILE_MAX];
1768                 static char *ext = NULL;
1769                 char str[FILE_MAX];
1770
1771                 if (view_id == 0) {
1772                         char path[FILE_MAX];
1773                         BLI_join_dirfile(path, sizeof(path), seq->strip->dir,
1774                                          seq->strip->stripdata->name);
1775                         BLI_path_abs(path, G.main->name);
1776                         BKE_scene_multiview_view_prefix_get(scene, path, prefix, &ext);
1777                 }
1778
1779                 if (prefix[0] == '\0')
1780                         return view_id != 0;
1781
1782                 seq_multiview_name(scene, view_id, prefix, ext, str, FILE_MAX);
1783
1784                 if (BLI_access(str, R_OK) == 0)
1785                         return false;
1786                 else
1787                         return view_id != 0;
1788         }
1789         return false;
1790 }
1791
1792 /** This returns the maximum possible number of required contexts
1793 */
1794 static size_t seq_proxy_context_count(Sequence *seq, Scene *scene)
1795 {
1796         size_t num_views = 1;
1797
1798         if ((scene->r.scemode & R_MULTIVIEW) == 0)
1799                 return 1;
1800
1801         switch (seq->type) {
1802                 case SEQ_TYPE_MOVIE:
1803                 {
1804                         num_views = BLI_listbase_count(&seq->anims);
1805                         break;
1806                 }
1807                 case SEQ_TYPE_IMAGE:
1808                 {
1809                         switch (seq->views_format) {
1810                                 case R_IMF_VIEWS_INDIVIDUAL:
1811                                         num_views = BKE_scene_multiview_num_views_get(&scene->r);
1812                                         break;
1813                                 case R_IMF_VIEWS_STEREO_3D:
1814                                         num_views = 2;
1815                                         break;
1816                                 case R_IMF_VIEWS_MULTIVIEW:
1817                                         /* not supported at the moment */
1818                                         /* pass through */
1819                                 default:
1820                                         num_views = 1;
1821                         }
1822                         break;
1823                 }
1824         }
1825
1826         return num_views;
1827 }
1828
1829 void BKE_sequencer_proxy_rebuild_context(Main *bmain, Scene *scene, Sequence *seq, struct GSet *file_list, ListBase *queue)
1830 {
1831         SeqIndexBuildContext *context;
1832         Sequence *nseq;
1833         LinkData *link;
1834         size_t i;
1835         size_t num_files;
1836
1837         if (!seq->strip || !seq->strip->proxy) {
1838                 return;
1839         }
1840
1841         if (!(seq->flag & SEQ_USE_PROXY)) {
1842                 return;
1843         }
1844
1845         num_files = seq_proxy_context_count(seq, scene);
1846
1847         for (i = 0; i < num_files; i++) {
1848                 if (seq_proxy_multiview_context_invalid(seq, scene, i))
1849                         continue;
1850
1851                 context = MEM_callocN(sizeof(SeqIndexBuildContext), "seq proxy rebuild context");
1852
1853                 nseq = BKE_sequence_dupli_recursive(scene, scene, seq, 0);
1854
1855                 context->tc_flags   = nseq->strip->proxy->build_tc_flags;
1856                 context->size_flags = nseq->strip->proxy->build_size_flags;
1857                 context->quality    = nseq->strip->proxy->quality;
1858                 context->overwrite = (nseq->strip->proxy->build_flags & SEQ_PROXY_SKIP_EXISTING) == 0;
1859
1860                 context->bmain = bmain;
1861                 context->scene = scene;
1862                 context->orig_seq = seq;
1863                 context->seq = nseq;
1864
1865                 context->view_id = i; /* only for images */
1866
1867                 link = BLI_genericNodeN(context);
1868                 BLI_addtail(queue, link);
1869
1870                 if (nseq->type == SEQ_TYPE_MOVIE) {
1871                         StripAnim *sanim;
1872
1873                         seq_open_anim_file(scene, nseq, true);
1874                         sanim = BLI_findlink(&nseq->anims,  i);
1875
1876                         if (sanim->anim) {
1877                                 context->index_context = IMB_anim_index_rebuild_context(sanim->anim,
1878                                         context->tc_flags, context->size_flags, context->quality,
1879                                         context->overwrite, file_list);
1880                         }
1881                 }
1882         }
1883 }
1884
1885 void BKE_sequencer_proxy_rebuild(SeqIndexBuildContext *context, short *stop, short *do_update, float *progress)
1886 {
1887         const bool overwrite = context->overwrite;
1888         SeqRenderData render_context;
1889         Sequence *seq = context->seq;
1890         Scene *scene = context->scene;
1891         Main *bmain = context->bmain;
1892         int cfra;
1893
1894         if (seq->type == SEQ_TYPE_MOVIE) {
1895                 if (context->index_context) {
1896                         IMB_anim_index_rebuild(context->index_context, stop, do_update, progress);
1897                 }
1898
1899                 return;
1900         }
1901
1902         if (!(seq->flag & SEQ_USE_PROXY)) {
1903                 return;
1904         }
1905
1906         /* that's why it is called custom... */
1907         if (seq->strip->proxy && seq->strip->proxy->storage & SEQ_STORAGE_PROXY_CUSTOM_FILE) {
1908                 return;
1909         }
1910
1911         /* fail safe code */
1912
1913         BKE_sequencer_new_render_data(
1914                 bmain->eval_ctx, bmain, context->scene,
1915                 (scene->r.size * (float) scene->r.xsch) / 100.0f + 0.5f,
1916                 (scene->r.size * (float) scene->r.ysch) / 100.0f + 0.5f, 100,
1917                 &render_context);
1918
1919         render_context.skip_cache = true;
1920         render_context.is_proxy_render = true;
1921         render_context.view_id = context->view_id;
1922
1923         for (cfra = seq->startdisp + seq->startstill;  cfra < seq->enddisp - seq->endstill; cfra++) {
1924                 if (context->size_flags & IMB_PROXY_25) {
1925                         seq_proxy_build_frame(&render_context, seq, cfra, 25, overwrite);
1926                 }
1927                 if (context->size_flags & IMB_PROXY_50) {
1928                         seq_proxy_build_frame(&render_context, seq, cfra, 50, overwrite);
1929                 }
1930                 if (context->size_flags & IMB_PROXY_75) {
1931                         seq_proxy_build_frame(&render_context, seq, cfra, 75, overwrite);
1932                 }
1933                 if (context->size_flags & IMB_PROXY_100) {
1934                         seq_proxy_build_frame(&render_context, seq, cfra, 100, overwrite);
1935                 }
1936
1937                 *progress = (float) (cfra - seq->startdisp - seq->startstill) / (seq->enddisp - seq->endstill - seq->startdisp - seq->startstill);
1938                 *do_update = true;
1939
1940                 if (*stop || G.is_break)
1941                         break;
1942         }
1943 }
1944
1945 void BKE_sequencer_proxy_rebuild_finish(SeqIndexBuildContext *context, bool stop)
1946 {
1947         if (context->index_context) {
1948                 StripAnim *sanim;
1949
1950                 for (sanim = context->seq->anims.first; sanim; sanim = sanim->next)
1951                         IMB_close_anim_proxies(sanim->anim);
1952
1953                 for (sanim = context->orig_seq->anims.first; sanim; sanim = sanim->next)
1954                         IMB_close_anim_proxies(sanim->anim);
1955
1956                 IMB_anim_index_rebuild_finish(context->index_context, stop);
1957         }
1958
1959         seq_free_sequence_recurse(NULL, context->seq);
1960
1961         MEM_freeN(context);
1962 }
1963
1964 void BKE_sequencer_proxy_set(struct Sequence *seq, bool value)
1965 {
1966         if (value) {
1967                 seq->flag |= SEQ_USE_PROXY;
1968                 if (seq->strip->proxy == NULL) {
1969                         seq->strip->proxy = MEM_callocN(sizeof(struct StripProxy), "StripProxy");
1970                         seq->strip->proxy->quality = 90;
1971                         seq->strip->proxy->build_tc_flags = SEQ_PROXY_TC_ALL;
1972                         seq->strip->proxy->build_size_flags = SEQ_PROXY_IMAGE_SIZE_25;
1973                 }
1974         }
1975         else {
1976                 seq->flag ^= SEQ_USE_PROXY;
1977         }       
1978 }
1979
1980 /*********************** color balance *************************/
1981
1982 static StripColorBalance calc_cb(StripColorBalance *cb_)
1983 {
1984         StripColorBalance cb = *cb_;
1985         int c;
1986
1987         for (c = 0; c < 3; c++) {
1988                 cb.lift[c] = 2.0f - cb.lift[c];
1989         }
1990
1991         if (cb.flag & SEQ_COLOR_BALANCE_INVERSE_LIFT) {
1992                 for (c = 0; c < 3; c++) {
1993                         /* tweak to give more subtle results
1994                          * values above 1.0 are scaled */
1995                         if (cb.lift[c] > 1.0f)
1996                                 cb.lift[c] = pow(cb.lift[c] - 1.0f, 2.0) + 1.0;
1997
1998                         cb.lift[c] = 2.0f - cb.lift[c];
1999                 }
2000         }
2001
2002         if (cb.flag & SEQ_COLOR_BALANCE_INVERSE_GAIN) {
2003                 for (c = 0; c < 3; c++) {
2004                         if (cb.gain[c] != 0.0f) {
2005                                 cb.gain[c] = 1.0f / cb.gain[c];
2006                         }
2007                         else {
2008                                 cb.gain[c] = 1000000; /* should be enough :) */
2009                         }
2010                 }
2011         }
2012
2013         if (!(cb.flag & SEQ_COLOR_BALANCE_INVERSE_GAMMA)) {
2014                 for (c = 0; c < 3; c++) {
2015                         if (cb.gamma[c] != 0.0f) {
2016                                 cb.gamma[c] = 1.0f / cb.gamma[c];
2017                         }
2018                         else {
2019                                 cb.gamma[c] = 1000000; /* should be enough :) */
2020                         }
2021                 }
2022         }
2023
2024         return cb;
2025 }
2026
2027 /* note: lift is actually 2-lift */
2028 MINLINE float color_balance_fl(float in, const float lift, const float gain, const float gamma, const float mul)
2029 {
2030         float x = (((in - 1.0f) * lift) + 1.0f) * gain;
2031
2032         /* prevent NaN */
2033         if (x < 0.f)
2034                 x = 0.f;
2035
2036         return powf(x, gamma) * mul;
2037 }
2038
2039 static void make_cb_table_float(float lift, float gain, float gamma,
2040                                 float *table, float mul)
2041 {
2042         int y;
2043
2044         for (y = 0; y < 256; y++) {
2045                 float v = color_balance_fl((float)y * (1.0f / 255.0f), lift, gain, gamma, mul);
2046
2047                 table[y] = v;
2048         }
2049 }
2050
2051 static void color_balance_byte_byte(StripColorBalance *cb_, unsigned char *rect, unsigned char *mask_rect, int width, int height, float mul)
2052 {
2053         //unsigned char cb_tab[3][256];
2054         unsigned char *cp = rect;
2055         unsigned char *e = cp + width * 4 * height;
2056         unsigned char *m = mask_rect;
2057
2058         StripColorBalance cb = calc_cb(cb_);
2059
2060         while (cp < e) {
2061                 float p[4];
2062                 int c;
2063
2064                 straight_uchar_to_premul_float(p, cp);
2065
2066                 for (c = 0; c < 3; c++) {
2067                         float t = color_balance_fl(p[c], cb.lift[c], cb.gain[c], cb.gamma[c], mul);
2068
2069                         if (m) {
2070                                 float m_normal = (float) m[c] / 255.0f;
2071
2072                                 p[c] = p[c] * (1.0f - m_normal) + t * m_normal;
2073                         }
2074                         else
2075                                 p[c] = t;
2076                 }
2077                 
2078                 premul_float_to_straight_uchar(cp, p);
2079
2080                 cp += 4;
2081                 if (m)
2082                         m += 4;
2083         }
2084 }
2085
2086 static void color_balance_byte_float(StripColorBalance *cb_, unsigned char *rect, float *rect_float, unsigned char *mask_rect, int width, int height, float mul)
2087 {
2088         float cb_tab[4][256];
2089         int c, i;
2090         unsigned char *p = rect;
2091         unsigned char *e = p + width * 4 * height;
2092         unsigned char *m = mask_rect;
2093         float *o;
2094         StripColorBalance cb;
2095
2096         o = rect_float;
2097
2098         cb = calc_cb(cb_);
2099
2100         for (c = 0; c < 3; c++) {
2101                 make_cb_table_float(cb.lift[c], cb.gain[c], cb.gamma[c], cb_tab[c], mul);
2102         }
2103
2104         for (i = 0; i < 256; i++) {
2105                 cb_tab[3][i] = ((float)i) * (1.0f / 255.0f);
2106         }
2107
2108         while (p < e) {
2109                 if (m) {
2110                         float t[3] = {m[0] / 255.0f, m[1] / 255.0f, m[2] / 255.0f};
2111
2112                         p[0] = p[0] * (1.0f - t[0]) + t[0] * cb_tab[0][p[0]];
2113                         p[1] = p[1] * (1.0f - t[1]) + t[1] * cb_tab[1][p[1]];
2114                         p[2] = p[2] * (1.0f - t[2]) + t[2] * cb_tab[2][p[2]];
2115
2116                         m += 4;
2117                 }
2118                 else {
2119                         o[0] = cb_tab[0][p[0]];
2120                         o[1] = cb_tab[1][p[1]];
2121                         o[2] = cb_tab[2][p[2]];
2122                 }
2123
2124                 o[3] = cb_tab[3][p[3]];
2125
2126                 p += 4; o += 4;
2127         }
2128 }
2129
2130 static void color_balance_float_float(StripColorBalance *cb_, float *rect_float, float *mask_rect_float, int width, int height, float mul)
2131 {
2132         float *p = rect_float;
2133         const float *e = rect_float + width * 4 * height;
2134         const float *m = mask_rect_float;
2135         StripColorBalance cb = calc_cb(cb_);
2136
2137         while (p < e) {
2138                 int c;
2139                 for (c = 0; c < 3; c++) {
2140                         float t = color_balance_fl(p[c], cb.lift[c], cb.gain[c], cb.gamma[c], mul);
2141
2142                         if (m)
2143                                 p[c] = p[c] * (1.0f - m[c]) + t * m[c];
2144                         else
2145                                 p[c] = t;
2146                 }
2147
2148                 p += 4;
2149                 if (m)
2150                         m += 4;
2151         }
2152 }
2153
2154 typedef struct ColorBalanceInitData {
2155         StripColorBalance *cb;
2156         ImBuf *ibuf;
2157         float mul;
2158         ImBuf *mask;
2159         bool make_float;
2160 } ColorBalanceInitData;
2161
2162 typedef struct ColorBalanceThread {
2163         StripColorBalance *cb;
2164         float mul;
2165
2166         int width, height;
2167
2168         unsigned char *rect, *mask_rect;
2169         float *rect_float, *mask_rect_float;
2170
2171         bool make_float;
2172 } ColorBalanceThread;
2173
2174 static void color_balance_init_handle(void *handle_v, int start_line, int tot_line, void *init_data_v)
2175 {
2176         ColorBalanceThread *handle = (ColorBalanceThread *) handle_v;
2177         ColorBalanceInitData *init_data = (ColorBalanceInitData *) init_data_v;
2178         ImBuf *ibuf = init_data->ibuf;
2179         ImBuf *mask = init_data->mask;
2180
2181         int offset = 4 * start_line * ibuf->x;
2182
2183         memset(handle, 0, sizeof(ColorBalanceThread));
2184
2185         handle->cb = init_data->cb;
2186         handle->mul = init_data->mul;
2187         handle->width = ibuf->x;
2188         handle->height = tot_line;
2189         handle->make_float = init_data->make_float;
2190
2191         if (ibuf->rect)
2192                 handle->rect = (unsigned char *) ibuf->rect + offset;
2193
2194         if (ibuf->rect_float)
2195                 handle->rect_float = ibuf->rect_float + offset;
2196
2197         if (mask) {
2198                 if (mask->rect)
2199                         handle->mask_rect = (unsigned char *) mask->rect + offset;
2200
2201                 if (mask->rect_float)
2202                         handle->mask_rect_float = mask->rect_float + offset;
2203         }
2204         else {
2205                 handle->mask_rect = NULL;
2206                 handle->mask_rect_float = NULL;
2207         }
2208 }
2209
2210 static void *color_balance_do_thread(void *thread_data_v)
2211 {
2212         ColorBalanceThread *thread_data = (ColorBalanceThread *) thread_data_v;
2213         StripColorBalance *cb = thread_data->cb;
2214         int width = thread_data->width, height = thread_data->height;
2215         unsigned char *rect = thread_data->rect;
2216         unsigned char *mask_rect = thread_data->mask_rect;
2217         float *rect_float = thread_data->rect_float;
2218         float *mask_rect_float = thread_data->mask_rect_float;
2219         float mul = thread_data->mul;
2220
2221         if (rect_float) {
2222                 color_balance_float_float(cb, rect_float, mask_rect_float, width, height, mul);
2223         }
2224         else if (thread_data->make_float) {
2225                 color_balance_byte_float(cb, rect, rect_float, mask_rect, width, height, mul);
2226         }
2227         else {
2228                 color_balance_byte_byte(cb, rect, mask_rect, width, height, mul);
2229         }
2230
2231         return NULL;
2232 }
2233
2234 /* cfra is offset by fra_offset only in case we are using a real mask. */
2235 ImBuf *BKE_sequencer_render_mask_input(
2236         const SeqRenderData *context, int mask_input_type, Sequence *mask_sequence, Mask *mask_id,
2237         int cfra, int fra_offset, bool make_float)
2238 {
2239         ImBuf *mask_input = NULL;
2240
2241         if (mask_input_type == SEQUENCE_MASK_INPUT_STRIP) {
2242                 if (mask_sequence) {
2243                         mask_input = seq_render_strip(context, mask_sequence, cfra);
2244
2245                         if (make_float) {
2246                                 if (!mask_input->rect_float)
2247                                         IMB_float_from_rect(mask_input);
2248                         }
2249                         else {
2250                                 if (!mask_input->rect)
2251                                         IMB_rect_from_float(mask_input);
2252                         }
2253                 }
2254         }
2255         else if (mask_input_type == SEQUENCE_MASK_INPUT_ID) {
2256                 mask_input = seq_render_mask(context, mask_id, cfra - fra_offset, make_float);
2257         }
2258
2259         return mask_input;
2260 }
2261
2262 void BKE_sequencer_color_balance_apply(StripColorBalance *cb, ImBuf *ibuf, float mul, bool make_float, ImBuf *mask_input)
2263 {
2264         ColorBalanceInitData init_data;
2265
2266         if (!ibuf->rect_float && make_float)
2267                 imb_addrectfloatImBuf(ibuf);
2268
2269         init_data.cb = cb;
2270         init_data.ibuf = ibuf;
2271         init_data.mul = mul;
2272         init_data.make_float = make_float;
2273         init_data.mask = mask_input;
2274
2275         IMB_processor_apply_threaded(ibuf->y, sizeof(ColorBalanceThread), &init_data,
2276                                      color_balance_init_handle, color_balance_do_thread);
2277
2278         /* color balance either happens on float buffer or byte buffer, but never on both,
2279          * free byte buffer if there's float buffer since float buffer would be used for
2280          * color balance in favor of byte buffer
2281          */
2282         if (ibuf->rect_float && ibuf->rect)
2283                 imb_freerectImBuf(ibuf);
2284 }
2285
2286 /*
2287  *  input preprocessing for SEQ_TYPE_IMAGE, SEQ_TYPE_MOVIE, SEQ_TYPE_MOVIECLIP and SEQ_TYPE_SCENE
2288  *
2289  *  Do all the things you can't really do afterwards using sequence effects
2290  *  (read: before rescaling to render resolution has been done)
2291  *
2292  *  Order is important!
2293  *
2294  *  - Deinterlace
2295  *  - Crop and transform in image source coordinate space
2296  *  - Flip X + Flip Y (could be done afterwards, backward compatibility)
2297  *  - Promote image to float data (affects pipeline operations afterwards)
2298  *  - Color balance (is most efficient in the byte -> float
2299  *    (future: half -> float should also work fine!)
2300  *    case, if done on load, since we can use lookup tables)
2301  *  - Premultiply
2302  */
2303
2304 bool BKE_sequencer_input_have_to_preprocess(const SeqRenderData *context, Sequence *seq, float UNUSED(cfra))
2305 {
2306         float mul;
2307
2308         if (context->is_proxy_render) {
2309                 return false;
2310         }
2311
2312         if (seq->flag & (SEQ_FILTERY | SEQ_USE_CROP | SEQ_USE_TRANSFORM | SEQ_FLIPX | SEQ_FLIPY | SEQ_MAKE_FLOAT)) {
2313                 return true;
2314         }
2315
2316         mul = seq->mul;
2317
2318         if (seq->blend_mode == SEQ_BLEND_REPLACE) {
2319                 mul *= seq->blend_opacity / 100.0f;
2320         }
2321
2322         if (mul != 1.0f) {
2323                 return true;
2324         }
2325
2326         if (seq->sat != 1.0f) {
2327                 return true;
2328         }
2329
2330         if (seq->modifiers.first) {
2331                 return true;
2332         }
2333
2334         return false;
2335 }
2336
2337 static ImBuf *input_preprocess(const SeqRenderData *context, Sequence *seq, float cfra, ImBuf *ibuf,
2338                                const bool is_proxy_image, const bool is_preprocessed)
2339 {
2340         Scene *scene = context->scene;
2341         float mul;
2342
2343         ibuf = IMB_makeSingleUser(ibuf);
2344
2345         if ((seq->flag & SEQ_FILTERY) &&
2346             !ELEM(seq->type, SEQ_TYPE_MOVIE, SEQ_TYPE_MOVIECLIP))
2347         {
2348                 IMB_filtery(ibuf);
2349         }
2350
2351         if (seq->flag & (SEQ_USE_CROP | SEQ_USE_TRANSFORM)) {
2352                 StripCrop c = {0};
2353                 StripTransform t = {0};
2354                 int sx, sy, dx, dy;
2355
2356                 if (is_proxy_image) {
2357                         double f = seq_rendersize_to_scale_factor(context->preview_render_size);
2358
2359                         if (f != 1.0) {
2360                                 IMB_scalefastImBuf(ibuf, ibuf->x / f, ibuf->y / f);
2361                         }
2362                 }
2363
2364                 if (seq->flag & SEQ_USE_CROP && seq->strip->crop) {
2365                         c = *seq->strip->crop;
2366                 }
2367                 if (seq->flag & SEQ_USE_TRANSFORM && seq->strip->transform) {
2368                         t = *seq->strip->transform;
2369                 }
2370
2371                 if (is_preprocessed) {
2372                         double xscale = scene->r.xsch ? ((double)context->rectx / (double)scene->r.xsch) : 1.0;
2373                         double yscale = scene->r.ysch ? ((double)context->recty / (double)scene->r.ysch) : 1.0;
2374                         if (seq->flag & SEQ_USE_TRANSFORM) {
2375                                 t.xofs *= xscale;
2376                                 t.yofs *= yscale;
2377                         }
2378                         if (seq->flag & SEQ_USE_CROP) {
2379                                 c.left *= xscale;
2380                                 c.right *= xscale;
2381                                 c.top *= yscale;
2382                                 c.bottom *= yscale;
2383                         }
2384                 }
2385
2386                 sx = ibuf->x - c.left - c.right;
2387                 sy = ibuf->y - c.top - c.bottom;
2388
2389                 if (seq->flag & SEQ_USE_TRANSFORM) {
2390                         if (is_preprocessed) {
2391                                 dx = context->rectx;
2392                                 dy = context->recty;
2393                         }
2394                         else {
2395                                 dx = scene->r.xsch;
2396                                 dy = scene->r.ysch;
2397                         }
2398                 }
2399                 else {
2400                         dx = sx;
2401                         dy = sy;
2402                 }
2403
2404                 if (c.top  + c.bottom >= ibuf->y ||
2405                     c.left + c.right  >= ibuf->x ||
2406                     t.xofs >= dx || t.yofs >= dy)
2407                 {
2408                         make_black_ibuf(ibuf);
2409                 }
2410                 else {
2411                         ImBuf *i = IMB_allocImBuf(dx, dy, 32, ibuf->rect_float ? IB_rectfloat : IB_rect);
2412
2413                         IMB_rectcpy(i, ibuf, t.xofs, t.yofs, c.left, c.bottom, sx, sy);
2414                         sequencer_imbuf_assign_spaces(scene, i);
2415
2416                         IMB_freeImBuf(ibuf);
2417
2418                         ibuf = i;
2419                 }
2420         }
2421
2422         if (seq->flag & SEQ_FLIPX) {
2423                 IMB_flipx(ibuf);
2424         }
2425         
2426         if (seq->flag & SEQ_FLIPY) {
2427                 IMB_flipy(ibuf);
2428         }
2429
2430         if (seq->sat != 1.0f) {
2431                 IMB_saturation(ibuf, seq->sat);
2432         }
2433
2434         mul = seq->mul;
2435
2436         if (seq->blend_mode == SEQ_BLEND_REPLACE) {
2437                 mul *= seq->blend_opacity / 100.0f;
2438         }
2439
2440         if (seq->flag & SEQ_MAKE_FLOAT) {
2441                 if (!ibuf->rect_float) {
2442                         BKE_sequencer_imbuf_to_sequencer_space(scene, ibuf, true);
2443                 }
2444
2445                 if (ibuf->rect) {
2446                         imb_freerectImBuf(ibuf);
2447                 }
2448         }
2449
2450         if (mul != 1.0f) {
2451                 multibuf(ibuf, mul);
2452         }
2453
2454         if (ibuf->x != context->rectx || ibuf->y != context->recty) {
2455                 if (scene->r.mode & R_OSA) {
2456                         IMB_scaleImBuf(ibuf, (short)context->rectx, (short)context->recty);
2457                 }
2458                 else {
2459                         IMB_scalefastImBuf(ibuf, (short)context->rectx, (short)context->recty);
2460                 }
2461         }
2462
2463         if (seq->modifiers.first) {
2464                 ImBuf *ibuf_new = BKE_sequence_modifier_apply_stack(context, seq, ibuf, cfra);
2465
2466                 if (ibuf_new != ibuf) {
2467                         IMB_freeImBuf(ibuf);
2468                         ibuf = ibuf_new;
2469                 }
2470         }
2471
2472         return ibuf;
2473 }
2474
2475 static ImBuf *copy_from_ibuf_still(const SeqRenderData *context, Sequence *seq, float nr)
2476 {
2477         ImBuf *rval = NULL;
2478         ImBuf *ibuf = NULL;
2479
2480         if (nr == 0) {
2481                 ibuf = BKE_sequencer_cache_get(context, seq, seq->start, SEQ_STRIPELEM_IBUF_STARTSTILL);
2482         }
2483         else if (nr == seq->len - 1) {
2484                 ibuf = BKE_sequencer_cache_get(context, seq, seq->start, SEQ_STRIPELEM_IBUF_ENDSTILL);
2485         }
2486
2487         if (ibuf) {
2488                 rval = IMB_dupImBuf(ibuf);
2489                 IMB_freeImBuf(ibuf);
2490         }
2491
2492         return rval;
2493 }
2494
2495 static void copy_to_ibuf_still(const SeqRenderData *context, Sequence *seq, float nr, ImBuf *ibuf)
2496 {
2497         /* warning: ibuf may be NULL if the video fails to load */
2498         if (nr == 0 || nr == seq->len - 1) {
2499                 /* we have to store a copy, since the passed ibuf
2500                  * could be preprocessed afterwards (thereby silently
2501                  * changing the cached image... */
2502                 ibuf = IMB_dupImBuf(ibuf);
2503
2504                 if (ibuf) {
2505                         sequencer_imbuf_assign_spaces(context->scene, ibuf);
2506                 }
2507
2508                 if (nr == 0) {
2509                         BKE_sequencer_cache_put(context, seq, seq->start, SEQ_STRIPELEM_IBUF_STARTSTILL, ibuf);
2510                 }
2511
2512                 if (nr == seq->len - 1) {
2513                         BKE_sequencer_cache_put(context, seq, seq->start, SEQ_STRIPELEM_IBUF_ENDSTILL, ibuf);
2514                 }
2515
2516                 IMB_freeImBuf(ibuf);
2517         }
2518 }
2519
2520 /*********************** strip rendering functions  *************************/
2521
2522 typedef struct RenderEffectInitData {
2523         struct SeqEffectHandle *sh;
2524         const SeqRenderData *context;
2525         Sequence *seq;
2526         float cfra, facf0, facf1;
2527         ImBuf *ibuf1, *ibuf2, *ibuf3;
2528
2529         ImBuf *out;
2530 } RenderEffectInitData;
2531
2532 typedef struct RenderEffectThread {
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         int start_line, tot_line;
2541 } RenderEffectThread;
2542
2543 static void render_effect_execute_init_handle(void *handle_v, int start_line, int tot_line, void *init_data_v)
2544 {
2545         RenderEffectThread *handle = (RenderEffectThread *) handle_v;
2546         RenderEffectInitData *init_data = (RenderEffectInitData *) init_data_v;
2547
2548         handle->sh = init_data->sh;
2549         handle->context = init_data->context;
2550         handle->seq = init_data->seq;
2551         handle->cfra = init_data->cfra;
2552         handle->facf0 = init_data->facf0;
2553         handle->facf1 = init_data->facf1;
2554         handle->ibuf1 = init_data->ibuf1;
2555         handle->ibuf2 = init_data->ibuf2;
2556         handle->ibuf3 = init_data->ibuf3;
2557         handle->out = init_data->out;
2558
2559         handle->start_line = start_line;
2560         handle->tot_line = tot_line;
2561 }
2562
2563 static void *render_effect_execute_do_thread(void *thread_data_v)
2564 {
2565         RenderEffectThread *thread_data = (RenderEffectThread *) thread_data_v;
2566
2567         thread_data->sh->execute_slice(thread_data->context, thread_data->seq, thread_data->cfra,
2568                                        thread_data->facf0, thread_data->facf1, thread_data->ibuf1,
2569                                        thread_data->ibuf2, thread_data->ibuf3, thread_data->start_line,
2570                                        thread_data->tot_line, thread_data->out);
2571
2572         return NULL;
2573 }
2574
2575 static ImBuf *seq_render_effect_execute_threaded(struct SeqEffectHandle *sh, const SeqRenderData *context, Sequence *seq,
2576                                                  float cfra, float facf0, float facf1,
2577                                                  ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *ibuf3)
2578 {
2579         RenderEffectInitData init_data;
2580         ImBuf *out = sh->init_execution(context, ibuf1, ibuf2, ibuf3);
2581
2582         init_data.sh = sh;
2583         init_data.context = context;
2584         init_data.seq = seq;
2585         init_data.cfra = cfra;
2586         init_data.facf0 = facf0;
2587         init_data.facf1 = facf1;
2588         init_data.ibuf1 = ibuf1;
2589         init_data.ibuf2 = ibuf2;
2590         init_data.ibuf3 = ibuf3;
2591         init_data.out = out;
2592
2593         IMB_processor_apply_threaded(out->y, sizeof(RenderEffectThread), &init_data,
2594                                      render_effect_execute_init_handle, render_effect_execute_do_thread);
2595
2596         return out;
2597 }
2598
2599 static ImBuf *seq_render_effect_strip_impl(const SeqRenderData *context, Sequence *seq, float cfra)
2600 {
2601         Scene *scene = context->scene;
2602         float fac, facf;
2603         int early_out;
2604         int i;
2605         struct SeqEffectHandle sh = BKE_sequence_get_effect(seq);
2606         FCurve *fcu = NULL;
2607         ImBuf *ibuf[3];
2608         Sequence *input[3];
2609         ImBuf *out = NULL;
2610
2611         ibuf[0] = ibuf[1] = ibuf[2] = NULL;
2612
2613         input[0] = seq->seq1; input[1] = seq->seq2; input[2] = seq->seq3;
2614
2615         if (!sh.execute && !(sh.execute_slice && sh.init_execution)) {
2616                 /* effect not supported in this version... */
2617                 out = IMB_allocImBuf(context->rectx, context->recty, 32, IB_rect);
2618                 return out;
2619         }
2620
2621         if (seq->flag & SEQ_USE_EFFECT_DEFAULT_FADE) {
2622                 sh.get_default_fac(seq, cfra, &fac, &facf);
2623                 
2624                 if ((scene->r.mode & R_FIELDS) == 0)
2625                         facf = fac;
2626         }
2627         else {
2628                 fcu = id_data_find_fcurve(&scene->id, seq, &RNA_Sequence, "effect_fader", 0, NULL);
2629                 if (fcu) {
2630                         fac = facf = evaluate_fcurve(fcu, cfra);
2631                         if (scene->r.mode & R_FIELDS) {
2632                                 facf = evaluate_fcurve(fcu, cfra + 0.5f);
2633                         }
2634                 }
2635                 else {
2636                         fac = facf = seq->effect_fader;
2637                 }
2638         }
2639
2640         early_out = sh.early_out(seq, fac, facf);
2641
2642         switch (early_out) {
2643                 case EARLY_NO_INPUT:
2644                         out = sh.execute(context, seq, cfra, fac, facf, NULL, NULL, NULL);
2645                         break;
2646                 case EARLY_DO_EFFECT:
2647                         for (i = 0; i < 3; i++) {
2648                                 if (input[i])
2649                                         ibuf[i] = seq_render_strip(context, input[i], cfra);
2650                         }
2651
2652                         if (ibuf[0] && ibuf[1]) {
2653                                 if (sh.multithreaded)
2654                                         out = seq_render_effect_execute_threaded(&sh, context, seq, cfra, fac, facf, ibuf[0], ibuf[1], ibuf[2]);
2655                                 else
2656                                         out = sh.execute(context, seq, cfra, fac, facf, ibuf[0], ibuf[1], ibuf[2]);
2657                         }
2658                         break;
2659                 case EARLY_USE_INPUT_1:
2660                         if (input[0]) {
2661                                 ibuf[0] = seq_render_strip(context, input[0], cfra);
2662                         }
2663                         if (ibuf[0]) {
2664                                 if (BKE_sequencer_input_have_to_preprocess(context, seq, cfra)) {
2665                                         out = IMB_dupImBuf(ibuf[0]);
2666                                 }
2667                                 else {
2668                                         out = ibuf[0];
2669                                         IMB_refImBuf(out);
2670                                 }
2671                         }
2672                         break;
2673                 case EARLY_USE_INPUT_2:
2674                         if (input[1]) {
2675                                 ibuf[1] = seq_render_strip(context, input[1], cfra);
2676                         }
2677                         if (ibuf[1]) {
2678                                 if (BKE_sequencer_input_have_to_preprocess(context, seq, cfra)) {
2679                                         out = IMB_dupImBuf(ibuf[1]);
2680                                 }
2681                                 else {
2682                                         out = ibuf[1];
2683                                         IMB_refImBuf(out);
2684                                 }
2685                         }
2686                         break;
2687         }
2688
2689         for (i = 0; i < 3; i++) {
2690                 IMB_freeImBuf(ibuf[i]);
2691         }
2692
2693         if (out == NULL) {
2694                 out = IMB_allocImBuf(context->rectx, context->recty, 32, IB_rect);
2695         }
2696
2697         return out;
2698 }
2699
2700 static ImBuf *seq_render_image_strip(const SeqRenderData *context, Sequence *seq, float nr, float cfra)
2701 {
2702         ImBuf *ibuf = NULL;
2703         char name[FILE_MAX];
2704         bool is_multiview = (seq->flag & SEQ_USE_VIEWS) != 0 &&
2705                             (context->scene->r.scemode & R_MULTIVIEW) != 0;
2706         StripElem *s_elem = BKE_sequencer_give_stripelem(seq, cfra);
2707         int flag;
2708
2709         if (s_elem) {
2710                 BLI_join_dirfile(name, sizeof(name), seq->strip->dir, s_elem->name);
2711                 BLI_path_abs(name, G.main->name);
2712         }
2713
2714         flag = IB_rect;
2715         if (seq->alpha_mode == SEQ_ALPHA_PREMUL)
2716                 flag |= IB_alphamode_premul;
2717
2718         if (!s_elem) {
2719                 /* don't do anything */
2720         }
2721         else if (is_multiview) {
2722                 size_t totfiles = seq_num_files(context->scene, seq->views_format, true);
2723                 size_t totviews;
2724                 struct ImBuf **ibufs_arr;
2725                 char prefix[FILE_MAX];
2726                 char *ext = NULL;
2727                 int i;
2728
2729                 if (totfiles > 1) {
2730                         BKE_scene_multiview_view_prefix_get(context->scene, name, prefix, &ext);
2731                         if (prefix[0] == '\0') {
2732                                 goto monoview_image;
2733                         }
2734                 }
2735
2736                 totviews = BKE_scene_multiview_num_views_get(&context->scene->r);
2737                 ibufs_arr = MEM_callocN(sizeof(ImBuf *) * totviews, "Sequence Image Views Imbufs");
2738
2739                 for (i = 0; i < totfiles; i++) {
2740
2741                         if (prefix[0] == '\0') {
2742                                 ibufs_arr[i] = IMB_loadiffname(name, flag, seq->strip->colorspace_settings.name);
2743                         }
2744                         else {
2745                                 char str[FILE_MAX];
2746                                 seq_multiview_name(context->scene, i, prefix, ext, str, FILE_MAX);
2747                                 ibufs_arr[i] = IMB_loadiffname(str, flag, seq->strip->colorspace_settings.name);
2748                         }
2749
2750                         if (ibufs_arr[i]) {
2751                                 /* we don't need both (speed reasons)! */
2752                                 if (ibufs_arr[i]->rect_float && ibufs_arr[i]->rect)
2753                                         imb_freerectImBuf(ibufs_arr[i]);
2754                         }
2755                 }
2756
2757                 if (seq->views_format == R_IMF_VIEWS_STEREO_3D && ibufs_arr[0])
2758                         IMB_ImBufFromStereo3d(seq->stereo3d_format, ibufs_arr[0], &ibufs_arr[0], &ibufs_arr[1]);
2759
2760                 for (i = 0; i < totviews; i++) {
2761                         if (ibufs_arr[i]) {
2762                                 SeqRenderData localcontext = *context;
2763                                 localcontext.view_id = i;
2764
2765                                 /* all sequencer color is done in SRGB space, linear gives odd crossfades */
2766                                 BKE_sequencer_imbuf_to_sequencer_space(context->scene, ibufs_arr[i], false);
2767
2768                                 if (i != context->view_id) {
2769                                         copy_to_ibuf_still(&localcontext, seq, nr, ibufs_arr[i]);
2770                                         BKE_sequencer_cache_put(&localcontext, seq, cfra, SEQ_STRIPELEM_IBUF, ibufs_arr[i]);
2771                                 }
2772                         }
2773                 }
2774
2775                 /* return the original requested ImBuf */
2776                 ibuf = ibufs_arr[context->view_id];
2777                 if (ibuf) {
2778                         s_elem->orig_width  = ibufs_arr[0]->x;
2779                         s_elem->orig_height = ibufs_arr[0]->y;
2780                 }
2781
2782                 /* "remove" the others (decrease their refcount) */
2783                 for (i = 0; i < totviews; i++) {
2784                         if (ibufs_arr[i] != ibuf) {
2785                                 IMB_freeImBuf(ibufs_arr[i]);
2786                         }
2787                 }
2788
2789                 MEM_freeN(ibufs_arr);
2790         }
2791         else {
2792 monoview_image:
2793                 if ((ibuf = IMB_loadiffname(name, flag, seq->strip->colorspace_settings.name))) {
2794                         /* we don't need both (speed reasons)! */
2795                         if (ibuf->rect_float && ibuf->rect)
2796                                 imb_freerectImBuf(ibuf);
2797
2798                         /* all sequencer color is done in SRGB space, linear gives odd crossfades */
2799                         BKE_sequencer_imbuf_to_sequencer_space(context->scene, ibuf, false);
2800
2801                         s_elem->orig_width  = ibuf->x;
2802                         s_elem->orig_height = ibuf->y;
2803                 }
2804         }
2805
2806         return ibuf;
2807 }
2808
2809 static ImBuf *seq_render_movie_strip(const SeqRenderData *context, Sequence *seq, float nr, float cfra)
2810 {
2811         ImBuf *ibuf = NULL;
2812         StripAnim *sanim;
2813         bool is_multiview = (seq->flag & SEQ_USE_VIEWS) != 0 &&
2814                             (context->scene->r.scemode & R_MULTIVIEW) != 0;
2815
2816         /* load all the videos */
2817         seq_open_anim_file(context->scene, seq, false);
2818
2819         if (is_multiview) {
2820                 ImBuf **ibuf_arr;
2821                 size_t totviews;
2822                 size_t totfiles = seq_num_files(context->scene, seq->views_format, true);
2823                 int i;
2824
2825                 if (totfiles != BLI_listbase_count_ex(&seq->anims, totfiles + 1))
2826                         goto monoview_movie;
2827
2828                 totviews = BKE_scene_multiview_num_views_get(&context->scene->r);
2829                 ibuf_arr = MEM_callocN(sizeof(ImBuf *) * totviews, "Sequence Image Views Imbufs");
2830
2831                 for (i = 0, sanim = seq->anims.first; sanim; sanim = sanim->next, i++) {
2832                         if (sanim->anim) {
2833                                 IMB_Proxy_Size proxy_size = seq_rendersize_to_proxysize(context->preview_render_size);
2834                                 IMB_anim_set_preseek(sanim->anim, seq->anim_preseek);
2835
2836                                 ibuf_arr[i] = IMB_anim_absolute(sanim->anim, nr + seq->anim_startofs,
2837                                                                 seq->strip->proxy ? seq->strip->proxy->tc : IMB_TC_RECORD_RUN,
2838                                                                 proxy_size);
2839
2840                         /* fetching for requested proxy size failed, try fetching the original instead */
2841                         if (!ibuf_arr[i] && proxy_size != IMB_PROXY_NONE) {
2842                                 ibuf_arr[i] = IMB_anim_absolute(sanim->anim, nr + seq->anim_startofs,
2843                                                                 seq->strip->proxy ? seq->strip->proxy->tc : IMB_TC_RECORD_RUN,
2844                                                                 IMB_PROXY_NONE);
2845                         }
2846                                 if (ibuf_arr[i]) {
2847                                         /* we don't need both (speed reasons)! */
2848                                         if (ibuf_arr[i]->rect_float && ibuf_arr[i]->rect)
2849                                                 imb_freerectImBuf(ibuf_arr[i]);
2850                                 }
2851                         }
2852                 }
2853
2854                 if (seq->views_format == R_IMF_VIEWS_STEREO_3D) {
2855                         if (ibuf_arr[0]) {
2856                                 IMB_ImBufFromStereo3d(seq->stereo3d_format, ibuf_arr[0], &ibuf_arr[0], &ibuf_arr[1]);
2857                         }
2858                         else {
2859                                 /* probably proxy hasn't been created yet */
2860                                 MEM_freeN(ibuf_arr);
2861                                 return NULL;
2862                         }
2863                 }
2864
2865                 for (i = 0; i < totviews; i++) {
2866                         SeqRenderData localcontext = *context;
2867                         localcontext.view_id = i;
2868
2869                         if (ibuf_arr[i]) {
2870                                 /* all sequencer color is done in SRGB space, linear gives odd crossfades */
2871                                 BKE_sequencer_imbuf_to_sequencer_space(context->scene, ibuf_arr[i], false);
2872                         }
2873                         if (i != context->view_id) {
2874                                 copy_to_ibuf_still(&localcontext, seq, nr, ibuf_arr[i]);
2875                                 BKE_sequencer_cache_put(&localcontext, seq, cfra, SEQ_STRIPELEM_IBUF, ibuf_arr[i]);
2876                         }
2877                 }
2878
2879                 /* return the original requested ImBuf */
2880                 ibuf = ibuf_arr[context->view_id];
2881                 if (ibuf) {
2882                         seq->strip->stripdata->orig_width = ibuf->x;
2883                         seq->strip->stripdata->orig_height = ibuf->y;
2884                 }
2885
2886                 /* "remove" the others (decrease their refcount) */
2887                 for (i = 0; i < totviews; i++) {
2888                         if (ibuf_arr[i] != ibuf) {
2889                                 IMB_freeImBuf(ibuf_arr[i]);
2890                         }
2891                 }
2892
2893                 MEM_freeN(ibuf_arr);
2894         }
2895         else {
2896 monoview_movie:
2897                 sanim = seq->anims.first;
2898                 if (sanim && sanim->anim) {
2899                         IMB_Proxy_Size proxy_size = seq_rendersize_to_proxysize(context->preview_render_size);
2900                         IMB_anim_set_preseek(sanim->anim, seq->anim_preseek);
2901
2902                         ibuf = IMB_anim_absolute(sanim->anim, nr + seq->anim_startofs,
2903                                                  seq->strip->proxy ? seq->strip->proxy->tc : IMB_TC_RECORD_RUN,
2904                                                  proxy_size);
2905
2906                         /* fetching for requested proxy size failed, try fetching the original instead */
2907                         if (!ibuf && proxy_size != IMB_PROXY_NONE) {
2908                                 ibuf = IMB_anim_absolute(sanim->anim, nr + seq->anim_startofs,
2909                                                          seq->strip->proxy ? seq->strip->proxy->tc : IMB_TC_RECORD_RUN,
2910                                                          IMB_PROXY_NONE);
2911                         }
2912                         if (ibuf) {
2913                                 BKE_sequencer_imbuf_to_sequencer_space(context->scene, ibuf, false);
2914
2915                                 /* we don't need both (speed reasons)! */
2916                                 if (ibuf->rect_float && ibuf->rect) {
2917                                         imb_freerectImBuf(ibuf);
2918                                 }
2919
2920                                 seq->strip->stripdata->orig_width = ibuf->x;
2921                                 seq->strip->stripdata->orig_height = ibuf->y;
2922                         }
2923                 }
2924         }
2925         return ibuf;
2926 }
2927
2928 static ImBuf *seq_render_movieclip_strip(const SeqRenderData *context, Sequence *seq, float nr)
2929 {
2930         ImBuf *ibuf = NULL;
2931         MovieClipUser user;
2932         float tloc[2], tscale, tangle;
2933
2934         if (!seq->clip) {
2935                 return NULL;
2936         }
2937
2938         memset(&user, 0, sizeof(MovieClipUser));
2939         
2940         BKE_movieclip_user_set_frame(&user, nr + seq->anim_startofs + seq->clip->start_frame);
2941
2942         user.render_size = MCLIP_PROXY_RENDER_SIZE_FULL;
2943
2944         switch (seq_rendersize_to_proxysize(context->preview_render_size)) {
2945                 case IMB_PROXY_NONE:
2946                         user.render_size = MCLIP_PROXY_RENDER_SIZE_FULL;
2947                         break;
2948                 case IMB_PROXY_100:
2949                         user.render_size = MCLIP_PROXY_RENDER_SIZE_100;
2950                         break;
2951                 case IMB_PROXY_75:
2952                         user.render_size = MCLIP_PROXY_RENDER_SIZE_75;
2953                         break;
2954                 case IMB_PROXY_50:
2955                         user.render_size = MCLIP_PROXY_RENDER_SIZE_50;
2956                         break;
2957                 case IMB_PROXY_25:
2958                         user.render_size = MCLIP_PROXY_RENDER_SIZE_25;
2959                         break;
2960         }
2961
2962         if (seq->clip_flag & SEQ_MOVIECLIP_RENDER_UNDISTORTED) {
2963                 user.render_flag = MCLIP_PROXY_RENDER_UNDISTORT;
2964         }
2965
2966         if (seq->clip_flag & SEQ_MOVIECLIP_RENDER_STABILIZED) {
2967                 ibuf = BKE_movieclip_get_stable_ibuf(seq->clip, &user, tloc, &tscale, &tangle, 0);
2968         }
2969         else {
2970                 ibuf = BKE_movieclip_get_ibuf_flag(seq->clip, &user, 0, MOVIECLIP_CACHE_SKIP);
2971         }
2972
2973         return ibuf;
2974 }
2975
2976
2977 static ImBuf *seq_render_mask(const SeqRenderData *context, Mask *mask, float nr, bool make_float)
2978 {
2979         /* TODO - add option to rasterize to alpha imbuf? */
2980         ImBuf *ibuf = NULL;
2981         float *maskbuf;
2982         int i;
2983
2984         if (!mask) {
2985                 return NULL;
2986         }
2987         else {
2988                 Mask *mask_temp;
2989                 MaskRasterHandle *mr_handle;
2990
2991                 mask_temp = BKE_mask_copy_nolib(mask);
2992
2993                 BKE_mask_evaluate(mask_temp, mask->sfra + nr, true);
2994
2995                 maskbuf = MEM_mallocN(sizeof(float) * context->rectx * context->recty, __func__);
2996
2997                 mr_handle = BKE_maskrasterize_handle_new();
2998
2999                 BKE_maskrasterize_handle_init(mr_handle, mask_temp, context->rectx, context->recty, true, true, true);
3000
3001                 BKE_mask_free_nolib(mask_temp);
3002                 MEM_freeN(mask_temp);
3003
3004                 BKE_maskrasterize_buffer(mr_handle, context->rectx, context->recty, maskbuf);
3005
3006                 BKE_maskrasterize_handle_free(mr_handle);
3007         }
3008
3009
3010         if (make_float) {
3011                 /* pixels */
3012                 const float *fp_src;
3013                 float *fp_dst;
3014
3015                 ibuf = IMB_allocImBuf(context->rectx, context->recty, 32, IB_rectfloat);
3016
3017                 fp_src = maskbuf;
3018                 fp_dst = ibuf->rect_float;
3019                 i = context->rectx * context->recty;
3020                 while (--i) {
3021                         fp_dst[0] = fp_dst[1] = fp_dst[2] = *fp_src;
3022                         fp_dst[3] = 1.0f;
3023
3024                         fp_src += 1;
3025                         fp_dst += 4;
3026                 }
3027         }
3028         else {
3029                 /* pixels */
3030                 const float *fp_src;
3031                 unsigned char *ub_dst;
3032
3033                 ibuf = IMB_allocImBuf(context->rectx, context->recty, 32, IB_rect);
3034
3035                 fp_src = maskbuf;
3036                 ub_dst = (unsigned char *)ibuf->rect;
3037                 i = context->rectx * context->recty;
3038                 while (--i) {
3039                         ub_dst[0] = ub_dst[1] = ub_dst[2] = (unsigned char)(*fp_src * 255.0f); /* already clamped */
3040                         ub_dst[3] = 255;
3041
3042                         fp_src += 1;
3043                         ub_dst += 4;
3044                 }
3045         }
3046
3047         MEM_freeN(maskbuf);
3048
3049         return ibuf;
3050 }
3051
3052 static ImBuf *seq_render_mask_strip(const SeqRenderData *context, Sequence *seq, float nr)
3053 {
3054         bool make_float = (seq->flag & SEQ_MAKE_FLOAT) != 0;
3055
3056         return seq_render_mask(context, seq->mask, nr, make_float);
3057 }
3058
3059 static ImBuf *seq_render_scene_strip(const SeqRenderData *context, Sequence *seq, float nr, float cfra)
3060 {
3061         ImBuf *ibuf = NULL;
3062         float frame;
3063         float oldcfra;
3064         Object *camera;
3065         ListBase oldmarkers;
3066         
3067         /* Old info:
3068          * Hack! This function can be called from do_render_seq(), in that case
3069          * the seq->scene can already have a Render initialized with same name,
3070          * so we have to use a default name. (compositor uses scene name to
3071          * find render).
3072          * However, when called from within the UI (image preview in sequencer)
3073          * we do want to use scene Render, that way the render result is defined
3074          * for display in render/imagewindow
3075          *
3076          * Hmm, don't see, why we can't do that all the time,
3077          * and since G.is_rendering is uhm, gone... (Peter)
3078          */
3079
3080         /* New info:
3081          * Using the same name for the renders works just fine as the do_render_seq()
3082          * render is not used while the scene strips are rendered.
3083          *
3084          * However rendering from UI (through sequencer_preview_area_draw) can crash in
3085          * very many cases since other renders (material preview, an actual render etc.)
3086          * can be started while this sequence preview render is running. The only proper
3087          * solution is to make the sequencer preview render a proper job, which can be
3088          * stopped when needed. This would also give a nice progress bar for the preview
3089          * space so that users know there's something happening.
3090          *
3091          * As a result the active scene now only uses OpenGL rendering for the sequencer
3092          * preview. This is far from nice, but is the only way to prevent crashes at this
3093          * time.
3094          *
3095          * -jahka
3096          */
3097
3098         const bool is_rendering = G.is_rendering;
3099         const bool is_background = G.background;
3100         const bool do_seq_gl = is_rendering ?
3101                 0 /* (context->scene->r.seq_flag & R_SEQ_GL_REND) */ :
3102                 (context->scene->r.seq_flag & R_SEQ_GL_PREV) != 0;
3103         int do_seq;
3104         // bool have_seq = false;  /* UNUSED */
3105         bool have_comp = false;
3106         bool use_gpencil = true;
3107         Scene *scene;
3108         int is_thread_main = BLI_thread_is_main();
3109
3110         /* don't refer to seq->scene above this point!, it can be NULL */
3111         if (seq->scene == NULL) {
3112                 return NULL;
3113         }
3114
3115         scene = seq->scene;
3116         frame = scene->r.sfra + nr + seq->anim_startofs;
3117