Merging r37704 through r37731 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->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
216         BKE_tracking_reset_settings(&clip->tracking);
217         clip->tracking.camera.focal= 24.0f;
218
219         return clip;
220 }
221
222 /* checks if image was already loaded, then returns same image
223    otherwise creates new.
224    does not load ibuf itself
225    pass on optional frame for #name images */
226 MovieClip *BKE_add_movieclip_file(const char *name)
227 {
228         MovieClip *clip;
229         int file, len;
230         const char *libname;
231         char str[FILE_MAX], strtest[FILE_MAX];
232
233         BLI_strncpy(str, name, sizeof(str));
234         BLI_path_abs(str, G.main->name);
235
236         /* exists? */
237         file= open(str, O_BINARY|O_RDONLY);
238         if(file== -1) return NULL;
239         close(file);
240
241         /* ** first search an identical clip ** */
242         for(clip= G.main->movieclip.first; clip; clip= clip->id.next) {
243                 BLI_strncpy(strtest, clip->name, sizeof(clip->name));
244                 BLI_path_abs(strtest, G.main->name);
245
246                 if(strcmp(strtest, str)==0) {
247                         BLI_strncpy(clip->name, name, sizeof(clip->name));  /* for stringcode */
248                         clip->id.us++;  /* officially should not, it doesn't link here! */
249
250                         return clip;
251                 }
252         }
253
254         /* ** add new movieclip ** */
255
256         /* create a short library name */
257         len= strlen(name);
258
259         while (len > 0 && name[len - 1] != '/' && name[len - 1] != '\\') len--;
260         libname= name+len;
261
262         clip= movieclip_alloc(libname);
263         BLI_strncpy(clip->name, name, sizeof(clip->name));
264
265         if(BLI_testextensie_array(name, imb_ext_movie)) clip->source= MCLIP_SRC_MOVIE;
266         else clip->source= MCLIP_SRC_SEQUENCE;
267
268         return clip;
269 }
270
271 ImBuf *BKE_movieclip_acquire_ibuf(MovieClip *clip, MovieClipUser *user)
272 {
273         ImBuf *ibuf= NULL;
274         int framenr= user?user->framenr:clip->lastframe;
275
276         clip->lastframe= framenr;
277         ibuf= get_imbuf_cache(clip, user);
278
279         if(!ibuf) {
280                 if(clip->source==MCLIP_SRC_SEQUENCE)
281                         ibuf= movieclip_load_sequence_file(clip, framenr);
282                 else
283                         ibuf= movieclip_load_movie_file(clip, framenr);
284
285                 if(ibuf)
286                         put_imbuf_cache(clip, user, ibuf);
287         }
288
289         return ibuf;
290 }
291
292 int BKE_movieclip_has_frame(MovieClip *clip, MovieClipUser *user)
293 {
294         ImBuf *ibuf= BKE_movieclip_acquire_ibuf(clip, user);
295
296         if(ibuf) {
297                 IMB_freeImBuf(ibuf);
298                 return 1;
299         }
300
301         return 0;
302 }
303
304 void BKE_movieclip_acquire_size(MovieClip *clip, MovieClipUser *user, int *width, int *height)
305 {
306         ImBuf *ibuf= BKE_movieclip_acquire_ibuf(clip, user);
307
308         if(ibuf && ibuf->x && ibuf->y) {
309                 *width= ibuf->x;
310                 *height= ibuf->y;
311         } else {
312                 *width= 0;
313                 *height= 0;
314         }
315
316         if(ibuf)
317                 IMB_freeImBuf(ibuf);
318 }
319
320 /* get segments of cached frames. useful for debugging cache policies */
321 void BKE_movieclip_get_cache_segments(MovieClip *clip, int *totseg_r, int **points_r)
322 {
323         *totseg_r= 0;
324         *points_r= NULL;
325
326         if(clip->ibuf_cache)
327                 BKE_moviecache_get_cache_segments(clip->ibuf_cache, totseg_r, points_r);
328 }
329
330 void BKE_movieclip_user_set_frame(MovieClipUser *iuser, int framenr)
331 {
332         /* TODO: clamp framenr here? */
333
334         iuser->framenr= framenr;
335 }
336
337 static void free_buffers(MovieClip *clip)
338 {
339         if(clip->ibuf_cache) {
340                 BKE_moviecache_free(clip->ibuf_cache);
341                 clip->ibuf_cache= NULL;
342         }
343
344         if(clip->anim) {
345                 IMB_free_anim(clip->anim);
346                 clip->anim= FALSE;
347         }
348 }
349
350 void BKE_movieclip_reload(MovieClip *clip)
351 {
352         /* clear cache */
353         free_buffers(clip);
354
355         /* update clip source */
356         if(BLI_testextensie_array(clip->name, imb_ext_movie)) clip->source= MCLIP_SRC_MOVIE;
357         else clip->source= MCLIP_SRC_SEQUENCE;
358 }
359
360 /* area - which part of marker should be selected:
361     0 - the whole marker and pattern/search
362         1 - only marker
363         2 - only pattern
364         3 - only search */
365 void BKE_movieclip_select_track(MovieClip *clip, MovieTrackingTrack *track, int area, int extend)
366 {
367         if(extend) {
368                 if(track->bundle) track->bundle->flag|= SELECT;
369                 BKE_tracking_track_flag(track, area, SELECT, 0);
370         } else {
371                 MovieTrackingTrack *cur= clip->tracking.tracks.first;
372
373                 while(cur) {
374                         if(cur==track) {
375                                 if(cur->bundle) cur->bundle->flag|= SELECT;
376                                 BKE_tracking_track_flag(cur, TRACK_AREA_ALL, SELECT, 1);
377                                 BKE_tracking_track_flag(cur, area, SELECT, 0);
378                         }
379                         else {
380                                 if(cur->bundle) cur->bundle->flag&= ~SELECT;
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 }