Fix a particle memory allocation mismatch using MEM_allocN for alloc and BLI_cellallo...
[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_constraint.h"
68 #include "BKE_library.h"
69 #include "BKE_global.h"
70 #include "BKE_main.h"
71 #include "BKE_utildefines.h"
72 #include "BKE_movieclip.h"
73 #include "BKE_image.h"  /* openanim */
74 #include "BKE_tracking.h"
75
76 #include "IMB_imbuf_types.h"
77 #include "IMB_imbuf.h"
78 #include "IMB_moviecache.h"
79
80 /*********************** movieclip buffer loaders *************************/
81
82 static int sequence_guess_offset(const char *full_name, int head_len, int numlen)
83 {
84         char num[FILE_MAX]= {0};
85
86         BLI_strncpy(num, full_name+head_len, numlen+1);
87
88         return atoi(num);
89 }
90
91 static int rendersize_to_proxy(MovieClipUser *user, int flag)
92 {
93         if((flag&MCLIP_USE_PROXY)==0)
94                 return IMB_PROXY_NONE;
95
96         switch(user->render_size) {
97                 case MCLIP_PROXY_RENDER_SIZE_25:
98                         return IMB_PROXY_25;
99
100                 case MCLIP_PROXY_RENDER_SIZE_50:
101                         return IMB_PROXY_50;
102
103                 case MCLIP_PROXY_RENDER_SIZE_75:
104                         return IMB_PROXY_75;
105
106                 case MCLIP_PROXY_RENDER_SIZE_100:
107                         return IMB_PROXY_100;
108
109                 case MCLIP_PROXY_RENDER_SIZE_FULL:
110                         return IMB_PROXY_NONE;
111         }
112
113         return IMB_PROXY_NONE;
114 }
115
116 static int rendersize_to_number(int render_size)
117 {
118         switch(render_size) {
119                 case MCLIP_PROXY_RENDER_SIZE_25:
120                         return 25;
121
122                 case MCLIP_PROXY_RENDER_SIZE_50:
123                         return 50;
124
125                 case MCLIP_PROXY_RENDER_SIZE_75:
126                         return 75;
127
128                 case MCLIP_PROXY_RENDER_SIZE_100:
129                         return 100;
130
131                 case MCLIP_PROXY_RENDER_SIZE_FULL:
132                         return 100;
133         }
134
135         return 100;
136 }
137
138 static int get_timecode(MovieClip *clip, int flag)
139 {
140         if((flag&MCLIP_USE_PROXY)==0)
141                 return IMB_TC_NONE;
142
143         return clip->proxy.tc;
144 }
145
146 static void get_sequence_fname(MovieClip *clip, int framenr, char *name)
147 {
148         unsigned short numlen;
149         char head[FILE_MAX], tail[FILE_MAX];
150         int offset;
151
152         BLI_strncpy(name, clip->name, sizeof(clip->name));
153         BLI_stringdec(name, head, tail, &numlen);
154
155         /* movieclips always points to first image from sequence,
156            autoguess offset for now. could be something smarter in the future */
157         offset= sequence_guess_offset(clip->name, strlen(head), numlen);
158
159         if (numlen) BLI_stringenc(name, head, tail, numlen, offset+framenr-1);
160         else        BLI_strncpy(name, clip->name, sizeof(clip->name));
161
162         BLI_path_abs(name, ID_BLEND_PATH(G.main, &clip->id));
163 }
164
165 /* supposed to work with sequences only */
166 static void get_proxy_fname(MovieClip *clip, int proxy_render_size, int undistorted, int framenr, char *name)
167 {
168         int size= rendersize_to_number(proxy_render_size);
169         char dir[FILE_MAX], clipdir[FILE_MAX], clipfile[FILE_MAX];
170
171         BLI_split_dirfile(clip->name, clipdir, clipfile, FILE_MAX, FILE_MAX);
172
173         if(clip->flag&MCLIP_USE_PROXY_CUSTOM_DIR) {
174                 BLI_strncpy(dir, clip->proxy.dir, sizeof(dir));
175         } else {
176                 BLI_snprintf(dir, FILE_MAX, "%s/BL_proxy", clipdir);
177         }
178
179         if(undistorted)
180                 BLI_snprintf(name, FILE_MAX, "%s/%s/proxy_%d_undistorted/%08d", dir, clipfile, size, framenr);
181         else
182                 BLI_snprintf(name, FILE_MAX, "%s/%s/proxy_%d/%08d", dir, clipfile, size, framenr);
183
184         BLI_path_abs(name, G.main->name);
185         BLI_path_frame(name, 1, 0);
186
187         strcat(name, ".jpg");
188 }
189
190 static ImBuf *movieclip_load_sequence_file(MovieClip *clip, MovieClipUser *user, int framenr, int flag)
191 {
192         struct ImBuf *ibuf;
193         char name[FILE_MAX];
194         int loadflag /*, size */ /* UNUSED */, undistort;
195
196         /* size= rendersize_to_number(user->render_size); */
197
198         undistort= user->render_flag&MCLIP_PROXY_RENDER_UNDISTORT;
199
200         if((flag&MCLIP_USE_PROXY) && user->render_size != MCLIP_PROXY_RENDER_SIZE_FULL)
201                 get_proxy_fname(clip, user->render_size, undistort, framenr, name);
202         else
203                 get_sequence_fname(clip, framenr, name);
204
205         loadflag= IB_rect|IB_multilayer;
206
207         /* read ibuf */
208         ibuf= IMB_loadiffname(name, loadflag);
209
210         return ibuf;
211 }
212
213 static ImBuf *movieclip_load_movie_file(MovieClip *clip, MovieClipUser *user, int framenr, int flag)
214 {
215         ImBuf *ibuf= NULL;
216         int tc= get_timecode(clip, flag);
217         int proxy= rendersize_to_proxy(user, flag);
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         if(clip->anim) {
238                 int dur;
239                 int fra;
240
241                 dur= IMB_anim_get_duration(clip->anim, tc);
242                 fra= framenr-1;
243
244                 if(fra<0)
245                         fra= 0;
246
247                 if(fra>(dur-1))
248                         fra= dur-1;
249
250                 ibuf= IMB_anim_absolute(clip->anim, fra, tc, proxy);
251         }
252
253         return ibuf;
254 }
255
256 /*********************** image buffer cache *************************/
257
258 typedef struct MovieClipCache {
259         /* regular movie cache */
260         struct MovieCache *moviecache;
261
262         /* cache for stable shot */
263         int stable_framenr;
264         float stable_loc[2], stable_scale, stable_angle;
265         ImBuf *stableibuf;
266         int proxy;
267         short render_flag;
268
269         /* cache for undistorted shot */
270         int undist_framenr;
271         float principal[2];
272         float k1, k2, k3;
273         ImBuf *undistibuf;
274 } MovieClipCache;
275
276 typedef struct MovieClipImBufCacheKey {
277         int framenr;
278         int proxy;
279         short render_flag;
280 } MovieClipImBufCacheKey;
281
282 static void moviecache_keydata(void *userkey, int *framenr, int *proxy, int *render_flags)
283 {
284         MovieClipImBufCacheKey *key= (MovieClipImBufCacheKey*)userkey;
285
286         *framenr= key->framenr;
287         *proxy= key->proxy;
288         *render_flags= key->render_flag;
289 }
290
291 static unsigned int moviecache_hashhash(const void *keyv)
292 {
293         MovieClipImBufCacheKey *key= (MovieClipImBufCacheKey*)keyv;
294         int rval= key->framenr;
295
296         return rval;
297 }
298
299 static int moviecache_hashcmp(const void *av, const void *bv)
300 {
301         const MovieClipImBufCacheKey *a= (MovieClipImBufCacheKey*)av;
302         const MovieClipImBufCacheKey *b= (MovieClipImBufCacheKey*)bv;
303
304         if(a->framenr<b->framenr) return -1;
305         else if(a->framenr>b->framenr) return 1;
306
307         if(a->proxy<b->proxy) return -1;
308         else if(a->proxy>b->proxy) return 1;
309
310         if(a->render_flag<b->render_flag) return -1;
311         else if(a->render_flag>b->render_flag) return 1;
312
313         return 0;
314 }
315
316 static ImBuf *get_imbuf_cache(MovieClip *clip, MovieClipUser *user, int flag)
317 {
318         if(clip->cache) {
319                 MovieClipImBufCacheKey key;
320
321                 key.framenr= user?user->framenr:clip->lastframe;
322
323                 if(flag&MCLIP_USE_PROXY) {
324                         key.proxy= rendersize_to_proxy(user, flag);
325                         key.render_flag= user->render_flag;
326                 }
327                 else {
328                         key.proxy= IMB_PROXY_NONE;
329                         key.render_flag= 0;
330                 }
331
332                 return IMB_moviecache_get(clip->cache->moviecache, &key);
333         }
334
335         return NULL;
336 }
337
338 static void put_imbuf_cache(MovieClip *clip, MovieClipUser *user, ImBuf *ibuf, int flag)
339 {
340         MovieClipImBufCacheKey key;
341
342         if(!clip->cache) {
343                 clip->cache= MEM_callocN(sizeof(MovieClipCache), "movieClipCache");
344
345                 clip->cache->moviecache= IMB_moviecache_create(sizeof(MovieClipImBufCacheKey), moviecache_hashhash,
346                                 moviecache_hashcmp, moviecache_keydata);
347         }
348
349         key.framenr= user?user->framenr:clip->lastframe;
350
351         if(flag&MCLIP_USE_PROXY) {
352                 key.proxy= rendersize_to_proxy(user, flag);
353                 key.render_flag= user->render_flag;
354         }
355         else {
356                 key.proxy= IMB_PROXY_NONE;
357                 key.render_flag= 0;
358         }
359
360         IMB_moviecache_put(clip->cache->moviecache, &key, ibuf);
361 }
362
363 /*********************** common functions *************************/
364
365 /* only image block itself */
366 static MovieClip *movieclip_alloc(const char *name)
367 {
368         MovieClip *clip;
369
370         clip= alloc_libblock(&G.main->movieclip, ID_MC, name);
371
372         clip->aspx= clip->aspy= 1.0f;
373
374         clip->tracking.camera.sensor_width= 35.0f;
375         clip->tracking.camera.pixel_aspect= 1.0f;
376         clip->tracking.camera.units= CAMERA_UNITS_MM;
377
378         clip->tracking.settings.frames_limit= 0;
379         clip->tracking.settings.keyframe1= 1;
380         clip->tracking.settings.keyframe2= 30;
381         clip->tracking.settings.dist= 1;
382
383         clip->tracking.stabilization.scaleinf= 1.0f;
384         clip->tracking.stabilization.locinf= 1.0f;
385         clip->tracking.stabilization.rotinf= 1.0f;
386         clip->tracking.stabilization.maxscale= 2.0f;
387
388         clip->proxy.build_size_flag= IMB_PROXY_25;
389         clip->proxy.build_tc_flag= IMB_TC_RECORD_RUN|IMB_TC_FREE_RUN|IMB_TC_INTERPOLATED_REC_DATE_FREE_RUN;
390         clip->proxy.quality= 90;
391
392         return clip;
393 }
394
395 /* checks if image was already loaded, then returns same image
396    otherwise creates new.
397    does not load ibuf itself
398    pass on optional frame for #name images */
399 MovieClip *BKE_add_movieclip_file(const char *name)
400 {
401         MovieClip *clip;
402         MovieClipUser user;
403         int file, len, width, height;
404         const char *libname;
405         char str[FILE_MAX], strtest[FILE_MAX];
406
407         BLI_strncpy(str, name, sizeof(str));
408         BLI_path_abs(str, G.main->name);
409
410         /* exists? */
411         file= open(str, O_BINARY|O_RDONLY);
412         if(file== -1) return NULL;
413         close(file);
414
415         /* ** first search an identical clip ** */
416         for(clip= G.main->movieclip.first; clip; clip= clip->id.next) {
417                 BLI_strncpy(strtest, clip->name, sizeof(clip->name));
418                 BLI_path_abs(strtest, G.main->name);
419
420                 if(strcmp(strtest, str)==0) {
421                         BLI_strncpy(clip->name, name, sizeof(clip->name));  /* for stringcode */
422                         clip->id.us++;  /* officially should not, it doesn't link here! */
423
424                         return clip;
425                 }
426         }
427
428         /* ** add new movieclip ** */
429
430         /* create a short library name */
431         len= strlen(name);
432
433         while (len > 0 && name[len - 1] != '/' && name[len - 1] != '\\') len--;
434         libname= name+len;
435
436         clip= movieclip_alloc(libname);
437         BLI_strncpy(clip->name, name, sizeof(clip->name));
438
439         if(BLI_testextensie_array(name, imb_ext_movie)) clip->source= MCLIP_SRC_MOVIE;
440         else clip->source= MCLIP_SRC_SEQUENCE;
441
442         user.framenr= 1;
443         BKE_movieclip_get_size(clip, &user, &width, &height);
444         if(width && height) {
445                 clip->tracking.camera.principal[0]= ((float)width)/2;
446                 clip->tracking.camera.principal[1]= ((float)height)/2;
447
448                 clip->tracking.camera.focal= 24.0f*width/clip->tracking.camera.sensor_width;
449         }
450
451         return clip;
452 }
453
454 static void real_ibuf_size(MovieClip *clip, MovieClipUser *user, ImBuf *ibuf, int *width, int *height)
455 {
456         *width= ibuf->x;
457         *height= ibuf->y;
458
459         if(clip->flag&MCLIP_USE_PROXY) {
460                 switch(user->render_size) {
461                         case MCLIP_PROXY_RENDER_SIZE_25:
462                                 (*width)*= 4;
463                                 (*height)*= 4;
464                                 break;
465
466                         case MCLIP_PROXY_RENDER_SIZE_50:
467                                 (*width)*= 2.0f;
468                                 (*height)*= 2.0f;
469                                 break;
470
471                         case MCLIP_PROXY_RENDER_SIZE_75:
472                                 *width= ((float)*width)*4.0f/3.0f;
473                                 *height= ((float)*height)*4.0f/3.0f;
474                                 break;
475                 }
476         }
477 }
478
479 static int need_undistorted_cache(MovieClipUser *user, int flag)
480 {
481         if (!user)
482                 return 0;
483
484         /* only full undistorted render can be used as on-fly undistorting image */
485         if(flag&MCLIP_USE_PROXY) {
486                 if(user->render_size != MCLIP_PROXY_RENDER_SIZE_FULL || (user->render_flag&MCLIP_PROXY_RENDER_UNDISTORT)==0)
487                         return 0;
488         }
489         else return 0;
490
491         return 1;
492 }
493
494 static ImBuf *get_undistorted_cache(MovieClip *clip, MovieClipUser *user)
495 {
496         MovieClipCache *cache= clip->cache;
497         MovieTrackingCamera *camera= &clip->tracking.camera;
498         int framenr= user?user->framenr:clip->lastframe;
499
500         /* no cache or no cached undistorted image */
501         if(!clip->cache || !clip->cache->undistibuf)
502                 return NULL;
503
504         /* undistortion happened for other frame */
505         if(cache->undist_framenr!=framenr)
506                 return NULL;
507
508         /* check for distortion model changes */
509         if(!equals_v2v2(camera->principal, cache->principal))
510                 return NULL;
511
512         if(!equals_v3v3(&camera->k1, &cache->k1))
513                 return NULL;
514
515         IMB_refImBuf(cache->undistibuf);
516
517         return cache->undistibuf;
518 }
519
520 static ImBuf *get_undistorted_ibuf(MovieClip *clip, struct MovieDistortion *distortion, ImBuf *ibuf)
521 {
522         ImBuf *undistibuf;
523
524         /* XXX: because of #27997 do not use float buffers to undistort,
525                 otherwise, undistorted proxy can be darker than it should */
526         imb_freerectfloatImBuf(ibuf);
527
528         if(distortion)
529                 undistibuf= BKE_tracking_distortion_exec(distortion, &clip->tracking, ibuf, ibuf->x, ibuf->y, 0.0f, 1);
530         else
531                 undistibuf= BKE_tracking_undistort(&clip->tracking, ibuf, ibuf->x, ibuf->y, 0.0f);
532
533         if(undistibuf->userflags|= IB_RECT_INVALID) {
534                 ibuf->userflags&= ~IB_RECT_INVALID;
535                 IMB_rect_from_float(undistibuf);
536         }
537
538         IMB_scaleImBuf(undistibuf, ibuf->x, ibuf->y);
539
540         return undistibuf;
541 }
542
543 static ImBuf *put_undistorted_cache(MovieClip *clip, MovieClipUser *user, ImBuf *ibuf)
544 {
545         MovieClipCache *cache= clip->cache;
546         MovieTrackingCamera *camera= &clip->tracking.camera;
547
548         copy_v2_v2(cache->principal, camera->principal);
549         copy_v3_v3(&cache->k1, &camera->k1);
550         cache->undist_framenr= user?user->framenr:clip->lastframe;
551
552         if(cache->undistibuf)
553                 IMB_freeImBuf(cache->undistibuf);
554
555         cache->undistibuf= get_undistorted_ibuf(clip, NULL, ibuf);
556
557         if(cache->stableibuf) {
558                 /* force stable buffer be re-calculated */
559                 IMB_freeImBuf(cache->stableibuf);
560                 cache->stableibuf= NULL;
561         }
562
563         IMB_refImBuf(cache->undistibuf);
564
565         return cache->undistibuf;
566 }
567
568 ImBuf *BKE_movieclip_get_ibuf(MovieClip *clip, MovieClipUser *user)
569 {
570         ImBuf *ibuf= NULL;
571         int framenr= user?user->framenr:clip->lastframe;
572         int cache_undistorted= 0;
573
574         /* cache isn't threadsafe itself and also loading of movies
575            can't happen from concurent threads that's why we use lock here */
576         BLI_lock_thread(LOCK_MOVIECLIP);
577
578         /* try to obtain cached undistorted image first */
579         if(need_undistorted_cache(user, clip->flag)) {
580                 ibuf= get_undistorted_cache(clip, user);
581                 if(!ibuf)
582                         cache_undistorted= 1;
583         }
584
585         if(!ibuf)
586                 ibuf= get_imbuf_cache(clip, user, clip->flag);
587
588         if(!ibuf) {
589                 int use_sequence= 1;
590
591                 /* undistorted proxies for movies should be read as image sequence */
592                 use_sequence&= user->render_flag&MCLIP_PROXY_RENDER_UNDISTORT;
593                 use_sequence&= user->render_size!=MCLIP_PROXY_RENDER_SIZE_FULL;
594
595                 if(clip->source==MCLIP_SRC_SEQUENCE || use_sequence)
596                         ibuf= movieclip_load_sequence_file(clip, user, framenr, clip->flag);
597                 else {
598                         ibuf= movieclip_load_movie_file(clip, user, framenr, clip->flag);
599                 }
600
601                 if(ibuf)
602                         put_imbuf_cache(clip, user, ibuf, clip->flag);
603         }
604
605         if(ibuf) {
606                 clip->lastframe= framenr;
607                 real_ibuf_size(clip, user, ibuf, &clip->lastsize[0], &clip->lastsize[1]);
608
609                 /* put undistorted frame to cache */
610                 if(cache_undistorted) {
611                         ImBuf *tmpibuf= ibuf;
612                         ibuf= put_undistorted_cache(clip, user, tmpibuf);
613                         IMB_freeImBuf(tmpibuf);
614                 }
615         }
616
617         BLI_unlock_thread(LOCK_MOVIECLIP);
618
619         return ibuf;
620 }
621
622 ImBuf *BKE_movieclip_get_ibuf_flag(MovieClip *clip, MovieClipUser *user, int flag)
623 {
624         ImBuf *ibuf= NULL;
625         int framenr= user?user->framenr:clip->lastframe;
626         int cache_undistorted= 0;
627
628         /* cache isn't threadsafe itself and also loading of movies
629            can't happen from concurent threads that's why we use lock here */
630         BLI_lock_thread(LOCK_MOVIECLIP);
631
632         /* try to obtain cached undistorted image first */
633         if(need_undistorted_cache(user, flag)) {
634                 ibuf= get_undistorted_cache(clip, user);
635                 if(!ibuf)
636                         cache_undistorted= 1;
637         }
638
639         ibuf= get_imbuf_cache(clip, user, flag);
640
641         if(!ibuf) {
642                 if(clip->source==MCLIP_SRC_SEQUENCE) {
643                         ibuf= movieclip_load_sequence_file(clip, user, framenr, flag);
644                 } else {
645                         ibuf= movieclip_load_movie_file(clip, user, framenr, flag);
646                 }
647
648                 if(ibuf) {
649                         int bits= MCLIP_USE_PROXY|MCLIP_USE_PROXY_CUSTOM_DIR;
650
651                         if((flag&bits)==(clip->flag&bits))
652                                 put_imbuf_cache(clip, user, ibuf, clip->flag);
653                 }
654         }
655
656         /* put undistorted frame to cache */
657         if(ibuf && cache_undistorted) {
658                 ImBuf *tmpibuf= ibuf;
659                 ibuf= put_undistorted_cache(clip, user, tmpibuf);
660                 IMB_freeImBuf(tmpibuf);
661         }
662
663         BLI_unlock_thread(LOCK_MOVIECLIP);
664
665         return ibuf;
666 }
667
668 ImBuf *BKE_movieclip_get_stable_ibuf(MovieClip *clip, MovieClipUser *user, float loc[2], float *scale, float *angle)
669 {
670         ImBuf *ibuf, *stableibuf= NULL;
671         int framenr= user?user->framenr:clip->lastframe;
672
673         ibuf= BKE_movieclip_get_ibuf(clip, user);
674
675         if(!ibuf)
676                 return NULL;
677
678         if(clip->tracking.stabilization.flag&TRACKING_2D_STABILIZATION) {
679                 float tloc[2], tscale, tangle;
680                 short proxy= IMB_PROXY_NONE;
681                 int render_flag= 0;
682
683                 if(clip->flag&MCLIP_USE_PROXY) {
684                         proxy= rendersize_to_proxy(user, clip->flag);
685                         render_flag= user->render_flag;
686                 }
687
688                 if(clip->cache->stableibuf && clip->cache->stable_framenr==framenr) {   /* there's cached ibuf */
689                         if(clip->cache->render_flag==render_flag && clip->cache->proxy==proxy) {        /* cached ibuf used the same proxy settings */
690                                 stableibuf= clip->cache->stableibuf;
691
692                                 BKE_tracking_stabilization_data(&clip->tracking, framenr, stableibuf->x, stableibuf->y, tloc, &tscale, &tangle);
693
694                                 /* check for stabilization parameters */
695                                 if(!equals_v2v2(tloc, clip->cache->stable_loc) || tscale!=clip->cache->stable_scale || tangle!=clip->cache->stable_angle) {
696                                         stableibuf= NULL;
697                                 }
698                         }
699                 }
700
701                 if(!stableibuf) {
702                         if(clip->cache->stableibuf)
703                                 IMB_freeImBuf(clip->cache->stableibuf);
704
705                         stableibuf= BKE_tracking_stabilize(&clip->tracking, framenr, ibuf, tloc, &tscale, &tangle);
706
707                         copy_v2_v2(clip->cache->stable_loc, tloc);
708                         clip->cache->stable_scale= tscale;
709                         clip->cache->stable_angle= tangle;
710                         clip->cache->stable_framenr= framenr;
711                         clip->cache->stableibuf= stableibuf;
712                         clip->cache->proxy= proxy;
713                         clip->cache->render_flag= render_flag;
714                 }
715
716                 IMB_refImBuf(stableibuf);
717
718                 if(loc)         copy_v2_v2(loc, tloc);
719                 if(scale)       *scale= tscale;
720                 if(angle)       *angle= tangle;
721         } else {
722                 if(loc)         zero_v2(loc);
723                 if(scale)       *scale= 1.0f;
724                 if(angle)       *angle= 0.0f;
725
726                 stableibuf= ibuf;
727         }
728
729         if(stableibuf!=ibuf) {
730                 IMB_freeImBuf(ibuf);
731                 ibuf= stableibuf;
732         }
733
734         return ibuf;
735
736 }
737
738 int BKE_movieclip_has_frame(MovieClip *clip, MovieClipUser *user)
739 {
740         ImBuf *ibuf= BKE_movieclip_get_ibuf(clip, user);
741
742         if(ibuf) {
743                 IMB_freeImBuf(ibuf);
744                 return 1;
745         }
746
747         return 0;
748 }
749
750 void BKE_movieclip_get_size(MovieClip *clip, MovieClipUser *user, int *width, int *height)
751 {
752         if(!user || user->framenr==clip->lastframe) {
753                 *width= clip->lastsize[0];
754                 *height= clip->lastsize[1];
755         } else {
756                 ImBuf *ibuf= BKE_movieclip_get_ibuf(clip, user);
757
758                 if(ibuf && ibuf->x && ibuf->y) {
759                         real_ibuf_size(clip, user, ibuf, width, height);
760                 } else {
761                         *width= 0;
762                         *height= 0;
763                 }
764
765                 if(ibuf)
766                         IMB_freeImBuf(ibuf);
767         }
768 }
769
770 void BKE_movieclip_aspect(MovieClip *clip, float *aspx, float *aspy)
771 {
772         *aspx= *aspy= 1.0;
773
774         /* x is always 1 */
775         *aspy = clip->aspy/clip->aspx/clip->tracking.camera.pixel_aspect;
776 }
777
778 /* get segments of cached frames. useful for debugging cache policies */
779 void BKE_movieclip_get_cache_segments(MovieClip *clip, MovieClipUser *user, int *totseg_r, int **points_r)
780 {
781         *totseg_r= 0;
782         *points_r= NULL;
783
784         if(clip->cache) {
785                 int proxy= rendersize_to_proxy(user, clip->flag);
786
787                 IMB_moviecache_get_cache_segments(clip->cache->moviecache, proxy, user->render_flag, totseg_r, points_r);
788         }
789 }
790
791 void BKE_movieclip_user_set_frame(MovieClipUser *iuser, int framenr)
792 {
793         /* TODO: clamp framenr here? */
794
795         iuser->framenr= framenr;
796 }
797
798 static void free_buffers(MovieClip *clip)
799 {
800         if(clip->cache) {
801                 IMB_moviecache_free(clip->cache->moviecache);
802
803                 if(clip->cache->stableibuf)
804                         IMB_freeImBuf(clip->cache->stableibuf);
805
806                 if(clip->cache->undistibuf)
807                         IMB_freeImBuf(clip->cache->undistibuf);
808
809                 MEM_freeN(clip->cache);
810                 clip->cache= NULL;
811         }
812
813         if(clip->anim) {
814                 IMB_free_anim(clip->anim);
815                 clip->anim= FALSE;
816         }
817 }
818
819 void BKE_movieclip_reload(MovieClip *clip)
820 {
821         /* clear cache */
822         free_buffers(clip);
823
824         clip->tracking.stabilization.ok= 0;
825
826         /* update clip source */
827         if(BLI_testextensie_array(clip->name, imb_ext_movie)) clip->source= MCLIP_SRC_MOVIE;
828         else clip->source= MCLIP_SRC_SEQUENCE;
829 }
830
831 void BKE_movieclip_update_scopes(MovieClip *clip, MovieClipUser *user, MovieClipScopes *scopes)
832 {
833         if(scopes->ok) return;
834
835         if(scopes->track_preview) {
836                 IMB_freeImBuf(scopes->track_preview);
837                 scopes->track_preview= NULL;
838         }
839
840         scopes->marker= NULL;
841         scopes->track= NULL;
842
843         if(clip) {
844                 if(clip->tracking.act_track) {
845                         MovieTrackingTrack *track= clip->tracking.act_track;
846                         MovieTrackingMarker *marker= BKE_tracking_get_marker(track, user->framenr);
847
848                         if(marker->flag&MARKER_DISABLED) {
849                                 scopes->track_disabled= 1;
850                         } else {
851                                 ImBuf *ibuf= BKE_movieclip_get_ibuf(clip, user);
852
853                                 scopes->track_disabled= 0;
854
855                                 if(ibuf && ibuf->rect) {
856                                         ImBuf *tmpibuf;
857                                         MovieTrackingMarker undist_marker= *marker;
858
859                                         if(user->render_flag&MCLIP_PROXY_RENDER_UNDISTORT) {
860                                                 int width, height;
861                                                 float aspy= 1.0f/clip->tracking.camera.pixel_aspect;;
862
863                                                 BKE_movieclip_get_size(clip, user, &width, &height);
864
865                                                 undist_marker.pos[0]*= width;
866                                                 undist_marker.pos[1]*= height*aspy;
867
868                                                 BKE_tracking_invert_intrinsics(&clip->tracking, undist_marker.pos, undist_marker.pos);
869
870                                                 undist_marker.pos[0]/= width;
871                                                 undist_marker.pos[1]/= height*aspy;
872                                         }
873
874                                         tmpibuf= BKE_tracking_get_pattern_imbuf(ibuf, track, &undist_marker, 1, 1, scopes->track_pos, NULL);
875
876                                         if(tmpibuf->rect_float)
877                                                 IMB_rect_from_float(tmpibuf);
878
879                                         if(tmpibuf->rect)
880                                                 scopes->track_preview= tmpibuf;
881                                         else
882                                                 IMB_freeImBuf(tmpibuf);
883                                 }
884
885                                 IMB_freeImBuf(ibuf);
886                         }
887
888                         if((track->flag&TRACK_LOCKED)==0) {
889                                 scopes->marker= marker;
890                                 scopes->track= track;
891                                 scopes->slide_scale[0]= track->pat_max[0]-track->pat_min[0];
892                                 scopes->slide_scale[1]= track->pat_max[1]-track->pat_min[1];
893                         }
894                 }
895         }
896
897         scopes->framenr= user->framenr;
898         scopes->ok= 1;
899 }
900
901 static void movieclip_build_proxy_ibuf(MovieClip *clip, ImBuf *ibuf, int cfra, int proxy_render_size, int undistorted)
902 {
903         char name[FILE_MAXFILE+FILE_MAXDIR];
904         int quality, rectx, recty;
905         int size= size= rendersize_to_number(proxy_render_size);
906         ImBuf *scaleibuf;
907
908         get_proxy_fname(clip, proxy_render_size, undistorted, cfra, name);
909
910         rectx= ibuf->x*size/100.0f;
911         recty= ibuf->y*size/100.0f;
912
913         scaleibuf= IMB_dupImBuf(ibuf);
914
915         IMB_scaleImBuf(scaleibuf, (short)rectx, (short)recty);
916
917         quality= clip->proxy.quality;
918         scaleibuf->ftype= JPG | quality;
919
920         /* unsupported feature only confuses other s/w */
921         if(scaleibuf->depth==32)
922                 scaleibuf->depth= 24;
923
924         BLI_lock_thread(LOCK_MOVIECLIP);
925
926         BLI_make_existing_file(name);
927         if(IMB_saveiff(scaleibuf, name, IB_rect)==0)
928                 perror(name);
929
930         BLI_unlock_thread(LOCK_MOVIECLIP);
931
932         IMB_freeImBuf(scaleibuf);
933 }
934
935 void BKE_movieclip_build_proxy_frame(MovieClip *clip, struct MovieDistortion *distortion,
936                         int cfra, int *build_sizes, int build_count, int undistorted)
937 {
938         ImBuf *ibuf;
939         MovieClipUser user;
940
941         user.framenr= cfra;
942
943         ibuf= BKE_movieclip_get_ibuf_flag(clip, &user, 0);
944
945         if(ibuf) {
946                 ImBuf *tmpibuf= ibuf;
947                 int i;
948
949                 if(undistorted)
950                         tmpibuf= get_undistorted_ibuf(clip, distortion, ibuf);
951
952                 for(i= 0; i<build_count; i++)
953                         movieclip_build_proxy_ibuf(clip, tmpibuf, cfra, build_sizes[i], undistorted);
954
955                 IMB_freeImBuf(ibuf);
956
957                 if(tmpibuf!=ibuf)
958                         IMB_freeImBuf(tmpibuf);
959         }
960 }
961
962 void free_movieclip(MovieClip *clip)
963 {
964         free_buffers(clip);
965
966         BKE_tracking_free(&clip->tracking);
967 }
968
969 void unlink_movieclip(Main *bmain, MovieClip *clip)
970 {
971         bScreen *scr;
972         ScrArea *area;
973         SpaceLink *sl;
974         Scene *sce;
975         Object *ob;
976
977         for(scr= bmain->screen.first; scr; scr= scr->id.next) {
978                 for(area= scr->areabase.first; area; area= area->next) {
979                         for(sl= area->spacedata.first; sl; sl= sl->next) {
980                                 if(sl->spacetype==SPACE_CLIP) {
981                                         SpaceClip *sc= (SpaceClip *) sl;
982
983                                         if(sc->clip==clip)
984                                                 sc->clip= NULL;
985                                 }
986                                 else if(sl->spacetype==SPACE_VIEW3D) {
987                                         View3D *v3d= (View3D *) sl;
988                                         BGpic *bgpic;
989
990                                         for(bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next) {
991                                                 if(bgpic->clip==clip)
992                                                         bgpic->clip= NULL;
993                                         }
994                                 }
995                         }
996                 }
997         }
998
999         for(sce= bmain->scene.first; sce; sce= sce->id.next) {
1000                 if(sce->clip==clip)
1001                         sce->clip= NULL;
1002         }
1003
1004         for(ob= bmain->object.first; ob; ob= ob->id.next) {
1005                 bConstraint *con= ob->constraints.first;
1006
1007                 for (con= ob->constraints.first; con; con= con->next) {
1008                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1009
1010                         if(cti->type==CONSTRAINT_TYPE_FOLLOWTRACK) {
1011                                 bFollowTrackConstraint *data= (bFollowTrackConstraint *) con->data;
1012
1013                                 if(data->clip==clip)
1014                                         data->clip= NULL;
1015                         }
1016                         else if(cti->type==CONSTRAINT_TYPE_CAMERASOLVER) {
1017                                 bCameraSolverConstraint *data= (bCameraSolverConstraint *) con->data;
1018
1019                                 if(data->clip==clip)
1020                                         data->clip= NULL;
1021                         }
1022                 }
1023         }
1024
1025         clip->id.us= 0;
1026 }