Revision: 38820
          
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=38820
Author:   nazgul
Date:     2011-07-29 15:25:00 +0000 (Fri, 29 Jul 2011)
Log Message:
-----------
Camera tracking integration
===========================

Implemented 2D stabilization:

- Based on average point of several tracks.
- Tracks for stabilization are controllable from
  "2D stabilization" panel.
- Implemented autoscaling. Could fail if average point is
  too close to frame boundary.
- Background clip in 3D viewport and MovieClip input
  compositor nodes are using stable shot.

Some internal weak things:
- Stable shots aren't cached in moviecache. There is
  only one "slot" for stable shot inside stabilization
  structure.
- This could fail if several threads are requested different
  frames (for example when starting playback, pause to make
  Nodes Editor trigger refresh, continue playback).
- Due to it shouldn't crash i decided to implement better
  caching for stable shots a bit later.

Modified Paths:
--------------
    branches/soc-2011-tomato/release/scripts/startup/bl_ui/space_clip.py
    branches/soc-2011-tomato/source/blender/blenkernel/BKE_movieclip.h
    branches/soc-2011-tomato/source/blender/blenkernel/BKE_tracking.h
    branches/soc-2011-tomato/source/blender/blenkernel/intern/depsgraph.c
    branches/soc-2011-tomato/source/blender/blenkernel/intern/movieclip.c
    branches/soc-2011-tomato/source/blender/blenkernel/intern/tracking.c
    branches/soc-2011-tomato/source/blender/blenloader/intern/readfile.c
    branches/soc-2011-tomato/source/blender/editors/include/ED_clip.h
    branches/soc-2011-tomato/source/blender/editors/space_clip/clip_draw.c
    branches/soc-2011-tomato/source/blender/editors/space_clip/clip_editor.c
    branches/soc-2011-tomato/source/blender/editors/space_clip/clip_intern.h
    branches/soc-2011-tomato/source/blender/editors/space_clip/clip_ops.c
    branches/soc-2011-tomato/source/blender/editors/space_clip/space_clip.c
    branches/soc-2011-tomato/source/blender/editors/space_clip/tracking_ops.c
    branches/soc-2011-tomato/source/blender/editors/space_view3d/view3d_draw.c
    branches/soc-2011-tomato/source/blender/makesdna/DNA_space_types.h
    branches/soc-2011-tomato/source/blender/makesdna/DNA_tracking_types.h
    branches/soc-2011-tomato/source/blender/makesrna/intern/rna_tracking.c
    
branches/soc-2011-tomato/source/blender/nodes/intern/CMP_nodes/CMP_movieclip.c
    branches/soc-2011-tomato/source/blender/nodes/intern/CMP_util.h

Modified: branches/soc-2011-tomato/release/scripts/startup/bl_ui/space_clip.py
===================================================================
--- branches/soc-2011-tomato/release/scripts/startup/bl_ui/space_clip.py        
2011-07-29 15:18:14 UTC (rev 38819)
+++ branches/soc-2011-tomato/release/scripts/startup/bl_ui/space_clip.py        
2011-07-29 15:25:00 UTC (rev 38820)
@@ -350,6 +350,44 @@
             layout.prop(clip, "display_aspect", text="")
 
 
+class CLIP_PT_stabilization(bpy.types.Panel):
+    bl_space_type = 'CLIP_EDITOR'
+    bl_region_type = 'UI'
+    bl_label = "2D Stabilization"
+    bl_options = {'DEFAULT_CLOSED'}
+
+    @classmethod
+    def poll(cls, context):
+        sc = context.space_data
+
+        return sc.clip
+
+    def draw_header(self, context):
+        sc = context.space_data
+        tracking = sc.clip.tracking
+        stab = tracking.stabilization
+
+        self.layout.prop(stab, "use_2d_stabilization", text="")
+
+    def draw(self, context):
+        layout = self.layout
+        sc = context.space_data
+        tracking = sc.clip.tracking
+        stab = tracking.stabilization
+
+        layout.active = stab.use_2d_stabilization
+
+        layout.prop(stab, "use_autoscale")
+
+        row = layout.row()
+        row.template_list(stab, "tracks", stab, "active_track_index", rows=3)
+
+        sub = row.column(align=True)
+        sub.operator("clip.stabilize_2d_add", icon='ZOOMIN', text="")
+        sub.operator("clip.stabilize_2d_remove", icon='ZOOMOUT', text="")
+        sub.menu('CLIP_MT_stabilize_2d_specials', text="", 
icon="DOWNARROW_HLT")
+
+
 class CLIP_PT_footage(bpy.types.Panel):
     bl_space_type = 'CLIP_EDITOR'
     bl_region_type = 'UI'
@@ -588,5 +626,14 @@
         layout.operator('clip.track_copy_color', icon='COPY_ID')
 
 
+class CLIP_MT_stabilize_2d_specials(bpy.types.Menu):
+    bl_label = "Track Color Specials"
+
+    def draw(self, context):
+        layout = self.layout
+
+        layout.operator('clip.stabilize_2d_select')
+
+
 if __name__ == "__main__":  # only for live edit.
     bpy.utils.register_module(__name__)

Modified: branches/soc-2011-tomato/source/blender/blenkernel/BKE_movieclip.h
===================================================================
--- branches/soc-2011-tomato/source/blender/blenkernel/BKE_movieclip.h  
2011-07-29 15:18:14 UTC (rev 38819)
+++ branches/soc-2011-tomato/source/blender/blenkernel/BKE_movieclip.h  
2011-07-29 15:25:00 UTC (rev 38820)
@@ -48,6 +48,7 @@
 void BKE_movieclip_reload(struct MovieClip *clip);
 
 struct ImBuf *BKE_movieclip_acquire_ibuf(struct MovieClip *clip, struct 
MovieClipUser *user);
+struct ImBuf *BKE_movieclip_acquire_stable_ibuf(struct MovieClip *clip, struct 
MovieClipUser *user, float mat[4][4]);
 void BKE_movieclip_acquire_size(struct MovieClip *clip, struct MovieClipUser 
*user, int *width, int *height);
 int BKE_movieclip_has_frame(struct MovieClip *clip, struct MovieClipUser 
*user);
 void BKE_movieclip_user_set_frame(struct MovieClipUser *user, int framenr);

Modified: branches/soc-2011-tomato/source/blender/blenkernel/BKE_tracking.h
===================================================================
--- branches/soc-2011-tomato/source/blender/blenkernel/BKE_tracking.h   
2011-07-29 15:18:14 UTC (rev 38819)
+++ branches/soc-2011-tomato/source/blender/blenkernel/BKE_tracking.h   
2011-07-29 15:25:00 UTC (rev 38820)
@@ -87,6 +87,8 @@
 
 struct MovieTrackingTrack *BKE_tracking_indexed_bundle(struct MovieTracking 
*tracking, int bundlenr);
 
+struct ImBuf *BKE_tracking_stabelize_shot(struct MovieTracking *tracking, int 
framenr, struct ImBuf *ibuf, float mat[4][4]);
+
 #define TRACK_SELECTED(track)                          
(((track->flag&TRACK_HIDDEN)==0) && ((track)->flag&SELECT || 
(track)->pat_flag&SELECT || (track)->search_flag&SELECT))
 #define TRACK_AREA_SELECTED(track, area)       
(((track->flag&TRACK_HIDDEN)==0) && 
((area)==TRACK_AREA_POINT?(track)->flag&SELECT : 
((area)==TRACK_AREA_PAT?(track)->pat_flag&SELECT:(track)->search_flag&SELECT)))
 

Modified: branches/soc-2011-tomato/source/blender/blenkernel/intern/depsgraph.c
===================================================================
--- branches/soc-2011-tomato/source/blender/blenkernel/intern/depsgraph.c       
2011-07-29 15:18:14 UTC (rev 38819)
+++ branches/soc-2011-tomato/source/blender/blenkernel/intern/depsgraph.c       
2011-07-29 15:25:00 UTC (rev 38820)
@@ -2482,6 +2482,8 @@
                }
 
                if(idtype == ID_MC) {
+                       bNode *node;
+
                        for(obt=bmain->object.first; obt; obt= obt->id.next){
                                bConstraint *con;
                                for (con = obt->constraints.first; con; 
con=con->next) {
@@ -2492,6 +2494,12 @@
                                        }
                                }
                        }
+
+                       for(node= sce->nodetree->nodes.first; node; node= 
node->next) {
+                               if(node->id==id) {
+                                       NodeTagChanged(sce->nodetree, node);
+                               }
+                       }
                }
 
                /* camera's matrix is used to orient reconstructed stuff,

Modified: branches/soc-2011-tomato/source/blender/blenkernel/intern/movieclip.c
===================================================================
--- branches/soc-2011-tomato/source/blender/blenkernel/intern/movieclip.c       
2011-07-29 15:18:14 UTC (rev 38819)
+++ branches/soc-2011-tomato/source/blender/blenkernel/intern/movieclip.c       
2011-07-29 15:25:00 UTC (rev 38820)
@@ -321,6 +321,42 @@
        return ibuf;
 }
 
+ImBuf *BKE_movieclip_acquire_stable_ibuf(MovieClip *clip, MovieClipUser *user, 
float mat[4][4])
+{
+       ImBuf *ibuf, *stableibuf;
+       int framenr= user?user->framenr:clip->lastframe;
+
+       ibuf= BKE_movieclip_acquire_ibuf(clip, user);
+
+       if(clip->tracking.stabilization.flag&TRACKING_2D_STABILIZATION) {
+               MovieTrackingStabilization *stab= &clip->tracking.stabilization;
+
+               BLI_lock_thread(LOCK_MOVIECLIP);
+
+               if(user->framenr!=stab->framenr)
+                       stab->ibufok= 0;
+
+               stableibuf= BKE_tracking_stabelize_shot(&clip->tracking, 
framenr, ibuf, mat);
+
+               stab->framenr= user->framenr;
+
+               BLI_unlock_thread(LOCK_MOVIECLIP);
+       } else {
+               if(mat)
+                       unit_m4(mat);
+
+               stableibuf= ibuf;
+       }
+
+       if(stableibuf!=ibuf) {
+               IMB_freeImBuf(ibuf);
+               ibuf= stableibuf;
+       }
+
+       return ibuf;
+
+}
+
 int BKE_movieclip_has_frame(MovieClip *clip, MovieClipUser *user)
 {
        ImBuf *ibuf= BKE_movieclip_acquire_ibuf(clip, user);
@@ -389,6 +425,9 @@
        /* clear cache */
        free_buffers(clip);
 
+       clip->tracking.stabilization.ok= 0;
+       clip->tracking.stabilization.ibufok= 0;
+
        /* update clip source */
        if(BLI_testextensie_array(clip->name, imb_ext_movie)) clip->source= 
MCLIP_SRC_MOVIE;
        else clip->source= MCLIP_SRC_SEQUENCE;

Modified: branches/soc-2011-tomato/source/blender/blenkernel/intern/tracking.c
===================================================================
--- branches/soc-2011-tomato/source/blender/blenkernel/intern/tracking.c        
2011-07-29 15:18:14 UTC (rev 38819)
+++ branches/soc-2011-tomato/source/blender/blenkernel/intern/tracking.c        
2011-07-29 15:25:00 UTC (rev 38820)
@@ -33,6 +33,7 @@
 #include <stddef.h>
 #include <limits.h>
 #include <math.h>
+#include <memory.h>
 
 #include "MEM_guardedalloc.h"
 
@@ -449,6 +450,12 @@
 
        if(tracking->camera.reconstructed)
                MEM_freeN(tracking->camera.reconstructed);
+
+       if(tracking->stabilization.ibuf)
+               IMB_freeImBuf(tracking->stabilization.ibuf);
+
+       if(tracking->stabilization.scaleibuf)
+               IMB_freeImBuf(tracking->stabilization.scaleibuf);
 }
 
 /*********************** tracking *************************/
@@ -1211,3 +1218,227 @@
 
        return NULL;
 }
+
+static int stabilization_median_point(MovieTracking *tracking, int framenr, 
float median[2])
+{
+       int ok= 0;
+       float min[2], max[2];
+       MovieTrackingTrack *track;
+
+       INIT_MINMAX2(min, max);
+
+       track= tracking->tracks.first;
+       while(track) {
+               if(track->flag&TRACK_USE_2D_STAB) {
+                       MovieTrackingMarker *marker= 
BKE_tracking_get_marker(track, framenr);
+
+                       DO_MINMAX2(marker->pos, min, max);
+
+                       ok= 1;
+               }
+
+               track= track->next;
+       }
+
+       median[0]= (max[0]+min[0])/2.f;
+       median[1]= (max[1]+min[1])/2.f;
+
+       return ok;
+}
+
+static float stabilization_auto_scale_factor(MovieTracking *tracking)
+{
+       float firstmedian[2];
+       MovieTrackingStabilization *stab= &tracking->stabilization;
+
+       if(stab->ok)
+               return stab->scale;
+
+       if(stabilization_median_point(tracking, 1, firstmedian)) {
+               int sfra= INT_MAX, efra= INT_MIN, cfra;
+               float delta[2]= {0.f, 0.f}, scalex, scaley, near[2]={1.f, 1.f};
+               MovieTrackingTrack *track;
+
+               track= tracking->tracks.first;
+               while(track) {
+                       if(track->flag&TRACK_USE_2D_STAB) {
+                               if(track->markersnr) {
+                                       sfra= MIN2(sfra, 
track->markers[0].framenr);
+                                       efra= MAX2(efra, 
track->markers[track->markersnr-1].framenr);
+                               }
+                       }
+
+                       track= track->next;
+               }
+
+               for(cfra=sfra; cfra<=efra; cfra++) {
+                       float median[2], d[2];
+
+                       stabilization_median_point(tracking, cfra, median);
+
+                       sub_v2_v2v2(d, firstmedian, median);
+                       d[0]= fabsf(d[0]);
+                       d[1]= fabsf(d[1]);
+
+                       delta[0]= MAX2(delta[0], d[0]);
+                       delta[1]= MAX2(delta[1], d[1]);
+
+                       near[0]= MIN3(near[0], median[0], 1.f-median[0]);
+                       near[1]= MIN3(near[1], median[1], 1.f-median[1]);
+               }
+
+               near[0]= MAX2(near[0], 0.05);
+               near[1]= MAX2(near[1], 0.05);
+
+               scalex= 1.f+delta[0]/near[0];
+               scaley= 1.f+delta[1]/near[1];
+
+               stab->scale= MAX2(scalex, scaley);
+       } else {
+               stab->scale= 1.f;
+       }
+
+       stab->ok= 1;
+
+       return stab->scale;
+}
+
+static void calculate_stabmat(MovieTrackingStabilization *stab, float width, 
float height,
+                       float firstmedian[2], float curmedian[2], float 
mat[4][4])
+{
+       unit_m4(mat);
+
+       mat[0][0]= stab->scale;
+       mat[1][1]= stab->scale;
+       mat[3][0]= (firstmedian[0]-curmedian[0])*width*stab->scale;
+       mat[3][1]= (firstmedian[1]-curmedian[1])*height*stab->scale;
+
+       mat[3][0]-= (firstmedian[0]*stab->scale-firstmedian[0])*width;
+       mat[3][1]-= (firstmedian[1]*stab->scale-firstmedian[1])*height;
+       /*mat[3][0]-= (width*stab->scale-width)/2.0f;
+       mat[3][1]-= (height*stab->scale-height)/2.0f;*/
+
+}
+
+static int stabelize_need_recalc(MovieTracking *tracking, float width, float 
height,
+                       float firstmedian[2], float curmedian[2], float 
mat[4][4])
+{
+       float stabmat[4][4];
+       MovieTrackingStabilization *stab= &tracking->stabilization;
+
+       if(!mat)
+               return 1;
+
+       if(stab->flag&TRACKING_AUTOSCALE)
+               stabilization_auto_scale_factor(tracking);
+
+       calculate_stabmat(stab, width, height, firstmedian, curmedian, stabmat);
+
+       return memcmp(mat, stabmat, sizeof(float)*16);
+}
+
+static ImBuf* stabelize_acquire_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;
+

@@ Diff output truncated at 10240 characters. @@
_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to