Merge branch 'blender2.7'
[blender.git] / source / blender / blenkernel / intern / movieclip.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2011 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file \ingroup bke
21  */
22
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <fcntl.h>
27
28 #ifndef WIN32
29 #  include <unistd.h>
30 #else
31 #  include <io.h>
32 #endif
33
34 #include <time.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_constraint_types.h"
39 #include "DNA_screen_types.h"
40 #include "DNA_space_types.h"
41 #include "DNA_movieclip_types.h"
42 #include "DNA_node_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_view3d_types.h"
46
47 #include "BLI_utildefines.h"
48
49 #include "BLI_blenlib.h"
50 #include "BLI_ghash.h"
51 #include "BLI_math.h"
52 #include "BLI_threads.h"
53
54 #include "BKE_animsys.h"
55 #include "BKE_colortools.h"
56 #include "BKE_library.h"
57 #include "BKE_main.h"
58 #include "BKE_movieclip.h"
59 #include "BKE_node.h"
60 #include "BKE_image.h"  /* openanim */
61 #include "BKE_tracking.h"
62
63 #include "IMB_imbuf_types.h"
64 #include "IMB_imbuf.h"
65 #include "IMB_moviecache.h"
66
67 #include "DEG_depsgraph.h"
68
69 #ifdef WITH_OPENEXR
70 #  include "intern/openexr/openexr_multi.h"
71 #endif
72
73 /*********************** movieclip buffer loaders *************************/
74
75 static int sequence_guess_offset(const char *full_name, int head_len, unsigned short numlen)
76 {
77         char num[FILE_MAX] = {0};
78
79         BLI_strncpy(num, full_name + head_len, numlen + 1);
80
81         return atoi(num);
82 }
83
84 static int rendersize_to_proxy(const MovieClipUser *user, int flag)
85 {
86         if ((flag & MCLIP_USE_PROXY) == 0)
87                 return IMB_PROXY_NONE;
88
89         switch (user->render_size) {
90                 case MCLIP_PROXY_RENDER_SIZE_25:
91                         return IMB_PROXY_25;
92
93                 case MCLIP_PROXY_RENDER_SIZE_50:
94                         return IMB_PROXY_50;
95
96                 case MCLIP_PROXY_RENDER_SIZE_75:
97                         return IMB_PROXY_75;
98
99                 case MCLIP_PROXY_RENDER_SIZE_100:
100                         return IMB_PROXY_100;
101
102                 case MCLIP_PROXY_RENDER_SIZE_FULL:
103                         return IMB_PROXY_NONE;
104         }
105
106         return IMB_PROXY_NONE;
107 }
108
109 static int rendersize_to_number(int render_size)
110 {
111         switch (render_size) {
112                 case MCLIP_PROXY_RENDER_SIZE_25:
113                         return 25;
114
115                 case MCLIP_PROXY_RENDER_SIZE_50:
116                         return 50;
117
118                 case MCLIP_PROXY_RENDER_SIZE_75:
119                         return 75;
120
121                 case MCLIP_PROXY_RENDER_SIZE_100:
122                         return 100;
123
124                 case MCLIP_PROXY_RENDER_SIZE_FULL:
125                         return 100;
126         }
127
128         return 100;
129 }
130
131 static int get_timecode(MovieClip *clip, int flag)
132 {
133         if ((flag & MCLIP_USE_PROXY) == 0)
134                 return IMB_TC_NONE;
135
136         return clip->proxy.tc;
137 }
138
139 static void get_sequence_fname(const MovieClip *clip,
140                                const int framenr,
141                                char *name)
142 {
143         unsigned short numlen;
144         char head[FILE_MAX], tail[FILE_MAX];
145         int offset;
146
147         BLI_strncpy(name, clip->name, sizeof(clip->name));
148         BLI_stringdec(name, head, tail, &numlen);
149
150         /* movieclips always points to first image from sequence,
151          * autoguess offset for now. could be something smarter in the future
152          */
153         offset = sequence_guess_offset(clip->name, strlen(head), numlen);
154
155         if (numlen) {
156                 BLI_stringenc(name,
157                               head, tail,
158                               numlen,
159                               offset + framenr - clip->start_frame + clip->frame_offset);
160         }
161         else {
162                 BLI_strncpy(name, clip->name, sizeof(clip->name));
163         }
164
165         BLI_path_abs(name, ID_BLEND_PATH_FROM_GLOBAL(&clip->id));
166 }
167
168 /* supposed to work with sequences only */
169 static void get_proxy_fname(const MovieClip *clip,
170                             int proxy_render_size,
171                             bool undistorted,
172                             int framenr,
173                             char *name)
174 {
175         int size = rendersize_to_number(proxy_render_size);
176         char dir[FILE_MAX], clipdir[FILE_MAX], clipfile[FILE_MAX];
177         int proxynr = framenr - clip->start_frame + 1 + clip->frame_offset;
178
179         BLI_split_dirfile(clip->name, clipdir, clipfile, FILE_MAX, FILE_MAX);
180
181         if (clip->flag & MCLIP_USE_PROXY_CUSTOM_DIR) {
182                 BLI_strncpy(dir, clip->proxy.dir, sizeof(dir));
183         }
184         else {
185                 BLI_snprintf(dir, FILE_MAX, "%s/BL_proxy", clipdir);
186         }
187
188         if (undistorted)
189                 BLI_snprintf(name, FILE_MAX, "%s/%s/proxy_%d_undistorted/%08d", dir, clipfile, size, proxynr);
190         else
191                 BLI_snprintf(name, FILE_MAX, "%s/%s/proxy_%d/%08d", dir, clipfile, size, proxynr);
192
193         BLI_path_abs(name, BKE_main_blendfile_path_from_global());
194         BLI_path_frame(name, 1, 0);
195
196         strcat(name, ".jpg");
197 }
198
199 #ifdef WITH_OPENEXR
200
201 typedef struct MultilayerConvertContext {
202         float *combined_pass;
203         int num_combined_channels;
204 } MultilayerConvertContext;
205
206 static void *movieclip_convert_multilayer_add_view(
207         void *UNUSED(ctx_v),
208         const char *UNUSED(view_name))
209 {
210         return NULL;
211 }
212
213 static void *movieclip_convert_multilayer_add_layer(
214         void *ctx_v,
215         const char *UNUSED(layer_name))
216 {
217         /* Return dummy non-NULL value, we don't use layer handle but need to return
218          * something, so render API invokes the add_pass() callbacks. */
219         return ctx_v;
220 }
221
222 static void movieclip_convert_multilayer_add_pass(
223         void *UNUSED(layer),
224         void *ctx_v,
225         const char *pass_name,
226         float *rect,
227         int num_channels,
228         const char *chan_id,
229         const char *UNUSED(view_name))
230 {
231         /* NOTE: This function must free pass pixels data if it is not used, this
232          * is how IMB_exr_multilayer_convert() is working. */
233         MultilayerConvertContext *ctx = ctx_v;
234         /* If we've found a first combined pass, skip all the rest ones. */
235         if (ctx->combined_pass != NULL) {
236                 MEM_freeN(rect);
237                 return;
238         }
239         if (STREQ(pass_name, RE_PASSNAME_COMBINED) ||
240             STREQ(chan_id, "RGBA") ||
241             STREQ(chan_id, "RGB"))
242         {
243                 ctx->combined_pass = rect;
244                 ctx->num_combined_channels = num_channels;
245         }
246         else {
247                 MEM_freeN(rect);
248         }
249 }
250
251 #endif  /* WITH_OPENEXR */
252
253 /* Will try to make image buffer usable when originating from the multi-layer
254  * source.
255  * Internally finds a first combined pass and uses that as a buffer. Not ideal,
256  * but is better than a complete empty buffer. */
257 void BKE_movieclip_convert_multilayer_ibuf(struct ImBuf *ibuf)
258 {
259         if (ibuf == NULL) {
260                 return;
261         }
262 #ifdef WITH_OPENEXR
263         if (ibuf->ftype != IMB_FTYPE_OPENEXR || ibuf->userdata == NULL) {
264                 return;
265         }
266         MultilayerConvertContext ctx;
267         ctx.combined_pass = NULL;
268         ctx.num_combined_channels = 0;
269         IMB_exr_multilayer_convert(
270                 ibuf->userdata,
271                 &ctx,
272                 movieclip_convert_multilayer_add_view,
273                 movieclip_convert_multilayer_add_layer,
274                 movieclip_convert_multilayer_add_pass);
275         if (ctx.combined_pass != NULL) {
276                 BLI_assert(ibuf->rect_float == NULL);
277                 ibuf->rect_float = ctx.combined_pass;
278                 ibuf->channels = ctx.num_combined_channels;
279                 ibuf->flags |= IB_rectfloat;
280                 ibuf->mall |= IB_rectfloat;
281         }
282         IMB_exr_close(ibuf->userdata);
283         ibuf->userdata = NULL;
284 #endif
285 }
286
287 static ImBuf *movieclip_load_sequence_file(MovieClip *clip,
288                                            const MovieClipUser *user,
289                                            int framenr,
290                                            int flag)
291 {
292         struct ImBuf *ibuf;
293         char name[FILE_MAX];
294         int loadflag;
295         bool use_proxy = false;
296         char *colorspace;
297
298         use_proxy = (flag & MCLIP_USE_PROXY) && user->render_size != MCLIP_PROXY_RENDER_SIZE_FULL;
299         if (use_proxy) {
300                 int undistort = user->render_flag & MCLIP_PROXY_RENDER_UNDISTORT;
301                 get_proxy_fname(clip, user->render_size, undistort, framenr, name);
302
303                 /* Well, this is a bit weird, but proxies for movie sources
304                  * are built in the same exact color space as the input,
305                  *
306                  * But image sequences are built in the display space.
307                  */
308                 if (clip->source == MCLIP_SRC_MOVIE) {
309                         colorspace = clip->colorspace_settings.name;
310                 }
311                 else {
312                         colorspace = NULL;
313                 }
314         }
315         else {
316                 get_sequence_fname(clip, framenr, name);
317                 colorspace = clip->colorspace_settings.name;
318         }
319
320         loadflag = IB_rect | IB_multilayer | IB_alphamode_detect | IB_metadata;
321
322         /* read ibuf */
323         ibuf = IMB_loadiffname(name, loadflag, colorspace);
324         BKE_movieclip_convert_multilayer_ibuf(ibuf);
325
326         return ibuf;
327 }
328
329 static void movieclip_open_anim_file(MovieClip *clip)
330 {
331         char str[FILE_MAX];
332
333         if (!clip->anim) {
334                 BLI_strncpy(str, clip->name, FILE_MAX);
335                 BLI_path_abs(str, ID_BLEND_PATH_FROM_GLOBAL(&clip->id));
336
337                 /* FIXME: make several stream accessible in image editor, too */
338                 clip->anim = openanim(str, IB_rect, 0, clip->colorspace_settings.name);
339
340                 if (clip->anim) {
341                         if (clip->flag & MCLIP_USE_PROXY_CUSTOM_DIR) {
342                                 char dir[FILE_MAX];
343                                 BLI_strncpy(dir, clip->proxy.dir, sizeof(dir));
344                                 BLI_path_abs(dir, BKE_main_blendfile_path_from_global());
345                                 IMB_anim_set_index_dir(clip->anim, dir);
346                         }
347                 }
348         }
349 }
350
351 static ImBuf *movieclip_load_movie_file(MovieClip *clip,
352                                         const MovieClipUser *user,
353                                         int framenr,
354                                         int flag)
355 {
356         ImBuf *ibuf = NULL;
357         int tc = get_timecode(clip, flag);
358         int proxy = rendersize_to_proxy(user, flag);
359
360         movieclip_open_anim_file(clip);
361
362         if (clip->anim) {
363                 int fra = framenr - clip->start_frame + clip->frame_offset;
364
365                 ibuf = IMB_anim_absolute(clip->anim, fra, tc, proxy);
366         }
367
368         return ibuf;
369 }
370
371 static void movieclip_calc_length(MovieClip *clip)
372 {
373         if (clip->source == MCLIP_SRC_MOVIE) {
374                 movieclip_open_anim_file(clip);
375
376                 if (clip->anim) {
377                         clip->len = IMB_anim_get_duration(clip->anim, clip->proxy.tc);
378                 }
379         }
380         else if (clip->source == MCLIP_SRC_SEQUENCE) {
381                 unsigned short numlen;
382                 char name[FILE_MAX], head[FILE_MAX], tail[FILE_MAX];
383
384                 BLI_stringdec(clip->name, head, tail, &numlen);
385
386                 if (numlen == 0) {
387                         /* there's no number group in file name, assume it's single framed sequence */
388                         clip->len = 1;
389                 }
390                 else {
391                         clip->len = 0;
392                         for (;;) {
393                                 get_sequence_fname(clip,
394                                                    clip->len + clip->start_frame,
395                                                    name);
396
397                                 if (BLI_exists(name))
398                                         clip->len++;
399                                 else
400                                         break;
401                         }
402                 }
403         }
404 }
405
406 /*********************** image buffer cache *************************/
407
408 typedef struct MovieClipCache {
409         /* regular movie cache */
410         struct MovieCache *moviecache;
411
412         /* cached postprocessed shot */
413         struct {
414                 ImBuf *ibuf;
415                 int framenr;
416                 int flag;
417
418                 /* cache for undistorted shot */
419                 float principal[2];
420                 float polynomial_k1, polynomial_k2, polynomial_k3;
421                 float division_k1, division_k2;
422                 short distortion_model;
423                 bool undistortion_used;
424
425                 int proxy;
426                 short render_flag;
427         } postprocessed;
428
429         /* cache for stable shot */
430         struct {
431                 ImBuf *reference_ibuf;
432
433                 ImBuf *ibuf;
434                 int framenr;
435                 int postprocess_flag;
436
437                 float loc[2], scale, angle, aspect;
438                 int proxy, filter;
439                 short render_flag;
440         } stabilized;
441
442         int sequence_offset;
443
444         bool is_still_sequence;
445 } MovieClipCache;
446
447 typedef struct MovieClipImBufCacheKey {
448         int framenr;
449         int proxy;
450         short render_flag;
451 } MovieClipImBufCacheKey;
452
453 typedef struct MovieClipCachePriorityData {
454         int framenr;
455 } MovieClipCachePriorityData;
456
457 static int user_frame_to_cache_frame(MovieClip *clip, int framenr)
458 {
459         int index;
460
461         index = framenr - clip->start_frame + clip->frame_offset;
462
463         if (clip->source == MCLIP_SRC_SEQUENCE) {
464                 if (clip->cache->sequence_offset == -1) {
465                         unsigned short numlen;
466                         char head[FILE_MAX], tail[FILE_MAX];
467
468                         BLI_stringdec(clip->name, head, tail, &numlen);
469
470                         /* see comment in get_sequence_fname */
471                         clip->cache->sequence_offset = sequence_guess_offset(clip->name, strlen(head), numlen);
472                 }
473
474                 index += clip->cache->sequence_offset;
475         }
476
477         if (index < 0)
478                 return framenr - index;
479
480         return framenr;
481 }
482
483 static void moviecache_keydata(void *userkey, int *framenr, int *proxy, int *render_flags)
484 {
485         const MovieClipImBufCacheKey *key = userkey;
486
487         *framenr = key->framenr;
488         *proxy = key->proxy;
489         *render_flags = key->render_flag;
490 }
491
492 static unsigned int moviecache_hashhash(const void *keyv)
493 {
494         const MovieClipImBufCacheKey *key = keyv;
495         int rval = key->framenr;
496
497         return rval;
498 }
499
500 static bool moviecache_hashcmp(const void *av, const void *bv)
501 {
502         const MovieClipImBufCacheKey *a = av;
503         const MovieClipImBufCacheKey *b = bv;
504
505         return ((a->framenr != b->framenr) ||
506                 (a->proxy != b->proxy) ||
507                 (a->render_flag != b->render_flag));
508 }
509
510 static void *moviecache_getprioritydata(void *key_v)
511 {
512         MovieClipImBufCacheKey *key = (MovieClipImBufCacheKey *) key_v;
513         MovieClipCachePriorityData *priority_data;
514
515         priority_data = MEM_callocN(sizeof(*priority_data), "movie cache clip priority data");
516         priority_data->framenr = key->framenr;
517
518         return priority_data;
519 }
520
521 static int moviecache_getitempriority(void *last_userkey_v, void *priority_data_v)
522 {
523         MovieClipImBufCacheKey *last_userkey = (MovieClipImBufCacheKey *) last_userkey_v;
524         MovieClipCachePriorityData *priority_data = (MovieClipCachePriorityData *) priority_data_v;
525
526         return -abs(last_userkey->framenr - priority_data->framenr);
527 }
528
529 static void moviecache_prioritydeleter(void *priority_data_v)
530 {
531         MovieClipCachePriorityData *priority_data = (MovieClipCachePriorityData *) priority_data_v;
532
533         MEM_freeN(priority_data);
534 }
535
536 static ImBuf *get_imbuf_cache(MovieClip *clip,
537                               const MovieClipUser *user,
538                               int flag)
539 {
540         if (clip->cache) {
541                 MovieClipImBufCacheKey key;
542
543                 if (!clip->cache->is_still_sequence) {
544                         key.framenr = user_frame_to_cache_frame(clip, user->framenr);
545                 }
546                 else {
547                         key.framenr = 1;
548                 }
549
550                 if (flag & MCLIP_USE_PROXY) {
551                         key.proxy = rendersize_to_proxy(user, flag);
552                         key.render_flag = user->render_flag;
553                 }
554                 else {
555                         key.proxy = IMB_PROXY_NONE;
556                         key.render_flag = 0;
557                 }
558
559                 return IMB_moviecache_get(clip->cache->moviecache, &key);
560         }
561
562         return NULL;
563 }
564
565 static bool has_imbuf_cache(MovieClip *clip, MovieClipUser *user, int flag)
566 {
567         if (clip->cache) {
568                 MovieClipImBufCacheKey key;
569
570                 key.framenr = user_frame_to_cache_frame(clip, user->framenr);
571
572                 if (flag & MCLIP_USE_PROXY) {
573                         key.proxy = rendersize_to_proxy(user, flag);
574                         key.render_flag = user->render_flag;
575                 }
576                 else {
577                         key.proxy = IMB_PROXY_NONE;
578                         key.render_flag = 0;
579                 }
580
581                 return IMB_moviecache_has_frame(clip->cache->moviecache, &key);
582         }
583
584         return false;
585 }
586
587 static bool put_imbuf_cache(MovieClip *clip,
588                             const MovieClipUser *user,
589                             ImBuf *ibuf,
590                             int flag,
591                             bool destructive)
592 {
593         MovieClipImBufCacheKey key;
594
595         if (clip->cache == NULL) {
596                 struct MovieCache *moviecache;
597
598                 // char cache_name[64];
599                 // BLI_snprintf(cache_name, sizeof(cache_name), "movie %s", clip->id.name);
600
601                 clip->cache = MEM_callocN(sizeof(MovieClipCache), "movieClipCache");
602
603                 moviecache = IMB_moviecache_create("movieclip",
604                                                    sizeof(MovieClipImBufCacheKey),
605                                                    moviecache_hashhash,
606                                                    moviecache_hashcmp);
607
608                 IMB_moviecache_set_getdata_callback(moviecache, moviecache_keydata);
609                 IMB_moviecache_set_priority_callback(moviecache,
610                                                      moviecache_getprioritydata,
611                                                      moviecache_getitempriority,
612                                                      moviecache_prioritydeleter);
613
614                 clip->cache->moviecache = moviecache;
615                 clip->cache->sequence_offset = -1;
616                 if (clip->source == MCLIP_SRC_SEQUENCE) {
617                         unsigned short numlen;
618                         BLI_stringdec(clip->name, NULL, NULL, &numlen);
619                         clip->cache->is_still_sequence = (numlen == 0);
620                 }
621         }
622
623         if (!clip->cache->is_still_sequence) {
624                 key.framenr = user_frame_to_cache_frame(clip, user->framenr);
625         }
626         else {
627                 key.framenr = 1;
628         }
629
630         if (flag & MCLIP_USE_PROXY) {
631                 key.proxy = rendersize_to_proxy(user, flag);
632                 key.render_flag = user->render_flag;
633         }
634         else {
635                 key.proxy = IMB_PROXY_NONE;
636                 key.render_flag = 0;
637         }
638
639         if (destructive) {
640                 IMB_moviecache_put(clip->cache->moviecache, &key, ibuf);
641                 return true;
642         }
643         else {
644                 return IMB_moviecache_put_if_possible(clip->cache->moviecache, &key, ibuf);
645         }
646 }
647
648 static bool moviecache_check_free_proxy(ImBuf *UNUSED(ibuf),
649                                         void *userkey,
650                                         void *UNUSED(userdata))
651 {
652         MovieClipImBufCacheKey *key = (MovieClipImBufCacheKey *)userkey;
653
654         return !(key->proxy == IMB_PROXY_NONE && key->render_flag == 0);
655 }
656
657 /*********************** common functions *************************/
658
659 /* only image block itself */
660 static MovieClip *movieclip_alloc(Main *bmain, const char *name)
661 {
662         MovieClip *clip;
663
664         clip = BKE_libblock_alloc(bmain, ID_MC, name, 0);
665
666         clip->aspx = clip->aspy = 1.0f;
667
668         BKE_tracking_settings_init(&clip->tracking);
669         BKE_color_managed_colorspace_settings_init(&clip->colorspace_settings);
670
671         clip->proxy.build_size_flag = IMB_PROXY_25;
672         clip->proxy.build_tc_flag = IMB_TC_RECORD_RUN |
673                                     IMB_TC_FREE_RUN |
674                                     IMB_TC_INTERPOLATED_REC_DATE_FREE_RUN |
675                                     IMB_TC_RECORD_RUN_NO_GAPS;
676         clip->proxy.quality = 90;
677
678         clip->start_frame = 1;
679         clip->frame_offset = 0;
680
681         return clip;
682 }
683
684 static void movieclip_load_get_size(MovieClip *clip)
685 {
686         int width, height;
687         MovieClipUser user = {0};
688
689         user.framenr = 1;
690         BKE_movieclip_get_size(clip, &user, &width, &height);
691
692         if (width && height) {
693                 clip->tracking.camera.principal[0] = ((float)width) / 2.0f;
694                 clip->tracking.camera.principal[1] = ((float)height) / 2.0f;
695         }
696         else {
697                 clip->lastsize[0] = clip->lastsize[1] = IMG_SIZE_FALLBACK;
698         }
699 }
700
701 static void detect_clip_source(Main *bmain, MovieClip *clip)
702 {
703         ImBuf *ibuf;
704         char name[FILE_MAX];
705
706         BLI_strncpy(name, clip->name, sizeof(name));
707         BLI_path_abs(name, BKE_main_blendfile_path(bmain));
708
709         ibuf = IMB_testiffname(name, IB_rect | IB_multilayer);
710         if (ibuf) {
711                 clip->source = MCLIP_SRC_SEQUENCE;
712                 IMB_freeImBuf(ibuf);
713         }
714         else {
715                 clip->source = MCLIP_SRC_MOVIE;
716         }
717 }
718
719 /* checks if image was already loaded, then returns same image
720  * otherwise creates new.
721  * does not load ibuf itself
722  * pass on optional frame for #name images */
723 MovieClip *BKE_movieclip_file_add(Main *bmain, const char *name)
724 {
725         MovieClip *clip;
726         int file;
727         char str[FILE_MAX];
728
729         BLI_strncpy(str, name, sizeof(str));
730         BLI_path_abs(str, BKE_main_blendfile_path(bmain));
731
732         /* exists? */
733         file = BLI_open(str, O_BINARY | O_RDONLY, 0);
734         if (file == -1)
735                 return NULL;
736         close(file);
737
738         /* ** add new movieclip ** */
739
740         /* create a short library name */
741         clip = movieclip_alloc(bmain, BLI_path_basename(name));
742         BLI_strncpy(clip->name, name, sizeof(clip->name));
743
744         detect_clip_source(bmain, clip);
745
746         movieclip_load_get_size(clip);
747         if (clip->lastsize[0]) {
748                 int width = clip->lastsize[0];
749
750                 clip->tracking.camera.focal = 24.0f * width / clip->tracking.camera.sensor_width;
751         }
752
753         movieclip_calc_length(clip);
754
755         return clip;
756 }
757
758 MovieClip *BKE_movieclip_file_add_exists_ex(Main *bmain, const char *filepath, bool *r_exists)
759 {
760         MovieClip *clip;
761         char str[FILE_MAX], strtest[FILE_MAX];
762
763         BLI_strncpy(str, filepath, sizeof(str));
764         BLI_path_abs(str, BKE_main_blendfile_path(bmain));
765
766         /* first search an identical filepath */
767         for (clip = bmain->movieclip.first; clip; clip = clip->id.next) {
768                 BLI_strncpy(strtest, clip->name, sizeof(clip->name));
769                 BLI_path_abs(strtest, ID_BLEND_PATH(bmain, &clip->id));
770
771                 if (BLI_path_cmp(strtest, str) == 0) {
772                         id_us_plus(&clip->id);  /* officially should not, it doesn't link here! */
773                         if (r_exists)
774                                 *r_exists = true;
775                         return clip;
776                 }
777         }
778
779         if (r_exists)
780                 *r_exists = false;
781         return BKE_movieclip_file_add(bmain, filepath);
782 }
783
784 MovieClip *BKE_movieclip_file_add_exists(Main *bmain, const char *filepath)
785 {
786         return BKE_movieclip_file_add_exists_ex(bmain, filepath, NULL);
787 }
788
789 static void real_ibuf_size(const MovieClip *clip,
790                            const MovieClipUser *user,
791                            const ImBuf *ibuf,
792                            int *width, int *height)
793 {
794         *width = ibuf->x;
795         *height = ibuf->y;
796
797         if (clip->flag & MCLIP_USE_PROXY) {
798                 switch (user->render_size) {
799                         case MCLIP_PROXY_RENDER_SIZE_25:
800                                 (*width) *= 4;
801                                 (*height) *= 4;
802                                 break;
803
804                         case MCLIP_PROXY_RENDER_SIZE_50:
805                                 (*width) *= 2.0f;
806                                 (*height) *= 2.0f;
807                                 break;
808
809                         case MCLIP_PROXY_RENDER_SIZE_75:
810                                 *width = ((float)*width) * 4.0f / 3.0f;
811                                 *height = ((float)*height) * 4.0f / 3.0f;
812                                 break;
813                 }
814         }
815 }
816
817 static ImBuf *get_undistorted_ibuf(MovieClip *clip,
818                                    struct MovieDistortion *distortion,
819                                    ImBuf *ibuf)
820 {
821         ImBuf *undistibuf;
822
823         if (distortion)
824                 undistibuf = BKE_tracking_distortion_exec(distortion, &clip->tracking, ibuf, ibuf->x, ibuf->y, 0.0f, 1);
825         else
826                 undistibuf = BKE_tracking_undistort_frame(&clip->tracking, ibuf, ibuf->x, ibuf->y, 0.0f);
827
828         IMB_scaleImBuf(undistibuf, ibuf->x, ibuf->y);
829
830         return undistibuf;
831 }
832
833 static int need_undistortion_postprocess(const MovieClipUser *user)
834 {
835         int result = 0;
836
837         /* only full undistorted render can be used as on-fly undistorting image */
838         result |= (user->render_size == MCLIP_PROXY_RENDER_SIZE_FULL) &&
839                   (user->render_flag & MCLIP_PROXY_RENDER_UNDISTORT) != 0;
840
841         return result;
842 }
843
844 static int need_postprocessed_frame(const MovieClipUser *user,
845                                     int postprocess_flag)
846 {
847         int result = postprocess_flag;
848
849         result |= need_undistortion_postprocess(user);
850
851         return result;
852 }
853
854 static bool check_undistortion_cache_flags(const MovieClip *clip)
855 {
856         const MovieClipCache *cache = clip->cache;
857         const MovieTrackingCamera *camera = &clip->tracking.camera;
858
859         /* check for distortion model changes */
860         if (!equals_v2v2(camera->principal, cache->postprocessed.principal)) {
861                 return false;
862         }
863
864         if (camera->distortion_model != cache->postprocessed.distortion_model) {
865                 return false;
866         }
867
868         if (!equals_v3v3(&camera->k1, &cache->postprocessed.polynomial_k1)) {
869                 return false;
870         }
871
872         if (!equals_v2v2(&camera->division_k1, &cache->postprocessed.division_k1)) {
873                 return false;
874         }
875
876         return true;
877 }
878
879 static ImBuf *get_postprocessed_cached_frame(const MovieClip *clip,
880                                              const MovieClipUser *user,
881                                              int flag,
882                                              int postprocess_flag)
883 {
884         const MovieClipCache *cache = clip->cache;
885         int framenr = user->framenr;
886         short proxy = IMB_PROXY_NONE;
887         int render_flag = 0;
888
889         if (flag & MCLIP_USE_PROXY) {
890                 proxy = rendersize_to_proxy(user, flag);
891                 render_flag = user->render_flag;
892         }
893
894         /* no cache or no cached postprocessed image */
895         if (!clip->cache || !clip->cache->postprocessed.ibuf)
896                 return NULL;
897
898         /* postprocessing happened for other frame */
899         if (cache->postprocessed.framenr != framenr)
900                 return NULL;
901
902         /* cached ibuf used different proxy settings */
903         if (cache->postprocessed.render_flag != render_flag || cache->postprocessed.proxy != proxy)
904                 return NULL;
905
906         if (cache->postprocessed.flag != postprocess_flag)
907                 return NULL;
908
909         if (need_undistortion_postprocess(user)) {
910                 if (!check_undistortion_cache_flags(clip))
911                         return NULL;
912         }
913         else if (cache->postprocessed.undistortion_used)
914                 return NULL;
915
916         IMB_refImBuf(cache->postprocessed.ibuf);
917
918         return cache->postprocessed.ibuf;
919 }
920
921 static ImBuf *postprocess_frame(MovieClip *clip,
922                                 const MovieClipUser *user,
923                                 ImBuf *ibuf,
924                                 int postprocess_flag)
925 {
926         ImBuf *postproc_ibuf = NULL;
927
928         if (need_undistortion_postprocess(user)) {
929                 postproc_ibuf = get_undistorted_ibuf(clip, NULL, ibuf);
930         }
931         else {
932                 postproc_ibuf = IMB_dupImBuf(ibuf);
933         }
934
935         if (postprocess_flag) {
936                 bool disable_red   = (postprocess_flag & MOVIECLIP_DISABLE_RED) != 0;
937                 bool disable_green = (postprocess_flag & MOVIECLIP_DISABLE_GREEN) != 0;
938                 bool disable_blue  = (postprocess_flag & MOVIECLIP_DISABLE_BLUE) != 0;
939                 bool grayscale     = (postprocess_flag & MOVIECLIP_PREVIEW_GRAYSCALE) != 0;
940
941                 if (disable_red || disable_green || disable_blue || grayscale)
942                         BKE_tracking_disable_channels(postproc_ibuf, disable_red, disable_green, disable_blue, 1);
943         }
944
945         return postproc_ibuf;
946 }
947
948 static void put_postprocessed_frame_to_cache(MovieClip *clip,
949                                              const MovieClipUser *user,
950                                              ImBuf *ibuf,
951                                              int flag,
952                                              int postprocess_flag)
953 {
954         MovieClipCache *cache = clip->cache;
955         MovieTrackingCamera *camera = &clip->tracking.camera;
956
957         cache->postprocessed.framenr = user->framenr;
958         cache->postprocessed.flag = postprocess_flag;
959
960         if (flag & MCLIP_USE_PROXY) {
961                 cache->postprocessed.proxy = rendersize_to_proxy(user, flag);
962                 cache->postprocessed.render_flag = user->render_flag;
963         }
964         else {
965                 cache->postprocessed.proxy = IMB_PROXY_NONE;
966                 cache->postprocessed.render_flag = 0;
967         }
968
969         if (need_undistortion_postprocess(user)) {
970                 cache->postprocessed.distortion_model = camera->distortion_model;
971                 copy_v2_v2(cache->postprocessed.principal, camera->principal);
972                 copy_v3_v3(&cache->postprocessed.polynomial_k1, &camera->k1);
973                 copy_v2_v2(&cache->postprocessed.division_k1, &camera->division_k1);
974                 cache->postprocessed.undistortion_used = true;
975         }
976         else {
977                 cache->postprocessed.undistortion_used = false;
978         }
979
980         IMB_refImBuf(ibuf);
981
982         if (cache->postprocessed.ibuf)
983                 IMB_freeImBuf(cache->postprocessed.ibuf);
984
985         cache->postprocessed.ibuf = ibuf;
986 }
987
988 static ImBuf *movieclip_get_postprocessed_ibuf(MovieClip *clip,
989                                                const MovieClipUser *user,
990                                                int flag,
991                                                int postprocess_flag,
992                                                int cache_flag)
993 {
994         ImBuf *ibuf = NULL;
995         int framenr = user->framenr;
996         bool need_postprocess = false;
997
998         /* cache isn't threadsafe itself and also loading of movies
999          * can't happen from concurrent threads that's why we use lock here */
1000         BLI_thread_lock(LOCK_MOVIECLIP);
1001
1002         /* try to obtain cached postprocessed frame first */
1003         if (need_postprocessed_frame(user, postprocess_flag)) {
1004                 ibuf = get_postprocessed_cached_frame(clip, user, flag, postprocess_flag);
1005
1006                 if (!ibuf)
1007                         need_postprocess = true;
1008         }
1009
1010         if (!ibuf)
1011                 ibuf = get_imbuf_cache(clip, user, flag);
1012
1013         if (!ibuf) {
1014                 bool use_sequence = false;
1015
1016                 /* undistorted proxies for movies should be read as image sequence */
1017                 use_sequence = (user->render_flag & MCLIP_PROXY_RENDER_UNDISTORT) &&
1018                                (user->render_size != MCLIP_PROXY_RENDER_SIZE_FULL);
1019
1020                 if (clip->source == MCLIP_SRC_SEQUENCE || use_sequence) {
1021                         ibuf = movieclip_load_sequence_file(clip,
1022                                                             user,
1023                                                             framenr,
1024                                                             flag);
1025                 }
1026                 else {
1027                         ibuf = movieclip_load_movie_file(clip, user, framenr, flag);
1028                 }
1029
1030                 if (ibuf && (cache_flag & MOVIECLIP_CACHE_SKIP) == 0) {
1031                         put_imbuf_cache(clip, user, ibuf, flag, true);
1032                 }
1033         }
1034
1035         if (ibuf) {
1036                 clip->lastframe = framenr;
1037                 real_ibuf_size(clip, user, ibuf, &clip->lastsize[0], &clip->lastsize[1]);
1038
1039                 /* postprocess frame and put to cache if needed*/
1040                 if (need_postprocess) {
1041                         ImBuf *tmpibuf = ibuf;
1042                         ibuf = postprocess_frame(clip, user, tmpibuf, postprocess_flag);
1043                         IMB_freeImBuf(tmpibuf);
1044                         if (ibuf && (cache_flag & MOVIECLIP_CACHE_SKIP) == 0) {
1045                                 put_postprocessed_frame_to_cache(clip, user, ibuf, flag, postprocess_flag);
1046                         }
1047                 }
1048         }
1049
1050         BLI_thread_unlock(LOCK_MOVIECLIP);
1051
1052         return ibuf;
1053 }
1054
1055 ImBuf *BKE_movieclip_get_ibuf(MovieClip *clip, MovieClipUser *user)
1056 {
1057         return BKE_movieclip_get_ibuf_flag(clip, user, clip->flag, 0);
1058 }
1059
1060 ImBuf *BKE_movieclip_get_ibuf_flag(MovieClip *clip, MovieClipUser *user, int flag, int cache_flag)
1061 {
1062         return movieclip_get_postprocessed_ibuf(clip, user, flag, 0, cache_flag);
1063 }
1064
1065 ImBuf *BKE_movieclip_get_postprocessed_ibuf(MovieClip *clip, MovieClipUser *user, int postprocess_flag)
1066 {
1067         return movieclip_get_postprocessed_ibuf(clip, user, clip->flag, postprocess_flag, 0);
1068 }
1069
1070 static ImBuf *get_stable_cached_frame(MovieClip *clip, MovieClipUser *user, ImBuf *reference_ibuf,
1071                                       int framenr, int postprocess_flag)
1072 {
1073         MovieClipCache *cache = clip->cache;
1074         MovieTracking *tracking = &clip->tracking;
1075         ImBuf *stableibuf;
1076         float tloc[2], tscale, tangle;
1077         short proxy = IMB_PROXY_NONE;
1078         int render_flag = 0;
1079         int clip_framenr = BKE_movieclip_remap_scene_to_clip_frame(clip, framenr);
1080
1081         if (clip->flag & MCLIP_USE_PROXY) {
1082                 proxy = rendersize_to_proxy(user, clip->flag);
1083                 render_flag = user->render_flag;
1084         }
1085
1086         /* there's no cached frame or it was calculated for another frame */
1087         if (!cache->stabilized.ibuf || cache->stabilized.framenr != framenr)
1088                 return NULL;
1089
1090         if (cache->stabilized.reference_ibuf != reference_ibuf)
1091                 return NULL;
1092
1093         /* cached ibuf used different proxy settings */
1094         if (cache->stabilized.render_flag != render_flag || cache->stabilized.proxy != proxy)
1095                 return NULL;
1096
1097         if (cache->stabilized.postprocess_flag != postprocess_flag)
1098                 return NULL;
1099
1100         /* stabilization also depends on pixel aspect ratio */
1101         if (cache->stabilized.aspect != tracking->camera.pixel_aspect)
1102                 return NULL;
1103
1104         if (cache->stabilized.filter != tracking->stabilization.filter)
1105                 return NULL;
1106
1107         stableibuf = cache->stabilized.ibuf;
1108
1109         BKE_tracking_stabilization_data_get(clip, clip_framenr, stableibuf->x, stableibuf->y, tloc, &tscale, &tangle);
1110
1111         /* check for stabilization parameters */
1112         if (tscale != cache->stabilized.scale ||
1113             tangle != cache->stabilized.angle ||
1114             !equals_v2v2(tloc, cache->stabilized.loc))
1115         {
1116                 return NULL;
1117         }
1118
1119         IMB_refImBuf(stableibuf);
1120
1121         return stableibuf;
1122 }
1123
1124 static ImBuf *put_stabilized_frame_to_cache(MovieClip *clip, MovieClipUser *user, ImBuf *ibuf,
1125                                             int framenr, int postprocess_flag)
1126 {
1127         MovieClipCache *cache = clip->cache;
1128         MovieTracking *tracking = &clip->tracking;
1129         ImBuf *stableibuf;
1130         float tloc[2], tscale, tangle;
1131         int clip_framenr = BKE_movieclip_remap_scene_to_clip_frame(clip, framenr);
1132
1133         stableibuf = BKE_tracking_stabilize_frame(clip, clip_framenr, ibuf, tloc, &tscale, &tangle);
1134
1135         copy_v2_v2(cache->stabilized.loc, tloc);
1136
1137         cache->stabilized.reference_ibuf = ibuf;
1138         cache->stabilized.scale = tscale;
1139         cache->stabilized.angle = tangle;
1140         cache->stabilized.framenr = framenr;
1141         cache->stabilized.aspect = tracking->camera.pixel_aspect;
1142         cache->stabilized.filter = tracking->stabilization.filter;
1143
1144         if (clip->flag & MCLIP_USE_PROXY) {
1145                 cache->stabilized.proxy = rendersize_to_proxy(user, clip->flag);
1146                 cache->stabilized.render_flag = user->render_flag;
1147         }
1148         else {
1149                 cache->stabilized.proxy = IMB_PROXY_NONE;
1150                 cache->stabilized.render_flag = 0;
1151         }
1152
1153         cache->stabilized.postprocess_flag = postprocess_flag;
1154
1155         if (cache->stabilized.ibuf)
1156                 IMB_freeImBuf(cache->stabilized.ibuf);
1157
1158         cache->stabilized.ibuf = stableibuf;
1159
1160         IMB_refImBuf(stableibuf);
1161
1162         return stableibuf;
1163 }
1164
1165 ImBuf *BKE_movieclip_get_stable_ibuf(MovieClip *clip, MovieClipUser *user, float loc[2], float *scale, float *angle,
1166                                      int postprocess_flag)
1167 {
1168         ImBuf *ibuf, *stableibuf = NULL;
1169         int framenr = user->framenr;
1170
1171         ibuf = BKE_movieclip_get_postprocessed_ibuf(clip, user, postprocess_flag);
1172
1173         if (!ibuf)
1174                 return NULL;
1175
1176         if (clip->tracking.stabilization.flag & TRACKING_2D_STABILIZATION) {
1177                 MovieClipCache *cache = clip->cache;
1178
1179                 stableibuf = get_stable_cached_frame(clip, user, ibuf, framenr, postprocess_flag);
1180
1181                 if (!stableibuf)
1182                         stableibuf = put_stabilized_frame_to_cache(clip, user, ibuf, framenr, postprocess_flag);
1183
1184                 if (loc)
1185                         copy_v2_v2(loc, cache->stabilized.loc);
1186
1187                 if (scale)
1188                         *scale = cache->stabilized.scale;
1189
1190                 if (angle)
1191                         *angle = cache->stabilized.angle;
1192         }
1193         else {
1194                 if (loc)
1195                         zero_v2(loc);
1196
1197                 if (scale)
1198                         *scale = 1.0f;
1199
1200                 if (angle)
1201                         *angle = 0.0f;
1202
1203                 stableibuf = ibuf;
1204         }
1205
1206         if (stableibuf != ibuf) {
1207                 IMB_freeImBuf(ibuf);
1208                 ibuf = stableibuf;
1209         }
1210
1211         return ibuf;
1212
1213 }
1214
1215 bool BKE_movieclip_has_frame(MovieClip *clip, MovieClipUser *user)
1216 {
1217         ImBuf *ibuf = BKE_movieclip_get_ibuf(clip, user);
1218
1219         if (ibuf) {
1220                 IMB_freeImBuf(ibuf);
1221                 return true;
1222         }
1223
1224         return false;
1225 }
1226
1227 void BKE_movieclip_get_size(MovieClip *clip, MovieClipUser *user, int *width, int *height)
1228 {
1229 #if 0
1230         /* originally was needed to support image sequences with different image dimensions,
1231          * which might be useful for such things as reconstruction of unordered image sequence,
1232          * or painting/rotoscoping of non-equal-sized images, but this ended up in unneeded
1233          * cache lookups and even unwanted non-proxied files loading when doing mask parenting,
1234          * so let's disable this for now and assume image sequence consists of images with
1235          * equal sizes (sergey)
1236          * TODO(sergey): Support reading sequences of different resolution.
1237          */
1238         if (user->framenr == clip->lastframe) {
1239 #endif
1240         if (clip->lastsize[0] != 0 && clip->lastsize[1] != 0) {
1241                 *width = clip->lastsize[0];
1242                 *height = clip->lastsize[1];
1243         }
1244         else {
1245                 ImBuf *ibuf = BKE_movieclip_get_ibuf(clip, user);
1246
1247                 if (ibuf && ibuf->x && ibuf->y) {
1248                         real_ibuf_size(clip, user, ibuf, width, height);
1249                 }
1250                 else {
1251                         *width = clip->lastsize[0];
1252                         *height = clip->lastsize[1];
1253                 }
1254
1255                 if (ibuf)
1256                         IMB_freeImBuf(ibuf);
1257         }
1258 }
1259 void BKE_movieclip_get_size_fl(MovieClip *clip, MovieClipUser *user, float size[2])
1260 {
1261         int width, height;
1262         BKE_movieclip_get_size(clip, user, &width, &height);
1263
1264         size[0] = (float)width;
1265         size[1] = (float)height;
1266 }
1267
1268 int BKE_movieclip_get_duration(MovieClip *clip)
1269 {
1270         if (!clip->len) {
1271                 movieclip_calc_length(clip);
1272         }
1273
1274         return clip->len;
1275 }
1276
1277 float BKE_movieclip_get_fps(MovieClip *clip)
1278 {
1279         if (clip->source != MCLIP_SRC_MOVIE) {
1280                 return 0.0f;
1281         }
1282         movieclip_open_anim_file(clip);
1283         if (clip->anim == NULL) {
1284                 return 0.0f;
1285         }
1286         short frs_sec;
1287         float frs_sec_base;
1288         if (IMB_anim_get_fps(clip->anim, &frs_sec, &frs_sec_base, true)) {
1289                 return (float)frs_sec / frs_sec_base;
1290         }
1291         return 0.0f;
1292 }
1293
1294 void BKE_movieclip_get_aspect(MovieClip *clip, float *aspx, float *aspy)
1295 {
1296         *aspx = 1.0;
1297
1298         /* x is always 1 */
1299         *aspy = clip->aspy / clip->aspx / clip->tracking.camera.pixel_aspect;
1300 }
1301
1302 /* get segments of cached frames. useful for debugging cache policies */
1303 void BKE_movieclip_get_cache_segments(MovieClip *clip, MovieClipUser *user, int *r_totseg, int **r_points)
1304 {
1305         *r_totseg = 0;
1306         *r_points = NULL;
1307
1308         if (clip->cache) {
1309                 int proxy = rendersize_to_proxy(user, clip->flag);
1310
1311                 IMB_moviecache_get_cache_segments(clip->cache->moviecache, proxy, user->render_flag, r_totseg, r_points);
1312         }
1313 }
1314
1315 void BKE_movieclip_user_set_frame(MovieClipUser *iuser, int framenr)
1316 {
1317         /* TODO: clamp framenr here? */
1318
1319         iuser->framenr = framenr;
1320 }
1321
1322 static void free_buffers(MovieClip *clip)
1323 {
1324         if (clip->cache) {
1325                 IMB_moviecache_free(clip->cache->moviecache);
1326
1327                 if (clip->cache->postprocessed.ibuf)
1328                         IMB_freeImBuf(clip->cache->postprocessed.ibuf);
1329
1330                 if (clip->cache->stabilized.ibuf)
1331                         IMB_freeImBuf(clip->cache->stabilized.ibuf);
1332
1333                 MEM_freeN(clip->cache);
1334                 clip->cache = NULL;
1335         }
1336
1337         if (clip->anim) {
1338                 IMB_free_anim(clip->anim);
1339                 clip->anim = NULL;
1340         }
1341 }
1342
1343 void BKE_movieclip_clear_cache(MovieClip *clip)
1344 {
1345         free_buffers(clip);
1346 }
1347
1348 void BKE_movieclip_clear_proxy_cache(MovieClip *clip)
1349 {
1350         if (clip->cache && clip->cache->moviecache) {
1351                 IMB_moviecache_cleanup(clip->cache->moviecache,
1352                                        moviecache_check_free_proxy,
1353                                        NULL);
1354         }
1355 }
1356
1357 void BKE_movieclip_reload(Main *bmain, MovieClip *clip)
1358 {
1359         /* clear cache */
1360         free_buffers(clip);
1361
1362         /* update clip source */
1363         detect_clip_source(bmain, clip);
1364
1365         clip->lastsize[0] = clip->lastsize[1] = 0;
1366         movieclip_load_get_size(clip);
1367
1368         movieclip_calc_length(clip);
1369
1370         /* same as for image update -- don't use notifiers because they are not 100% sure to succeeded
1371          * (node trees which are not currently visible wouldn't be refreshed)
1372          */
1373         {
1374                 Scene *scene;
1375                 for (scene = bmain->scene.first; scene; scene = scene->id.next) {
1376                         if (scene->nodetree) {
1377                                 nodeUpdateID(scene->nodetree, &clip->id);
1378                         }
1379                 }
1380         }
1381 }
1382
1383 void BKE_movieclip_update_scopes(MovieClip *clip, MovieClipUser *user, MovieClipScopes *scopes)
1384 {
1385         if (scopes->ok)
1386                 return;
1387
1388         if (scopes->track_preview) {
1389                 IMB_freeImBuf(scopes->track_preview);
1390                 scopes->track_preview = NULL;
1391         }
1392
1393         if (scopes->track_search) {
1394                 IMB_freeImBuf(scopes->track_search);
1395                 scopes->track_search = NULL;
1396         }
1397
1398         scopes->marker = NULL;
1399         scopes->track = NULL;
1400         scopes->track_locked = true;
1401
1402         if (clip) {
1403                 MovieTrackingTrack *act_track = BKE_tracking_track_get_active(&clip->tracking);
1404
1405                 if (act_track) {
1406                         MovieTrackingTrack *track = act_track;
1407                         int framenr = BKE_movieclip_remap_scene_to_clip_frame(clip, user->framenr);
1408                         MovieTrackingMarker *marker = BKE_tracking_marker_get(track, framenr);
1409
1410                         scopes->marker = marker;
1411                         scopes->track = track;
1412
1413                         if (marker->flag & MARKER_DISABLED) {
1414                                 scopes->track_disabled = true;
1415                         }
1416                         else {
1417                                 ImBuf *ibuf = BKE_movieclip_get_ibuf(clip, user);
1418
1419                                 scopes->track_disabled = false;
1420
1421                                 if (ibuf && (ibuf->rect || ibuf->rect_float)) {
1422                                         MovieTrackingMarker undist_marker = *marker;
1423
1424                                         if (user->render_flag & MCLIP_PROXY_RENDER_UNDISTORT) {
1425                                                 int width, height;
1426                                                 float aspy = 1.0f / clip->tracking.camera.pixel_aspect;
1427
1428                                                 BKE_movieclip_get_size(clip, user, &width, &height);
1429
1430                                                 undist_marker.pos[0] *= width;
1431                                                 undist_marker.pos[1] *= height * aspy;
1432
1433                                                 BKE_tracking_undistort_v2(&clip->tracking, undist_marker.pos, undist_marker.pos);
1434
1435                                                 undist_marker.pos[0] /= width;
1436                                                 undist_marker.pos[1] /= height * aspy;
1437                                         }
1438
1439                                         scopes->track_search = BKE_tracking_get_search_imbuf(ibuf, track, &undist_marker, true, true);
1440
1441                                         scopes->undist_marker = undist_marker;
1442
1443                                         scopes->frame_width = ibuf->x;
1444                                         scopes->frame_height = ibuf->y;
1445
1446                                         scopes->use_track_mask = (track->flag & TRACK_PREVIEW_ALPHA) != 0;
1447                                 }
1448
1449                                 IMB_freeImBuf(ibuf);
1450                         }
1451
1452                         if ((track->flag & TRACK_LOCKED) == 0) {
1453                                 float pat_min[2], pat_max[2];
1454
1455                                 scopes->track_locked = false;
1456
1457                                 /* XXX: would work fine with non-transformed patterns, but would likely fail
1458                                  *      with transformed patterns, but that would be easier to debug when
1459                                  *      we'll have real pattern sampling (at least to test) */
1460                                 BKE_tracking_marker_pattern_minmax(marker, pat_min, pat_max);
1461
1462                                 scopes->slide_scale[0] = pat_max[0] - pat_min[0];
1463                                 scopes->slide_scale[1] = pat_max[1] - pat_min[1];
1464                         }
1465                 }
1466         }
1467
1468         scopes->framenr = user->framenr;
1469         scopes->ok = true;
1470 }
1471
1472 static void movieclip_build_proxy_ibuf(MovieClip *clip, ImBuf *ibuf, int cfra, int proxy_render_size, bool undistorted, bool threaded)
1473 {
1474         char name[FILE_MAX];
1475         int quality, rectx, recty;
1476         int size = rendersize_to_number(proxy_render_size);
1477         ImBuf *scaleibuf;
1478
1479         get_proxy_fname(clip, proxy_render_size, undistorted, cfra, name);
1480
1481         rectx = ibuf->x * size / 100.0f;
1482         recty = ibuf->y * size / 100.0f;
1483
1484         scaleibuf = IMB_dupImBuf(ibuf);
1485
1486         if (threaded)
1487                 IMB_scaleImBuf_threaded(scaleibuf, (short)rectx, (short)recty);
1488         else
1489                 IMB_scaleImBuf(scaleibuf, (short)rectx, (short)recty);
1490
1491         quality = clip->proxy.quality;
1492         scaleibuf->ftype = IMB_FTYPE_JPG;
1493         scaleibuf->foptions.quality = quality;
1494         /* unsupported feature only confuses other s/w */
1495         if (scaleibuf->planes == 32)
1496                 scaleibuf->planes = 24;
1497
1498         /* TODO: currently the most weak part of multithreaded proxies,
1499          *       could be solved in a way that thread only prepares memory
1500          *       buffer and write to disk happens separately
1501          */
1502         BLI_thread_lock(LOCK_MOVIECLIP);
1503
1504         BLI_make_existing_file(name);
1505         if (IMB_saveiff(scaleibuf, name, IB_rect) == 0)
1506                 perror(name);
1507
1508         BLI_thread_unlock(LOCK_MOVIECLIP);
1509
1510         IMB_freeImBuf(scaleibuf);
1511 }
1512
1513 /* note: currently used by proxy job for movies, threading happens within single frame
1514  * (meaning scaling shall be threaded)
1515  */
1516 void BKE_movieclip_build_proxy_frame(MovieClip *clip, int clip_flag, struct MovieDistortion *distortion,
1517                                      int cfra, int *build_sizes, int build_count, bool undistorted)
1518 {
1519         ImBuf *ibuf;
1520         MovieClipUser user;
1521
1522         if (!build_count)
1523                 return;
1524
1525         user.framenr = cfra;
1526         user.render_flag = 0;
1527         user.render_size = MCLIP_PROXY_RENDER_SIZE_FULL;
1528
1529         ibuf = BKE_movieclip_get_ibuf_flag(clip, &user, clip_flag, MOVIECLIP_CACHE_SKIP);
1530
1531         if (ibuf) {
1532                 ImBuf *tmpibuf = ibuf;
1533                 int i;
1534
1535                 if (undistorted)
1536                         tmpibuf = get_undistorted_ibuf(clip, distortion, ibuf);
1537
1538                 for (i = 0; i < build_count; i++)
1539                         movieclip_build_proxy_ibuf(clip, tmpibuf, cfra, build_sizes[i], undistorted, true);
1540
1541                 IMB_freeImBuf(ibuf);
1542
1543                 if (tmpibuf != ibuf)
1544                         IMB_freeImBuf(tmpibuf);
1545         }
1546 }
1547
1548 /* note: currently used by proxy job for sequences, threading happens within sequence
1549  * (different threads handles different frames, no threading within frame is needed)
1550  */
1551 void BKE_movieclip_build_proxy_frame_for_ibuf(MovieClip *clip, ImBuf *ibuf, struct MovieDistortion *distortion,
1552                                               int cfra, int *build_sizes, int build_count, bool undistorted)
1553 {
1554         if (!build_count)
1555                 return;
1556
1557         if (ibuf) {
1558                 ImBuf *tmpibuf = ibuf;
1559                 int i;
1560
1561                 if (undistorted)
1562                         tmpibuf = get_undistorted_ibuf(clip, distortion, ibuf);
1563
1564                 for (i = 0; i < build_count; i++)
1565                         movieclip_build_proxy_ibuf(clip, tmpibuf, cfra, build_sizes[i], undistorted, false);
1566
1567                 if (tmpibuf != ibuf)
1568                         IMB_freeImBuf(tmpibuf);
1569         }
1570 }
1571
1572 /** Free (or release) any data used by this movie clip (does not free the clip itself). */
1573 void BKE_movieclip_free(MovieClip *clip)
1574 {
1575         /* Also frees animdata. */
1576         free_buffers(clip);
1577
1578         BKE_tracking_free(&clip->tracking);
1579         BKE_animdata_free((ID *) clip, false);
1580 }
1581
1582 /**
1583  * Only copy internal data of MovieClip ID from source to already allocated/initialized destination.
1584  * You probably never want to use that directly, use BKE_id_copy or BKE_id_copy_ex for typical needs.
1585  *
1586  * WARNING! This function will not handle ID user count!
1587  *
1588  * \param flag: Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
1589  */
1590 void BKE_movieclip_copy_data(Main *UNUSED(bmain), MovieClip *clip_dst, const MovieClip *clip_src, const int flag)
1591 {
1592         /* We never handle usercount here for own data. */
1593         const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT;
1594
1595         clip_dst->anim = NULL;
1596         clip_dst->cache = NULL;
1597
1598         BKE_tracking_copy(&clip_dst->tracking, &clip_src->tracking, flag_subdata);
1599         clip_dst->tracking_context = NULL;
1600
1601         BKE_color_managed_colorspace_settings_copy(&clip_dst->colorspace_settings, &clip_src->colorspace_settings);
1602 }
1603
1604 MovieClip *BKE_movieclip_copy(Main *bmain, const MovieClip *clip)
1605 {
1606         MovieClip *clip_copy;
1607         BKE_id_copy(bmain, &clip->id, (ID **)&clip_copy);
1608         return clip_copy;
1609 }
1610
1611 void BKE_movieclip_make_local(Main *bmain, MovieClip *clip, const bool lib_local)
1612 {
1613         BKE_id_make_local_generic(bmain, &clip->id, true, lib_local);
1614 }
1615
1616 float BKE_movieclip_remap_scene_to_clip_frame(const MovieClip *clip, float framenr)
1617 {
1618         return framenr - (float) clip->start_frame + 1.0f;
1619 }
1620
1621 float BKE_movieclip_remap_clip_to_scene_frame(const MovieClip *clip, float framenr)
1622 {
1623         return framenr + (float) clip->start_frame - 1.0f;
1624 }
1625
1626 void BKE_movieclip_filename_for_frame(MovieClip *clip, MovieClipUser *user, char *name)
1627 {
1628         if (clip->source == MCLIP_SRC_SEQUENCE) {
1629                 int use_proxy;
1630
1631                 use_proxy = (clip->flag & MCLIP_USE_PROXY) && user->render_size != MCLIP_PROXY_RENDER_SIZE_FULL;
1632
1633                 if (use_proxy) {
1634                         int undistort = user->render_flag & MCLIP_PROXY_RENDER_UNDISTORT;
1635                         get_proxy_fname(clip, user->render_size, undistort, user->framenr, name);
1636                 }
1637                 else {
1638                         get_sequence_fname(clip, user->framenr, name);
1639                 }
1640         }
1641         else {
1642                 BLI_strncpy(name, clip->name, FILE_MAX);
1643                 BLI_path_abs(name, ID_BLEND_PATH_FROM_GLOBAL(&clip->id));
1644         }
1645 }
1646
1647 ImBuf *BKE_movieclip_anim_ibuf_for_frame(MovieClip *clip, MovieClipUser *user)
1648 {
1649         ImBuf *ibuf = NULL;
1650
1651         if (clip->source == MCLIP_SRC_MOVIE) {
1652                 BLI_thread_lock(LOCK_MOVIECLIP);
1653                 ibuf = movieclip_load_movie_file(clip, user, user->framenr, clip->flag);
1654                 BLI_thread_unlock(LOCK_MOVIECLIP);
1655         }
1656
1657         return ibuf;
1658 }
1659
1660 bool BKE_movieclip_has_cached_frame(MovieClip *clip, MovieClipUser *user)
1661 {
1662         bool has_frame = false;
1663
1664         BLI_thread_lock(LOCK_MOVIECLIP);
1665         has_frame = has_imbuf_cache(clip, user, clip->flag);
1666         BLI_thread_unlock(LOCK_MOVIECLIP);
1667
1668         return has_frame;
1669 }
1670
1671 bool BKE_movieclip_put_frame_if_possible(MovieClip *clip,
1672                                          MovieClipUser *user,
1673                                          ImBuf *ibuf)
1674 {
1675         bool result;
1676
1677         BLI_thread_lock(LOCK_MOVIECLIP);
1678         result = put_imbuf_cache(clip, user, ibuf, clip->flag, false);
1679         BLI_thread_unlock(LOCK_MOVIECLIP);
1680
1681         return result;
1682 }
1683
1684 static void movieclip_selection_synchronize(MovieClip *clip_dst, const MovieClip *clip_src)
1685 {
1686         BLI_assert(clip_dst != clip_src);
1687         MovieTracking *tracking_dst = &clip_dst->tracking, tracking_src = clip_src->tracking;
1688         /* Syncs the active object, track and plane track. */
1689         tracking_dst->objectnr = tracking_src.objectnr;
1690         const int active_track_index = BLI_findindex(&tracking_src.tracks, tracking_src.act_track);
1691         const int active_plane_track_index = BLI_findindex(&tracking_src.plane_tracks, tracking_src.act_plane_track);
1692         tracking_dst->act_track = BLI_findlink(&tracking_dst->tracks, active_track_index);
1693         tracking_dst->act_plane_track = BLI_findlink(&tracking_dst->plane_tracks, active_plane_track_index);
1694
1695         /* Syncs the tracking selection flag. */
1696         MovieTrackingObject *tracking_object_dst, *tracking_object_src;
1697         tracking_object_src = tracking_src.objects.first;
1698
1699         for (tracking_object_dst = tracking_dst->objects.first;
1700              tracking_object_dst != NULL;
1701              tracking_object_dst = tracking_object_dst->next,
1702              tracking_object_src = tracking_object_src->next)
1703         {
1704                 ListBase *tracksbase_dst, *tracksbase_src;
1705                 tracksbase_dst = BKE_tracking_object_get_tracks(tracking_dst, tracking_object_dst);
1706                 tracksbase_src = BKE_tracking_object_get_tracks(&tracking_src, tracking_object_src);
1707
1708                 MovieTrackingTrack *track_dst, *track_src;
1709                 track_src = tracksbase_src->first;
1710                 for (track_dst = tracksbase_dst->first;
1711                      track_dst != NULL;
1712                      track_dst = track_dst->next, track_src = track_src->next)
1713                 {
1714                         track_dst->flag = track_src->flag;
1715                         track_dst->pat_flag = track_src->pat_flag;
1716                         track_dst->search_flag = track_src->search_flag;
1717                 }
1718         }
1719 }
1720
1721 void BKE_movieclip_eval_update(struct Depsgraph *depsgraph, MovieClip *clip)
1722 {
1723         DEG_debug_print_eval(depsgraph, __func__, clip->id.name, clip);
1724         BKE_tracking_dopesheet_tag_update(&clip->tracking);
1725 }
1726
1727 void BKE_movieclip_eval_selection_update(struct Depsgraph *depsgraph, MovieClip *clip)
1728 {
1729         DEG_debug_print_eval(depsgraph, __func__, clip->id.name, clip);
1730         movieclip_selection_synchronize(clip, (MovieClip *)clip->id.orig_id);
1731 }