Fix for crash when using 2D stabilization for float movie clips
authorSergey Sharybin <sergey.vfx@gmail.com>
Mon, 6 May 2013 17:59:02 +0000 (17:59 +0000)
committerSergey Sharybin <sergey.vfx@gmail.com>
Mon, 6 May 2013 17:59:02 +0000 (17:59 +0000)
Also removed unneeded image buffer scaling, it was only needed
for "early output" if there was no rotation. That is no longer
supported since it used to pixelate result a lot and interpolation
is always used now.

Saves quite a few of memory and CPU cycles.

source/blender/blenkernel/intern/tracking.c
source/blender/blenloader/intern/readfile.c
source/blender/makesdna/DNA_tracking_types.h

index d7ac08bf2bc3b518f7748088c250e1406373457b..c202a210de7e300ba922c4e3eb93c0c1f5e5a6aa 100644 (file)
@@ -154,9 +154,6 @@ void BKE_tracking_free(MovieTracking *tracking)
        tracking_reconstruction_free(&tracking->reconstruction);
        tracking_objects_free(&tracking->objects);
 
-       if (tracking->stabilization.scaleibuf)
-               IMB_freeImBuf(tracking->stabilization.scaleibuf);
-
        if (tracking->camera.intrinsics)
                BKE_tracking_distortion_free(tracking->camera.intrinsics);
 
@@ -3488,34 +3485,6 @@ static float stabilization_calculate_autoscale_factor(MovieTracking *tracking, i
        return stab->scale;
 }
 
-static ImBuf *stabilization_allocate_ibuf(ImBuf *cacheibuf, ImBuf *srcibuf, int fill)
-{
-       int flags;
-
-       if (cacheibuf && (cacheibuf->x != srcibuf->x || cacheibuf->y != srcibuf->y)) {
-               IMB_freeImBuf(cacheibuf);
-               cacheibuf = NULL;
-       }
-
-       flags = IB_rect;
-
-       if (srcibuf->rect_float)
-               flags |= IB_rectfloat;
-
-       if (cacheibuf) {
-               if (fill) {
-                       float col[4] = {0.0f, 0.0f, 0.0f, 0.0f};
-
-                       IMB_rectfill(cacheibuf, col);
-               }
-       }
-       else {
-               cacheibuf = IMB_allocImBuf(srcibuf->x, srcibuf->y, srcibuf->planes, flags);
-       }
-
-       return cacheibuf;
-}
-
 /* NOTE: frame number should be in clip space, not scene space */
 void BKE_tracking_stabilization_data_get(MovieTracking *tracking, int framenr, int width, int height,
                                          float loc[2], float *scale, float *angle)
@@ -3568,6 +3537,7 @@ ImBuf *BKE_tracking_stabilize_frame(MovieTracking *tracking, int framenr, ImBuf
        float mat[4][4];
        int j, filter = tracking->stabilization.filter;
        void (*interpolation)(struct ImBuf *, struct ImBuf *, float, float, int, int) = NULL;
+       int ibuf_flags;
 
        if (loc)
                copy_v2_v2(tloc, loc);
@@ -3575,6 +3545,7 @@ ImBuf *BKE_tracking_stabilize_frame(MovieTracking *tracking, int framenr, ImBuf
        if (scale)
                tscale = *scale;
 
+       /* Perform early output if no stabilization is used. */
        if ((stab->flag & TRACKING_2D_STABILIZATION) == 0) {
                if (loc)
                        zero_v2(loc);
@@ -3588,25 +3559,17 @@ ImBuf *BKE_tracking_stabilize_frame(MovieTracking *tracking, int framenr, ImBuf
                return ibuf;
        }
 
-       BKE_tracking_stabilization_data_get(tracking, framenr, width, height, tloc, &tscale, &tangle);
-
-       tmpibuf = stabilization_allocate_ibuf(NULL, ibuf, TRUE);
-
-       /* scale would be handled by matrix transformation when angle is non-zero */
-       if (tscale != 1.0f && tangle == 0.0f) {
-               ImBuf *scaleibuf;
-
-               stabilization_calculate_autoscale_factor(tracking, width, height);
-
-               scaleibuf = stabilization_allocate_ibuf(stab->scaleibuf, ibuf, 0);
-               stab->scaleibuf = scaleibuf;
-
-               IMB_rectcpy(scaleibuf, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
-               IMB_scalefastImBuf(scaleibuf, ibuf->x * tscale, ibuf->y * tscale);
+       /* Allocate frame for stabilization result. */
+       ibuf_flags = 0;
+       if (ibuf->rect)
+               ibuf_flags |= IB_rect;
+       if (ibuf->rect_float)
+               ibuf_flags |= IB_rectfloat;
 
-               ibuf = scaleibuf;
-       }
+       tmpibuf = IMB_allocImBuf(ibuf->x, ibuf->y, ibuf->planes, ibuf_flags);
 
+       /* Calculate stabilization matrix. */
+       BKE_tracking_stabilization_data_get(tracking, framenr, width, height, tloc, &tscale, &tangle);
        BKE_tracking_stabilization_data_to_mat4(ibuf->x, ibuf->y, aspect, tloc, tscale, tangle, mat);
        invert_m4(mat);
 
@@ -3627,7 +3590,7 @@ ImBuf *BKE_tracking_stabilize_frame(MovieTracking *tracking, int framenr, ImBuf
         * But need to keep an eye on this if the function will be
         * used in other cases.
         */
-    #pragma omp parallel for if(tmpibuf->y > 128)
+       #pragma omp parallel for if(tmpibuf->y > 128)
        for (j = 0; j < tmpibuf->y; j++) {
                int i;
                for (i = 0; i < tmpibuf->x; i++) {
@@ -3639,6 +3602,7 @@ ImBuf *BKE_tracking_stabilize_frame(MovieTracking *tracking, int framenr, ImBuf
                }
        }
 
+       /* TODO(sergey): we've got no mipmaps actually? */
        tmpibuf->userflags |= IB_MIPMAP_INVALID;
 
        if (tmpibuf->rect_float)
index 3b9ae245661fd5f2de7bd7f02532512da967ac11..fcd27e42c96481f3860efc875d0e06f90b030b56 100644 (file)
@@ -6633,7 +6633,6 @@ static void direct_link_movieclip(FileData *fd, MovieClip *clip)
        clip->tracking.stats = NULL;
 
        clip->tracking.stabilization.ok = 0;
-       clip->tracking.stabilization.scaleibuf = NULL;
        clip->tracking.stabilization.rot_track = newdataadr(fd, clip->tracking.stabilization.rot_track);
 
        clip->tracking.dopesheet.ok = 0;
index 73cda070fd273d166867e34a563c714b80804982..02c482096f68cef95c9cf7623acde18c6e7758d8 100644 (file)
@@ -205,8 +205,6 @@ typedef struct MovieTrackingStabilization {
        /* some pre-computing run-time variables */
        int ok;                     /* are precomputed values and scaled buf relevant? */
        float scale;                /* autoscale factor */
-
-       struct ImBuf *scaleibuf;    /* currently scaled ibuf */
 } MovieTrackingStabilization;
 
 typedef struct MovieTrackingReconstruction {