Blender V4.3
tracking_stabilize.cc File Reference
#include <climits>
#include "DNA_movieclip_types.h"
#include "DNA_scene_types.h"
#include "RNA_prototypes.hh"
#include "BLI_ghash.h"
#include "BLI_listbase.h"
#include "BLI_math_geom.h"
#include "BLI_math_rotation.h"
#include "BLI_math_vector.h"
#include "BLI_sort_utils.h"
#include "BLI_task.h"
#include "BLI_utildefines.h"
#include "BKE_fcurve.hh"
#include "BKE_movieclip.h"
#include "BKE_tracking.h"
#include "IMB_colormanagement.hh"
#include "IMB_imbuf.hh"
#include "IMB_imbuf_types.hh"
#include "IMB_interp.hh"
#include "MEM_guardedalloc.h"

Go to the source code of this file.

Classes

struct  TrackStabilizationBase
 
struct  TrackInitOrder
 
struct  StabContext
 
struct  TrackingStabilizeFrameInterpolationData
 

Functions

static TrackStabilizationBaseaccess_stabilization_baseline_data (StabContext *ctx, MovieTrackingTrack *track)
 
static void attach_stabilization_baseline_data (StabContext *ctx, MovieTrackingTrack *track, TrackStabilizationBase *private_data)
 
static void discard_stabilization_baseline_data (void *val)
 
static FCurveretrieve_stab_animation (MovieClip *clip, const char *data_path, int idx)
 
static FCurveretrieve_track_weight_animation (MovieClip *clip, MovieTrackingTrack *track)
 
static float fetch_from_fcurve (const FCurve *animationCurve, int framenr, StabContext *ctx, float default_value)
 
static float get_animated_locinf (StabContext *ctx, int framenr)
 
static float get_animated_rotinf (StabContext *ctx, int framenr)
 
static float get_animated_scaleinf (StabContext *ctx, int framenr)
 
static void get_animated_target_pos (StabContext *ctx, int framenr, float target_pos[2])
 
static float get_animated_target_rot (StabContext *ctx, int framenr)
 
static float get_animated_target_scale (StabContext *ctx, int framenr)
 
static float get_animated_weight (StabContext *ctx, MovieTrackingTrack *track, int framenr)
 
static void use_values_from_fcurves (StabContext *ctx, bool toggle)
 
static StabContextinit_stabilization_working_context (MovieClip *clip)
 
static void discard_stabilization_working_context (StabContext *ctx)
 
static bool is_init_for_stabilization (StabContext *ctx, MovieTrackingTrack *track)
 
static bool is_usable_for_stabilization (StabContext *ctx, MovieTrackingTrack *track)
 
static bool is_effectively_disabled (StabContext *ctx, MovieTrackingTrack *track, MovieTrackingMarker *marker)
 
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)
 
static void retrieve_next_lower_usable_frame (StabContext *ctx, MovieTrackingTrack *track, int i, int ref_frame, int *next_lower)
 
static void find_next_working_frames (StabContext *ctx, int framenr, int *next_lower, int *next_higher)
 
static MovieTrackingMarkerget_closest_marker (StabContext *ctx, MovieTrackingTrack *track, int ref_frame)
 
static MovieTrackingMarkerget_tracking_data_point (StabContext *ctx, MovieTrackingTrack *track, int framenr, float *r_weight)
 
static void setup_pivot (const float ref_pos[2], float r_pivot[2])
 
static void translation_contribution (TrackStabilizationBase *track_ref, MovieTrackingMarker *marker, float result_offset[2])
 
static float rotation_contribution (TrackStabilizationBase *track_ref, MovieTrackingMarker *marker, const float aspect, const float pivot[2], float *result_angle, float *result_scale)
 
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 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 average_marker_positions (StabContext *ctx, int framenr, float r_ref_pos[2])
 
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 int establish_track_initialization_order (StabContext *ctx, TrackInitOrder *order)
 
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)
 
static void init_all_tracks (StabContext *ctx, float aspect)
 
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 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 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 float calculate_autoscale_factor (StabContext *ctx, int size, float aspect)
 
static StabContextinit_stabilizer (MovieClip *clip, int size, float aspect)
 
void BKE_tracking_stabilization_data_get (MovieClip *clip, int framenr, int width, int height, float translation[2], float *scale, float *angle)
 
static void tracking_stabilize_frame_interpolation_cb (void *__restrict userdata, const int y, const TaskParallelTLS *__restrict)
 
ImBufBKE_tracking_stabilize_frame (MovieClip *clip, int framenr, ImBuf *ibuf, float translation[2], float *scale, float *angle)
 
void BKE_tracking_stabilization_data_to_mat4 (int buffer_width, int buffer_height, float pixel_aspect, float translation[2], float scale, float angle, float r_mat[4][4])
 

Variables

static float SCALE_ERROR_LIMIT_BIAS = 0.01f
 
static float EPSILON_WEIGHT = 0.005f
 

Detailed Description

This file contains implementation of 2D image stabilization.

Definition in file tracking_stabilize.cc.

Function Documentation

◆ access_stabilization_baseline_data()

◆ attach_stabilization_baseline_data()

static void attach_stabilization_baseline_data ( StabContext * ctx,
MovieTrackingTrack * track,
TrackStabilizationBase * private_data )
static

Definition at line 109 of file tracking_stabilize.cc.

References BLI_ghash_insert(), and StabContext::private_track_data.

Referenced by init_all_tracks().

◆ average_marker_positions()

◆ average_track_contributions()

◆ BKE_tracking_stabilization_data_get()

void BKE_tracking_stabilization_data_get ( struct MovieClip * clip,
int framenr,
int width,
int height,
float translation[2],
float * scale,
float * angle )

Get stabilization data (translation, scaling and angle) for a given frame. Returned data describes how to compensate the detected movement, but with any chosen scale factor already applied and any target frame position already compensated. In case stabilization fails or is disabled, neutral values are returned.

Parameters
framenris a frame number, relative to the clip (not relative to the scene timeline).
widthis an effective width of the canvas (square pixels), used to scale the determined translation.

Outputs:

Parameters
translationof the lateral shift, absolute canvas coordinates (square pixels).
scaleof the scaling to apply.
angleof the rotation angle, relative to the frame center.

TODO(sergey): Use r_ prefix for output parameters here.

Definition at line 1250 of file tracking_stabilize.cc.

References MovieTracking::camera, compensate_rotation_center(), discard_stabilization_working_context(), float, init_stabilizer(), MovieTrackingCamera::pixel_aspect, stabilization_calculate_data(), stabilization_determine_offset_for_frame(), TRACKING_2D_STABILIZATION, and zero_v2().

Referenced by BKE_tracking_stabilize_frame(), blender::nodes::node_composite_movieclip_cc::MovieClipOperation::compute_stabilization_data(), blender::compositor::MovieClipNode::convert_to_operations(), blender::nodes::node_composite_stabilize2d_cc::Stabilize2DOperation::execute(), and get_stable_cached_frame().

◆ BKE_tracking_stabilization_data_to_mat4()

void BKE_tracking_stabilization_data_to_mat4 ( int width,
int height,
float aspect,
float translation[2],
float scale,
float angle,
float mat[4][4] )

Build a 4x4 transformation matrix based on the given 2D stabilization data. mat is a 4x4 matrix in homogeneous coordinates, adapted to the final image buffer size and compensated for pixel aspect ratio, ready for direct OpenGL drawing.

TODO(sergey): The signature of this function should be changed. we actually don't need the dimensions of the image buffer. Instead we should consider to provide the pivot point of the rotation as a further stabilization data parameter.

Definition at line 1454 of file tracking_stabilize.cc.

References stabilization_data_to_mat4().

Referenced by BKE_tracking_stabilize_frame(), and clip_draw_main().

◆ BKE_tracking_stabilize_frame()

ImBuf * BKE_tracking_stabilize_frame ( struct MovieClip * clip,
int framenr,
struct ImBuf * ibuf,
float translation[2],
float * scale,
float * angle )

◆ calculate_autoscale_factor()

◆ compensate_rotation_center()

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

◆ discard_stabilization_baseline_data()

static void discard_stabilization_baseline_data ( void * val)
static

Definition at line 116 of file tracking_stabilize.cc.

References MEM_freeN().

Referenced by discard_stabilization_working_context().

◆ discard_stabilization_working_context()

static void discard_stabilization_working_context ( StabContext * ctx)
static

Discard all private working data attached to this call context.

Note
We allocate the record for the per track baseline contribution locally for each call context (i.e. call to BKE_tracking_stabilization_data_get) Thus it is correct to discard all allocations found within the corresponding local GHash.

Definition at line 230 of file tracking_stabilize.cc.

References BLI_ghash_free(), discard_stabilization_baseline_data(), MEM_freeN(), and StabContext::private_track_data.

Referenced by BKE_tracking_stabilization_data_get().

◆ establish_track_initialization_order()

◆ fetch_from_fcurve()

◆ find_next_working_frames()

◆ get_animated_locinf()

static float get_animated_locinf ( StabContext * ctx,
int framenr )
static

◆ get_animated_rotinf()

static float get_animated_rotinf ( StabContext * ctx,
int framenr )
static

◆ get_animated_scaleinf()

static float get_animated_scaleinf ( StabContext * ctx,
int framenr )
static

◆ get_animated_target_pos()

static void get_animated_target_pos ( StabContext * ctx,
int framenr,
float target_pos[2] )
static

◆ get_animated_target_rot()

static float get_animated_target_rot ( StabContext * ctx,
int framenr )
static

◆ get_animated_target_scale()

static float get_animated_target_scale ( StabContext * ctx,
int framenr )
static

◆ get_animated_weight()

◆ get_closest_marker()

◆ get_tracking_data_point()

static MovieTrackingMarker * get_tracking_data_point ( StabContext * ctx,
MovieTrackingTrack * track,
int framenr,
float * r_weight )
static

◆ init_all_tracks()

◆ init_stabilization_working_context()

◆ init_stabilizer()

◆ init_track_for_stabilization()

◆ interpolate_averaged_track_contributions()

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

◆ is_effectively_disabled()

◆ is_init_for_stabilization()

static bool is_init_for_stabilization ( StabContext * ctx,
MovieTrackingTrack * track )
static

◆ is_usable_for_stabilization()

static bool is_usable_for_stabilization ( StabContext * ctx,
MovieTrackingTrack * track )
static

◆ retrieve_next_higher_usable_frame()

static void retrieve_next_higher_usable_frame ( StabContext * ctx,
MovieTrackingTrack * track,
int i,
int ref_frame,
int * next_higher )
static

◆ retrieve_next_lower_usable_frame()

static void retrieve_next_lower_usable_frame ( StabContext * ctx,
MovieTrackingTrack * track,
int i,
int ref_frame,
int * next_lower )
static

◆ retrieve_stab_animation()

static FCurve * retrieve_stab_animation ( MovieClip * clip,
const char * data_path,
int idx )
static

Definition at line 125 of file tracking_stabilize.cc.

References id_data_find_fcurve().

Referenced by init_stabilization_working_context().

◆ retrieve_track_weight_animation()

static FCurve * retrieve_track_weight_animation ( MovieClip * clip,
MovieTrackingTrack * track )
static

Definition at line 135 of file tracking_stabilize.cc.

References id_data_find_fcurve().

Referenced by init_all_tracks().

◆ rotation_contribution()

static float rotation_contribution ( TrackStabilizationBase * track_ref,
MovieTrackingMarker * marker,
const float aspect,
const float pivot[2],
float * result_angle,
float * result_scale )
static

◆ search_closest_marker_index()

static int search_closest_marker_index ( MovieTrackingTrack * track,
int ref_frame )
static

◆ setup_pivot()

static void setup_pivot ( const float ref_pos[2],
float r_pivot[2] )
static

Definition at line 373 of file tracking_stabilize.cc.

References add_v2_v2(), and zero_v2().

Referenced by average_track_contributions(), and init_all_tracks().

◆ stabilization_calculate_data()

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

◆ stabilization_data_to_mat4()

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

◆ stabilization_determine_offset_for_frame()

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

◆ tracking_stabilize_frame_interpolation_cb()

static void tracking_stabilize_frame_interpolation_cb ( void *__restrict userdata,
const int y,
const TaskParallelTLS * __restrict )
static

◆ translation_contribution()

static void translation_contribution ( TrackStabilizationBase * track_ref,
MovieTrackingMarker * marker,
float result_offset[2] )
static

◆ use_values_from_fcurves()

static void use_values_from_fcurves ( StabContext * ctx,
bool toggle )
static

Variable Documentation

◆ EPSILON_WEIGHT

float EPSILON_WEIGHT = 0.005f
static

◆ SCALE_ERROR_LIMIT_BIAS

float SCALE_ERROR_LIMIT_BIAS = 0.01f
static

Definition at line 47 of file tracking_stabilize.cc.

Referenced by init_track_for_stabilization(), and rotation_contribution().