]> git.sesse.net Git - vlc/blobdiff - modules/codec/ffmpeg/chroma.c
Another time : remove useless test before free().
[vlc] / modules / codec / ffmpeg / chroma.c
index d524c20b828580152371194615391a3e9076b139..7bdfb25dd93c1a0916c7849e31b96d21761c94b9 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * chroma.c: chroma conversion using ffmpeg library
  *****************************************************************************
- * Copyright (C) 1999-2001 VideoLAN (Centrale Réseaux) and its contributors
+ * Copyright (C) 1999-2001 the VideoLAN team
  * $Id$
  *
  * Authors: Gildas Bazin <gbazin@videolan.org>
  *
  * 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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include <vlc/vlc.h>
-#include <vlc/vout.h>
+#include <vlc_vout.h>
+
+#if defined(HAVE_LIBSWSCALE_SWSCALE_H)  || defined(HAVE_FFMPEG_SWSCALE_H) || defined(HAVE_LIBSWSCALE_TREE)
+#include <vlc_filter.h>
+#endif
 
 /* ffmpeg header */
-#ifdef HAVE_FFMPEG_AVCODEC_H
+#ifdef HAVE_LIBAVCODEC_AVCODEC_H
+#   include <libavcodec/avcodec.h>
+#elif defined(HAVE_FFMPEG_AVCODEC_H)
 #   include <ffmpeg/avcodec.h>
 #else
 #   include <avcodec.h>
@@ -36,6 +47,7 @@
 
 #include "ffmpeg.h"
 
+#if !defined(HAVE_LIBSWSCALE_SWSCALE_H)  && !defined(HAVE_FFMPEG_SWSCALE_H) && !defined(HAVE_LIBSWSCALE_TREE)
 void E_(InitLibavcodec) ( vlc_object_t *p_object );
 static void ChromaConversion( vout_thread_t *, picture_t *, picture_t * );
 
@@ -81,7 +93,7 @@ int E_(OpenChroma)( vlc_object_t *p_this )
     p_vout->chroma.p_sys = malloc( sizeof( chroma_sys_t ) );
     if( p_vout->chroma.p_sys == NULL )
     {
-        return VLC_EGENERIC;
+        return VLC_ENOMEM;
     }
 
     p_vout->chroma.p_sys->i_src_vlc_chroma = p_vout->render.i_chroma;
@@ -91,9 +103,8 @@ int E_(OpenChroma)( vlc_object_t *p_this )
 
     if( ( p_vout->render.i_height != p_vout->output.i_height ||
           p_vout->render.i_width != p_vout->output.i_width ) &&
-        ( p_vout->chroma.p_sys->i_dst_vlc_chroma == VLC_FOURCC('I','4','2','0')  ||
+        ( p_vout->chroma.p_sys->i_dst_vlc_chroma == VLC_FOURCC('I','4','2','0') ||
           p_vout->chroma.p_sys->i_dst_vlc_chroma == VLC_FOURCC('Y','V','1','2') ))
-        
     {
         msg_Dbg( p_vout, "preparing to resample picture" );
         p_vout->chroma.p_sys->p_rsc =
@@ -188,3 +199,168 @@ void E_(CloseChroma)( vlc_object_t *p_this )
     }
     free( p_vout->chroma.p_sys );
 }
+#else
+
+static void ChromaConversion( vout_thread_t *, picture_t *, picture_t * );
+
+/*****************************************************************************
+ * chroma_sys_t: chroma method descriptor
+ *****************************************************************************
+ * This structure is part of the chroma transformation descriptor, it
+ * describes the chroma plugin specific properties.
+ *****************************************************************************/
+struct chroma_sys_t
+{
+    filter_t *p_swscaler;
+};
+
+/*****************************************************************************
+ * Video Filter2 functions
+ *****************************************************************************/
+struct filter_owner_sys_t
+{
+    vout_thread_t *p_vout;
+};
+
+static void PictureRelease( picture_t *p_pic )
+{
+    free( p_pic->p_data_orig );
+}
+
+static picture_t *video_new_buffer_filter( filter_t *p_filter )
+{
+    picture_t *p_picture = malloc( sizeof(picture_t) );
+    if( !p_picture ) return NULL;
+    if( vout_AllocatePicture( p_filter, p_picture,
+                              p_filter->fmt_out.video.i_chroma,
+                              p_filter->fmt_out.video.i_width,
+                              p_filter->fmt_out.video.i_height,
+                              p_filter->fmt_out.video.i_aspect )
+        != VLC_SUCCESS )
+    {
+        free( p_picture );
+        return NULL;
+    }
+
+    p_picture->pf_release = PictureRelease;
+
+    return p_picture;
+}
+
+static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
+{
+    VLC_UNUSED(p_filter);
+    if( p_pic )
+    {
+        free( p_pic->p_data_orig );
+        free( p_pic );
+    }
+}
+
+/*****************************************************************************
+ * OpenChroma: allocate a chroma function
+ *****************************************************************************
+ * This function allocates and initializes a chroma function
+ *****************************************************************************/
+int E_(OpenChroma)( vlc_object_t *p_this )
+{
+    vout_thread_t *p_vout = (vout_thread_t *)p_this;
+    chroma_sys_t  *p_sys = p_vout->chroma.p_sys;
+
+    p_vout->chroma.p_sys = p_sys = malloc( sizeof( chroma_sys_t ) );
+    if( p_vout->chroma.p_sys == NULL )
+    {
+        return VLC_ENOMEM;
+    }
+    p_vout->chroma.pf_convert = ChromaConversion;
+
+    p_sys->p_swscaler = vlc_object_create( p_vout, VLC_OBJECT_FILTER );
+    vlc_object_attach( p_sys->p_swscaler, p_vout );
+
+    p_sys->p_swscaler->pf_vout_buffer_new = video_new_buffer_filter;
+    p_sys->p_swscaler->pf_vout_buffer_del = video_del_buffer_filter;
+
+    p_sys->p_swscaler->fmt_out.video.i_x_offset =
+        p_sys->p_swscaler->fmt_out.video.i_y_offset = 0;
+    p_sys->p_swscaler->fmt_in.video = p_vout->fmt_in;
+    p_sys->p_swscaler->fmt_out.video = p_vout->fmt_out;
+    p_sys->p_swscaler->fmt_out.video.i_aspect = p_vout->render.i_aspect;
+    p_sys->p_swscaler->fmt_in.video.i_chroma = p_vout->render.i_chroma;
+    p_sys->p_swscaler->fmt_out.video.i_chroma = p_vout->output.i_chroma;
+
+    p_sys->p_swscaler->p_module = module_Need( p_sys->p_swscaler,
+                           "video filter2", 0, 0 );
+
+    if( p_sys->p_swscaler->p_module )
+    {
+        p_sys->p_swscaler->p_owner =
+            malloc( sizeof( filter_owner_sys_t ) );
+        if( p_sys->p_swscaler->p_owner )
+            p_sys->p_swscaler->p_owner->p_vout = p_vout;
+    }
+
+    if( !p_sys->p_swscaler->p_module || !p_sys->p_swscaler->p_owner )
+    {
+        vlc_object_detach( p_sys->p_swscaler );
+        vlc_object_release( p_sys->p_swscaler );
+        free( p_vout->chroma.p_sys );
+        return VLC_EGENERIC;
+    }
+
+    return VLC_SUCCESS;
+}
+
+/*****************************************************************************
+ * ChromaConversion: actual chroma conversion function
+ *****************************************************************************/
+static void ChromaConversion( vout_thread_t *p_vout,
+                              picture_t *p_src, picture_t *p_dest )
+{
+    chroma_sys_t *p_sys = (chroma_sys_t *) p_vout->chroma.p_sys;
+
+    if( p_sys && p_src && p_dest && 
+        p_sys->p_swscaler && p_sys->p_swscaler->p_module )
+    {
+        picture_t *p_pic;
+
+        p_sys->p_swscaler->fmt_in.video = p_vout->fmt_in;
+        p_sys->p_swscaler->fmt_out.video = p_vout->fmt_out;
+
+#if 0
+        msg_Dbg( p_vout, "chroma %4.4s (%d) to %4.4s (%d)",
+                 (char *)&p_vout->fmt_in.i_chroma, p_src->i_planes,
+                 (char *)&p_vout->fmt_out.i_chroma, p_dest->i_planes  );
+#endif
+        p_pic = p_sys->p_swscaler->pf_vout_buffer_new( p_sys->p_swscaler );
+        if( p_pic )
+        {
+            picture_t *p_dst_pic;
+            vout_CopyPicture( p_vout, p_pic, p_src );
+            p_dst_pic = p_sys->p_swscaler->pf_video_filter( p_sys->p_swscaler, p_pic );
+            vout_CopyPicture( p_vout, p_dest, p_dst_pic );
+            p_dst_pic->pf_release( p_dst_pic );
+        }
+    }
+}
+
+/*****************************************************************************
+ * CloseChroma: free the chroma function
+ *****************************************************************************
+ * This function frees the previously allocated chroma function
+ *****************************************************************************/
+void E_(CloseChroma)( vlc_object_t *p_this )
+{
+    vout_thread_t *p_vout = (vout_thread_t *)p_this;
+    chroma_sys_t  *p_sys  = (chroma_sys_t *)p_vout->chroma.p_sys;
+    if( p_sys->p_swscaler && p_sys->p_swscaler->p_module )
+    {
+        free( p_sys->p_swscaler->p_owner );
+        module_Unneed( p_sys->p_swscaler, p_sys->p_swscaler->p_module );
+        vlc_object_detach( p_sys->p_swscaler );
+        vlc_object_release( p_sys->p_swscaler );
+        p_sys->p_swscaler= NULL;
+    }
+    free( p_vout->chroma.p_sys );
+}
+
+#endif /* !defined(HAVE_LIBSWSCALE_SWSCALE_H)  && !defined(HAVE_FFMPEG_SWSCALE_H) && !defined(HAVE_LIBSWSCALE_TREE) */