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