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