f063933f3f91553c16f8a513328aa7fcabf880b6
[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;
226
227         /* read ibuf */
228         ibuf = IMB_loadiffname(name, loadflag, colorspace);
229
230 #ifdef WITH_OPENEXR
231         if (ibuf) {
232                 if (ibuf->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, len;
607         const char *libname;
608         char str[FILE_MAX], strtest[FILE_MAX];
609
610         BLI_strncpy(str, name, sizeof(str));
611         BLI_path_abs(str, bmain->name);
612
613         /* exists? */
614         file = BLI_open(str, O_BINARY | O_RDONLY, 0);
615         if (file == -1)
616                 return NULL;
617         close(file);
618
619         /* ** first search an identical clip ** */
620         for (clip = bmain->movieclip.first; clip; clip = clip->id.next) {
621                 BLI_strncpy(strtest, clip->name, sizeof(clip->name));
622                 BLI_path_abs(strtest, G.main->name);
623
624                 if (STREQ(strtest, str)) {
625                         BLI_strncpy(clip->name, name, sizeof(clip->name));  /* for stringcode */
626                         clip->id.us++;  /* officially should not, it doesn't link here! */
627
628                         return clip;
629                 }
630         }
631
632         /* ** add new movieclip ** */
633
634         /* create a short library name */
635         len = strlen(name);
636
637         while (len > 0 && name[len - 1] != '/' && name[len - 1] != '\\')
638                 len--;
639         libname = name + len;
640
641         clip = movieclip_alloc(bmain, libname);
642         BLI_strncpy(clip->name, name, sizeof(clip->name));
643
644         detect_clip_source(clip);
645
646         movieclip_load_get_szie(clip);
647         if (clip->lastsize[0]) {
648                 int width = clip->lastsize[0];
649
650                 clip->tracking.camera.focal = 24.0f * width / clip->tracking.camera.sensor_width;
651         }
652
653         movieclip_calc_length(clip);
654
655         return clip;
656 }
657
658 static void real_ibuf_size(MovieClip *clip, MovieClipUser *user, ImBuf *ibuf, int *width, int *height)
659 {
660         *width = ibuf->x;
661         *height = ibuf->y;
662
663         if (clip->flag & MCLIP_USE_PROXY) {
664                 switch (user->render_size) {
665                         case MCLIP_PROXY_RENDER_SIZE_25:
666                                 (*width) *= 4;
667                                 (*height) *= 4;
668                                 break;
669
670                         case MCLIP_PROXY_RENDER_SIZE_50:
671                                 (*width) *= 2.0f;
672                                 (*height) *= 2.0f;
673                                 break;
674
675                         case MCLIP_PROXY_RENDER_SIZE_75:
676                                 *width = ((float)*width) * 4.0f / 3.0f;
677                                 *height = ((float)*height) * 4.0f / 3.0f;
678                                 break;
679                 }
680         }
681 }
682
683 static ImBuf *get_undistorted_ibuf(MovieClip *clip, struct MovieDistortion *distortion, ImBuf *ibuf)
684 {
685         ImBuf *undistibuf;
686
687         if (distortion)
688                 undistibuf = BKE_tracking_distortion_exec(distortion, &clip->tracking, ibuf, ibuf->x, ibuf->y, 0.0f, 1);
689         else
690                 undistibuf = BKE_tracking_undistort_frame(&clip->tracking, ibuf, ibuf->x, ibuf->y, 0.0f);
691
692         IMB_scaleImBuf(undistibuf, ibuf->x, ibuf->y);
693
694         return undistibuf;
695 }
696
697 static int need_undistortion_postprocess(MovieClipUser *user)
698 {
699         int result = 0;
700
701         /* only full undistorted render can be used as on-fly undistorting image */
702         result |= (user->render_size == MCLIP_PROXY_RENDER_SIZE_FULL) &&
703                   (user->render_flag & MCLIP_PROXY_RENDER_UNDISTORT) != 0;
704
705         return result;
706 }
707
708 static int need_postprocessed_frame(MovieClipUser *user, int postprocess_flag)
709 {
710         int result = postprocess_flag;
711
712         result |= need_undistortion_postprocess(user);
713
714         return result;
715 }
716
717 static bool check_undistortion_cache_flags(MovieClip *clip)
718 {
719         MovieClipCache *cache = clip->cache;
720         MovieTrackingCamera *camera = &clip->tracking.camera;
721
722         /* check for distortion model changes */
723         if (!equals_v2v2(camera->principal, cache->postprocessed.principal)) {
724                 return false;
725         }
726
727         if (camera->distortion_model != cache->postprocessed.distortion_model) {
728                 return false;
729         }
730
731         if (!equals_v3v3(&camera->k1, &cache->postprocessed.polynomial_k1)) {
732                 return false;
733         }
734
735         if (!equals_v2v2(&camera->division_k1, &cache->postprocessed.division_k1)) {
736                 return false;
737         }
738
739         return true;
740 }
741
742 static ImBuf *get_postprocessed_cached_frame(MovieClip *clip, MovieClipUser *user, int flag, int postprocess_flag)
743 {
744         MovieClipCache *cache = clip->cache;
745         int framenr = user->framenr;
746         short proxy = IMB_PROXY_NONE;
747         int render_flag = 0;
748
749         if (flag & MCLIP_USE_PROXY) {
750                 proxy = rendersize_to_proxy(user, flag);
751                 render_flag = user->render_flag;
752         }
753
754         /* no cache or no cached postprocessed image */
755         if (!clip->cache || !clip->cache->postprocessed.ibuf)
756                 return NULL;
757
758         /* postprocessing happened for other frame */
759         if (cache->postprocessed.framenr != framenr)
760                 return NULL;
761
762         /* cached ibuf used different proxy settings */
763         if (cache->postprocessed.render_flag != render_flag || cache->postprocessed.proxy != proxy)
764                 return NULL;
765
766         if (cache->postprocessed.flag != postprocess_flag)
767                 return NULL;
768
769         if (need_undistortion_postprocess(user)) {
770                 if (!check_undistortion_cache_flags(clip))
771                         return NULL;
772         }
773         else if (cache->postprocessed.undistortion_used)
774                 return NULL;
775
776         IMB_refImBuf(cache->postprocessed.ibuf);
777
778         return cache->postprocessed.ibuf;
779 }
780
781 static ImBuf *postprocess_frame(MovieClip *clip, MovieClipUser *user, ImBuf *ibuf, int postprocess_flag)
782 {
783         ImBuf *postproc_ibuf = NULL;
784
785         if (need_undistortion_postprocess(user)) {
786                 postproc_ibuf = get_undistorted_ibuf(clip, NULL, ibuf);
787         }
788         else {
789                 postproc_ibuf = IMB_dupImBuf(ibuf);
790         }
791
792         if (postprocess_flag) {
793                 bool disable_red   = (postprocess_flag & MOVIECLIP_DISABLE_RED) != 0;
794                 bool disable_green = (postprocess_flag & MOVIECLIP_DISABLE_GREEN) != 0;
795                 bool disable_blue  = (postprocess_flag & MOVIECLIP_DISABLE_BLUE) != 0;
796                 bool grayscale     = (postprocess_flag & MOVIECLIP_PREVIEW_GRAYSCALE) != 0;
797
798                 if (disable_red || disable_green || disable_blue || grayscale)
799                         BKE_tracking_disable_channels(postproc_ibuf, disable_red, disable_green, disable_blue, 1);
800         }
801
802         return postproc_ibuf;
803 }
804
805 static void put_postprocessed_frame_to_cache(MovieClip *clip, MovieClipUser *user, ImBuf *ibuf,
806                                                int flag, int postprocess_flag)
807 {
808         MovieClipCache *cache = clip->cache;
809         MovieTrackingCamera *camera = &clip->tracking.camera;
810
811         cache->postprocessed.framenr = user->framenr;
812         cache->postprocessed.flag = postprocess_flag;
813
814         if (flag & MCLIP_USE_PROXY) {
815                 cache->postprocessed.proxy = rendersize_to_proxy(user, flag);
816                 cache->postprocessed.render_flag = user->render_flag;
817         }
818         else {
819                 cache->postprocessed.proxy = IMB_PROXY_NONE;
820                 cache->postprocessed.render_flag = 0;
821         }
822
823         if (need_undistortion_postprocess(user)) {
824                 cache->postprocessed.distortion_model = camera->distortion_model;
825                 copy_v2_v2(cache->postprocessed.principal, camera->principal);
826                 copy_v3_v3(&cache->postprocessed.polynomial_k1, &camera->k1);
827                 copy_v2_v2(&cache->postprocessed.division_k1, &camera->division_k1);
828                 cache->postprocessed.undistortion_used = true;
829         }
830         else {
831                 cache->postprocessed.undistortion_used = false;
832         }
833
834         IMB_refImBuf(ibuf);
835
836         if (cache->postprocessed.ibuf)
837                 IMB_freeImBuf(cache->postprocessed.ibuf);
838
839         cache->postprocessed.ibuf = ibuf;
840 }
841
842 static ImBuf *movieclip_get_postprocessed_ibuf(MovieClip *clip, MovieClipUser *user, int flag,
843                                                int postprocess_flag, int cache_flag)
844 {
845         ImBuf *ibuf = NULL;
846         int framenr = user->framenr;
847         bool need_postprocess = false;
848
849         /* cache isn't threadsafe itself and also loading of movies
850          * can't happen from concurent threads that's why we use lock here */
851         BLI_lock_thread(LOCK_MOVIECLIP);
852
853         /* try to obtain cached postprocessed frame first */
854         if (need_postprocessed_frame(user, postprocess_flag)) {
855                 ibuf = get_postprocessed_cached_frame(clip, user, flag, postprocess_flag);
856
857                 if (!ibuf)
858                         need_postprocess = true;
859         }
860
861         if (!ibuf)
862                 ibuf = get_imbuf_cache(clip, user, flag);
863
864         if (!ibuf) {
865                 bool use_sequence = false;
866
867                 /* undistorted proxies for movies should be read as image sequence */
868                 use_sequence = (user->render_flag & MCLIP_PROXY_RENDER_UNDISTORT) &&
869                                (user->render_size != MCLIP_PROXY_RENDER_SIZE_FULL);
870
871                 if (clip->source == MCLIP_SRC_SEQUENCE || use_sequence) {
872                         ibuf = movieclip_load_sequence_file(clip, user, framenr, flag);
873                 }
874                 else {
875                         ibuf = movieclip_load_movie_file(clip, user, framenr, flag);
876                 }
877
878                 if (ibuf && (cache_flag & MOVIECLIP_CACHE_SKIP) == 0)
879                         put_imbuf_cache(clip, user, ibuf, flag, true);
880         }
881
882         if (ibuf) {
883                 clip->lastframe = framenr;
884                 real_ibuf_size(clip, user, ibuf, &clip->lastsize[0], &clip->lastsize[1]);
885
886                 /* postprocess frame and put to cache if needed*/
887                 if (need_postprocess) {
888                         ImBuf *tmpibuf = ibuf;
889                         ibuf = postprocess_frame(clip, user, tmpibuf, postprocess_flag);
890                         IMB_freeImBuf(tmpibuf);
891                         if (ibuf && (cache_flag & MOVIECLIP_CACHE_SKIP) == 0) {
892                                 put_postprocessed_frame_to_cache(clip, user, ibuf, flag, postprocess_flag);
893                         }
894                 }
895         }
896
897         BLI_unlock_thread(LOCK_MOVIECLIP);
898
899         return ibuf;
900 }
901
902 ImBuf *BKE_movieclip_get_ibuf(MovieClip *clip, MovieClipUser *user)
903 {
904         return BKE_movieclip_get_ibuf_flag(clip, user, clip->flag, 0);
905 }
906
907 ImBuf *BKE_movieclip_get_ibuf_flag(MovieClip *clip, MovieClipUser *user, int flag, int cache_flag)
908 {
909         return movieclip_get_postprocessed_ibuf(clip, user, flag, 0, cache_flag);
910 }
911
912 ImBuf *BKE_movieclip_get_postprocessed_ibuf(MovieClip *clip, MovieClipUser *user, int postprocess_flag)
913 {
914         return movieclip_get_postprocessed_ibuf(clip, user, clip->flag, postprocess_flag, 0);
915 }
916
917 static ImBuf *get_stable_cached_frame(MovieClip *clip, MovieClipUser *user, ImBuf *reference_ibuf,
918                                       int framenr, int postprocess_flag)
919 {
920         MovieClipCache *cache = clip->cache;
921         MovieTracking *tracking = &clip->tracking;
922         ImBuf *stableibuf;
923         float tloc[2], tscale, tangle;
924         short proxy = IMB_PROXY_NONE;
925         int render_flag = 0;
926         int clip_framenr = BKE_movieclip_remap_scene_to_clip_frame(clip, framenr);
927
928         if (clip->flag & MCLIP_USE_PROXY) {
929                 proxy = rendersize_to_proxy(user, clip->flag);
930                 render_flag = user->render_flag;
931         }
932
933         /* there's no cached frame or it was calculated for another frame */
934         if (!cache->stabilized.ibuf || cache->stabilized.framenr != framenr)
935                 return NULL;
936
937         if (cache->stabilized.reference_ibuf != reference_ibuf)
938                 return NULL;
939
940         /* cached ibuf used different proxy settings */
941         if (cache->stabilized.render_flag != render_flag || cache->stabilized.proxy != proxy)
942                 return NULL;
943
944         if (cache->stabilized.postprocess_flag != postprocess_flag)
945                 return NULL;
946
947         /* stabilization also depends on pixel aspect ratio */
948         if (cache->stabilized.aspect != tracking->camera.pixel_aspect)
949                 return NULL;
950
951         if (cache->stabilized.filter != tracking->stabilization.filter)
952                 return NULL;
953
954         stableibuf = cache->stabilized.ibuf;
955
956         BKE_tracking_stabilization_data_get(&clip->tracking, clip_framenr, stableibuf->x, stableibuf->y, tloc, &tscale, &tangle);
957
958         /* check for stabilization parameters */
959         if (tscale != cache->stabilized.scale ||
960             tangle != cache->stabilized.angle ||
961             !equals_v2v2(tloc, cache->stabilized.loc))
962         {
963                 return NULL;
964         }
965
966         IMB_refImBuf(stableibuf);
967
968         return stableibuf;
969 }
970
971 static ImBuf *put_stabilized_frame_to_cache(MovieClip *clip, MovieClipUser *user, ImBuf *ibuf,
972                                             int framenr, int postprocess_flag)
973 {
974         MovieClipCache *cache = clip->cache;
975         MovieTracking *tracking = &clip->tracking;
976         ImBuf *stableibuf;
977         float tloc[2], tscale, tangle;
978         int clip_framenr = BKE_movieclip_remap_scene_to_clip_frame(clip, framenr);
979
980         stableibuf = BKE_tracking_stabilize_frame(&clip->tracking, clip_framenr, ibuf, tloc, &tscale, &tangle);
981
982         copy_v2_v2(cache->stabilized.loc, tloc);
983
984         cache->stabilized.reference_ibuf = ibuf;
985         cache->stabilized.scale = tscale;
986         cache->stabilized.angle = tangle;
987         cache->stabilized.framenr = framenr;
988         cache->stabilized.aspect = tracking->camera.pixel_aspect;
989         cache->stabilized.filter = tracking->stabilization.filter;
990
991         if (clip->flag & MCLIP_USE_PROXY) {
992                 cache->stabilized.proxy = rendersize_to_proxy(user, clip->flag);
993                 cache->stabilized.render_flag = user->render_flag;
994         }
995         else {
996                 cache->stabilized.proxy = IMB_PROXY_NONE;
997                 cache->stabilized.render_flag = 0;
998         }
999
1000         cache->stabilized.postprocess_flag = postprocess_flag;
1001
1002         if (cache->stabilized.ibuf)
1003                 IMB_freeImBuf(cache->stabilized.ibuf);
1004
1005         cache->stabilized.ibuf = stableibuf;
1006
1007         IMB_refImBuf(stableibuf);
1008
1009         return stableibuf;
1010 }
1011
1012 ImBuf *BKE_movieclip_get_stable_ibuf(MovieClip *clip, MovieClipUser *user, float loc[2], float *scale, float *angle,
1013                                      int postprocess_flag)
1014 {
1015         ImBuf *ibuf, *stableibuf = NULL;
1016         int framenr = user->framenr;
1017
1018         ibuf = BKE_movieclip_get_postprocessed_ibuf(clip, user, postprocess_flag);
1019
1020         if (!ibuf)
1021                 return NULL;
1022
1023         if (clip->tracking.stabilization.flag & TRACKING_2D_STABILIZATION) {
1024                 MovieClipCache *cache = clip->cache;
1025
1026                 stableibuf = get_stable_cached_frame(clip, user, ibuf, framenr, postprocess_flag);
1027
1028                 if (!stableibuf)
1029                         stableibuf = put_stabilized_frame_to_cache(clip, user, ibuf, framenr, postprocess_flag);
1030
1031                 if (loc)
1032                         copy_v2_v2(loc, cache->stabilized.loc);
1033
1034                 if (scale)
1035                         *scale = cache->stabilized.scale;
1036
1037                 if (angle)
1038                         *angle = cache->stabilized.angle;
1039         }
1040         else {
1041                 if (loc)
1042                         zero_v2(loc);
1043
1044                 if (scale)
1045                         *scale = 1.0f;
1046
1047                 if (angle)
1048                         *angle = 0.0f;
1049
1050                 stableibuf = ibuf;
1051         }
1052
1053         if (stableibuf != ibuf) {
1054                 IMB_freeImBuf(ibuf);
1055                 ibuf = stableibuf;
1056         }
1057
1058         return ibuf;
1059
1060 }
1061
1062 bool BKE_movieclip_has_frame(MovieClip *clip, MovieClipUser *user)
1063 {
1064         ImBuf *ibuf = BKE_movieclip_get_ibuf(clip, user);
1065
1066         if (ibuf) {
1067                 IMB_freeImBuf(ibuf);
1068                 return true;
1069         }
1070
1071         return false;
1072 }
1073
1074 void BKE_movieclip_get_size(MovieClip *clip, MovieClipUser *user, int *width, int *height)
1075 {
1076 #if 0
1077         /* originally was needed to support image sequences with different image dimensions,
1078          * which might be useful for such things as reconstruction of unordered image sequence,
1079          * or painting/rotoscoping of non-equal-sized images, but this ended up in unneeded
1080          * cache lookups and even unwanted non-proxied files loading when doing mask parenting,
1081          * so let's disable this for now and assume image sequence consists of images with
1082          * equal sizes (sergey)
1083          */
1084         if (user->framenr == clip->lastframe) {
1085 #endif
1086         if (clip->lastsize[0] != 0 && clip->lastsize[1] != 0) {
1087                 *width = clip->lastsize[0];
1088                 *height = clip->lastsize[1];
1089         }
1090         else {
1091                 ImBuf *ibuf = BKE_movieclip_get_ibuf(clip, user);
1092
1093                 if (ibuf && ibuf->x && ibuf->y) {
1094                         real_ibuf_size(clip, user, ibuf, width, height);
1095                 }
1096                 else {
1097                         *width = clip->lastsize[0];
1098                         *height = clip->lastsize[1];
1099                 }
1100
1101                 if (ibuf)
1102                         IMB_freeImBuf(ibuf);
1103         }
1104 }
1105 void BKE_movieclip_get_size_fl(MovieClip *clip, MovieClipUser *user, float size[2])
1106 {
1107         int width, height;
1108         BKE_movieclip_get_size(clip, user, &width, &height);
1109
1110         size[0] = (float)width;
1111         size[1] = (float)height;
1112 }
1113
1114 int BKE_movieclip_get_duration(MovieClip *clip)
1115 {
1116         if (!clip->len) {
1117                 movieclip_calc_length(clip);
1118         }
1119
1120         return clip->len;
1121 }
1122
1123 void BKE_movieclip_get_aspect(MovieClip *clip, float *aspx, float *aspy)
1124 {
1125         *aspx = 1.0;
1126
1127         /* x is always 1 */
1128         *aspy = clip->aspy / clip->aspx / clip->tracking.camera.pixel_aspect;
1129 }
1130
1131 /* get segments of cached frames. useful for debugging cache policies */
1132 void BKE_movieclip_get_cache_segments(MovieClip *clip, MovieClipUser *user, int *r_totseg, int **r_points)
1133 {
1134         *r_totseg = 0;
1135         *r_points = NULL;
1136
1137         if (clip->cache) {
1138                 int proxy = rendersize_to_proxy(user, clip->flag);
1139
1140                 IMB_moviecache_get_cache_segments(clip->cache->moviecache, proxy, user->render_flag, r_totseg, r_points);
1141         }
1142 }
1143
1144 void BKE_movieclip_user_set_frame(MovieClipUser *iuser, int framenr)
1145 {
1146         /* TODO: clamp framenr here? */
1147
1148         iuser->framenr = framenr;
1149 }
1150
1151 static void free_buffers(MovieClip *clip)
1152 {
1153         if (clip->cache) {
1154                 IMB_moviecache_free(clip->cache->moviecache);
1155
1156                 if (clip->cache->postprocessed.ibuf)
1157                         IMB_freeImBuf(clip->cache->postprocessed.ibuf);
1158
1159                 if (clip->cache->stabilized.ibuf)
1160                         IMB_freeImBuf(clip->cache->stabilized.ibuf);
1161
1162                 MEM_freeN(clip->cache);
1163                 clip->cache = NULL;
1164         }
1165
1166         if (clip->anim) {
1167                 IMB_free_anim(clip->anim);
1168                 clip->anim = NULL;
1169         }
1170
1171         BKE_animdata_free((ID *) clip);
1172 }
1173
1174 void BKE_movieclip_clear_cache(MovieClip *clip)
1175 {
1176         free_buffers(clip);
1177 }
1178
1179 void BKE_movieclip_clear_proxy_cache(MovieClip *clip)
1180 {
1181         if (clip->cache && clip->cache->moviecache) {
1182                 IMB_moviecache_cleanup(clip->cache->moviecache,
1183                                        moviecache_check_free_proxy,
1184                                        NULL);
1185         }
1186 }
1187
1188 void BKE_movieclip_reload(MovieClip *clip)
1189 {
1190         /* clear cache */
1191         free_buffers(clip);
1192
1193         clip->tracking.stabilization.ok = false;
1194
1195         /* update clip source */
1196         detect_clip_source(clip);
1197
1198         clip->lastsize[0] = clip->lastsize[1] = 0;
1199         movieclip_load_get_szie(clip);
1200
1201         movieclip_calc_length(clip);
1202
1203         /* same as for image update -- don't use notifiers because they are not 100% sure to succeeded
1204          * (node trees which are not currently visible wouldn't be refreshed)
1205          */
1206         {
1207                 Scene *scene;
1208                 for (scene = G.main->scene.first; scene; scene = scene->id.next) {
1209                         if (scene->nodetree) {
1210                                 nodeUpdateID(scene->nodetree, &clip->id);
1211                         }
1212                 }
1213         }
1214 }
1215
1216 void BKE_movieclip_update_scopes(MovieClip *clip, MovieClipUser *user, MovieClipScopes *scopes)
1217 {
1218         if (scopes->ok)
1219                 return;
1220
1221         if (scopes->track_preview) {
1222                 IMB_freeImBuf(scopes->track_preview);
1223                 scopes->track_preview = NULL;
1224         }
1225
1226         if (scopes->track_search) {
1227                 IMB_freeImBuf(scopes->track_search);
1228                 scopes->track_search = NULL;
1229         }
1230
1231         scopes->marker = NULL;
1232         scopes->track = NULL;
1233         scopes->track_locked = true;
1234
1235         if (clip) {
1236                 MovieTrackingTrack *act_track = BKE_tracking_track_get_active(&clip->tracking);
1237
1238                 if (act_track) {
1239                         MovieTrackingTrack *track = act_track;
1240                         int framenr = BKE_movieclip_remap_scene_to_clip_frame(clip, user->framenr);
1241                         MovieTrackingMarker *marker = BKE_tracking_marker_get(track, framenr);
1242
1243                         scopes->marker = marker;
1244                         scopes->track = track;
1245
1246                         if (marker->flag & MARKER_DISABLED) {
1247                                 scopes->track_disabled = true;
1248                         }
1249                         else {
1250                                 ImBuf *ibuf = BKE_movieclip_get_ibuf(clip, user);
1251
1252                                 scopes->track_disabled = false;
1253
1254                                 if (ibuf && (ibuf->rect || ibuf->rect_float)) {
1255                                         MovieTrackingMarker undist_marker = *marker;
1256
1257                                         if (user->render_flag & MCLIP_PROXY_RENDER_UNDISTORT) {
1258                                                 int width, height;
1259                                                 float aspy = 1.0f / clip->tracking.camera.pixel_aspect;
1260
1261                                                 BKE_movieclip_get_size(clip, user, &width, &height);
1262
1263                                                 undist_marker.pos[0] *= width;
1264                                                 undist_marker.pos[1] *= height * aspy;
1265
1266                                                 BKE_tracking_undistort_v2(&clip->tracking, undist_marker.pos, undist_marker.pos);
1267
1268                                                 undist_marker.pos[0] /= width;
1269                                                 undist_marker.pos[1] /= height * aspy;
1270                                         }
1271
1272                                         scopes->track_search = BKE_tracking_get_search_imbuf(ibuf, track, &undist_marker, true, true);
1273
1274                                         scopes->undist_marker = undist_marker;
1275
1276                                         scopes->frame_width = ibuf->x;
1277                                         scopes->frame_height = ibuf->y;
1278
1279                                         scopes->use_track_mask = (track->flag & TRACK_PREVIEW_ALPHA) != 0;
1280                                 }
1281
1282                                 IMB_freeImBuf(ibuf);
1283                         }
1284
1285                         if ((track->flag & TRACK_LOCKED) == 0) {
1286                                 float pat_min[2], pat_max[2];
1287
1288                                 scopes->track_locked = false;
1289
1290                                 /* XXX: would work fine with non-transformed patterns, but would likely fail
1291                                  *      with transformed patterns, but that would be easier to debug when
1292                                  *      we'll have real pattern sampling (at least to test) */
1293                                 BKE_tracking_marker_pattern_minmax(marker, pat_min, pat_max);
1294
1295                                 scopes->slide_scale[0] = pat_max[0] - pat_min[0];
1296                                 scopes->slide_scale[1] = pat_max[1] - pat_min[1];
1297                         }
1298                 }
1299         }
1300
1301         scopes->framenr = user->framenr;
1302         scopes->ok = true;
1303 }
1304
1305 static void movieclip_build_proxy_ibuf(MovieClip *clip, ImBuf *ibuf, int cfra, int proxy_render_size, bool undistorted, bool threaded)
1306 {
1307         char name[FILE_MAX];
1308         int quality, rectx, recty;
1309         int size = rendersize_to_number(proxy_render_size);
1310         ImBuf *scaleibuf;
1311
1312         get_proxy_fname(clip, proxy_render_size, undistorted, cfra, name);
1313
1314         rectx = ibuf->x * size / 100.0f;
1315         recty = ibuf->y * size / 100.0f;
1316
1317         scaleibuf = IMB_dupImBuf(ibuf);
1318
1319         if (threaded)
1320                 IMB_scaleImBuf_threaded(scaleibuf, (short)rectx, (short)recty);
1321         else
1322                 IMB_scaleImBuf(scaleibuf, (short)rectx, (short)recty);
1323
1324         quality = clip->proxy.quality;
1325         scaleibuf->ftype = JPG | quality;
1326
1327         /* unsupported feature only confuses other s/w */
1328         if (scaleibuf->planes == 32)
1329                 scaleibuf->planes = 24;
1330
1331         /* TODO: currently the most weak part of multithreaded proxies,
1332          *       could be solved in a way that thread only prepares memory
1333          *       buffer and write to disk happens separately
1334          */
1335         BLI_lock_thread(LOCK_MOVIECLIP);
1336
1337         BLI_make_existing_file(name);
1338         if (IMB_saveiff(scaleibuf, name, IB_rect) == 0)
1339                 perror(name);
1340
1341         BLI_unlock_thread(LOCK_MOVIECLIP);
1342
1343         IMB_freeImBuf(scaleibuf);
1344 }
1345
1346 /* note: currently used by proxy job for movies, threading happens within single frame
1347  * (meaning scaling shall be threaded)
1348  */
1349 void BKE_movieclip_build_proxy_frame(MovieClip *clip, int clip_flag, struct MovieDistortion *distortion,
1350                                      int cfra, int *build_sizes, int build_count, bool undistorted)
1351 {
1352         ImBuf *ibuf;
1353         MovieClipUser user;
1354
1355         if (!build_count)
1356                 return;
1357
1358         user.framenr = cfra;
1359         user.render_flag = 0;
1360         user.render_size = MCLIP_PROXY_RENDER_SIZE_FULL;
1361
1362         ibuf = BKE_movieclip_get_ibuf_flag(clip, &user, clip_flag, MOVIECLIP_CACHE_SKIP);
1363
1364         if (ibuf) {
1365                 ImBuf *tmpibuf = ibuf;
1366                 int i;
1367
1368                 if (undistorted)
1369                         tmpibuf = get_undistorted_ibuf(clip, distortion, ibuf);
1370
1371                 for (i = 0; i < build_count; i++)
1372                         movieclip_build_proxy_ibuf(clip, tmpibuf, cfra, build_sizes[i], undistorted, true);
1373
1374                 IMB_freeImBuf(ibuf);
1375
1376                 if (tmpibuf != ibuf)
1377                         IMB_freeImBuf(tmpibuf);
1378         }
1379 }
1380
1381 /* note: currently used by proxy job for sequences, threading happens within sequence
1382  * (different threads handles different frames, no threading within frame is needed)
1383  */
1384 void BKE_movieclip_build_proxy_frame_for_ibuf(MovieClip *clip, ImBuf *ibuf, struct MovieDistortion *distortion,
1385                                               int cfra, int *build_sizes, int build_count, bool undistorted)
1386 {
1387         if (!build_count)
1388                 return;
1389
1390         if (ibuf) {
1391                 ImBuf *tmpibuf = ibuf;
1392                 int i;
1393
1394                 if (undistorted)
1395                         tmpibuf = get_undistorted_ibuf(clip, distortion, ibuf);
1396
1397                 for (i = 0; i < build_count; i++)
1398                         movieclip_build_proxy_ibuf(clip, tmpibuf, cfra, build_sizes[i], undistorted, false);
1399
1400                 if (tmpibuf != ibuf)
1401                         IMB_freeImBuf(tmpibuf);
1402         }
1403 }
1404
1405 void BKE_movieclip_free(MovieClip *clip)
1406 {
1407         free_buffers(clip);
1408
1409         BKE_tracking_free(&clip->tracking);
1410 }
1411
1412 void BKE_movieclip_unlink(Main *bmain, MovieClip *clip)
1413 {
1414         bScreen *scr;
1415         ScrArea *area;
1416         SpaceLink *sl;
1417         Scene *sce;
1418         Object *ob;
1419
1420         for (scr = bmain->screen.first; scr; scr = scr->id.next) {
1421                 for (area = scr->areabase.first; area; area = area->next) {
1422                         for (sl = area->spacedata.first; sl; sl = sl->next) {
1423                                 if (sl->spacetype == SPACE_CLIP) {
1424                                         SpaceClip *sc = (SpaceClip *) sl;
1425
1426                                         if (sc->clip == clip)
1427                                                 sc->clip = NULL;
1428                                 }
1429                                 else if (sl->spacetype == SPACE_VIEW3D) {
1430                                         View3D *v3d = (View3D *) sl;
1431                                         BGpic *bgpic;
1432
1433                                         for (bgpic = v3d->bgpicbase.first; bgpic; bgpic = bgpic->next) {
1434                                                 if (bgpic->clip == clip)
1435                                                         bgpic->clip = NULL;
1436                                         }
1437                                 }
1438                         }
1439                 }
1440         }
1441
1442         for (sce = bmain->scene.first; sce; sce = sce->id.next) {
1443                 if (sce->clip == clip)
1444                         sce->clip = NULL;
1445         }
1446
1447         for (ob = bmain->object.first; ob; ob = ob->id.next) {
1448                 bConstraint *con;
1449
1450                 for (con = ob->constraints.first; con; con = con->next) {
1451                         if (con->type == CONSTRAINT_TYPE_FOLLOWTRACK) {
1452                                 bFollowTrackConstraint *data = (bFollowTrackConstraint *) con->data;
1453
1454                                 if (data->clip == clip)
1455                                         data->clip = NULL;
1456                         }
1457                         else if (con->type == CONSTRAINT_TYPE_CAMERASOLVER) {
1458                                 bCameraSolverConstraint *data = (bCameraSolverConstraint *) con->data;
1459
1460                                 if (data->clip == clip)
1461                                         data->clip = NULL;
1462                         }
1463                         else if (con->type == CONSTRAINT_TYPE_OBJECTSOLVER) {
1464                                 bObjectSolverConstraint *data = (bObjectSolverConstraint *) con->data;
1465
1466                                 if (data->clip == clip)
1467                                         data->clip = NULL;
1468                         }
1469                 }
1470         }
1471
1472         FOREACH_NODETREE(bmain, ntree, id) {
1473                 BKE_node_tree_unlink_id((ID *)clip, ntree);
1474         } FOREACH_NODETREE_END
1475
1476         clip->id.us = 0;
1477 }
1478
1479 float BKE_movieclip_remap_scene_to_clip_frame(MovieClip *clip, float framenr)
1480 {
1481         return framenr - (float) clip->start_frame + 1.0f;
1482 }
1483
1484 float BKE_movieclip_remap_clip_to_scene_frame(MovieClip *clip, float framenr)
1485 {
1486         return framenr + (float) clip->start_frame - 1.0f;
1487 }
1488
1489 void BKE_movieclip_filename_for_frame(MovieClip *clip, MovieClipUser *user, char *name)
1490 {
1491         if (clip->source == MCLIP_SRC_SEQUENCE) {
1492                 int use_proxy;
1493
1494                 use_proxy = (clip->flag & MCLIP_USE_PROXY) && user->render_size != MCLIP_PROXY_RENDER_SIZE_FULL;
1495
1496                 if (use_proxy) {
1497                         int undistort = user->render_flag & MCLIP_PROXY_RENDER_UNDISTORT;
1498                         get_proxy_fname(clip, user->render_size, undistort, user->framenr, name);
1499                 }
1500                 else {
1501                         get_sequence_fname(clip, user->framenr, name);
1502                 }
1503         }
1504         else {
1505                 BLI_strncpy(name, clip->name, FILE_MAX);
1506                 BLI_path_abs(name, ID_BLEND_PATH(G.main, &clip->id));
1507         }
1508 }
1509
1510 ImBuf *BKE_movieclip_anim_ibuf_for_frame(MovieClip *clip, MovieClipUser *user)
1511 {
1512         ImBuf *ibuf = NULL;
1513
1514         if (clip->source == MCLIP_SRC_MOVIE) {
1515                 BLI_lock_thread(LOCK_MOVIECLIP);
1516                 ibuf = movieclip_load_movie_file(clip, user, user->framenr, clip->flag);
1517                 BLI_unlock_thread(LOCK_MOVIECLIP);
1518         }
1519
1520         return ibuf;
1521 }
1522
1523 bool BKE_movieclip_has_cached_frame(MovieClip *clip, MovieClipUser *user)
1524 {
1525         bool has_frame = false;
1526
1527         BLI_lock_thread(LOCK_MOVIECLIP);
1528         has_frame = has_imbuf_cache(clip, user, clip->flag);
1529         BLI_unlock_thread(LOCK_MOVIECLIP);
1530
1531         return has_frame;
1532 }
1533
1534 bool BKE_movieclip_put_frame_if_possible(MovieClip *clip, MovieClipUser *user, ImBuf *ibuf)
1535 {
1536         bool result;
1537
1538         BLI_lock_thread(LOCK_MOVIECLIP);
1539         result = put_imbuf_cache(clip, user, ibuf, clip->flag, false);
1540         BLI_unlock_thread(LOCK_MOVIECLIP);
1541
1542         return result;
1543 }