Camera tracking integration
authorSergey Sharybin <sergey.vfx@gmail.com>
Sun, 17 Jul 2011 17:26:32 +0000 (17:26 +0000)
committerSergey Sharybin <sergey.vfx@gmail.com>
Sun, 17 Jul 2011 17:26:32 +0000 (17:26 +0000)
===========================

Added compositor node "Movie Clip"

There could be some problems due to bug #27997, but
heneral workflow works fine here.

13 files changed:
source/blender/blenkernel/BKE_node.h
source/blender/blenkernel/intern/movieclip.c
source/blender/blenkernel/intern/node.c
source/blender/blenlib/BLI_threads.h
source/blender/blenlib/intern/threads.c
source/blender/editors/space_clip/clip_draw.c
source/blender/editors/space_node/drawnode.c
source/blender/makesrna/intern/rna_nodetree.c
source/blender/makesrna/intern/rna_nodetree_types.h
source/blender/nodes/CMP_node.h
source/blender/nodes/CMakeLists.txt
source/blender/nodes/intern/CMP_nodes/CMP_movieclip.c [new file with mode: 0644]
source/blender/nodes/intern/CMP_util.h

index e44b5d96852151748a4694bbf2cba30f83672a88..91c2666e83dabad1da87237d11594324eb3aa9df 100644 (file)
@@ -387,6 +387,7 @@ void                        ntreeGPUMaterialNodes(struct bNodeTree *ntree, struct GPUMaterial *mat);
 #define CMP_NODE_COLOR_MATTE 259
 #define CMP_NODE_COLORBALANCE 260
 #define CMP_NODE_HUECORRECT 261
+#define CMP_NODE_MOVIECLIP     262
 
 #define CMP_NODE_GLARE         301
 #define CMP_NODE_TONEMAP       302
index ef3cd1eb4508861b3a0db1c66e02099aaedbd8b7..ae819b4d61f5d30dcd982ee49d701dde06929b9f 100644 (file)
 #include "DNA_movieclip_types.h"
 #include "DNA_object_types.h"  /* SELECT */
 
-#include "BLI_blenlib.h"
 #include "BLI_utildefines.h"
+
+#include "BLI_blenlib.h"
 #include "BLI_ghash.h"
-#include "BLI_mempool.h"
 #include "BLI_math.h"
+#include "BLI_mempool.h"
+#include "BLI_threads.h"
 
 #include "BKE_library.h"
 #include "BKE_global.h"
@@ -288,14 +290,21 @@ ImBuf *BKE_movieclip_acquire_ibuf(MovieClip *clip, MovieClipUser *user)
        ImBuf *ibuf= NULL;
        int framenr= user?user->framenr:clip->lastframe;
 
+       /* cache is supposed to be threadsafe */
        ibuf= get_imbuf_cache(clip, user);
 
        if(!ibuf) {
                if(clip->source==MCLIP_SRC_SEQUENCE)
                        ibuf= movieclip_load_sequence_file(clip, framenr);
-               else
+               else {
+                       /* loading of movies can't happen from concurent threads */
+                       BLI_lock_thread(LOCK_MOVIECLIP);
+
                        ibuf= movieclip_load_movie_file(clip, framenr);
 
+                       BLI_unlock_thread(LOCK_MOVIECLIP);
+               }
+
                if(ibuf)
                        put_imbuf_cache(clip, user, ibuf);
        }
index 13469e0b58bcb0a0c93cf942d8af9dd2ac04bf25..e4f9f5ebae29045432427b6b2d9024d507f348a6 100644 (file)
@@ -3305,6 +3305,10 @@ int ntreeCompositTagAnimated(bNodeTree *ntree)
                                NodeTagChanged(ntree, node);
                        }
                }
+               else if(node->type==CMP_NODE_MOVIECLIP) {
+                       NodeTagChanged(ntree, node);
+                       tagged= 1;
+               }
        }
        
        return tagged;
@@ -3451,6 +3455,7 @@ static void registerCompositNodes(ListBase *ntypelist)
        register_node_type_cmp_value(ntypelist);
        register_node_type_cmp_rgb(ntypelist);
        register_node_type_cmp_curve_time(ntypelist);
+       register_node_type_cmp_movieclip(ntypelist);
        
        register_node_type_cmp_composite(ntypelist);
        register_node_type_cmp_viewer(ntypelist);
index 5bf5423d312c548fc0368b8672f793786282ab94..268227a12e6b64a4877a8d9aed9c92e250a6011b 100644 (file)
@@ -71,6 +71,7 @@ int           BLI_system_thread_count(void); /* gets the number of threads the system can
 #define LOCK_CUSTOM1   3
 #define LOCK_RCACHE            4
 #define LOCK_OPENGL            5
+#define LOCK_MOVIECLIP 6
 
 void   BLI_lock_thread(int type);
 void   BLI_unlock_thread(int type);
index 7b59a7905aa290da3f185a7165d9aa04f5d32dfb..47b6cf133f9afad9d876457c10e89a5174d2e9e4 100644 (file)
@@ -114,6 +114,7 @@ static pthread_mutex_t _viewer_lock = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t _custom1_lock = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t _rcache_lock = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t _opengl_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t _movieclip_lock = PTHREAD_MUTEX_INITIALIZER;
 static pthread_t mainid;
 static int thread_levels= 0;   /* threads can be invoked inside threads */
 
@@ -349,6 +350,8 @@ void BLI_lock_thread(int type)
                pthread_mutex_lock(&_rcache_lock);
        else if (type==LOCK_OPENGL)
                pthread_mutex_lock(&_opengl_lock);
+       else if (type==LOCK_MOVIECLIP)
+               pthread_mutex_lock(&_movieclip_lock);
 }
 
 void BLI_unlock_thread(int type)
@@ -365,6 +368,8 @@ void BLI_unlock_thread(int type)
                pthread_mutex_unlock(&_rcache_lock);
        else if(type==LOCK_OPENGL)
                pthread_mutex_unlock(&_opengl_lock);
+       else if(type==LOCK_MOVIECLIP)
+               pthread_mutex_unlock(&_movieclip_lock);
 }
 
 /* Mutex Locks */
index dc70b0d3ba9e906961abfda334d81f3b18b24364..425757b54cc0ea22d21c1f99190dcc4a3e1b1211 100644 (file)
@@ -155,8 +155,9 @@ static void draw_movieclip_buffer(SpaceClip *sc, ARegion *ar, ImBuf *ibuf, float
                glColor3f(0.0f, 0.0f, 0.0f);
                glRectf(x, y, x+ibuf->x*sc->zoom, y+ibuf->y*sc->zoom);
        } else {
-               if(ibuf->rect_float)
+               if(ibuf->rect_float && !ibuf->rect) {
                        IMB_rect_from_float(ibuf);
+               }
 
                if(ibuf->rect)
                        glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
index 1bf2c3d89bdd8bd2338ce2cb1dd6bc4606acc231..d4925e39407b6392c81901c752f9976596f0b4b8 100644 (file)
@@ -1075,6 +1075,11 @@ static void node_composit_buts_ycc(uiLayout *layout, bContext *UNUSED(C), Pointe
        uiItemR(layout, ptr, "mode", 0, "", ICON_NONE);
 }
 
+static void node_composit_buts_movieclip(uiLayout *layout, bContext *C, PointerRNA *ptr)
+{
+       uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
+}
+
 /* only once called */
 static void node_composit_set_butfunc(bNodeType *ntype)
 {
@@ -1225,6 +1230,9 @@ static void node_composit_set_butfunc(bNodeType *ntype)
                case CMP_NODE_SEPYCCA:
                        ntype->uifunc=node_composit_buts_ycc;
                        break;
+               case CMP_NODE_MOVIECLIP:
+                       ntype->uifunc= node_composit_buts_movieclip;
+                       break;
                default:
                        ntype->uifunc= NULL;
        }
index 7fd6a9dacfe06982eaaacc4e486e02dfa60e2c53..a166d31b1e6a2b4450bf3955ff40dced520aa388 100644 (file)
@@ -2268,6 +2268,20 @@ static void def_cmp_ycc(StructRNA *srna)
        RNA_def_property_update(prop, NC_NODE|NA_EDITED, "rna_Node_update");
 }
 
+static void def_cmp_movieclip(StructRNA *srna)
+{
+       PropertyRNA *prop;
+
+       prop = RNA_def_property(srna, "clip", PROP_POINTER, PROP_NONE);
+       RNA_def_property_pointer_sdna(prop, NULL, "id");
+       RNA_def_property_struct_type(prop, "MovieClip");
+       RNA_def_property_flag(prop, PROP_EDITABLE);
+       RNA_def_property_ui_text(prop, "Movie Clip", "");
+       RNA_def_property_update(prop, NC_NODE|NA_EDITED, "rna_Node_update");
+
+       RNA_def_struct_sdna_from(srna, "MovieClipUser", "storage");
+}
+
 
 /* -- Texture Nodes --------------------------------------------------------- */
 
index d48df85697ae258a509f138d27b569e74cae59b9..b32f2bad2833ab37be70be2fbb6606cbad17bbc8 100644 (file)
@@ -114,6 +114,7 @@ DefNode( CompositorNode, CMP_NODE_COLOR_MATTE,    def_cmp_color_matte,    "COLOR
 DefNode( CompositorNode, CMP_NODE_DIST_MATTE,     def_cmp_distance_matte, "DISTANCE_MATTE", DistanceMatte,    "Distance Matte",    ""              )
 DefNode( CompositorNode, CMP_NODE_COLORBALANCE,   def_cmp_colorbalance,   "COLORBALANCE",   ColorBalance,     "Color Balance",     ""              )
 DefNode( CompositorNode, CMP_NODE_HUECORRECT,     def_cmp_huecorrect,     "HUECORRECT",     HueCorrect,       "Hue Correct",       ""              )
+DefNode( CompositorNode, CMP_NODE_MOVIECLIP,      def_cmp_movieclip,      "MOVIECLIP",      MovieClip,        "MovieClip",         ""              )
                                                                                                                                                    
 DefNode( TextureNode,    TEX_NODE_OUTPUT,         def_tex_output,         "OUTPUT",         Output,           "Output",            ""              )
 DefNode( TextureNode,    TEX_NODE_CHECKER,        0,                      "CHECKER",        Checker,          "Checker",           ""              )
index 65c9236710f397b5e284769748ce56269ad42847..152bbf75738397205cb6c3ebb487f361f974e717 100644 (file)
@@ -48,6 +48,7 @@ void register_node_type_cmp_texture(ListBase *lb);
 void register_node_type_cmp_value(ListBase *lb);
 void register_node_type_cmp_rgb(ListBase *lb);
 void register_node_type_cmp_curve_time(ListBase *lb);
+void register_node_type_cmp_movieclip(ListBase *lb);
 
 void register_node_type_cmp_composite(ListBase *lb);
 void register_node_type_cmp_viewer(ListBase *lb);
index c3bd37c18ee3702a1ef23e8c4105a59fe330adcc..fa77d08bd8057af07079e4f5e6e193a516fa91a2 100644 (file)
@@ -75,6 +75,7 @@ set(SRC
        intern/CMP_nodes/CMP_mapValue.c
        intern/CMP_nodes/CMP_math.c
        intern/CMP_nodes/CMP_mixrgb.c
+       intern/CMP_nodes/CMP_movieclip.c
        intern/CMP_nodes/CMP_normal.c
        intern/CMP_nodes/CMP_normalize.c
        intern/CMP_nodes/CMP_outputFile.c
diff --git a/source/blender/nodes/intern/CMP_nodes/CMP_movieclip.c b/source/blender/nodes/intern/CMP_nodes/CMP_movieclip.c
new file mode 100644 (file)
index 0000000..cd26c9b
--- /dev/null
@@ -0,0 +1,157 @@
+/*
+ * $Id$
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2011 Blender Foundation.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): Blender Foundation,
+ *                 Sergey Sharybin
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/nodes/intern/CMP_nodes/CMP_movieclip.c
+ *  \ingroup cmpnodes
+ */
+
+
+#include "../CMP_util.h"
+
+
+static bNodeSocketType cmp_node_rlayers_out[]= {
+       {       SOCK_RGBA, 0, "Image",          0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
+       {       -1, 0, ""       }
+};
+
+static CompBuf *node_composit_get_movieclip(RenderData *rd, MovieClip *clip, MovieClipUser *user)
+{
+       ImBuf *ibuf;
+       CompBuf *stackbuf;
+       int type;
+
+       float *rect;
+       int alloc= FALSE;
+
+       ibuf= BKE_movieclip_acquire_ibuf(clip, user);
+       if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL)) {
+               IMB_freeImBuf(ibuf);
+               return NULL;
+       }
+
+       if (ibuf->rect_float == NULL) {
+               IMB_float_from_rect(ibuf);
+       }
+
+       /* now we need a float buffer from the image with matching color management */
+       if(ibuf->channels == 4) {
+               if(rd->color_mgt_flag & R_COLOR_MANAGEMENT) {
+                       if(ibuf->profile != IB_PROFILE_NONE) {
+                               rect= ibuf->rect_float;
+                       }
+                       else {
+                               rect= MEM_mapallocN(sizeof(float) * 4 * ibuf->x * ibuf->y, "node_composit_get_image");
+                               srgb_to_linearrgb_rgba_rgba_buf(rect, ibuf->rect_float, ibuf->x * ibuf->y);
+                               alloc= TRUE;
+                       }
+               }
+               else {
+                       if(ibuf->profile == IB_PROFILE_NONE) {
+                               rect= ibuf->rect_float;
+                       }
+                       else {
+                               rect= MEM_mapallocN(sizeof(float) * 4 * ibuf->x * ibuf->y, "node_composit_get_image");
+                               linearrgb_to_srgb_rgba_rgba_buf(rect, ibuf->rect_float, ibuf->x * ibuf->y);
+                               alloc= TRUE;
+                       }
+               }
+       }
+       else {
+               /* non-rgba passes can't use color profiles */
+               rect= ibuf->rect_float;
+       }
+       /* done coercing into the correct color management */
+
+       if(!alloc) {
+               rect= MEM_dupallocN(rect);
+               alloc= 1;
+       }
+
+       type= ibuf->channels;
+
+       if(rd->scemode & R_COMP_CROP) {
+               stackbuf= get_cropped_compbuf(&rd->disprect, rect, ibuf->x, ibuf->y, type);
+               if(alloc)
+                       MEM_freeN(rect);
+       }
+       else {
+               /* we put imbuf copy on stack, cbuf knows rect is from other ibuf when freed! */
+               stackbuf= alloc_compbuf(ibuf->x, ibuf->y, type, FALSE);
+               stackbuf->rect= rect;
+               stackbuf->malloc= alloc;
+       }
+
+       IMB_freeImBuf(ibuf);
+
+       return stackbuf;
+}
+
+static void node_composit_exec_movieclip(void *data, bNode *node, bNodeStack **UNUSED(in), bNodeStack **out)
+{
+       if(node->id) {
+               RenderData *rd= data;
+               MovieClip *clip= (MovieClip *)node->id;
+               MovieClipUser *user= (MovieClipUser *)node->storage;
+               CompBuf *stackbuf= NULL;
+
+               BKE_movieclip_user_set_frame(user, rd->cfra);
+
+               stackbuf= node_composit_get_movieclip(rd, clip, user);
+
+               if (stackbuf) {
+                       /* put image on stack */
+                       out[0]->data= stackbuf;
+
+                       /* generate preview */
+                       generate_preview(data, node, stackbuf);
+               }
+       }
+}
+
+static void node_composit_init_movieclip(bNode* node)
+{
+       MovieClipUser *user= MEM_callocN(sizeof(MovieClipUser), "node movie clip user");
+       node->storage= user;
+       user->framenr= 1;
+}
+
+void register_node_type_cmp_movieclip(ListBase *lb)
+{
+       static bNodeType ntype;
+
+       node_type_base(&ntype, CMP_NODE_MOVIECLIP, "Movie Clip", NODE_CLASS_INPUT, NODE_PREVIEW|NODE_OPTIONS,
+               NULL, cmp_node_rlayers_out);
+       node_type_size(&ntype, 120, 80, 300);
+       node_type_init(&ntype, node_composit_init_movieclip);
+       node_type_storage(&ntype, "MovieClipUser", node_free_standard_storage, node_copy_standard_storage);
+       node_type_exec(&ntype, node_composit_exec_movieclip);
+
+       nodeRegisterType(lb, &ntype);
+}
index 3f37eae2af9e99fb3f89d8a53e6375ff78cf87e7..91dfff53c2ac9e79d998d578508618771f6acab1 100644 (file)
@@ -46,6 +46,7 @@
 #include "DNA_ID.h"
 #include "DNA_image_types.h"
 #include "DNA_material_types.h"
+#include "DNA_movieclip_types.h"
 #include "DNA_node_types.h"
 #include "DNA_object_types.h"
 #include "DNA_scene_types.h"
@@ -64,6 +65,7 @@
 #include "BKE_image.h"
 #include "BKE_main.h"
 #include "BKE_material.h"
+#include "BKE_movieclip.h"
 #include "BKE_node.h"
 #include "BKE_texture.h"