]> git.sesse.net Git - vlc/blobdiff - modules/stream_out/switcher.c
Trailing ;
[vlc] / modules / stream_out / switcher.c
index 694dcdc764ca2f48064331881309806435ff4351..dba845fb71975f09c872b7513d6b84c4c19d5389 100644 (file)
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 #include <vlc_sout.h>
 #include <vlc_vout.h>
 
+#include <vlc_block.h>
+
 #include <vlc_charset.h>
 #include <vlc_network.h>
 
@@ -107,31 +110,31 @@ static block_t *AudioGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
 #define AUDIO_LONGTEXT N_( \
     "Mute audio when command is not 0." )
 
-vlc_module_begin();
-    set_description( _("MPEG2 video switcher stream output") );
-    set_capability( "sout stream", 50 );
-    add_shortcut( "switcher" );
-    set_callbacks( Open, Close );
+vlc_module_begin ()
+    set_description( N_("MPEG2 video switcher stream output") )
+    set_capability( "sout stream", 50 )
+    add_shortcut( "switcher" )
+    set_callbacks( Open, Close )
 
     add_string( SOUT_CFG_PREFIX "files", "", NULL, FILES_TEXT,
-                FILES_LONGTEXT, false );
+                FILES_LONGTEXT, false )
     add_string( SOUT_CFG_PREFIX "sizes", "", NULL, SIZES_TEXT,
-                SIZES_LONGTEXT, false );
+                SIZES_LONGTEXT, false )
     add_string( SOUT_CFG_PREFIX "aspect-ratio", "4:3", NULL, RATIO_TEXT,
-                RATIO_LONGTEXT, false );
+                RATIO_LONGTEXT, false )
     add_integer( SOUT_CFG_PREFIX "port", 5001, NULL,
-                 PORT_TEXT, PORT_LONGTEXT, true );
+                 PORT_TEXT, PORT_LONGTEXT, true )
     add_integer( SOUT_CFG_PREFIX "command", 0, NULL,
-                 COMMAND_TEXT, COMMAND_LONGTEXT, true );
+                 COMMAND_TEXT, COMMAND_LONGTEXT, true )
     add_integer( SOUT_CFG_PREFIX "gop", 8, NULL,
-                 GOP_TEXT, GOP_LONGTEXT, true );
+                 GOP_TEXT, GOP_LONGTEXT, true )
     add_integer( SOUT_CFG_PREFIX "qscale", 5, NULL,
-                 QSCALE_TEXT, QSCALE_LONGTEXT, true );
+                 QSCALE_TEXT, QSCALE_LONGTEXT, true )
     add_bool( SOUT_CFG_PREFIX "mute-audio", 1, NULL,
-              AUDIO_TEXT, AUDIO_LONGTEXT, true );
-vlc_module_end();
+              AUDIO_TEXT, AUDIO_LONGTEXT, true )
+vlc_module_end ()
 
-static const char *ppsz_sout_options[] = {
+static const char *const ppsz_sout_options[] = {
     "files", "sizes", "aspect-ratio", "port", "command", "gop", "qscale",
     "mute-audio", NULL
 };
@@ -182,8 +185,9 @@ static int Open( vlc_object_t *p_this )
     char              *psz_files, *psz_sizes;
     int               i_height = 0, i_width = 0;
 
-    p_sys = malloc( sizeof(sout_stream_sys_t) );
-    memset( p_sys, 0, sizeof(sout_stream_sys_t) );
+    p_sys = calloc( 1, sizeof(sout_stream_sys_t) );
+    if( !p_sys )
+        return VLC_ENOMEM;
 
     p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
     if( !p_sys->p_out )
@@ -313,36 +317,35 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
     sout_stream_sys_t   *p_sys = p_stream->p_sys;
     sout_stream_id_t    *id;
 
-    id = malloc( sizeof( sout_stream_id_t ) );
-    memset( id, 0, sizeof( sout_stream_id_t ) );
-    id->id = NULL;
+    id = calloc( 1, sizeof( sout_stream_id_t ) );
+    if( !id )
+        return NULL;
 
-    if ( p_fmt->i_cat == VIDEO_ES
-            && (p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'v')
-                 || p_fmt->i_codec == VLC_FOURCC('f', 'a', 'k', 'e')) )
+    if( p_fmt->i_cat == VIDEO_ES &&
+        ( p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'v') ||
+          p_fmt->i_codec == VLC_FOURCC('f', 'a', 'k', 'e') ) )
     {
         id->b_switcher_video = true;
         p_fmt->i_codec = VLC_FOURCC('m', 'p', 'g', 'v');
-        msg_Dbg( p_stream,
-                 "creating video switcher for fcc=`%4.4s' cmd:%d",
+        msg_Dbg( p_stream, "creating video switcher for fcc=`%4.4s' cmd:%d",
                  (char*)&p_fmt->i_codec, p_sys->i_cmd );
     }
-    else if ( p_fmt->i_cat == AUDIO_ES
-               && p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'a')
-               && p_sys->b_audio )
+    else if ( p_fmt->i_cat == AUDIO_ES &&
+              p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'a') &&
+              p_sys->b_audio )
     {
         int i_ff_codec = CODEC_ID_MP2;
         int i;
 
         id->b_switcher_audio = true;
-        msg_Dbg( p_stream,
-                 "creating audio switcher for fcc=`%4.4s' cmd:%d",
+        msg_Dbg( p_stream, "creating audio switcher for fcc=`%4.4s' cmd:%d",
                  (char*)&p_fmt->i_codec, p_sys->i_cmd );
 
         /* Allocate the encoder right now. */
         if( i_ff_codec == 0 )
         {
             msg_Err( p_stream, "cannot find encoder" );
+            free( id );
             return NULL;
         }
 
@@ -350,6 +353,7 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
         if( !id->ff_enc )
         {
             msg_Err( p_stream, "cannot find encoder (avcodec)" );
+            free( id );
             return NULL;
         }
 
@@ -383,29 +387,29 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
         if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
         {
             msg_Err( p_stream, "cannot open encoder" );
+            av_free( id->ff_enc_c );
+            free( id );
             return NULL;
         }
 
         id->p_buffer_out = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * 2 );
-        id->p_samples = malloc( id->ff_enc_c->frame_size
-                                 * p_fmt->audio.i_channels * sizeof(int16_t) );
-        memset( id->p_samples, 0,
-                id->ff_enc_c->frame_size * p_fmt->audio.i_channels
-                 * sizeof(int16_t) );
+        id->p_samples = calloc( id->ff_enc_c->frame_size * p_fmt->audio.i_channels,
+                                sizeof(int16_t) );
+        if( !id->p_buffer_out || !id->p_samples )
+            goto error;
 
-        for ( i = 0; i < MAX_AUDIO; i++ )
+        for( i = 0; i < MAX_AUDIO; i++ )
         {
-            if ( p_sys->pp_audio_ids[i] == NULL )
+            if( p_sys->pp_audio_ids[i] == NULL )
             {
                 p_sys->pp_audio_ids[i] = id;
                 break;
             }
         }
-        if ( i == MAX_AUDIO )
+        if( i == MAX_AUDIO )
         {
             msg_Err( p_stream, "too many audio streams!" );
-            free( id );
-            return NULL;
+            goto error;
         }
     }
     else
@@ -420,13 +424,16 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
     /* open output stream */
     id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
 
-    if ( id->id == NULL )
-    {
-        free( id );
-        return NULL;
-    }
+    if( id->id != NULL )
+        return id;
 
-    return id;
+error:
+    avcodec_close( id->ff_enc_c );
+    free( id->p_samples );
+    free( id->p_buffer_out );
+    av_free( id->ff_enc_c );
+    free( id );
+    return NULL;
 }
 
 /*****************************************************************************
@@ -860,10 +867,8 @@ static block_t *VideoGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
             = id->ff_enc_c->coded_frame->motion_subsample_log2;
         id->p_frame->mb_type = malloc( ((mb_width + 1) * (mb_height + 1) + 1)
                                     * sizeof(uint32_t) );
-        p_stream->p_libvlc->pf_memcpy( id->p_frame->mb_type,
-                                    id->ff_enc_c->coded_frame->mb_type,
-                                    (mb_width + 1) * mb_height
-                                      * sizeof(id->p_frame->mb_type[0]));
+        vlc_memcpy( id->p_frame->mb_type, id->ff_enc_c->coded_frame->mb_type,
+                    (mb_width + 1) * mb_height * sizeof(id->p_frame->mb_type[0]));
 
         for ( i = 0; i < 2; i++ )
         {
@@ -877,24 +882,24 @@ static block_t *VideoGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
             {
                 id->p_frame->motion_val[i] = malloc( 2 * stride * height
                                                 * sizeof(int16_t) );
-                p_stream->p_libvlc->pf_memcpy( id->p_frame->motion_val[i],
-                                     id->ff_enc_c->coded_frame->motion_val[i],
-                                     2 * stride * height * sizeof(int16_t) );
+                vlc_memcpy( id->p_frame->motion_val[i],
+                            id->ff_enc_c->coded_frame->motion_val[i],
+                            2 * stride * height * sizeof(int16_t) );
             }
             if ( id->ff_enc_c->coded_frame->ref_index[i] )
             {
                 id->p_frame->ref_index[i] = malloc( b8_stride * 2 * mb_height
                                                * sizeof(int8_t) );
-                p_stream->p_libvlc->pf_memcpy( id->p_frame->ref_index[i],
-                                 id->ff_enc_c->coded_frame->ref_index[i],
-                                 b8_stride * 2 * mb_height * sizeof(int8_t));
+                vlc_memcpy( id->p_frame->ref_index[i],
+                            id->ff_enc_c->coded_frame->ref_index[i],
+                            b8_stride * 2 * mb_height * sizeof(int8_t));
             }
         }
     }
 #endif
 
     p_out = block_New( p_stream, i_out );
-    p_stream->p_libvlc->pf_memcpy( p_out->p_buffer, id->p_buffer_out, i_out );
+    vlc_memcpy( p_out->p_buffer, id->p_buffer_out, i_out );
     p_out->i_length = p_buffer->i_length;
     p_out->i_pts = p_buffer->i_dts;
     p_out->i_dts = p_buffer->i_dts;
@@ -937,7 +942,7 @@ static block_t *AudioGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
         return NULL;
 
     p_out = block_New( p_stream, i_out );
-    p_stream->p_libvlc->pf_memcpy( p_out->p_buffer, id->p_buffer_out, i_out );
+    vlc_memcpy( p_out->p_buffer, id->p_buffer_out, i_out );
     p_out->i_length = p_buffer->i_length;
     p_out->i_pts = p_buffer->i_dts;
     p_out->i_dts = p_buffer->i_dts;