]> git.sesse.net Git - vlc/blobdiff - modules/codec/fake.c
Potential memleak.
[vlc] / modules / codec / fake.c
index 8bd084bd4c2f4589c43cf3ad00c4bc6b107cb88b..bd4c1cddc8a9575299cd985dd4c3a6ae4dc0d68e 100644 (file)
@@ -5,6 +5,7 @@
  * $Id$
  *
  * Authors: Christophe Massiot <massiot@via.ecp.fr>
+ *          Jean-Paul Saman <jpsaman at m2x dot nl>
  *
  * 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
  *
  * 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
  *****************************************************************************/
-#include <vlc/vlc.h>
-#include <vlc/decoder.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
-#include "vlc_image.h"
-#include "vlc_filter.h"
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_codec.h>
+
+#include <vlc_image.h>
+#include <vlc_filter.h>
+#include <vlc_charset.h>
 
 /*****************************************************************************
  * Local prototypes
@@ -37,33 +44,41 @@ static int  OpenDecoder   ( vlc_object_t * );
 static void CloseDecoder  ( vlc_object_t * );
 
 static picture_t *DecodeBlock  ( decoder_t *, block_t ** );
+static int FakeCallback( vlc_object_t *, char const *,
+                         vlc_value_t, vlc_value_t, void * );
 
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
 #define FILE_TEXT N_("Image file")
 #define FILE_LONGTEXT N_( \
-    "Path of the image file when using the fake input." )
+    "Path of the image file for fake input." )
+#define RELOAD_TEXT N_("Reload image file")
+#define RELOAD_LONGTEXT N_( \
+    "Reload image file every n seconds." )
 #define WIDTH_TEXT N_("Video width")
 #define WIDTH_LONGTEXT N_( \
-    "Allows you to specify the output video width." )
+    "Output video width." )
 #define HEIGHT_TEXT N_("Video height")
 #define HEIGHT_LONGTEXT N_( \
-    "Allows you to specify the output video height." )
+    "Output video height." )
 #define KEEP_AR_TEXT N_("Keep aspect ratio")
 #define KEEP_AR_LONGTEXT N_( \
-    "If selected, width and height will be considered as maximum values." )
+    "Consider width and height as maximum values." )
 #define ASPECT_RATIO_TEXT N_("Background aspect ratio")
 #define ASPECT_RATIO_LONGTEXT N_( \
     "Aspect ratio of the image file (4:3, 16:9). Default is square pixels." )
 #define DEINTERLACE_TEXT N_("Deinterlace video")
 #define DEINTERLACE_LONGTEXT N_( \
-    "Allows you to deinterlace the image after loading." )
+    "Deinterlace the image after loading it." )
 #define DEINTERLACE_MODULE_TEXT N_("Deinterlace module")
 #define DEINTERLACE_MODULE_LONGTEXT N_( \
-    "Specifies the deinterlace module to use." )
+    "Deinterlace module to use." )
+#define CHROMA_TEXT N_("Chroma used.")
+#define CHROMA_LONGTEXT N_( \
+    "Force use of a specific chroma for output. Default is I420." )
 
-static char *ppsz_deinterlace_type[] =
+static const char *const ppsz_deinterlace_type[] =
 {
     "deinterlace", "ffmpeg-deinterlace"
 };
@@ -71,30 +86,44 @@ static char *ppsz_deinterlace_type[] =
 vlc_module_begin();
     set_category( CAT_INPUT );
     set_subcategory( SUBCAT_INPUT_VCODEC );
-    set_shortname( _("Fake") );
-    set_description( _("Fake video decoder") );
+    set_shortname( N_("Fake") );
+    set_description( N_("Fake video decoder") );
     set_capability( "decoder", 1000 );
     set_callbacks( OpenDecoder, CloseDecoder );
     add_shortcut( "fake" );
 
     add_file( "fake-file", "", NULL, FILE_TEXT,
-                FILE_LONGTEXT, VLC_FALSE );
+                FILE_LONGTEXT, false );
+    add_integer( "fake-file-reload", 0, NULL, RELOAD_TEXT,
+                RELOAD_LONGTEXT, false );
     add_integer( "fake-width", 0, NULL, WIDTH_TEXT,
-                 WIDTH_LONGTEXT, VLC_TRUE );
+                 WIDTH_LONGTEXT, true );
     add_integer( "fake-height", 0, NULL, HEIGHT_TEXT,
-                 HEIGHT_LONGTEXT, VLC_TRUE );
+                 HEIGHT_LONGTEXT, true );
     add_bool( "fake-keep-ar", 0, NULL, KEEP_AR_TEXT, KEEP_AR_LONGTEXT,
-              VLC_TRUE );
+              true );
     add_string( "fake-aspect-ratio", "", NULL,
-                ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, VLC_TRUE );
+                ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, true );
     add_bool( "fake-deinterlace", 0, NULL, DEINTERLACE_TEXT,
-              DEINTERLACE_LONGTEXT, VLC_FALSE );
+              DEINTERLACE_LONGTEXT, false );
     add_string( "fake-deinterlace-module", "deinterlace", NULL,
                 DEINTERLACE_MODULE_TEXT, DEINTERLACE_MODULE_LONGTEXT,
-                VLC_FALSE );
+                false );
         change_string_list( ppsz_deinterlace_type, 0, 0 );
+    add_string( "fake-chroma", "I420", NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
+                true );
 vlc_module_end();
 
+struct decoder_sys_t
+{
+    picture_t *p_image;
+    vlc_mutex_t lock;
+
+    bool  b_reload;
+    mtime_t     i_reload;
+    mtime_t     i_next;
+};
+
 /*****************************************************************************
  * OpenDecoder: probe the decoder and return score
  *****************************************************************************/
@@ -105,8 +134,8 @@ static int OpenDecoder( vlc_object_t *p_this )
     image_handler_t *p_handler;
     video_format_t fmt_in, fmt_out;
     picture_t *p_image;
-    char *psz_file, *psz_local;
-    vlc_bool_t b_keep_ar;
+    char *psz_file, *psz_chroma;
+    bool b_keep_ar;
     int i_aspect = 0;
 
     if( p_dec->fmt_in.i_codec != VLC_FOURCC('f','a','k','e') )
@@ -114,19 +143,47 @@ static int OpenDecoder( vlc_object_t *p_this )
         return VLC_EGENERIC;
     }
 
-    var_Create( p_dec, "fake-file", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
-    var_Get( p_dec, "fake-file", &val );
-    if( val.psz_string == NULL || !*val.psz_string )
+    p_dec->p_sys = (decoder_sys_t *)malloc( sizeof( decoder_sys_t ) );
+    if( !p_dec->p_sys )
+    {
+        return VLC_ENOMEM;
+    }
+    memset( p_dec->p_sys, 0, sizeof( decoder_sys_t ) );
+
+    psz_file = var_CreateGetNonEmptyStringCommand( p_dec, "fake-file" );
+    if( !psz_file )
     {
-        if( val.psz_string ) free( val.psz_string );
         msg_Err( p_dec, "specify a file with --fake-file=..." );
+        free( p_dec->p_sys );
         return VLC_EGENERIC;
     }
-    psz_file = val.psz_string;
+    var_AddCallback( p_dec, "fake-file", FakeCallback, p_dec );
 
     memset( &fmt_in, 0, sizeof(fmt_in) );
     memset( &fmt_out, 0, sizeof(fmt_out) );
-    fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
+
+    val.i_int = var_CreateGetIntegerCommand( p_dec, "fake-file-reload" );
+    if( val.i_int > 0)
+    {
+        p_dec->p_sys->b_reload = true;
+        p_dec->p_sys->i_reload = (mtime_t)(val.i_int * 1000000);
+        p_dec->p_sys->i_next   = (mtime_t)(p_dec->p_sys->i_reload + mdate());
+    }
+
+    psz_chroma = var_CreateGetString( p_dec, "fake-chroma" );
+    if( strlen( psz_chroma ) != 4 )
+    {
+        msg_Warn( p_dec, "Invalid chroma (%s). Using I420.", psz_chroma );
+        fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
+    }
+    else
+    {
+        fmt_out.i_chroma = VLC_FOURCC( psz_chroma[0],
+                                       psz_chroma[1],
+                                       psz_chroma[2],
+                                       psz_chroma[3] );
+    }
+    free( psz_chroma );
 
     var_Create( p_dec, "fake-keep-ar", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
     var_Get( p_dec, "fake-keep-ar", &val );
@@ -160,19 +217,19 @@ static int OpenDecoder( vlc_object_t *p_this )
     }
 
     p_handler = image_HandlerCreate( p_dec );
-    psz_local = ToLocale( psz_file );
-    p_image = image_ReadUrl( p_handler, psz_local, &fmt_in, &fmt_out );
-    LocaleFree( psz_local );
+    p_image = image_ReadUrl( p_handler, psz_file, &fmt_in, &fmt_out );
     image_HandlerDelete( p_handler );
 
     if ( p_image == NULL )
     {
         msg_Err( p_dec, "unable to read image file %s", psz_file );
+        free( psz_file );
+        free( p_dec->p_sys );
         return VLC_EGENERIC;
     }
     msg_Dbg( p_dec, "file %s loaded successfully", psz_file );
 
-    if ( psz_file ) free( psz_file );
+    free( psz_file );
 
     if ( b_keep_ar )
     {
@@ -223,7 +280,7 @@ static int OpenDecoder( vlc_object_t *p_this )
             }
             else
             {
-                p_old->pf_release( p_old );
+                picture_Release( p_old );
             }
         }
     }
@@ -251,7 +308,7 @@ static int OpenDecoder( vlc_object_t *p_this )
         p_handler = image_HandlerCreate( p_dec );
         p_image = image_Filter( p_handler, p_old, &fmt_out, val.psz_string );
         image_HandlerDelete( p_handler );
-        if ( val.psz_string != NULL ) free( val.psz_string );
+        free( val.psz_string );
 
         if ( p_image == NULL )
         {
@@ -260,18 +317,20 @@ static int OpenDecoder( vlc_object_t *p_this )
         }
         else
         {
-            p_old->pf_release( p_old );
+            picture_Release( p_old );
         }
     }
 
     /* Set output properties */
     p_dec->fmt_out.i_cat = VIDEO_ES;
-    p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
+    p_dec->fmt_out.i_codec = fmt_out.i_chroma;
     p_dec->fmt_out.video = fmt_out;
 
     /* Set callbacks */
     p_dec->pf_decode_video = DecodeBlock;
-    p_dec->p_sys = (decoder_sys_t *)p_image;
+
+    p_dec->p_sys->p_image = p_image;
+    vlc_mutex_init( &p_dec->p_sys->lock );
 
     return VLC_SUCCESS;
 }
@@ -281,7 +340,7 @@ static int OpenDecoder( vlc_object_t *p_this )
  ****************************************************************************/
 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
 {
-    picture_t *p_image = (picture_t *)p_dec->p_sys;
+    decoder_sys_t *p_sys = (decoder_sys_t*) p_dec->p_sys;
     picture_t *p_pic;
 
     if( pp_block == NULL || !*pp_block ) return NULL;
@@ -292,7 +351,16 @@ static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
         goto error;
     }
 
-    vout_CopyPicture( p_dec, p_pic, p_image );
+    if( p_sys->b_reload && (mdate() >= p_sys->i_next) )
+    {
+        var_TriggerCallback( p_dec, "fake-file" );
+        /* next period */
+        p_sys->i_next = (mtime_t)(p_sys->i_reload + mdate());
+    }
+    vlc_mutex_lock( &p_dec->p_sys->lock );
+    vout_CopyPicture( p_dec, p_pic, p_dec->p_sys->p_image );
+    vlc_mutex_unlock( &p_dec->p_sys->lock );
+
     p_pic->date = (*pp_block)->i_pts;
 
 error:
@@ -308,8 +376,74 @@ error:
 static void CloseDecoder( vlc_object_t *p_this )
 {
     decoder_t *p_dec = (decoder_t *)p_this;
-    picture_t *p_image = (picture_t *)p_dec->p_sys;
+    picture_t *p_image = p_dec->p_sys->p_image;
 
     if( p_image != NULL )
-        p_image->pf_release( p_image );
+        picture_Release( p_image );
+
+    vlc_mutex_destroy( &p_dec->p_sys->lock );
+    free( p_dec->p_sys );
+}
+
+/*****************************************************************************
+ * FakeCallback: Image source change callback.
+ *****************************************************************************/
+static int FakeCallback( vlc_object_t *p_this, char const *psz_var,
+                         vlc_value_t oldval, vlc_value_t newval,
+                         void *p_data )
+{
+    VLC_UNUSED(p_this); VLC_UNUSED(oldval);
+    decoder_t *p_dec = (decoder_t *)p_data;
+
+    if( !strcmp( psz_var, "fake-file" ) )
+    {
+        image_handler_t *p_handler;
+        picture_t *p_new_image;
+        video_format_t fmt_in, fmt_out;
+        char *psz_file = newval.psz_string;
+        picture_t *p_image;
+
+        vlc_mutex_lock( &p_dec->p_sys->lock );
+        p_image = p_dec->p_sys->p_image;
+
+        if( !psz_file || !*psz_file )
+        {
+            msg_Err( p_dec, "fake-file value must be non empty." );
+            vlc_mutex_unlock( &p_dec->p_sys->lock );
+            return VLC_EGENERIC;
+        }
+        msg_Dbg( p_dec, "Changing fake-file to %s.", psz_file );
+
+        memset( &fmt_in, 0, sizeof(fmt_in) );
+        fmt_out = p_dec->fmt_out.video;
+        p_handler = image_HandlerCreate( p_dec );
+        p_new_image = image_ReadUrl( p_handler, psz_file, &fmt_in, &fmt_out );
+        image_HandlerDelete( p_handler );
+
+        if( !p_new_image )
+        {
+            msg_Err( p_dec, "error while reading image (%s)", psz_file );
+            vlc_mutex_unlock( &p_dec->p_sys->lock );
+            return VLC_EGENERIC;
+        }
+
+        p_dec->p_sys->p_image = p_new_image;
+        picture_Release( p_image );
+        vlc_mutex_unlock( &p_dec->p_sys->lock );
+    }
+    else if( !strcmp( psz_var, "fake-file-reload" ) )
+    {
+        if( newval.i_int > 0)
+        {
+            p_dec->p_sys->b_reload = true;
+            p_dec->p_sys->i_reload = (mtime_t)(newval.i_int * 1000000);
+            p_dec->p_sys->i_next   = (mtime_t)(p_dec->p_sys->i_reload + mdate());
+        }
+        else
+        {
+            p_dec->p_sys->b_reload = false;
+        }
+    }
+
+    return VLC_SUCCESS;
 }