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