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