Cleanup: remove redundant doxygen \file argument
[blender.git] / source / blender / blenkernel / intern / tracking_region_tracker.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2011 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file \ingroup bke
21  *
22  * This file contains implementation of blender-side region tracker
23  * which is used for 2D feature tracking.
24  */
25
26 #include "MEM_guardedalloc.h"
27
28 #include "DNA_movieclip_types.h"
29
30 #include "BLI_utildefines.h"
31 #include "BLI_threads.h"
32
33 #include "BKE_tracking.h"
34 #include "BKE_movieclip.h"
35
36 #include "IMB_imbuf_types.h"
37 #include "IMB_imbuf.h"
38
39 #include "libmv-capi.h"
40 #include "tracking_private.h"
41
42 /* **** utility functions for tracking **** */
43
44 /* convert from float and byte RGBA to grayscale. Supports different coefficients for RGB. */
45 static void float_rgba_to_gray(const float *rgba, float *gray, int num_pixels,
46                                float weight_red, float weight_green, float weight_blue)
47 {
48         int i;
49
50         for (i = 0; i < num_pixels; i++) {
51                 const float *pixel = rgba + 4 * i;
52
53                 gray[i] = weight_red * pixel[0] + weight_green * pixel[1] + weight_blue * pixel[2];
54         }
55 }
56
57 static void uint8_rgba_to_float_gray(const unsigned char *rgba, float *gray, int num_pixels,
58                                      float weight_red, float weight_green, float weight_blue)
59 {
60         int i;
61
62         for (i = 0; i < num_pixels; i++) {
63                 const unsigned char *pixel = rgba + i * 4;
64
65                 gray[i] = (weight_red * pixel[0] + weight_green * pixel[1] + weight_blue * pixel[2]) / 255.0f;
66         }
67 }
68
69 /* Get grayscale float search buffer for given marker and frame. */
70 static float *track_get_search_floatbuf(ImBuf *ibuf, MovieTrackingTrack *track, MovieTrackingMarker *marker,
71                                         int *width_r, int *height_r)
72 {
73         ImBuf *searchibuf;
74         float *gray_pixels;
75         int width, height;
76
77         searchibuf = BKE_tracking_get_search_imbuf(ibuf, track, marker, false, true);
78
79         if (!searchibuf) {
80                 *width_r = 0;
81                 *height_r = 0;
82                 return NULL;
83         }
84
85         width = searchibuf->x;
86         height = searchibuf->y;
87
88         gray_pixels = MEM_callocN(width * height * sizeof(float), "tracking floatBuf");
89
90         if (searchibuf->rect_float) {
91                 float_rgba_to_gray(searchibuf->rect_float, gray_pixels, width * height,
92                                    0.2126f, 0.7152f, 0.0722f);
93         }
94         else {
95                 uint8_rgba_to_float_gray((unsigned char *)searchibuf->rect, gray_pixels, width * height,
96                                          0.2126f, 0.7152f, 0.0722f);
97         }
98
99         IMB_freeImBuf(searchibuf);
100
101         *width_r = width;
102         *height_r = height;
103
104         return gray_pixels;
105 }
106
107 /* Get image buffer for a given frame
108  *
109  * Frame is in clip space.
110  */
111 static ImBuf *tracking_context_get_frame_ibuf(MovieClip *clip, MovieClipUser *user, int clip_flag, int framenr)
112 {
113         ImBuf *ibuf;
114         MovieClipUser new_user = *user;
115
116         new_user.framenr = BKE_movieclip_remap_clip_to_scene_frame(clip, framenr);
117
118         ibuf = BKE_movieclip_get_ibuf_flag(clip, &new_user, clip_flag, MOVIECLIP_CACHE_SKIP);
119
120         return ibuf;
121 }
122
123 /* Get image buffer for previous marker's keyframe. */
124 static ImBuf *tracking_context_get_keyframed_ibuf(MovieClip *clip, MovieClipUser *user, int clip_flag,
125                                                   MovieTrackingTrack *track, int curfra, bool backwards,
126                                                   MovieTrackingMarker **marker_keyed_r)
127 {
128         MovieTrackingMarker *marker_keyed;
129         int keyed_framenr;
130
131         marker_keyed = tracking_get_keyframed_marker(track, curfra, backwards);
132         if (marker_keyed == NULL) {
133                 return NULL;
134         }
135
136         keyed_framenr = marker_keyed->framenr;
137
138         *marker_keyed_r = marker_keyed;
139
140         return tracking_context_get_frame_ibuf(clip, user, clip_flag, keyed_framenr);
141 }
142
143 /* Get image buffer which si used as reference for track. */
144 static ImBuf *tracking_context_get_reference_ibuf(MovieClip *clip, MovieClipUser *user, int clip_flag,
145                                                   MovieTrackingTrack *track, int curfra, bool backwards,
146                                                   MovieTrackingMarker **reference_marker)
147 {
148         ImBuf *ibuf = NULL;
149
150         if (track->pattern_match == TRACK_MATCH_KEYFRAME) {
151                 ibuf = tracking_context_get_keyframed_ibuf(clip, user, clip_flag, track, curfra, backwards, reference_marker);
152         }
153         else {
154                 ibuf = tracking_context_get_frame_ibuf(clip, user, clip_flag, curfra);
155
156                 /* use current marker as keyframed position */
157                 *reference_marker = BKE_tracking_marker_get(track, curfra);
158         }
159
160         return ibuf;
161 }
162
163 /* Fill in libmv tracker options structure with settings need to be used to perform track. */
164 void tracking_configure_tracker(const MovieTrackingTrack *track, float *mask,
165                                 libmv_TrackRegionOptions *options)
166 {
167         options->motion_model = track->motion_model;
168
169         options->use_brute = ((track->algorithm_flag & TRACK_ALGORITHM_FLAG_USE_BRUTE) != 0);
170
171         options->use_normalization = ((track->algorithm_flag & TRACK_ALGORITHM_FLAG_USE_NORMALIZATION) != 0);
172
173         options->num_iterations = 50;
174         options->minimum_correlation = track->minimum_correlation;
175         options->sigma = 0.9;
176
177         if ((track->algorithm_flag & TRACK_ALGORITHM_FLAG_USE_MASK) != 0)
178                 options->image1_mask = mask;
179         else
180                 options->image1_mask = NULL;
181 }
182
183 /* Perform tracking from a reference_marker to destination_ibuf.
184  * Uses marker as an initial position guess.
185  *
186  * Returns truth if tracker returned success, puts result
187  * to dst_pixel_x and dst_pixel_y.
188  */
189 static bool configure_and_run_tracker(ImBuf *destination_ibuf, MovieTrackingTrack *track,
190                                       MovieTrackingMarker *reference_marker, MovieTrackingMarker *marker,
191                                       float *reference_search_area, int reference_search_area_width,
192                                       int reference_search_area_height, float *mask,
193                                       double dst_pixel_x[5], double dst_pixel_y[5])
194 {
195         /* To convert to the x/y split array format for libmv. */
196         double src_pixel_x[5], src_pixel_y[5];
197
198         /* Settings for the tracker */
199         libmv_TrackRegionOptions options = {0};
200         libmv_TrackRegionResult result;
201
202         float *patch_new;
203
204         int new_search_area_width, new_search_area_height;
205         int frame_width, frame_height;
206
207         bool tracked;
208
209         frame_width = destination_ibuf->x;
210         frame_height = destination_ibuf->y;
211
212         /* for now track to the same search area dimension as marker has got for current frame
213          * will make all tracked markers in currently tracked segment have the same search area
214          * size, but it's quite close to what is actually needed
215          */
216         patch_new = track_get_search_floatbuf(destination_ibuf, track, marker,
217                                               &new_search_area_width, &new_search_area_height);
218
219         /* configure the tracker */
220         tracking_configure_tracker(track, mask, &options);
221
222         /* convert the marker corners and center into pixel coordinates in the search/destination images. */
223         tracking_get_marker_coords_for_tracking(frame_width, frame_height, reference_marker, src_pixel_x, src_pixel_y);
224         tracking_get_marker_coords_for_tracking(frame_width, frame_height, marker, dst_pixel_x, dst_pixel_y);
225
226         if (patch_new == NULL || reference_search_area == NULL)
227                 return false;
228
229         /* run the tracker! */
230         tracked = libmv_trackRegion(&options,
231                                     reference_search_area,
232                                     reference_search_area_width,
233                                     reference_search_area_height,
234                                     patch_new,
235                                     new_search_area_width,
236                                     new_search_area_height,
237                                     src_pixel_x, src_pixel_y,
238                                     &result,
239                                     dst_pixel_x, dst_pixel_y);
240
241         MEM_freeN(patch_new);
242
243         return tracked;
244 }
245
246 static bool refine_marker_reference_frame_get(MovieTrackingTrack *track,
247                                               MovieTrackingMarker *marker,
248                                               bool backwards,
249                                               int *reference_framenr)
250 {
251         const MovieTrackingMarker *first_marker = track->markers;
252         const MovieTrackingMarker *last_marker = track->markers + track->markersnr - 1;
253         MovieTrackingMarker *reference = backwards ? marker + 1 : marker - 1;
254
255         while (reference >= first_marker &&
256                reference <= last_marker &&
257                (reference->flag & MARKER_DISABLED) != 0)
258         {
259                 if (backwards)
260                         reference++;
261                 else
262                         reference--;
263         }
264
265         if (reference < first_marker ||
266             reference > last_marker)
267         {
268                 return false;
269         }
270
271         *reference_framenr = reference->framenr;
272         return (reference->flag & MARKER_DISABLED) == 0;
273 }
274
275 /* Refine marker's position using previously known keyframe.
276  * Direction of searching for a keyframe depends on backwards flag,
277  * which means if backwards is false, previous keyframe will be as
278  * reference.
279  */
280 void BKE_tracking_refine_marker(MovieClip *clip, MovieTrackingTrack *track, MovieTrackingMarker *marker, bool backwards)
281 {
282         MovieTrackingMarker *reference_marker = NULL;
283         ImBuf *reference_ibuf, *destination_ibuf;
284         float *search_area, *mask = NULL;
285         int frame_width, frame_height;
286         int search_area_height, search_area_width;
287         int clip_flag = clip->flag & MCLIP_TIMECODE_FLAGS;
288         int reference_framenr;
289         MovieClipUser user = {0};
290         double dst_pixel_x[5], dst_pixel_y[5];
291         bool tracked;
292
293         /* Construct a temporary clip used, used to acquire image buffers. */
294         user.framenr = BKE_movieclip_remap_clip_to_scene_frame(clip, marker->framenr);
295
296         BKE_movieclip_get_size(clip, &user, &frame_width, &frame_height);
297
298         /* Get an image buffer for reference frame, also gets reference marker. */
299         if (!refine_marker_reference_frame_get(track,
300                                                marker,
301                                                backwards,
302                                                &reference_framenr))
303         {
304                 return;
305         }
306
307         reference_ibuf = tracking_context_get_reference_ibuf(clip, &user, clip_flag, track, reference_framenr,
308                                                              backwards, &reference_marker);
309         if (reference_ibuf == NULL) {
310                 return;
311         }
312
313         /* Could not refine with self. */
314         if (reference_marker == marker) {
315                 return;
316         }
317
318         /* Destination image buffer has got frame number corresponding to refining marker. */
319         destination_ibuf = BKE_movieclip_get_ibuf_flag(clip, &user, clip_flag, MOVIECLIP_CACHE_SKIP);
320         if (destination_ibuf == NULL) {
321                 IMB_freeImBuf(reference_ibuf);
322                 return;
323         }
324
325         /* Get search area from reference image. */
326         search_area = track_get_search_floatbuf(reference_ibuf, track, reference_marker,
327                                                 &search_area_width, &search_area_height);
328
329         /* If needed, compute track's mask. */
330         if ((track->algorithm_flag & TRACK_ALGORITHM_FLAG_USE_MASK) != 0)
331                 mask = BKE_tracking_track_get_mask(frame_width, frame_height, track, marker);
332
333         /* Run the tracker from reference frame to current one. */
334         tracked = configure_and_run_tracker(destination_ibuf, track, reference_marker, marker,
335                                             search_area, search_area_width, search_area_height,
336                                             mask, dst_pixel_x, dst_pixel_y);
337
338         /* Refine current marker's position if track was successful. */
339         if (tracked) {
340                 tracking_set_marker_coords_from_tracking(frame_width, frame_height, marker, dst_pixel_x, dst_pixel_y);
341                 marker->flag |= MARKER_TRACKED;
342         }
343
344         /* Free memory used for refining */
345         MEM_freeN(search_area);
346         if (mask)
347                 MEM_freeN(mask);
348         IMB_freeImBuf(reference_ibuf);
349         IMB_freeImBuf(destination_ibuf);
350 }