]> git.sesse.net Git - vlc/blobdiff - modules/video_filter/opencv_wrapper.c
anaglyph: remove spurious printf
[vlc] / modules / video_filter / opencv_wrapper.c
index 11e0e8c030706fe3db75d17b2364209f72342617..ec9e5c442ef6a22722352866f192ca9e9f9e6770 100644 (file)
@@ -1,25 +1,25 @@
 /*****************************************************************************
  * opencv_wrapper.c : OpenCV wrapper video filter
  *****************************************************************************
- * Copyright (C) 2006-2012 the VideoLAN team
+ * Copyright (C) 2006-2012 VLC authors and VideoLAN
  * Copyright (C) 2012 Edward Wang
  *
  * Authors: Dugal Harris <dugalh@protoclea.co.za>
  *          Edward Wang <edward.c.wang@compdigitec.com>
  *
- * 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
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 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.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser 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.
+ * You should have received a copy of the GNU Lesser 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.
  *****************************************************************************/
 
 /*****************************************************************************
 #include <vlc_vout.h>
 #include <vlc_modules.h>
 
-#include <math.h>
-#include <time.h>
-
 #include <vlc_filter.h>
 #include <vlc_image.h>
-#include <vlc_input.h>
 #include "filter_picture.h"
 
 #include <cxcore.h>
@@ -88,11 +84,11 @@ vlc_module_begin ()
     add_string( "opencv-chroma", "input",
                           N_("OpenCV filter chroma"),
                           N_("Chroma to convert picture to before sending it to the internal OpenCV filter"), false);
-        change_string_list( chroma_list, chroma_list_text, 0);
+        change_string_list( chroma_list, chroma_list_text )
     add_string( "opencv-output", "input",
                           N_("Wrapper filter output"),
                           N_("Determines what (if any) video is displayed by the wrapper filter"), false);
-        change_string_list( output_list, output_list_text, 0);
+        change_string_list( output_list, output_list_text )
     add_string( "opencv-filter-name", "none",
                           N_("OpenCV internal filter name"),
                           N_("Name of internal OpenCV plugin filter to use"), false);
@@ -155,7 +151,7 @@ struct filter_sys_t
 static int Create( vlc_object_t *p_this )
 {
     filter_t* p_filter = (filter_t*)p_this;
-    char *psz_chroma, *psz_output, *psz_verbosity;
+    char *psz_chroma, *psz_output;
 
     /* Allocate structure */
     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
@@ -323,7 +319,6 @@ static void VlcPictureToIplImage( filter_t* p_filter, picture_t* p_in )
     // input video size
     CvSize sz = cvSize(abs(p_in->format.i_width), abs(p_in->format.i_height));
     video_format_t fmt_out;
-    double  duration;
     filter_sys_t* p_sys = p_filter->p_sys;
 
     memset( &fmt_out, 0, sizeof(video_format_t) );
@@ -342,7 +337,7 @@ static void VlcPictureToIplImage( filter_t* p_filter, picture_t* p_in )
             //rgb2 gives 3 separate planes, this gives 1 interleaved plane
             //rv24 gives is about 20% faster but gives r&b the wrong way round
             //and I can't think of an easy way to fix this
-            fmt_out.i_chroma = VLC_CODEC_RGB32;
+            fmt_out.i_chroma = VLC_CODEC_RGB24;
         }
         else if (p_sys->i_internal_chroma == GREY)
         {
@@ -366,8 +361,12 @@ static void VlcPictureToIplImage( filter_t* p_filter, picture_t* p_in )
     }
     else    //((p_sys->f_scale != 1) || (p_sys->i_internal_chroma != CINPUT))
     {
-        //use the input image without conversion
-        p_sys->p_proc_image = p_in;
+        // In theory, you could use the input image without conversion,
+        // but it seems to cause weird picture effects (like repeated
+        // image filtering) and picture leaking.
+        p_sys->p_proc_image = filter_NewPicture( p_filter ); //p_in
+        picture_Copy( p_sys->p_proc_image, p_in );
+        p_sys->p_to_be_freed = p_sys->p_proc_image;
     }
 
     //Convert to the IplImage array that is to be processed.
@@ -413,6 +412,8 @@ static picture_t* Filter( filter_t* p_filter, picture_t* p_pic )
         return NULL;
     }
 
+    video_format_t fmt_out;
+
     // Make a copy if we want to show the original input
     if (p_filter->p_sys->i_wrapper_output == VINPUT)
         picture_Copy( p_outpic, p_pic );
@@ -420,13 +421,35 @@ static picture_t* Filter( filter_t* p_filter, picture_t* p_pic )
     VlcPictureToIplImage( p_filter, p_pic );
     // Pass the image (as a pointer to the first IplImage*) to the
     // internal OpenCV filter for processing.
-    p_filter->p_sys->p_opencv->pf_video_filter( p_filter->p_sys->p_opencv, &(p_filter->p_sys->p_cv_image[0]) );
+    p_filter->p_sys->p_opencv->pf_video_filter( p_filter->p_sys->p_opencv, (picture_t*)&(p_filter->p_sys->p_cv_image[0]) );
 
     if(p_filter->p_sys->i_wrapper_output == PROCESSED) {
         // Processed video
-        if ((p_filter->p_sys->p_proc_image) && (p_filter->p_sys->p_proc_image->i_planes > 0)) {
+        if( (p_filter->p_sys->p_proc_image) &&
+            (p_filter->p_sys->p_proc_image->i_planes > 0) &&
+            (p_filter->p_sys->i_internal_chroma != CINPUT) ) {
+            //p_filter->p_sys->p_proc_image->format.i_chroma = VLC_CODEC_RGB24;
+
+            memset( &fmt_out, 0, sizeof(video_format_t) );
+            fmt_out = p_pic->format;
+            //picture_Release( p_outpic );
+
+            /*
+             * We have to copy out the image from image_Convert(), otherwise
+             * you leak pictures for some reason:
+             * main video output error: pictures leaked, trying to workaround
+             */
+            picture_t* p_outpic_tmp = image_Convert(
+                        p_filter->p_sys->p_image,
+                        p_filter->p_sys->p_proc_image,
+                        &(p_filter->p_sys->p_proc_image->format),
+                        &fmt_out );
+
+            picture_CopyPixels( p_outpic, p_outpic_tmp );
+            CopyInfoAndRelease( p_outpic, p_outpic_tmp );
+        } else if( p_filter->p_sys->i_internal_chroma == CINPUT ) {
             picture_CopyPixels( p_outpic, p_filter->p_sys->p_proc_image );
-            CopyInfoAndRelease( p_outpic, p_pic );
+            picture_CopyProperties( p_outpic, p_filter->p_sys->p_proc_image );
         }
     }