quiet warnings when building without libmv
[blender.git] / source / blender / blenkernel / intern / tracking.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2011 Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation,
22  *                 Sergey Sharybin
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/blenkernel/intern/tracking.c
28  *  \ingroup bke
29  */
30
31 #include <stddef.h>
32 #include <limits.h>
33 #include <math.h>
34 #include <memory.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_gpencil_types.h"
39 #include "DNA_camera_types.h"
40 #include "DNA_movieclip_types.h"
41 #include "DNA_object_types.h"   /* SELECT */
42 #include "DNA_scene_types.h"
43
44 #include "BLI_utildefines.h"
45 #include "BLI_math.h"
46 #include "BLI_math_base.h"
47 #include "BLI_listbase.h"
48 #include "BLI_ghash.h"
49 #include "BLI_path_util.h"
50
51 #include "BKE_global.h"
52 #include "BKE_tracking.h"
53 #include "BKE_movieclip.h"
54 #include "BKE_object.h"
55 #include "BKE_scene.h"
56
57 #include "IMB_imbuf_types.h"
58 #include "IMB_imbuf.h"
59
60 #ifdef WITH_LIBMV
61 #  include "libmv-capi.h"
62 #else
63 struct libmv_Features;
64 #endif
65
66 typedef struct MovieDistortion {
67         struct libmv_CameraIntrinsics *intrinsics;
68 } MovieDistortion;
69
70 /*********************** common functions *************************/
71
72 void BKE_tracking_clamp_track(MovieTrackingTrack *track, int event)
73 {
74         int a;
75         float pat_min[2];
76         float pat_max[2];
77         float max_pyramid_level_factor = 1.0;
78         if (track->tracker == TRACKER_KLT) {
79                 max_pyramid_level_factor = 1 << (track->pyramid_levels - 1);
80         }
81
82         /* sort */
83         for(a= 0; a<2; a++) {
84                 if(track->pat_min[a]>track->pat_max[a])
85                         SWAP(float, track->pat_min[a], track->pat_max[a]);
86
87                 if(track->search_min[a]>track->search_max[a])
88                         SWAP(float, track->search_min[a], track->search_max[a]);
89         }
90
91         /* compute the effective pattern size, which differs from the fine resolution
92          * pattern size for the pyramid KLT tracker */
93         for(a= 0; a<2; a++) {
94                 pat_min[a] = max_pyramid_level_factor * track->pat_min[a];
95                 pat_max[a] = max_pyramid_level_factor * track->pat_max[a];
96         }
97
98         if(event==CLAMP_PAT_DIM) {
99                 for(a= 0; a<2; a++) {
100                         /* search shouldn't be resized smaller than pattern */
101                         track->search_min[a]= MIN2(pat_min[a], track->search_min[a]);
102                         track->search_max[a]= MAX2(pat_max[a], track->search_max[a]);
103                 }
104         }
105         else if(event==CLAMP_PAT_POS) {
106                 float dim[2];
107                 sub_v2_v2v2(dim, track->pat_max, pat_min);
108
109                 for(a= 0; a<2; a++) {
110                         /* pattern shouldn't be moved outside of search */
111                         if(pat_min[a] < track->search_min[a]) {
112                                 track->pat_min[a]= track->search_min[a] - (pat_min[a] - track->pat_min[a]);
113                                 track->pat_max[a]=  (pat_min[a] - track->pat_min[a])+dim[a];
114                         }
115                         if(track->pat_max[a] > track->search_max[a]) {
116                                 track->pat_max[a]= track->search_max[a] - (pat_max[a] - track->pat_max[a]);
117                                 track->pat_min[a]= track->pat_max[a]-dim[a] - (pat_min[a] - track->pat_min[a]);
118                         }
119                 }
120         }
121         else if(event==CLAMP_SEARCH_DIM) {
122                 float max_pyramid_level_factor = 1.0;
123                 if (track->tracker == TRACKER_KLT) {
124                         max_pyramid_level_factor = 1 << (track->pyramid_levels - 1);
125                 }
126                 for(a= 0; a<2; a++) {
127                         /* search shouldn't be resized smaller than pattern */
128                         track->search_min[a]= MIN2(pat_min[a], track->search_min[a]);
129                         track->search_max[a]= MAX2(pat_max[a], track->search_max[a]);
130                 }
131         }
132         else if(event==CLAMP_SEARCH_POS) {
133                 float dim[2];
134                 sub_v2_v2v2(dim, track->search_max, track->search_min);
135
136                 for(a= 0; a<2; a++) {
137                         /* search shouldn't be moved inside pattern */
138                         if(track->search_min[a] > pat_min[a]) {
139                                 track->search_min[a]= pat_min[a];
140                                 track->search_max[a]= track->search_min[a]+dim[a];
141                         }
142                         if(track->search_max[a] < pat_max[a]) {
143                                 track->search_max[a]= pat_max[a];
144                                 track->search_min[a]= track->search_max[a]-dim[a];
145                         }
146                 }
147         }
148
149         else if(event==CLAMP_PYRAMID_LEVELS || (event==CLAMP_SEARCH_DIM && track->tracker == TRACKER_KLT))  {
150                 float dim[2];
151                 sub_v2_v2v2(dim, track->pat_max, track->pat_min);
152                 {
153                         float search_ratio = 2.3f * max_pyramid_level_factor;
154
155                         /* resize the search area to something sensible based
156                          * on the number of pyramid levels */
157                         for(a= 0; a<2; a++) {
158                                 track->search_min[a]= search_ratio * track->pat_min[a];
159                                 track->search_max[a]= search_ratio * track->pat_max[a];
160                         }
161                 }
162         }
163
164         /* marker's center should be in center of pattern */
165         if(event==CLAMP_PAT_DIM || event==CLAMP_PAT_POS) {
166                 float dim[2];
167                 sub_v2_v2v2(dim, track->pat_max, track->pat_min);
168
169                 for(a= 0; a<2; a++) {
170                         track->pat_min[a]= -dim[a]/2.0f;
171                         track->pat_max[a]= dim[a]/2.0f;
172                 }
173         }
174 }
175
176 void BKE_tracking_track_flag(MovieTrackingTrack *track, int area, int flag, int clear)
177 {
178         if(area==TRACK_AREA_NONE)
179                 return;
180
181         if(clear) {
182                 if(area&TRACK_AREA_POINT)       track->flag&= ~flag;
183                 if(area&TRACK_AREA_PAT)         track->pat_flag&= ~flag;
184                 if(area&TRACK_AREA_SEARCH)      track->search_flag&= ~flag;
185         } else {
186                 if(area&TRACK_AREA_POINT)       track->flag|= flag;
187                 if(area&TRACK_AREA_PAT)         track->pat_flag|= flag;
188                 if(area&TRACK_AREA_SEARCH)      track->search_flag|= flag;
189         }
190 }
191
192 MovieTrackingTrack *BKE_tracking_add_track(MovieTracking *tracking, float x, float y,
193                         int framenr, int width, int height)
194 {
195         MovieTrackingTrack *track;
196         MovieTrackingMarker marker;
197
198         /* pick reasonable defaults */
199         float pat[2]= {5.5f, 5.5f}, search[2]= {25.5f, 25.5f}; /* TODO: move to default setting? */
200
201         pat[0] /= (float)width;
202         pat[1] /= (float)height;
203
204         search[0] /= (float)width;
205         search[1] /= (float)height;
206
207         track= MEM_callocN(sizeof(MovieTrackingTrack), "add_marker_exec track");
208         strcpy(track->name, "Track");
209
210         /* default to KLT tracker */
211         track->tracker = TRACKER_KLT;
212         track->pyramid_levels = 2;
213
214         /* set SAD defaults even though it's not selected by default */
215         track->minimum_correlation= 0.75f;
216
217         memset(&marker, 0, sizeof(marker));
218         marker.pos[0]= x;
219         marker.pos[1]= y;
220         marker.framenr= framenr;
221
222         copy_v2_v2(track->pat_max, pat);
223         negate_v2_v2(track->pat_min, pat);
224
225         copy_v2_v2(track->search_max, search);
226         negate_v2_v2(track->search_min, search);
227
228         BKE_tracking_insert_marker(track, &marker);
229
230         BLI_addtail(&tracking->tracks, track);
231         BKE_track_unique_name(tracking, track);
232
233         return track;
234 }
235
236 void BKE_tracking_insert_marker(MovieTrackingTrack *track, MovieTrackingMarker *marker)
237 {
238         MovieTrackingMarker *old_marker= BKE_tracking_get_marker(track, marker->framenr);
239
240         if(old_marker && old_marker->framenr==marker->framenr) {
241                 *old_marker= *marker;
242         } else {
243                 int a= track->markersnr;
244
245                 while(a--) {
246                         if(track->markers[a].framenr<marker->framenr)
247                                 break;
248                 }
249
250                 track->markersnr++;
251
252                 if(track->markers) track->markers= MEM_reallocN(track->markers, sizeof(MovieTrackingMarker)*track->markersnr);
253                 else track->markers= MEM_callocN(sizeof(MovieTrackingMarker), "MovieTracking markers");
254
255                 memmove(track->markers+a+2, track->markers+a+1, (track->markersnr-a-2)*sizeof(MovieTrackingMarker));
256                 track->markers[a+1]= *marker;
257
258                 track->last_marker= a+1;
259         }
260 }
261
262 void BKE_tracking_delete_marker(MovieTrackingTrack *track, int framenr)
263 {
264         int a= 0;
265
266         while(a<track->markersnr) {
267                 if(track->markers[a].framenr==framenr) {
268                         if(track->markersnr>1) {
269                                 memmove(track->markers+a, track->markers+a+1, (track->markersnr-a-1)*sizeof(MovieTrackingMarker));
270                                 track->markersnr--;
271                                 track->markers= MEM_reallocN(track->markers, sizeof(MovieTrackingMarker)*track->markersnr);
272                         } else {
273                                 MEM_freeN(track->markers);
274                                 track->markers= NULL;
275                                 track->markersnr= 0;
276                         }
277
278                         break;
279                 }
280
281                 a++;
282         }
283 }
284
285 MovieTrackingMarker *BKE_tracking_get_marker(MovieTrackingTrack *track, int framenr)
286 {
287         int a= track->markersnr-1;
288
289         if(!track->markersnr)
290                 return NULL;
291
292         /* approximate pre-first framenr marker with first marker */
293         if(framenr<track->markers[0].framenr)
294                 return &track->markers[0];
295
296         if(track->last_marker<track->markersnr)
297                 a= track->last_marker;
298
299         if(track->markers[a].framenr<=framenr) {
300                 while(a<track->markersnr && track->markers[a].framenr<=framenr) {
301                         if(track->markers[a].framenr==framenr) {
302                                 track->last_marker= a;
303                                 return &track->markers[a];
304                         }
305                         a++;
306                 }
307
308                 /* if there's no marker for exact position, use nearest marker from left side */
309                 return &track->markers[a-1];
310         } else {
311                 while(a>=0 && track->markers[a].framenr>=framenr) {
312                         if(track->markers[a].framenr==framenr) {
313                                 track->last_marker= a;
314                                 return &track->markers[a];
315                         }
316
317                         a--;
318                 }
319
320                 /* if there's no marker for exact position, use nearest marker from left side */
321                 return &track->markers[a];
322         }
323
324         return NULL;
325 }
326
327 MovieTrackingMarker *BKE_tracking_ensure_marker(MovieTrackingTrack *track, int framenr)
328 {
329         MovieTrackingMarker *marker= BKE_tracking_get_marker(track, framenr);
330
331         if(marker && marker->framenr!=framenr) {
332                 MovieTrackingMarker marker_new;
333
334                 marker_new= *marker;
335                 marker_new.framenr= framenr;
336
337                 BKE_tracking_insert_marker(track, &marker_new);
338                 marker= BKE_tracking_get_marker(track, framenr);
339         }
340
341         return marker;
342 }
343
344 MovieTrackingMarker *BKE_tracking_exact_marker(MovieTrackingTrack *track, int framenr)
345 {
346         MovieTrackingMarker *marker= BKE_tracking_get_marker(track, framenr);
347
348         if(marker && marker->framenr!=framenr)
349                 return NULL;
350
351         return marker;
352 }
353
354 int BKE_tracking_has_marker(MovieTrackingTrack *track, int framenr)
355 {
356         return BKE_tracking_exact_marker(track, framenr) != 0;
357 }
358
359 void BKE_tracking_free_track(MovieTrackingTrack *track)
360 {
361         if(track->markers) MEM_freeN(track->markers);
362 }
363
364 MovieTrackingTrack *BKE_tracking_copy_track(MovieTrackingTrack *track)
365 {
366         MovieTrackingTrack *new_track= MEM_dupallocN(track);
367
368         new_track->next= new_track->prev= NULL;
369
370         if(new_track->markers)
371                 new_track->markers= MEM_dupallocN(new_track->markers);
372
373         return new_track;
374 }
375
376 static void put_disabled_marker(MovieTrackingTrack *track, MovieTrackingMarker *ref_marker, int before, int overwrite)
377 {
378         MovieTrackingMarker marker_new;
379
380         marker_new= *ref_marker;
381         marker_new.flag&= ~MARKER_TRACKED;
382         marker_new.flag|= MARKER_DISABLED;
383
384         if(before) marker_new.framenr--;
385         else marker_new.framenr++;
386
387         if(!BKE_tracking_has_marker(track, marker_new.framenr) || overwrite)
388                 BKE_tracking_insert_marker(track, &marker_new);
389 }
390
391 void BKE_tracking_clear_path(MovieTrackingTrack *track, int ref_frame, int action)
392 {
393         int a;
394
395         if(action==TRACK_CLEAR_REMAINED) {
396                 a= 1;
397                 while(a<track->markersnr) {
398                         if(track->markers[a].framenr>ref_frame) {
399                                 track->markersnr= a;
400                                 track->markers= MEM_reallocN(track->markers, sizeof(MovieTrackingMarker)*track->markersnr);
401
402                                 break;
403                         }
404
405                         a++;
406                 }
407
408                 if(track->markersnr)
409                         put_disabled_marker(track, &track->markers[track->markersnr-1], 0, 1);
410         } else if(action==TRACK_CLEAR_UPTO) {
411                 a= track->markersnr-1;
412                 while(a>=0) {
413                         if(track->markers[a].framenr<=ref_frame) {
414                                 memmove(track->markers, track->markers+a, (track->markersnr-a)*sizeof(MovieTrackingMarker));
415
416                                 track->markersnr= track->markersnr-a;
417                                 track->markers= MEM_reallocN(track->markers, sizeof(MovieTrackingMarker)*track->markersnr);
418
419                                 break;
420                         }
421
422                         a--;
423                 }
424
425                 if(track->markersnr)
426                         put_disabled_marker(track, &track->markers[0], 1, 1);
427         } else if(action==TRACK_CLEAR_ALL) {
428                 MovieTrackingMarker *marker, marker_new;
429
430                 marker= BKE_tracking_get_marker(track, ref_frame);
431                 marker_new= *marker;
432
433                 MEM_freeN(track->markers);
434                 track->markers= NULL;
435                 track->markersnr= 0;
436
437                 BKE_tracking_insert_marker(track, &marker_new);
438
439                 put_disabled_marker(track, &marker_new, 1, 1);
440                 put_disabled_marker(track, &marker_new, 0, 1);
441         }
442 }
443
444 int BKE_tracking_test_join_tracks(MovieTrackingTrack *dst_track, MovieTrackingTrack *src_track)
445 {
446         int i, a= 0, b= 0, tot= dst_track->markersnr+src_track->markersnr;
447         int count= 0;
448
449         for(i= 0; i<tot; i++) {
450                 if(a>=src_track->markersnr) {
451                         b++;
452                         count++;
453                 }
454                 else if(b>=dst_track->markersnr) {
455                         a++;
456                         count++;
457                 }
458                 else if(src_track->markers[a].framenr<dst_track->markers[b].framenr) {
459                         a++;
460                         count++;
461                 } else if(src_track->markers[a].framenr>dst_track->markers[b].framenr) {
462                         b++;
463                         count++;
464                 } else {
465                         if((src_track->markers[a].flag&MARKER_DISABLED)==0 && (dst_track->markers[b].flag&MARKER_DISABLED)==0)
466                                 return 0;
467
468                         a++;
469                         b++;
470                         count++;
471                 }
472         }
473
474         return count;
475 }
476
477 void BKE_tracking_join_tracks(MovieTrackingTrack *dst_track, MovieTrackingTrack *src_track)
478 {
479         int i, a= 0, b= 0, tot;
480         MovieTrackingMarker *markers;
481
482         tot= BKE_tracking_test_join_tracks(dst_track, src_track);
483
484         markers= MEM_callocN(tot*sizeof(MovieTrackingMarker), "tracking joined tracks");
485
486         for(i= 0; i<tot; i++) {
487                 if(b>=dst_track->markersnr) {
488                         markers[i]= src_track->markers[a++];
489                 }
490                 else if(a>=src_track->markersnr) {
491                         markers[i]= dst_track->markers[b++];
492                 }
493                 else if(src_track->markers[a].framenr<dst_track->markers[b].framenr) {
494                         markers[i]= src_track->markers[a++];
495                 } else if(src_track->markers[a].framenr>dst_track->markers[b].framenr) {
496                         markers[i]= dst_track->markers[b++];
497                 } else {
498                         if((src_track->markers[a].flag&MARKER_DISABLED)) markers[i]= dst_track->markers[b];
499                         else markers[i]= src_track->markers[a++];
500
501                         a++;
502                         b++;
503                 }
504         }
505
506         MEM_freeN(dst_track->markers);
507
508         dst_track->markers= markers;
509         dst_track->markersnr= tot;
510 }
511
512 void BKE_tracking_free(MovieTracking *tracking)
513 {
514         MovieTrackingTrack *track;
515
516         for(track= tracking->tracks.first; track; track= track->next) {
517                 BKE_tracking_free_track(track);
518         }
519
520         BLI_freelistN(&tracking->tracks);
521
522         if(tracking->reconstruction.cameras)
523                 MEM_freeN(tracking->reconstruction.cameras);
524
525         if(tracking->stabilization.scaleibuf)
526                 IMB_freeImBuf(tracking->stabilization.scaleibuf);
527
528         if(tracking->camera.intrinsics)
529                 BKE_tracking_distortion_destroy(tracking->camera.intrinsics);
530 }
531
532 /*********************** tracking *************************/
533
534 typedef struct TrackContext {
535         MovieTrackingTrack *track;
536
537 #ifdef WITH_LIBMV
538         float keyframed_pos[2];
539
540         /* ** KLT tracker ** */
541         struct libmv_RegionTracker *region_tracker;
542         float *patch;                   /* keyframed patch */
543
544         /* ** SAD tracker ** */
545         int patsize;                    /* size of pattern (currently only 16x16 due to libmv side) */
546         unsigned char *pattern; /* keyframed pattern */
547         unsigned char *warped;  /* warped version of reference */
548 #endif
549 } TrackContext;
550
551 typedef struct MovieTrackingContext {
552         MovieClipUser user;
553         MovieClip *clip;
554
555         int first_time, frames;
556
557         TrackContext *track_context;
558         int num_tracks;
559
560         GHash *hash;
561         MovieTrackingSettings settings;
562
563         short backwards, disable_failed;
564         int sync_frame;
565 } MovieTrackingContext;
566
567 MovieTrackingContext *BKE_tracking_context_new(MovieClip *clip, MovieClipUser *user, short backwards, short disable_failed)
568 {
569         MovieTrackingContext *context= MEM_callocN(sizeof(MovieTrackingContext), "trackingContext");
570         MovieTracking *tracking= &clip->tracking;
571         MovieTrackingSettings *settings= &tracking->settings;
572         MovieTrackingTrack *track;
573         TrackContext *track_context;
574
575         context->settings= *settings;
576         context->backwards= backwards;
577         context->disable_failed= disable_failed;
578         context->hash= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "tracking trackHash");
579         context->sync_frame= user->framenr;
580         context->first_time= 1;
581
582         /* count */
583         track= tracking->tracks.first;
584         while(track) {
585                 if(TRACK_SELECTED(track) && (track->flag&TRACK_LOCKED)==0) {
586                         MovieTrackingMarker *marker= BKE_tracking_get_marker(track, user->framenr);
587
588                         if((marker->flag&MARKER_DISABLED)==0)
589                                 context->num_tracks++;
590                 }
591
592                 track= track->next;
593         }
594
595         if(context->num_tracks) {
596                 int width, height;
597
598                 BKE_movieclip_get_size(clip, user, &width, &height);
599
600                 /* create tracking data */
601                 context->track_context= MEM_callocN(sizeof(TrackContext)*context->num_tracks, "tracking track_context");
602
603                 track_context= context->track_context;
604                 track= tracking->tracks.first;
605                 while(track) {
606                         if(TRACK_SELECTED(track) && (track->flag&TRACK_LOCKED)==0) {
607                                 MovieTrackingMarker *marker= BKE_tracking_get_marker(track, user->framenr);
608
609                                 if((marker->flag&MARKER_DISABLED)==0) {
610                                         MovieTrackingTrack *new_track= BKE_tracking_copy_track(track);
611
612                                         track_context->track= new_track;
613 #ifdef WITH_LIBMV
614                                         {
615                                                 if(track_context->track->tracker==TRACKER_KLT) {
616                                                         float search_size_x= (track->search_max[0]-track->search_min[0])*width;
617                                                         float search_size_y= (track->search_max[1]-track->search_min[1])*height;
618                                                         float pattern_size_x= (track->pat_max[0]-track->pat_min[0])*width;
619                                                         float pattern_size_y= (track->pat_max[1]-track->pat_min[1])*height;
620
621                                                         /* compute the maximum pyramid size */
622                                                         double search_to_pattern_ratio= MIN2(search_size_x,  search_size_y)
623                                                                 / MAX2(pattern_size_x, pattern_size_y);
624                                                         double log2_search_to_pattern_ratio = log(floor(search_to_pattern_ratio)) / M_LN2;
625                                                         int max_pyramid_levels= floor(log2_search_to_pattern_ratio + 1);
626
627                                                         /* try to accomodate the user's choice of pyramid level in a way
628                                                          * that doesn't cause the coarsest pyramid pattern to be larger
629                                                          * than the search size */
630                                                         int level= MIN2(track_context->track->pyramid_levels, max_pyramid_levels);
631                                                         track_context->region_tracker= libmv_regionTrackerNew(100, level);
632                                                 }
633                                                 else if(track_context->track->tracker==TRACKER_SAD) {
634                                                         /* nothing to initialize */
635                                                 }
636                                         }
637 #endif
638
639                                         BLI_ghash_insert(context->hash, new_track, track);
640
641                                         track_context++;
642                                 }
643                         }
644
645                         track= track->next;
646                 }
647         }
648
649         context->clip= clip;
650         context->user= *user;
651
652         return context;
653 }
654
655 void BKE_tracking_context_free(MovieTrackingContext *context)
656 {
657         int a;
658         TrackContext *track_context;
659
660         for(a= 0, track_context= context->track_context; a<context->num_tracks; a++, track_context++) {
661                 BKE_tracking_free_track(context->track_context[a].track);
662
663 #if WITH_LIBMV
664                 if(track_context->region_tracker)
665                         libmv_regionTrackerDestroy(track_context->region_tracker);
666
667                 if(track_context->patch)
668                         MEM_freeN(track_context->patch);
669
670                 if(track_context->pattern)
671                         MEM_freeN(track_context->pattern);
672
673                 if(track_context->warped)
674                         MEM_freeN(track_context->warped);
675 #endif
676
677                 MEM_freeN(track_context->track);
678         }
679
680         if(context->track_context)
681                 MEM_freeN(context->track_context);
682
683         BLI_ghash_free(context->hash, NULL, NULL);
684
685         MEM_freeN(context);
686 }
687
688 static void disable_imbuf_channels(ImBuf *ibuf, MovieTrackingTrack *track)
689 {
690         int x, y;
691
692         if((track->flag&(TRACK_DISABLE_RED|TRACK_DISABLE_GREEN|TRACK_DISABLE_BLUE))==0)
693                 return;
694
695         for(y= 0; y<ibuf->y; y++) {
696                 for (x= 0; x<ibuf->x; x++) {
697                         int pixel= ibuf->x*y + x;
698
699                         if(ibuf->rect_float) {
700                                 float *rrgbf= ibuf->rect_float + pixel*4;
701
702                                 if(track->flag&TRACK_DISABLE_RED)       rrgbf[0]= 0;
703                                 if(track->flag&TRACK_DISABLE_GREEN)     rrgbf[1]= 0;
704                                 if(track->flag&TRACK_DISABLE_BLUE)      rrgbf[2]= 0;
705                         } else {
706                                 char *rrgb= (char*)ibuf->rect + pixel*4;
707
708                                 if(track->flag&TRACK_DISABLE_RED)       rrgb[0]= 0;
709                                 if(track->flag&TRACK_DISABLE_GREEN)     rrgb[1]= 0;
710                                 if(track->flag&TRACK_DISABLE_BLUE)      rrgb[2]= 0;
711                         }
712                 }
713         }
714 }
715
716 static ImBuf *get_area_imbuf(ImBuf *ibuf, MovieTrackingTrack *track, MovieTrackingMarker *marker,
717                         float min[2], float max[2], int margin, int anchored, float pos[2], int origin[2])
718 {
719         ImBuf *tmpibuf;
720         int x, y;
721         int x1, y1, x2, y2, w, h;
722         float mpos[2];
723
724         copy_v2_v2(mpos, marker->pos);
725         if(anchored)
726                 add_v2_v2(mpos, track->offset);
727
728         x= mpos[0]*ibuf->x;
729         y= mpos[1]*ibuf->y;
730         x1= x-(int)(-min[0]*ibuf->x);
731         y1= y-(int)(-min[1]*ibuf->y);
732         x2= x+(int)(max[0]*ibuf->x);
733         y2= y+(int)(max[1]*ibuf->y);
734
735         /* dimensions should be odd */
736         w= (x2-x1)|1;
737         h= (y2-y1)|1;
738
739         tmpibuf= IMB_allocImBuf(w+margin*2, h+margin*2, 32, IB_rect);
740         IMB_rectcpy(tmpibuf, ibuf, 0, 0, x1-margin, y1-margin, w+margin*2, h+margin*2);
741
742         if(pos != NULL) {
743                 pos[0]= mpos[0]*ibuf->x-x1+margin;
744                 pos[1]= mpos[1]*ibuf->y-y1+margin;
745         }
746
747         if(origin != NULL) {
748                 origin[0]= x1-margin;
749                 origin[1]= y1-margin;
750         }
751
752         disable_imbuf_channels(tmpibuf, track);
753
754         return tmpibuf;
755 }
756
757 ImBuf *BKE_tracking_get_pattern_imbuf(ImBuf *ibuf, MovieTrackingTrack *track, MovieTrackingMarker *marker,
758                         int margin, int anchored, float pos[2], int origin[2])
759 {
760         return get_area_imbuf(ibuf, track, marker, track->pat_min, track->pat_max, margin, anchored, pos, origin);
761 }
762
763 ImBuf *BKE_tracking_get_search_imbuf(ImBuf *ibuf, MovieTrackingTrack *track, MovieTrackingMarker *marker,
764                         int margin, int anchored, float pos[2], int origin[2])
765 {
766         return get_area_imbuf(ibuf, track, marker, track->search_min, track->search_max, margin, anchored, pos, origin);
767 }
768
769 #ifdef WITH_LIBMV
770 static float *get_search_floatbuf(ImBuf *ibuf, MovieTrackingTrack *track, MovieTrackingMarker *marker,
771                         int *width_r, int *height_r, float pos[2], int origin[2])
772 {
773         ImBuf *tmpibuf;
774         float *pixels, *fp;
775         int x, y, width, height;
776
777         width= (track->search_max[0]-track->search_min[0])*ibuf->x;
778         height= (track->search_max[1]-track->search_min[1])*ibuf->y;
779
780         tmpibuf= BKE_tracking_get_search_imbuf(ibuf, track, marker, 0, 0, pos, origin);
781         disable_imbuf_channels(tmpibuf, track);
782
783         *width_r= width;
784         *height_r= height;
785
786         fp= pixels= MEM_callocN(width*height*sizeof(float), "tracking floatBuf");
787         for(y= 0; y<(int)height; y++) {
788                 for (x= 0; x<(int)width; x++) {
789                         int pixel= tmpibuf->x*y + x;
790
791                         if(tmpibuf->rect_float) {
792                                 float *rrgbf= tmpibuf->rect_float + pixel*4;
793
794                                 *fp= 0.2126*rrgbf[0] + 0.7152*rrgbf[1] + 0.0722*rrgbf[2];
795                         } else {
796                                 unsigned char *rrgb= (unsigned char*)tmpibuf->rect + pixel*4;
797
798                                 *fp= (0.2126*rrgb[0] + 0.7152*rrgb[1] + 0.0722*rrgb[2])/255.0f;
799                         }
800
801                         fp++;
802                 }
803         }
804
805         IMB_freeImBuf(tmpibuf);
806
807         return pixels;
808 }
809
810 static unsigned char *get_ucharbuf(ImBuf *ibuf)
811 {
812         int x, y;
813         unsigned char *pixels, *cp;
814
815         cp= pixels= MEM_callocN(ibuf->x*ibuf->y*sizeof(unsigned char), "tracking ucharBuf");
816         for(y= 0; y<ibuf->y; y++) {
817                 for (x= 0; x<ibuf->x; x++) {
818                         int pixel= ibuf->x*y + x;
819
820                         if(ibuf->rect_float) {
821                                 float *rrgbf= ibuf->rect_float + pixel*4;
822
823                                 *cp= FTOCHAR(0.2126f*rrgbf[0] + 0.7152f*rrgbf[1] + 0.0722f*rrgbf[2]);
824                         } else {
825                                 unsigned char *rrgb= (unsigned char*)ibuf->rect + pixel*4;
826
827                                 *cp= 0.2126f*rrgb[0] + 0.7152f*rrgb[1] + 0.0722f*rrgb[2];
828                         }
829
830                         cp++;
831                 }
832         }
833
834         return pixels;
835 }
836
837 static unsigned char *get_search_bytebuf(ImBuf *ibuf, MovieTrackingTrack *track, MovieTrackingMarker *marker,
838                         int *width_r, int *height_r, float pos[2], int origin[2])
839 {
840         ImBuf *tmpibuf;
841         unsigned char *pixels;
842
843         tmpibuf= BKE_tracking_get_search_imbuf(ibuf, track, marker, 0, 0, pos, origin);
844         disable_imbuf_channels(tmpibuf, track);
845
846         *width_r= tmpibuf->x;
847         *height_r= tmpibuf->y;
848
849         pixels= get_ucharbuf(tmpibuf);
850
851         IMB_freeImBuf(tmpibuf);
852
853         return pixels;
854 }
855
856 static ImBuf *get_frame_ibuf(MovieTrackingContext *context, int framenr)
857 {
858         ImBuf *ibuf;
859         int framenr_old= context->user.framenr;
860
861         context->user.framenr= framenr;
862
863         ibuf= BKE_movieclip_get_ibuf_flag(context->clip, &context->user, 0);
864
865         context->user.framenr= framenr_old;
866
867         return ibuf;
868 }
869
870 static ImBuf *get_keyframed_ibuf(MovieTrackingContext *context, MovieTrackingTrack *track,
871                         MovieTrackingMarker *marker, MovieTrackingMarker **marker_keyed)
872 {
873         int framenr= marker->framenr;
874         int a= marker-track->markers;
875
876         *marker_keyed= marker;
877
878         while(a>=0 && a<track->markersnr) {
879                 int next= (context->backwards) ? a+1 : a-1;
880                 int is_keyframed= 0;
881                 MovieTrackingMarker *marker= &track->markers[a];
882                 MovieTrackingMarker *next_marker= NULL;
883
884                 if(next>=0 && next<track->markersnr)
885                         next_marker= &track->markers[next];
886
887                 /* if next mrker is disabled, stop searching keyframe and use current frame as keyframe */
888                 if(next_marker && next_marker->flag&MARKER_DISABLED)
889                         is_keyframed= 1;
890
891                 is_keyframed|= (marker->flag&MARKER_TRACKED)==0;
892
893                 if(is_keyframed) {
894                         framenr= marker->framenr;
895                         *marker_keyed= marker;
896                         break;
897                 }
898
899                 a= next;
900         }
901
902         return get_frame_ibuf(context, framenr);
903 }
904
905 static ImBuf *get_adjust_ibuf(MovieTrackingContext *context, MovieTrackingTrack *track, MovieTrackingMarker *marker,
906                         int curfra, MovieTrackingMarker **marker_keyed)
907 {
908         ImBuf *ibuf= NULL;
909
910         if(context->settings.adjframes == 0) {
911                 ibuf= get_keyframed_ibuf(context, track, marker, marker_keyed);
912         } else {
913                 ibuf= get_frame_ibuf(context, curfra);
914
915                 /* use current marker as keyframed position */
916                 *marker_keyed= marker;
917         }
918
919         return ibuf;
920 }
921
922 static void get_warped(TrackContext *track_context, int x, int y, int width, unsigned char *image)
923 {
924         int i, j;
925
926         for(i=0; i<track_context->patsize; i++) {
927                 for(j=0; j<track_context->patsize; j++) {
928                         track_context->warped[i*track_context->patsize+j]=
929                                         image[(y+i-track_context->patsize/2)*width+x+j-track_context->patsize/2];
930                 }
931         }
932 }
933
934 #endif
935
936 void BKE_tracking_sync(MovieTrackingContext *context)
937 {
938         TrackContext *track_context;
939         MovieTracking *tracking= &context->clip->tracking;
940         MovieTrackingTrack *track;
941         ListBase tracks= {NULL, NULL}, new_tracks= {NULL, NULL};
942         ListBase *old_tracks= &context->clip->tracking.tracks;
943         int a, newframe;
944
945         /* duplicate currently tracking tracks to temporary list.
946            this is needed to keep names in unique state and it's faster to change names
947            of currently tracking tracks (if needed) */
948         for(a= 0, track_context= context->track_context; a<context->num_tracks; a++, track_context++) {
949                 int replace_sel= 0;
950                 MovieTrackingTrack *new_track, *old;
951
952                 track= track_context->track;
953
954                 /* find original of tracking track in list of previously displayed tracks */
955                 old= BLI_ghash_lookup(context->hash, track);
956                 if(old) {
957                         MovieTrackingTrack *cur= old_tracks->first;
958
959                         while(cur) {
960                                 if(cur==old)
961                                         break;
962
963                                 cur= cur->next;
964                         }
965
966                         /* original track was found, re-use flags and remove this track */
967                         if(cur) {
968                                 if(cur==tracking->act_track)
969                                         replace_sel= 1;
970
971                                 track->flag= cur->flag;
972                                 track->pat_flag= cur->pat_flag;
973                                 track->search_flag= cur->search_flag;
974
975                                 BKE_tracking_free_track(cur);
976                                 BLI_freelinkN(old_tracks, cur);
977                         }
978                 }
979
980                 new_track= BKE_tracking_copy_track(track);
981
982                 BLI_ghash_remove(context->hash, track, NULL, NULL); /* XXX: are we actually need this */
983                 BLI_ghash_insert(context->hash, track, new_track);
984
985                 if(replace_sel)         /* update current selection in clip */
986                         tracking->act_track= new_track;
987
988                 BLI_addtail(&tracks, new_track);
989         }
990
991         /* move all tracks, which aren't tracking */
992         track= old_tracks->first;
993         while(track) {
994                 MovieTrackingTrack *next= track->next;
995
996                 track->next= track->prev= NULL;
997                 BLI_addtail(&new_tracks, track);
998
999                 track= next;
1000         }
1001
1002         /* now move all tracks which are currently tracking and keep their names unique */
1003         track= tracks.first;
1004         while(track) {
1005                 MovieTrackingTrack *next= track->next;
1006
1007                 BLI_remlink(&tracks, track);
1008
1009                 track->next= track->prev= NULL;
1010                 BLI_addtail(&new_tracks, track);
1011
1012                 BLI_uniquename(&new_tracks, track, "Track", '.', offsetof(MovieTrackingTrack, name), sizeof(track->name));
1013
1014                 track= next;
1015         }
1016
1017         context->clip->tracking.tracks= new_tracks;
1018
1019         if(context->backwards) newframe= context->user.framenr+1;
1020         else newframe= context->user.framenr-1;
1021
1022         context->sync_frame= newframe;
1023 }
1024
1025 void BKE_tracking_sync_user(MovieClipUser *user, MovieTrackingContext *context)
1026 {
1027         user->framenr= context->sync_frame;
1028 }
1029
1030 int BKE_tracking_next(MovieTrackingContext *context)
1031 {
1032         ImBuf *ibuf_new;
1033         int curfra= context->user.framenr;
1034         int a, ok= 0;
1035
1036         /* nothing to track, avoid unneeded frames reading to save time and memory */
1037         if(!context->num_tracks)
1038                 return 0;
1039
1040         if(context->backwards) context->user.framenr--;
1041         else context->user.framenr++;
1042
1043         ibuf_new= BKE_movieclip_get_ibuf_flag(context->clip, &context->user, 0);
1044         if(!ibuf_new)
1045                 return 0;
1046
1047         #pragma omp parallel for private(a) shared(ibuf_new, ok) if(context->num_tracks>1)
1048         for(a= 0; a<context->num_tracks; a++) {
1049                 TrackContext *track_context= &context->track_context[a];
1050                 MovieTrackingTrack *track= track_context->track;
1051                 MovieTrackingMarker *marker= BKE_tracking_get_marker(track, curfra);
1052
1053                 if(marker && (marker->flag&MARKER_DISABLED)==0 && marker->framenr==curfra) {
1054 #ifdef WITH_LIBMV
1055                         int width, height, origin[2], tracked= 0, need_readjust= 0;
1056                         float pos[2], margin[2];
1057                         double x1, y1, x2, y2;
1058                         ImBuf *ibuf= NULL;
1059                         MovieTrackingMarker marker_new, *marker_keyed;
1060                         int onbound= 0, coords_correct= 0;
1061                         int nextfra;
1062
1063                         if(!context->settings.adjframes) need_readjust= context->first_time;
1064                         else need_readjust= context->frames%context->settings.adjframes == 0;
1065
1066                         if(context->backwards) nextfra= curfra-1;
1067                         else nextfra= curfra+1;
1068
1069                         /* margin from frame boundaries */
1070                         sub_v2_v2v2(margin, track->pat_max, track->pat_min);
1071
1072                         margin[0]= MAX2(margin[0], (float)context->settings.margin / ibuf_new->x);
1073                         margin[1]= MAX2(margin[1], (float)context->settings.margin / ibuf_new->y);
1074
1075                         /* do not track markers which are too close to boundary */
1076                         if(marker->pos[0]<margin[0] || marker->pos[0]>1.0f-margin[0] ||
1077                            marker->pos[1]<margin[1] || marker->pos[1]>1.0f-margin[1]) {
1078                                 onbound= 1;
1079                         }
1080                         else if(track_context->track->tracker==TRACKER_KLT) {
1081                                 int wndx, wndy;
1082                                 float *patch_new;
1083
1084                                 if(need_readjust) {
1085                                         /* calculate patch for keyframed position */
1086                                         ibuf= get_adjust_ibuf(context, track, marker, curfra, &marker_keyed);
1087
1088                                         if(track_context->patch)
1089                                                 MEM_freeN(track_context->patch);
1090
1091                                         track_context->patch= get_search_floatbuf(ibuf, track, marker_keyed, &width, &height, track_context->keyframed_pos, origin);
1092
1093                                         IMB_freeImBuf(ibuf);
1094                                 }
1095
1096                                 patch_new= get_search_floatbuf(ibuf_new, track, marker, &width, &height, pos, origin);
1097
1098                                 x1= track_context->keyframed_pos[0];
1099                                 y1= track_context->keyframed_pos[1];
1100
1101                                 x2= pos[0];
1102                                 y2= pos[1];
1103
1104                                 wndx= (int)((track->pat_max[0]-track->pat_min[0])*ibuf_new->x)/2;
1105                                 wndy= (int)((track->pat_max[1]-track->pat_min[1])*ibuf_new->y)/2;
1106
1107                                 tracked= libmv_regionTrackerTrack(track_context->region_tracker, track_context->patch, patch_new,
1108                                                         width, height, MAX2(wndx, wndy), x1, y1, &x2, &y2);
1109
1110                                 MEM_freeN(patch_new);
1111                         }
1112                         else if(track_context->track->tracker==TRACKER_SAD) {
1113                                 unsigned char *image_new;
1114                                 float correlation;
1115                                 float warp[3][2]={{0}};
1116
1117                                 if(need_readjust) {
1118                                         unsigned char *image;
1119
1120                                         /* calculate pattern for keyframed position */
1121                                         ibuf= get_adjust_ibuf(context, track, marker, curfra, &marker_keyed);
1122
1123                                         image= get_search_bytebuf(ibuf, track, marker_keyed, &width, &height, pos, origin);
1124
1125                                         memset(warp, 0, sizeof(warp));
1126                                         warp[0][0]= 1;
1127                                         warp[1][1]= 1;
1128                                         warp[2][0]= pos[0];
1129                                         warp[2][1]= pos[1];
1130
1131                                         /* pattern size is hardcoded to 16x16px in libmv */
1132                                         track_context->patsize= 16;
1133
1134                                         if(!track_context->pattern)
1135                                                 track_context->pattern= MEM_callocN(sizeof(unsigned char)*track_context->patsize*track_context->patsize, "trackking pattern");
1136
1137                                         libmv_SADSamplePattern(image, width, warp, track_context->pattern);
1138
1139                                         MEM_freeN(image);
1140                                         IMB_freeImBuf(ibuf);
1141                                 }
1142
1143                                 image_new= get_search_bytebuf(ibuf_new, track, marker, &width, &height, pos, origin);
1144
1145                                 if(track_context->warped==NULL) {
1146                                         unsigned char *image_old;
1147
1148                                         ibuf= get_frame_ibuf(context, curfra);
1149
1150                                         if(track_context->warped==NULL)
1151                                                 track_context->warped= MEM_callocN(sizeof(unsigned char)*track_context->patsize*track_context->patsize, "trackking warped");
1152
1153                                         image_old= get_search_bytebuf(ibuf, track, marker, &width, &height, pos, origin);
1154                                         get_warped(track_context, pos[0], pos[1], width, image_old);
1155                                         IMB_freeImBuf(ibuf);
1156                                         MEM_freeN(image_old);
1157                                 }
1158
1159                                 memset(warp, 0, sizeof(warp));
1160                                 warp[0][0]= 1;
1161                                 warp[1][1]= 1;
1162                                 warp[2][0]= pos[0];
1163                                 warp[2][1]= pos[1];
1164
1165                                 correlation= libmv_SADTrackerTrack(track_context->pattern, track_context->warped, image_new, width, width, height, warp);
1166
1167                                 x2= warp[2][0];
1168                                 y2= warp[2][1];
1169
1170                                 tracked= track_context->track->minimum_correlation < correlation;
1171
1172                                 if(tracked)
1173                                         get_warped(track_context, x2, y2, width, image_new);
1174
1175                                 MEM_freeN(image_new);
1176                         }
1177
1178                         coords_correct= !isnan(x2) && !isnan(y2) && finite(x2) && finite(y2);
1179                         if(coords_correct && (tracked || !context->disable_failed)) {
1180                                 if(context->first_time) {
1181                                         #pragma omp critical
1182                                         {
1183                                                 /* check if there's no keyframe/tracked markers before tracking marker.
1184                                                     if so -- create disabled marker before currently tracking "segment" */
1185                                                 put_disabled_marker(track, marker, 1, 0);
1186                                         }
1187                                 }
1188
1189                                 memset(&marker_new, 0, sizeof(marker_new));
1190
1191                                 if(!onbound) {
1192                                         marker_new.pos[0]= (origin[0]+x2)/ibuf_new->x;
1193                                         marker_new.pos[1]= (origin[1]+y2)/ibuf_new->y;
1194                                 } else {
1195                                         copy_v2_v2(marker_new.pos, marker->pos);
1196                                 }
1197
1198                                 marker_new.flag|= MARKER_TRACKED;
1199                                 marker_new.framenr= nextfra;
1200
1201                                 #pragma omp critical
1202                                 {
1203                                         BKE_tracking_insert_marker(track, &marker_new);
1204                                 }
1205
1206                                 /* make currently tracked segment be finished with disabled marker */
1207                                 #pragma omp critical
1208                                 {
1209                                         put_disabled_marker(track, &marker_new, 0, 0);
1210                                 }
1211                         } else {
1212                                 marker_new= *marker;
1213
1214                                 marker_new.framenr= nextfra;
1215                                 marker_new.flag|= MARKER_DISABLED;
1216
1217                                 #pragma omp critical
1218                                 {
1219                                         BKE_tracking_insert_marker(track, &marker_new);
1220                                 }
1221                         }
1222
1223                         ok= 1;
1224 #endif
1225                 }
1226         }
1227
1228         IMB_freeImBuf(ibuf_new);
1229
1230         context->first_time= 0;
1231         context->frames++;
1232
1233         return ok;
1234 }
1235
1236 #if WITH_LIBMV
1237 static struct libmv_Tracks *create_libmv_tracks(MovieTracking *tracking, int width, int height)
1238 {
1239         int tracknr= 0;
1240         MovieTrackingTrack *track;
1241         struct libmv_Tracks *tracks= libmv_tracksNew();
1242
1243         track= tracking->tracks.first;
1244         while(track) {
1245                 int a= 0;
1246
1247                 for(a= 0; a<track->markersnr; a++) {
1248                         MovieTrackingMarker *marker= &track->markers[a];
1249
1250                         if((marker->flag&MARKER_DISABLED)==0)
1251                                 libmv_tracksInsert(tracks, marker->framenr, tracknr,
1252                                                         marker->pos[0]*width, marker->pos[1]*height);
1253                 }
1254
1255                 track= track->next;
1256                 tracknr++;
1257         }
1258
1259         return tracks;
1260 }
1261
1262 static int retrieve_libmv_reconstruct(MovieTracking *tracking, struct libmv_Reconstruction *libmv_reconstruction)
1263 {
1264         int tracknr= 0;
1265         int sfra= INT_MAX, efra= INT_MIN, a, origin_set= 0;
1266         MovieTrackingTrack *track;
1267         MovieTrackingReconstruction *reconstruction= &tracking->reconstruction;
1268         MovieReconstructedCamera *reconstructed;
1269         float origin[3]= {0.0f, 0.0f, 0.0f};
1270         int ok= 1;
1271
1272         track= tracking->tracks.first;
1273         while(track) {
1274                 double pos[3];
1275
1276                 if(libmv_reporojectionPointForTrack(libmv_reconstruction, tracknr, pos)) {
1277                         track->bundle_pos[0]= pos[0];
1278                         track->bundle_pos[1]= pos[1];
1279                         track->bundle_pos[2]= pos[2];
1280
1281                         track->flag|= TRACK_HAS_BUNDLE;
1282                         track->error= libmv_reporojectionErrorForTrack(libmv_reconstruction, tracknr);
1283                 } else {
1284                         track->flag&= ~TRACK_HAS_BUNDLE;
1285                         ok= 0;
1286
1287                         printf("No bundle for track #%d '%s'\n", tracknr, track->name);
1288                 }
1289
1290                 if(track->markersnr) {
1291                         if(track->markers[0].framenr<sfra) sfra= track->markers[0].framenr;
1292                         if(track->markers[track->markersnr-1].framenr>efra) efra= track->markers[track->markersnr-1].framenr;
1293                 }
1294
1295                 track= track->next;
1296                 tracknr++;
1297         }
1298
1299         if(reconstruction->cameras)
1300                 MEM_freeN(reconstruction->cameras);
1301
1302         reconstruction->camnr= 0;
1303         reconstruction->cameras= NULL;
1304         reconstructed= MEM_callocN((efra-sfra+1)*sizeof(MovieReconstructedCamera), "temp reconstructed camera");
1305
1306         for(a= sfra; a<=efra; a++) {
1307                 double matd[4][4];
1308
1309                 if(libmv_reporojectionCameraForImage(libmv_reconstruction, a, matd)) {
1310                         int i, j;
1311                         float mat[4][4];
1312                         float error= libmv_reporojectionErrorForImage(libmv_reconstruction, a);
1313
1314                         for(i=0; i<4; i++)
1315                                 for(j= 0; j<4; j++)
1316                                         mat[i][j]= matd[i][j];
1317
1318                         if(!origin_set) {
1319                                 copy_v3_v3(origin, mat[3]);
1320                                 origin_set= 1;
1321                         }
1322
1323                         if(origin_set)
1324                                 sub_v3_v3(mat[3], origin);
1325
1326                         copy_m4_m4(reconstructed[reconstruction->camnr].mat, mat);
1327                         reconstructed[reconstruction->camnr].framenr= a;
1328                         reconstructed[reconstruction->camnr].error= error;
1329                         reconstruction->camnr++;
1330                 } else {
1331                         ok= 0;
1332                         printf("No camera for frame %d\n", a);
1333                 }
1334         }
1335
1336         if(reconstruction->camnr) {
1337                 reconstruction->cameras= MEM_callocN(reconstruction->camnr*sizeof(MovieReconstructedCamera), "reconstructed camera");
1338                 memcpy(reconstruction->cameras, reconstructed, reconstruction->camnr*sizeof(MovieReconstructedCamera));
1339         }
1340
1341         if(origin_set) {
1342                 track= tracking->tracks.first;
1343                 while(track) {
1344                         if(track->flag&TRACK_HAS_BUNDLE)
1345                                 sub_v3_v3(track->bundle_pos, origin);
1346
1347                         track= track->next;
1348                 }
1349         }
1350
1351         MEM_freeN(reconstructed);
1352
1353         return ok;
1354 }
1355
1356 #endif
1357
1358 float BKE_tracking_solve_reconstruction(MovieTracking *tracking, int width, int height)
1359 {
1360 #if WITH_LIBMV
1361         {
1362                 MovieTrackingCamera *camera= &tracking->camera;
1363                 float aspy= 1.0f/tracking->camera.pixel_aspect;
1364                 struct libmv_Tracks *tracks= create_libmv_tracks(tracking, width, height*aspy);
1365                 struct libmv_Reconstruction *reconstruction = libmv_solveReconstruction(tracks,
1366                         tracking->settings.keyframe1, tracking->settings.keyframe2,
1367                         camera->focal,
1368                         camera->principal[0], camera->principal[1]*aspy,
1369                         camera->k1, camera->k2, camera->k3);
1370                 float error= libmv_reprojectionError(reconstruction);
1371
1372                 tracking->reconstruction.error= error;
1373
1374                 if(!retrieve_libmv_reconstruct(tracking, reconstruction))
1375                         error= -1.0f;
1376
1377                 libmv_destroyReconstruction(reconstruction);
1378                 libmv_tracksDestroy(tracks);
1379
1380                 tracking->reconstruction.flag|= TRACKING_RECONSTRUCTED;
1381
1382                 return error;
1383         }
1384 #else
1385         (void)tracking;
1386         (void)width;
1387         (void)height;
1388
1389         return -1.0f;
1390 #endif
1391 }
1392
1393 void BKE_track_unique_name(MovieTracking *tracking, MovieTrackingTrack *track)
1394 {
1395         BLI_uniquename(&tracking->tracks, track, "Track", '.', offsetof(MovieTrackingTrack, name), sizeof(track->name));
1396 }
1397
1398 MovieTrackingTrack *BKE_tracking_named_track(MovieTracking *tracking, const char *name)
1399 {
1400         MovieTrackingTrack *track= tracking->tracks.first;
1401
1402         while(track) {
1403                 if(!strcmp(track->name, name))
1404                         return track;
1405
1406                 track= track->next;
1407         }
1408
1409         return NULL;
1410 }
1411
1412 static int reconstruction_camera_index(MovieTracking *tracking, int framenr, int nearest)
1413 {
1414         MovieTrackingReconstruction *reconstruction= &tracking->reconstruction;
1415         MovieReconstructedCamera *cameras= reconstruction->cameras;
1416         int a= 0, d= 1;
1417
1418         if(!reconstruction->camnr)
1419                 return -1;
1420
1421         if(framenr<cameras[0].framenr) {
1422                 if(nearest) return 0;
1423                 else return -1;
1424         }
1425
1426         if(framenr>cameras[reconstruction->camnr-1].framenr) {
1427                 if(nearest) return reconstruction->camnr-1;
1428                 else return -1;
1429         }
1430
1431         if(reconstruction->last_camera<reconstruction->camnr)
1432                 a= reconstruction->last_camera;
1433
1434         if(cameras[a].framenr>=framenr)
1435                 d= -1;
1436
1437         while(a>=0 && a<reconstruction->camnr) {
1438                 int cfra= cameras[a].framenr;
1439
1440                 /* check if needed framenr was "skipped" -- no data for requested frame */
1441
1442                 if(d>0 && cfra>framenr) {
1443                         /* interpolate with previous position */
1444                         if(nearest) return a-1;
1445                         else break;
1446                 }
1447
1448                 if(d<0 && cfra<framenr) {
1449                         /* interpolate with next position */
1450                         if(nearest) return a;
1451                         else break;
1452                 }
1453
1454                 if(cfra==framenr) {
1455                         reconstruction->last_camera= a;
1456
1457                         return a;
1458                 }
1459
1460                 a+= d;
1461         }
1462
1463         return -1;
1464 }
1465
1466 MovieReconstructedCamera *BKE_tracking_get_reconstructed_camera(MovieTracking *tracking, int framenr)
1467 {
1468         int a= reconstruction_camera_index(tracking, framenr, 0);
1469
1470         if(a==-1)
1471                 return NULL;
1472
1473         return &tracking->reconstruction.cameras[a];
1474 }
1475
1476 void BKE_tracking_get_interpolated_camera(MovieTracking *tracking, int framenr, float mat[4][4])
1477 {
1478         MovieTrackingReconstruction *reconstruction= &tracking->reconstruction;
1479         MovieReconstructedCamera *cameras= reconstruction->cameras;
1480         int a= reconstruction_camera_index(tracking, framenr, 1);
1481
1482         if(a==-1) {
1483                 unit_m4(mat);
1484                 return;
1485         }
1486
1487         if(cameras[a].framenr!=framenr && a>0 && a<reconstruction->camnr-1) {
1488                 float t= ((float)framenr-cameras[a].framenr) / (cameras[a+1].framenr-cameras[a].framenr);
1489
1490                 blend_m4_m4m4(mat, cameras[a].mat, cameras[a+1].mat, t);
1491         } else {
1492                 copy_m4_m4(mat, cameras[a].mat);
1493         }
1494 }
1495
1496 void BKE_get_tracking_mat(Scene *scene, Object *ob, float mat[4][4])
1497 {
1498         if(!ob) {
1499                 if(scene->camera) ob= scene->camera;
1500                 else ob= scene_find_camera(scene);
1501         }
1502
1503         if(ob)
1504                 where_is_object_mat(scene, ob, mat);
1505         else
1506                 unit_m4(mat);
1507 }
1508
1509 void BKE_tracking_camera_shift(MovieTracking *tracking, int winx, int winy, float *shiftx, float *shifty)
1510 {
1511         *shiftx= (0.5f*winx-tracking->camera.principal[0]) / winx;
1512         *shifty= (0.5f*winy-tracking->camera.principal[1]) / winx;
1513 }
1514
1515 void BKE_tracking_camera_to_blender(MovieTracking *tracking, Scene *scene, Camera *camera, int width, int height)
1516 {
1517         float focal= tracking->camera.focal;
1518
1519         camera->sensor_x= tracking->camera.sensor_width;
1520         camera->sensor_fit= CAMERA_SENSOR_FIT_AUTO;
1521         camera->lens= focal*camera->sensor_x/width;
1522
1523         scene->r.xsch= width*tracking->camera.pixel_aspect;
1524         scene->r.ysch= height;
1525
1526         scene->r.xasp= 1.0f;
1527         scene->r.yasp= 1.0f;
1528
1529         BKE_tracking_camera_shift(tracking, width, height, &camera->shiftx, &camera->shifty);
1530 }
1531
1532 void BKE_tracking_projection_matrix(MovieTracking *tracking, int framenr, int winx, int winy, float mat[4][4])
1533 {
1534         MovieReconstructedCamera *camera;
1535         float lens= tracking->camera.focal*tracking->camera.sensor_width/(float)winx;
1536         float viewfac, pixsize, left, right, bottom, top, clipsta, clipend;
1537         float winmat[4][4];
1538         float ycor= 1.0f/tracking->camera.pixel_aspect;
1539         float shiftx, shifty, winside= MAX2(winx, winy);
1540
1541         BKE_tracking_camera_shift(tracking, winx, winy, &shiftx, &shifty);
1542
1543         clipsta= 0.1f;
1544         clipend= 1000.0f;
1545
1546         if(winx >= winy)
1547                 viewfac= (lens*winx)/tracking->camera.sensor_width;
1548         else
1549                 viewfac= (ycor*lens*winy)/tracking->camera.sensor_width;
1550
1551         pixsize= clipsta/viewfac;
1552
1553         left= -0.5f*(float)winx + shiftx*winside;
1554         bottom= -0.5f*(ycor)*(float)winy + shifty*winside;
1555         right=  0.5f*(float)winx + shiftx*winside;
1556         top=  0.5f*(ycor)*(float)winy + shifty*winside;
1557
1558         left *= pixsize;
1559         right *= pixsize;
1560         bottom *= pixsize;
1561         top *= pixsize;
1562
1563         perspective_m4(winmat, left, right, bottom, top, clipsta, clipend);
1564
1565         camera= BKE_tracking_get_reconstructed_camera(tracking, framenr);
1566         if(camera) {
1567                 float imat[4][4];
1568
1569                 invert_m4_m4(imat, camera->mat);
1570                 mul_m4_m4m4(mat, imat, winmat);
1571         } else copy_m4_m4(mat, winmat);
1572 }
1573
1574 void BKE_tracking_apply_intrinsics(MovieTracking *tracking, float co[2], float nco[2])
1575 {
1576         MovieTrackingCamera *camera= &tracking->camera;
1577
1578 #ifdef WITH_LIBMV
1579         double x, y;
1580         float aspy= 1.0f/tracking->camera.pixel_aspect;
1581
1582         /* normalize coords */
1583         x= (co[0]-camera->principal[0]) / camera->focal;
1584         y= (co[1]-camera->principal[1] * aspy) / camera->focal;
1585
1586         libmv_applyCameraIntrinsics(camera->focal, camera->principal[0], camera->principal[1] * aspy,
1587                                 camera->k1, camera->k2, camera->k3, x, y, &x, &y);
1588
1589         /* result is in image coords already */
1590         nco[0]= x;
1591         nco[1]= y;
1592 #else
1593         (void)camera;
1594         (void)co;
1595         (void)nco;
1596 #endif
1597 }
1598
1599 void BKE_tracking_invert_intrinsics(MovieTracking *tracking, float co[2], float nco[2])
1600 {
1601         MovieTrackingCamera *camera= &tracking->camera;
1602
1603 #ifdef WITH_LIBMV
1604         double x= co[0], y= co[1];
1605         float aspy= 1.0f/tracking->camera.pixel_aspect;
1606
1607         libmv_InvertIntrinsics(camera->focal, camera->principal[0], camera->principal[1] * aspy,
1608                                 camera->k1, camera->k2, camera->k3, x, y, &x, &y);
1609
1610         nco[0]= x * camera->focal + camera->principal[0];
1611         nco[1]= y * camera->focal + camera->principal[1] * aspy;
1612 #else
1613         (void)camera;
1614         (void)co;
1615         (void)nco;
1616 #endif
1617 }
1618
1619 #ifdef WITH_LIBMV
1620 static int point_in_stroke(bGPDstroke *stroke, float x, float y)
1621 {
1622         int i, prev;
1623         int count= 0;
1624         bGPDspoint *points= stroke->points;
1625
1626         prev= stroke->totpoints-1;
1627
1628         for(i= 0; i<stroke->totpoints; i++) {
1629                 if((points[i].y<y && points[prev].y>=y) || (points[prev].y<y && points[i].y>=y)) {
1630                         float fac= (y-points[i].y)/(points[prev].y-points[i].y);
1631
1632                         if (points[i].x+fac*(points[prev].x-points[i].x)<x)
1633                                 count++;
1634                 }
1635
1636                 prev= i;
1637         }
1638
1639         return count%2;
1640 }
1641
1642 static int point_in_layer(bGPDlayer *layer, float x, float y)
1643 {
1644         bGPDframe *frame= layer->frames.first;
1645
1646         while(frame) {
1647                 bGPDstroke *stroke= frame->strokes.first;
1648                 while(stroke) {
1649                         if(point_in_stroke(stroke, x, y))
1650                                 return 1;
1651
1652                         stroke= stroke->next;
1653                 }
1654                 frame= frame->next;
1655         }
1656
1657         return 0;
1658 }
1659
1660 static void retrieve_libmv_features(MovieTracking *tracking, struct libmv_Features *features,
1661                         int framenr, int width, int height, bGPDlayer *layer, int place_outside_layer)
1662 {
1663         int a;
1664
1665         a= libmv_countFeatures(features);
1666         while(a--) {
1667                 MovieTrackingTrack *track;
1668                 double x, y, size, score;
1669                 int ok= 1;
1670                 float xu, yu;
1671
1672                 libmv_getFeature(features, a, &x, &y, &score, &size);
1673
1674                 xu= x/width;
1675                 yu= y/height;
1676
1677                 if(layer)
1678                         ok= point_in_layer(layer, xu, yu)!=place_outside_layer;
1679
1680                 if(ok) {
1681                         track= BKE_tracking_add_track(tracking, xu, yu, framenr, width, height);
1682                         track->flag|= SELECT;
1683                         track->pat_flag|= SELECT;
1684                         track->search_flag|= SELECT;
1685                 }
1686         }
1687 }
1688 #endif
1689
1690 void BKE_tracking_detect_fast(MovieTracking *tracking, ImBuf *ibuf,
1691                         int framenr, int margin, int min_trackness, int min_distance, bGPDlayer *layer,
1692                         int place_outside_layer)
1693 {
1694 #ifdef WITH_LIBMV
1695         struct libmv_Features *features;
1696         unsigned char *pixels= get_ucharbuf(ibuf);
1697
1698         features= libmv_detectFeaturesFAST(pixels, ibuf->x, ibuf->y, ibuf->x, margin, min_trackness, min_distance);
1699
1700         MEM_freeN(pixels);
1701
1702         retrieve_libmv_features(tracking, features, framenr, ibuf->x, ibuf->y, layer, place_outside_layer);
1703
1704         libmv_destroyFeatures(features);
1705 #else
1706         (void)tracking;
1707         (void)ibuf;
1708         (void)framenr;
1709         (void)margin;
1710         (void)min_trackness;
1711         (void)min_distance;
1712         (void)layer;
1713         (void)place_outside_layer;
1714 #endif
1715 }
1716
1717 MovieTrackingTrack *BKE_tracking_indexed_track(MovieTracking *tracking, int tracknr)
1718 {
1719         MovieTrackingTrack *track= tracking->tracks.first;
1720         int cur= 1;
1721
1722         while(track) {
1723                 if(track->flag&TRACK_HAS_BUNDLE) {
1724                         if(cur==tracknr)
1725                                 return track;
1726
1727                         cur++;
1728                 }
1729
1730                 track= track->next;
1731         }
1732
1733         return NULL;
1734 }
1735
1736 static int stabilization_median_point(MovieTracking *tracking, int framenr, float median[2])
1737 {
1738         int ok= 0;
1739         float min[2], max[2];
1740         MovieTrackingTrack *track;
1741
1742         INIT_MINMAX2(min, max);
1743
1744         track= tracking->tracks.first;
1745         while(track) {
1746                 if(track->flag&TRACK_USE_2D_STAB) {
1747                         MovieTrackingMarker *marker= BKE_tracking_get_marker(track, framenr);
1748
1749                         DO_MINMAX2(marker->pos, min, max);
1750
1751                         ok= 1;
1752                 }
1753
1754                 track= track->next;
1755         }
1756
1757         median[0]= (max[0]+min[0])/2.0f;
1758         median[1]= (max[1]+min[1])/2.0f;
1759
1760         return ok;
1761 }
1762
1763 static void calculate_stabdata(MovieTracking *tracking, int framenr, float width, float height,
1764                         float firstmedian[2], float median[2], float loc[2], float *scale, float *angle)
1765 {
1766         MovieTrackingStabilization *stab= &tracking->stabilization;
1767
1768         *scale= (stab->scale-1.0f)*stab->scaleinf+1.0f;
1769         *angle= 0.0f;
1770
1771         loc[0]= (firstmedian[0]-median[0])*width*(*scale);
1772         loc[1]= (firstmedian[1]-median[1])*height*(*scale);
1773
1774         mul_v2_fl(loc, stab->locinf);
1775
1776         if(stab->rot_track && stab->rotinf) {
1777                 MovieTrackingMarker *marker;
1778                 float a[2], b[2];
1779                 float x0= (float)width/2.0f, y0= (float)height/2.0f;
1780                 float x= median[0]*width, y= median[1]*height;
1781
1782                 marker= BKE_tracking_get_marker(stab->rot_track, 1);
1783                 sub_v2_v2v2(a, marker->pos, firstmedian);
1784                 a[0]*= width;
1785                 a[1]*= height;
1786
1787                 marker= BKE_tracking_get_marker(stab->rot_track, framenr);
1788                 sub_v2_v2v2(b, marker->pos, median);
1789                 b[0]*= width;
1790                 b[1]*= height;
1791
1792                 *angle= -atan2(a[0]*b[1]-a[1]*b[0], a[0]*b[0]+a[1]*b[1]);
1793                 *angle*= stab->rotinf;
1794
1795                 /* convert to rotation around image center */
1796                 loc[0]-= (x0 + (x-x0)*cos(*angle)-(y-y0)*sin(*angle) - x)*(*scale);
1797                 loc[1]-= (y0 + (x-x0)*sin(*angle)+(y-y0)*cos(*angle) - y)*(*scale);
1798         }
1799 }
1800
1801 static float stabilization_auto_scale_factor(MovieTracking *tracking, int width, int height)
1802 {
1803         float firstmedian[2];
1804         MovieTrackingStabilization *stab= &tracking->stabilization;
1805
1806         if(stab->ok)
1807                 return stab->scale;
1808
1809         if(stabilization_median_point(tracking, 1, firstmedian)) {
1810                 int sfra= INT_MAX, efra= INT_MIN, cfra;
1811                 float delta[2]= {0.0f, 0.0f}, scalex= 1.0f, scaley= 1.0f;
1812                 MovieTrackingTrack *track;
1813
1814                 stab->scale= 1.0f;
1815
1816                 track= tracking->tracks.first;
1817                 while(track) {
1818                         if(track->flag&TRACK_USE_2D_STAB || track==stab->rot_track) {
1819                                 if(track->markersnr) {
1820                                         sfra= MIN2(sfra, track->markers[0].framenr);
1821                                         efra= MAX2(efra, track->markers[track->markersnr-1].framenr);
1822                                 }
1823                         }
1824
1825                         track= track->next;
1826                 }
1827
1828                 for(cfra=sfra; cfra<=efra; cfra++) {
1829                         float median[2], near[2];
1830                         float loc[2], scale, angle;
1831
1832                         stabilization_median_point(tracking, cfra, median);
1833
1834                         calculate_stabdata(tracking, cfra, width, height, firstmedian, median,
1835                                                 loc, &scale, &angle);
1836
1837                         if(angle==0.0f) {
1838                                 loc[0]= fabsf(loc[0]);
1839                                 loc[1]= fabsf(loc[1]);
1840
1841                                 delta[0]= MAX2(delta[0], loc[0]);
1842                                 delta[1]= MAX2(delta[1], loc[1]);
1843
1844                                 near[0]= MIN2(median[0], 1.0f-median[0]);
1845                                 near[1]= MIN2(median[1], 1.0f-median[1]);
1846                                 near[0]= MAX2(near[0], 0.05f);
1847                                 near[1]= MAX2(near[1], 0.05f);
1848
1849                                 scalex= 1.0f+delta[0]/(near[0]*width);
1850                                 scaley= 1.0f+delta[1]/(near[1]*height);
1851                         } else {
1852                                 int i;
1853                                 float mat[4][4];
1854                                 float points[4][2]={{0.0f, 0.0f}, {0.0f, height}, {width, height}, {width, 0.0f}};
1855
1856                                 BKE_tracking_stabdata_to_mat4(width, height, loc, scale, angle, mat);
1857
1858                                 for(i= 0; i<4; i++) {
1859                                         int j;
1860                                         float a[3]= {0.0f, 0.0f, 0.0f}, b[3]= {0.0f, 0.0f, 0.0f};
1861
1862                                         copy_v3_v3(a, points[i]);
1863                                         copy_v3_v3(b, points[(i+1)%4]);
1864
1865                                         mul_m4_v3(mat, a);
1866                                         mul_m4_v3(mat, b);
1867
1868                                         for(j= 0; j<4; j++) {
1869                                                 float point[3]= {points[j][0], points[j][1], 0.0f};
1870                                                 float v1[3], v2[3];
1871
1872                                                 sub_v3_v3v3(v1, b, a);
1873                                                 sub_v3_v3v3(v2, point, a);
1874
1875                                                 if(cross_v2v2(v1, v2) >= 0.0f) {
1876                                                         float dist= dist_to_line_v2(point, a, b);
1877                                                         if(i%2==0) {
1878                                                                 scalex= MAX2(scalex, (width+2*dist)/width);
1879                                                         } else {
1880                                                                 scaley= MAX2(scaley, (height+2*dist)/height);
1881                                                         }
1882                                                 }
1883                                         }
1884                                 }
1885                         }
1886                 }
1887
1888                 stab->scale= MAX2(scalex, scaley);
1889
1890                 if(stab->maxscale>0.0f)
1891                         stab->scale= MIN2(stab->scale, stab->maxscale);
1892         } else {
1893                 stab->scale= 1.0f;
1894         }
1895
1896         stab->ok= 1;
1897
1898         return stab->scale;
1899 }
1900
1901 static ImBuf* stabilize_alloc_ibuf(ImBuf *cacheibuf, ImBuf *srcibuf, int fill)
1902 {
1903         int flags;
1904
1905         if(cacheibuf && (cacheibuf->x != srcibuf->x || cacheibuf->y != srcibuf->y)) {
1906                 IMB_freeImBuf(cacheibuf);
1907                 cacheibuf= NULL;
1908         }
1909
1910         flags= IB_rect;
1911
1912         if(srcibuf->rect_float)
1913                 flags|= IB_rectfloat;
1914
1915         if(cacheibuf) {
1916                 if(fill) {
1917                         float col[4]= {0.0f, 0.0f, 0.0f, 0.0f};
1918                         IMB_rectfill(cacheibuf, col);
1919                 }
1920         }
1921         else {
1922                 cacheibuf= IMB_allocImBuf(srcibuf->x, srcibuf->y, srcibuf->depth, flags);
1923                 cacheibuf->profile= srcibuf->profile;
1924         }
1925
1926         return cacheibuf;
1927 }
1928
1929 void BKE_tracking_stabilization_data(MovieTracking *tracking, int framenr, int width, int height, float loc[2], float *scale, float *angle)
1930 {
1931         float firstmedian[2], median[2];
1932         MovieTrackingStabilization *stab= &tracking->stabilization;
1933
1934         if((stab->flag&TRACKING_2D_STABILIZATION)==0) {
1935                 zero_v2(loc);
1936                 *scale= 1.0f;
1937                 *angle= 0.0f;
1938
1939                 return;
1940         }
1941
1942         if(stabilization_median_point(tracking, 1, firstmedian)) {
1943                 stabilization_median_point(tracking, framenr, median);
1944
1945                 if((stab->flag&TRACKING_AUTOSCALE)==0)
1946                         stab->scale= 1.0f;
1947
1948                 if(!stab->ok) {
1949                         if(stab->flag&TRACKING_AUTOSCALE)
1950                                 stabilization_auto_scale_factor(tracking, width, height);
1951
1952                         calculate_stabdata(tracking, framenr, width, height, firstmedian, median, loc, scale, angle);
1953
1954                         stab->ok= 1;
1955                 } else {
1956                         calculate_stabdata(tracking, framenr, width, height, firstmedian, median, loc, scale, angle);
1957                 }
1958         } else {
1959                 zero_v2(loc);
1960                 *scale= 1.0f;
1961                 *angle= 0.0f;
1962         }
1963 }
1964
1965 ImBuf *BKE_tracking_stabilize(MovieTracking *tracking, int framenr, ImBuf *ibuf, float loc[2], float *scale, float *angle)
1966 {
1967         float tloc[2], tscale, tangle;
1968         MovieTrackingStabilization *stab= &tracking->stabilization;
1969         ImBuf *tmpibuf;
1970         float width= ibuf->x, height= ibuf->y;
1971
1972         if(loc)         copy_v2_v2(tloc, loc);
1973         if(scale)       tscale= *scale;
1974
1975         if((stab->flag&TRACKING_2D_STABILIZATION)==0) {
1976                 if(loc)         zero_v2(loc);
1977                 if(scale)       *scale= 1.0f;
1978
1979                 return ibuf;
1980         }
1981
1982         BKE_tracking_stabilization_data(tracking, framenr, width, height, tloc, &tscale, &tangle);
1983
1984         tmpibuf= stabilize_alloc_ibuf(NULL, ibuf, 1);
1985
1986         /* scale would be handled by matrix transformation when angle is non-zero */
1987         if(tscale!=1.0f && tangle==0.0f) {
1988                 ImBuf *scaleibuf;
1989
1990                 stabilization_auto_scale_factor(tracking, width, height);
1991
1992                 scaleibuf= stabilize_alloc_ibuf(stab->scaleibuf, ibuf, 0);
1993                 stab->scaleibuf= scaleibuf;
1994
1995                 IMB_rectcpy(scaleibuf, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
1996                 IMB_scalefastImBuf(scaleibuf, ibuf->x*tscale, ibuf->y*tscale);
1997
1998                 ibuf= scaleibuf;
1999         }
2000
2001         if(tangle==0.0f) {
2002                 /* if angle is zero, then it's much faster to use rect copy
2003                    but could be issues with subpixel precisions */
2004                 IMB_rectcpy(tmpibuf, ibuf, tloc[0]-(tscale-1.0f)*width/2.0f, tloc[1]-(tscale-1.0f)*height/2.0f, 0, 0, ibuf->x, ibuf->y);
2005         } else {
2006                 float mat[4][4];
2007                 int i, j;
2008
2009                 BKE_tracking_stabdata_to_mat4(ibuf->x, ibuf->y, tloc, tscale, tangle, mat);
2010                 invert_m4(mat);
2011
2012                 for(j=0; j<tmpibuf->y; j++) {
2013                         for(i=0; i<tmpibuf->x;i++) {
2014                                 float vec[3]= {i, j, 0};
2015
2016                                 mul_v3_m4v3(vec, mat, vec);
2017
2018                                 /* TODO: add selector for interpolation method */
2019                                 neareast_interpolation(ibuf, tmpibuf, vec[0], vec[1], i, j);
2020                         }
2021                 }
2022         }
2023
2024         tmpibuf->userflags|= IB_MIPMAP_INVALID;
2025
2026         if(tmpibuf->rect_float)
2027                 tmpibuf->userflags|= IB_RECT_INVALID;
2028
2029         if(loc)         copy_v2_v2(loc, tloc);
2030         if(scale)       *scale= tscale;
2031         if(angle)       *angle= tangle;
2032
2033         return tmpibuf;
2034 }
2035
2036 void BKE_tracking_stabdata_to_mat4(int width, int height, float loc[2], float scale, float angle, float mat[4][4])
2037 {
2038         float lmat[4][4], rmat[4][4], smat[4][4], cmat[4][4], icmat[4][4];
2039         float svec[3]= {scale, scale, scale};
2040
2041         unit_m4(rmat);
2042         unit_m4(lmat);
2043         unit_m4(smat);
2044         unit_m4(cmat);
2045
2046         /* image center as rotation center */
2047         cmat[3][0]= (float)width/2.0f;
2048         cmat[3][1]= (float)height/2.0f;
2049         invert_m4_m4(icmat, cmat);
2050
2051         size_to_mat4(smat, svec);               /* scale matrix */
2052         add_v2_v2(lmat[3], loc);                /* tranlation matrix */
2053         rotate_m4(rmat, 'Z', angle);    /* rotation matrix */
2054
2055         /* compose transformation matrix */
2056         mul_serie_m4(mat, lmat, cmat, rmat, smat, icmat, NULL, NULL, NULL);
2057 }
2058
2059 MovieDistortion *BKE_tracking_distortion_create(void)
2060 {
2061         MovieDistortion *distortion;
2062
2063         distortion= MEM_callocN(sizeof(MovieDistortion), "BKE_tracking_distortion_create");
2064
2065         return distortion;
2066 }
2067
2068 MovieDistortion *BKE_tracking_distortion_copy(MovieDistortion *distortion)
2069 {
2070         MovieDistortion *new_distortion;
2071
2072         new_distortion= MEM_callocN(sizeof(MovieDistortion), "BKE_tracking_distortion_create");
2073
2074 #ifdef WITH_LIBMV
2075         new_distortion->intrinsics= libmv_CameraIntrinsicsCopy(distortion->intrinsics);
2076 #else
2077         (void)distortion;
2078 #endif
2079
2080         return new_distortion;
2081 }
2082
2083 void BKE_tracking_distortion_update(MovieDistortion *distortion, MovieTracking *tracking, int width, int height)
2084 {
2085         MovieTrackingCamera *camera= &tracking->camera;
2086         float aspy= 1.0f/tracking->camera.pixel_aspect;
2087
2088 #ifdef WITH_LIBMV
2089         if(!distortion->intrinsics) {
2090                 distortion->intrinsics= libmv_CameraIntrinsicsNew(camera->focal,
2091                                 camera->principal[0], camera->principal[1] * aspy,
2092                                 camera->k1, camera->k2, camera->k3, width, height * aspy);
2093         } else {
2094                 libmv_CameraIntrinsicsUpdate(distortion->intrinsics, camera->focal,
2095                                 camera->principal[0], camera->principal[1] * aspy,
2096                                 camera->k1, camera->k2, camera->k3, width, height * aspy);
2097         }
2098 #else
2099         (void)distortion;
2100         (void)width;
2101         (void)height;
2102         (void)camera;
2103         (void)aspy;
2104 #endif
2105 }
2106
2107 ImBuf *BKE_tracking_distortion_exec(MovieDistortion *distortion, MovieTracking *tracking,
2108                         ImBuf *ibuf, int width, int height, float overscan, int undistort)
2109 {
2110         ImBuf *resibuf;
2111
2112         BKE_tracking_distortion_update(distortion, tracking, width, height);
2113
2114         resibuf= IMB_dupImBuf(ibuf);
2115
2116         if(ibuf->rect_float) {
2117 #ifdef WITH_LIBMV
2118                 if(undistort) {
2119                         libmv_CameraIntrinsicsUndistortFloat(distortion->intrinsics,
2120                                                 ibuf->rect_float, resibuf->rect_float,
2121                                                 ibuf->x, ibuf->y, overscan, ibuf->channels);
2122                 } else {
2123                         libmv_CameraIntrinsicsDistortFloat(distortion->intrinsics,
2124                                                 ibuf->rect_float, resibuf->rect_float,
2125                                                 ibuf->x, ibuf->y, overscan, ibuf->channels);
2126                 }
2127 #endif
2128
2129                 ibuf->userflags|= IB_RECT_INVALID;
2130         } else {
2131 #ifdef WITH_LIBMV
2132                 if(undistort) {
2133                                 libmv_CameraIntrinsicsUndistortByte(distortion->intrinsics,
2134                                                         (unsigned char*)ibuf->rect, (unsigned char*)resibuf->rect,
2135                                                         ibuf->x, ibuf->y, overscan, ibuf->channels);
2136                 } else {
2137                         libmv_CameraIntrinsicsDistortByte(distortion->intrinsics,
2138                                                 (unsigned char*)ibuf->rect, (unsigned char*)resibuf->rect,
2139                                                 ibuf->x, ibuf->y, overscan, ibuf->channels);
2140                 }
2141 #endif
2142         }
2143
2144 #ifndef WITH_LIBMV
2145         (void)overscan;
2146         (void)undistort;
2147 #endif
2148
2149         return resibuf;
2150 }
2151
2152 void BKE_tracking_distortion_destroy(MovieDistortion *distortion)
2153 {
2154 #ifdef WITH_LIBMV
2155         libmv_CameraIntrinsicsDestroy(distortion->intrinsics);
2156 #endif
2157
2158         MEM_freeN(distortion);
2159 }
2160
2161 ImBuf *BKE_tracking_undistort(MovieTracking *tracking, ImBuf *ibuf, int width, int height, float overscan)
2162 {
2163         MovieTrackingCamera *camera= &tracking->camera;
2164
2165         if(camera->intrinsics == NULL)
2166                 camera->intrinsics= BKE_tracking_distortion_create();
2167
2168         return BKE_tracking_distortion_exec(camera->intrinsics, tracking, ibuf, width, height, overscan, 1);
2169 }
2170
2171 ImBuf *BKE_tracking_distort(MovieTracking *tracking, ImBuf *ibuf, int width, int height, float overscan)
2172 {
2173         MovieTrackingCamera *camera= &tracking->camera;
2174
2175         if(camera->intrinsics == NULL)
2176                 camera->intrinsics= BKE_tracking_distortion_create();
2177
2178         return BKE_tracking_distortion_exec(camera->intrinsics, tracking, ibuf, width, height, overscan, 0);
2179 }
2180
2181 /* area - which part of marker should be selected. see TRACK_AREA_* constants */
2182 void BKE_tracking_select_track(MovieTracking *tracking, MovieTrackingTrack *track, int area, int extend)
2183 {
2184         if(extend) {
2185                 BKE_tracking_track_flag(track, area, SELECT, 0);
2186         } else {
2187                 MovieTrackingTrack *cur= tracking->tracks.first;
2188
2189                 while(cur) {
2190                         if(cur==track) {
2191                                 BKE_tracking_track_flag(cur, TRACK_AREA_ALL, SELECT, 1);
2192                                 BKE_tracking_track_flag(cur, area, SELECT, 0);
2193                         }
2194                         else {
2195                                 BKE_tracking_track_flag(cur, TRACK_AREA_ALL, SELECT, 1);
2196                         }
2197
2198                         cur= cur->next;
2199                 }
2200         }
2201 }
2202
2203 void BKE_tracking_deselect_track(MovieTrackingTrack *track, int area)
2204 {
2205         BKE_tracking_track_flag(track, area, SELECT, 1);
2206 }