]> git.sesse.net Git - vlc/blobdiff - modules/mux/asf.c
Write aspect ratio only when defined (asf).
[vlc] / modules / mux / asf.c
index 10ffa04b179d2853e44db5ea4d016c7497d8147d..600f635aaf6356e7f84e30cb54fdd047269704d1 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * asf.c: asf muxer module for vlc
  *****************************************************************************
- * Copyright (C) 2003-2004 the VideoLAN team
+ * Copyright (C) 2003-2004, 2006 the VideoLAN team
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@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 <stdlib.h>
 
-#include <vlc/vlc.h>
-#include <vlc/input.h>
-#include <vlc/sout.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_sout.h>
+#include <vlc_block.h>
+#include <vlc_codecs.h>
 
-#include "codecs.h"
 typedef GUID guid_t;
 
 #define MAX_ASF_TRACKS 128
@@ -46,52 +50,53 @@ static void Close  ( vlc_object_t * );
 #define SOUT_CFG_PREFIX "sout-asf-"
 
 #define TITLE_TEXT N_("Title")
-#define TITLE_LONGTEXT N_("Allows you to define the title that will be put " \
-                          "in ASF comments.")
+#define TITLE_LONGTEXT N_("Title to put in ASF comments." )
 #define AUTHOR_TEXT N_("Author")
-#define AUTHOR_LONGTEXT N_("Allows you to define the author that will be put "\
-                           "in ASF comments.")
+#define AUTHOR_LONGTEXT N_("Author to put in ASF comments." )
 #define COPYRIGHT_TEXT N_("Copyright")
-#define COPYRIGHT_LONGTEXT N_("Allows you to define the copyright string " \
-                              "that will be put in ASF comments.")
+#define COPYRIGHT_LONGTEXT N_("Copyright string to put in ASF comments." )
 #define COMMENT_TEXT N_("Comment")
-#define COMMENT_LONGTEXT N_("Allows you to define the comment that will be " \
-                            "put in ASF comments.")
+#define COMMENT_LONGTEXT N_("Comment to put in ASF comments." )
 #define RATING_TEXT N_("Rating")
-#define RATING_LONGTEXT N_("Allows you to define the \"rating\" that will " \
-                           "be put in ASF comments.")
+#define RATING_LONGTEXT N_("\"Rating\" to put in ASF comments." )
 #define PACKETSIZE_TEXT N_("Packet Size")
-#define PACKETSIZE_LONGTEXT N_("The ASF packet size -- default is 4096 bytes")
+#define PACKETSIZE_LONGTEXT N_("ASF packet size -- default is 4096 bytes")
+#define BITRATE_TEXT N_("Bitrate override")
+#define BITRATE_LONGTEXT N_("Do not try to guess ASF bitrate. Setting this, allows you to control how Windows Media Player will cache streamed content. Set to audio+video bitrate in bytes")
 
-vlc_module_begin();
-    set_description( _("ASF muxer") );
-    set_category( CAT_SOUT );
-    set_subcategory( SUBCAT_SOUT_MUX );
-    set_shortname( "ASF" );
 
-    set_capability( "sout mux", 5 );
-    add_shortcut( "asf" );
-    add_shortcut( "asfh" );
-    set_callbacks( Open, Close );
+vlc_module_begin ()
+    set_description( N_("ASF muxer") )
+    set_category( CAT_SOUT )
+    set_subcategory( SUBCAT_SOUT_MUX )
+    set_shortname( "ASF" )
+
+    set_capability( "sout mux", 5 )
+    add_shortcut( "asf" )
+    add_shortcut( "asfh" )
+    set_callbacks( Open, Close )
 
     add_string( SOUT_CFG_PREFIX "title", "", NULL, TITLE_TEXT, TITLE_LONGTEXT,
-                                 VLC_TRUE );
+                                 true )
     add_string( SOUT_CFG_PREFIX "author",   "", NULL, AUTHOR_TEXT,
-                                 AUTHOR_LONGTEXT, VLC_TRUE );
+                                 AUTHOR_LONGTEXT, true )
     add_string( SOUT_CFG_PREFIX "copyright","", NULL, COPYRIGHT_TEXT,
-                                 COPYRIGHT_LONGTEXT, VLC_TRUE );
+                                 COPYRIGHT_LONGTEXT, true )
     add_string( SOUT_CFG_PREFIX "comment",  "", NULL, COMMENT_TEXT,
-                                 COMMENT_LONGTEXT, VLC_TRUE );
+                                 COMMENT_LONGTEXT, true )
     add_string( SOUT_CFG_PREFIX "rating",  "", NULL, RATING_TEXT,
-                                 RATING_LONGTEXT, VLC_TRUE );
-    add_integer( "sout-asf-packet-size", 4096, NULL, PACKETSIZE_TEXT, PACKETSIZE_LONGTEXT, VLC_TRUE );
+                                 RATING_LONGTEXT, true )
+    add_integer( SOUT_CFG_PREFIX "packet-size", 4096, NULL, PACKETSIZE_TEXT,
+                                 PACKETSIZE_LONGTEXT, true )
+    add_integer( SOUT_CFG_PREFIX "bitrate-override", 0, NULL, BITRATE_TEXT,
+                                 BITRATE_LONGTEXT, true )
 
-vlc_module_end();
+vlc_module_end ()
 
 /*****************************************************************************
  * Locales prototypes
  *****************************************************************************/
-static const char *ppsz_sout_options[] = {
+static const char *const ppsz_sout_options[] = {
     "title", "author", "copyright", "comment", "rating", NULL
 };
 
@@ -108,9 +113,9 @@ typedef struct
     /* codec information */
     uint16_t     i_tag;     /* for audio */
     vlc_fourcc_t i_fourcc;  /* for video */
-    char         *psz_name; /* codec name */
+    const char         *psz_name; /* codec name */
     int          i_blockalign; /* for audio only */
-    vlc_bool_t   b_audio_correction;
+    bool   b_audio_correction;
 
     int          i_sequence;
 
@@ -130,18 +135,19 @@ struct sout_mux_sys_t
     mtime_t         i_dts_last;
     mtime_t         i_preroll_time;
     int64_t         i_bitrate;
+    int64_t         i_bitrate_override;
 
     int             i_track;
     asf_track_t     track[MAX_ASF_TRACKS];
 
-    vlc_bool_t      b_write_header;
+    bool      b_write_header;
 
     block_t         *pk;
     int             i_pk_used;
     int             i_pk_frame;
     mtime_t         i_pk_dts;
 
-    vlc_bool_t      b_asf_http;
+    bool      b_asf_http;
     int             i_seq;
 
     /* meta data */
@@ -154,7 +160,7 @@ struct sout_mux_sys_t
 
 static int MuxGetStream( sout_mux_t *, int *pi_stream, mtime_t *pi_dts );
 
-static block_t *asf_header_create( sout_mux_t *, vlc_bool_t );
+static block_t *asf_header_create( sout_mux_t *, bool );
 static block_t *asf_packet_create( sout_mux_t *, asf_track_t *, block_t * );
 static block_t *asf_stream_end_create( sout_mux_t *);
 static block_t *asf_packet_flush( sout_mux_t * );
@@ -174,7 +180,7 @@ static void bo_addle_u32( bo_t *, uint32_t );
 static void bo_addle_u64( bo_t *, uint64_t );
 static void bo_add_mem  ( bo_t *, uint8_t *, int );
 
-static void bo_addle_str16( bo_t *, char * );
+static void bo_addle_str16( bo_t *, const char * );
 
 /*****************************************************************************
  * Open:
@@ -186,8 +192,8 @@ static int Open( vlc_object_t *p_this )
     vlc_value_t    val;
     int i;
 
-    msg_Dbg( p_mux, "Asf muxer opened" );
-    sout_CfgParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
+    msg_Dbg( p_mux, "asf muxer opened" );
+    config_ChainParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
 
     p_mux->pf_control   = Control;
     p_mux->pf_addstream = AddStream;
@@ -195,6 +201,8 @@ static int Open( vlc_object_t *p_this )
     p_mux->pf_mux       = Mux;
 
     p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
+    if( !p_sys )
+        return VLC_ENOMEM;
     p_sys->b_asf_http = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "asfh" );
     if( p_sys->b_asf_http )
     {
@@ -207,12 +215,16 @@ static int Open( vlc_object_t *p_this )
     p_sys->i_dts_last   = 0;
     p_sys->i_preroll_time = 2000;
     p_sys->i_bitrate    = 0;
+    p_sys->i_bitrate_override = 0;
     p_sys->i_seq        = 0;
 
-    p_sys->b_write_header = VLC_TRUE;
+    p_sys->b_write_header = true;
     p_sys->i_track = 0;
     p_sys->i_packet_size = config_GetInt( p_mux, "sout-asf-packet-size" );
+    p_sys->i_bitrate_override = config_GetInt( p_mux, "sout-asf-bitrate-override" );
     msg_Dbg( p_mux, "Packet size %d", p_sys->i_packet_size);
+    if (p_sys->i_bitrate_override)
+        msg_Dbg( p_mux, "Bitrate override %"PRId64, p_sys->i_bitrate_override);
     p_sys->i_packet_count= 0;
 
     /* Generate a random fid */
@@ -241,8 +253,8 @@ static int Open( vlc_object_t *p_this )
     var_Get( p_mux, SOUT_CFG_PREFIX "rating", &val );
     p_sys->psz_rating = val.psz_string;
 
-    msg_Dbg( p_mux, "meta data: title='%s' author='%s' copyright='%s' "
-             "comment='%s' rating='%s'",
+    msg_Dbg( p_mux, "meta data: title='%s', author='%s', copyright='%s', "
+             "comment='%s', rating='%s'",
              p_sys->psz_title, p_sys->psz_author, p_sys->psz_copyright,
              p_sys->psz_comment, p_sys->psz_rating );
 
@@ -273,9 +285,9 @@ static void Close( vlc_object_t * p_this )
     }
 
     /* rewrite header */
-    if( !sout_AccessOutSeek( p_mux->p_access, 0 ) )
+    if( sout_AccessOutSeek( p_mux->p_access, 0 ) == VLC_SUCCESS )
     {
-        out = asf_header_create( p_mux, VLC_FALSE );
+        out = asf_header_create( p_mux, false );
         sout_AccessOutWrite( p_mux->p_access, out );
     }
 
@@ -284,6 +296,12 @@ static void Close( vlc_object_t * p_this )
         free( p_sys->track[i].p_extra );
         es_format_Clean( &p_sys->track[i].fmt );
     }
+
+    free( p_sys->psz_title );
+    free( p_sys->psz_author );
+    free( p_sys->psz_copyright );
+    free( p_sys->psz_comment );
+    free( p_sys->psz_rating );
     free( p_sys );
 }
 
@@ -293,20 +311,20 @@ static void Close( vlc_object_t * p_this )
 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
 {
     sout_mux_sys_t *p_sys = p_mux->p_sys;
-    vlc_bool_t *pb_bool;
+    bool *pb_bool;
     char **ppsz;
 
     switch( i_query )
     {
        case MUX_CAN_ADD_STREAM_WHILE_MUXING:
-           pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
-           if( p_sys->b_asf_http ) *pb_bool = VLC_TRUE;
-           else *pb_bool = VLC_FALSE;
+           pb_bool = (bool*)va_arg( args, bool * );
+           if( p_sys->b_asf_http ) *pb_bool = true;
+           else *pb_bool = false;
            return VLC_SUCCESS;
 
        case MUX_GET_ADD_STREAM_WAIT:
-           pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
-           *pb_bool = VLC_TRUE;
+           pb_bool = (bool*)va_arg( args, bool * );
+           *pb_bool = true;
            return VLC_SUCCESS;
 
        case MUX_GET_MIME:
@@ -334,7 +352,7 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
     msg_Dbg( p_mux, "adding input" );
     if( p_sys->i_track >= MAX_ASF_TRACKS )
     {
-        msg_Dbg( p_mux, "cannot add this track (too much track)" );
+        msg_Dbg( p_mux, "cannot add this track (too much tracks)" );
         return VLC_EGENERIC;
     }
 
@@ -359,6 +377,11 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
                     tk->psz_name = "A/52";
                     i_bitspersample = 0;
                     break;
+                case VLC_FOURCC( 'm', 'p', '4', 'a' ):
+                    tk->i_tag = WAVE_FORMAT_AAC;
+                    tk->psz_name = "MPEG-4 Audio";
+                    i_bitspersample = 0;
+                    break;
                 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
 #if 1
                     tk->psz_name = "MPEG Audio Layer 3";
@@ -378,23 +401,23 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
                 case VLC_FOURCC( 'w', 'm', 'a', '1' ):
                     tk->psz_name = "Windows Media Audio v1";
                     tk->i_tag = WAVE_FORMAT_WMA1;
-                    tk->b_audio_correction = VLC_TRUE;
+                    tk->b_audio_correction = true;
                     break;
                 case VLC_FOURCC( 'w', 'm', 'a', ' ' ):
                 case VLC_FOURCC( 'w', 'm', 'a', '2' ):
                     tk->psz_name= "Windows Media Audio (v2) 7, 8 and 9 Series";
                     tk->i_tag = WAVE_FORMAT_WMA2;
-                    tk->b_audio_correction = VLC_TRUE;
+                    tk->b_audio_correction = true;
                     break;
                 case VLC_FOURCC( 'w', 'm', 'a', 'p' ):
                     tk->psz_name = "Windows Media Audio 9 Professional";
                     tk->i_tag = WAVE_FORMAT_WMAP;
-                    tk->b_audio_correction = VLC_TRUE;
+                    tk->b_audio_correction = true;
                     break;
                 case VLC_FOURCC( 'w', 'm', 'a', 'l' ):
                     tk->psz_name = "Windows Media Audio 9 Lossless";
                     tk->i_tag = WAVE_FORMAT_WMAL;
-                    tk->b_audio_correction = VLC_TRUE;
+                    tk->b_audio_correction = true;
                     break;
                     /* raw codec */
                 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
@@ -428,6 +451,8 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
             tk->i_extra = sizeof( WAVEFORMATEX ) +
                           p_input->p_fmt->i_extra + i_extra;
             tk->p_extra = malloc( tk->i_extra );
+            if( !tk->p_extra )
+                return VLC_ENOMEM;
             bo_init( &bo, tk->p_extra, tk->i_extra );
             bo_addle_u16( &bo, tk->i_tag );
             bo_addle_u16( &bo, p_input->p_fmt->audio.i_channels );
@@ -474,8 +499,10 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
             }
             else
             {
-                p_sys->i_bitrate += 512000;
+                p_sys->i_bitrate += 128000;
             }
+            if (p_sys->i_bitrate_override)
+                p_sys->i_bitrate = p_sys->i_bitrate_override;
             break;
         }
         case VIDEO_ES:
@@ -483,6 +510,8 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
             tk->i_extra = 11 + sizeof( BITMAPINFOHEADER ) +
                           p_input->p_fmt->i_extra;
             tk->p_extra = malloc( tk->i_extra );
+            if( !tk->p_extra )
+                return VLC_ENOMEM;
             bo_init( &bo, tk->p_extra, tk->i_extra );
             bo_addle_u32( &bo, p_input->p_fmt->video.i_width );
             bo_addle_u32( &bo, p_input->p_fmt->video.i_height );
@@ -558,8 +587,10 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
             }
             else
             {
-                p_sys->i_bitrate += 1000000;
+                p_sys->i_bitrate += 512000;
             }
+            if (p_sys->i_bitrate_override)
+                p_sys->i_bitrate = p_sys->i_bitrate_override;
             break;
         }
         default:
@@ -578,6 +609,27 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
  *****************************************************************************/
 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
 {
+    /* if bitrate ain't defined in commanline, reduce it when tracks are deleted
+     */
+    sout_mux_sys_t   *p_sys = p_mux->p_sys;
+    asf_track_t      *tk = p_input->p_sys;
+    if(!p_sys->i_bitrate_override)
+    {
+        if( tk->i_cat == AUDIO_ES )
+        {
+             if( p_input->p_fmt->i_bitrate > 24000 )
+                 p_sys->i_bitrate -= p_input->p_fmt->i_bitrate;
+             else
+                 p_sys->i_bitrate -= 128000;
+        }
+        else if(tk->i_cat == VIDEO_ES )
+        {
+             if( p_input->p_fmt->i_bitrate > 50000 )
+                 p_sys->i_bitrate -= p_input->p_fmt->i_bitrate;
+             else
+                 p_sys->i_bitrate -= 512000;
+        }
+    }
     msg_Dbg( p_mux, "removing input" );
     return VLC_SUCCESS;
 }
@@ -591,12 +643,12 @@ static int Mux( sout_mux_t *p_mux )
 
     if( p_sys->b_write_header )
     {
-        block_t *out = asf_header_create( p_mux, VLC_TRUE );
+        block_t *out = asf_header_create( p_mux, true );
 
         out->i_flags |= BLOCK_FLAG_HEADER;
         sout_AccessOutWrite( p_mux->p_access, out );
 
-        p_sys->b_write_header = VLC_FALSE;
+        p_sys->b_write_header = false;
     }
 
     for( ;; )
@@ -648,7 +700,7 @@ static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
         sout_input_t  *p_input = p_mux->pp_inputs[i];
         block_t *p_data;
 
-        if( p_input->p_fifo->i_depth <= 0 )
+        if( block_FifoCount( p_input->p_fifo ) <= 0 )
         {
             if( p_input->p_fmt->i_cat == AUDIO_ES ||
                 p_input->p_fmt->i_cat == VIDEO_ES )
@@ -722,7 +774,7 @@ static void bo_add_mem( bo_t *p_bo, uint8_t *p_mem, int i_size )
     p_bo->i_buffer += i_size;
 }
 
-static void bo_addle_str16( bo_t *bo, char *str )
+static void bo_addle_str16( bo_t *bo, const char *str )
 {
     bo_addle_u16( bo, strlen( str ) + 1 );
     for( ;; )
@@ -733,7 +785,7 @@ static void bo_addle_str16( bo_t *bo, char *str )
     }
 }
 
-static void bo_addle_str16_nosize( bo_t *bo, char *str )
+static void bo_addle_str16_nosize( bo_t *bo, const char *str )
 {
     for( ;; )
     {
@@ -766,7 +818,7 @@ static const guid_t asf_object_file_properties_guid =
 {0x8cabdca1, 0xa947, 0x11cf, {0x8e, 0xe4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
 static const guid_t asf_object_stream_properties_guid =
 {0xB7DC0791, 0xA9B7, 0x11CF, {0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
-static const guid_t asf_object_header_extention_guid =
+static const guid_t asf_object_header_extension_guid =
 {0x5FBF03B5, 0xA92E, 0x11CF, {0x8E, 0xE3, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
 static const guid_t asf_object_stream_type_audio =
 {0xF8699E40, 0x5B4D, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
@@ -804,7 +856,7 @@ static void asf_chunk_add( bo_t *bo,
     bo_addle_u16( bo, i_len + 8 );
 }
 
-static block_t *asf_header_create( sout_mux_t *p_mux, vlc_bool_t b_broadcast )
+static block_t *asf_header_create( sout_mux_t *p_mux, bool b_broadcast )
 {
     sout_mux_sys_t *p_sys = p_mux->p_sys;
     asf_track_t    *tk;
@@ -850,7 +902,8 @@ static block_t *asf_header_create( sout_mux_t *p_mux, vlc_bool_t b_broadcast )
     /* size of the metadata object */
     for( i = 0; i < p_sys->i_track; i++ )
     {
-        if( p_sys->track[i].i_cat == VIDEO_ES )
+        const asf_track_t *p_track = &p_sys->track[i];
+        if( p_track->i_cat == VIDEO_ES && p_track->fmt.video.i_aspect != 0 )
         {
             i_cm_size = 26 + 2 * (16 + 2 * sizeof("AspectRatio?"));
             break;
@@ -898,10 +951,10 @@ static block_t *asf_header_create( sout_mux_t *p_mux, vlc_bool_t b_broadcast )
     bo_addle_u32( &bo, p_sys->i_packet_size );  /* packet size max */
     bo_addle_u32( &bo, p_sys->i_bitrate );      /* maxbitrate */
 
-    /* header extention */
+    /* header extension */
     if( i_header_ext_size )
     {
-        bo_add_guid ( &bo, &asf_object_header_extention_guid );
+        bo_add_guid ( &bo, &asf_object_header_extension_guid );
         bo_addle_u64( &bo, i_header_ext_size );
         bo_add_guid ( &bo, &asf_guid_reserved_1 );
         bo_addle_u16( &bo, 6 );
@@ -912,7 +965,7 @@ static block_t *asf_header_create( sout_mux_t *p_mux, vlc_bool_t b_broadcast )
     if( i_cm_size )
     {
         int64_t i_num, i_den;
-        int i_dst_num, i_dst_den;
+        unsigned int i_dst_num, i_dst_den;
 
         for( i = 0; i < p_sys->i_track; i++ )
             if( p_sys->track[i].i_cat == VIDEO_ES ) break;