d63ac7df3ce283633bc3d3d900adfd9e68250f98
[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          */
1166         if (user->framenr == clip->lastframe) {
1167 #endif
1168         if (clip->lastsize[0] != 0 && clip->lastsize[1] != 0) {
1169                 *width = clip->lastsize[0];
1170                 *height = clip->lastsize[1];
1171         }
1172         else {
1173                 ImBuf *ibuf = BKE_movieclip_get_ibuf(clip, user);
1174
1175                 if (ibuf && ibuf->x && ibuf->y) {
1176                         real_ibuf_size(clip, user, ibuf, width, height);
1177                 }
1178                 else {
1179                         *width = clip->lastsize[0];
1180                         *height = clip->lastsize[1];
1181                 }
1182
1183                 if (ibuf)
1184                         IMB_freeImBuf(ibuf);
1185         }
1186 }
1187 void BKE_movieclip_get_size_fl(MovieClip *clip, MovieClipUser *user, float size[2])
1188 {
1189         int width, height;
1190         BKE_movieclip_get_size(clip, user, &width, &height);
1191
1192         size[0] = (float)width;
1193         size[1] = (float)height;
1194 }
1195
1196 int BKE_movieclip_get_duration(MovieClip *clip)
1197 {
1198         if (!clip->len) {
1199                 movieclip_calc_length(clip);
1200         }
1201
1202         return clip->len;
1203 }
1204
1205 float BKE_movieclip_get_fps(MovieClip *clip)
1206 {
1207         if (clip->source != MCLIP_SRC_MOVIE) {
1208                 return 0.0f;
1209         }
1210         movieclip_open_anim_file(clip);
1211         if (clip->anim == NULL) {
1212                 return 0.0f;
1213         }
1214         short frs_sec;
1215         float frs_sec_base;
1216         if (IMB_anim_get_fps(clip->anim, &frs_sec, &frs_sec_base, true)) {
1217                 return (float)frs_sec / frs_sec_base;
1218         }
1219         return 0.0f;
1220 }
1221
1222 void BKE_movieclip_get_aspect(MovieClip *clip, float *aspx, float *aspy)
1223 {
1224         *aspx = 1.0;
1225
1226         /* x is always 1 */
1227         *aspy = clip->aspy / clip->aspx / clip->tracking.camera.pixel_aspect;
1228 }
1229
1230 /* get segments of cached frames. useful for debugging cache policies */
1231 void BKE_movieclip_get_cache_segments(MovieClip *clip, MovieClipUser *user, int *r_totseg, int **r_points)
1232 {
1233         *r_totseg = 0;
1234         *r_points = NULL;
1235
1236         if (clip->cache) {
1237                 int proxy = rendersize_to_proxy(user, clip->flag);
1238
1239                 IMB_moviecache_get_cache_segments(clip->cache->moviecache, proxy, user->render_flag, r_totseg, r_points);
1240         }
1241 }
1242
1243 void BKE_movieclip_user_set_frame(MovieClipUser *iuser, int framenr)
1244 {
1245         /* TODO: clamp framenr here? */
1246
1247         iuser->framenr = framenr;
1248 }
1249
1250 static void free_buffers(MovieClip *clip)
1251 {
1252         if (clip->cache) {
1253                 IMB_moviecache_free(clip->cache->moviecache);
1254
1255                 if (clip->cache->postprocessed.ibuf)
1256                         IMB_freeImBuf(clip->cache->postprocessed.ibuf);
1257
1258                 if (clip->cache->stabilized.ibuf)
1259                         IMB_freeImBuf(clip->cache->stabilized.ibuf);
1260
1261                 MEM_freeN(clip->cache);
1262                 clip->cache = NULL;
1263         }
1264
1265         if (clip->anim) {
1266                 IMB_free_anim(clip->anim);
1267                 clip->anim = NULL;
1268         }
1269 }
1270
1271 void BKE_movieclip_clear_cache(MovieClip *clip)
1272 {
1273         free_buffers(clip);
1274 }
1275
1276 void BKE_movieclip_clear_proxy_cache(MovieClip *clip)
1277 {
1278         if (clip->cache && clip->cache->moviecache) {
1279                 IMB_moviecache_cleanup(clip->cache->moviecache,
1280                                        moviecache_check_free_proxy,
1281                                        NULL);
1282         }
1283 }
1284
1285 void BKE_movieclip_reload(Main *bmain, MovieClip *clip)
1286 {
1287         /* clear cache */
1288         free_buffers(clip);
1289
1290         /* update clip source */
1291         detect_clip_source(bmain, clip);
1292
1293         clip->lastsize[0] = clip->lastsize[1] = 0;
1294         movieclip_load_get_size(clip);
1295
1296         movieclip_calc_length(clip);
1297
1298         /* same as for image update -- don't use notifiers because they are not 100% sure to succeeded
1299          * (node trees which are not currently visible wouldn't be refreshed)
1300          */
1301         {
1302                 Scene *scene;
1303                 for (scene = bmain->scene.first; scene; scene = scene->id.next) {
1304                         if (scene->nodetree) {
1305                                 nodeUpdateID(scene->nodetree, &clip->id);
1306                         }
1307                 }
1308         }
1309 }
1310
1311 void BKE_movieclip_update_scopes(MovieClip *clip, MovieClipUser *user, MovieClipScopes *scopes)
1312 {
1313         if (scopes->ok)
1314                 return;
1315
1316         if (scopes->track_preview) {
1317                 IMB_freeImBuf(scopes->track_preview);
1318                 scopes->track_preview = NULL;
1319         }
1320
1321         if (scopes->track_search) {
1322                 IMB_freeImBuf(scopes->track_search);
1323                 scopes->track_search = NULL;
1324         }
1325
1326         scopes->marker = NULL;
1327         scopes->track = NULL;
1328         scopes->track_locked = true;
1329
1330         if (clip) {
1331                 MovieTrackingTrack *act_track = BKE_tracking_track_get_active(&clip->tracking);
1332
1333                 if (act_track) {
1334                         MovieTrackingTrack *track = act_track;
1335                         int framenr = BKE_movieclip_remap_scene_to_clip_frame(clip, user->framenr);
1336                         MovieTrackingMarker *marker = BKE_tracking_marker_get(track, framenr);
1337
1338                         scopes->marker = marker;
1339                         scopes->track = track;
1340
1341                         if (marker->flag & MARKER_DISABLED) {
1342                                 scopes->track_disabled = true;
1343                         }
1344                         else {
1345                                 ImBuf *ibuf = BKE_movieclip_get_ibuf(clip, user);
1346
1347                                 scopes->track_disabled = false;
1348
1349                                 if (ibuf && (ibuf->rect || ibuf->rect_float)) {
1350                                         MovieTrackingMarker undist_marker = *marker;
1351
1352                                         if (user->render_flag & MCLIP_PROXY_RENDER_UNDISTORT) {
1353                                                 int width, height;
1354                                                 float aspy = 1.0f / clip->tracking.camera.pixel_aspect;
1355
1356                                                 BKE_movieclip_get_size(clip, user, &width, &height);
1357
1358                                                 undist_marker.pos[0] *= width;
1359                                                 undist_marker.pos[1] *= height * aspy;
1360
1361                                                 BKE_tracking_undistort_v2(&clip->tracking, undist_marker.pos, undist_marker.pos);
1362
1363                                                 undist_marker.pos[0] /= width;
1364                                                 undist_marker.pos[1] /= height * aspy;
1365                                         }
1366
1367                                         scopes->track_search = BKE_tracking_get_search_imbuf(ibuf, track, &undist_marker, true, true);
1368
1369                                         scopes->undist_marker = undist_marker;
1370
1371                                         scopes->frame_width = ibuf->x;
1372                                         scopes->frame_height = ibuf->y;
1373
1374                                         scopes->use_track_mask = (track->flag & TRACK_PREVIEW_ALPHA) != 0;
1375                                 }
1376
1377                                 IMB_freeImBuf(ibuf);
1378                         }
1379
1380                         if ((track->flag & TRACK_LOCKED) == 0) {
1381                                 float pat_min[2], pat_max[2];
1382
1383                                 scopes->track_locked = false;
1384
1385                                 /* XXX: would work fine with non-transformed patterns, but would likely fail
1386                                  *      with transformed patterns, but that would be easier to debug when
1387                                  *      we'll have real pattern sampling (at least to test) */
1388                                 BKE_tracking_marker_pattern_minmax(marker, pat_min, pat_max);
1389
1390                                 scopes->slide_scale[0] = pat_max[0] - pat_min[0];
1391                                 scopes->slide_scale[1] = pat_max[1] - pat_min[1];
1392                         }
1393                 }
1394         }
1395
1396         scopes->framenr = user->framenr;
1397         scopes->ok = true;
1398 }
1399
1400 static void movieclip_build_proxy_ibuf(MovieClip *clip, ImBuf *ibuf, int cfra, int proxy_render_size, bool undistorted, bool threaded)
1401 {
1402         char name[FILE_MAX];
1403         int quality, rectx, recty;
1404         int size = rendersize_to_number(proxy_render_size);
1405         ImBuf *scaleibuf;
1406
1407         get_proxy_fname(clip, proxy_render_size, undistorted, cfra, name);
1408
1409         rectx = ibuf->x * size / 100.0f;
1410         recty = ibuf->y * size / 100.0f;
1411
1412         scaleibuf = IMB_dupImBuf(ibuf);
1413
1414         if (threaded)
1415                 IMB_scaleImBuf_threaded(scaleibuf, (short)rectx, (short)recty);
1416         else
1417                 IMB_scaleImBuf(scaleibuf, (short)rectx, (short)recty);
1418
1419         quality = clip->proxy.quality;
1420         scaleibuf->ftype = IMB_FTYPE_JPG;
1421         scaleibuf->foptions.quality = quality;
1422         /* unsupported feature only confuses other s/w */
1423         if (scaleibuf->planes == 32)
1424                 scaleibuf->planes = 24;
1425
1426         /* TODO: currently the most weak part of multithreaded proxies,
1427          *       could be solved in a way that thread only prepares memory
1428          *       buffer and write to disk happens separately
1429          */
1430         BLI_thread_lock(LOCK_MOVIECLIP);
1431
1432         BLI_make_existing_file(name);
1433         if (IMB_saveiff(scaleibuf, name, IB_rect) == 0)
1434                 perror(name);
1435
1436         BLI_thread_unlock(LOCK_MOVIECLIP);
1437
1438         IMB_freeImBuf(scaleibuf);
1439 }
1440
1441 /* note: currently used by proxy job for movies, threading happens within single frame
1442  * (meaning scaling shall be threaded)
1443  */
1444 void BKE_movieclip_build_proxy_frame(MovieClip *clip, int clip_flag, struct MovieDistortion *distortion,
1445                                      int cfra, int *build_sizes, int build_count, bool undistorted)
1446 {
1447         ImBuf *ibuf;
1448         MovieClipUser user;
1449
1450         if (!build_count)
1451                 return;
1452
1453         user.framenr = cfra;
1454         user.render_flag = 0;
1455         user.render_size = MCLIP_PROXY_RENDER_SIZE_FULL;
1456
1457         ibuf = BKE_movieclip_get_ibuf_flag(clip, &user, clip_flag, MOVIECLIP_CACHE_SKIP);
1458
1459         if (ibuf) {
1460                 ImBuf *tmpibuf = ibuf;
1461                 int i;
1462
1463                 if (undistorted)
1464                         tmpibuf = get_undistorted_ibuf(clip, distortion, ibuf);
1465
1466                 for (i = 0; i < build_count; i++)
1467                         movieclip_build_proxy_ibuf(clip, tmpibuf, cfra, build_sizes[i], undistorted, true);
1468
1469                 IMB_freeImBuf(ibuf);
1470
1471                 if (tmpibuf != ibuf)
1472                         IMB_freeImBuf(tmpibuf);
1473         }
1474 }
1475
1476 /* note: currently used by proxy job for sequences, threading happens within sequence
1477  * (different threads handles different frames, no threading within frame is needed)
1478  */
1479 void BKE_movieclip_build_proxy_frame_for_ibuf(MovieClip *clip, ImBuf *ibuf, struct MovieDistortion *distortion,
1480                                               int cfra, int *build_sizes, int build_count, bool undistorted)
1481 {
1482         if (!build_count)
1483                 return;
1484
1485         if (ibuf) {
1486                 ImBuf *tmpibuf = ibuf;
1487                 int i;
1488
1489                 if (undistorted)
1490                         tmpibuf = get_undistorted_ibuf(clip, distortion, ibuf);
1491
1492                 for (i = 0; i < build_count; i++)
1493                         movieclip_build_proxy_ibuf(clip, tmpibuf, cfra, build_sizes[i], undistorted, false);
1494
1495                 if (tmpibuf != ibuf)
1496                         IMB_freeImBuf(tmpibuf);
1497         }
1498 }
1499
1500 /** Free (or release) any data used by this movie clip (does not free the clip itself). */
1501 void BKE_movieclip_free(MovieClip *clip)
1502 {
1503         /* Also frees animdata. */
1504         free_buffers(clip);
1505
1506         BKE_tracking_free(&clip->tracking);
1507         BKE_animdata_free((ID *) clip, false);
1508 }
1509
1510 /**
1511  * Only copy internal data of MovieClip ID from source to already allocated/initialized destination.
1512  * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs.
1513  *
1514  * WARNING! This function will not handle ID user count!
1515  *
1516  * \param flag  Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
1517  */
1518 void BKE_movieclip_copy_data(Main *UNUSED(bmain), MovieClip *clip_dst, const MovieClip *clip_src, const int flag)
1519 {
1520         /* We never handle usercount here for own data. */
1521         const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT;
1522
1523         clip_dst->anim = NULL;
1524         clip_dst->cache = NULL;
1525
1526         BKE_tracking_copy(&clip_dst->tracking, &clip_src->tracking, flag_subdata);
1527         clip_dst->tracking_context = NULL;
1528
1529         BKE_color_managed_colorspace_settings_copy(&clip_dst->colorspace_settings, &clip_src->colorspace_settings);
1530 }
1531
1532 MovieClip *BKE_movieclip_copy(Main *bmain, const MovieClip *clip)
1533 {
1534         MovieClip *clip_copy;
1535         BKE_id_copy_ex(bmain, &clip->id, (ID **)&clip_copy, 0, false);
1536         return clip_copy;
1537 }
1538
1539 void BKE_movieclip_make_local(Main *bmain, MovieClip *clip, const bool lib_local)
1540 {
1541         BKE_id_make_local_generic(bmain, &clip->id, true, lib_local);
1542 }
1543
1544 float BKE_movieclip_remap_scene_to_clip_frame(const MovieClip *clip, float framenr)
1545 {
1546         return framenr - (float) clip->start_frame + 1.0f;
1547 }
1548
1549 float BKE_movieclip_remap_clip_to_scene_frame(const MovieClip *clip, float framenr)
1550 {
1551         return framenr + (float) clip->start_frame - 1.0f;
1552 }
1553
1554 void BKE_movieclip_filename_for_frame(MovieClip *clip, MovieClipUser *user, char *name)
1555 {
1556         if (clip->source == MCLIP_SRC_SEQUENCE) {
1557                 int use_proxy;
1558
1559                 use_proxy = (clip->flag & MCLIP_USE_PROXY) && user->render_size != MCLIP_PROXY_RENDER_SIZE_FULL;
1560
1561                 if (use_proxy) {
1562                         int undistort = user->render_flag & MCLIP_PROXY_RENDER_UNDISTORT;
1563                         get_proxy_fname(clip, user->render_size, undistort, user->framenr, name);
1564                 }
1565                 else {
1566                         get_sequence_fname(clip, user->framenr, name);
1567                 }
1568         }
1569         else {
1570                 BLI_strncpy(name, clip->name, FILE_MAX);
1571                 BLI_path_abs(name, ID_BLEND_PATH_FROM_GLOBAL(&clip->id));
1572         }
1573 }
1574
1575 ImBuf *BKE_movieclip_anim_ibuf_for_frame(MovieClip *clip, MovieClipUser *user)
1576 {
1577         ImBuf *ibuf = NULL;
1578
1579         if (clip->source == MCLIP_SRC_MOVIE) {
1580                 BLI_thread_lock(LOCK_MOVIECLIP);
1581                 ibuf = movieclip_load_movie_file(clip, user, user->framenr, clip->flag);
1582                 BLI_thread_unlock(LOCK_MOVIECLIP);
1583         }
1584
1585         return ibuf;
1586 }
1587
1588 bool BKE_movieclip_has_cached_frame(MovieClip *clip, MovieClipUser *user)
1589 {
1590         bool has_frame = false;
1591
1592         BLI_thread_lock(LOCK_MOVIECLIP);
1593         has_frame = has_imbuf_cache(clip, user, clip->flag);
1594         BLI_thread_unlock(LOCK_MOVIECLIP);
1595
1596         return has_frame;
1597 }
1598
1599 bool BKE_movieclip_put_frame_if_possible(MovieClip *clip,
1600                                          MovieClipUser *user,
1601                                          ImBuf *ibuf)
1602 {
1603         bool result;
1604
1605         BLI_thread_lock(LOCK_MOVIECLIP);
1606         result = put_imbuf_cache(clip, user, ibuf, clip->flag, false);
1607         BLI_thread_unlock(LOCK_MOVIECLIP);
1608
1609         return result;
1610 }
1611
1612 static void movieclip_selection_synchronize(MovieClip *clip_dst, const MovieClip *clip_src)
1613 {
1614         BLI_assert(clip_dst != clip_src);
1615         MovieTracking *tracking_dst = &clip_dst->tracking, tracking_src = clip_src->tracking;
1616         /* Syncs the active object, track and plane track. */
1617         tracking_dst->objectnr = tracking_src.objectnr;
1618         const int active_track_index = BLI_findindex(&tracking_src.tracks, tracking_src.act_track);
1619         const int active_plane_track_index = BLI_findindex(&tracking_src.plane_tracks, tracking_src.act_plane_track);
1620         tracking_dst->act_track = BLI_findlink(&tracking_dst->tracks, active_track_index);
1621         tracking_dst->act_plane_track = BLI_findlink(&tracking_dst->plane_tracks, active_plane_track_index);
1622
1623         /* Syncs the tracking selection flag. */
1624         MovieTrackingObject *tracking_object_dst, *tracking_object_src;
1625         tracking_object_src = tracking_src.objects.first;
1626
1627         for (tracking_object_dst = tracking_dst->objects.first;
1628              tracking_object_dst != NULL;
1629              tracking_object_dst = tracking_object_dst->next,
1630              tracking_object_src = tracking_object_src->next)
1631         {
1632                 ListBase *tracksbase_dst, *tracksbase_src;
1633                 tracksbase_dst = BKE_tracking_object_get_tracks(tracking_dst, tracking_object_dst);
1634                 tracksbase_src = BKE_tracking_object_get_tracks(&tracking_src, tracking_object_src);
1635
1636                 MovieTrackingTrack *track_dst, *track_src;
1637                 track_src = tracksbase_src->first;
1638                 for (track_dst = tracksbase_dst->first;
1639                      track_dst != NULL;
1640                      track_dst = track_dst->next, track_src = track_src->next)
1641                 {
1642                         track_dst->flag = track_src->flag;
1643                         track_dst->pat_flag = track_src->pat_flag;
1644                         track_dst->search_flag = track_src->search_flag;
1645                 }
1646         }
1647 }
1648
1649 void BKE_movieclip_eval_update(struct Depsgraph *depsgraph, MovieClip *clip)
1650 {
1651         DEG_debug_print_eval(depsgraph, __func__, clip->id.name, clip);
1652         BKE_tracking_dopesheet_tag_update(&clip->tracking);
1653 }
1654
1655 void BKE_movieclip_eval_selection_update(struct Depsgraph *depsgraph, MovieClip *clip)
1656 {
1657         DEG_debug_print_eval(depsgraph, __func__, clip->id.name, clip);
1658         movieclip_selection_synchronize(clip, (MovieClip *)clip->id.orig_id);
1659 }