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_blenlib.h"
59 #include "BLI_utildefines.h"
60 #include "BLI_ghash.h"
61 #include "BLI_mempool.h"
62
63 #include "BKE_library.h"
64 #include "BKE_global.h"
65 #include "BKE_main.h"
66 #include "BKE_utildefines.h"
67 #include "BKE_movieclip.h"
68 #include "BKE_moviecache.h"
69 #include "BKE_image.h"  /* openanim */
70 #include "BKE_tracking.h"
71 #include "BKE_animsys.h"
72
73 #include "IMB_imbuf_types.h"
74 #include "IMB_imbuf.h"
75
76 /*********************** movieclip buffer loaders *************************/
77
78 static int sequence_guess_offset(const char *full_name, int head_len, int numlen)
79 {
80         char num[FILE_MAX]= {0};
81
82         strncpy(num, full_name+head_len, numlen);
83
84         return atoi(num);
85 }
86
87 static ImBuf *movieclip_load_sequence_file(MovieClip *clip, int framenr)
88 {
89         struct ImBuf *ibuf;
90         unsigned short numlen;
91         char name[FILE_MAX], head[FILE_MAX], tail[FILE_MAX];
92         int flag, offset;
93
94         BLI_strncpy(name, clip->name, sizeof(name));
95         BLI_stringdec(name, head, tail, &numlen);
96
97         /* movieclips always points to first image from sequence,
98            autoguess offset for now. could be something smarter in the future */
99         offset= sequence_guess_offset(clip->name, strlen(head), numlen);
100
101         if(numlen) BLI_stringenc(name, head, tail, numlen, offset+framenr);
102         else strncpy(name, clip->name, sizeof(name));
103
104         if(clip->id.lib)
105                 BLI_path_abs(name, clip->id.lib->filepath);
106         else
107                 BLI_path_abs(name, G.main->name);
108
109         flag= IB_rect|IB_multilayer;
110
111         /* read ibuf */
112         ibuf= IMB_loadiffname(name, flag);
113
114         return ibuf;
115 }
116
117 static ImBuf *movieclip_load_movie_file(MovieClip *clip, int framenr)
118 {
119         ImBuf *ibuf= NULL;
120         char str[FILE_MAX];
121
122         if(!clip->anim) {
123                 BLI_strncpy(str, clip->name, FILE_MAX);
124
125                 if(clip->id.lib)
126                         BLI_path_abs(str, clip->id.lib->filepath);
127                 else
128                         BLI_path_abs(str, G.main->name);
129
130                 clip->anim= openanim(str, IB_rect);
131         }
132
133         if(clip->anim) {
134                 int dur= IMB_anim_get_duration(clip->anim);
135                 int fra= framenr-1;
136
137                 if(fra<0)
138                         fra= 0;
139
140                 if(fra>(dur-1))
141                         fra= dur-1;
142
143                 ibuf= IMB_anim_absolute(clip->anim, fra);
144         }
145
146         return ibuf;
147 }
148
149 /*********************** image buffer cache *************************/
150
151 typedef struct MovieClipImBufCacheKey {
152         int framenr;
153 } MovieClipImBufCacheKey;
154
155 static void moviecache_keydata(void *userkey, int *framenr)
156 {
157         MovieClipImBufCacheKey *key= (MovieClipImBufCacheKey*)userkey;
158
159         *framenr= key->framenr;
160 }
161
162 static unsigned int moviecache_hashhash(const void *keyv)
163 {
164         MovieClipImBufCacheKey *key= (MovieClipImBufCacheKey*)keyv;
165         int rval= key->framenr;
166
167         return rval;
168 }
169
170 static int moviecache_hashcmp(const void *av, const void *bv)
171 {
172         const MovieClipImBufCacheKey *a= (MovieClipImBufCacheKey*)av;
173         const MovieClipImBufCacheKey *b= (MovieClipImBufCacheKey*)bv;
174
175         if(a->framenr<b->framenr) return -1;
176         else if(a->framenr>b->framenr) return 1;
177
178         return 0;
179 }
180
181 static ImBuf *get_imbuf_cache(MovieClip *clip, MovieClipUser *user)
182 {
183         if(clip->ibuf_cache) {
184                 MovieClipImBufCacheKey key;
185
186                 key.framenr= user->framenr;
187                 return BKE_moviecache_get(clip->ibuf_cache, &key);
188         }
189
190         return NULL;
191 }
192
193 static void put_imbuf_cache(MovieClip *clip, MovieClipUser *user, ImBuf *ibuf)
194 {
195         MovieClipImBufCacheKey key;
196
197         if(!clip->ibuf_cache) {
198                 clip->ibuf_cache= BKE_moviecache_create(sizeof(MovieClipImBufCacheKey), moviecache_hashhash,
199                                 moviecache_hashcmp, moviecache_keydata);
200         }
201
202         key.framenr= user->framenr;
203
204         BKE_moviecache_put(clip->ibuf_cache, &key, ibuf);
205 }
206
207 /*********************** common functions *************************/
208
209 /* only image block itself */
210 static MovieClip *movieclip_alloc(const char *name)
211 {
212         MovieClip *clip;
213
214         clip= alloc_libblock(&G.main->movieclip, ID_MC, name);
215         clip->tracking.camera.focal= 24.0f;
216
217         return clip;
218 }
219
220 /* checks if image was already loaded, then returns same image
221    otherwise creates new.
222    does not load ibuf itself
223    pass on optional frame for #name images */
224 MovieClip *BKE_add_movieclip_file(const char *name)
225 {
226         MovieClip *clip;
227         int file, len;
228         const char *libname;
229         char str[FILE_MAX], strtest[FILE_MAX];
230
231         BLI_strncpy(str, name, sizeof(str));
232         BLI_path_abs(str, G.main->name);
233
234         /* exists? */
235         file= open(str, O_BINARY|O_RDONLY);
236         if(file== -1) return NULL;
237         close(file);
238
239         /* ** first search an identical clip ** */
240         for(clip= G.main->movieclip.first; clip; clip= clip->id.next) {
241                 BLI_strncpy(strtest, clip->name, sizeof(clip->name));
242                 BLI_path_abs(strtest, G.main->name);
243
244                 if(strcmp(strtest, str)==0) {
245                         BLI_strncpy(clip->name, name, sizeof(clip->name));  /* for stringcode */
246                         clip->id.us++;  /* officially should not, it doesn't link here! */
247
248                         return clip;
249                 }
250         }
251
252         /* ** add new movieclip ** */
253
254         /* create a short library name */
255         len= strlen(name);
256
257         while (len > 0 && name[len - 1] != '/' && name[len - 1] != '\\') len--;
258         libname= name+len;
259
260         clip= movieclip_alloc(libname);
261         BLI_strncpy(clip->name, name, sizeof(clip->name));
262
263         if(BLI_testextensie_array(name, imb_ext_movie)) clip->source= MCLIP_SRC_MOVIE;
264         else clip->source= MCLIP_SRC_SEQUENCE;
265
266         return clip;
267 }
268
269 ImBuf *BKE_movieclip_acquire_ibuf(MovieClip *clip, MovieClipUser *user)
270 {
271         ImBuf *ibuf= NULL;
272         int framenr= user?user->framenr:clip->lastframe;
273
274         clip->lastframe= framenr;
275         ibuf= get_imbuf_cache(clip, user);
276
277         if(!ibuf) {
278                 if(clip->source==MCLIP_SRC_SEQUENCE)
279                         ibuf= movieclip_load_sequence_file(clip, framenr);
280                 else
281                         ibuf= movieclip_load_movie_file(clip, framenr);
282
283                 if(ibuf)
284                         put_imbuf_cache(clip, user, ibuf);
285         }
286
287         return ibuf;
288 }
289
290 int BKE_movieclip_has_frame(MovieClip *clip, MovieClipUser *user)
291 {
292         ImBuf *ibuf= BKE_movieclip_acquire_ibuf(clip, user);
293
294         if(ibuf) {
295                 IMB_freeImBuf(ibuf);
296                 return 1;
297         }
298
299         return 0;
300 }
301
302 void BKE_movieclip_acquire_size(MovieClip *clip, MovieClipUser *user, int *width, int *height)
303 {
304         ImBuf *ibuf= BKE_movieclip_acquire_ibuf(clip, user);
305
306         if(ibuf && ibuf->x && ibuf->y) {
307                 *width= ibuf->x;
308                 *height= ibuf->y;
309         } else {
310                 *width= 0;
311                 *height= 0;
312         }
313
314         if(ibuf)
315                 IMB_freeImBuf(ibuf);
316 }
317
318 /* get segments of cached frames. useful for debugging cache policies */
319 void BKE_movieclip_get_cache_segments(MovieClip *clip, int *totseg_r, int **points_r)
320 {
321         *totseg_r= 0;
322         *points_r= NULL;
323
324         if(clip->ibuf_cache)
325                 BKE_moviecache_get_cache_segments(clip->ibuf_cache, totseg_r, points_r);
326 }
327
328 void BKE_movieclip_user_set_frame(MovieClipUser *iuser, int framenr)
329 {
330         /* TODO: clamp framenr here? */
331
332         iuser->framenr= framenr;
333 }
334
335 static void free_buffers(MovieClip *clip)
336 {
337         if(clip->ibuf_cache) {
338                 BKE_moviecache_free(clip->ibuf_cache);
339                 clip->ibuf_cache= NULL;
340         }
341
342         if(clip->anim) {
343                 IMB_free_anim(clip->anim);
344                 clip->anim= FALSE;
345         }
346 }
347
348 void BKE_movieclip_reload(MovieClip *clip)
349 {
350         /* clear cache */
351         free_buffers(clip);
352
353         /* update clip source */
354         if(BLI_testextensie_array(clip->name, imb_ext_movie)) clip->source= MCLIP_SRC_MOVIE;
355         else clip->source= MCLIP_SRC_SEQUENCE;
356 }
357
358 /* area - which part of marker should be selected:
359     0 - the whole marker and pattern/search
360         1 - only marker
361         2 - only pattern
362         3 - only search */
363 void BKE_movieclip_select_track(MovieClip *clip, MovieTrackingTrack *track, int area, int extend)
364 {
365         if(extend) {
366                 if(track->bundle) track->bundle->flag|= SELECT;
367                 BKE_tracking_track_flag(track, area, SELECT, 0);
368         } else {
369                 MovieTrackingTrack *cur= clip->tracking.tracks.first;
370
371                 while(cur) {
372                         if(cur==track) {
373                                 if(cur->bundle) cur->bundle->flag|= SELECT;
374                                 BKE_tracking_track_flag(cur, TRACK_AREA_ALL, SELECT, 1);
375                                 BKE_tracking_track_flag(cur, area, SELECT, 0);
376                         }
377                         else {
378                                 if(cur->bundle) cur->bundle->flag&= ~SELECT;
379                                 BKE_tracking_track_flag(cur, TRACK_AREA_ALL, SELECT, 1);
380                         }
381
382                         cur= cur->next;
383                 }
384         }
385
386         if(!TRACK_SELECTED(track))
387                 BKE_movieclip_set_selection(clip, MCLIP_SEL_NONE, NULL);
388 }
389
390 void BKE_movieclip_deselect_track(MovieClip *clip, MovieTrackingTrack *track, int area)
391 {
392         BKE_tracking_track_flag(track, area, SELECT, 1);
393
394         if(!TRACK_SELECTED(track))
395                 BKE_movieclip_set_selection(clip, MCLIP_SEL_NONE, NULL);
396 }
397
398 void BKE_movieclip_set_selection(MovieClip *clip, int type, void *sel)
399 {
400         clip->sel_type= type;
401
402         if(type == MCLIP_SEL_NONE) clip->last_sel= NULL;
403         else clip->last_sel= sel;
404 }
405
406 void BKE_movieclip_last_selection(MovieClip *clip, int *type, void **sel)
407 {
408         *type= clip->sel_type;
409
410         if(clip->sel_type == MCLIP_SEL_NONE) *sel= NULL;
411         else *sel= clip->last_sel;
412 }
413
414 void free_movieclip(MovieClip *clip)
415 {
416         free_buffers(clip);
417
418         if(clip->adt) BKE_free_animdata((ID *)clip);
419
420         BKE_tracking_free(&clip->tracking);
421 }
422
423 void unlink_movieclip(Main *bmain, MovieClip *clip)
424 {
425         bScreen *scr;
426         ScrArea *area;
427         SpaceLink *sl;
428
429         /* text space */
430         for(scr= bmain->screen.first; scr; scr= scr->id.next) {
431                 for(area= scr->areabase.first; area; area= area->next) {
432                         for(sl= area->spacedata.first; sl; sl= sl->next) {
433                                 if(sl->spacetype==SPACE_CLIP) {
434                                         SpaceClip *sc= (SpaceClip*) sl;
435
436                                         if(sc->clip==clip) {
437                                                 sc->clip= NULL;
438                                         }
439                                 }
440                         }
441                 }
442         }
443
444         clip->id.us= 0;
445 }