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