]> git.sesse.net Git - vlc/blobdiff - modules/stream_out/switcher.c
playlist: Don't allow pl_Release(p_playlist) and pl_Yield(p_playlist).
[vlc] / modules / stream_out / switcher.c
index 7c1b491fa6cae9524d7cab814f7e438e821a21c1..a28c4096addd54c647c3861cab68a86850e8ea4a 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * switcher.c: MPEG2 video switcher module
  *****************************************************************************
- * Copyright (C) 2004 VideoLAN
+ * Copyright (C) 2004 the VideoLAN team
  * $Id$
  *
  * Authors: Christophe Massiot <massiot@via.ecp.fr>
  *
  * 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 <stdio.h>
-#include <stdlib.h>
 #include <math.h>
 
-#include <vlc/vlc.h>
-#include <vlc/sout.h>
-#include <vlc/vout.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_sout.h>
+#include <vlc_vout.h>
 
-#include "network.h"
+#include <vlc_charset.h>
+#include <vlc_network.h>
 
 #define HAVE_MMX
-#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>
@@ -48,9 +54,8 @@
 #endif
 
 #define SOUT_CFG_PREFIX "sout-switcher-"
-#define MAX_PICTURES 12
-#define MAX_AUDIO 12
-#define AVCODEC_MAX_VIDEO_FRAME_SIZE (3*1024*1024)
+#define MAX_PICTURES 10
+#define MAX_AUDIO 30
 #define MAX_THRESHOLD 99999999
 
 /*****************************************************************************
@@ -84,6 +89,9 @@ static block_t *AudioGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
 #define SIZES_TEXT N_("Sizes")
 #define SIZES_LONGTEXT N_( \
     "List of sizes separated by colons (720x576:480x576)." )
+#define RATIO_TEXT N_("Aspect ratio")
+#define RATIO_LONGTEXT N_( \
+    "Aspect ratio (4:3, 16:9)." )
 #define PORT_TEXT N_("Command UDP port")
 #define PORT_LONGTEXT N_( \
     "UDP port to listen to for commands." )
@@ -95,30 +103,38 @@ static block_t *AudioGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
     "Number of P frames between two I frames." )
 #define QSCALE_TEXT N_("Quantizer scale")
 #define QSCALE_LONGTEXT N_( \
-    "Quantizer scale." )
+    "Fixed quantizer scale to use." )
+#define AUDIO_TEXT N_("Mute audio")
+#define AUDIO_LONGTEXT N_( \
+    "Mute audio when command is not 0." )
 
 vlc_module_begin();
-    set_description( _("MPEG2 video switcher stream output") );
+    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, VLC_FALSE );
+                FILES_LONGTEXT, false );
     add_string( SOUT_CFG_PREFIX "sizes", "", NULL, SIZES_TEXT,
-                SIZES_LONGTEXT, VLC_FALSE );
+                SIZES_LONGTEXT, false );
+    add_string( SOUT_CFG_PREFIX "aspect-ratio", "4:3", NULL, RATIO_TEXT,
+                RATIO_LONGTEXT, false );
     add_integer( SOUT_CFG_PREFIX "port", 5001, NULL,
-                 PORT_TEXT, PORT_LONGTEXT, VLC_TRUE );
+                 PORT_TEXT, PORT_LONGTEXT, true );
     add_integer( SOUT_CFG_PREFIX "command", 0, NULL,
-                 COMMAND_TEXT, COMMAND_LONGTEXT, VLC_TRUE );
+                 COMMAND_TEXT, COMMAND_LONGTEXT, true );
     add_integer( SOUT_CFG_PREFIX "gop", 8, NULL,
-                 GOP_TEXT, GOP_LONGTEXT, VLC_TRUE );
+                 GOP_TEXT, GOP_LONGTEXT, true );
     add_integer( SOUT_CFG_PREFIX "qscale", 5, NULL,
-                 QSCALE_TEXT, QSCALE_LONGTEXT, VLC_TRUE );
+                 QSCALE_TEXT, QSCALE_LONGTEXT, true );
+    add_bool( SOUT_CFG_PREFIX "mute-audio", 1, NULL,
+              AUDIO_TEXT, AUDIO_LONGTEXT, true );
 vlc_module_end();
 
-static const char *ppsz_sout_options[] = {
-    "files", "sizes", "port", "command", "gop", "qscale", NULL
+static const char *const ppsz_sout_options[] = {
+    "files", "sizes", "aspect-ratio", "port", "command", "gop", "qscale",
+    "mute-audio", NULL
 };
 
 struct sout_stream_sys_t
@@ -126,7 +142,9 @@ struct sout_stream_sys_t
     sout_stream_t   *p_out;
     int             i_gop;
     int             i_qscale;
+    int             i_aspect;
     sout_stream_id_t *pp_audio_ids[MAX_AUDIO];
+    bool      b_audio;
 
     /* Pictures */
     picture_t       p_pictures[MAX_PICTURES];
@@ -140,8 +158,8 @@ struct sout_stream_sys_t
 struct sout_stream_id_t
 {
     void            *id;
-    vlc_bool_t      b_switcher_video;
-    vlc_bool_t      b_switcher_audio;
+    bool      b_switcher_video;
+    bool      b_switcher_audio;
     es_format_t     f_src;
     block_t         *p_queued;
 
@@ -149,7 +167,7 @@ struct sout_stream_id_t
     AVCodec         *ff_enc;
     AVCodecContext  *ff_enc_c;
     AVFrame         *p_frame;
-    char            *p_buffer_out;
+    uint8_t         *p_buffer_out;
     int             i_nb_pred;
     int16_t         *p_samples;
 };
@@ -176,7 +194,7 @@ static int Open( vlc_object_t *p_this )
         return VLC_EGENERIC;
     }
 
-    sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
+    config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
                    p_stream->p_cfg );
 
     var_Get( p_stream, SOUT_CFG_PREFIX "files", &val );
@@ -219,8 +237,32 @@ static int Open( vlc_object_t *p_this )
         p_sys->i_nb_pictures++;
     }
 
+    var_Get( p_stream, SOUT_CFG_PREFIX "aspect-ratio", &val );
+    if ( val.psz_string )
+    {
+        char *psz_parser = strchr( val.psz_string, ':' );
+
+        if( psz_parser )
+        {
+            *psz_parser++ = '\0';
+            p_sys->i_aspect = atoi( val.psz_string ) * VOUT_ASPECT_FACTOR
+                / atoi( psz_parser );
+        }
+        else
+        {
+            msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
+            p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
+        }
+
+        free( val.psz_string );
+    }
+    else
+    {
+        p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
+    }
+
     var_Get( p_stream, SOUT_CFG_PREFIX "port", &val );
-    p_sys->i_fd = net_OpenUDP( p_stream, NULL, val.i_int, NULL, 0 );
+    p_sys->i_fd = net_ListenUDP1( p_stream, NULL, val.i_int );
     if ( p_sys->i_fd < 0 )
     {
         free( p_sys );
@@ -237,6 +279,9 @@ static int Open( vlc_object_t *p_this )
     var_Get( p_stream, SOUT_CFG_PREFIX "qscale", &val );
     p_sys->i_qscale = val.i_int;
 
+    var_Get( p_stream, SOUT_CFG_PREFIX "mute-audio", &val );
+    p_sys->b_audio = val.b_bool;
+
     p_stream->pf_add    = Add;
     p_stream->pf_del    = Del;
     p_stream->pf_send   = Send;
@@ -277,22 +322,23 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
             && (p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'v')
                  || p_fmt->i_codec == VLC_FOURCC('f', 'a', 'k', 'e')) )
     {
-        id->b_switcher_video = VLC_TRUE;
+        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'",
-                 (char*)&p_fmt->i_codec );
+                 "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_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 = VLC_TRUE;
+        id->b_switcher_audio = true;
         msg_Dbg( p_stream,
-                 "creating audio switcher for fcc=`%4.4s'",
-                 (char*)&p_fmt->i_codec );
+                 "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 )
@@ -311,20 +357,21 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
         id->ff_enc_c = avcodec_alloc_context();
 
         /* Set CPU capabilities */
+        unsigned i_cpu = vlc_CPU();
         id->ff_enc_c->dsp_mask = 0;
-        if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
+        if( !(i_cpu & CPU_CAPABILITY_MMX) )
         {
             id->ff_enc_c->dsp_mask |= FF_MM_MMX;
         }
-        if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
+        if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
         {
             id->ff_enc_c->dsp_mask |= FF_MM_MMXEXT;
         }
-        if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW) )
+        if( !(i_cpu & CPU_CAPABILITY_3DNOW) )
         {
             id->ff_enc_c->dsp_mask |= FF_MM_3DNOW;
         }
-        if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
+        if( !(i_cpu & CPU_CAPABILITY_SSE) )
         {
             id->ff_enc_c->dsp_mask |= FF_MM_SSE;
             id->ff_enc_c->dsp_mask |= FF_MM_SSE2;
@@ -357,7 +404,7 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
         }
         if ( i == MAX_AUDIO )
         {
-            msg_Err( p_stream, "too many audio streams !" );
+            msg_Err( p_stream, "too many audio streams!" );
             free( id );
             return NULL;
         }
@@ -544,7 +591,7 @@ static int UnpackFromFile( sout_stream_t *p_stream, const char *psz_file,
                            picture_t *p_pic )
 {
     int i, j;
-    FILE *p_file = fopen( psz_file, "r" );
+    FILE *p_file = utf8_fopen( psz_file, "r" );
 
     if ( p_file == NULL )
     {
@@ -572,7 +619,7 @@ static int UnpackFromFile( sout_stream_t *p_stream, const char *psz_file,
         uint8_t *p_u = &p_pic->p[1].p_pixels[i/2 * p_pic->p[1].i_pitch];
         uint8_t *p_v = &p_pic->p[2].p_pixels[i/2 * p_pic->p[2].i_pitch];
 
-        if ( fread( p_buffer, 2, i_width, p_file ) != i_width )
+        if ( fread( p_buffer, 2, i_width, p_file ) != (size_t)i_width )
         {
             msg_Err( p_stream, "premature end of file %s", psz_file );
             fclose( p_file );
@@ -608,12 +655,12 @@ static int UnpackFromFile( sout_stream_t *p_stream, const char *psz_file,
 static void NetCommand( sout_stream_t *p_stream )
 {
     sout_stream_sys_t *p_sys = p_stream->p_sys;
-    char psz_buffer[10];
-    int i_len = net_ReadNonBlock( p_stream, p_sys->i_fd, NULL, psz_buffer,
-                                  sizeof( psz_buffer ), 0 );
+    char psz_buffer[11];
+    int i_len = recv( p_sys->i_fd, psz_buffer, sizeof( psz_buffer ) - 1, 0 );
 
     if ( i_len > 0 )
     {
+        psz_buffer[i_len] = '\0';
         int i_cmd = strtol( psz_buffer, NULL, 0 );
         if ( i_cmd < -1 || i_cmd > p_sys->i_nb_pictures )
         {
@@ -622,6 +669,9 @@ static void NetCommand( sout_stream_t *p_stream )
         }
 
         p_sys->i_cmd = i_cmd;
+
+        msg_Dbg( p_stream, "new command: %d old:%d", p_sys->i_cmd,
+                 p_sys->i_old_cmd );
     }
 }
 
@@ -663,6 +713,8 @@ static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id )
     {
         /* Create a new encoder. */
         int i_ff_codec = CODEC_ID_MPEG2VIDEO;
+        int i_aspect_num, i_aspect_den;
+
         if( i_ff_codec == 0 )
         {
             msg_Err( p_stream, "cannot find encoder" );
@@ -679,20 +731,21 @@ static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id )
         id->ff_enc_c = avcodec_alloc_context();
 
         /* Set CPU capabilities */
+        unsigned i_cpu = vlc_CPU();
         id->ff_enc_c->dsp_mask = 0;
-        if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
+        if( !(i_cpu & CPU_CAPABILITY_MMX) )
         {
             id->ff_enc_c->dsp_mask |= FF_MM_MMX;
         }
-        if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
+        if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
         {
             id->ff_enc_c->dsp_mask |= FF_MM_MMXEXT;
         }
-        if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW) )
+        if( !(i_cpu & CPU_CAPABILITY_3DNOW) )
         {
             id->ff_enc_c->dsp_mask |= FF_MM_3DNOW;
         }
-        if( !(p_stream->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
+        if( !(i_cpu & CPU_CAPABILITY_SSE) )
         {
             id->ff_enc_c->dsp_mask |= FF_MM_SSE;
             id->ff_enc_c->dsp_mask |= FF_MM_SSE2;
@@ -700,9 +753,21 @@ static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id )
 
         id->ff_enc_c->width = p_sys->p_pictures[p_sys->i_cmd-1].format.i_width;
         id->ff_enc_c->height = p_sys->p_pictures[p_sys->i_cmd-1].format.i_height;
-
+        av_reduce( &i_aspect_num, &i_aspect_den,
+                   p_sys->i_aspect,
+                   VOUT_ASPECT_FACTOR, 1 << 30 /* something big */ );
+        av_reduce( &id->ff_enc_c->sample_aspect_ratio.num,
+                   &id->ff_enc_c->sample_aspect_ratio.den,
+                   i_aspect_num * (int64_t)id->ff_enc_c->height,
+                   i_aspect_den * (int64_t)id->ff_enc_c->width, 1 << 30 );
+
+#if LIBAVCODEC_BUILD >= 4754
+        id->ff_enc_c->time_base.num = 1;
+        id->ff_enc_c->time_base.den = 25; /* FIXME */
+#else
         id->ff_enc_c->frame_rate    = 25; /* FIXME */
         id->ff_enc_c->frame_rate_base = 1;
+#endif
 
         id->ff_enc_c->gop_size = 200;
         id->ff_enc_c->max_b_frames = 0;
@@ -712,6 +777,7 @@ static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id )
                             | CODEC_FLAG_LOW_DELAY;
 
         id->ff_enc_c->mb_decision = FF_MB_DECISION_SIMPLE;
+        id->ff_enc_c->pix_fmt = PIX_FMT_YUV420P;
 
         if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
         {
@@ -719,7 +785,7 @@ static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id )
             return 0;
         }
 
-        id->p_buffer_out = malloc( AVCODEC_MAX_VIDEO_FRAME_SIZE );
+        id->p_buffer_out = malloc( id->ff_enc_c->width * id->ff_enc_c->height * 3 );
         id->p_frame = avcodec_alloc_frame();
         id->p_frame->linesize[0] = p_sys->p_pictures[p_sys->i_cmd-1].p[0].i_pitch;
         id->p_frame->linesize[1] = p_sys->p_pictures[p_sys->i_cmd-1].p[1].i_pitch;
@@ -773,7 +839,7 @@ static block_t *VideoGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
     }
 
     i_out = avcodec_encode_video( id->ff_enc_c, id->p_buffer_out,
-                                  AVCODEC_MAX_VIDEO_FRAME_SIZE,
+                                  id->ff_enc_c->width * id->ff_enc_c->height * 3,
                                   id->p_frame );
 
     if ( i_out <= 0 )
@@ -787,7 +853,7 @@ static block_t *VideoGetBuffer( sout_stream_t *p_stream, sout_stream_id_t *id,
         int mb_height = (id->ff_enc_c->height + 15) / 16;
         int h_chroma_shift, v_chroma_shift;
         int i;
-        
+
         avcodec_get_chroma_sub_sample( id->ff_enc_c->pix_fmt, &h_chroma_shift,
                                        &v_chroma_shift );
 
@@ -795,11 +861,9 @@ 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_vlc->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++ )
         {
             int stride = ((16 * mb_width )
@@ -812,27 +876,28 @@ 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_vlc->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_vlc->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_vlc->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;
+    p_out->i_rate = p_buffer->i_rate;
 
     switch ( id->ff_enc_c->coded_frame->pict_type )
     {
@@ -871,13 +936,13 @@ 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_vlc->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;
+    p_out->i_rate = p_buffer->i_rate;
 
     block_Release( p_buffer );
 
     return p_out;
 }
-