Blender V5.3
tracking_stabilize.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
10
11#include <climits>
12
13#include "DNA_movieclip_types.h"
14#include "DNA_scene_types.h"
15#include "RNA_prototypes.hh"
16
17#include "BLI_listbase.hh"
18#include "BLI_map.hh"
19#include "BLI_math_geom_c.hh"
21#include "BLI_math_vector_c.hh"
22#include "BLI_sort_utils.hh"
23#include "BLI_task_c.hh"
24
25#include "BKE_fcurve.hh"
26#include "BKE_movieclip.hh"
27#include "BKE_tracking.hh"
28
30#include "IMB_imbuf.hh"
31#include "IMB_imbuf_types.hh"
32#include "IMB_interp.hh"
33#include "MEM_guardedalloc.h"
34
35namespace blender {
36
37/* == Parameterization constants == */
38
39/* When measuring the scale changes relative to the rotation pivot point, it
40 * might happen accidentally that a probe point (tracking point), which doesn't
41 * actually move on a circular path, gets very close to the pivot point, causing
42 * the measured scale contribution to go toward infinity. We damp this undesired
43 * effect by adding a bias (floor) to the measured distances, which will
44 * dominate very small distances and thus cause the corresponding track's
45 * contribution to diminish.
46 * Measurements happen in normalized (0...1) coordinates within a frame.
47 */
48static float SCALE_ERROR_LIMIT_BIAS = 0.01f;
49
50/* When to consider a track as completely faded out.
51 * This is used in conjunction with the "disabled" flag of the track
52 * to determine start positions, end positions and gaps
53 */
54static float EPSILON_WEIGHT = 0.005f;
55
56/* == private working data == */
57
58/* Per track baseline for stabilization, defined at reference frame.
59 * A track's reference frame is chosen as close as possible to the (global)
60 * anchor_frame. Baseline holds the constant part of each track's contribution
61 * to the observed movement; it is calculated at initialization pass, using the
62 * measurement value at reference frame plus the average contribution to fill
63 * the gap between global anchor_frame and the reference frame for this track.
64 * This struct with private working data is associated to the local call context
65 * via `StabContext::private_track_data`
66 */
69
70 /* measured relative to translated pivot */
72
73 /* measured relative to translated pivot */
75
78};
79
80/* Tracks are reordered for initialization, starting as close as possible to
81 * anchor_frame
82 */
88
89/* Per frame private working data, for accessing possibly animated values. */
103
105 MovieTrackingTrack *track)
106{
107 return ctx->private_track_data->lookup_default(track, nullptr);
108}
109
111 MovieTrackingTrack *track,
112 TrackStabilizationBase *private_data)
113{
114 ctx->private_track_data->add(track, private_data);
115}
116
118{
119 if (val != nullptr) {
120 MEM_delete(val);
121 }
122}
123
124/* == access animated values for given frame == */
125
126static FCurve *retrieve_stab_animation(MovieClip *clip, const char *data_path, int idx)
127{
128 return id_data_find_fcurve(&clip->id,
129 &clip->tracking.stabilization,
130 RNA_MovieTrackingStabilization,
131 data_path,
132 idx,
133 nullptr);
134}
135
137{
138 return id_data_find_fcurve(&clip->id, track, RNA_MovieTrackingTrack, "weight_stab", 0, nullptr);
139}
140
141static float fetch_from_fcurve(const FCurve *animationCurve,
142 int framenr,
143 StabContext *ctx,
144 float default_value)
145{
146 if (ctx && ctx->use_animation && animationCurve) {
147 int scene_framenr = BKE_movieclip_remap_clip_to_scene_frame(ctx->clip, framenr);
148 return evaluate_fcurve(animationCurve, scene_framenr);
149 }
150 return default_value;
151}
152
153static float get_animated_locinf(StabContext *ctx, int framenr)
154{
155 return fetch_from_fcurve(ctx->locinf, framenr, ctx, ctx->stab->locinf);
156}
157
158static float get_animated_rotinf(StabContext *ctx, int framenr)
159{
160 return fetch_from_fcurve(ctx->rotinf, framenr, ctx, ctx->stab->rotinf);
161}
162
163static float get_animated_scaleinf(StabContext *ctx, int framenr)
164{
165 return fetch_from_fcurve(ctx->scaleinf, framenr, ctx, ctx->stab->scaleinf);
166}
167
168static void get_animated_target_pos(StabContext *ctx, int framenr, float target_pos[2])
169{
170 target_pos[0] = fetch_from_fcurve(ctx->target_pos[0], framenr, ctx, ctx->stab->target_pos[0]);
171 target_pos[1] = fetch_from_fcurve(ctx->target_pos[1], framenr, ctx, ctx->stab->target_pos[1]);
172}
173
174static float get_animated_target_rot(StabContext *ctx, int framenr)
175{
176 return fetch_from_fcurve(ctx->target_rot, framenr, ctx, ctx->stab->target_rot);
177}
178
179static float get_animated_target_scale(StabContext *ctx, int framenr)
180{
181 return fetch_from_fcurve(ctx->target_scale, framenr, ctx, ctx->stab->scale);
182}
183
184static float get_animated_weight(StabContext *ctx, MovieTrackingTrack *track, int framenr)
185{
187 if (working_data && working_data->track_weight_curve) {
188 int scene_framenr = BKE_movieclip_remap_clip_to_scene_frame(ctx->clip, framenr);
189 return evaluate_fcurve(working_data->track_weight_curve, scene_framenr);
190 }
191 /* Use weight at global 'current frame' as fallback default. */
192 return track->weight_stab;
193}
194
195static void use_values_from_fcurves(StabContext *ctx, bool toggle)
196{
197 if (ctx != nullptr) {
198 ctx->use_animation = toggle;
199 }
200}
201
202/* Prepare per call private working area.
203 * Used for access to possibly animated values: retrieve available F-Curves.
204 */
206{
207 StabContext *ctx = MEM_new_zeroed<StabContext>("2D stabilization animation runtime data");
208 ctx->clip = clip;
209 ctx->tracking = &clip->tracking;
210 ctx->stab = &clip->tracking.stabilization;
211 ctx->private_track_data = MEM_new<Map<MovieTrackingTrack *, TrackStabilizationBase *>>(
212 "2D stabilization per track private working data");
213 ctx->locinf = retrieve_stab_animation(clip, "influence_location", 0);
214 ctx->rotinf = retrieve_stab_animation(clip, "influence_rotation", 0);
215 ctx->scaleinf = retrieve_stab_animation(clip, "influence_scale", 0);
216 ctx->target_pos[0] = retrieve_stab_animation(clip, "target_pos", 0);
217 ctx->target_pos[1] = retrieve_stab_animation(clip, "target_pos", 1);
218 ctx->target_rot = retrieve_stab_animation(clip, "target_rot", 0);
219 ctx->target_scale = retrieve_stab_animation(clip, "target_zoom", 0);
220 ctx->use_animation = true;
221 return ctx;
222}
223
233{
234 if (ctx != nullptr) {
235 for (TrackStabilizationBase *data : ctx->private_track_data->values()) {
237 }
238 MEM_delete(ctx->private_track_data);
239 MEM_delete(ctx);
240 }
241}
242
244{
246 return (working_data != nullptr && working_data->is_init_for_stabilization);
247}
248
250{
251 return (track->flag & TRACK_USE_2D_STAB) && is_init_for_stabilization(ctx, track);
252}
253
255 MovieTrackingTrack *track,
256 MovieTrackingMarker *marker)
257{
258 return (marker->flag & MARKER_DISABLED) ||
259 (EPSILON_WEIGHT > get_animated_weight(ctx, track, marker->framenr));
260}
261
262static int search_closest_marker_index(MovieTrackingTrack *track, int ref_frame)
263{
264 const MovieTrackingMarker *marker = BKE_tracking_marker_get(track, ref_frame);
265 return marker - track->markers;
266}
267
269 StabContext *ctx, MovieTrackingTrack *track, int i, int ref_frame, int *next_higher)
270{
271 MovieTrackingMarker *markers = track->markers;
272 int end = track->markersnr;
273 BLI_assert(0 <= i && i < end);
274
275 while (i < end &&
276 (markers[i].framenr < ref_frame || is_effectively_disabled(ctx, track, &markers[i])))
277 {
278 i++;
279 }
280 if (i < end && markers[i].framenr < *next_higher) {
281 BLI_assert(markers[i].framenr >= ref_frame);
282 *next_higher = markers[i].framenr;
283 }
284}
285
287 StabContext *ctx, MovieTrackingTrack *track, int i, int ref_frame, int *next_lower)
288{
289 MovieTrackingMarker *markers = track->markers;
290 BLI_assert(0 <= i && i < track->markersnr);
291 while (i >= 0 &&
292 (markers[i].framenr > ref_frame || is_effectively_disabled(ctx, track, &markers[i])))
293 {
294 i--;
295 }
296 if (0 <= i && markers[i].framenr > *next_lower) {
297 BLI_assert(markers[i].framenr <= ref_frame);
298 *next_lower = markers[i].framenr;
299 }
300}
301
302/* Find closest frames with usable stabilization data.
303 * A frame counts as _usable_ when there is at least one track marked for
304 * translation stabilization, which has an enabled tracking marker at this very
305 * frame. We search both for the next lower and next higher position, to allow
306 * the caller to interpolate gaps and to extrapolate at the ends of the
307 * definition range. */
309 int framenr,
310 int *next_lower,
311 int *next_higher)
312{
313 MovieTrackingObject *tracking_camera_object = BKE_tracking_object_get_camera(ctx->tracking);
314
315 for (MovieTrackingTrack &track : tracking_camera_object->tracks) {
316 if (is_usable_for_stabilization(ctx, &track)) {
317 int startpoint = search_closest_marker_index(&track, framenr);
318 retrieve_next_higher_usable_frame(ctx, &track, startpoint, framenr, next_higher);
319 retrieve_next_lower_usable_frame(ctx, &track, startpoint, framenr, next_lower);
320 }
321 }
322}
323
324/* Find active (enabled) marker closest to the reference frame. */
326 MovieTrackingTrack *track,
327 int ref_frame)
328{
329 int next_lower = MINAFRAME;
330 int next_higher = MAXFRAME;
331 int i = search_closest_marker_index(track, ref_frame);
332 retrieve_next_higher_usable_frame(ctx, track, i, ref_frame, &next_higher);
333 retrieve_next_lower_usable_frame(ctx, track, i, ref_frame, &next_lower);
334
335 if ((next_higher - ref_frame) < (ref_frame - next_lower)) {
336 return BKE_tracking_marker_get_exact(track, next_higher);
337 }
338
339 return BKE_tracking_marker_get_exact(track, next_lower);
340}
341
342/* Retrieve tracking data, if available and applicable for this frame.
343 * The returned weight value signals the validity; data recorded for this
344 * tracking marker on the exact requested frame is output with the full weight
345 * of this track, while gaps in the data sequence cause the weight to go to zero.
346 */
348 MovieTrackingTrack *track,
349 int framenr,
350 float *r_weight)
351{
352 MovieTrackingMarker *marker = BKE_tracking_marker_get_exact(track, framenr);
353 if (marker != nullptr && !(marker->flag & MARKER_DISABLED)) {
354 *r_weight = get_animated_weight(ctx, track, framenr);
355 return marker;
356 }
357
358 /* No marker at this frame (=gap) or marker disabled. */
359 *r_weight = 0.0f;
360 return nullptr;
361}
362
363/* Define the reference point for rotation/scale measurement and compensation.
364 * The stabilizer works by assuming the image was distorted by a affine linear
365 * transform, i.e. it was rotated and stretched around this reference point
366 * (pivot point) and then shifted laterally. Any scale and orientation changes
367 * will be picked up relative to this point. And later the image will be
368 * stabilized by rotating around this point. The result can only be as
369 * accurate as this pivot point actually matches the real rotation center
370 * of the actual movements. Thus any scheme to define a pivot point is
371 * always guesswork.
372 *
373 * As a simple default, we use the weighted average of the location markers
374 * of the current frame as pivot point. TODO: It is planned to add further
375 * options, like e.g. anchoring the pivot point at the canvas. Moreover,
376 * it is planned to allow for a user controllable offset.
377 */
378static void setup_pivot(const float ref_pos[2], float r_pivot[2])
379{
380 zero_v2(r_pivot); /* TODO: add an animated offset position here. */
381 add_v2_v2(r_pivot, ref_pos);
382}
383
384/* Calculate the contribution of a single track at the time position (frame) of
385 * the given marker. Each track has a local reference frame, which is as close
386 * as possible to the global anchor_frame. Thus the translation contribution is
387 * comprised of the offset relative to the image position at that reference
388 * frame, plus a guess of the contribution for the time span between the
389 * anchor_frame and the local reference frame of this track. The constant part
390 * of this contribution is precomputed initially. At the anchor_frame, by
391 * definition the contribution of all tracks is zero, keeping the frame in place.
392 *
393 * track_ref is per track baseline contribution at reference frame; filled in at
394 * initialization
395 * marker is tracking data to use as contribution for current frame.
396 * result_offset is a total cumulated contribution of this track,
397 * relative to the stabilization anchor_frame,
398 * in normalized (0...1) coordinates.
399 */
401 MovieTrackingMarker *marker,
402 float result_offset[2])
403{
404 add_v2_v2v2(result_offset, track_ref->stabilization_offset_base, marker->pos);
405}
406
407/* Similar to the ::translation_contribution(), the rotation contribution is
408 * comprised of the contribution by this individual track, and the averaged
409 * contribution from anchor_frame to the ref point of this track.
410 * - Contribution is in terms of angles, -pi < angle < +pi, and all averaging
411 * happens in this domain.
412 * - Yet the actual measurement happens as vector between pivot and the current
413 * tracking point
414 * - Currently we use the center of frame as approximation for the rotation pivot
415 * point.
416 * - Moreover, the pivot point has to be compensated for the already determined
417 * shift offset, in order to get the pure rotation around the pivot.
418 * To turn this into a _contribution_, the likewise corrected angle at the
419 * reference frame has to be subtracted, to get only the pure angle difference
420 * this tracking point has captured.
421 * - To get from vectors to angles, we have to go through an arcus tangens,
422 * which involves the issue of the definition range: the resulting angles will
423 * flip by 360deg when the measured vector passes from the 2nd to the third
424 * quadrant, thus messing up the average calculation. Since _any_ tracking
425 * point might be used, these problems are quite common in practice.
426 * - Thus we perform the subtraction of the reference and the addition of the
427 * baseline contribution in polar coordinates as simple addition of angles;
428 * since these parts are fixed, we can bake them into a rotation matrix.
429 * With this approach, the border of the arcus tangens definition range will
430 * be reached only, when the _whole_ contribution approaches +- 180deg,
431 * meaning we've already tilted the frame upside down. This situation is way
432 * less common and can be tolerated.
433 * - As an additional feature, when activated, also changes in image scale
434 * relative to the rotation center can be picked up. To handle those values
435 * in the same framework, we average the scales as logarithms.
436 *
437 * aspect is a total aspect ratio of the undistorted image (includes fame and
438 * pixel aspect). The function returns a quality factor, which can be used
439 * to damp the contributions of points in close proximity to the pivot point,
440 * since such contributions might be dominated by rounding errors and thus
441 * poison the calculated average. When the quality factor goes towards zero,
442 * the weight of this contribution should be reduced accordingly.
443 */
445 MovieTrackingMarker *marker,
446 const float aspect,
447 const float pivot[2],
448 float *result_angle,
449 float *result_scale)
450{
451 float len, quality;
452 float pos[2];
453 sub_v2_v2v2(pos, marker->pos, pivot);
454
455 pos[0] *= aspect;
456 mul_m2_v2(track_ref->stabilization_rotation_base, pos);
457
458 *result_angle = atan2f(pos[1], pos[0]);
459
460 len = len_v2(pos);
461
462 /* prevent points very close to the pivot point from poisoning the result */
465
466 *result_scale = len * track_ref->stabilization_scale_base;
467 BLI_assert(0.0 < *result_scale);
468
469 return quality;
470}
471
472/* Workaround to allow for rotation around an arbitrary pivot point.
473 * Currently, the public API functions do not support this flexibility.
474 * Rather, rotation will always be applied around a fixed origin.
475 * As a workaround, we shift the image after rotation to match the
476 * desired rotation center. And since this offset needs to be applied
477 * after the rotation and scaling, we can collapse it with the
478 * translation compensation, which is also a lateral shift (offset).
479 * The offset to apply is intended_pivot - rotated_pivot
480 */
481static void compensate_rotation_center(const int size,
482 float aspect,
483 const float angle,
484 const float scale,
485 const float pivot[2],
486 float result_translation[2])
487{
488 const float origin[2] = {0.5f * aspect * size, 0.5f * size};
489 float intended_pivot[2], rotated_pivot[2];
490 float rotation_mat[2][2];
491
492 copy_v2_v2(intended_pivot, pivot);
493 copy_v2_v2(rotated_pivot, pivot);
494 angle_to_mat2(rotation_mat, +angle);
495 sub_v2_v2(rotated_pivot, origin);
496 mul_m2_v2(rotation_mat, rotated_pivot);
497 mul_v2_fl(rotated_pivot, scale);
498 add_v2_v2(rotated_pivot, origin);
499 add_v2_v2(result_translation, intended_pivot);
500 sub_v2_v2(result_translation, rotated_pivot);
501}
502
503/* Weighted average of the per track cumulated contributions at given frame.
504 * Returns truth if all desired calculations could be done and all averages are
505 * available.
506 *
507 * NOTE: Even if the result is not `true`, the returned translation and angle
508 * are always sensible and as good as can be. Especially in the
509 * initialization phase we might not be able to get any average (yet) or
510 * get only a translation value. Since initialization visits tracks in a
511 * specific order, starting from anchor_frame, the result is logically
512 * correct non the less. But under normal operation conditions,
513 * a result of `false` should disable the stabilization function
514 */
516 int framenr,
517 float aspect,
518 float r_translation[2],
519 float r_pivot[2],
520 float *r_angle,
521 float *r_scale_step)
522{
523 bool ok;
524 float weight_sum;
525 MovieTracking *tracking = ctx->tracking;
526 MovieTrackingStabilization *stab = &tracking->stabilization;
527 MovieTrackingObject *tracking_camera_object = BKE_tracking_object_get_camera(ctx->tracking);
528 float ref_pos[2];
530
531 zero_v2(r_translation);
532 *r_scale_step = 0.0f; /* logarithm */
533 *r_angle = 0.0f;
534
535 zero_v2(ref_pos);
536
537 ok = false;
538 weight_sum = 0.0f;
539 for (MovieTrackingTrack &track : tracking_camera_object->tracks) {
540 if (!is_init_for_stabilization(ctx, &track)) {
541 continue;
542 }
543 if (track.flag & TRACK_USE_2D_STAB) {
544 float weight = 0.0f;
545 MovieTrackingMarker *marker = get_tracking_data_point(ctx, &track, framenr, &weight);
546 if (marker) {
548 &track);
549 BLI_assert(stabilization_base != nullptr);
550 float offset[2];
551 weight_sum += weight;
552 translation_contribution(stabilization_base, marker, offset);
553 r_translation[0] += weight * offset[0];
554 r_translation[1] += weight * offset[1];
555 ref_pos[0] += weight * marker->pos[0];
556 ref_pos[1] += weight * marker->pos[1];
557 ok |= (weight_sum > EPSILON_WEIGHT);
558 }
559 }
560 }
561 if (!ok) {
562 return false;
563 }
564
565 ref_pos[0] /= weight_sum;
566 ref_pos[1] /= weight_sum;
567 r_translation[0] /= weight_sum;
568 r_translation[1] /= weight_sum;
569 setup_pivot(ref_pos, r_pivot);
570
571 if (!(stab->flag & TRACKING_STABILIZE_ROTATION)) {
572 return ok;
573 }
574
575 ok = false;
576 weight_sum = 0.0f;
577 for (MovieTrackingTrack &track : tracking_camera_object->tracks) {
578 if (!is_init_for_stabilization(ctx, &track)) {
579 continue;
580 }
581 if (track.flag & TRACK_USE_2D_STAB_ROT) {
582 float weight = 0.0f;
583 MovieTrackingMarker *marker = get_tracking_data_point(ctx, &track, framenr, &weight);
584 if (marker) {
586 &track);
587 BLI_assert(stabilization_base != nullptr);
588 float rotation, scale, quality;
589 quality = rotation_contribution(
590 stabilization_base, marker, aspect, r_pivot, &rotation, &scale);
591 const float quality_weight = weight * quality;
592 weight_sum += quality_weight;
593 *r_angle += rotation * quality_weight;
594 if (stab->flag & TRACKING_STABILIZE_SCALE) {
595 *r_scale_step += logf(scale) * quality_weight;
596 }
597 else {
598 *r_scale_step = 0;
599 }
600 /* NOTE: Use original marker weight and not the scaled one with the proximity here to allow
601 * simple stabilization setups when there is a single track in a close proximity of the
602 * center. */
603 ok |= (weight > EPSILON_WEIGHT);
604 }
605 }
606 }
607 if (ok) {
608 *r_scale_step /= weight_sum;
609 *r_angle /= weight_sum;
610 }
611 else {
612 /* We reach this point because translation could be calculated,
613 * but rotation/scale found no data to work on.
614 */
615 *r_scale_step = 0.0f;
616 *r_angle = 0.0f;
617 }
618 return true;
619}
620
621/* Calculate weight center of location tracks for given frame.
622 * This function performs similar calculations as average_track_contributions(),
623 * but does not require the tracks to be initialized for stabilization. Moreover,
624 * when there is no usable tracking data for the given frame number, data from
625 * a neighboring frame is used. Thus this function can be used to calculate
626 * a starting point on initialization.
627 */
628static void average_marker_positions(StabContext *ctx, int framenr, float r_ref_pos[2])
629{
630 bool ok = false;
631 float weight_sum;
632 MovieTrackingObject *tracking_camera_object = BKE_tracking_object_get_camera(ctx->tracking);
633
634 zero_v2(r_ref_pos);
635 weight_sum = 0.0f;
636 for (MovieTrackingTrack &track : tracking_camera_object->tracks) {
637 if (track.flag & TRACK_USE_2D_STAB) {
638 float weight = 0.0f;
639 MovieTrackingMarker *marker = get_tracking_data_point(ctx, &track, framenr, &weight);
640 if (marker) {
641 weight_sum += weight;
642 r_ref_pos[0] += weight * marker->pos[0];
643 r_ref_pos[1] += weight * marker->pos[1];
644 ok |= (weight_sum > EPSILON_WEIGHT);
645 }
646 }
647 }
648 if (ok) {
649 r_ref_pos[0] /= weight_sum;
650 r_ref_pos[1] /= weight_sum;
651 }
652 else {
653 /* No usable tracking data on any track on this frame.
654 * Use data from neighboring frames to extrapolate...
655 */
656 int next_lower = MINAFRAME;
657 int next_higher = MAXFRAME;
658 use_values_from_fcurves(ctx, true);
659 for (MovieTrackingTrack &track : tracking_camera_object->tracks) {
660 /* NOTE: we deliberately do not care if this track
661 * is already initialized for stabilization. */
662 if (track.flag & TRACK_USE_2D_STAB) {
663 int startpoint = search_closest_marker_index(&track, framenr);
664 retrieve_next_higher_usable_frame(ctx, &track, startpoint, framenr, &next_higher);
665 retrieve_next_lower_usable_frame(ctx, &track, startpoint, framenr, &next_lower);
666 }
667 }
668 if (next_lower >= MINFRAME) {
669 /* use next usable frame to the left.
670 * Also default to this frame when we're in a gap */
671 average_marker_positions(ctx, next_lower, r_ref_pos);
672 }
673 else if (next_higher < MAXFRAME) {
674 average_marker_positions(ctx, next_higher, r_ref_pos);
675 }
676 use_values_from_fcurves(ctx, false);
677 }
678}
679
680/* Linear interpolation of data retrieved at two measurement points.
681 * This function is used to fill gaps in the middle of the covered area,
682 * at frames without any usable tracks for stabilization.
683 *
684 * framenr is a position to interpolate for.
685 * frame_a is a valid measurement point below framenr
686 * frame_b is a valid measurement point above framenr
687 * Returns truth if both measurements could actually be retrieved.
688 * Otherwise output parameters remain unaltered
689 */
691 int framenr,
692 int frame_a,
693 int frame_b,
694 const float aspect,
695 float r_translation[2],
696 float r_pivot[2],
697 float *r_angle,
698 float *r_scale_step)
699{
700 float t, s;
701 float trans_a[2], trans_b[2];
702 float angle_a, angle_b;
703 float scale_a, scale_b;
704 float pivot_a[2], pivot_b[2];
705 bool success = false;
706
707 BLI_assert(frame_a <= frame_b);
708 BLI_assert(frame_a <= framenr);
709 BLI_assert(framenr <= frame_b);
710
711 t = (float(framenr) - frame_a) / (frame_b - frame_a);
712 s = 1.0f - t;
713
715 ctx, frame_a, aspect, trans_a, pivot_a, &angle_a, &scale_a);
716 if (!success) {
717 return false;
718 }
720 ctx, frame_b, aspect, trans_b, pivot_b, &angle_b, &scale_b);
721 if (!success) {
722 return false;
723 }
724
725 interp_v2_v2v2(r_translation, trans_a, trans_b, t);
726 interp_v2_v2v2(r_pivot, pivot_a, pivot_b, t);
727 *r_scale_step = s * scale_a + t * scale_b;
728 *r_angle = s * angle_a + t * angle_b;
729 return true;
730}
731
732/* Reorder tracks starting with those providing a tracking data frame
733 * closest to the global anchor_frame. Tracks with a gap at anchor_frame or
734 * starting farer away from anchor_frame altogether will be visited later.
735 * This allows to build up baseline contributions incrementally.
736 *
737 * order is an array for sorting the tracks. Must be of suitable size to hold
738 * all tracks.
739 * Returns number of actually usable tracks, can be less than the overall number
740 * of tracks.
741 *
742 * NOTE: After returning, the order array holds entries up to the number of
743 * usable tracks, appropriately sorted starting with the closest tracks.
744 * Initialization includes disabled tracks, since they might be enabled
745 * through automation later.
746 */
748{
749 size_t tracknr = 0;
750 MovieTracking *tracking = ctx->tracking;
751 MovieTrackingObject *tracking_camera_object = BKE_tracking_object_get_camera(tracking);
752 int anchor_frame = tracking->stabilization.anchor_frame;
753
754 for (MovieTrackingTrack &track : tracking_camera_object->tracks) {
755 MovieTrackingMarker *marker;
756 order[tracknr].data = &track;
757 marker = get_closest_marker(ctx, &track, anchor_frame);
758 if (marker != nullptr && (track.flag & (TRACK_USE_2D_STAB | TRACK_USE_2D_STAB_ROT))) {
759 order[tracknr].sort_value = abs(marker->framenr - anchor_frame);
760 order[tracknr].reference_frame = marker->framenr;
761 tracknr++;
762 }
763 }
764 if (tracknr) {
765 qsort(order, tracknr, sizeof(TrackInitOrder), BLI_sortutil_cmp_int);
766 }
767 return tracknr;
768}
769
770/* Setup the constant part of this track's contribution to the determined frame
771 * movement. Tracks usually don't provide tracking data for every frame. Thus,
772 * for determining data at a given frame, we split up the contribution into a
773 * part covered by actual measurements on this track, and the initial gap
774 * between this track's reference frame and the global anchor_frame.
775 * The (missing) data for the gap can be substituted by the average offset
776 * observed by the other tracks covering the gap. This approximation doesn't
777 * introduce wrong data, but it records data with incorrect weight. A totally
778 * correct solution would require us to average the contribution per frame, and
779 * then integrate stepwise over all frames -- which of course would be way more
780 * expensive, especially for longer clips. To the contrary, our solution
781 * cumulates the total contribution per track and averages afterwards over all
782 * tracks; it can thus be calculated just based on the data of a single frame,
783 * plus the "baseline" for the reference frame, which is what we are computing
784 * here.
785 *
786 * Since we're averaging _contributions_, we have to calculate the _difference_
787 * of the measured position at current frame and the position at the reference
788 * frame. But the "reference" part of this difference is constant and can thus
789 * be packed together with the baseline contribution into a single precomputed
790 * vector per track.
791 *
792 * In case of the rotation contribution, the principle is the same, but we have
793 * to compensate for the already determined translation and measure the pure
794 * rotation, simply because this is how we model the offset: shift plus rotation
795 * around the shifted rotation center. To circumvent problems with the
796 * definition range of the arcus tangens function, we perform this baseline
797 * addition and reference angle subtraction in polar coordinates and bake this
798 * operation into a precomputed rotation matrix.
799 *
800 * track is a track to be initialized to initialize
801 * reference_frame is a local frame for this track, the closest pick to the
802 * global anchor_frame.
803 * aspect is a total aspect ratio of the undistorted image (includes fame and
804 * pixel aspect).
805 * target_pos is a possibly animated target position as set by the user for
806 * the reference_frame
807 * average_translation is a value observed by the _other_ tracks for the gap
808 * between reference_frame and anchor_frame. This
809 * average must not contain contributions of frames
810 * not yet initialized
811 * average_angle in a similar way, the rotation value observed by the
812 * _other_ tracks.
813 * average_scale_step is an image scale factor observed on average by the other
814 * tracks for this frame. This value is recorded and
815 * averaged as logarithm. The recorded scale changes
816 * are damped for very small contributions, to limit
817 * the effect of probe points approaching the pivot
818 * too closely.
819 *
820 * NOTE: when done, this track is marked as initialized
821 */
823 MovieTrackingTrack *track,
824 int reference_frame,
825 float aspect,
826 const float average_translation[2],
827 const float pivot[2],
828 const float average_angle,
829 const float average_scale_step)
830{
831 float pos[2], angle, len;
833 MovieTrackingMarker *marker = BKE_tracking_marker_get_exact(track, reference_frame);
834 /* Logic for initialization order ensures there *is* a marker on that
835 * very frame.
836 */
837 BLI_assert(marker != nullptr);
838 BLI_assert(local_data != nullptr);
839
840 /* Per track baseline value for translation. */
841 sub_v2_v2v2(local_data->stabilization_offset_base, average_translation, marker->pos);
842
843 /* Per track baseline value for rotation. */
844 sub_v2_v2v2(pos, marker->pos, pivot);
845
846 pos[0] *= aspect;
847 angle = average_angle - atan2f(pos[1], pos[0]);
849
850 /* Per track baseline value for zoom. */
852 local_data->stabilization_scale_base = expf(average_scale_step) / len;
853
854 local_data->is_init_for_stabilization = true;
855}
856
857static void init_all_tracks(StabContext *ctx, float aspect)
858{
859 size_t track_len = 0;
860 MovieClip *clip = ctx->clip;
861 MovieTracking *tracking = ctx->tracking;
862 MovieTrackingObject *tracking_camera_object = BKE_tracking_object_get_camera(tracking);
863 TrackInitOrder *order;
864
865 /* Attempt to start initialization at anchor_frame.
866 * By definition, offset contribution is zero there.
867 */
868 int reference_frame = tracking->stabilization.anchor_frame;
869 float average_angle = 0, average_scale_step = 0;
870 float average_translation[2], average_pos[2], pivot[2];
871 zero_v2(average_translation);
872 zero_v2(pivot);
873
874 /* Initialize private working data. */
875 for (MovieTrackingTrack &track : tracking_camera_object->tracks) {
877 if (!local_data) {
879 "2D stabilization per track baseline data");
880 attach_stabilization_baseline_data(ctx, &track, local_data);
881 }
882 BLI_assert(local_data != nullptr);
883 local_data->track_weight_curve = retrieve_track_weight_animation(clip, &track);
884 local_data->is_init_for_stabilization = false;
885
886 track_len++;
887 }
888 if (!track_len) {
889 return;
890 }
891
892 order = MEM_new_array_zeroed<TrackInitOrder>(track_len, "stabilization track order");
893 if (!order) {
894 return;
895 }
896
897 track_len = establish_track_initialization_order(ctx, order);
898 if (track_len == 0) {
899 goto cleanup;
900 }
901
902 /* starting point for pivot, before having initialized any track */
903 average_marker_positions(ctx, reference_frame, average_pos);
904 setup_pivot(average_pos, pivot);
905
906 for (int i = 0; i < track_len; i++) {
907 MovieTrackingTrack *track = order[i].data;
908 if (reference_frame != order[i].reference_frame) {
909 reference_frame = order[i].reference_frame;
911 reference_frame,
912 aspect,
913 average_translation,
914 pivot,
915 &average_angle,
916 &average_scale_step);
917 }
919 track,
920 reference_frame,
921 aspect,
922 average_translation,
923 pivot,
924 average_angle,
925 average_scale_step);
926 }
927
928cleanup:
929 MEM_delete(order);
930}
931
932/* Retrieve the measurement of frame movement by averaging contributions of
933 * active tracks.
934 *
935 * translation is a measurement in normalized 0..1 coordinates.
936 * angle is a measurement in radians -pi..+pi counter clockwise relative to
937 * translation compensated frame center
938 * scale_step is a measurement of image scale changes, in logarithmic scale
939 * (zero means scale == 1)
940 * Returns calculation enabled and all data retrieved as expected for this frame.
941 *
942 * NOTE: when returning `false`, output parameters are reset to neutral values.
943 */
945 int framenr,
946 float aspect,
947 float r_translation[2],
948 float r_pivot[2],
949 float *r_angle,
950 float *r_scale_step)
951{
952 bool success = false;
953
954 /* Early output if stabilization is disabled. */
955 if ((ctx->stab->flag & TRACKING_2D_STABILIZATION) == 0) {
956 zero_v2(r_translation);
957 *r_scale_step = 0.0f;
958 *r_angle = 0.0f;
959 return false;
960 }
961
963 ctx, framenr, aspect, r_translation, r_pivot, r_angle, r_scale_step);
964 if (!success) {
965 /* Try to hold extrapolated settings beyond the definition range
966 * and to interpolate in gaps without any usable tracking data
967 * to prevent sudden jump to image zero position.
968 */
969 int next_lower = MINAFRAME;
970 int next_higher = MAXFRAME;
971 use_values_from_fcurves(ctx, true);
972 find_next_working_frames(ctx, framenr, &next_lower, &next_higher);
973 if (next_lower >= MINFRAME && next_higher < MAXFRAME) {
975 framenr,
976 next_lower,
977 next_higher,
978 aspect,
979 r_translation,
980 r_pivot,
981 r_angle,
982 r_scale_step);
983 }
984 else if (next_higher < MAXFRAME) {
985 /* Before start of stabilized range: extrapolate start point
986 * settings.
987 */
989 ctx, next_higher, aspect, r_translation, r_pivot, r_angle, r_scale_step);
990 }
991 else if (next_lower >= MINFRAME) {
992 /* After end of stabilized range: extrapolate end point settings. */
994 ctx, next_lower, aspect, r_translation, r_pivot, r_angle, r_scale_step);
995 }
996 use_values_from_fcurves(ctx, false);
997 }
998 return success;
999}
1000
1001/* Calculate stabilization data (translation, scale and rotation) from given raw
1002 * measurements. Result is in absolute image dimensions (expanded image, square
1003 * pixels), includes automatic or manual scaling and compensates for a target
1004 * frame position, if given.
1005 *
1006 * size is a size of the expanded image, the width in pixels is size * aspect.
1007 * aspect is a ratio (width / height) of the effective canvas (square pixels).
1008 * do_compensate denotes whether to actually output values necessary to
1009 * _compensate_ the determined frame movement.
1010 * Otherwise, the effective target movement is returned.
1011 */
1013 int framenr,
1014 int size,
1015 float aspect,
1016 bool do_compensate,
1017 float scale_step,
1018 float r_translation[2],
1019 float r_pivot[2],
1020 float *r_scale,
1021 float *r_angle)
1022{
1023 float target_pos[2], target_scale;
1024 float scaleinf = get_animated_scaleinf(ctx, framenr);
1025
1026 if (ctx->stab->flag & TRACKING_STABILIZE_SCALE) {
1027 *r_scale = expf(scale_step * scaleinf); /* Averaged in log scale */
1028 }
1029 else {
1030 *r_scale = 1.0f;
1031 }
1032
1033 mul_v2_fl(r_translation, get_animated_locinf(ctx, framenr));
1034 *r_angle *= get_animated_rotinf(ctx, framenr);
1035
1036 /* Compensate for a target frame position.
1037 * This allows to follow tracking / panning shots in a semi manual fashion,
1038 * when animating the settings for the target frame position.
1039 */
1040 get_animated_target_pos(ctx, framenr, target_pos);
1041 sub_v2_v2(r_translation, target_pos);
1042 *r_angle -= get_animated_target_rot(ctx, framenr);
1043 target_scale = get_animated_target_scale(ctx, framenr);
1044 if (target_scale != 0.0f) {
1045 *r_scale /= target_scale;
1046 /* target_scale is an expected/intended reference zoom value */
1047 }
1048
1049 /* Convert from relative to absolute coordinates, square pixels. */
1050 r_translation[0] *= float(size) * aspect;
1051 r_translation[1] *= float(size);
1052 r_pivot[0] *= float(size) * aspect;
1053 r_pivot[1] *= float(size);
1054
1055 /* Output measured data, or inverse of the measured values for
1056 * compensation?
1057 */
1058 if (do_compensate) {
1059 mul_v2_fl(r_translation, -1.0f);
1060 *r_angle *= -1.0f;
1061 if (*r_scale != 0.0f) {
1062 *r_scale = 1.0f / *r_scale;
1063 }
1064 }
1065}
1066
1067static void stabilization_data_to_mat4(float pixel_aspect,
1068 const float pivot[2],
1069 const float translation[2],
1070 float scale,
1071 float angle,
1072 float r_mat[4][4])
1073{
1074 float translation_mat[4][4], rotation_mat[4][4], scale_mat[4][4], pivot_mat[4][4],
1075 inv_pivot_mat[4][4], aspect_mat[4][4], inv_aspect_mat[4][4];
1076 const float scale_vector[3] = {scale, scale, 1.0f};
1077
1078 unit_m4(translation_mat);
1079 unit_m4(rotation_mat);
1080 unit_m4(scale_mat);
1081 unit_m4(aspect_mat);
1082 unit_m4(pivot_mat);
1083 unit_m4(inv_pivot_mat);
1084
1085 /* aspect ratio correction matrix */
1086 aspect_mat[0][0] /= pixel_aspect;
1087 invert_m4_m4(inv_aspect_mat, aspect_mat);
1088
1089 add_v2_v2(pivot_mat[3], pivot);
1090 sub_v2_v2(inv_pivot_mat[3], pivot);
1091
1092 size_to_mat4(scale_mat, scale_vector); /* scale matrix */
1093 add_v2_v2(translation_mat[3], translation); /* translation matrix */
1094 rotate_m4(rotation_mat, 'Z', angle); /* rotation matrix */
1095
1096 /* Compose transformation matrix. */
1097 mul_m4_series(r_mat,
1098 aspect_mat,
1099 translation_mat,
1100 pivot_mat,
1101 scale_mat,
1102 rotation_mat,
1103 inv_pivot_mat,
1104 inv_aspect_mat);
1105}
1106
1107/* Calculate scale factor necessary to eliminate black image areas
1108 * caused by the compensating movements of the stabilizer.
1109 * This function visits every frame where stabilization data is
1110 * available and determines the factor for this frame. The overall
1111 * largest factor found is returned as result.
1112 *
1113 * NOTE: all tracks need to be initialized before calling this function.
1114 */
1115static float calculate_autoscale_factor(StabContext *ctx, int size, float aspect)
1116{
1117 MovieTrackingStabilization *stab = ctx->stab;
1118 MovieTrackingObject *tracking_camera_object = BKE_tracking_object_get_camera(ctx->tracking);
1119 float pixel_aspect = ctx->tracking->camera.pixel_aspect;
1120 int height = size, width = aspect * size;
1121
1122 int sfra = INT_MAX, efra = INT_MIN;
1123 float scale = 1.0f, scale_step = 0.0f;
1124
1125 /* Calculate maximal frame range of tracks where stabilization is active. */
1126 for (MovieTrackingTrack &track : tracking_camera_object->tracks) {
1127 if ((track.flag & TRACK_USE_2D_STAB) ||
1129 {
1130 int first_frame = track.markers[0].framenr;
1131 int last_frame = track.markers[track.markersnr - 1].framenr;
1132 sfra = min_ii(sfra, first_frame);
1133 efra = max_ii(efra, last_frame);
1134 }
1135 }
1136
1137 use_values_from_fcurves(ctx, true);
1138 for (int cfra = sfra; cfra <= efra; cfra++) {
1139 float translation[2], pivot[2], angle, tmp_scale;
1140 float mat[4][4];
1141 const float points[4][2] = {
1142 {0.0f, 0.0f}, {0.0f, float(height)}, {float(width), float(height)}, {float(width), 0.0f}};
1143 const bool do_compensate = true;
1144 /* Calculate stabilization parameters for the current frame. */
1146 ctx, cfra, aspect, translation, pivot, &angle, &scale_step);
1148 cfra,
1149 size,
1150 aspect,
1151 do_compensate,
1152 scale_step,
1153 translation,
1154 pivot,
1155 &tmp_scale,
1156 &angle);
1157 /* Compose transformation matrix. */
1158 /* NOTE: Here we operate in NON-COMPENSATED coordinates, meaning we have
1159 * to construct transformation matrix using proper pivot point.
1160 * Compensation for that will happen later on.
1161 */
1162 stabilization_data_to_mat4(pixel_aspect, pivot, translation, tmp_scale, angle, mat);
1163 /* Investigate the transformed border lines for this frame;
1164 * find out, where it cuts the original frame.
1165 */
1166 for (int edge_index = 0; edge_index < 4; edge_index++) {
1167 /* Calculate coordinates of stabilized frame edge points.
1168 * Use matrix multiplication here so we operate in homogeneous
1169 * coordinates.
1170 */
1171 float stable_edge_p1[3], stable_edge_p2[3];
1172 copy_v2_v2(stable_edge_p1, points[edge_index]);
1173 copy_v2_v2(stable_edge_p2, points[(edge_index + 1) % 4]);
1174 stable_edge_p1[2] = stable_edge_p2[2] = 0.0f;
1175 mul_m4_v3(mat, stable_edge_p1);
1176 mul_m4_v3(mat, stable_edge_p2);
1177 /* Now we iterate over all original frame corners (we call them
1178 * 'point' here) to see if there's black area between stabilized
1179 * frame edge and original point.
1180 */
1181 for (int point_index = 0; point_index < 4; point_index++) {
1182 const float point[3] = {points[point_index][0], points[point_index][1], 0.0f};
1183 /* Calculate vector which goes from first edge point to
1184 * second one.
1185 */
1186 float stable_edge_vec[3];
1187 sub_v3_v3v3(stable_edge_vec, stable_edge_p2, stable_edge_p1);
1188 /* Calculate vector which connects current frame point to
1189 * first edge point.
1190 */
1191 float point_to_edge_start_vec[3];
1192 sub_v3_v3v3(point_to_edge_start_vec, point, stable_edge_p1);
1193 /* Use this two vectors to check whether frame point is inside
1194 * of the stabilized frame or not.
1195 * If the point is inside, there is no black area happening
1196 * and no scaling required for it.
1197 */
1198 if (cross_v2v2(stable_edge_vec, point_to_edge_start_vec) >= 0.0f) {
1199 /* We are scaling around motion-compensated pivot point. */
1200 float scale_pivot[2];
1201 add_v2_v2v2(scale_pivot, pivot, translation);
1202 /* Calculate line which goes via `point` and parallel to
1203 * the stabilized frame edge. This line is coming via
1204 * `point` and `point2` at the end.
1205 */
1206 float point2[2];
1207 add_v2_v2v2(point2, point, stable_edge_vec);
1208 /* Calculate actual distance between pivot point and
1209 * the stabilized frame edge. Then calculate distance
1210 * between pivot point and line which goes via actual
1211 * corner and is parallel to the edge.
1212 *
1213 * Dividing one by another will give us required scale
1214 * factor to get rid of black areas.
1215 */
1216 float real_dist = dist_to_line_v2(scale_pivot, stable_edge_p1, stable_edge_p2);
1217 float required_dist = dist_to_line_v2(scale_pivot, point, point2);
1218 const float S = required_dist / real_dist;
1219 scale = max_ff(scale, S);
1220 }
1221 }
1222 }
1223 }
1224 if (stab->maxscale > 0.0f) {
1225 scale = min_ff(scale, stab->maxscale);
1226 }
1227 use_values_from_fcurves(ctx, false);
1228
1229 return scale;
1230}
1231
1232/* Prepare working data and determine reference point for each track.
1233 *
1234 * NOTE: These calculations _could_ be cached and reused for all frames of the
1235 * same clip. However, since proper initialization depends on (weight)
1236 * animation and setup of tracks, ensuring consistency of cached init data
1237 * turns out to be tricky, hard to maintain and generally not worth the
1238 * effort. Thus we'll re-initialize on every frame.
1239 */
1240static StabContext *init_stabilizer(MovieClip *clip, int size, float aspect)
1241{
1243 BLI_assert(ctx != nullptr);
1244 init_all_tracks(ctx, aspect);
1245 if (ctx->stab->flag & TRACKING_AUTOSCALE) {
1246 ctx->stab->scale = 1.0;
1247 ctx->stab->scale = calculate_autoscale_factor(ctx, size, aspect);
1248 }
1249 /* By default, just use values for the global current frame. */
1250 use_values_from_fcurves(ctx, false);
1251 return ctx;
1252}
1253
1254/* === public interface functions === */
1255
1257 int framenr,
1258 int width,
1259 int height,
1260 float translation[2],
1261 float *scale,
1262 float *angle)
1263{
1264 StabContext *ctx = nullptr;
1265 MovieTracking *tracking = &clip->tracking;
1266 bool enabled = (tracking->stabilization.flag & TRACKING_2D_STABILIZATION);
1267 /* Might become a parameter of a stabilization compositor node. */
1268 bool do_compensate = true;
1269 float scale_step = 0.0f;
1270 float pixel_aspect = tracking->camera.pixel_aspect;
1271 float aspect = float(width) * pixel_aspect / height;
1272 int size = height;
1273 float pivot[2];
1274
1275 if (enabled) {
1276 ctx = init_stabilizer(clip, size, aspect);
1277 }
1278
1280 ctx, framenr, aspect, translation, pivot, angle, &scale_step))
1281 {
1283 ctx, framenr, size, aspect, do_compensate, scale_step, translation, pivot, scale, angle);
1284 compensate_rotation_center(size, aspect, *angle, *scale, pivot, translation);
1285 }
1286 else {
1287 zero_v2(translation);
1288 *scale = 1.0f;
1289 *angle = 0.0f;
1290 }
1292}
1293
1300
1301static void tracking_stabilize_frame_interpolation_cb(void *__restrict userdata,
1302 const int y,
1303 const TaskParallelTLS *__restrict /*tls*/)
1304{
1306 static_cast<TrackingStabilizeFrameInterpolationData *>(userdata);
1307 ImBuf *ibuf = data->ibuf;
1308 ImBuf *tmpibuf = data->tmpibuf;
1309 float (*mat)[4] = data->mat;
1310
1311 float vec[3] = {0.0f, float(y), 0.0f};
1312 float rvec[3];
1313
1314 if (ibuf->float_data()) {
1315 /* Float image. */
1316 float4 *dst = reinterpret_cast<float4 *>(tmpibuf->float_data_for_write()) + y * tmpibuf->x;
1317 if (data->tracking_filter == TRACKING_FILTER_BILINEAR) {
1318 for (int x = 0; x < tmpibuf->x; x++, dst++) {
1319 vec[0] = float(x);
1320 mul_v3_m4v3(rvec, mat, vec);
1321 *dst = imbuf::interpolate_bilinear_border_fl(ibuf, rvec[0], rvec[1]);
1322 }
1323 }
1324 else if (data->tracking_filter == TRACKING_FILTER_BICUBIC) {
1325 for (int x = 0; x < tmpibuf->x; x++, dst++) {
1326 vec[0] = float(x);
1327 mul_v3_m4v3(rvec, mat, vec);
1328 *dst = imbuf::interpolate_cubic_bspline_fl(ibuf, rvec[0], rvec[1]);
1329 }
1330 }
1331 else {
1332 /* Nearest or fall back to nearest. */
1333 for (int x = 0; x < tmpibuf->x; x++, dst++) {
1334 vec[0] = float(x);
1335 mul_v3_m4v3(rvec, mat, vec);
1336 *dst = imbuf::interpolate_nearest_border_fl(ibuf, rvec[0], rvec[1]);
1337 }
1338 }
1339 }
1340 else if (ibuf->byte_data()) {
1341 /* Byte image. */
1342 uchar4 *dst = reinterpret_cast<uchar4 *>(tmpibuf->byte_data_for_write()) + y * tmpibuf->x;
1343 if (data->tracking_filter == TRACKING_FILTER_BILINEAR) {
1344 for (int x = 0; x < tmpibuf->x; x++, dst++) {
1345 vec[0] = float(x);
1346 mul_v3_m4v3(rvec, mat, vec);
1347 *dst = imbuf::interpolate_bilinear_border_byte(ibuf, rvec[0], rvec[1]);
1348 }
1349 }
1350 else if (data->tracking_filter == TRACKING_FILTER_BICUBIC) {
1351 for (int x = 0; x < tmpibuf->x; x++, dst++) {
1352 vec[0] = float(x);
1353 mul_v3_m4v3(rvec, mat, vec);
1354 *dst = imbuf::interpolate_cubic_bspline_byte(ibuf, rvec[0], rvec[1]);
1355 }
1356 }
1357 else {
1358 /* Nearest or fall back to nearest. */
1359 for (int x = 0; x < tmpibuf->x; x++, dst++) {
1360 vec[0] = float(x);
1361 mul_v3_m4v3(rvec, mat, vec);
1362 *dst = imbuf::interpolate_nearest_border_byte(ibuf, rvec[0], rvec[1]);
1363 }
1364 }
1365 }
1366}
1367
1369 MovieClip *clip, int framenr, ImBuf *ibuf, float translation[2], float *scale, float *angle)
1370{
1371 float tloc[2], tscale, tangle;
1372 MovieTracking *tracking = &clip->tracking;
1373 MovieTrackingStabilization *stab = &tracking->stabilization;
1374 ImBuf *tmpibuf;
1375 int width = ibuf->x, height = ibuf->y;
1376 float pixel_aspect = tracking->camera.pixel_aspect;
1377 float mat[4][4];
1378
1379 if (translation) {
1380 copy_v2_v2(tloc, translation);
1381 }
1382
1383 if (scale) {
1384 tscale = *scale;
1385 }
1386
1387 /* Perform early output if no stabilization is used. */
1388 if ((stab->flag & TRACKING_2D_STABILIZATION) == 0) {
1389 if (translation) {
1390 zero_v2(translation);
1391 }
1392
1393 if (scale) {
1394 *scale = 1.0f;
1395 }
1396
1397 if (angle) {
1398 *angle = 0.0f;
1399 }
1400
1401 return ibuf;
1402 }
1403
1404 /* Allocate frame for stabilization result, copy alpha mode and color-space. */
1405 ImBufFlags ibuf_flags = ImBufFlags::Zero;
1406 if (ibuf->byte_data()) {
1407 ibuf_flags |= ImBufFlags::ByteData;
1408 }
1409 if (ibuf->float_data()) {
1410 ibuf_flags |= ImBufFlags::FloatData;
1411 }
1412
1413 tmpibuf = IMB_allocImBuf(ibuf->x, ibuf->y, ibuf_flags);
1414 tmpibuf->color_mode = ibuf->color_mode;
1415 IMB_colormanagement_copy_settings(ibuf, tmpibuf);
1416
1417 /* Calculate stabilization matrix. */
1418 BKE_tracking_stabilization_data_get(clip, framenr, width, height, tloc, &tscale, &tangle);
1420 ibuf->x, ibuf->y, pixel_aspect, tloc, tscale, tangle, mat);
1421
1422 /* The following code visits each nominal target grid position
1423 * and picks interpolated data "backwards" from source.
1424 * thus we need the inverse of the transformation to apply. */
1425 invert_m4(mat);
1426
1428 data.ibuf = ibuf;
1429 data.tmpibuf = tmpibuf;
1430 data.mat = mat;
1431 data.tracking_filter = TrackingStabilizationFilter(tracking->stabilization.filter);
1432
1433 TaskParallelSettings settings;
1435 settings.use_threading = (tmpibuf->y > 128);
1437 0, tmpibuf->y, &data, tracking_stabilize_frame_interpolation_cb, &settings);
1438
1439 if (translation) {
1440 copy_v2_v2(translation, tloc);
1441 }
1442
1443 if (scale) {
1444 *scale = tscale;
1445 }
1446
1447 if (angle) {
1448 *angle = tangle;
1449 }
1450
1451 return tmpibuf;
1452}
1453
1455 int buffer_height,
1456 float pixel_aspect,
1457 float translation[2],
1458 float scale,
1459 float angle,
1460 float r_mat[4][4])
1461{
1462 /* Since we cannot receive the real pivot point coordinates (API limitation),
1463 * we perform the rotation/scale around the center of frame.
1464 * Then we correct by an additional shift, which was calculated in
1465 * compensate_rotation_center() and "sneaked in" as additional offset
1466 * in the translation parameter. This works, since translation needs to be
1467 * applied after rotation/scale anyway. Thus effectively the image gets
1468 * rotated around the desired pivot point
1469 */
1470 /* TODO(sergey): pivot shouldn't be calculated here, rather received
1471 * as a parameter.
1472 */
1473 float pivot[2];
1474 pivot[0] = 0.5f * pixel_aspect * buffer_width;
1475 pivot[1] = 0.5f * buffer_height;
1476 /* Compose transformation matrix. */
1477 stabilization_data_to_mat4(pixel_aspect, pivot, translation, scale, angle, r_mat);
1478}
1479
1480} // namespace blender
#define mul_m4_series(...)
#define MINFRAME
#define MINAFRAME
#define MAXFRAME
Read Blender memory allocation and freeing.
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
#define logf(x)
#define expf(x)
#define INT_MAX
#define abs
float3x3 scale(float3x3 mat, float2 scale)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
float4 interpolate_nearest_border_fl(const ImBuf *in, float u, float v)
Definition IMB_interp.hh:29
uchar4 interpolate_nearest_border_byte(const ImBuf *in, float u, float v)
Definition IMB_interp.hh:25
uchar4 interpolate_cubic_bspline_byte(const ImBuf *in, float u, float v)
float4 interpolate_cubic_bspline_fl(const ImBuf *in, float u, float v)
uchar4 interpolate_bilinear_border_byte(const ImBuf *in, float u, float v)
Definition IMB_interp.hh:76
float4 interpolate_bilinear_border_fl(const ImBuf *in, float u, float v)
Definition IMB_interp.hh:80
BMesh Iterators.
MINLINE float min_ff(float a, float b)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static float get_animated_rotinf(StabContext *ctx, int framenr)
MovieTrackingMarker * BKE_tracking_marker_get_exact(MovieTrackingTrack *track, int framenr)
Definition tracking.cc:1396
VecBase< uint8_t, 4 > uchar4
static float get_animated_scaleinf(StabContext *ctx, int framenr)
static bool interpolate_averaged_track_contributions(StabContext *ctx, int framenr, int frame_a, int frame_b, const float aspect, float r_translation[2], float r_pivot[2], float *r_angle, float *r_scale_step)
static FCurve * retrieve_track_weight_animation(MovieClip *clip, MovieTrackingTrack *track)
static float get_animated_locinf(StabContext *ctx, int framenr)
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
MINLINE void add_v2_v2v2(float r[2], const float a[2], const float b[2])
static TrackStabilizationBase * access_stabilization_baseline_data(StabContext *ctx, MovieTrackingTrack *track)
static int establish_track_initialization_order(StabContext *ctx, TrackInitOrder *order)
MINLINE void mul_v2_fl(float r[2], float f)
MINLINE float cross_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
MINLINE void copy_v2_v2(float r[2], const float a[2])
void BKE_tracking_stabilization_data_get(MovieClip *clip, int framenr, int width, int height, float translation[2], float *scale, float *angle)
static bool stabilization_determine_offset_for_frame(StabContext *ctx, int framenr, float aspect, float r_translation[2], float r_pivot[2], float *r_angle, float *r_scale_step)
static bool is_init_for_stabilization(StabContext *ctx, MovieTrackingTrack *track)
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
static float SCALE_ERROR_LIMIT_BIAS
static void average_marker_positions(StabContext *ctx, int framenr, float r_ref_pos[2])
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
bool invert_m4(float mat[4][4])
MINLINE int max_ii(int a, int b)
void BLI_task_parallel_range(int start, int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
void BKE_tracking_stabilization_data_to_mat4(int width, int height, float aspect, float translation[2], float scale, float angle, float mat[4][4])
static float get_animated_target_scale(StabContext *ctx, int framenr)
VecBase< float, 4 > float4
static void setup_pivot(const float ref_pos[2], float r_pivot[2])
void mul_m4_v3(const float M[4][4], float r[3])
void size_to_mat4(float R[4][4], const float size[3])
static float EPSILON_WEIGHT
static float get_animated_weight(StabContext *ctx, MovieTrackingTrack *track, int framenr)
static float calculate_autoscale_factor(StabContext *ctx, int size, float aspect)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
MINLINE void add_v2_v2(float r[2], const float a[2])
static void stabilization_data_to_mat4(float pixel_aspect, const float pivot[2], const float translation[2], float scale, float angle, float r_mat[4][4])
static bool is_effectively_disabled(StabContext *ctx, MovieTrackingTrack *track, MovieTrackingMarker *marker)
BMesh const char void * data
float dist_to_line_v2(const float p[2], const float l1[2], const float l2[2])
Definition math_geom.cc:302
MovieTrackingObject * BKE_tracking_object_get_camera(const MovieTracking *tracking)
Definition tracking.cc:1987
static void stabilization_calculate_data(StabContext *ctx, int framenr, int size, float aspect, bool do_compensate, float scale_step, float r_translation[2], float r_pivot[2], float *r_scale, float *r_angle)
static float get_animated_target_rot(StabContext *ctx, int framenr)
static bool average_track_contributions(StabContext *ctx, int framenr, float aspect, float r_translation[2], float r_pivot[2], float *r_angle, float *r_scale_step)
static void use_values_from_fcurves(StabContext *ctx, bool toggle)
void unit_m4(float m[4][4])
static StabContext * init_stabilization_working_context(MovieClip *clip)
@ TRACKING_STABILIZE_ROTATION
static MovieTrackingMarker * get_tracking_data_point(StabContext *ctx, MovieTrackingTrack *track, int framenr, float *r_weight)
void mul_m2_v2(const float mat[2][2], float vec[2])
void angle_to_mat2(float R[2][2], float angle)
static void tracking_stabilize_frame_interpolation_cb(void *__restrict userdata, const int y, const TaskParallelTLS *__restrict)
static void discard_stabilization_working_context(StabContext *ctx)
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
static float fetch_from_fcurve(const FCurve *animationCurve, int framenr, StabContext *ctx, float default_value)
static void discard_stabilization_baseline_data(TrackStabilizationBase *val)
static void init_track_for_stabilization(StabContext *ctx, MovieTrackingTrack *track, int reference_frame, float aspect, const float average_translation[2], const float pivot[2], const float average_angle, const float average_scale_step)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
static void translation_contribution(TrackStabilizationBase *track_ref, MovieTrackingMarker *marker, float result_offset[2])
void rotate_m4(float mat[4][4], char axis, float angle)
void interp_v2_v2v2(float r[2], const float a[2], const float b[2], float t)
static FCurve * retrieve_stab_animation(MovieClip *clip, const char *data_path, int idx)
MINLINE float max_ff(float a, float b)
static void attach_stabilization_baseline_data(StabContext *ctx, MovieTrackingTrack *track, TrackStabilizationBase *private_data)
FCurve * id_data_find_fcurve(ID *id, void *data, StructRNA *type, const char *prop_name, int index, bool *r_driven)
static void get_animated_target_pos(StabContext *ctx, int framenr, float target_pos[2])
static bool is_usable_for_stabilization(StabContext *ctx, MovieTrackingTrack *track)
ImBuf * BKE_tracking_stabilize_frame(MovieClip *clip, int framenr, ImBuf *ibuf, float translation[2], float *scale, float *angle)
static StabContext * init_stabilizer(MovieClip *clip, int size, float aspect)
static MovieTrackingMarker * get_closest_marker(StabContext *ctx, MovieTrackingTrack *track, int ref_frame)
float BKE_movieclip_remap_clip_to_scene_frame(const MovieClip *clip, float framenr)
int BLI_sortutil_cmp_int(const void *a_, const void *b_)
Definition sort_utils.cc:55
MINLINE void sub_v2_v2(float r[2], const float a[2])
static void find_next_working_frames(StabContext *ctx, int framenr, int *next_lower, int *next_higher)
MINLINE float len_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
static void retrieve_next_lower_usable_frame(StabContext *ctx, MovieTrackingTrack *track, int i, int ref_frame, int *next_lower)
MINLINE int min_ii(int a, int b)
static void compensate_rotation_center(const int size, float aspect, const float angle, const float scale, const float pivot[2], float result_translation[2])
static float rotation_contribution(TrackStabilizationBase *track_ref, MovieTrackingMarker *marker, const float aspect, const float pivot[2], float *result_angle, float *result_scale)
MINLINE void zero_v2(float r[2])
static int search_closest_marker_index(MovieTrackingTrack *track, int ref_frame)
static void retrieve_next_higher_usable_frame(StabContext *ctx, MovieTrackingTrack *track, int i, int ref_frame, int *next_higher)
void IMB_colormanagement_copy_settings(ImBuf *ibuf_src, ImBuf *ibuf_dst)
static void init_all_tracks(StabContext *ctx, float aspect)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
float evaluate_fcurve(const FCurve *fcu, float evaltime)
MovieTrackingMarker * BKE_tracking_marker_get(MovieTrackingTrack *track, int framenr)
Definition tracking.cc:1363
#define atan2f
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
const float * float_data() const
ImColorMode color_mode
float * float_data_for_write()
Definition allocimbuf.cc:79
struct MovieTracking tracking
ListBaseT< MovieTrackingTrack > tracks
TrackingStabilizationFilter filter
MovieTrackingMarker * markers
MovieTrackingCamera camera
MovieTrackingStabilization stabilization
Map< MovieTrackingTrack *, TrackStabilizationBase * > * private_track_data
MovieTrackingStabilization * stab
MovieTrackingTrack * data