Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

asf.c

00001 /*****************************************************************************
00002  * asf.c: asf muxer module for vlc
00003  *****************************************************************************
00004  * Copyright (C) 2003-2004 the VideoLAN team
00005  * $Id: asf.c 12696 2005-09-28 18:49:52Z dionoea $
00006  *
00007  * Authors: Laurent Aimar <[email protected]>
00008  *          Gildas Bazin <[email protected]>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00023  *****************************************************************************/
00024 
00025 /*****************************************************************************
00026  * Preamble
00027  *****************************************************************************/
00028 #include <stdlib.h>
00029 
00030 #include <vlc/vlc.h>
00031 #include <vlc/input.h>
00032 #include <vlc/sout.h>
00033 
00034 #include "codecs.h"
00035 typedef GUID guid_t;
00036 
00037 #define MAX_ASF_TRACKS 128
00038 #define ASF_DATA_PACKET_SIZE 4096  // deprecated -- added sout-asf-packet-size
00039 
00040 /*****************************************************************************
00041  * Module descriptor
00042  *****************************************************************************/
00043 static int  Open   ( vlc_object_t * );
00044 static void Close  ( vlc_object_t * );
00045 
00046 #define SOUT_CFG_PREFIX "sout-asf-"
00047 
00048 #define TITLE_TEXT N_("Title")
00049 #define TITLE_LONGTEXT N_("Allows you to define the title that will be put " \
00050                           "in ASF comments.")
00051 #define AUTHOR_TEXT N_("Author")
00052 #define AUTHOR_LONGTEXT N_("Allows you to define the author that will be put "\
00053                            "in ASF comments.")
00054 #define COPYRIGHT_TEXT N_("Copyright")
00055 #define COPYRIGHT_LONGTEXT N_("Allows you to define the copyright string " \
00056                               "that will be put in ASF comments.")
00057 #define COMMENT_TEXT N_("Comment")
00058 #define COMMENT_LONGTEXT N_("Allows you to define the comment that will be " \
00059                             "put in ASF comments.")
00060 #define RATING_TEXT N_("Rating")
00061 #define RATING_LONGTEXT N_("Allows you to define the \"rating\" that will " \
00062                            "be put in ASF comments.")
00063 #define PACKETSIZE_TEXT N_("Packet Size")
00064 #define PACKETSIZE_LONGTEXT N_("The ASF packet size -- default is 4096 bytes")
00065 
00066 vlc_module_begin();
00067     set_description( _("ASF muxer") );
00068     set_category( CAT_SOUT );
00069     set_subcategory( SUBCAT_SOUT_MUX );
00070     set_shortname( "ASF" );
00071 
00072     set_capability( "sout mux", 5 );
00073     add_shortcut( "asf" );
00074     add_shortcut( "asfh" );
00075     set_callbacks( Open, Close );
00076 
00077     add_string( SOUT_CFG_PREFIX "title", "", NULL, TITLE_TEXT, TITLE_LONGTEXT,
00078                                  VLC_TRUE );
00079     add_string( SOUT_CFG_PREFIX "author",   "", NULL, AUTHOR_TEXT,
00080                                  AUTHOR_LONGTEXT, VLC_TRUE );
00081     add_string( SOUT_CFG_PREFIX "copyright","", NULL, COPYRIGHT_TEXT,
00082                                  COPYRIGHT_LONGTEXT, VLC_TRUE );
00083     add_string( SOUT_CFG_PREFIX "comment",  "", NULL, COMMENT_TEXT,
00084                                  COMMENT_LONGTEXT, VLC_TRUE );
00085     add_string( SOUT_CFG_PREFIX "rating",  "", NULL, RATING_TEXT,
00086                                  RATING_LONGTEXT, VLC_TRUE );
00087     add_integer( "sout-asf-packet-size", 4096, NULL, PACKETSIZE_TEXT, PACKETSIZE_LONGTEXT, VLC_TRUE );
00088 
00089 vlc_module_end();
00090 
00091 /*****************************************************************************
00092  * Locales prototypes
00093  *****************************************************************************/
00094 static const char *ppsz_sout_options[] = {
00095     "title", "author", "copyright", "comment", "rating", NULL
00096 };
00097 
00098 static int Control  ( sout_mux_t *, int, va_list );
00099 static int AddStream( sout_mux_t *, sout_input_t * );
00100 static int DelStream( sout_mux_t *, sout_input_t * );
00101 static int Mux      ( sout_mux_t * );
00102 
00103 typedef struct
00104 {
00105     int          i_id;
00106     int          i_cat;
00107 
00108     /* codec information */
00109     uint16_t     i_tag;     /* for audio */
00110     vlc_fourcc_t i_fourcc;  /* for video */
00111     char         *psz_name; /* codec name */
00112     int          i_blockalign; /* for audio only */
00113     vlc_bool_t   b_audio_correction;
00114 
00115     int          i_sequence;
00116 
00117     int          i_extra;
00118     uint8_t      *p_extra;
00119 
00120     es_format_t  fmt;
00121 
00122 } asf_track_t;
00123 
00124 struct sout_mux_sys_t
00125 {
00126     guid_t          fid;    /* file id */
00127     int             i_packet_size;
00128     int64_t         i_packet_count;
00129     mtime_t         i_dts_first;
00130     mtime_t         i_dts_last;
00131     mtime_t         i_preroll_time;
00132     int64_t         i_bitrate;
00133 
00134     int             i_track;
00135     asf_track_t     track[MAX_ASF_TRACKS];
00136 
00137     vlc_bool_t      b_write_header;
00138 
00139     block_t         *pk;
00140     int             i_pk_used;
00141     int             i_pk_frame;
00142     mtime_t         i_pk_dts;
00143 
00144     vlc_bool_t      b_asf_http;
00145     int             i_seq;
00146 
00147     /* meta data */
00148     char            *psz_title;
00149     char            *psz_author;
00150     char            *psz_copyright;
00151     char            *psz_comment;
00152     char            *psz_rating;
00153 };
00154 
00155 static int MuxGetStream( sout_mux_t *, int *pi_stream, mtime_t *pi_dts );
00156 
00157 static block_t *asf_header_create( sout_mux_t *, vlc_bool_t );
00158 static block_t *asf_packet_create( sout_mux_t *, asf_track_t *, block_t * );
00159 static block_t *asf_stream_end_create( sout_mux_t *);
00160 static block_t *asf_packet_flush( sout_mux_t * );
00161 
00162 typedef struct
00163 {
00164     int      i_buffer_size;
00165     int      i_buffer;
00166     uint8_t  *p_buffer;
00167 
00168 } bo_t;
00169 
00170 static void bo_init     ( bo_t *, uint8_t *, int  );
00171 static void bo_add_u8   ( bo_t *, uint8_t  );
00172 static void bo_addle_u16( bo_t *, uint16_t );
00173 static void bo_addle_u32( bo_t *, uint32_t );
00174 static void bo_addle_u64( bo_t *, uint64_t );
00175 static void bo_add_mem  ( bo_t *, uint8_t *, int );
00176 
00177 static void bo_addle_str16( bo_t *, char * );
00178 
00179 /*****************************************************************************
00180  * Open:
00181  *****************************************************************************/
00182 static int Open( vlc_object_t *p_this )
00183 {
00184     sout_mux_t     *p_mux = (sout_mux_t*)p_this;
00185     sout_mux_sys_t *p_sys;
00186     vlc_value_t    val;
00187     int i;
00188 
00189     msg_Dbg( p_mux, "Asf muxer opened" );
00190     sout_CfgParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
00191 
00192     p_mux->pf_control   = Control;
00193     p_mux->pf_addstream = AddStream;
00194     p_mux->pf_delstream = DelStream;
00195     p_mux->pf_mux       = Mux;
00196 
00197     p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
00198     p_sys->b_asf_http = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "asfh" );
00199     if( p_sys->b_asf_http )
00200     {
00201         msg_Dbg( p_mux, "creating asf stream to be used with mmsh" );
00202     }
00203     p_sys->pk = NULL;
00204     p_sys->i_pk_used    = 0;
00205     p_sys->i_pk_frame   = 0;
00206     p_sys->i_dts_first  = -1;
00207     p_sys->i_dts_last   = 0;
00208     p_sys->i_preroll_time = 2000;
00209     p_sys->i_bitrate    = 0;
00210     p_sys->i_seq        = 0;
00211 
00212     p_sys->b_write_header = VLC_TRUE;
00213     p_sys->i_track = 0;
00214     p_sys->i_packet_size = config_GetInt( p_mux, "sout-asf-packet-size" );
00215     msg_Dbg( p_mux, "Packet size %d", p_sys->i_packet_size);
00216     p_sys->i_packet_count= 0;
00217 
00218     /* Generate a random fid */
00219     srand( mdate() & 0xffffffff );
00220     p_sys->fid.Data1 = 0xbabac001;
00221     p_sys->fid.Data2 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
00222     p_sys->fid.Data3 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
00223     for( i = 0; i < 8; i++ )
00224     {
00225         p_sys->fid.Data4[i] = ( (uint64_t)rand() << 8 ) / RAND_MAX;
00226     }
00227 
00228     /* Meta data */
00229     var_Get( p_mux, SOUT_CFG_PREFIX "title", &val );
00230     p_sys->psz_title = val.psz_string;
00231 
00232     var_Get( p_mux, SOUT_CFG_PREFIX "author", &val );
00233     p_sys->psz_author = val.psz_string;
00234 
00235     var_Get( p_mux, SOUT_CFG_PREFIX "copyright", &val );
00236     p_sys->psz_copyright = val.psz_string;
00237 
00238     var_Get( p_mux, SOUT_CFG_PREFIX "comment", &val );
00239     p_sys->psz_comment = val.psz_string;
00240 
00241     var_Get( p_mux, SOUT_CFG_PREFIX "rating", &val );
00242     p_sys->psz_rating = val.psz_string;
00243 
00244     msg_Dbg( p_mux, "meta data: title='%s' author='%s' copyright='%s' "
00245              "comment='%s' rating='%s'",
00246              p_sys->psz_title, p_sys->psz_author, p_sys->psz_copyright,
00247              p_sys->psz_comment, p_sys->psz_rating );
00248 
00249     return VLC_SUCCESS;
00250 }
00251 
00252 /*****************************************************************************
00253  * Close:
00254  *****************************************************************************/
00255 static void Close( vlc_object_t * p_this )
00256 {
00257     sout_mux_t     *p_mux = (sout_mux_t*)p_this;
00258     sout_mux_sys_t *p_sys = p_mux->p_sys;
00259     block_t  *out;
00260     int i;
00261 
00262     msg_Dbg( p_mux, "Asf muxer closed" );
00263 
00264     /* Flush last packet if any */
00265     if( (out = asf_packet_flush( p_mux ) ) )
00266     {
00267         sout_AccessOutWrite( p_mux->p_access, out );
00268     }
00269 
00270     if( ( out = asf_stream_end_create( p_mux ) ) )
00271     {
00272         sout_AccessOutWrite( p_mux->p_access, out );
00273     }
00274 
00275     /* rewrite header */
00276     if( !sout_AccessOutSeek( p_mux->p_access, 0 ) )
00277     {
00278         out = asf_header_create( p_mux, VLC_FALSE );
00279         sout_AccessOutWrite( p_mux->p_access, out );
00280     }
00281 
00282     for( i = 0; i < p_sys->i_track; i++ )
00283     {
00284         free( p_sys->track[i].p_extra );
00285         es_format_Clean( &p_sys->track[i].fmt );
00286     }
00287     free( p_sys );
00288 }
00289 
00290 /*****************************************************************************
00291  * Capability:
00292  *****************************************************************************/
00293 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
00294 {
00295     sout_mux_sys_t *p_sys = p_mux->p_sys;
00296     vlc_bool_t *pb_bool;
00297     char **ppsz;
00298 
00299     switch( i_query )
00300     {
00301        case MUX_CAN_ADD_STREAM_WHILE_MUXING:
00302            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
00303            if( p_sys->b_asf_http ) *pb_bool = VLC_TRUE;
00304            else *pb_bool = VLC_FALSE;
00305            return VLC_SUCCESS;
00306 
00307        case MUX_GET_ADD_STREAM_WAIT:
00308            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
00309            *pb_bool = VLC_TRUE;
00310            return VLC_SUCCESS;
00311 
00312        case MUX_GET_MIME:
00313            ppsz = (char**)va_arg( args, char ** );
00314            if( p_sys->b_asf_http )
00315                *ppsz = strdup( "video/x-ms-asf-stream" );
00316            else
00317                *ppsz = strdup( "video/x-ms-asf" );
00318            return VLC_SUCCESS;
00319 
00320         default:
00321             return VLC_EGENERIC;
00322     }
00323 }
00324 
00325 /*****************************************************************************
00326  * AddStream:
00327  *****************************************************************************/
00328 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
00329 {
00330     sout_mux_sys_t   *p_sys = p_mux->p_sys;
00331     asf_track_t      *tk;
00332     bo_t             bo;
00333 
00334     msg_Dbg( p_mux, "adding input" );
00335     if( p_sys->i_track >= MAX_ASF_TRACKS )
00336     {
00337         msg_Dbg( p_mux, "cannot add this track (too much track)" );
00338         return VLC_EGENERIC;
00339     }
00340 
00341     tk = p_input->p_sys = &p_sys->track[p_sys->i_track];
00342     tk->i_id  = p_sys->i_track + 1;
00343     tk->i_cat = p_input->p_fmt->i_cat;
00344     tk->i_sequence = 0;
00345     tk->b_audio_correction = 0;
00346 
00347     switch( tk->i_cat )
00348     {
00349         case AUDIO_ES:
00350         {
00351             int i_blockalign = p_input->p_fmt->audio.i_blockalign;
00352             int i_bitspersample = p_input->p_fmt->audio.i_bitspersample;
00353             int i_extra = 0;
00354 
00355             switch( p_input->p_fmt->i_codec )
00356             {
00357                 case VLC_FOURCC( 'a', '5', '2', ' ' ):
00358                     tk->i_tag = WAVE_FORMAT_A52;
00359                     tk->psz_name = "A/52";
00360                     i_bitspersample = 0;
00361                     break;
00362                 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
00363 #if 1
00364                     tk->psz_name = "MPEG Audio Layer 3";
00365                     tk->i_tag = WAVE_FORMAT_MPEGLAYER3;
00366                     i_bitspersample = 0;
00367                     i_blockalign = 1;
00368                     i_extra = 12;
00369                     break;
00370 #else
00371                     tk->psz_name = "MPEG Audio Layer 1/2";
00372                     tk->i_tag = WAVE_FORMAT_MPEG;
00373                     i_bitspersample = 0;
00374                     i_blockalign = 1;
00375                     i_extra = 22;
00376                     break;
00377 #endif
00378                 case VLC_FOURCC( 'w', 'm', 'a', '1' ):
00379                     tk->psz_name = "Windows Media Audio v1";
00380                     tk->i_tag = WAVE_FORMAT_WMA1;
00381                     tk->b_audio_correction = VLC_TRUE;
00382                     break;
00383                 case VLC_FOURCC( 'w', 'm', 'a', ' ' ):
00384                 case VLC_FOURCC( 'w', 'm', 'a', '2' ):
00385                     tk->psz_name= "Windows Media Audio (v2) 7, 8 and 9 Series";
00386                     tk->i_tag = WAVE_FORMAT_WMA2;
00387                     tk->b_audio_correction = VLC_TRUE;
00388                     break;
00389                 case VLC_FOURCC( 'w', 'm', 'a', 'p' ):
00390                     tk->psz_name = "Windows Media Audio 9 Professional";
00391                     tk->i_tag = WAVE_FORMAT_WMAP;
00392                     tk->b_audio_correction = VLC_TRUE;
00393                     break;
00394                 case VLC_FOURCC( 'w', 'm', 'a', 'l' ):
00395                     tk->psz_name = "Windows Media Audio 9 Lossless";
00396                     tk->i_tag = WAVE_FORMAT_WMAL;
00397                     tk->b_audio_correction = VLC_TRUE;
00398                     break;
00399                     /* raw codec */
00400                 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
00401                     tk->psz_name = "Raw audio 8bits";
00402                     tk->i_tag = WAVE_FORMAT_PCM;
00403                     i_blockalign= p_input->p_fmt->audio.i_channels;
00404                     i_bitspersample = 8;
00405                     break;
00406                 case VLC_FOURCC( 's', '1', '6', 'l' ):
00407                     tk->psz_name = "Raw audio 16bits";
00408                     tk->i_tag = WAVE_FORMAT_PCM;
00409                     i_blockalign= 2 * p_input->p_fmt->audio.i_channels;
00410                     i_bitspersample = 16;
00411                     break;
00412                 case VLC_FOURCC( 's', '2', '4', 'l' ):
00413                     tk->psz_name = "Raw audio 24bits";
00414                     tk->i_tag = WAVE_FORMAT_PCM;
00415                     i_blockalign= 3 * p_input->p_fmt->audio.i_channels;
00416                     i_bitspersample = 24;
00417                     break;
00418                 case VLC_FOURCC( 's', '3', '2', 'l' ):
00419                     tk->psz_name = "Raw audio 32bits";
00420                     tk->i_tag = WAVE_FORMAT_PCM;
00421                     i_blockalign= 4 * p_input->p_fmt->audio.i_channels;
00422                     i_bitspersample = 32;
00423                     break;
00424                 default:
00425                     return VLC_EGENERIC;
00426             }
00427 
00428             tk->i_extra = sizeof( WAVEFORMATEX ) +
00429                           p_input->p_fmt->i_extra + i_extra;
00430             tk->p_extra = malloc( tk->i_extra );
00431             bo_init( &bo, tk->p_extra, tk->i_extra );
00432             bo_addle_u16( &bo, tk->i_tag );
00433             bo_addle_u16( &bo, p_input->p_fmt->audio.i_channels );
00434             bo_addle_u32( &bo, p_input->p_fmt->audio.i_rate );
00435             bo_addle_u32( &bo, p_input->p_fmt->i_bitrate / 8 );
00436             bo_addle_u16( &bo, i_blockalign );
00437             tk->i_blockalign = i_blockalign;
00438             bo_addle_u16( &bo, i_bitspersample );
00439             if( p_input->p_fmt->i_extra > 0 )
00440             {
00441                 bo_addle_u16( &bo, p_input->p_fmt->i_extra );
00442                 bo_add_mem  ( &bo, p_input->p_fmt->p_extra,
00443                               p_input->p_fmt->i_extra );
00444             }
00445             else
00446             {
00447                 bo_addle_u16( &bo, i_extra );
00448                 if( tk->i_tag == WAVE_FORMAT_MPEGLAYER3 )
00449                 {
00450                     msg_Dbg( p_mux, "adding mp3 header" );
00451                     bo_addle_u16( &bo, 1 );     /* wId */
00452                     bo_addle_u32( &bo, 2 );     /* fdwFlags */
00453                     bo_addle_u16( &bo, 1152 );  /* nBlockSize */
00454                     bo_addle_u16( &bo, 1 );     /* nFramesPerBlock */
00455                     bo_addle_u16( &bo, 1393 );  /* nCodecDelay */
00456                 }
00457                 else if( tk->i_tag == WAVE_FORMAT_MPEG )
00458                 {
00459                     msg_Dbg( p_mux, "adding mp2 header" );
00460                     bo_addle_u16( &bo, 2 );     /* fwHeadLayer */
00461                     bo_addle_u32( &bo, p_input->p_fmt->i_bitrate );
00462                     bo_addle_u16( &bo, p_input->p_fmt->audio.i_channels == 2 ?1:8 );
00463                     bo_addle_u16( &bo, 0 );     /* fwHeadModeExt */
00464                     bo_addle_u16( &bo, 1 );     /* wHeadEmphasis */
00465                     bo_addle_u16( &bo, 16 );    /* fwHeadFlags */
00466                     bo_addle_u32( &bo, 0 );     /* dwPTSLow */
00467                     bo_addle_u32( &bo, 0 );     /* dwPTSHigh */
00468                 }
00469             }
00470 
00471             if( p_input->p_fmt->i_bitrate > 24000 )
00472             {
00473                 p_sys->i_bitrate += p_input->p_fmt->i_bitrate;
00474             }
00475             else
00476             {
00477                 p_sys->i_bitrate += 512000;
00478             }
00479             break;
00480         }
00481         case VIDEO_ES:
00482         {
00483             tk->i_extra = 11 + sizeof( BITMAPINFOHEADER ) +
00484                           p_input->p_fmt->i_extra;
00485             tk->p_extra = malloc( tk->i_extra );
00486             bo_init( &bo, tk->p_extra, tk->i_extra );
00487             bo_addle_u32( &bo, p_input->p_fmt->video.i_width );
00488             bo_addle_u32( &bo, p_input->p_fmt->video.i_height );
00489             bo_add_u8   ( &bo, 0x02 );  /* flags */
00490             bo_addle_u16( &bo, sizeof( BITMAPINFOHEADER ) +
00491                                p_input->p_fmt->i_extra );
00492             bo_addle_u32( &bo, sizeof( BITMAPINFOHEADER ) +
00493                                p_input->p_fmt->i_extra );
00494             bo_addle_u32( &bo, p_input->p_fmt->video.i_width );
00495             bo_addle_u32( &bo, p_input->p_fmt->video.i_height );
00496             bo_addle_u16( &bo, 1 );
00497             bo_addle_u16( &bo, 24 );
00498             if( p_input->p_fmt->i_codec == VLC_FOURCC('m','p','4','v') )
00499             {
00500                 tk->psz_name = "MPEG-4 Video";
00501                 tk->i_fourcc = VLC_FOURCC( 'M', 'P', '4', 'S' );
00502             }
00503             else if( p_input->p_fmt->i_codec == VLC_FOURCC('D','I','V','3') )
00504             {
00505                 tk->psz_name = "MSMPEG-4 V3 Video";
00506                 tk->i_fourcc = VLC_FOURCC( 'M', 'P', '4', '3' );
00507             }
00508             else if( p_input->p_fmt->i_codec == VLC_FOURCC('D','I','V','2') )
00509             {
00510                 tk->psz_name = "MSMPEG-4 V2 Video";
00511                 tk->i_fourcc = VLC_FOURCC( 'M', 'P', '4', '2' );
00512             }
00513             else if( p_input->p_fmt->i_codec == VLC_FOURCC('D','I','V','1') )
00514             {
00515                 tk->psz_name = "MSMPEG-4 V1 Video";
00516                 tk->i_fourcc = VLC_FOURCC( 'M', 'P', 'G', '4' );
00517             }
00518             else if( p_input->p_fmt->i_codec == VLC_FOURCC('W','M','V','1') )
00519             {
00520                 tk->psz_name = "Windows Media Video 7";
00521                 tk->i_fourcc = VLC_FOURCC( 'W', 'M', 'V', '1' );
00522             }
00523             else if( p_input->p_fmt->i_codec == VLC_FOURCC('W','M','V','2') )
00524             {
00525                 tk->psz_name = "Windows Media Video 8";
00526                 tk->i_fourcc = VLC_FOURCC( 'W', 'M', 'V', '2' );
00527             }
00528             else if( p_input->p_fmt->i_codec == VLC_FOURCC('W','M','V','3') )
00529             {
00530                 tk->psz_name = "Windows Media Video 9";
00531                 tk->i_fourcc = VLC_FOURCC( 'W', 'M', 'V', '3' );
00532             }
00533             else if( p_input->p_fmt->i_codec == VLC_FOURCC('h','2','6','4') )
00534             {
00535                 tk->psz_name = "H.264/MPEG-4 AVC";
00536                 tk->i_fourcc = VLC_FOURCC('h','2','6','4');
00537             }
00538             else
00539             {
00540                 tk->psz_name = _("Unknown Video");
00541                 tk->i_fourcc = p_input->p_fmt->i_codec;
00542             }
00543             bo_add_mem( &bo, (uint8_t*)&tk->i_fourcc, 4 );
00544             bo_addle_u32( &bo, 0 );
00545             bo_addle_u32( &bo, 0 );
00546             bo_addle_u32( &bo, 0 );
00547             bo_addle_u32( &bo, 0 );
00548             bo_addle_u32( &bo, 0 );
00549             if( p_input->p_fmt->i_extra > 0 )
00550             {
00551                 bo_add_mem  ( &bo, p_input->p_fmt->p_extra,
00552                               p_input->p_fmt->i_extra );
00553             }
00554 
00555             if( p_input->p_fmt->i_bitrate > 50000 )
00556             {
00557                 p_sys->i_bitrate += p_input->p_fmt->i_bitrate;
00558             }
00559             else
00560             {
00561                 p_sys->i_bitrate += 1000000;
00562             }
00563             break;
00564         }
00565         default:
00566             msg_Err(p_mux, "unhandled track type" );
00567             return VLC_EGENERIC;
00568     }
00569 
00570     es_format_Copy( &tk->fmt, p_input->p_fmt );
00571 
00572     p_sys->i_track++;
00573     return VLC_SUCCESS;
00574 }
00575 
00576 /*****************************************************************************
00577  * DelStream:
00578  *****************************************************************************/
00579 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
00580 {
00581     msg_Dbg( p_mux, "removing input" );
00582     return VLC_SUCCESS;
00583 }
00584 
00585 /*****************************************************************************
00586  * Mux:
00587  *****************************************************************************/
00588 static int Mux( sout_mux_t *p_mux )
00589 {
00590     sout_mux_sys_t *p_sys = p_mux->p_sys;
00591 
00592     if( p_sys->b_write_header )
00593     {
00594         block_t *out = asf_header_create( p_mux, VLC_TRUE );
00595 
00596         out->i_flags |= BLOCK_FLAG_HEADER;
00597         sout_AccessOutWrite( p_mux->p_access, out );
00598 
00599         p_sys->b_write_header = VLC_FALSE;
00600     }
00601 
00602     for( ;; )
00603     {
00604         sout_input_t  *p_input;
00605         asf_track_t   *tk;
00606         int           i_stream;
00607         mtime_t       i_dts;
00608         block_t *data;
00609         block_t *pk;
00610 
00611         if( MuxGetStream( p_mux, &i_stream, &i_dts ) )
00612         {
00613             /* not enough data */
00614             return VLC_SUCCESS;
00615         }
00616 
00617         if( p_sys->i_dts_first < 0 )
00618         {
00619             p_sys->i_dts_first = i_dts;
00620         }
00621         if( p_sys->i_dts_last < i_dts )
00622         {
00623             p_sys->i_dts_last = i_dts;
00624         }
00625 
00626         p_input = p_mux->pp_inputs[i_stream];
00627         tk      = (asf_track_t*)p_input->p_sys;
00628 
00629         data = block_FifoGet( p_input->p_fifo );
00630 
00631         if( ( pk = asf_packet_create( p_mux, tk, data ) ) )
00632         {
00633             sout_AccessOutWrite( p_mux->p_access, pk );
00634         }
00635     }
00636 
00637     return VLC_SUCCESS;
00638 }
00639 
00640 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
00641 {
00642     mtime_t i_dts;
00643     int     i_stream;
00644     int     i;
00645 
00646     for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
00647     {
00648         sout_input_t  *p_input = p_mux->pp_inputs[i];
00649         block_t *p_data;
00650 
00651         if( p_input->p_fifo->i_depth <= 0 )
00652         {
00653             if( p_input->p_fmt->i_cat == AUDIO_ES ||
00654                 p_input->p_fmt->i_cat == VIDEO_ES )
00655             {
00656                 /* We need that audio+video fifo contain at least 1 packet */
00657                 return VLC_EGENERIC;
00658             }
00659             /* SPU */
00660             continue;
00661         }
00662 
00663         p_data = block_FifoShow( p_input->p_fifo );
00664         if( i_stream == -1 || p_data->i_dts < i_dts )
00665         {
00666             i_stream = i;
00667             i_dts    = p_data->i_dts;
00668         }
00669     }
00670 
00671     *pi_stream = i_stream;
00672     *pi_dts = i_dts;
00673 
00674     return VLC_SUCCESS;
00675 }
00676 
00677 /****************************************************************************
00678  * Asf header construction
00679  ****************************************************************************/
00680 
00681 /****************************************************************************
00682  * Buffer out
00683  ****************************************************************************/
00684 static void bo_init( bo_t *p_bo, uint8_t *p_buffer, int i_size )
00685 {
00686     p_bo->i_buffer_size = i_size;
00687     p_bo->i_buffer = 0;
00688     p_bo->p_buffer = p_buffer;
00689 }
00690 static void bo_add_u8( bo_t *p_bo, uint8_t i )
00691 {
00692     if( p_bo->i_buffer < p_bo->i_buffer_size )
00693     {
00694         p_bo->p_buffer[p_bo->i_buffer] = i;
00695     }
00696     p_bo->i_buffer++;
00697 }
00698 static void bo_addle_u16( bo_t *p_bo, uint16_t i )
00699 {
00700     bo_add_u8( p_bo, i &0xff );
00701     bo_add_u8( p_bo, ( ( i >> 8) &0xff ) );
00702 }
00703 static void bo_addle_u32( bo_t *p_bo, uint32_t i )
00704 {
00705     bo_addle_u16( p_bo, i &0xffff );
00706     bo_addle_u16( p_bo, ( ( i >> 16) &0xffff ) );
00707 }
00708 static void bo_addle_u64( bo_t *p_bo, uint64_t i )
00709 {
00710     bo_addle_u32( p_bo, i &0xffffffff );
00711     bo_addle_u32( p_bo, ( ( i >> 32) &0xffffffff ) );
00712 }
00713 
00714 static void bo_add_mem( bo_t *p_bo, uint8_t *p_mem, int i_size )
00715 {
00716     int i_copy = __MIN( i_size, p_bo->i_buffer_size - p_bo->i_buffer );
00717 
00718     if( i_copy > 0 )
00719     {
00720         memcpy( &p_bo->p_buffer[p_bo->i_buffer], p_mem, i_copy );
00721     }
00722     p_bo->i_buffer += i_size;
00723 }
00724 
00725 static void bo_addle_str16( bo_t *bo, char *str )
00726 {
00727     bo_addle_u16( bo, strlen( str ) + 1 );
00728     for( ;; )
00729     {
00730         uint16_t c = (uint8_t)*str++;
00731         bo_addle_u16( bo, c );
00732         if( c == '\0' ) break;
00733     }
00734 }
00735 
00736 static void bo_addle_str16_nosize( bo_t *bo, char *str )
00737 {
00738     for( ;; )
00739     {
00740         uint16_t c = (uint8_t)*str++;
00741         bo_addle_u16( bo, c );
00742         if( c == '\0' ) break;
00743     }
00744 }
00745 
00746 /****************************************************************************
00747  * GUID definitions
00748  ****************************************************************************/
00749 static void bo_add_guid( bo_t *p_bo, const guid_t *id )
00750 {
00751     int i;
00752     bo_addle_u32( p_bo, id->Data1 );
00753     bo_addle_u16( p_bo, id->Data2 );
00754     bo_addle_u16( p_bo, id->Data3 );
00755     for( i = 0; i < 8; i++ )
00756     {
00757         bo_add_u8( p_bo, id->Data4[i] );
00758     }
00759 }
00760 
00761 static const guid_t asf_object_header_guid =
00762 {0x75B22630, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
00763 static const guid_t asf_object_data_guid =
00764 {0x75B22636, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
00765 static const guid_t asf_object_file_properties_guid =
00766 {0x8cabdca1, 0xa947, 0x11cf, {0x8e, 0xe4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
00767 static const guid_t asf_object_stream_properties_guid =
00768 {0xB7DC0791, 0xA9B7, 0x11CF, {0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
00769 static const guid_t asf_object_header_extention_guid =
00770 {0x5FBF03B5, 0xA92E, 0x11CF, {0x8E, 0xE3, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
00771 static const guid_t asf_object_stream_type_audio =
00772 {0xF8699E40, 0x5B4D, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
00773 static const guid_t asf_object_stream_type_video =
00774 {0xbc19efc0, 0x5B4D, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
00775 static const guid_t asf_guid_audio_conceal_none =
00776 {0x20FB5700, 0x5B55, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
00777 static const guid_t asf_guid_audio_conceal_spread =
00778 {0xBFC3CD50, 0x618F, 0x11CF, {0x8B, 0xB2, 0x00, 0xAA, 0x00, 0xB4, 0xE2, 0x20}};
00779 static const guid_t asf_guid_video_conceal_none =
00780 {0x20FB5700, 0x5B55, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
00781 static const guid_t asf_guid_reserved_1 =
00782 {0xABD3D211, 0xA9BA, 0x11cf, {0x8E, 0xE6, 0x00, 0xC0, 0x0C ,0x20, 0x53, 0x65}};
00783 static const guid_t asf_object_codec_list_guid =
00784 {0x86D15240, 0x311D, 0x11D0, {0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6}};
00785 static const guid_t asf_object_codec_list_reserved_guid =
00786 {0x86D15241, 0x311D, 0x11D0, {0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6}};
00787 static const guid_t asf_object_content_description_guid =
00788 {0x75B22633, 0x668E, 0x11CF, {0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c}};
00789 static const guid_t asf_object_index_guid =
00790 {0x33000890, 0xE5B1, 0x11CF, {0x89, 0xF4, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xCB}};
00791 static const guid_t asf_object_metadata_guid =
00792 {0xC5F8CBEA, 0x5BAF, 0x4877, {0x84, 0x67, 0xAA, 0x8C, 0x44, 0xFA, 0x4C, 0xCA}};
00793 
00794 /****************************************************************************
00795  * Misc
00796  ****************************************************************************/
00797 static void asf_chunk_add( bo_t *bo,
00798                            int i_type, int i_len, int i_flags, int i_seq )
00799 {
00800     bo_addle_u16( bo, i_type );
00801     bo_addle_u16( bo, i_len + 8 );
00802     bo_addle_u32( bo, i_seq );
00803     bo_addle_u16( bo, i_flags );
00804     bo_addle_u16( bo, i_len + 8 );
00805 }
00806 
00807 static block_t *asf_header_create( sout_mux_t *p_mux, vlc_bool_t b_broadcast )
00808 {
00809     sout_mux_sys_t *p_sys = p_mux->p_sys;
00810     asf_track_t    *tk;
00811     mtime_t i_duration = 0;
00812     int i_size, i_header_ext_size, i;
00813     int i_ci_size, i_cm_size = 0, i_cd_size = 0;
00814     block_t *out;
00815     bo_t bo;
00816 
00817     msg_Dbg( p_mux, "Asf muxer creating header" );
00818 
00819     if( p_sys->i_dts_first > 0 )
00820     {
00821         i_duration = p_sys->i_dts_last - p_sys->i_dts_first;
00822         if( i_duration < 0 ) i_duration = 0;
00823     }
00824 
00825     /* calculate header size */
00826     i_size = 30 + 104;
00827     i_ci_size = 44;
00828     for( i = 0; i < p_sys->i_track; i++ )
00829     {
00830         i_size += 78 + p_sys->track[i].i_extra;
00831         i_ci_size += 8 + 2 * strlen( p_sys->track[i].psz_name );
00832         if( p_sys->track[i].i_cat == AUDIO_ES ) i_ci_size += 4;
00833         else if( p_sys->track[i].i_cat == VIDEO_ES ) i_ci_size += 6;
00834 
00835         /* Error correction data field */
00836         if( p_sys->track[i].b_audio_correction ) i_size += 8;
00837     }
00838 
00839     /* size of the content description object */
00840     if( *p_sys->psz_title || *p_sys->psz_author || *p_sys->psz_copyright ||
00841         *p_sys->psz_comment || *p_sys->psz_rating )
00842     {
00843         i_cd_size = 34 + 2 * ( strlen( p_sys->psz_title ) + 1 +
00844                              strlen( p_sys->psz_author ) + 1 +
00845                              strlen( p_sys->psz_copyright ) + 1 +
00846                              strlen( p_sys->psz_comment ) + 1 +
00847                              strlen( p_sys->psz_rating ) + 1 );
00848     }
00849 
00850     /* size of the metadata object */
00851     for( i = 0; i < p_sys->i_track; i++ )
00852     {
00853         if( p_sys->track[i].i_cat == VIDEO_ES )
00854         {
00855             i_cm_size = 26 + 2 * (16 + 2 * sizeof("AspectRatio?"));
00856             break;
00857         }
00858     }
00859 
00860     i_header_ext_size = i_cm_size ? i_cm_size + 46 : 0;
00861     i_size += i_ci_size + i_cd_size + i_header_ext_size ;
00862 
00863     if( p_sys->b_asf_http )
00864     {
00865         out = block_New( p_mux, i_size + 50 + 12 );
00866         bo_init( &bo, out->p_buffer, i_size + 50 + 12 );
00867         asf_chunk_add( &bo, 0x4824, i_size + 50, 0xc00, p_sys->i_seq++ );
00868     }
00869     else
00870     {
00871         out = block_New( p_mux, i_size + 50 );
00872         bo_init( &bo, out->p_buffer, i_size + 50 );
00873     }
00874 
00875     /* header object */
00876     bo_add_guid ( &bo, &asf_object_header_guid );
00877     bo_addle_u64( &bo, i_size );
00878     bo_addle_u32( &bo, 2 + p_sys->i_track +
00879                   (i_cd_size ? 1 : 0) + (i_cm_size ? 1 : 0) );
00880     bo_add_u8   ( &bo, 1 );
00881     bo_add_u8   ( &bo, 2 );
00882 
00883     /* sub object */
00884 
00885     /* file properties */
00886     bo_add_guid ( &bo, &asf_object_file_properties_guid );
00887     bo_addle_u64( &bo, 104 );
00888     bo_add_guid ( &bo, &p_sys->fid );
00889     bo_addle_u64( &bo, i_size + 50 + p_sys->i_packet_count *
00890                                 p_sys->i_packet_size ); /* file size */
00891     bo_addle_u64( &bo, 0 );                 /* creation date */
00892     bo_addle_u64( &bo, b_broadcast ? 0xffffffffLL : p_sys->i_packet_count );
00893     bo_addle_u64( &bo, i_duration * 10 );   /* play duration (100ns) */
00894     bo_addle_u64( &bo, i_duration * 10 );   /* send duration (100ns) */
00895     bo_addle_u64( &bo, p_sys->i_preroll_time ); /* preroll duration (ms) */
00896     bo_addle_u32( &bo, b_broadcast ? 0x01 : 0x02 /* seekable */ ); /* flags */
00897     bo_addle_u32( &bo, p_sys->i_packet_size );  /* packet size min */
00898     bo_addle_u32( &bo, p_sys->i_packet_size );  /* packet size max */
00899     bo_addle_u32( &bo, p_sys->i_bitrate );      /* maxbitrate */
00900 
00901     /* header extention */
00902     if( i_header_ext_size )
00903     {
00904         bo_add_guid ( &bo, &asf_object_header_extention_guid );
00905         bo_addle_u64( &bo, i_header_ext_size );
00906         bo_add_guid ( &bo, &asf_guid_reserved_1 );
00907         bo_addle_u16( &bo, 6 );
00908         bo_addle_u32( &bo, i_header_ext_size - 46 );
00909     }
00910 
00911     /* metadata object (part of header extension) */
00912     if( i_cm_size )
00913     {
00914         int64_t i_num, i_den;
00915         unsigned int i_dst_num, i_dst_den;
00916 
00917         for( i = 0; i < p_sys->i_track; i++ )
00918             if( p_sys->track[i].i_cat == VIDEO_ES ) break;
00919 
00920         i_num = p_sys->track[i].fmt.video.i_aspect *
00921             (int64_t)p_sys->track[i].fmt.video.i_height;
00922         i_den = VOUT_ASPECT_FACTOR * p_sys->track[i].fmt.video.i_width;
00923         vlc_ureduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
00924 
00925         msg_Dbg( p_mux, "pixel aspect-ratio: %i/%i", i_dst_num, i_dst_den );
00926 
00927         bo_add_guid ( &bo, &asf_object_metadata_guid );
00928         bo_addle_u64( &bo, i_cm_size );
00929         bo_addle_u16( &bo, 2 ); /* description records count */
00930         /* 1st description record */
00931         bo_addle_u16( &bo, 0 ); /* reserved */
00932         bo_addle_u16( &bo, i + 1 ); /* stream number (0 for the whole file) */
00933         bo_addle_u16( &bo, 2 * sizeof("AspectRatioX") ); /* name length */
00934         bo_addle_u16( &bo, 0x3 /* DWORD */ ); /* data type */
00935         bo_addle_u32( &bo, 4 ); /* data length */
00936         bo_addle_str16_nosize( &bo, "AspectRatioX" );
00937         bo_addle_u32( &bo, i_dst_num ); /* data */
00938         /* 2nd description record */
00939         bo_addle_u16( &bo, 0 ); /* reserved */
00940         bo_addle_u16( &bo, i + 1 ); /* stream number (0 for the whole file) */
00941         bo_addle_u16( &bo, 2 * sizeof("AspectRatioY") ); /* name length */
00942         bo_addle_u16( &bo, 0x3 /* DWORD */ ); /* data type */
00943         bo_addle_u32( &bo, 4 ); /* data length */
00944         bo_addle_str16_nosize( &bo, "AspectRatioY" );
00945         bo_addle_u32( &bo, i_dst_den ); /* data */
00946     }
00947 
00948     /* content description header */
00949     if( i_cd_size > 0 )
00950     {
00951         bo_add_guid ( &bo, &asf_object_content_description_guid );
00952         bo_addle_u64( &bo, i_cd_size );
00953         bo_addle_u16( &bo, 2 * strlen( p_sys->psz_title ) + 2 );
00954         bo_addle_u16( &bo, 2 * strlen( p_sys->psz_author ) + 2 );
00955         bo_addle_u16( &bo, 2 * strlen( p_sys->psz_copyright ) + 2 );
00956         bo_addle_u16( &bo, 2 * strlen( p_sys->psz_comment ) + 2 );
00957         bo_addle_u16( &bo, 2 * strlen( p_sys->psz_rating ) + 2 );
00958 
00959         bo_addle_str16_nosize( &bo, p_sys->psz_title );
00960         bo_addle_str16_nosize( &bo, p_sys->psz_author );
00961         bo_addle_str16_nosize( &bo, p_sys->psz_copyright );
00962         bo_addle_str16_nosize( &bo, p_sys->psz_comment );
00963         bo_addle_str16_nosize( &bo, p_sys->psz_rating );
00964     }
00965 
00966     /* stream properties */
00967     for( i = 0; i < p_sys->i_track; i++ )
00968     {
00969         tk = &p_sys->track[i];
00970 
00971         bo_add_guid ( &bo, &asf_object_stream_properties_guid );
00972         bo_addle_u64( &bo, 78 + tk->i_extra + (tk->b_audio_correction ? 8:0) );
00973 
00974         if( tk->i_cat == AUDIO_ES )
00975         {
00976             bo_add_guid( &bo, &asf_object_stream_type_audio );
00977             if( tk->b_audio_correction )
00978                 bo_add_guid( &bo, &asf_guid_audio_conceal_spread );
00979             else
00980                 bo_add_guid( &bo, &asf_guid_audio_conceal_none );
00981         }
00982         else if( tk->i_cat == VIDEO_ES )
00983         {
00984             bo_add_guid( &bo, &asf_object_stream_type_video );
00985             bo_add_guid( &bo, &asf_guid_video_conceal_none );
00986         }
00987         bo_addle_u64( &bo, 0 );         /* time offset */
00988         bo_addle_u32( &bo, tk->i_extra );
00989         /* correction data length */
00990         bo_addle_u32( &bo, tk->b_audio_correction ? 8 : 0 );
00991         bo_addle_u16( &bo, tk->i_id );  /* stream number */
00992         bo_addle_u32( &bo, 0 );
00993         bo_add_mem  ( &bo, tk->p_extra, tk->i_extra );
00994 
00995         /* Error correction data field */
00996         if( tk->b_audio_correction )
00997         {
00998             bo_add_u8( &bo, 0x1 ); /* span */
00999             bo_addle_u16( &bo, tk->i_blockalign );  /* virtual packet length */
01000             bo_addle_u16( &bo, tk->i_blockalign );  /* virtual chunck length */
01001             bo_addle_u16( &bo, 1 );  /* silence length */
01002             bo_add_u8( &bo, 0x0 ); /* data */
01003         }
01004     }
01005 
01006     /* Codec Infos */
01007     bo_add_guid ( &bo, &asf_object_codec_list_guid );
01008     bo_addle_u64( &bo, i_ci_size );
01009     bo_add_guid ( &bo, &asf_object_codec_list_reserved_guid );
01010     bo_addle_u32( &bo, p_sys->i_track );
01011     for( i = 0; i < p_sys->i_track; i++ )
01012     {
01013         tk = &p_sys->track[i];
01014 
01015         if( tk->i_cat == VIDEO_ES ) bo_addle_u16( &bo, 1 /* video */ );
01016         else if( tk->i_cat == AUDIO_ES ) bo_addle_u16( &bo, 2 /* audio */ );
01017         else bo_addle_u16( &bo, 0xFFFF /* unknown */ );
01018 
01019         bo_addle_str16( &bo, tk->psz_name );
01020         bo_addle_u16( &bo, 0 );
01021         if( tk->i_cat == AUDIO_ES )
01022         {
01023             bo_addle_u16( &bo, 2 );
01024             bo_addle_u16( &bo, tk->i_tag );
01025         }
01026         else if( tk->i_cat == VIDEO_ES )
01027         {
01028             bo_addle_u16( &bo, 4 );
01029             bo_add_mem  ( &bo, (uint8_t*)&tk->i_fourcc, 4 );
01030         }
01031     }
01032 
01033     /* data object */
01034     bo_add_guid ( &bo, &asf_object_data_guid );
01035     bo_addle_u64( &bo, 50 + p_sys->i_packet_count * p_sys->i_packet_size );
01036     bo_add_guid ( &bo, &p_sys->fid );
01037     bo_addle_u64( &bo, p_sys->i_packet_count );
01038     bo_addle_u16( &bo, 0x101 );
01039 
01040     return out;
01041 }
01042 
01043 /****************************************************************************
01044  *
01045  ****************************************************************************/
01046 static block_t *asf_packet_flush( sout_mux_t *p_mux )
01047 {
01048     sout_mux_sys_t *p_sys = p_mux->p_sys;
01049     int i_pad, i_preheader = p_sys->b_asf_http ? 12 : 0;
01050     block_t *pk;
01051     bo_t bo;
01052 
01053     if( !p_sys->pk ) return 0;
01054 
01055     i_pad = p_sys->i_packet_size - p_sys->i_pk_used;
01056     memset( p_sys->pk->p_buffer + p_sys->i_pk_used, 0, i_pad );
01057 
01058     bo_init( &bo, p_sys->pk->p_buffer, 14 + i_preheader );
01059 
01060     if( p_sys->b_asf_http )
01061         asf_chunk_add( &bo, 0x4424, p_sys->i_packet_size, 0x0, p_sys->i_seq++);
01062 
01063     bo_add_u8   ( &bo, 0x82 );
01064     bo_addle_u16( &bo, 0 );
01065     bo_add_u8( &bo, 0x11 );
01066     bo_add_u8( &bo, 0x5d );
01067     bo_addle_u16( &bo, i_pad );
01068     bo_addle_u32( &bo, (p_sys->i_pk_dts - p_sys->i_dts_first) / 1000 +
01069                   p_sys->i_preroll_time );
01070     bo_addle_u16( &bo, 0 /* data->i_length */ );
01071     bo_add_u8( &bo, 0x80 | p_sys->i_pk_frame );
01072 
01073     pk = p_sys->pk;
01074     p_sys->pk = NULL;
01075 
01076     p_sys->i_packet_count++;
01077 
01078     return pk;
01079 }
01080 
01081 static block_t *asf_packet_create( sout_mux_t *p_mux,
01082                                    asf_track_t *tk, block_t *data )
01083 {
01084     sout_mux_sys_t *p_sys = p_mux->p_sys;
01085 
01086     int     i_data = data->i_buffer;
01087     int     i_pos  = 0;
01088     uint8_t *p_data= data->p_buffer;
01089     block_t *first = NULL, **last = &first;
01090     int     i_preheader = p_sys->b_asf_http ? 12 : 0;
01091 
01092     while( i_pos < i_data )
01093     {
01094         bo_t bo;
01095         int i_payload;
01096 
01097         if( p_sys->pk == NULL )
01098         {
01099             p_sys->pk = block_New( p_mux, p_sys->i_packet_size + i_preheader );
01100             /* reserve 14 bytes for the packet header */
01101             p_sys->i_pk_used = 14 + i_preheader;
01102             p_sys->i_pk_frame = 0;
01103             p_sys->i_pk_dts = data->i_dts;
01104         }
01105 
01106         bo_init( &bo, &p_sys->pk->p_buffer[p_sys->i_pk_used],
01107                  p_sys->i_packet_size - p_sys->i_pk_used );
01108 
01109         /* add payload (header size = 17) */
01110         i_payload = __MIN( i_data - i_pos,
01111                            p_sys->i_packet_size - p_sys->i_pk_used - 17 );
01112 
01113         if( tk->b_audio_correction && p_sys->i_pk_frame && i_payload < i_data )
01114         {
01115             /* Don't know why yet but WMP doesn't like splitted WMA packets */
01116             *last = asf_packet_flush( p_mux );
01117             last  = &(*last)->p_next;
01118             continue;
01119         }
01120 
01121         bo_add_u8   ( &bo, !(data->i_flags & BLOCK_FLAG_TYPE_P ||
01122                       data->i_flags & BLOCK_FLAG_TYPE_B) ?
01123                       0x80 | tk->i_id : tk->i_id );
01124         bo_add_u8   ( &bo, tk->i_sequence );
01125         bo_addle_u32( &bo, i_pos );
01126         bo_add_u8   ( &bo, 0x08 );  /* flags */
01127         bo_addle_u32( &bo, i_data );
01128         bo_addle_u32( &bo, (data->i_dts - p_sys->i_dts_first) / 1000 +
01129                       p_sys->i_preroll_time );
01130         bo_addle_u16( &bo, i_payload );
01131         bo_add_mem  ( &bo, &p_data[i_pos], i_payload );
01132         i_pos += i_payload;
01133         p_sys->i_pk_used += 17 + i_payload;
01134 
01135         p_sys->i_pk_frame++;
01136 
01137         if( p_sys->i_pk_used + 17 >= p_sys->i_packet_size )
01138         {
01139             /* Not enough data for another payload, flush the packet */
01140             *last = asf_packet_flush( p_mux );
01141             last  = &(*last)->p_next;
01142         }
01143     }
01144 
01145     tk->i_sequence++;
01146     block_Release( data );
01147 
01148     return first;
01149 }
01150 
01151 static block_t *asf_stream_end_create( sout_mux_t *p_mux )
01152 {
01153     sout_mux_sys_t *p_sys = p_mux->p_sys;
01154 
01155     block_t *out = NULL;
01156     bo_t bo;
01157 
01158     if( p_sys->b_asf_http )
01159     {
01160         out = block_New( p_mux, 12 );
01161         bo_init( &bo, out->p_buffer, 12 );
01162         asf_chunk_add( &bo, 0x4524, 0, 0x00, p_sys->i_seq++ );
01163     }
01164     else
01165     {
01166         /* Create index */
01167         out = block_New( p_mux, 56 );
01168         bo_init( &bo, out->p_buffer, 56 );
01169         bo_add_guid ( &bo, &asf_object_index_guid );
01170         bo_addle_u64( &bo, 56 );
01171         bo_add_guid ( &bo, &p_sys->fid );
01172         bo_addle_u64( &bo, 10000000 );
01173         bo_addle_u32( &bo, 5 );
01174         bo_addle_u32( &bo, 0 );
01175     }
01176 
01177     return out;
01178 }

Generated on Tue Dec 20 10:14:25 2005 for vlc-0.8.4a by  doxygen 1.4.2