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