Camera tracking integration
[blender.git] / source / blender / blenkernel / intern / movieclip.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation,
24  *                 Sergey Sharybin
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/blenkernel/intern/movieclip.c
30  *  \ingroup bke
31  */
32
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <fcntl.h>
37
38 #ifndef WIN32
39 #include <unistd.h>
40 #else
41 #include <io.h>
42 #endif
43
44 #include <time.h>
45
46 #ifdef _WIN32
47 #define open _open
48 #define close _close
49 #endif
50
51 #include "MEM_guardedalloc.h"
52
53 #include "DNA_screen_types.h"
54 #include "DNA_space_types.h"
55 #include "DNA_movieclip_types.h"
56 #include "DNA_object_types.h"   /* SELECT */
57
58 #include "BLI_utildefines.h"
59
60 #include "BLI_blenlib.h"
61 #include "BLI_ghash.h"
62 #include "BLI_math.h"
63 #include "BLI_mempool.h"
64 #include "BLI_threads.h"
65
66 #include "BKE_library.h"
67 #include "BKE_global.h"
68 #include "BKE_main.h"
69 #include "BKE_utildefines.h"
70 #include "BKE_movieclip.h"
71 #include "BKE_moviecache.h"
72 #include "BKE_image.h"  /* openanim */
73 #include "BKE_tracking.h"
74 #include "BKE_animsys.h"
75
76 #include "IMB_imbuf_types.h"
77 #include "IMB_imbuf.h"
78
79 /*********************** movieclip buffer loaders *************************/
80
81 static int sequence_guess_offset(const char *full_name, int head_len, int numlen)
82 {
83         char num[FILE_MAX]= {0};
84
85         strncpy(num, full_name+head_len, numlen);
86
87         return atoi(num);
88 }
89
90 static ImBuf *movieclip_load_sequence_file(MovieClip *clip, int framenr)
91 {
92         struct ImBuf *ibuf;
93         unsigned short numlen;
94         char name[FILE_MAX], head[FILE_MAX], tail[FILE_MAX];
95         int flag, offset;
96
97         BLI_strncpy(name, clip->name, sizeof(name));
98         BLI_stringdec(name, head, tail, &numlen);
99
100         /* movieclips always points to first image from sequence,
101            autoguess offset for now. could be something smarter in the future */
102         offset= sequence_guess_offset(clip->name, strlen(head), numlen);
103
104         if(numlen) BLI_stringenc(name, head, tail, numlen, offset+framenr-1);
105         else strncpy(name, clip->name, sizeof(name));
106
107         if(clip->id.lib)
108                 BLI_path_abs(name, clip->id.lib->filepath);
109         else
110                 BLI_path_abs(name, G.main->name);
111
112         flag= IB_rect|IB_multilayer;
113
114         /* read ibuf */
115         ibuf= IMB_loadiffname(name, flag);
116
117         return ibuf;
118 }
119
120 static ImBuf *movieclip_load_movie_file(MovieClip *clip, int framenr)
121 {
122         ImBuf *ibuf= NULL;
123         char str[FILE_MAX];
124
125         if(!clip->anim) {
126                 BLI_strncpy(str, clip->name, FILE_MAX);
127
128                 if(clip->id.lib)
129                         BLI_path_abs(str, clip->id.lib->filepath);
130                 else
131                         BLI_path_abs(str, G.main->name);
132
133                 clip->anim= openanim(str, IB_rect);
134         }
135
136         if(clip->anim) {
137                 int dur= IMB_anim_get_duration(clip->anim);
138                 int fra= framenr-1;
139
140                 if(fra<0)
141                         fra= 0;
142
143                 if(fra>(dur-1))
144                         fra= dur-1;
145
146                 ibuf= IMB_anim_absolute(clip->anim, fra);
147         }
148
149         return ibuf;
150 }
151
152 /*********************** image buffer cache *************************/
153
154 typedef struct MovieClipCache {
155         /* regular moive cache */
156         struct MovieCache *moviecache;
157
158         /* cache for stable shot */
159         int stable_framenr;
160         float stable_loc[2], stable_scale;
161         ImBuf *stableibuf;
162 } MovieClipCache;
163
164 typedef struct MovieClipImBufCacheKey {
165         int framenr;
166 } MovieClipImBufCacheKey;
167
168 static void moviecache_keydata(void *userkey, int *framenr)
169 {
170         MovieClipImBufCacheKey *key= (MovieClipImBufCacheKey*)userkey;
171
172         *framenr= key->framenr;
173 }
174
175 static unsigned int moviecache_hashhash(const void *keyv)
176 {
177         MovieClipImBufCacheKey *key= (MovieClipImBufCacheKey*)keyv;
178         int rval= key->framenr;
179
180         return rval;
181 }
182
183 static int moviecache_hashcmp(const void *av, const void *bv)
184 {
185         const MovieClipImBufCacheKey *a= (MovieClipImBufCacheKey*)av;
186         const MovieClipImBufCacheKey *b= (MovieClipImBufCacheKey*)bv;
187
188         if(a->framenr<b->framenr) return -1;
189         else if(a->framenr>b->framenr) return 1;
190
191         return 0;
192 }
193
194 static ImBuf *get_imbuf_cache(MovieClip *clip, MovieClipUser *user)
195 {
196         if(clip->cache) {
197                 MovieClipImBufCacheKey key;
198
199                 key.framenr= user?user->framenr:clip->lastframe;
200                 return BKE_moviecache_get(clip->cache->moviecache, &key);
201         }
202
203         return NULL;
204 }
205
206 static void put_imbuf_cache(MovieClip *clip, MovieClipUser *user, ImBuf *ibuf)
207 {
208         MovieClipImBufCacheKey key;
209
210         if(!clip->cache) {
211                 clip->cache= MEM_callocN(sizeof(MovieClipCache), "movieClipCache");
212
213                 clip->cache->moviecache= BKE_moviecache_create(sizeof(MovieClipImBufCacheKey), moviecache_hashhash,
214                                 moviecache_hashcmp, moviecache_keydata);
215         }
216
217         key.framenr= user?user->framenr:clip->lastframe;
218
219         BKE_moviecache_put(clip->cache->moviecache, &key, ibuf);
220 }
221
222 /*********************** common functions *************************/
223
224 /* only image block itself */
225 static MovieClip *movieclip_alloc(const char *name)
226 {
227         MovieClip *clip;
228
229         clip= alloc_libblock(&G.main->movieclip, ID_MC, name);
230
231         clip->aspx= clip->aspy= 1.0f;
232
233         clip->tracking.camera.focal= 24.f;
234         clip->tracking.camera.sensor_width= 35.0f;
235         clip->tracking.camera.sensor_height= 18.0f;
236         clip->tracking.camera.units= CAMERA_UNITS_MM;
237
238         clip->tracking.settings.frames_limit= 20;
239         clip->tracking.settings.keyframe1= 1;
240         clip->tracking.settings.keyframe2= 30;
241
242         clip->tracking.stabilization.scaleinf= 1.f;
243         clip->tracking.stabilization.locinf= 1.f;
244
245         return clip;
246 }
247
248 /* checks if image was already loaded, then returns same image
249    otherwise creates new.
250    does not load ibuf itself
251    pass on optional frame for #name images */
252 MovieClip *BKE_add_movieclip_file(const char *name)
253 {
254         MovieClip *clip;
255         MovieClipUser user;
256         int file, len, width, height;
257         const char *libname;
258         char str[FILE_MAX], strtest[FILE_MAX];
259
260         BLI_strncpy(str, name, sizeof(str));
261         BLI_path_abs(str, G.main->name);
262
263         /* exists? */
264         file= open(str, O_BINARY|O_RDONLY);
265         if(file== -1) return NULL;
266         close(file);
267
268         /* ** first search an identical clip ** */
269         for(clip= G.main->movieclip.first; clip; clip= clip->id.next) {
270                 BLI_strncpy(strtest, clip->name, sizeof(clip->name));
271                 BLI_path_abs(strtest, G.main->name);
272
273                 if(strcmp(strtest, str)==0) {
274                         BLI_strncpy(clip->name, name, sizeof(clip->name));  /* for stringcode */
275                         clip->id.us++;  /* officially should not, it doesn't link here! */
276
277                         return clip;
278                 }
279         }
280
281         /* ** add new movieclip ** */
282
283         /* create a short library name */
284         len= strlen(name);
285
286         while (len > 0 && name[len - 1] != '/' && name[len - 1] != '\\') len--;
287         libname= name+len;
288
289         clip= movieclip_alloc(libname);
290         BLI_strncpy(clip->name, name, sizeof(clip->name));
291
292         if(BLI_testextensie_array(name, imb_ext_movie)) clip->source= MCLIP_SRC_MOVIE;
293         else clip->source= MCLIP_SRC_SEQUENCE;
294
295         user.framenr= 1;
296         BKE_movieclip_acquire_size(clip, &user, &width, &height);
297         if(width && height) {
298                 clip->tracking.camera.principal[0]= ((float)width)/2;
299                 clip->tracking.camera.principal[1]= ((float)height)/2;
300         }
301
302         return clip;
303 }
304
305 ImBuf *BKE_movieclip_acquire_ibuf(MovieClip *clip, MovieClipUser *user)
306 {
307         ImBuf *ibuf= NULL;
308         int framenr= user?user->framenr:clip->lastframe;
309
310         /* cache isn't threadsafe itself and also loading of movies
311            can't happen from concurent threads that's why we use lock here */
312         BLI_lock_thread(LOCK_MOVIECLIP);
313
314         /* cache is supposed to be threadsafe */
315         ibuf= get_imbuf_cache(clip, user);
316
317         if(!ibuf) {
318                 if(clip->source==MCLIP_SRC_SEQUENCE)
319                         ibuf= movieclip_load_sequence_file(clip, framenr);
320                 else {
321                         ibuf= movieclip_load_movie_file(clip, framenr);
322                 }
323
324                 if(ibuf)
325                         put_imbuf_cache(clip, user, ibuf);
326         }
327
328         if(ibuf) {
329                 clip->lastframe= framenr;
330
331                 clip->lastsize[0]= ibuf->x;
332                 clip->lastsize[1]= ibuf->y;
333         }
334
335         BLI_unlock_thread(LOCK_MOVIECLIP);
336
337         return ibuf;
338 }
339
340 ImBuf *BKE_movieclip_acquire_stable_ibuf(MovieClip *clip, MovieClipUser *user, float loc[2], float *scale)
341 {
342         ImBuf *ibuf, *stableibuf= NULL;
343         int framenr= user?user->framenr:clip->lastframe;
344
345         ibuf= BKE_movieclip_acquire_ibuf(clip, user);
346
347         if(clip->tracking.stabilization.flag&TRACKING_2D_STABILIZATION) {
348                 float tloc[2], tscale;
349
350                 if(clip->cache->stableibuf && clip->cache->stable_framenr==framenr) {
351                         stableibuf= clip->cache->stableibuf;
352
353                         BKE_tracking_stabilization_data(&clip->tracking, framenr, stableibuf->x, stableibuf->y, tloc, &tscale);
354
355                         if(!equals_v2v2(tloc, clip->cache->stable_loc) || tscale!=clip->cache->stable_scale) {
356                                 stableibuf= NULL;
357                         }
358                 }
359
360                 if(!stableibuf) {
361                         if(clip->cache->stableibuf)
362                                 IMB_freeImBuf(clip->cache->stableibuf);
363
364                         stableibuf= BKE_tracking_stabilize_shot(&clip->tracking, framenr, ibuf, tloc, &tscale);
365
366                         copy_v2_v2(clip->cache->stable_loc, tloc);
367                         clip->cache->stable_scale= tscale;
368                         clip->cache->stable_framenr= framenr;
369                         clip->cache->stableibuf= stableibuf;
370                 }
371
372                 IMB_refImBuf(stableibuf);
373
374                 if(loc)         copy_v2_v2(loc, tloc);
375                 if(scale)       *scale= tscale;
376         } else {
377                 if(loc)         zero_v2(loc);
378                 if(scale)       *scale= 1.f;
379
380                 stableibuf= ibuf;
381         }
382
383         if(stableibuf!=ibuf) {
384                 IMB_freeImBuf(ibuf);
385                 ibuf= stableibuf;
386         }
387
388         return ibuf;
389
390 }
391
392 int BKE_movieclip_has_frame(MovieClip *clip, MovieClipUser *user)
393 {
394         ImBuf *ibuf= BKE_movieclip_acquire_ibuf(clip, user);
395
396         if(ibuf) {
397                 IMB_freeImBuf(ibuf);
398                 return 1;
399         }
400
401         return 0;
402 }
403
404 void BKE_movieclip_acquire_size(MovieClip *clip, MovieClipUser *user, int *width, int *height)
405 {
406         if(!user || user->framenr==clip->lastframe) {
407                 *width= clip->lastsize[0];
408                 *height= clip->lastsize[1];
409         } else {
410                 ImBuf *ibuf= BKE_movieclip_acquire_ibuf(clip, user);
411
412                 if(ibuf && ibuf->x && ibuf->y) {
413                         *width= ibuf->x;
414                         *height= ibuf->y;
415                 } else {
416                         *width= 0;
417                         *height= 0;
418                 }
419
420                 if(ibuf)
421                         IMB_freeImBuf(ibuf);
422         }
423 }
424
425 void BKE_movieclip_aspect(MovieClip *clip, float *aspx, float *aspy)
426 {
427         *aspx= *aspy= 1.0;
428
429         /* x is always 1 */
430         *aspy = clip->aspy/clip->aspx;
431 }
432
433 /* get segments of cached frames. useful for debugging cache policies */
434 void BKE_movieclip_get_cache_segments(MovieClip *clip, int *totseg_r, int **points_r)
435 {
436         *totseg_r= 0;
437         *points_r= NULL;
438
439         if(clip->cache)
440                 BKE_moviecache_get_cache_segments(clip->cache->moviecache, totseg_r, points_r);
441 }
442
443 void BKE_movieclip_user_set_frame(MovieClipUser *iuser, int framenr)
444 {
445         /* TODO: clamp framenr here? */
446
447         iuser->framenr= framenr;
448 }
449
450 static void free_buffers(MovieClip *clip)
451 {
452         if(clip->cache) {
453                 BKE_moviecache_free(clip->cache->moviecache);
454
455                 if(clip->cache->stableibuf)
456                         IMB_freeImBuf(clip->cache->stableibuf);
457
458                 MEM_freeN(clip->cache);
459                 clip->cache= NULL;
460         }
461
462         if(clip->anim) {
463                 IMB_free_anim(clip->anim);
464                 clip->anim= FALSE;
465         }
466 }
467
468 void BKE_movieclip_reload(MovieClip *clip)
469 {
470         /* clear cache */
471         free_buffers(clip);
472
473         clip->tracking.stabilization.ok= 0;
474
475         /* update clip source */
476         if(BLI_testextensie_array(clip->name, imb_ext_movie)) clip->source= MCLIP_SRC_MOVIE;
477         else clip->source= MCLIP_SRC_SEQUENCE;
478 }
479
480 /* area - which part of marker should be selected. see TRACK_AREA_* constants */
481 void BKE_movieclip_select_track(MovieClip *clip, MovieTrackingTrack *track, int area, int extend)
482 {
483         if(extend) {
484                 BKE_tracking_track_flag(track, area, SELECT, 0);
485         } else {
486                 MovieTrackingTrack *cur= clip->tracking.tracks.first;
487
488                 while(cur) {
489                         if(cur==track) {
490                                 BKE_tracking_track_flag(cur, TRACK_AREA_ALL, SELECT, 1);
491                                 BKE_tracking_track_flag(cur, area, SELECT, 0);
492                         }
493                         else {
494                                 BKE_tracking_track_flag(cur, TRACK_AREA_ALL, SELECT, 1);
495                         }
496
497                         cur= cur->next;
498                 }
499         }
500
501         if(!TRACK_SELECTED(track))
502                 BKE_movieclip_set_selection(clip, MCLIP_SEL_NONE, NULL);
503 }
504
505 void BKE_movieclip_deselect_track(MovieClip *clip, MovieTrackingTrack *track, int area)
506 {
507         BKE_tracking_track_flag(track, area, SELECT, 1);
508
509         if(!TRACK_SELECTED(track))
510                 BKE_movieclip_set_selection(clip, MCLIP_SEL_NONE, NULL);
511 }
512
513 void BKE_movieclip_set_selection(MovieClip *clip, int type, void *sel)
514 {
515         clip->sel_type= type;
516
517         if(type == MCLIP_SEL_NONE) clip->last_sel= NULL;
518         else clip->last_sel= sel;
519 }
520
521 void BKE_movieclip_last_selection(MovieClip *clip, int *type, void **sel)
522 {
523         *type= clip->sel_type;
524
525         if(clip->sel_type == MCLIP_SEL_NONE) *sel= NULL;
526         else *sel= clip->last_sel;
527 }
528
529 void BKE_movieclip_update_scopes(MovieClip *clip, MovieClipUser *user, MovieClipScopes *scopes)
530 {
531         void *sel;
532         int sel_type;
533
534         if(scopes->ok) return;
535
536         if(scopes->track_preview) {
537                 IMB_freeImBuf(scopes->track_preview);
538                 scopes->track_preview= NULL;
539         }
540
541         scopes->marker= NULL;
542         scopes->track= NULL;
543
544         if(clip) {
545                 BKE_movieclip_last_selection(clip, &sel_type, &sel);
546
547                 if(sel_type==MCLIP_SEL_TRACK) {
548                         MovieTrackingTrack *track= (MovieTrackingTrack*)sel;
549                         MovieTrackingMarker *marker= BKE_tracking_get_marker(track, user->framenr);
550
551                         if(marker->flag&MARKER_DISABLED) {
552                                 scopes->track_disabled= 1;
553                         } else {
554                                 ImBuf *ibuf= BKE_movieclip_acquire_ibuf(clip, user);
555
556                                 scopes->track_disabled= 0;
557
558                                 if(ibuf && ibuf->rect) {
559                                         ImBuf *tmpibuf;
560
561                                         tmpibuf= BKE_tracking_acquire_pattern_imbuf(ibuf, track, marker, 1, scopes->track_pos, NULL);
562
563                                         if(tmpibuf->rect_float)
564                                                 IMB_rect_from_float(tmpibuf);
565
566                                         if(tmpibuf->rect)
567                                                 scopes->track_preview= tmpibuf;
568                                         else
569                                                 IMB_freeImBuf(tmpibuf);
570                                 }
571
572                                 IMB_freeImBuf(ibuf);
573                         }
574
575                         if((track->flag&TRACK_LOCKED)==0) {
576                                 scopes->marker= marker;
577                                 scopes->track= track;
578                                 scopes->slide_scale[0]= track->pat_max[0]-track->pat_min[0];
579                                 scopes->slide_scale[1]= track->pat_max[1]-track->pat_min[1];
580                         }
581                 }
582         }
583
584         scopes->framenr= user->framenr;
585         scopes->ok= 1;
586 }
587
588 void free_movieclip(MovieClip *clip)
589 {
590         free_buffers(clip);
591
592         BKE_tracking_free(&clip->tracking);
593 }
594
595 void unlink_movieclip(Main *bmain, MovieClip *clip)
596 {
597         bScreen *scr;
598         ScrArea *area;
599         SpaceLink *sl;
600
601         /* text space */
602         for(scr= bmain->screen.first; scr; scr= scr->id.next) {
603                 for(area= scr->areabase.first; area; area= area->next) {
604                         for(sl= area->spacedata.first; sl; sl= sl->next) {
605                                 if(sl->spacetype==SPACE_CLIP) {
606                                         SpaceClip *sc= (SpaceClip*) sl;
607
608                                         if(sc->clip==clip) {
609                                                 sc->clip= NULL;
610                                         }
611                                 }
612                         }
613                 }
614         }
615
616         clip->id.us= 0;
617 }