Merging r38197 through r38219 from trunk into soc-2011-tomato
[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?user->framenr:clip->lastframe;
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?user->framenr:clip->lastframe;
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
216         clip->tracking.settings.frames_limit= 20;
217         clip->tracking.settings.keyframe1= 1;
218         clip->tracking.settings.keyframe2= 30;
219
220         return clip;
221 }
222
223 /* checks if image was already loaded, then returns same image
224    otherwise creates new.
225    does not load ibuf itself
226    pass on optional frame for #name images */
227 MovieClip *BKE_add_movieclip_file(const char *name)
228 {
229         MovieClip *clip;
230         int file, len, width, height;
231         const char *libname;
232         char str[FILE_MAX], strtest[FILE_MAX];
233
234         BLI_strncpy(str, name, sizeof(str));
235         BLI_path_abs(str, G.main->name);
236
237         /* exists? */
238         file= open(str, O_BINARY|O_RDONLY);
239         if(file== -1) return NULL;
240         close(file);
241
242         /* ** first search an identical clip ** */
243         for(clip= G.main->movieclip.first; clip; clip= clip->id.next) {
244                 BLI_strncpy(strtest, clip->name, sizeof(clip->name));
245                 BLI_path_abs(strtest, G.main->name);
246
247                 if(strcmp(strtest, str)==0) {
248                         BLI_strncpy(clip->name, name, sizeof(clip->name));  /* for stringcode */
249                         clip->id.us++;  /* officially should not, it doesn't link here! */
250
251                         return clip;
252                 }
253         }
254
255         /* ** add new movieclip ** */
256
257         /* create a short library name */
258         len= strlen(name);
259
260         while (len > 0 && name[len - 1] != '/' && name[len - 1] != '\\') len--;
261         libname= name+len;
262
263         clip= movieclip_alloc(libname);
264         BLI_strncpy(clip->name, name, sizeof(clip->name));
265
266         if(BLI_testextensie_array(name, imb_ext_movie)) clip->source= MCLIP_SRC_MOVIE;
267         else clip->source= MCLIP_SRC_SEQUENCE;
268
269         BKE_movieclip_acquire_size(clip, NULL, &width, &height);
270         if(width && height) {
271                 clip->tracking.camera.principal[0]= ((float)width)/2;
272                 clip->tracking.camera.principal[1]= ((float)height)/2;
273         }
274
275         return clip;
276 }
277
278 ImBuf *BKE_movieclip_acquire_ibuf(MovieClip *clip, MovieClipUser *user)
279 {
280         ImBuf *ibuf= NULL;
281         int framenr= user?user->framenr:clip->lastframe;
282
283         clip->lastframe= framenr;
284         ibuf= get_imbuf_cache(clip, user);
285
286         if(!ibuf) {
287                 if(clip->source==MCLIP_SRC_SEQUENCE)
288                         ibuf= movieclip_load_sequence_file(clip, framenr);
289                 else
290                         ibuf= movieclip_load_movie_file(clip, framenr);
291
292                 if(ibuf)
293                         put_imbuf_cache(clip, user, ibuf);
294         }
295
296         return ibuf;
297 }
298
299 int BKE_movieclip_has_frame(MovieClip *clip, MovieClipUser *user)
300 {
301         ImBuf *ibuf= BKE_movieclip_acquire_ibuf(clip, user);
302
303         if(ibuf) {
304                 IMB_freeImBuf(ibuf);
305                 return 1;
306         }
307
308         return 0;
309 }
310
311 void BKE_movieclip_acquire_size(MovieClip *clip, MovieClipUser *user, int *width, int *height)
312 {
313         ImBuf *ibuf= BKE_movieclip_acquire_ibuf(clip, user);
314
315         if(ibuf && ibuf->x && ibuf->y) {
316                 *width= ibuf->x;
317                 *height= ibuf->y;
318         } else {
319                 *width= 0;
320                 *height= 0;
321         }
322
323         if(ibuf)
324                 IMB_freeImBuf(ibuf);
325 }
326
327 /* get segments of cached frames. useful for debugging cache policies */
328 void BKE_movieclip_get_cache_segments(MovieClip *clip, int *totseg_r, int **points_r)
329 {
330         *totseg_r= 0;
331         *points_r= NULL;
332
333         if(clip->ibuf_cache)
334                 BKE_moviecache_get_cache_segments(clip->ibuf_cache, totseg_r, points_r);
335 }
336
337 void BKE_movieclip_user_set_frame(MovieClipUser *iuser, int framenr)
338 {
339         /* TODO: clamp framenr here? */
340
341         iuser->framenr= framenr;
342 }
343
344 static void free_buffers(MovieClip *clip)
345 {
346         if(clip->ibuf_cache) {
347                 BKE_moviecache_free(clip->ibuf_cache);
348                 clip->ibuf_cache= NULL;
349         }
350
351         if(clip->anim) {
352                 IMB_free_anim(clip->anim);
353                 clip->anim= FALSE;
354         }
355 }
356
357 void BKE_movieclip_reload(MovieClip *clip)
358 {
359         /* clear cache */
360         free_buffers(clip);
361
362         /* update clip source */
363         if(BLI_testextensie_array(clip->name, imb_ext_movie)) clip->source= MCLIP_SRC_MOVIE;
364         else clip->source= MCLIP_SRC_SEQUENCE;
365 }
366
367 /* area - which part of marker should be selected. see TRACK_AREA_* constants */
368 void BKE_movieclip_select_track(MovieClip *clip, MovieTrackingTrack *track, int area, int extend)
369 {
370         if(extend) {
371                 BKE_tracking_track_flag(track, area, SELECT, 0);
372         } else {
373                 MovieTrackingTrack *cur= clip->tracking.tracks.first;
374
375                 while(cur) {
376                         if(cur==track) {
377                                 BKE_tracking_track_flag(cur, TRACK_AREA_ALL, SELECT, 1);
378                                 BKE_tracking_track_flag(cur, area, SELECT, 0);
379                         }
380                         else {
381                                 BKE_tracking_track_flag(cur, TRACK_AREA_ALL, SELECT, 1);
382                         }
383
384                         cur= cur->next;
385                 }
386         }
387
388         if(!TRACK_SELECTED(track))
389                 BKE_movieclip_set_selection(clip, MCLIP_SEL_NONE, NULL);
390 }
391
392 void BKE_movieclip_deselect_track(MovieClip *clip, MovieTrackingTrack *track, int area)
393 {
394         BKE_tracking_track_flag(track, area, SELECT, 1);
395
396         if(!TRACK_SELECTED(track))
397                 BKE_movieclip_set_selection(clip, MCLIP_SEL_NONE, NULL);
398 }
399
400 void BKE_movieclip_set_selection(MovieClip *clip, int type, void *sel)
401 {
402         clip->sel_type= type;
403
404         if(type == MCLIP_SEL_NONE) clip->last_sel= NULL;
405         else clip->last_sel= sel;
406 }
407
408 void BKE_movieclip_last_selection(MovieClip *clip, int *type, void **sel)
409 {
410         *type= clip->sel_type;
411
412         if(clip->sel_type == MCLIP_SEL_NONE) *sel= NULL;
413         else *sel= clip->last_sel;
414 }
415
416 void free_movieclip(MovieClip *clip)
417 {
418         free_buffers(clip);
419
420         if(clip->adt) BKE_free_animdata((ID *)clip);
421
422         BKE_tracking_free(&clip->tracking);
423 }
424
425 void unlink_movieclip(Main *bmain, MovieClip *clip)
426 {
427         bScreen *scr;
428         ScrArea *area;
429         SpaceLink *sl;
430
431         /* text space */
432         for(scr= bmain->screen.first; scr; scr= scr->id.next) {
433                 for(area= scr->areabase.first; area; area= area->next) {
434                         for(sl= area->spacedata.first; sl; sl= sl->next) {
435                                 if(sl->spacetype==SPACE_CLIP) {
436                                         SpaceClip *sc= (SpaceClip*) sl;
437
438                                         if(sc->clip==clip) {
439                                                 sc->clip= NULL;
440                                         }
441                                 }
442                         }
443                 }
444         }
445
446         clip->id.us= 0;
447 }