544902ae7ad74a0c86c59d4e26163aa6ffabd836
[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_utildefines.h"
59
60 #include "BLI_blenlib.h"
61 #include "BLI_ghash.h"
62 #include "BLI_math.h"
63 #include "BLI_mempool.h"
64 #include "BLI_threads.h"
65
66 #include "BKE_library.h"
67 #include "BKE_global.h"
68 #include "BKE_main.h"
69 #include "BKE_utildefines.h"
70 #include "BKE_movieclip.h"
71 #include "BKE_moviecache.h"
72 #include "BKE_image.h"  /* openanim */
73 #include "BKE_tracking.h"
74 #include "BKE_animsys.h"
75
76 #include "IMB_imbuf_types.h"
77 #include "IMB_imbuf.h"
78
79 /*********************** movieclip buffer loaders *************************/
80
81 static int sequence_guess_offset(const char *full_name, int head_len, int numlen)
82 {
83         char num[FILE_MAX]= {0};
84
85         strncpy(num, full_name+head_len, numlen);
86
87         return atoi(num);
88 }
89
90 static ImBuf *movieclip_load_sequence_file(MovieClip *clip, int framenr)
91 {
92         struct ImBuf *ibuf;
93         unsigned short numlen;
94         char name[FILE_MAX], head[FILE_MAX], tail[FILE_MAX];
95         int flag, offset;
96
97         BLI_strncpy(name, clip->name, sizeof(name));
98         BLI_stringdec(name, head, tail, &numlen);
99
100         /* movieclips always points to first image from sequence,
101            autoguess offset for now. could be something smarter in the future */
102         offset= sequence_guess_offset(clip->name, strlen(head), numlen);
103
104         if(numlen) BLI_stringenc(name, head, tail, numlen, offset+framenr-1);
105         else strncpy(name, clip->name, sizeof(name));
106
107         if(clip->id.lib)
108                 BLI_path_abs(name, clip->id.lib->filepath);
109         else
110                 BLI_path_abs(name, G.main->name);
111
112         flag= IB_rect|IB_multilayer;
113
114         /* read ibuf */
115         ibuf= IMB_loadiffname(name, flag);
116
117         return ibuf;
118 }
119
120 static ImBuf *movieclip_load_movie_file(MovieClip *clip, int framenr)
121 {
122         ImBuf *ibuf= NULL;
123         char str[FILE_MAX];
124
125         if(!clip->anim) {
126                 BLI_strncpy(str, clip->name, FILE_MAX);
127
128                 if(clip->id.lib)
129                         BLI_path_abs(str, clip->id.lib->filepath);
130                 else
131                         BLI_path_abs(str, G.main->name);
132
133                 clip->anim= openanim(str, IB_rect);
134         }
135
136         if(clip->anim) {
137                 int dur= IMB_anim_get_duration(clip->anim);
138                 int fra= framenr-1;
139
140                 if(fra<0)
141                         fra= 0;
142
143                 if(fra>(dur-1))
144                         fra= dur-1;
145
146                 ibuf= IMB_anim_absolute(clip->anim, fra);
147         }
148
149         return ibuf;
150 }
151
152 /*********************** image buffer cache *************************/
153
154 typedef struct MovieClipImBufCacheKey {
155         int framenr;
156 } MovieClipImBufCacheKey;
157
158 static void moviecache_keydata(void *userkey, int *framenr)
159 {
160         MovieClipImBufCacheKey *key= (MovieClipImBufCacheKey*)userkey;
161
162         *framenr= key->framenr;
163 }
164
165 static unsigned int moviecache_hashhash(const void *keyv)
166 {
167         MovieClipImBufCacheKey *key= (MovieClipImBufCacheKey*)keyv;
168         int rval= key->framenr;
169
170         return rval;
171 }
172
173 static int moviecache_hashcmp(const void *av, const void *bv)
174 {
175         const MovieClipImBufCacheKey *a= (MovieClipImBufCacheKey*)av;
176         const MovieClipImBufCacheKey *b= (MovieClipImBufCacheKey*)bv;
177
178         if(a->framenr<b->framenr) return -1;
179         else if(a->framenr>b->framenr) return 1;
180
181         return 0;
182 }
183
184 static ImBuf *get_imbuf_cache(MovieClip *clip, MovieClipUser *user)
185 {
186         if(clip->ibuf_cache) {
187                 MovieClipImBufCacheKey key;
188
189                 key.framenr= user?user->framenr:clip->lastframe;
190                 return BKE_moviecache_get(clip->ibuf_cache, &key);
191         }
192
193         return NULL;
194 }
195
196 static void put_imbuf_cache(MovieClip *clip, MovieClipUser *user, ImBuf *ibuf)
197 {
198         MovieClipImBufCacheKey key;
199
200         if(!clip->ibuf_cache) {
201                 clip->ibuf_cache= BKE_moviecache_create(sizeof(MovieClipImBufCacheKey), moviecache_hashhash,
202                                 moviecache_hashcmp, moviecache_keydata);
203         }
204
205         key.framenr= user?user->framenr:clip->lastframe;
206
207         BKE_moviecache_put(clip->ibuf_cache, &key, ibuf);
208 }
209
210 /*********************** common functions *************************/
211
212 /* only image block itself */
213 static MovieClip *movieclip_alloc(const char *name)
214 {
215         MovieClip *clip;
216
217         clip= alloc_libblock(&G.main->movieclip, ID_MC, name);
218
219         clip->aspx= clip->aspy= 1.0f;
220
221         clip->tracking.camera.sensor_width= 35.0f;
222         clip->tracking.camera.sensor_height= 18.0f;
223         clip->tracking.camera.units= CAMERA_UNITS_MM;
224
225         clip->tracking.settings.frames_limit= 20;
226         clip->tracking.settings.keyframe1= 1;
227         clip->tracking.settings.keyframe2= 30;
228         clip->tracking.settings.dist= 1;
229
230         return clip;
231 }
232
233 /* checks if image was already loaded, then returns same image
234    otherwise creates new.
235    does not load ibuf itself
236    pass on optional frame for #name images */
237 MovieClip *BKE_add_movieclip_file(const char *name)
238 {
239         MovieClip *clip;
240         MovieClipUser user;
241         int file, len, width, height;
242         const char *libname;
243         char str[FILE_MAX], strtest[FILE_MAX];
244
245         BLI_strncpy(str, name, sizeof(str));
246         BLI_path_abs(str, G.main->name);
247
248         /* exists? */
249         file= open(str, O_BINARY|O_RDONLY);
250         if(file== -1) return NULL;
251         close(file);
252
253         /* ** first search an identical clip ** */
254         for(clip= G.main->movieclip.first; clip; clip= clip->id.next) {
255                 BLI_strncpy(strtest, clip->name, sizeof(clip->name));
256                 BLI_path_abs(strtest, G.main->name);
257
258                 if(strcmp(strtest, str)==0) {
259                         BLI_strncpy(clip->name, name, sizeof(clip->name));  /* for stringcode */
260                         clip->id.us++;  /* officially should not, it doesn't link here! */
261
262                         return clip;
263                 }
264         }
265
266         /* ** add new movieclip ** */
267
268         /* create a short library name */
269         len= strlen(name);
270
271         while (len > 0 && name[len - 1] != '/' && name[len - 1] != '\\') len--;
272         libname= name+len;
273
274         clip= movieclip_alloc(libname);
275         BLI_strncpy(clip->name, name, sizeof(clip->name));
276
277         if(BLI_testextensie_array(name, imb_ext_movie)) clip->source= MCLIP_SRC_MOVIE;
278         else clip->source= MCLIP_SRC_SEQUENCE;
279
280         user.framenr= 1;
281         BKE_movieclip_acquire_size(clip, &user, &width, &height);
282         if(width && height) {
283                 clip->tracking.camera.principal[0]= ((float)width)/2;
284                 clip->tracking.camera.principal[1]= ((float)height)/2;
285         }
286
287         return clip;
288 }
289
290 ImBuf *BKE_movieclip_acquire_ibuf(MovieClip *clip, MovieClipUser *user)
291 {
292         ImBuf *ibuf= NULL;
293         int framenr= user?user->framenr:clip->lastframe;
294
295         /* cache is supposed to be threadsafe */
296         ibuf= get_imbuf_cache(clip, user);
297
298         if(!ibuf) {
299                 if(clip->source==MCLIP_SRC_SEQUENCE)
300                         ibuf= movieclip_load_sequence_file(clip, framenr);
301                 else {
302                         /* loading of movies can't happen from concurent threads */
303                         BLI_lock_thread(LOCK_MOVIECLIP);
304
305                         ibuf= movieclip_load_movie_file(clip, framenr);
306
307                         BLI_unlock_thread(LOCK_MOVIECLIP);
308                 }
309
310                 if(ibuf)
311                         put_imbuf_cache(clip, user, ibuf);
312         }
313
314         if(ibuf) {
315                 clip->lastframe= framenr;
316
317                 clip->lastsize[0]= ibuf->x;
318                 clip->lastsize[1]= ibuf->y;
319         }
320
321         return ibuf;
322 }
323
324 ImBuf *BKE_movieclip_acquire_stable_ibuf(MovieClip *clip, MovieClipUser *user, float mat[4][4])
325 {
326         ImBuf *ibuf, *stableibuf;
327         int framenr= user?user->framenr:clip->lastframe;
328
329         ibuf= BKE_movieclip_acquire_ibuf(clip, user);
330
331         if(clip->tracking.stabilization.flag&TRACKING_2D_STABILIZATION) {
332                 MovieTrackingStabilization *stab= &clip->tracking.stabilization;
333
334                 if(user->framenr!=stab->framenr)
335                         stab->ibufok= 0;
336
337                 stableibuf= BKE_tracking_stabilize_shot(&clip->tracking, framenr, ibuf, mat);
338
339                 stab->framenr= user->framenr;
340         } else {
341                 if(mat)
342                         unit_m4(mat);
343
344                 stableibuf= ibuf;
345         }
346
347         if(stableibuf!=ibuf) {
348                 IMB_freeImBuf(ibuf);
349                 ibuf= stableibuf;
350         }
351
352         return ibuf;
353
354 }
355
356 int BKE_movieclip_has_frame(MovieClip *clip, MovieClipUser *user)
357 {
358         ImBuf *ibuf= BKE_movieclip_acquire_ibuf(clip, user);
359
360         if(ibuf) {
361                 IMB_freeImBuf(ibuf);
362                 return 1;
363         }
364
365         return 0;
366 }
367
368 void BKE_movieclip_acquire_size(MovieClip *clip, MovieClipUser *user, int *width, int *height)
369 {
370         if(!user || user->framenr==clip->lastframe) {
371                 *width= clip->lastsize[0];
372                 *height= clip->lastsize[1];
373         } else {
374                 ImBuf *ibuf= BKE_movieclip_acquire_ibuf(clip, user);
375
376                 if(ibuf && ibuf->x && ibuf->y) {
377                         *width= ibuf->x;
378                         *height= ibuf->y;
379                 } else {
380                         *width= 0;
381                         *height= 0;
382                 }
383
384                 if(ibuf)
385                         IMB_freeImBuf(ibuf);
386         }
387 }
388
389 /* get segments of cached frames. useful for debugging cache policies */
390 void BKE_movieclip_get_cache_segments(MovieClip *clip, int *totseg_r, int **points_r)
391 {
392         *totseg_r= 0;
393         *points_r= NULL;
394
395         if(clip->ibuf_cache)
396                 BKE_moviecache_get_cache_segments(clip->ibuf_cache, totseg_r, points_r);
397 }
398
399 void BKE_movieclip_user_set_frame(MovieClipUser *iuser, int framenr)
400 {
401         /* TODO: clamp framenr here? */
402
403         iuser->framenr= framenr;
404 }
405
406 static void free_buffers(MovieClip *clip)
407 {
408         if(clip->ibuf_cache) {
409                 BKE_moviecache_free(clip->ibuf_cache);
410                 clip->ibuf_cache= NULL;
411         }
412
413         if(clip->anim) {
414                 IMB_free_anim(clip->anim);
415                 clip->anim= FALSE;
416         }
417 }
418
419 void BKE_movieclip_reload(MovieClip *clip)
420 {
421         /* clear cache */
422         free_buffers(clip);
423
424         clip->tracking.stabilization.ok= 0;
425         clip->tracking.stabilization.ibufok= 0;
426
427         /* update clip source */
428         if(BLI_testextensie_array(clip->name, imb_ext_movie)) clip->source= MCLIP_SRC_MOVIE;
429         else clip->source= MCLIP_SRC_SEQUENCE;
430 }
431
432 /* area - which part of marker should be selected. see TRACK_AREA_* constants */
433 void BKE_movieclip_select_track(MovieClip *clip, MovieTrackingTrack *track, int area, int extend)
434 {
435         if(extend) {
436                 BKE_tracking_track_flag(track, area, SELECT, 0);
437         } else {
438                 MovieTrackingTrack *cur= clip->tracking.tracks.first;
439
440                 while(cur) {
441                         if(cur==track) {
442                                 BKE_tracking_track_flag(cur, TRACK_AREA_ALL, SELECT, 1);
443                                 BKE_tracking_track_flag(cur, area, SELECT, 0);
444                         }
445                         else {
446                                 BKE_tracking_track_flag(cur, TRACK_AREA_ALL, SELECT, 1);
447                         }
448
449                         cur= cur->next;
450                 }
451         }
452
453         if(!TRACK_SELECTED(track))
454                 BKE_movieclip_set_selection(clip, MCLIP_SEL_NONE, NULL);
455 }
456
457 void BKE_movieclip_deselect_track(MovieClip *clip, MovieTrackingTrack *track, int area)
458 {
459         BKE_tracking_track_flag(track, area, SELECT, 1);
460
461         if(!TRACK_SELECTED(track))
462                 BKE_movieclip_set_selection(clip, MCLIP_SEL_NONE, NULL);
463 }
464
465 void BKE_movieclip_set_selection(MovieClip *clip, int type, void *sel)
466 {
467         clip->sel_type= type;
468
469         if(type == MCLIP_SEL_NONE) clip->last_sel= NULL;
470         else clip->last_sel= sel;
471 }
472
473 void BKE_movieclip_last_selection(MovieClip *clip, int *type, void **sel)
474 {
475         *type= clip->sel_type;
476
477         if(clip->sel_type == MCLIP_SEL_NONE) *sel= NULL;
478         else *sel= clip->last_sel;
479 }
480
481 void BKE_movieclip_update_scopes(MovieClip *clip, MovieClipUser *user, MovieClipScopes *scopes)
482 {
483         void *sel;
484         int sel_type;
485
486         if(scopes->ok) return;
487
488         if(scopes->track_preview) {
489                 IMB_freeImBuf(scopes->track_preview);
490                 scopes->track_preview= NULL;
491         }
492
493         scopes->marker= NULL;
494         scopes->track= NULL;
495
496         if(clip) {
497                 BKE_movieclip_last_selection(clip, &sel_type, &sel);
498
499                 if(sel_type==MCLIP_SEL_TRACK) {
500                         MovieTrackingTrack *track= (MovieTrackingTrack*)sel;
501                         MovieTrackingMarker *marker= BKE_tracking_get_marker(track, user->framenr);
502
503                         if(marker->flag&MARKER_DISABLED) {
504                                 scopes->track_disabled= 1;
505                         } else {
506                                 ImBuf *ibuf= BKE_movieclip_acquire_ibuf(clip, user);
507
508                                 scopes->track_disabled= 0;
509
510                                 if(ibuf && ibuf->rect) {
511                                         ImBuf *tmpibuf;
512
513                                         tmpibuf= BKE_tracking_acquire_pattern_imbuf(ibuf, track, marker, 1, scopes->track_pos, NULL);
514
515                                         if(tmpibuf->rect_float)
516                                                 IMB_rect_from_float(tmpibuf);
517
518                                         if(tmpibuf->rect)
519                                                 scopes->track_preview= tmpibuf;
520                                         else
521                                                 IMB_freeImBuf(tmpibuf);
522                                 }
523
524                                 IMB_freeImBuf(ibuf);
525                         }
526
527                         if((track->flag&TRACK_LOCKED)==0) {
528                                 scopes->marker= marker;
529                                 scopes->track= track;
530                                 scopes->slide_scale[0]= track->pat_max[0]-track->pat_min[0];
531                                 scopes->slide_scale[1]= track->pat_max[1]-track->pat_min[1];
532                         }
533                 }
534         }
535
536         scopes->framenr= user->framenr;
537         scopes->ok= 1;
538 }
539
540 void free_movieclip(MovieClip *clip)
541 {
542         free_buffers(clip);
543
544         BKE_tracking_free(&clip->tracking);
545 }
546
547 void unlink_movieclip(Main *bmain, MovieClip *clip)
548 {
549         bScreen *scr;
550         ScrArea *area;
551         SpaceLink *sl;
552
553         /* text space */
554         for(scr= bmain->screen.first; scr; scr= scr->id.next) {
555                 for(area= scr->areabase.first; area; area= area->next) {
556                         for(sl= area->spacedata.first; sl; sl= sl->next) {
557                                 if(sl->spacetype==SPACE_CLIP) {
558                                         SpaceClip *sc= (SpaceClip*) sl;
559
560                                         if(sc->clip==clip) {
561                                                 sc->clip= NULL;
562                                         }
563                                 }
564                         }
565                 }
566         }
567
568         clip->id.us= 0;
569 }