]> git.sesse.net Git - vlc/blobdiff - modules/video_filter/opencv_example.cpp
Fix memleak in opencv.
[vlc] / modules / video_filter / opencv_example.cpp
index ab73a2c17645a16d52d796ae36adf5f6c988387b..76b86659fd69e661a94c9a1e0321859f5f2a2a07 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * opencv_example.cpp : Example OpenCV internal video filter 
+ * opencv_example.cpp : Example OpenCV internal video filter
  * (performs face identification).  Mostly taken from the facedetect.c
  * OpenCV sample.
  *****************************************************************************
 #include <highgui.h>
 
 
-#include <vlc/vlc.h>
-#include <vlc/decoder.h>
-#include "vlc_filter.h"
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_filter.h>
 #include "filter_common.h"
-#include "vlc_image.h"
+#include <vlc_image.h>
 #include "filter_event_info.h"
 
 /*****************************************************************************
@@ -60,18 +64,18 @@ static picture_t *Filter( filter_t *, picture_t * );
  * Module descriptor
  *****************************************************************************/
 vlc_module_begin();
-    set_description( _("OpenCV face detection example filter") );
+    set_description( N_("OpenCV face detection example filter") );
     set_shortname( N_( "OpenCV example" ));
     set_capability( "opencv example", 1 );
     add_shortcut( "opencv_example" );
-    
+
     set_category( CAT_VIDEO );
     set_subcategory( SUBCAT_VIDEO_VFILTER2 );
     set_callbacks( OpenFilter, CloseFilter );
 
     add_string( "opencv-haarcascade-file", "c:\\haarcascade_frontalface_alt.xml", NULL,
-                          N_("Haar cascade filename"), 
-                          N_("Name of XML file containing Haar cascade description"), VLC_FALSE);    
+                          N_("Haar cascade filename"),
+                          N_("Name of XML file containing Haar cascade description"), false);
 vlc_module_end();
 
 /*****************************************************************************
@@ -86,11 +90,10 @@ static int OpenFilter( vlc_object_t *p_this )
     if( ( p_filter->p_sys = p_sys =
           (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
     {
-        msg_Err( p_filter, "out of memory" );
-        return VLC_EGENERIC;
+        return VLC_ENOMEM;
     }
-    
-    //init the video_filter_event_info_t struct   
+
+    //init the video_filter_event_info_t struct
     p_sys->event_info.i_region_size = 0;
     p_sys->event_info.p_region = NULL;
     p_sys->i_id = 0;
@@ -101,7 +104,7 @@ static int OpenFilter( vlc_object_t *p_this )
     vlc_value_t val;
     if (var_Create( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE, VLC_VAR_ADDRESS | VLC_VAR_DOINHERIT ) != VLC_SUCCESS)
         msg_Err( p_filter, "Could not create %s\n", VIDEO_FILTER_EVENT_VARIABLE);
-    
+
     val.p_address = &(p_sys->event_info);
     if (var_Set( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE, val )!=VLC_SUCCESS)
         msg_Err( p_filter, "Could not set %s\n", VIDEO_FILTER_EVENT_VARIABLE);
@@ -110,6 +113,7 @@ static int OpenFilter( vlc_object_t *p_this )
     char* filename = config_GetPsz( p_filter, "opencv-haarcascade-file" );
     p_filter->p_sys->p_cascade = (CvHaarClassifierCascade*)cvLoad( filename, 0, 0, 0 );
     p_filter->p_sys->p_storage = cvCreateMemStorage(0);
+    free( filename );
 
     return VLC_SUCCESS;
 }
@@ -121,7 +125,7 @@ static void CloseFilter( vlc_object_t *p_this )
 {
     filter_t *p_filter = (filter_t*)p_this;
     filter_sys_t *p_sys = p_filter->p_sys;
-    
+
     if( p_filter->p_sys->p_cascade )
         cvReleaseHaarClassifierCascade( &p_filter->p_sys->p_cascade );
 
@@ -129,7 +133,7 @@ static void CloseFilter( vlc_object_t *p_this )
         cvReleaseMemStorage( &p_filter->p_sys->p_storage );
 
     if (NULL != p_filter->p_sys->event_info.p_region)
-        free(p_filter->p_sys->event_info.p_region); 
+        free(p_filter->p_sys->event_info.p_region);
 
     free( p_sys );
 
@@ -139,8 +143,8 @@ static void CloseFilter( vlc_object_t *p_this )
 /****************************************************************************
  * Filter: Check for faces and raises an event when one is found.
  ****************************************************************************
- * p_pic: A picture_t with its p_data_orig member set to an array of 
- * IplImages (one image for each picture_t plane). 
+ * p_pic: A picture_t with its p_data_orig member set to an array of
+ * IplImages (one image for each picture_t plane).
  ****************************************************************************/
 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
 {
@@ -148,7 +152,7 @@ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
     int i_planes = 0;
     CvPoint pt1, pt2;
     int i, scale = 1;
-    
     if ((!p_pic) )
     {
         msg_Err( p_filter, "no image array" );
@@ -182,27 +186,26 @@ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
 
     //perform face detection
     cvClearMemStorage(p_filter->p_sys->p_storage);
-    CvSeq* faces = NULL; 
+    CvSeq* faces = NULL;
     if( p_filter->p_sys->p_cascade )
     {
         //we should make some of these params config variables
-        faces = cvHaarDetectObjects( p_img[0], p_filter->p_sys->p_cascade, 
+        faces = cvHaarDetectObjects( p_img[0], p_filter->p_sys->p_cascade,
             p_filter->p_sys->p_storage, 1.15, 5, CV_HAAR_DO_CANNY_PRUNING,
                                             cvSize(20, 20) );
         //create the video_filter_region_info_t struct
         CvRect* r;
         if (faces && (faces->total > 0))
         {
-            //msg_Dbg( p_filter, "Found %d face(s)\n", faces->total );            
+            //msg_Dbg( p_filter, "Found %d face(s)\n", faces->total );
             if (NULL != p_filter->p_sys->event_info.p_region)
             {
                 free(p_filter->p_sys->event_info.p_region);
                 p_filter->p_sys->event_info.p_region = NULL;
-            } 
+            }
             if( NULL == ( p_filter->p_sys->event_info.p_region =
                   (video_filter_region_info_t *)malloc(faces->total*sizeof(video_filter_region_info_t))))
             {
-                msg_Err( p_filter, "out of memory" );
                 return NULL;
             }
             memset(p_filter->p_sys->event_info.p_region, 0, faces->total*sizeof(video_filter_region_info_t));
@@ -229,7 +232,7 @@ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
     }
     else
         msg_Err( p_filter, "No cascade - is opencv-haarcascade-file valid?" );
-    
+
     return p_pic;
 }