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