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