Blender V5.3
tracking_util.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include <cstddef>
13
14#include "MEM_guardedalloc.h"
15
16#include "DNA_movieclip_types.h"
17
18#include "BLI_ghash.hh"
19#include "BLI_listbase.hh"
20#include "BLI_math_color_c.hh"
21#include "BLI_math_vector_c.hh"
22#include "BLI_string.hh"
23#include "BLI_string_utils.hh"
24#include "BLI_threads.hh"
25
26#include "BLT_translation.hh"
27
28#include "BKE_movieclip.hh"
29#include "BKE_tracking.hh"
30
31#include "IMB_imbuf.hh"
32#include "IMB_imbuf_types.hh"
33
34#include "tracking_private.hh"
35
36#include "libmv-capi.h"
37
38namespace blender {
39
40/* Uncomment this to have caching-specific debug prints. */
41// #define DEBUG_CACHE
42
43#ifdef DEBUG_CACHE
44# define CACHE_PRINTF(...) printf(__VA_ARGS__)
45#else
46# define CACHE_PRINTF(...)
47#endif
48
49/* -------------------------------------------------------------------- */
52
53TracksMap *tracks_map_new(const char *object_name, int num_tracks)
54{
55 TracksMap *map = MEM_new_zeroed<TracksMap>("TrackingsMap");
56
57 STRNCPY(map->object_name, object_name);
58
59 map->num_tracks = num_tracks;
60
61 map->tracks = MEM_new_array<MovieTrackingTrack>(num_tracks, "TrackingsMap tracks");
62
63 map->hash = MEM_new<Map<MovieTrackingTrack *, MovieTrackingTrack *>>("TracksMap hash");
64
66
67 return map;
68}
69
71{
72 return map->num_tracks;
73}
74
76{
77 MovieTrackingTrack new_track = *track;
78
79 new_track.markers = MEM_dupalloc(new_track.markers);
80
81 map->tracks[map->ptr] = new_track;
82
83 map->hash->add(&map->tracks[map->ptr], track);
84
85 map->ptr++;
86}
87
89{
90 MovieTrackingTrack *track;
91 ListBaseT<MovieTrackingTrack> tracks = {nullptr, nullptr}, new_tracks = {nullptr, nullptr};
92 ListBaseT<MovieTrackingTrack> *old_tracks;
93
94 MovieTrackingObject *tracking_object = BKE_tracking_object_get_named(tracking, map->object_name);
95 if (!tracking_object) {
96 /* object was deleted by user, create new one */
97 tracking_object = BKE_tracking_object_add(tracking, map->object_name);
98 }
99
100 old_tracks = &tracking_object->tracks;
101
102 /* duplicate currently operating tracks to temporary list.
103 * this is needed to keep names in unique state and it's faster to change names
104 * of currently operating tracks (if needed)
105 */
106 for (int a = 0; a < map->num_tracks; a++) {
107 MovieTrackingTrack *old_track;
108 bool mapped_to_old = false;
109
110 track = &map->tracks[a];
111
112 /* find original of operating track in list of previously displayed tracks */
113 old_track = map->hash->lookup_default(track, nullptr);
114 if (old_track) {
115 if (BLI_findindex(old_tracks, old_track) != -1) {
116 BLI_remlink(old_tracks, old_track);
117
119
120 /* Copy flags like selection back to the track map. */
121 track->flag = old_track->flag;
122 track->pat_flag = old_track->pat_flag;
123 track->search_flag = old_track->search_flag;
124
125 /* Copy all the rest settings back from the map to the actual tracks. */
126 MEM_delete(old_track->markers);
127 *old_track = *track;
128 old_track->markers = MEM_dupalloc(old_track->markers);
129
131
132 BLI_addtail(&tracks, old_track);
133
134 mapped_to_old = true;
135 }
136 }
137
138 if (mapped_to_old == false) {
140
141 /* Update old-new track mapping */
142 map->hash->add(track, new_track);
143
144 BLI_addtail(&tracks, new_track);
145 }
146 }
147
148 /* move all tracks, which aren't operating */
149 track = old_tracks->first();
150 while (track) {
151 MovieTrackingTrack *next = track->next;
152 BLI_addtail(&new_tracks, track);
153 track = next;
154 }
155
156 /* now move all tracks which are currently operating and keep their names unique */
157 track = tracks.first();
158 while (track) {
159 MovieTrackingTrack *next = track->next;
160
161 BLI_remlink(&tracks, track);
162
163 track->next = track->prev = nullptr;
164 BLI_addtail(&new_tracks, track);
165
166 BLI_uniquename(&new_tracks,
167 track,
169 '.',
171 sizeof(track->name));
172
173 track = next;
174 }
175
176 *old_tracks = new_tracks;
177}
178
180{
181 MEM_delete(map->hash);
182
183 for (int i = 0; i < map->num_tracks; i++) {
185 }
186
187 MEM_delete(map->tracks);
188
189 BLI_spin_end(&map->spin_lock);
190
191 MEM_delete(map);
192}
193
195
196/* -------------------------------------------------------------------- */
199
200/* Three coordinate frames: Frame, Search, and Marker
201 * Two units: Pixels, Unified
202 * Notation: {coordinate frame}_{unit}; for example, "search_pixel" are search
203 * window relative coordinates in pixels, and "frame_unified" are unified 0..1
204 * coordinates relative to the entire frame.
205 */
206static void unified_to_pixel(int frame_width,
207 int frame_height,
208 const float unified_coords[2],
209 float pixel_coords[2])
210{
211 pixel_coords[0] = unified_coords[0] * frame_width;
212 pixel_coords[1] = unified_coords[1] * frame_height;
213}
214
216 const float marker_unified_coords[2],
217 float frame_unified_coords[2])
218{
219 frame_unified_coords[0] = marker_unified_coords[0] + marker->pos[0];
220 frame_unified_coords[1] = marker_unified_coords[1] + marker->pos[1];
221}
222
224 int frame_height,
225 const MovieTrackingMarker *marker,
226 const float marker_unified_coords[2],
227 float frame_pixel_coords[2])
228{
229 marker_to_frame_unified(marker, marker_unified_coords, frame_pixel_coords);
230 unified_to_pixel(frame_width, frame_height, frame_pixel_coords, frame_pixel_coords);
231}
232
234 int frame_height,
235 const MovieTrackingMarker *marker,
236 float frame_pixel[2])
237{
238 /* Get the lower left coordinate of the search window and snap to pixel coordinates */
240 frame_width, frame_height, marker, marker->search_min, frame_pixel);
241 frame_pixel[0] = int(frame_pixel[0]);
242 frame_pixel[1] = int(frame_pixel[1]);
243}
244
245static void pixel_to_unified(int frame_width,
246 int frame_height,
247 const float pixel_coords[2],
248 float unified_coords[2])
249{
250 unified_coords[0] = pixel_coords[0] / frame_width;
251 unified_coords[1] = pixel_coords[1] / frame_height;
252}
253
254static void marker_unified_to_search_pixel(int frame_width,
255 int frame_height,
256 const MovieTrackingMarker *marker,
257 const float marker_unified[2],
258 float search_pixel[2])
259{
260 float frame_pixel[2];
261 float search_origin_frame_pixel[2];
262
264 frame_width, frame_height, marker, marker_unified, frame_pixel);
266 frame_width, frame_height, marker, search_origin_frame_pixel);
267 sub_v2_v2v2(search_pixel, frame_pixel, search_origin_frame_pixel);
268}
269
270static void search_pixel_to_marker_unified(int frame_width,
271 int frame_height,
272 const MovieTrackingMarker *marker,
273 const float search_pixel[2],
274 float marker_unified[2])
275{
276 float frame_unified[2];
277 float search_origin_frame_pixel[2];
278
280 frame_width, frame_height, marker, search_origin_frame_pixel);
281 add_v2_v2v2(frame_unified, search_pixel, search_origin_frame_pixel);
282 pixel_to_unified(frame_width, frame_height, frame_unified, frame_unified);
283
284 /* marker pos is in frame unified */
285 sub_v2_v2v2(marker_unified, frame_unified, marker->pos);
286}
287
289 int frame_height,
290 const MovieTrackingMarker *marker,
291 double search_pixel_x[5],
292 double search_pixel_y[5])
293{
294 float unified_coords[2];
295 float pixel_coords[2];
296
297 /* Convert the corners into search space coordinates. */
298 for (int i = 0; i < 4; i++) {
300 frame_width, frame_height, marker, marker->pattern_corners[i], pixel_coords);
301 search_pixel_x[i] = pixel_coords[0] - 0.5f;
302 search_pixel_y[i] = pixel_coords[1] - 0.5f;
303 }
304
305 /* Convert the center position (aka "pos"); this is the origin */
306 unified_coords[0] = 0.0f;
307 unified_coords[1] = 0.0f;
308 marker_unified_to_search_pixel(frame_width, frame_height, marker, unified_coords, pixel_coords);
309
310 search_pixel_x[4] = pixel_coords[0] - 0.5f;
311 search_pixel_y[4] = pixel_coords[1] - 0.5f;
312}
313
315 int frame_height,
316 MovieTrackingMarker *marker,
317 const double search_pixel_x[5],
318 const double search_pixel_y[5])
319{
320 float marker_unified[2];
321 float search_pixel[2];
322
323 /* Convert the corners into search space coordinates. */
324 for (int i = 0; i < 4; i++) {
325 search_pixel[0] = search_pixel_x[i] + 0.5;
326 search_pixel[1] = search_pixel_y[i] + 0.5;
328 frame_width, frame_height, marker, search_pixel, marker->pattern_corners[i]);
329 }
330
331 /* Convert the center position (aka "pos"); this is the origin */
332 search_pixel[0] = search_pixel_x[4] + 0.5;
333 search_pixel[1] = search_pixel_y[4] + 0.5;
334 search_pixel_to_marker_unified(frame_width, frame_height, marker, search_pixel, marker_unified);
335
336 /* If the tracker tracked nothing, then "marker_unified" would be zero.
337 * Otherwise, the entire patch shifted, and that delta should be applied to
338 * all the coordinates.
339 */
340 for (int i = 0; i < 4; i++) {
341 marker->pattern_corners[i][0] -= marker_unified[0];
342 marker->pattern_corners[i][1] -= marker_unified[1];
343 }
344
345 marker->pos[0] += marker_unified[0];
346 marker->pos[1] += marker_unified[1];
347}
348
349void tracking_principal_point_normalized_to_pixel(const float principal_point_normalized[2],
350 const int frame_width,
351 const int frame_height,
352 float r_principal_point_pixel[2])
353{
354 const float frame_center_x = float(frame_width) / 2;
355 const float frame_center_y = float(frame_height) / 2;
356
357 r_principal_point_pixel[0] = frame_center_x + principal_point_normalized[0] * frame_center_x;
358 r_principal_point_pixel[1] = frame_center_y + principal_point_normalized[1] * frame_center_y;
359}
360
361void tracking_principal_point_pixel_to_normalized(const float principal_point_pixel[2],
362 const int frame_width,
363 const int frame_height,
364 float r_principal_point_normalized[2])
365{
366 const float frame_center_x = float(frame_width) / 2;
367 const float frame_center_y = float(frame_height) / 2;
368
369 r_principal_point_normalized[0] = (principal_point_pixel[0] - frame_center_x) / frame_center_x;
370 r_principal_point_normalized[1] = (principal_point_pixel[1] - frame_center_y) / frame_center_y;
371}
372
374
375/* -------------------------------------------------------------------- */
378
380 const MovieTrackingMarker *ref_marker,
381 bool before,
382 bool overwrite)
383{
384 MovieTrackingMarker marker_new;
385
386 marker_new = *ref_marker;
387 marker_new.flag &= ~MARKER_TRACKED;
388 marker_new.flag |= MARKER_DISABLED;
389
390 if (before) {
391 marker_new.framenr--;
392 }
393 else {
394 marker_new.framenr++;
395 }
396
397 if (overwrite || !BKE_tracking_track_has_marker_at_frame(track, marker_new.framenr)) {
398 BKE_tracking_marker_insert(track, &marker_new);
399 }
400}
401
403 const MovieTrackingCamera *camera, libmv_CameraIntrinsicsOptions *camera_intrinsics_options)
404{
405 switch (camera->distortion_model) {
407 camera_intrinsics_options->distortion_model = LIBMV_DISTORTION_MODEL_POLYNOMIAL;
408 camera_intrinsics_options->polynomial_k1 = camera->k1;
409 camera_intrinsics_options->polynomial_k2 = camera->k2;
410 camera_intrinsics_options->polynomial_k3 = camera->k3;
411 camera_intrinsics_options->polynomial_p1 = 0.0;
412 camera_intrinsics_options->polynomial_p2 = 0.0;
413 return;
414
416 camera_intrinsics_options->distortion_model = LIBMV_DISTORTION_MODEL_DIVISION;
417 camera_intrinsics_options->division_k1 = camera->division_k1;
418 camera_intrinsics_options->division_k2 = camera->division_k2;
419 return;
420
422 camera_intrinsics_options->distortion_model = LIBMV_DISTORTION_MODEL_NUKE;
423 camera_intrinsics_options->nuke_k1 = camera->nuke_k1;
424 camera_intrinsics_options->nuke_k2 = camera->nuke_k2;
425 camera_intrinsics_options->nuke_p1 = camera->nuke_p1;
426 camera_intrinsics_options->nuke_p2 = camera->nuke_p2;
427 return;
429 camera_intrinsics_options->distortion_model = LIBMV_DISTORTION_MODEL_BROWN;
430 camera_intrinsics_options->brown_k1 = camera->brown_k1;
431 camera_intrinsics_options->brown_k2 = camera->brown_k2;
432 camera_intrinsics_options->brown_k3 = camera->brown_k3;
433 camera_intrinsics_options->brown_k4 = camera->brown_k4;
434 camera_intrinsics_options->brown_p1 = camera->brown_p1;
435 camera_intrinsics_options->brown_p2 = camera->brown_p2;
436 return;
437 }
438
439 /* Unknown distortion model, which might be due to opening newer file in older Blender.
440 * Fall back to a known and supported model with 0 distortion. */
441 camera_intrinsics_options->distortion_model = LIBMV_DISTORTION_MODEL_POLYNOMIAL;
442 camera_intrinsics_options->polynomial_k1 = 0.0;
443 camera_intrinsics_options->polynomial_k2 = 0.0;
444 camera_intrinsics_options->polynomial_k3 = 0.0;
445 camera_intrinsics_options->polynomial_p1 = 0.0;
446 camera_intrinsics_options->polynomial_p2 = 0.0;
447}
448
450 const libmv_CameraIntrinsicsOptions *camera_intrinsics_options, MovieTrackingCamera *camera)
451{
452 switch (camera_intrinsics_options->distortion_model) {
455 camera->k1 = camera_intrinsics_options->polynomial_k1;
456 camera->k2 = camera_intrinsics_options->polynomial_k2;
457 camera->k3 = camera_intrinsics_options->polynomial_k3;
458 return;
459
461 camera->distortion_model = TRACKING_DISTORTION_MODEL_DIVISION;
462 camera->division_k1 = camera_intrinsics_options->division_k1;
463 camera->division_k2 = camera_intrinsics_options->division_k2;
464 return;
465
467 camera->distortion_model = TRACKING_DISTORTION_MODEL_NUKE;
468 camera->nuke_k1 = camera_intrinsics_options->nuke_k1;
469 camera->nuke_k2 = camera_intrinsics_options->nuke_k2;
470 camera->nuke_p1 = camera_intrinsics_options->nuke_p1;
471 camera->nuke_p2 = camera_intrinsics_options->nuke_p2;
472 return;
474 camera->distortion_model = TRACKING_DISTORTION_MODEL_BROWN;
475 camera->brown_k1 = camera_intrinsics_options->brown_k1;
476 camera->brown_k2 = camera_intrinsics_options->brown_k2;
477 camera->brown_k3 = camera_intrinsics_options->brown_k3;
478 camera->brown_k4 = camera_intrinsics_options->brown_k4;
479 camera->brown_p1 = camera_intrinsics_options->brown_p1;
480 camera->brown_p2 = camera_intrinsics_options->brown_p2;
481 return;
482 }
483
484 /* Libmv returned distortion model which is not known to Blender. This is a logical error in code
485 * and Blender side is to be updated to match Libmv. */
486 BLI_assert_msg(0, "Unknown distortion model");
487}
488
490 MovieTracking *tracking,
491 const int calibration_width,
492 const int calibration_height,
493 libmv_CameraIntrinsicsOptions *camera_intrinsics_options)
494{
495 MovieTrackingCamera *camera = &tracking->camera;
496 const float aspy = 1.0f / tracking->camera.pixel_aspect;
497
498 float principal_px[2];
500 camera->principal_point, calibration_width, calibration_height, principal_px);
501
502 camera_intrinsics_options->focal_length = camera->focal;
503
504 camera_intrinsics_options->principal_point_x = principal_px[0];
505 camera_intrinsics_options->principal_point_y = principal_px[1] * aspy;
506
507 distortion_model_parameters_from_tracking(camera, camera_intrinsics_options);
508
509 camera_intrinsics_options->image_width = calibration_width;
510 camera_intrinsics_options->image_height = int(calibration_height * aspy);
511}
512
514 MovieTracking *tracking, const libmv_CameraIntrinsicsOptions *camera_intrinsics_options)
515{
516 MovieTrackingCamera *camera = &tracking->camera;
517
518 camera->focal = camera_intrinsics_options->focal_length;
519
520 /* NOTE: The image size stored in the `camera_intrinsics_options` is aspect-ratio corrected,
521 * so there is no need to "un-apply" it from the principal point. */
522 const float principal_px[2] = {float(camera_intrinsics_options->principal_point_x),
523 float(camera_intrinsics_options->principal_point_y)};
524
526 camera_intrinsics_options->image_width,
527 camera_intrinsics_options->image_height,
528 camera->principal_point);
529
530 distortion_model_parameters_from_options(camera_intrinsics_options, camera);
531}
532
534 int current_frame,
535 bool backwards)
536{
537 MovieTrackingMarker *marker_keyed = nullptr;
538 MovieTrackingMarker *marker_keyed_fallback = nullptr;
539 int a = BKE_tracking_marker_get(track, current_frame) - track->markers;
540
541 while (a >= 0 && a < track->markersnr) {
542 int next = backwards ? a + 1 : a - 1;
543 bool is_keyframed = false;
544 MovieTrackingMarker *cur_marker = &track->markers[a];
545 MovieTrackingMarker *next_marker = nullptr;
546
547 if (next >= 0 && next < track->markersnr) {
548 next_marker = &track->markers[next];
549 }
550
551 if ((cur_marker->flag & MARKER_DISABLED) == 0) {
552 /* If it'll happen so we didn't find a real keyframe marker,
553 * fall back to the first marker in current tracked segment
554 * as a keyframe. */
555 if (next_marker == nullptr) {
556 /* Could happen when trying to get reference marker for the fist
557 * one on the segment which isn't surrounded by disabled markers.
558 *
559 * There's no really good choice here, just use the reference
560 * marker which looks correct.. */
561 if (marker_keyed_fallback == nullptr) {
562 marker_keyed_fallback = cur_marker;
563 }
564 }
565 else if (next_marker->flag & MARKER_DISABLED) {
566 if (marker_keyed_fallback == nullptr) {
567 marker_keyed_fallback = cur_marker;
568 }
569 }
570
571 is_keyframed |= (cur_marker->flag & MARKER_TRACKED) == 0;
572 }
573
574 if (is_keyframed) {
575 marker_keyed = cur_marker;
576
577 break;
578 }
579
580 a = next;
581 }
582
583 if (marker_keyed == nullptr) {
584 marker_keyed = marker_keyed_fallback;
585 }
586
587 return marker_keyed;
588}
589
591
592/* -------------------------------------------------------------------- */
595
597 int clip_index,
598 int frame)
599{
600 MovieClip *clip;
601 MovieClipUser user;
602 ImBuf *ibuf;
603 int scene_frame;
604
605 BLI_assert(clip_index < accessor->num_clips);
606
607 clip = accessor->clips[clip_index];
608 scene_frame = BKE_movieclip_remap_clip_to_scene_frame(clip, frame);
609 BKE_movieclip_user_set_frame(&user, scene_frame);
612 ibuf = BKE_movieclip_get_ibuf(clip, &user);
613
614 return ibuf;
615}
616
618{
619 ImBuf *grayscale = IMB_allocImBuf(ibuf->x, ibuf->y, ImBufFlags::Zero);
620
621 BLI_assert(ELEM(ibuf->channels, 3, 4));
622
623 /* TODO(sergey): Bummer, currently IMB API only allows to create 4 channels
624 * float buffer, so we do it manually here.
625 *
626 * Will generalize it later.
627 */
628 const size_t num_pixels = size_t(grayscale->x) * size_t(grayscale->y);
629 grayscale->channels = 1;
630 float *rect_float = MEM_new_array_zeroed<float>(num_pixels, "tracking grayscale image");
631 if (rect_float != nullptr) {
632 grayscale->assign_float_data(rect_float);
633
634 for (int i = 0; i < grayscale->x * grayscale->y; i++) {
635 const float *pixel = ibuf->float_data() + ibuf->channels * i;
636
637 rect_float[i] = 0.2126f * pixel[0] + 0.7152f * pixel[1] + 0.0722f * pixel[2];
638 }
639 }
640
641 return grayscale;
642}
643
644static void ibuf_to_float_image(ImBuf *ibuf, libmv_FloatImage *float_image)
645{
646 BLI_assert(ibuf->float_data() != nullptr);
647 float_image->buffer = ibuf->float_data_for_write();
648 float_image->width = ibuf->x;
649 float_image->height = ibuf->y;
650 float_image->channels = ibuf->channels;
651}
652
654{
655 ImBuf *ibuf = IMB_allocImBuf(float_image->width, float_image->height, ImBufFlags::Zero);
656 size_t num_total_channels = size_t(ibuf->x) * size_t(ibuf->y) * float_image->channels;
657 ibuf->channels = float_image->channels;
658 float *rect_float = MEM_new_array_zeroed<float>(num_total_channels, "tracking grayscale image");
659 if (rect_float != nullptr) {
660 ibuf->assign_float_data(rect_float);
661
662 memcpy(rect_float, float_image->buffer, num_total_channels * sizeof(float));
663 }
664 return ibuf;
665}
666
668 int clip_index,
669 int frame,
670 libmv_InputMode input_mode,
671 int downscale,
672 const libmv_Region *region,
674{
675 /* First try to get fully processed image from the cache. */
676 CACHE_PRINTF("Calculate new buffer for frame %d\n", frame);
677 /* And now we do post-processing of the original frame. */
678 ImBuf *orig_ibuf = accessor_get_preprocessed_ibuf(accessor, clip_index, frame);
679 if (orig_ibuf == nullptr) {
680 return nullptr;
681 }
682 ImBuf *final_ibuf;
683 /* Cut a region if requested. */
684 if (region != nullptr) {
685 int width = region->max[0] - region->min[0], height = region->max[1] - region->min[1];
686
687 /* If the requested region goes outside of the actual frame we still
688 * return the requested region size, but only fill it's partially with
689 * the data we can.
690 */
691 int clamped_origin_x = max_ii(int(region->min[0]), 0),
692 clamped_origin_y = max_ii(int(region->min[1]), 0);
693 int dst_offset_x = clamped_origin_x - int(region->min[0]),
694 dst_offset_y = clamped_origin_y - int(region->min[1]);
695 int clamped_width = width - dst_offset_x, clamped_height = height - dst_offset_y;
696 clamped_width = min_ii(clamped_width, orig_ibuf->x - clamped_origin_x);
697 clamped_height = min_ii(clamped_height, orig_ibuf->y - clamped_origin_y);
698
699 final_ibuf = IMB_allocImBuf(width, height, ImBufFlags::FloatData);
700
701 if (orig_ibuf->float_data() != nullptr) {
702 if (clamped_width > 0 && clamped_height > 0) {
704 int2(final_ibuf->x, final_ibuf->y),
705 orig_ibuf->float_data(),
706 int2(orig_ibuf->x, orig_ibuf->y),
707 orig_ibuf->channels,
708 int2(clamped_origin_x, clamped_origin_y),
709 int2(dst_offset_x, dst_offset_y),
710 int2(clamped_width, clamped_height));
711 }
712 }
713 else {
714 /* TODO(sergey): We don't do any color space or alpha conversion
715 * here. Probably Libmv is better to work in the linear space,
716 * but keep sRGB space here for compatibility for now.
717 */
718 const uchar *data_src = orig_ibuf->byte_data();
719 float *data_dst = final_ibuf->float_data_for_write();
720 for (int y = 0; y < clamped_height; y++) {
721 for (int x = 0; x < clamped_width; x++) {
722 int src_x = x + clamped_origin_x, src_y = y + clamped_origin_y;
723 int dst_x = x + dst_offset_x, dst_y = y + dst_offset_y;
724 int dst_index = (dst_y * width + dst_x) * 4,
725 src_index = (src_y * orig_ibuf->x + src_x) * 4;
726 rgba_uchar_to_float(data_dst + dst_index, data_src + src_index);
727 }
728 }
729 }
730 }
731 else {
732 /* Libmv only works with float images,
733 *
734 * This would likely make it so loads of float buffers are being stored
735 * in the cache which is nice on the one hand (faster re-use of the
736 * frames) but on the other hand it bumps the memory usage up.
737 */
739 IMB_float_from_byte(orig_ibuf);
741 final_ibuf = orig_ibuf;
742 }
743 /* Downscale if needed. */
744 if (downscale > 0) {
745 if (final_ibuf == orig_ibuf) {
746 final_ibuf = IMB_dupImBuf(orig_ibuf);
747 }
748 IMB_scale(final_ibuf,
749 orig_ibuf->x / (1 << downscale),
750 orig_ibuf->y / (1 << downscale),
752 false);
753 }
754 /* Apply possible transformation. */
755 if (transform != nullptr) {
756 libmv_FloatImage input_image, output_image;
757 ibuf_to_float_image(final_ibuf, &input_image);
758 libmv_frameAccessorgetTransformRun(transform, &input_image, &output_image);
759 if (final_ibuf != orig_ibuf) {
760 IMB_freeImBuf(final_ibuf);
761 }
762 final_ibuf = float_image_to_ibuf(&output_image);
763 libmv_floatImageDestroy(&output_image);
764 }
765 /* Transform number of channels. */
766 if (input_mode == LIBMV_IMAGE_MODE_RGBA) {
767 BLI_assert(ELEM(orig_ibuf->channels, 3, 4));
768 /* pass */
769 }
770 else /* if (input_mode == LIBMV_IMAGE_MODE_MONO) */ {
771 BLI_assert(input_mode == LIBMV_IMAGE_MODE_MONO);
772 if (final_ibuf->channels != 1) {
773 ImBuf *grayscale_ibuf = make_grayscale_ibuf_copy(final_ibuf);
774 if (final_ibuf != orig_ibuf) {
775 /* We dereference original frame later. */
776 IMB_freeImBuf(final_ibuf);
777 }
778 final_ibuf = grayscale_ibuf;
779 }
780 }
781 /* It's possible processing still didn't happen at this point,
782 * but we really need a copy of the buffer to be transformed
783 * and to be put to the cache.
784 */
785 if (final_ibuf == orig_ibuf) {
786 final_ibuf = IMB_dupImBuf(orig_ibuf);
787 }
788 IMB_freeImBuf(orig_ibuf);
789 return final_ibuf;
790}
791
793 int clip_index,
794 int frame,
795 libmv_InputMode input_mode,
796 int downscale,
797 const libmv_Region *region,
799 float **destination,
800 int *width,
801 int *height,
802 int *channels)
803{
804 TrackingImageAccessor *accessor = reinterpret_cast<TrackingImageAccessor *>(user_data);
805 ImBuf *ibuf;
806
807 BLI_assert(clip_index >= 0 && clip_index < accessor->num_clips);
808
809 ibuf = accessor_get_ibuf(accessor, clip_index, frame, input_mode, downscale, region, transform);
810
811 if (ibuf) {
812 *destination = ibuf->float_data_for_write();
813 *width = ibuf->x;
814 *height = ibuf->y;
815 *channels = ibuf->channels;
816 }
817 else {
818 *destination = nullptr;
819 *width = 0;
820 *height = 0;
821 *channels = 0;
822 }
823
824 return ibuf;
825}
826
828{
829 ImBuf *ibuf = static_cast<ImBuf *>(cache_key);
830 IMB_freeImBuf(ibuf);
831}
832
834 int clip_index,
835 int frame,
836 int track_index,
837 const libmv_Region *region,
838 float **r_destination,
839 int *r_width,
840 int *r_height)
841{
842 /* Perform sanity checks first. */
843 TrackingImageAccessor *accessor = reinterpret_cast<TrackingImageAccessor *>(user_data);
844 BLI_assert(clip_index < accessor->num_clips);
845 BLI_assert(track_index < accessor->num_tracks);
846 MovieTrackingTrack *track = accessor->tracks[track_index];
847 /* Early output, track does not use mask. */
848 if ((track->algorithm_flag & TRACK_ALGORITHM_FLAG_USE_MASK) == 0) {
849 return nullptr;
850 }
851 MovieClip *clip = accessor->clips[clip_index];
852 /* Construct fake user so we can access movie clip. */
853 MovieClipUser user;
854 int scene_frame = BKE_movieclip_remap_clip_to_scene_frame(clip, frame);
855 BKE_movieclip_user_set_frame(&user, scene_frame);
858 /* Get frame width and height so we can convert stroke coordinates
859 * and other things from normalized to pixel space.
860 */
861 int frame_width, frame_height;
862 BKE_movieclip_get_size(clip, &user, &frame_width, &frame_height);
863 /* Actual mask sampling. */
865 const float region_min[2] = {
866 region->min[0] - marker->pos[0] * frame_width,
867 region->min[1] - marker->pos[1] * frame_height,
868 };
869 const float region_max[2] = {
870 region->max[0] - marker->pos[0] * frame_width,
871 region->max[1] - marker->pos[1] * frame_height,
872 };
873 *r_destination = tracking_track_get_mask_for_region(
874 frame_width, frame_height, region_min, region_max, track);
875 *r_width = region->max[0] - region->min[0];
876 *r_height = region->max[1] - region->min[1];
877 return *r_destination;
878}
879
881{
882 if (cache_key != nullptr) {
883 float *mask = static_cast<float *>(cache_key);
884 MEM_delete(mask);
885 }
886}
887
889 int num_clips,
890 MovieTrackingTrack **tracks,
891 int num_tracks)
892{
894 "tracking image accessor");
895
896 BLI_assert(num_clips <= MAX_ACCESSOR_CLIP);
897
898 memcpy(accessor->clips, clips, num_clips * sizeof(MovieClip *));
899 accessor->num_clips = num_clips;
900
902 "image accessor tracks");
903 memcpy(accessor->tracks, tracks, num_tracks * sizeof(MovieTrackingTrack *));
904 accessor->num_tracks = num_tracks;
905
907 reinterpret_cast<libmv_FrameAccessorUserData *>(accessor),
912
913 BLI_spin_init(&accessor->cache_lock);
914
915 return accessor;
916}
917
919{
921 BLI_spin_end(&accessor->cache_lock);
922 MEM_delete(accessor->tracks);
923 MEM_delete(accessor);
924}
925
927
928} // namespace blender
#define CTX_DATA_(context, msgid)
#define BLT_I18NCONTEXT_ID_MOVIECLIP
Read Blender memory allocation and freeing.
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define offsetof(t, d)
void libmv_FrameAccessorDestroy(libmv_FrameAccessor *frame_accessor)
void libmv_frameAccessorgetTransformRun(const libmv_FrameTransform *transform, const libmv_FloatImage *input_image, libmv_FloatImage *output_image)
libmv_FrameAccessor * libmv_FrameAccessorNew(libmv_FrameAccessorUserData *user_data, libmv_GetImageCallback get_image_callback, libmv_ReleaseImageCallback release_image_callback, libmv_GetMaskForTrackCallback get_mask_for_track_callback, libmv_ReleaseMaskCallback release_mask_callback)
void camera(KernelGlobals &kg, const ShadingData &sd, float3 &outview, float &outdepth, float &outdist)
@ LIBMV_DISTORTION_MODEL_POLYNOMIAL
@ LIBMV_DISTORTION_MODEL_NUKE
@ LIBMV_DISTORTION_MODEL_BROWN
@ LIBMV_DISTORTION_MODEL_DIVISION
struct libmv_FrameTransform libmv_FrameTransform
void * libmv_CacheKey
struct libmv_FrameAccessorUserData libmv_FrameAccessorUserData
@ LIBMV_IMAGE_MODE_RGBA
@ LIBMV_IMAGE_MODE_MONO
void libmv_floatImageDestroy(libmv_FloatImage *image)
BMesh Iterators.
float * tracking_track_get_mask_for_region(const int frame_width, const int frame_height, const float region_min[2], const float region_max[2], const MovieTrackingTrack *track)
Definition tracking.cc:1141
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MovieTrackingMarker * BKE_tracking_marker_get_exact(MovieTrackingTrack *track, int framenr)
Definition tracking.cc:1396
MINLINE void rgba_uchar_to_float(float r_col[4], const unsigned char col_ub[4])
char * STRNCPY(char(&dst)[N], const char *src)
@ TRACK_ALGORITHM_FLAG_USE_MASK
void BKE_movieclip_get_size(MovieClip *clip, const MovieClipUser *user, int *r_width, int *r_height)
MINLINE void add_v2_v2v2(float r[2], const float a[2], const float b[2])
void tracking_get_search_origin_frame_pixel(int frame_width, int frame_height, const MovieTrackingMarker *marker, float frame_pixel[2])
MovieTrackingObject * BKE_tracking_object_add(MovieTracking *tracking, const char *name)
Definition tracking.cc:1902
void tracking_marker_insert_disabled(MovieTrackingTrack *track, const MovieTrackingMarker *ref_marker, bool before, bool overwrite)
static void search_pixel_to_marker_unified(int frame_width, int frame_height, const MovieTrackingMarker *marker, const float search_pixel[2], float marker_unified[2])
static ImBuf * accessor_get_preprocessed_ibuf(TrackingImageAccessor *accessor, int clip_index, int frame)
static void accessor_release_mask_callback(libmv_CacheKey cache_key)
@ TRACKING_DISTORTION_MODEL_DIVISION
@ TRACKING_DISTORTION_MODEL_POLYNOMIAL
@ TRACKING_DISTORTION_MODEL_NUKE
@ TRACKING_DISTORTION_MODEL_BROWN
void tracking_trackingCameraFromIntrinscisOptions(MovieTracking *tracking, const libmv_CameraIntrinsicsOptions *camera_intrinsics_options)
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
@ LOCK_MOVIECLIP
static void accessor_release_image_callback(libmv_CacheKey cache_key)
MINLINE int max_ii(int a, int b)
void tracks_map_insert(TracksMap *map, MovieTrackingTrack *track)
static void distortion_model_parameters_from_options(const libmv_CameraIntrinsicsOptions *camera_intrinsics_options, MovieTrackingCamera *camera)
static void unified_to_pixel(int frame_width, int frame_height, const float unified_coords[2], float pixel_coords[2])
@ MCLIP_PROXY_RENDER_SIZE_FULL
static void marker_to_frame_unified(const MovieTrackingMarker *marker, const float marker_unified_coords[2], float frame_unified_coords[2])
ImBuf * IMB_dupImBuf(const ImBuf *ibuf1)
MovieTrackingMarker * BKE_tracking_marker_insert(MovieTrackingTrack *track, MovieTrackingMarker *marker)
Definition tracking.cc:1240
static libmv_CacheKey accessor_get_mask_for_track_callback(libmv_FrameAccessorUserData *user_data, int clip_index, int frame, int track_index, const libmv_Region *region, float **r_destination, int *r_width, int *r_height)
void tracking_principal_point_normalized_to_pixel(const float principal_point_normalized[2], int frame_width, int frame_height, float r_principal_point_pixel[2])
void IMB_float_from_byte(ImBuf *ibuf)
MovieTrackingMarker * tracking_get_keyframed_marker(MovieTrackingTrack *track, int current_frame, bool backwards)
bool BKE_tracking_track_has_marker_at_frame(MovieTrackingTrack *track, int framenr)
Definition tracking.cc:715
void BLI_uniquename(const ListBase *list, void *vlink, const char *defname, char delim, int name_offset, size_t name_maxncpy) ATTR_NONNULL(1
void BLI_spin_end(SpinLock *spin)
Definition threads.cc:448
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
static void marker_unified_to_frame_pixel_coordinates(int frame_width, int frame_height, const MovieTrackingMarker *marker, const float marker_unified_coords[2], float frame_pixel_coords[2])
void tracking_cameraIntrinscisOptionsFromTracking(MovieTracking *tracking, int calibration_width, int calibration_height, libmv_CameraIntrinsicsOptions *camera_intrinsics_options)
static void distortion_model_parameters_from_tracking(const MovieTrackingCamera *camera, libmv_CameraIntrinsicsOptions *camera_intrinsics_options)
void IMB_freeImBuf(ImBuf *ibuf)
VecBase< int32_t, 2 > int2
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
unsigned char uchar
Definition BKE_text.h:58
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
static ImBuf * accessor_get_ibuf(TrackingImageAccessor *accessor, int clip_index, int frame, libmv_InputMode input_mode, int downscale, const libmv_Region *region, const libmv_FrameTransform *transform)
TrackingImageAccessor * tracking_image_accessor_new(MovieClip *clips[MAX_ACCESSOR_CLIP], int num_clips, MovieTrackingTrack **tracks, int num_tracks)
void tracking_set_marker_coords_from_tracking(int frame_width, int frame_height, MovieTrackingMarker *marker, const double search_pixel_x[5], const double search_pixel_y[5])
ImBuf * BKE_movieclip_get_ibuf(MovieClip *clip, const MovieClipUser *user)
static void pixel_to_unified(int frame_width, int frame_height, const float pixel_coords[2], float unified_coords[2])
void tracks_map_merge(TracksMap *map, MovieTracking *tracking)
void tracks_map_free(TracksMap *map)
void BKE_movieclip_user_set_frame(MovieClipUser *user, int framenr)
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
void BLI_spin_unlock(SpinLock *spin)
Definition threads.cc:433
void BKE_tracking_track_free(MovieTrackingTrack *track)
Definition tracking.cc:598
MovieTrackingTrack * BKE_tracking_track_duplicate(MovieTrackingTrack *track)
Definition tracking.cc:567
void BLI_spin_init(SpinLock *spin)
Definition threads.cc:394
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
TracksMap * tracks_map_new(const char *object_name, int num_tracks)
static void ibuf_to_float_image(ImBuf *ibuf, libmv_FloatImage *float_image)
static ImBuf * make_grayscale_ibuf_copy(ImBuf *ibuf)
float BKE_movieclip_remap_clip_to_scene_frame(const MovieClip *clip, float framenr)
MovieTrackingObject * BKE_tracking_object_get_named(MovieTracking *tracking, const char *name)
Definition tracking.cc:1971
int tracks_map_get_size(TracksMap *map)
bool IMB_scale(ImBuf *ibuf, int2 new_size, IMBScaleFilter filter, bool threaded=true)
Definition scaling.cc:467
void BLI_thread_unlock(int type)
Definition threads.cc:336
void tracking_principal_point_pixel_to_normalized(const float principal_point_pixel[2], int frame_width, int frame_height, float r_principal_point_normalized[2])
static ImBuf * float_image_to_ibuf(libmv_FloatImage *float_image)
static void marker_unified_to_search_pixel(int frame_width, int frame_height, const MovieTrackingMarker *marker, const float marker_unified[2], float search_pixel[2])
MINLINE int min_ii(int a, int b)
void BLI_spin_lock(SpinLock *spin)
Definition threads.cc:408
void tracking_get_marker_coords_for_tracking(int frame_width, int frame_height, const MovieTrackingMarker *marker, double search_pixel_x[5], double search_pixel_y[5])
void BLI_thread_lock(int type)
Definition threads.cc:331
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void IMB_copy_rect(float *dst, const int2 &dst_size, const float *src, const int2 &src_size, int channels, const int2 &src_rect_pos, const int2 &dst_rect_pos, const int2 &rect_size)
Definition rectop.cc:414
static libmv_CacheKey accessor_get_image_callback(libmv_FrameAccessorUserData *user_data, int clip_index, int frame, libmv_InputMode input_mode, int downscale, const libmv_Region *region, const libmv_FrameTransform *transform, float **destination, int *width, int *height, int *channels)
void tracking_image_accessor_destroy(TrackingImageAccessor *accessor)
MovieTrackingMarker * BKE_tracking_marker_get(MovieTrackingTrack *track, int framenr)
Definition tracking.cc:1363
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const uint8_t * byte_data() const
const float * float_data() const
float * float_data_for_write()
Definition allocimbuf.cc:79
void assign_float_data(float *data)
eMovieClipProxy_RenderFlag render_flag
eMovieClipProxy_RenderSize render_size
ListBaseT< MovieTrackingTrack > tracks
struct MovieTrackingTrack * next
struct MovieTrackingTrack * prev
MovieTrackingMarker * markers
TrackingAlgorithmFlag algorithm_flag
MovieTrackingCamera camera
MovieClip * clips[MAX_ACCESSOR_CLIP]
libmv_FrameAccessor * libmv_accessor
Map< MovieTrackingTrack *, MovieTrackingTrack * > * hash
char object_name[MAX_NAME]
MovieTrackingTrack * tracks
#define MAX_ACCESSOR_CLIP
#define CACHE_PRINTF(...)