]> git.sesse.net Git - vlc/blobdiff - modules/stream_out/mosaic_bridge.c
Add callback to change mosaic mask at runtime.
[vlc] / modules / stream_out / mosaic_bridge.c
index 41c2a98cfabde2ab3763de36383295a8e1f82065..cb0a8aafc627c86b6e066846fd158fb618d4ad93 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * mosaic_bridge.c:
  *****************************************************************************
- * Copyright (C) 2004-2005 VideoLAN
+ * Copyright (C) 2004-2005 the VideoLAN team
  * $Id$
  *
  * Authors: Antoine Cellerier <dionoea@videolan.org>
@@ -19,7 +19,7 @@
  *
  * 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.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -30,8 +30,9 @@
 #include <string.h>                                            /* strerror() */
 
 #include <vlc/vlc.h>
-#include <vlc/sout.h>
-#include <vlc/decoder.h>
+#include <vlc_sout.h>
+#include <vlc_block.h>
+#include <vlc_codec.h>
 
 #include "vlc_image.h"
 
@@ -48,14 +49,21 @@ struct sout_stream_sys_t
     decoder_t       *p_decoder;
     image_handler_t *p_image; /* filter for resizing */
     int i_height, i_width;
+    unsigned int i_sar_num, i_sar_den;
     char *psz_id;
     vlc_bool_t b_inited;
+
+    picture_t *p_mask;
+    vlc_mutex_t mask_lock;
 };
 
-#define PICTURE_RING_SIZE 64
+#define PICTURE_RING_SIZE 4
 struct decoder_owner_sys_t
 {
     picture_t *pp_pics[PICTURE_RING_SIZE];
+
+    /* Current format in use by the output */
+    video_format_t video;
 };
 
 typedef void (* pf_release_t)( picture_t * );
@@ -79,6 +87,22 @@ static void ReleasePicture( picture_t *p_pic )
     }
 }
 
+/* copied from video_filters/erase.c . Gruik ? */
+static void LoadMask( sout_stream_t *p_stream, const char *psz_filename )
+{
+    image_handler_t *p_image;
+    video_format_t fmt_in, fmt_out;
+    memset( &fmt_in, 0, sizeof( video_format_t ) );
+    memset( &fmt_out, 0, sizeof( video_format_t ) );
+    fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
+    if( p_stream->p_sys->p_mask )
+        p_stream->p_sys->p_mask->pf_release( p_stream->p_sys->p_mask );
+    p_image = image_HandlerCreate( p_stream );
+    p_stream->p_sys->p_mask =
+        image_ReadUrl( p_image, psz_filename, &fmt_in, &fmt_out );
+    image_HandlerDelete( p_image );
+}
+
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
@@ -92,6 +116,8 @@ static void video_del_buffer( decoder_t *, picture_t * );
 static picture_t *video_new_buffer( decoder_t * );
 static void video_link_picture_decoder( decoder_t *, picture_t * );
 static void video_unlink_picture_decoder( decoder_t *, picture_t * );
+static int MosaicBridgeCallback( vlc_object_t *, char const *,
+                                 vlc_value_t, vlc_value_t, void * );
 
 /*****************************************************************************
  * Module descriptor
@@ -102,10 +128,16 @@ static void video_unlink_picture_decoder( decoder_t *, picture_t * );
 
 #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 RATIO_TEXT N_("Sample aspect ratio")
+#define RATIO_LONGTEXT N_( \
+    "Sample aspect ratio of the destination (1:1, 3:4, 2:3)." )
+#define MASK_TEXT N_("Transparency mask")
+#define MASK_LONGTEXT N_( \
+    "Alpha blending transparency mask. Use's a png alpha channel.")
 
 #define SOUT_CFG_PREFIX "sout-mosaic-bridge-"
 
@@ -121,14 +153,18 @@ vlc_module_begin();
                  WIDTH_LONGTEXT, VLC_TRUE );
     add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
                  HEIGHT_LONGTEXT, VLC_TRUE );
+    add_string( SOUT_CFG_PREFIX "sar", "1:1", NULL, RATIO_TEXT,
+                RATIO_LONGTEXT, VLC_FALSE );
+    add_string( SOUT_CFG_PREFIX "mask", NULL, NULL, MASK_TEXT,
+                MASK_LONGTEXT, VLC_FALSE );
 
     set_callbacks( Open, Close );
 
-    var_Create( p_module->p_libvlc, "mosaic-lock", VLC_VAR_MUTEX );
+    var_Create( p_module->p_libvlc_global, "mosaic-lock", VLC_VAR_MUTEX );
 vlc_module_end();
 
 static const char *ppsz_sout_options[] = {
-    "id", "width", "height", NULL
+    "id", "width", "height", "sar", "mask", NULL
 };
 
 /*****************************************************************************
@@ -138,31 +174,69 @@ static int Open( vlc_object_t *p_this )
 {
     sout_stream_t     *p_stream = (sout_stream_t *)p_this;
     sout_stream_sys_t *p_sys;
-    libvlc_t *p_libvlc = p_this->p_libvlc;
+    libvlc_global_data_t *p_libvlc_global = p_this->p_libvlc_global;
     vlc_value_t val;
 
-    sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
+    config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
                    p_stream->p_cfg );
 
     p_sys          = malloc( sizeof( sout_stream_sys_t ) );
     p_stream->p_sys = p_sys;
     p_sys->b_inited = VLC_FALSE;
 
-    var_Get( p_libvlc, "mosaic-lock", &val );
+    var_Get( p_libvlc_global, "mosaic-lock", &val );
     p_sys->p_lock = val.p_address;
 
     var_Get( p_stream, SOUT_CFG_PREFIX "id", &val );
     p_sys->psz_id = val.psz_string;
 
     var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
-    p_sys->i_height = val.i_int; 
+    p_sys->i_height = val.i_int;
 
     var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
-    p_sys->i_width = val.i_int; 
+    p_sys->i_width = val.i_int;
+
+    vlc_mutex_init( p_stream, &p_sys->mask_lock );
+    val.psz_string =
+    var_CreateGetStringCommand( p_stream, SOUT_CFG_PREFIX "mask" );
+    var_AddCallback( p_stream, SOUT_CFG_PREFIX "mask", MosaicBridgeCallback,
+                     p_stream );
+    if( val.psz_string && *val.psz_string )
+    {
+        p_sys->p_mask = NULL;
+        LoadMask( p_stream, val.psz_string );
+        if( !p_sys->p_mask )
+            msg_Err( p_stream, "Error while loading mask (%s).",
+                     val.psz_string );
+    }
+    else
+        p_sys->p_mask = NULL;
+    free( val.psz_string );
 
-    if ( p_sys->i_height || p_sys->i_width )
+    var_Get( p_stream, SOUT_CFG_PREFIX "sar", &val );
+    if ( val.psz_string )
     {
-        p_sys->p_image = image_HandlerCreate( p_stream );
+        char *psz_parser = strchr( val.psz_string, ':' );
+
+        if( psz_parser )
+        {
+            *psz_parser++ = '\0';
+            p_sys->i_sar_num = atoi( val.psz_string );
+            p_sys->i_sar_den = atoi( psz_parser );
+            vlc_ureduce( &p_sys->i_sar_num, &p_sys->i_sar_den,
+                         p_sys->i_sar_num, p_sys->i_sar_den, 0 );
+        }
+        else
+        {
+            msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
+            p_sys->i_sar_num = p_sys->i_sar_den = 1;
+        }
+
+        free( val.psz_string );
+    }
+    else
+    {
+        p_sys->i_sar_num = p_sys->i_sar_den = 1;
     }
 
     p_stream->pf_add    = Add;
@@ -187,6 +261,10 @@ static void Close( vlc_object_t * p_this )
     if ( p_sys->psz_id )
         free( p_sys->psz_id );
 
+    vlc_mutex_destroy( &p_sys->mask_lock );
+    if( p_stream->p_sys->p_mask )
+        p_stream->p_sys->p_mask->pf_release( p_stream->p_sys->p_mask );
+
     free( p_sys );
 }
 
@@ -219,6 +297,7 @@ static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
     p_sys->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
     for( i = 0; i < PICTURE_RING_SIZE; i++ )
         p_sys->p_decoder->p_owner->pp_pics[i] = 0;
+    p_sys->p_decoder->p_owner->video = p_fmt->video;
     //p_sys->p_decoder->p_cfg = p_sys->p_video_cfg;
 
     p_sys->p_decoder->p_module =
@@ -238,14 +317,14 @@ static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
     p_bridge = GetBridge( p_stream );
     if ( p_bridge == NULL )
     {
-        libvlc_t *p_libvlc = p_stream->p_libvlc;
+        libvlc_global_data_t *p_libvlc_global = p_stream->p_libvlc_global;
         vlc_value_t val;
 
         p_bridge = malloc( sizeof( bridge_t ) );
 
-        var_Create( p_libvlc, "mosaic-struct", VLC_VAR_ADDRESS );
+        var_Create( p_libvlc_global, "mosaic-struct", VLC_VAR_ADDRESS );
         val.p_address = p_bridge;
-        var_Set( p_libvlc, "mosaic-struct", val );
+        var_Set( p_libvlc_global, "mosaic-struct", val );
 
         p_bridge->i_es_num = 0;
         p_bridge->pp_es = NULL;
@@ -276,6 +355,11 @@ static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
 
     vlc_mutex_unlock( p_sys->p_lock );
 
+    if ( p_sys->i_height || p_sys->i_width )
+    {
+        p_sys->p_image = image_HandlerCreate( p_stream );
+    }
+
     msg_Dbg( p_stream, "mosaic bridge id=%s pos=%d", p_es->psz_id, i );
 
     return (sout_stream_id_t *)p_sys;
@@ -294,12 +378,25 @@ static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
         return VLC_SUCCESS;
     }
 
-    if ( p_sys->p_decoder )
+    if ( p_sys->p_decoder != NULL )
     {
+        picture_t **pp_ring = p_sys->p_decoder->p_owner->pp_pics;
+
         if( p_sys->p_decoder->p_module )
             module_Unneed( p_sys->p_decoder, p_sys->p_decoder->p_module );
         vlc_object_detach( p_sys->p_decoder );
         vlc_object_destroy( p_sys->p_decoder );
+
+        for( i = 0; i < PICTURE_RING_SIZE; i++ )
+        {
+            if ( pp_ring[i] != NULL )
+            {
+                if ( pp_ring[i]->p_data_orig != NULL )
+                    free( pp_ring[i]->p_data_orig );
+                free( pp_ring[i]->p_sys );
+                free( pp_ring[i] );
+            }
+        }
     }
 
     vlc_mutex_lock( p_sys->p_lock );
@@ -326,12 +423,12 @@ static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
 
     if ( b_last_es )
     {
-        libvlc_t *p_libvlc = p_stream->p_libvlc;
+        libvlc_global_data_t *p_libvlc_global = p_stream->p_libvlc_global;
         for ( i = 0; i < p_bridge->i_es_num; i++ )
             free( p_bridge->pp_es[i] );
         free( p_bridge->pp_es );
         free( p_bridge );
-        var_Destroy( p_libvlc, "mosaic-struct" );
+        var_Destroy( p_libvlc_global, "mosaic-struct" );
     }
 
     vlc_mutex_unlock( p_sys->p_lock );
@@ -380,14 +477,41 @@ static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
     {
         picture_t *p_new_pic;
 
-        if ( p_sys->i_height || p_sys->i_width )
+        if( p_sys->i_height || p_sys->i_width )
         {
-            video_format_t fmt_out = {0}, fmt_in = {0};
+            video_format_t fmt_out, fmt_in;
+
+            memset( &fmt_in, 0, sizeof(video_format_t) );
+            memset( &fmt_out, 0, sizeof(video_format_t) );
             fmt_in = p_sys->p_decoder->fmt_out.video;
 
-            fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
-            fmt_out.i_width = p_sys->i_width;
-            fmt_out.i_height = p_sys->i_height;
+            if( p_sys->p_mask )
+            {
+                vlc_mutex_lock( &p_sys->mask_lock );
+                fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
+            }
+            else
+                fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
+
+            if ( !p_sys->i_height )
+            {
+                fmt_out.i_width = p_sys->i_width;
+                fmt_out.i_height = (p_sys->i_width * VOUT_ASPECT_FACTOR
+                    * p_sys->i_sar_num / p_sys->i_sar_den / fmt_in.i_aspect)
+                      & ~0x1;
+            }
+            else if ( !p_sys->i_width )
+            {
+                fmt_out.i_height = p_sys->i_height;
+                fmt_out.i_width = (p_sys->i_height * fmt_in.i_aspect
+                    * p_sys->i_sar_den / p_sys->i_sar_num / VOUT_ASPECT_FACTOR)
+                      & ~0x1;
+            }
+            else
+            {
+                fmt_out.i_width = p_sys->i_width;
+                fmt_out.i_height = p_sys->i_height;
+            }
             fmt_out.i_visible_width = fmt_out.i_width;
             fmt_out.i_visible_height = fmt_out.i_height;
 
@@ -398,6 +522,56 @@ static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
                 msg_Err( p_stream, "image conversion failed" );
                 continue;
             }
+
+            if( p_sys->p_mask )
+            {
+                plane_t *p_mask = p_sys->p_mask->p+A_PLANE;
+                plane_t *p_apic = p_new_pic->p+A_PLANE;
+                if(    p_mask->i_visible_pitch
+                    != p_apic->i_visible_pitch
+                    || p_mask->i_visible_lines
+                    != p_apic->i_visible_lines )
+                {
+                    msg_Warn( p_stream,
+                              "Mask size (%d x %d) and image size (%d x %d) "
+                              "don't match. The mask will not be applied.",
+                              p_mask->i_visible_pitch,
+                              p_mask->i_visible_lines,
+                              p_apic->i_visible_pitch,
+                              p_apic->i_visible_lines );
+                }
+                else
+                {
+                    if( p_mask->i_pitch != p_apic->i_pitch
+                    ||  p_mask->i_lines != p_apic->i_lines )
+                    {
+                        /* visible plane sizes match ... but not the undelying
+                         * buffer. I'm not sure that this can happen,
+                         * but better safe than sorry. */
+                        int i_line;
+                        int i_lines = p_mask->i_visible_lines;
+                        uint8_t *p_src = p_mask->p_pixels;
+                        uint8_t *p_dst = p_apic->p_pixels;
+                        int i_src_pitch = p_mask->i_pitch;
+                        int i_dst_pitch = p_apic->i_pitch;
+                        int i_visible_pitch = p_mask->i_visible_pitch;
+                        for( i_line = 0; i_line < i_lines; i_line++,
+                             p_src += i_src_pitch, p_dst += i_dst_pitch )
+                        {
+                            p_stream->p_libvlc->pf_memcpy(
+                                p_dst, p_src, i_visible_pitch );
+                        }
+                    }
+                    else
+                    {
+                        /* plane sizes match */
+                        p_stream->p_libvlc->pf_memcpy(
+                            p_apic->p_pixels, p_mask->p_pixels,
+                            p_mask->i_pitch * p_mask->i_lines );
+                    }
+                }
+                vlc_mutex_unlock( &p_sys->mask_lock );
+            }
         }
         else
         {
@@ -408,7 +582,6 @@ static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
 
             vout_CopyPicture( p_stream, p_new_pic, p_pic );
         }
-        p_pic->pf_release( p_pic );
 
         p_new_pic->i_refcount = 1;
         p_new_pic->i_status = DESTROYED_PICTURE;
@@ -417,6 +590,7 @@ static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
         p_new_pic->pf_release = ReleasePicture;
         p_new_pic->date = p_pic->date;
 
+        p_pic->pf_release( p_pic );
         PushPicture( p_stream, p_new_pic );
     }
 
@@ -426,6 +600,7 @@ static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
 struct picture_sys_t
 {
     vlc_object_t *p_owner;
+    vlc_bool_t b_dead;
 };
 
 static void video_release_buffer( picture_t *p_pic )
@@ -439,14 +614,67 @@ static void video_release_buffer( picture_t *p_pic )
 
 static picture_t *video_new_buffer( decoder_t *p_dec )
 {
+    decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
     picture_t **pp_ring = p_dec->p_owner->pp_pics;
     picture_t *p_pic;
     int i;
 
+    if( p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
+        p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
+        p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
+        p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
+    {
+        if( !p_dec->fmt_out.video.i_sar_num ||
+            !p_dec->fmt_out.video.i_sar_den )
+        {
+            p_dec->fmt_out.video.i_sar_num =
+              p_dec->fmt_out.video.i_aspect * p_dec->fmt_out.video.i_height;
+
+            p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
+              p_dec->fmt_out.video.i_width;
+        }
+
+        vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
+                     &p_dec->fmt_out.video.i_sar_den,
+                     p_dec->fmt_out.video.i_sar_num,
+                     p_dec->fmt_out.video.i_sar_den, 0 );
+
+        if( !p_dec->fmt_out.video.i_visible_width ||
+            !p_dec->fmt_out.video.i_visible_height )
+        {
+            p_dec->fmt_out.video.i_visible_width =
+                p_dec->fmt_out.video.i_width;
+            p_dec->fmt_out.video.i_visible_height =
+                p_dec->fmt_out.video.i_height;
+        }
+
+        p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
+        p_sys->video = p_dec->fmt_out.video;
+
+        for( i = 0; i < PICTURE_RING_SIZE; i++ )
+        {
+            if ( pp_ring[i] != NULL )
+            {
+                if ( pp_ring[i]->i_status == DESTROYED_PICTURE )
+                {
+                    if ( pp_ring[i]->p_data_orig != NULL )
+                        free( pp_ring[i]->p_data_orig );
+                    free( pp_ring[i]->p_sys );
+                    free( pp_ring[i] );
+                }
+                else
+                {
+                    pp_ring[i]->p_sys->b_dead = VLC_TRUE;
+                }
+                pp_ring[i] = NULL;
+            }
+        }
+    }
+
     /* Find an empty space in the picture ring buffer */
     for( i = 0; i < PICTURE_RING_SIZE; i++ )
     {
-        if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
+        if( pp_ring[i] != NULL && pp_ring[i]->i_status == DESTROYED_PICTURE )
         {
             pp_ring[i]->i_status = RESERVED_PICTURE;
             return pp_ring[i];
@@ -454,7 +682,7 @@ static picture_t *video_new_buffer( decoder_t *p_dec )
     }
     for( i = 0; i < PICTURE_RING_SIZE; i++ )
     {
-        if( pp_ring[i] == 0 ) break;
+        if( pp_ring[i] == NULL ) break;
     }
 
     if( i == PICTURE_RING_SIZE )
@@ -481,12 +709,13 @@ static picture_t *video_new_buffer( decoder_t *p_dec )
     if( !p_pic->i_planes )
     {
         free( p_pic );
-        return 0;
+        return NULL;
     }
 
     p_pic->pf_release = video_release_buffer;
     p_pic->p_sys = malloc( sizeof(picture_sys_t) );
     p_pic->p_sys->p_owner = VLC_OBJECT(p_dec);
+    p_pic->p_sys->b_dead = VLC_FALSE;
     p_pic->i_status = RESERVED_PICTURE;
 
     pp_ring[i] = p_pic;
@@ -498,6 +727,13 @@ static void video_del_buffer( decoder_t *p_this, picture_t *p_pic )
 {
     p_pic->i_refcount = 0;
     p_pic->i_status = DESTROYED_PICTURE;
+    if ( p_pic->p_sys->b_dead )
+    {
+        if ( p_pic->p_data_orig != NULL )
+            free( p_pic->p_data_orig );
+        free( p_pic->p_sys );
+        free( p_pic );
+    }
 }
 
 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
@@ -510,3 +746,34 @@ static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
     video_release_buffer( p_pic );
 }
 
+
+/**********************************************************************
+ * Callback to update (some) params on the fly
+ **********************************************************************/
+static int MosaicBridgeCallback( vlc_object_t *p_this, char const *psz_var,
+                                 vlc_value_t oldval, vlc_value_t newval,
+                                 void *p_data )
+{
+    sout_stream_t *p_stream = (sout_stream_t *)p_data;
+    sout_stream_sys_t *p_sys = p_stream->p_sys;
+
+    if( !strcmp( psz_var, SOUT_CFG_PREFIX "mask" ) )
+    {
+        vlc_mutex_lock( &p_sys->mask_lock );
+        if( newval.psz_string && *newval.psz_string )
+        {
+            LoadMask( p_stream, newval.psz_string );
+            if( !p_sys->p_mask )
+                msg_Err( p_stream, "Error while loading mask (%s).",
+                         newval.psz_string );
+        }
+        else if( p_sys->p_mask )
+        {
+            p_sys->p_mask->pf_release( p_sys->p_mask );
+            p_sys->p_mask = NULL;
+        }
+        vlc_mutex_unlock( &p_sys->mask_lock );
+    }
+
+    return VLC_SUCCESS;
+}