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