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