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