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