]> git.sesse.net Git - vlc/blob - modules/mux/asf.c
Fixed VC1 muxing in ASF.
[vlc] / modules / mux / asf.c
1 /*****************************************************************************
2  * asf.c: asf muxer module for vlc
3  *****************************************************************************
4  * Copyright (C) 2003-2004, 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_sout.h>
36 #include <vlc_block.h>
37 #include <vlc_codecs.h>
38
39 typedef GUID guid_t;
40
41 #define MAX_ASF_TRACKS 128
42 #define ASF_DATA_PACKET_SIZE 4096  // deprecated -- added sout-asf-packet-size
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 static int  Open   ( vlc_object_t * );
48 static void Close  ( vlc_object_t * );
49
50 #define SOUT_CFG_PREFIX "sout-asf-"
51
52 #define TITLE_TEXT N_("Title")
53 #define TITLE_LONGTEXT N_("Title to put in ASF comments." )
54 #define AUTHOR_TEXT N_("Author")
55 #define AUTHOR_LONGTEXT N_("Author to put in ASF comments." )
56 #define COPYRIGHT_TEXT N_("Copyright")
57 #define COPYRIGHT_LONGTEXT N_("Copyright string to put in ASF comments." )
58 #define COMMENT_TEXT N_("Comment")
59 #define COMMENT_LONGTEXT N_("Comment to put in ASF comments." )
60 #define RATING_TEXT N_("Rating")
61 #define RATING_LONGTEXT N_("\"Rating\" to put in ASF comments." )
62 #define PACKETSIZE_TEXT N_("Packet Size")
63 #define PACKETSIZE_LONGTEXT N_("ASF packet size -- default is 4096 bytes")
64 #define BITRATE_TEXT N_("Bitrate override")
65 #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")
66
67
68 vlc_module_begin ()
69     set_description( N_("ASF muxer") )
70     set_category( CAT_SOUT )
71     set_subcategory( SUBCAT_SOUT_MUX )
72     set_shortname( "ASF" )
73
74     set_capability( "sout mux", 5 )
75     add_shortcut( "asf" )
76     add_shortcut( "asfh" )
77     set_callbacks( Open, Close )
78
79     add_string( SOUT_CFG_PREFIX "title", "", NULL, TITLE_TEXT, TITLE_LONGTEXT,
80                                  true )
81     add_string( SOUT_CFG_PREFIX "author",   "", NULL, AUTHOR_TEXT,
82                                  AUTHOR_LONGTEXT, true )
83     add_string( SOUT_CFG_PREFIX "copyright","", NULL, COPYRIGHT_TEXT,
84                                  COPYRIGHT_LONGTEXT, true )
85     add_string( SOUT_CFG_PREFIX "comment",  "", NULL, COMMENT_TEXT,
86                                  COMMENT_LONGTEXT, true )
87     add_string( SOUT_CFG_PREFIX "rating",  "", NULL, RATING_TEXT,
88                                  RATING_LONGTEXT, true )
89     add_integer( SOUT_CFG_PREFIX "packet-size", 4096, NULL, PACKETSIZE_TEXT,
90                                  PACKETSIZE_LONGTEXT, true )
91     add_integer( SOUT_CFG_PREFIX "bitrate-override", 0, NULL, BITRATE_TEXT,
92                                  BITRATE_LONGTEXT, true )
93
94 vlc_module_end ()
95
96 /*****************************************************************************
97  * Locales prototypes
98  *****************************************************************************/
99 static const char *const ppsz_sout_options[] = {
100     "title", "author", "copyright", "comment", "rating", NULL
101 };
102
103 static int Control  ( sout_mux_t *, int, va_list );
104 static int AddStream( sout_mux_t *, sout_input_t * );
105 static int DelStream( sout_mux_t *, sout_input_t * );
106 static int Mux      ( sout_mux_t * );
107
108 typedef struct
109 {
110     int          i_id;
111     int          i_cat;
112
113     /* codec information */
114     uint16_t     i_tag;     /* for audio */
115     vlc_fourcc_t i_fourcc;  /* for video */
116     const char         *psz_name; /* codec name */
117     int          i_blockalign; /* for audio only */
118     bool   b_audio_correction;
119
120     int          i_sequence;
121
122     int          i_extra;
123     uint8_t      *p_extra;
124     bool         b_extended;
125
126     es_format_t  fmt;
127
128 } asf_track_t;
129
130 struct sout_mux_sys_t
131 {
132     guid_t          fid;    /* file id */
133     int             i_packet_size;
134     int64_t         i_packet_count;
135     mtime_t         i_dts_first;
136     mtime_t         i_dts_last;
137     mtime_t         i_preroll_time;
138     int64_t         i_bitrate;
139     int64_t         i_bitrate_override;
140
141     int             i_track;
142     asf_track_t     track[MAX_ASF_TRACKS];
143
144     bool      b_write_header;
145
146     block_t         *pk;
147     int             i_pk_used;
148     int             i_pk_frame;
149     mtime_t         i_pk_dts;
150
151     bool      b_asf_http;
152     int             i_seq;
153
154     /* meta data */
155     char            *psz_title;
156     char            *psz_author;
157     char            *psz_copyright;
158     char            *psz_comment;
159     char            *psz_rating;
160 };
161
162 static int MuxGetStream( sout_mux_t *, int *pi_stream, mtime_t *pi_dts );
163
164 static block_t *asf_header_create( sout_mux_t *, bool );
165 static block_t *asf_packet_create( sout_mux_t *, asf_track_t *, block_t * );
166 static block_t *asf_stream_end_create( sout_mux_t *);
167 static block_t *asf_packet_flush( sout_mux_t * );
168
169 typedef struct
170 {
171     int      i_buffer_size;
172     int      i_buffer;
173     uint8_t  *p_buffer;
174
175 } bo_t;
176
177 static void bo_init     ( bo_t *, uint8_t *, int  );
178 static void bo_add_u8   ( bo_t *, uint8_t  );
179 static void bo_addle_u16( bo_t *, uint16_t );
180 static void bo_addle_u32( bo_t *, uint32_t );
181 static void bo_addle_u64( bo_t *, uint64_t );
182 static void bo_add_mem  ( bo_t *, uint8_t *, int );
183
184 static void bo_addle_str16( bo_t *, const char * );
185
186 /*****************************************************************************
187  * Open:
188  *****************************************************************************/
189 static int Open( vlc_object_t *p_this )
190 {
191     sout_mux_t     *p_mux = (sout_mux_t*)p_this;
192     sout_mux_sys_t *p_sys;
193     vlc_value_t    val;
194     int i;
195
196     msg_Dbg( p_mux, "asf muxer opened" );
197     config_ChainParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
198
199     p_mux->pf_control   = Control;
200     p_mux->pf_addstream = AddStream;
201     p_mux->pf_delstream = DelStream;
202     p_mux->pf_mux       = Mux;
203
204     p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
205     if( !p_sys )
206         return VLC_ENOMEM;
207     p_sys->b_asf_http = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "asfh" );
208     if( p_sys->b_asf_http )
209     {
210         msg_Dbg( p_mux, "creating asf stream to be used with mmsh" );
211     }
212     p_sys->pk = NULL;
213     p_sys->i_pk_used    = 0;
214     p_sys->i_pk_frame   = 0;
215     p_sys->i_dts_first  = -1;
216     p_sys->i_dts_last   = 0;
217     p_sys->i_preroll_time = 2000;
218     p_sys->i_bitrate    = 0;
219     p_sys->i_bitrate_override = 0;
220     p_sys->i_seq        = 0;
221
222     p_sys->b_write_header = true;
223     p_sys->i_track = 0;
224     p_sys->i_packet_size = config_GetInt( p_mux, "sout-asf-packet-size" );
225     p_sys->i_bitrate_override = config_GetInt( p_mux, "sout-asf-bitrate-override" );
226     msg_Dbg( p_mux, "Packet size %d", p_sys->i_packet_size);
227     if (p_sys->i_bitrate_override)
228         msg_Dbg( p_mux, "Bitrate override %"PRId64, p_sys->i_bitrate_override);
229     p_sys->i_packet_count= 0;
230
231     /* Generate a random fid */
232     srand( mdate() & 0xffffffff );
233     p_sys->fid.Data1 = 0xbabac001;
234     p_sys->fid.Data2 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
235     p_sys->fid.Data3 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
236     for( i = 0; i < 8; i++ )
237     {
238         p_sys->fid.Data4[i] = ( (uint64_t)rand() << 8 ) / RAND_MAX;
239     }
240
241     /* Meta data */
242     var_Get( p_mux, SOUT_CFG_PREFIX "title", &val );
243     p_sys->psz_title = val.psz_string;
244
245     var_Get( p_mux, SOUT_CFG_PREFIX "author", &val );
246     p_sys->psz_author = val.psz_string;
247
248     var_Get( p_mux, SOUT_CFG_PREFIX "copyright", &val );
249     p_sys->psz_copyright = val.psz_string;
250
251     var_Get( p_mux, SOUT_CFG_PREFIX "comment", &val );
252     p_sys->psz_comment = val.psz_string;
253
254     var_Get( p_mux, SOUT_CFG_PREFIX "rating", &val );
255     p_sys->psz_rating = val.psz_string;
256
257     msg_Dbg( p_mux, "meta data: title='%s', author='%s', copyright='%s', "
258              "comment='%s', rating='%s'",
259              p_sys->psz_title, p_sys->psz_author, p_sys->psz_copyright,
260              p_sys->psz_comment, p_sys->psz_rating );
261
262     return VLC_SUCCESS;
263 }
264
265 /*****************************************************************************
266  * Close:
267  *****************************************************************************/
268 static void Close( vlc_object_t * p_this )
269 {
270     sout_mux_t     *p_mux = (sout_mux_t*)p_this;
271     sout_mux_sys_t *p_sys = p_mux->p_sys;
272     block_t  *out;
273     int i;
274
275     msg_Dbg( p_mux, "Asf muxer closed" );
276
277     /* Flush last packet if any */
278     if( (out = asf_packet_flush( p_mux ) ) )
279     {
280         sout_AccessOutWrite( p_mux->p_access, out );
281     }
282
283     if( ( out = asf_stream_end_create( p_mux ) ) )
284     {
285         sout_AccessOutWrite( p_mux->p_access, out );
286     }
287
288     /* rewrite header */
289     if( sout_AccessOutSeek( p_mux->p_access, 0 ) == VLC_SUCCESS )
290     {
291         out = asf_header_create( p_mux, false );
292         sout_AccessOutWrite( p_mux->p_access, out );
293     }
294
295     for( i = 0; i < p_sys->i_track; i++ )
296     {
297         free( p_sys->track[i].p_extra );
298         es_format_Clean( &p_sys->track[i].fmt );
299     }
300
301     free( p_sys->psz_title );
302     free( p_sys->psz_author );
303     free( p_sys->psz_copyright );
304     free( p_sys->psz_comment );
305     free( p_sys->psz_rating );
306     free( p_sys );
307 }
308
309 /*****************************************************************************
310  * Capability:
311  *****************************************************************************/
312 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
313 {
314     sout_mux_sys_t *p_sys = p_mux->p_sys;
315     bool *pb_bool;
316     char **ppsz;
317
318     switch( i_query )
319     {
320        case MUX_CAN_ADD_STREAM_WHILE_MUXING:
321            pb_bool = (bool*)va_arg( args, bool * );
322            if( p_sys->b_asf_http ) *pb_bool = true;
323            else *pb_bool = false;
324            return VLC_SUCCESS;
325
326        case MUX_GET_ADD_STREAM_WAIT:
327            pb_bool = (bool*)va_arg( args, bool * );
328            *pb_bool = true;
329            return VLC_SUCCESS;
330
331        case MUX_GET_MIME:
332            ppsz = (char**)va_arg( args, char ** );
333            if( p_sys->b_asf_http )
334                *ppsz = strdup( "video/x-ms-asf-stream" );
335            else
336                *ppsz = strdup( "video/x-ms-asf" );
337            return VLC_SUCCESS;
338
339         default:
340             return VLC_EGENERIC;
341     }
342 }
343
344 /*****************************************************************************
345  * AddStream:
346  *****************************************************************************/
347 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
348 {
349     sout_mux_sys_t   *p_sys = p_mux->p_sys;
350     asf_track_t      *tk;
351     bo_t             bo;
352
353     msg_Dbg( p_mux, "adding input" );
354     if( p_sys->i_track >= MAX_ASF_TRACKS )
355     {
356         msg_Dbg( p_mux, "cannot add this track (too much tracks)" );
357         return VLC_EGENERIC;
358     }
359
360     tk = p_input->p_sys = &p_sys->track[p_sys->i_track];
361     tk->i_id  = p_sys->i_track + 1;
362     tk->i_cat = p_input->p_fmt->i_cat;
363     tk->i_sequence = 0;
364     tk->b_audio_correction = 0;
365     tk->b_extended = false;
366
367     switch( tk->i_cat )
368     {
369         case AUDIO_ES:
370         {
371             int i_blockalign = p_input->p_fmt->audio.i_blockalign;
372             int i_bitspersample = p_input->p_fmt->audio.i_bitspersample;
373             int i_extra = 0;
374
375             switch( p_input->p_fmt->i_codec )
376             {
377                 case VLC_FOURCC( 'a', '5', '2', ' ' ):
378                     tk->i_tag = WAVE_FORMAT_A52;
379                     tk->psz_name = "A/52";
380                     i_bitspersample = 0;
381                     break;
382                 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
383                     tk->i_tag = WAVE_FORMAT_AAC;
384                     tk->psz_name = "MPEG-4 Audio";
385                     i_bitspersample = 0;
386                     break;
387                 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
388 #if 1
389                     tk->psz_name = "MPEG Audio Layer 3";
390                     tk->i_tag = WAVE_FORMAT_MPEGLAYER3;
391                     i_bitspersample = 0;
392                     i_blockalign = 1;
393                     i_extra = 12;
394                     break;
395 #else
396                     tk->psz_name = "MPEG Audio Layer 1/2";
397                     tk->i_tag = WAVE_FORMAT_MPEG;
398                     i_bitspersample = 0;
399                     i_blockalign = 1;
400                     i_extra = 22;
401                     break;
402 #endif
403                 case VLC_FOURCC( 'w', 'm', 'a', '1' ):
404                     tk->psz_name = "Windows Media Audio v1";
405                     tk->i_tag = WAVE_FORMAT_WMA1;
406                     tk->b_audio_correction = true;
407                     break;
408                 case VLC_FOURCC( 'w', 'm', 'a', ' ' ):
409                 case VLC_FOURCC( 'w', 'm', 'a', '2' ):
410                     tk->psz_name= "Windows Media Audio (v2) 7, 8 and 9 Series";
411                     tk->i_tag = WAVE_FORMAT_WMA2;
412                     tk->b_audio_correction = true;
413                     break;
414                 case VLC_FOURCC( 'w', 'm', 'a', 'p' ):
415                     tk->psz_name = "Windows Media Audio 9 Professional";
416                     tk->i_tag = WAVE_FORMAT_WMAP;
417                     tk->b_audio_correction = true;
418                     break;
419                 case VLC_FOURCC( 'w', 'm', 'a', 'l' ):
420                     tk->psz_name = "Windows Media Audio 9 Lossless";
421                     tk->i_tag = WAVE_FORMAT_WMAL;
422                     tk->b_audio_correction = true;
423                     break;
424                     /* raw codec */
425                 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
426                     tk->psz_name = "Raw audio 8bits";
427                     tk->i_tag = WAVE_FORMAT_PCM;
428                     i_blockalign= p_input->p_fmt->audio.i_channels;
429                     i_bitspersample = 8;
430                     break;
431                 case VLC_FOURCC( 's', '1', '6', 'l' ):
432                     tk->psz_name = "Raw audio 16bits";
433                     tk->i_tag = WAVE_FORMAT_PCM;
434                     i_blockalign= 2 * p_input->p_fmt->audio.i_channels;
435                     i_bitspersample = 16;
436                     break;
437                 case VLC_FOURCC( 's', '2', '4', 'l' ):
438                     tk->psz_name = "Raw audio 24bits";
439                     tk->i_tag = WAVE_FORMAT_PCM;
440                     i_blockalign= 3 * p_input->p_fmt->audio.i_channels;
441                     i_bitspersample = 24;
442                     break;
443                 case VLC_FOURCC( 's', '3', '2', 'l' ):
444                     tk->psz_name = "Raw audio 32bits";
445                     tk->i_tag = WAVE_FORMAT_PCM;
446                     i_blockalign= 4 * p_input->p_fmt->audio.i_channels;
447                     i_bitspersample = 32;
448                     break;
449                 default:
450                     return VLC_EGENERIC;
451             }
452
453             tk->i_extra = sizeof( WAVEFORMATEX ) +
454                           p_input->p_fmt->i_extra + i_extra;
455             tk->p_extra = malloc( tk->i_extra );
456             if( !tk->p_extra )
457                 return VLC_ENOMEM;
458             bo_init( &bo, tk->p_extra, tk->i_extra );
459             bo_addle_u16( &bo, tk->i_tag );
460             bo_addle_u16( &bo, p_input->p_fmt->audio.i_channels );
461             bo_addle_u32( &bo, p_input->p_fmt->audio.i_rate );
462             bo_addle_u32( &bo, p_input->p_fmt->i_bitrate / 8 );
463             bo_addle_u16( &bo, i_blockalign );
464             tk->i_blockalign = i_blockalign;
465             bo_addle_u16( &bo, i_bitspersample );
466             if( p_input->p_fmt->i_extra > 0 )
467             {
468                 bo_addle_u16( &bo, p_input->p_fmt->i_extra );
469                 bo_add_mem  ( &bo, p_input->p_fmt->p_extra,
470                               p_input->p_fmt->i_extra );
471             }
472             else
473             {
474                 bo_addle_u16( &bo, i_extra );
475                 if( tk->i_tag == WAVE_FORMAT_MPEGLAYER3 )
476                 {
477                     msg_Dbg( p_mux, "adding mp3 header" );
478                     bo_addle_u16( &bo, 1 );     /* wId */
479                     bo_addle_u32( &bo, 2 );     /* fdwFlags */
480                     bo_addle_u16( &bo, 1152 );  /* nBlockSize */
481                     bo_addle_u16( &bo, 1 );     /* nFramesPerBlock */
482                     bo_addle_u16( &bo, 1393 );  /* nCodecDelay */
483                 }
484                 else if( tk->i_tag == WAVE_FORMAT_MPEG )
485                 {
486                     msg_Dbg( p_mux, "adding mp2 header" );
487                     bo_addle_u16( &bo, 2 );     /* fwHeadLayer */
488                     bo_addle_u32( &bo, p_input->p_fmt->i_bitrate );
489                     bo_addle_u16( &bo, p_input->p_fmt->audio.i_channels == 2 ?1:8 );
490                     bo_addle_u16( &bo, 0 );     /* fwHeadModeExt */
491                     bo_addle_u16( &bo, 1 );     /* wHeadEmphasis */
492                     bo_addle_u16( &bo, 16 );    /* fwHeadFlags */
493                     bo_addle_u32( &bo, 0 );     /* dwPTSLow */
494                     bo_addle_u32( &bo, 0 );     /* dwPTSHigh */
495                 }
496             }
497
498             if( p_input->p_fmt->i_bitrate > 24000 )
499             {
500                 p_sys->i_bitrate += p_input->p_fmt->i_bitrate;
501             }
502             else
503             {
504                 p_sys->i_bitrate += 128000;
505             }
506             if (p_sys->i_bitrate_override)
507                 p_sys->i_bitrate = p_sys->i_bitrate_override;
508             break;
509         }
510         case VIDEO_ES:
511         {
512             const es_format_t *p_fmt = p_input->p_fmt;
513             uint8_t *p_codec_extra = NULL;
514             int     i_codec_extra = 0;
515
516             if( p_input->p_fmt->i_codec == VLC_FOURCC('m','p','4','v') )
517             {
518                 tk->psz_name = "MPEG-4 Video";
519                 tk->i_fourcc = VLC_FOURCC( 'M', 'P', '4', 'S' );
520             }
521             else if( p_input->p_fmt->i_codec == VLC_FOURCC('D','I','V','3') )
522             {
523                 tk->psz_name = "MSMPEG-4 V3 Video";
524                 tk->i_fourcc = VLC_FOURCC( 'M', 'P', '4', '3' );
525             }
526             else if( p_input->p_fmt->i_codec == VLC_FOURCC('D','I','V','2') )
527             {
528                 tk->psz_name = "MSMPEG-4 V2 Video";
529                 tk->i_fourcc = VLC_FOURCC( 'M', 'P', '4', '2' );
530             }
531             else if( p_input->p_fmt->i_codec == VLC_FOURCC('D','I','V','1') )
532             {
533                 tk->psz_name = "MSMPEG-4 V1 Video";
534                 tk->i_fourcc = VLC_FOURCC( 'M', 'P', 'G', '4' );
535             }
536             else if( p_input->p_fmt->i_codec == VLC_FOURCC('W','M','V','1') )
537             {
538                 tk->psz_name = "Windows Media Video 7";
539                 tk->i_fourcc = VLC_FOURCC( 'W', 'M', 'V', '1' );
540             }
541             else if( p_input->p_fmt->i_codec == VLC_FOURCC('W','M','V','2') )
542             {
543                 tk->psz_name = "Windows Media Video 8";
544                 tk->i_fourcc = VLC_FOURCC( 'W', 'M', 'V', '2' );
545             }
546             else if( p_input->p_fmt->i_codec == VLC_FOURCC('W','M','V','3') )
547             {
548                 tk->psz_name = "Windows Media Video 9";
549                 tk->i_fourcc = VLC_FOURCC( 'W', 'M', 'V', '3' );
550             }
551             else if( p_input->p_fmt->i_codec == VLC_FOURCC('W','V','C','1') )
552             {
553                 tk->psz_name = "Windows Media Video 9 Advanced Profile";
554                 tk->i_fourcc = VLC_FOURCC( 'W', 'V', 'C', '1' );
555                 tk->b_extended = true;
556
557                 if( p_fmt->i_extra > 0 )
558                 {
559                     p_codec_extra = malloc( 1 + p_fmt->i_extra );
560                     if( p_codec_extra )
561                     {
562                         i_codec_extra = 1 + p_fmt->i_extra;
563                         p_codec_extra[0] = 0x01;
564                         memcpy( &p_codec_extra[1], p_fmt->p_extra, p_fmt->i_extra );
565                     }
566                 }
567             }
568             else if( p_input->p_fmt->i_codec == VLC_FOURCC('h','2','6','4') )
569             {
570                 tk->psz_name = "H.264/MPEG-4 AVC";
571                 tk->i_fourcc = VLC_FOURCC('h','2','6','4');
572             }
573             else
574             {
575                 tk->psz_name = _("Unknown Video");
576                 tk->i_fourcc = p_input->p_fmt->i_codec;
577             }
578             if( !i_codec_extra && p_fmt->i_extra > 0 )
579             {
580                 p_codec_extra = malloc( p_fmt->i_extra );
581                 if( p_codec_extra )
582                 {
583                     i_codec_extra = p_fmt->i_extra;
584                     memcpy( p_codec_extra, p_fmt->p_extra, p_fmt->i_extra );
585                 }
586             }
587
588             tk->i_extra = 11 + sizeof( BITMAPINFOHEADER ) + i_codec_extra;
589             tk->p_extra = malloc( tk->i_extra );
590             if( !tk->p_extra )
591                 return VLC_ENOMEM;
592             bo_init( &bo, tk->p_extra, tk->i_extra );
593             bo_addle_u32( &bo, p_input->p_fmt->video.i_width );
594             bo_addle_u32( &bo, p_input->p_fmt->video.i_height );
595             bo_add_u8   ( &bo, 0x02 );  /* flags */
596             bo_addle_u16( &bo, sizeof( BITMAPINFOHEADER ) + i_codec_extra );
597             bo_addle_u32( &bo, sizeof( BITMAPINFOHEADER ) + i_codec_extra );
598             bo_addle_u32( &bo, p_input->p_fmt->video.i_width );
599             bo_addle_u32( &bo, p_input->p_fmt->video.i_height );
600             bo_addle_u16( &bo, 1 );
601             bo_addle_u16( &bo, 24 );
602             bo_add_mem( &bo, (uint8_t*)&tk->i_fourcc, 4 );
603             bo_addle_u32( &bo, 0 );
604             bo_addle_u32( &bo, 0 );
605             bo_addle_u32( &bo, 0 );
606             bo_addle_u32( &bo, 0 );
607             bo_addle_u32( &bo, 0 );
608             if( i_codec_extra > 0 )
609                 bo_add_mem( &bo, p_codec_extra, i_codec_extra );
610
611             if( p_input->p_fmt->i_bitrate > 50000 )
612             {
613                 p_sys->i_bitrate += p_input->p_fmt->i_bitrate;
614             }
615             else
616             {
617                 p_sys->i_bitrate += 512000;
618             }
619             if( p_sys->i_bitrate_override )
620                 p_sys->i_bitrate = p_sys->i_bitrate_override;
621             break;
622         }
623         default:
624             msg_Err(p_mux, "unhandled track type" );
625             return VLC_EGENERIC;
626     }
627
628     es_format_Copy( &tk->fmt, p_input->p_fmt );
629
630     p_sys->i_track++;
631     return VLC_SUCCESS;
632 }
633
634 /*****************************************************************************
635  * DelStream:
636  *****************************************************************************/
637 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
638 {
639     /* if bitrate ain't defined in commanline, reduce it when tracks are deleted
640      */
641     sout_mux_sys_t   *p_sys = p_mux->p_sys;
642     asf_track_t      *tk = p_input->p_sys;
643     if(!p_sys->i_bitrate_override)
644     {
645         if( tk->i_cat == AUDIO_ES )
646         {
647              if( p_input->p_fmt->i_bitrate > 24000 )
648                  p_sys->i_bitrate -= p_input->p_fmt->i_bitrate;
649              else
650                  p_sys->i_bitrate -= 128000;
651         }
652         else if(tk->i_cat == VIDEO_ES )
653         {
654              if( p_input->p_fmt->i_bitrate > 50000 )
655                  p_sys->i_bitrate -= p_input->p_fmt->i_bitrate;
656              else
657                  p_sys->i_bitrate -= 512000;
658         }
659     }
660     msg_Dbg( p_mux, "removing input" );
661     return VLC_SUCCESS;
662 }
663
664 /*****************************************************************************
665  * Mux:
666  *****************************************************************************/
667 static int Mux( sout_mux_t *p_mux )
668 {
669     sout_mux_sys_t *p_sys = p_mux->p_sys;
670
671     if( p_sys->b_write_header )
672     {
673         block_t *out = asf_header_create( p_mux, true );
674
675         out->i_flags |= BLOCK_FLAG_HEADER;
676         sout_AccessOutWrite( p_mux->p_access, out );
677
678         p_sys->b_write_header = false;
679     }
680
681     for( ;; )
682     {
683         sout_input_t  *p_input;
684         asf_track_t   *tk;
685         int           i_stream;
686         mtime_t       i_dts;
687         block_t *data;
688         block_t *pk;
689
690         if( MuxGetStream( p_mux, &i_stream, &i_dts ) )
691         {
692             /* not enough data */
693             return VLC_SUCCESS;
694         }
695
696         if( p_sys->i_dts_first < 0 )
697         {
698             p_sys->i_dts_first = i_dts;
699         }
700         if( p_sys->i_dts_last < i_dts )
701         {
702             p_sys->i_dts_last = i_dts;
703         }
704
705         p_input = p_mux->pp_inputs[i_stream];
706         tk      = (asf_track_t*)p_input->p_sys;
707
708         data = block_FifoGet( p_input->p_fifo );
709
710         /* Convert VC1 to ASF special format */
711         if( tk->i_fourcc == VLC_FOURCC( 'W', 'V', 'C', '1' ) )
712         {
713             while( data->i_buffer >= 4 &&
714                    ( data->p_buffer[0] != 0x00 || data->p_buffer[1] != 0x00 ||
715                      data->p_buffer[2] != 0x01 ||
716                      ( data->p_buffer[3] != 0x0D && data->p_buffer[3] != 0x0C ) ) )
717             {
718                 data->i_buffer--;
719                 data->p_buffer++;
720             }
721             if( data->i_buffer >= 4 )
722             {
723                 data->i_buffer -= 4;
724                 data->p_buffer += 4;
725             }
726         }
727
728         if( ( pk = asf_packet_create( p_mux, tk, data ) ) )
729         {
730             sout_AccessOutWrite( p_mux->p_access, pk );
731         }
732     }
733
734     return VLC_SUCCESS;
735 }
736
737 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
738 {
739     mtime_t i_dts;
740     int     i_stream;
741     int     i;
742
743     for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
744     {
745         sout_input_t  *p_input = p_mux->pp_inputs[i];
746         block_t *p_data;
747
748         if( block_FifoCount( p_input->p_fifo ) <= 0 )
749         {
750             if( p_input->p_fmt->i_cat == AUDIO_ES ||
751                 p_input->p_fmt->i_cat == VIDEO_ES )
752             {
753                 /* We need that audio+video fifo contain at least 1 packet */
754                 return VLC_EGENERIC;
755             }
756             /* SPU */
757             continue;
758         }
759
760         p_data = block_FifoShow( p_input->p_fifo );
761         if( i_stream == -1 || p_data->i_dts < i_dts )
762         {
763             i_stream = i;
764             i_dts    = p_data->i_dts;
765         }
766     }
767
768     *pi_stream = i_stream;
769     *pi_dts = i_dts;
770
771     return VLC_SUCCESS;
772 }
773
774 /****************************************************************************
775  * Asf header construction
776  ****************************************************************************/
777
778 /****************************************************************************
779  * Buffer out
780  ****************************************************************************/
781 static void bo_init( bo_t *p_bo, uint8_t *p_buffer, int i_size )
782 {
783     p_bo->i_buffer_size = i_size;
784     p_bo->i_buffer = 0;
785     p_bo->p_buffer = p_buffer;
786 }
787 static void bo_add_u8( bo_t *p_bo, uint8_t i )
788 {
789     if( p_bo->i_buffer < p_bo->i_buffer_size )
790     {
791         p_bo->p_buffer[p_bo->i_buffer] = i;
792     }
793     p_bo->i_buffer++;
794 }
795 static void bo_addle_u16( bo_t *p_bo, uint16_t i )
796 {
797     bo_add_u8( p_bo, i &0xff );
798     bo_add_u8( p_bo, ( ( i >> 8) &0xff ) );
799 }
800 static void bo_addle_u32( bo_t *p_bo, uint32_t i )
801 {
802     bo_addle_u16( p_bo, i &0xffff );
803     bo_addle_u16( p_bo, ( ( i >> 16) &0xffff ) );
804 }
805 static void bo_addle_u64( bo_t *p_bo, uint64_t i )
806 {
807     bo_addle_u32( p_bo, i &0xffffffff );
808     bo_addle_u32( p_bo, ( ( i >> 32) &0xffffffff ) );
809 }
810
811 static void bo_add_mem( bo_t *p_bo, uint8_t *p_mem, int i_size )
812 {
813     int i_copy = __MIN( i_size, p_bo->i_buffer_size - p_bo->i_buffer );
814
815     if( i_copy > 0 )
816     {
817         memcpy( &p_bo->p_buffer[p_bo->i_buffer], p_mem, i_copy );
818     }
819     p_bo->i_buffer += i_size;
820 }
821
822 static void bo_addle_str16( bo_t *bo, const char *str )
823 {
824     bo_addle_u16( bo, strlen( str ) + 1 );
825     for( ;; )
826     {
827         uint16_t c = (uint8_t)*str++;
828         bo_addle_u16( bo, c );
829         if( c == '\0' ) break;
830     }
831 }
832
833 static void bo_addle_str16_nosize( bo_t *bo, const char *str )
834 {
835     for( ;; )
836     {
837         uint16_t c = (uint8_t)*str++;
838         bo_addle_u16( bo, c );
839         if( c == '\0' ) break;
840     }
841 }
842
843 /****************************************************************************
844  * GUID definitions
845  ****************************************************************************/
846 static void bo_add_guid( bo_t *p_bo, const guid_t *id )
847 {
848     int i;
849     bo_addle_u32( p_bo, id->Data1 );
850     bo_addle_u16( p_bo, id->Data2 );
851     bo_addle_u16( p_bo, id->Data3 );
852     for( i = 0; i < 8; i++ )
853     {
854         bo_add_u8( p_bo, id->Data4[i] );
855     }
856 }
857
858 static const guid_t asf_object_header_guid =
859 {0x75B22630, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
860 static const guid_t asf_object_data_guid =
861 {0x75B22636, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
862 static const guid_t asf_object_file_properties_guid =
863 {0x8cabdca1, 0xa947, 0x11cf, {0x8e, 0xe4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
864 static const guid_t asf_object_stream_properties_guid =
865 {0xB7DC0791, 0xA9B7, 0x11CF, {0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
866 static const guid_t asf_object_header_extension_guid =
867 {0x5FBF03B5, 0xA92E, 0x11CF, {0x8E, 0xE3, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
868 static const guid_t asf_object_stream_type_audio =
869 {0xF8699E40, 0x5B4D, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
870 static const guid_t asf_object_stream_type_video =
871 {0xbc19efc0, 0x5B4D, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
872 static const guid_t asf_guid_audio_conceal_none =
873 {0x20FB5700, 0x5B55, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
874 static const guid_t asf_guid_audio_conceal_spread =
875 {0xBFC3CD50, 0x618F, 0x11CF, {0x8B, 0xB2, 0x00, 0xAA, 0x00, 0xB4, 0xE2, 0x20}};
876 static const guid_t asf_guid_video_conceal_none =
877 {0x20FB5700, 0x5B55, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
878 static const guid_t asf_guid_reserved_1 =
879 {0xABD3D211, 0xA9BA, 0x11cf, {0x8E, 0xE6, 0x00, 0xC0, 0x0C ,0x20, 0x53, 0x65}};
880 static const guid_t asf_object_codec_list_guid =
881 {0x86D15240, 0x311D, 0x11D0, {0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6}};
882 static const guid_t asf_object_codec_list_reserved_guid =
883 {0x86D15241, 0x311D, 0x11D0, {0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6}};
884 static const guid_t asf_object_content_description_guid =
885 {0x75B22633, 0x668E, 0x11CF, {0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c}};
886 static const guid_t asf_object_index_guid =
887 {0x33000890, 0xE5B1, 0x11CF, {0x89, 0xF4, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xCB}};
888 static const guid_t asf_object_metadata_guid =
889 {0xC5F8CBEA, 0x5BAF, 0x4877, {0x84, 0x67, 0xAA, 0x8C, 0x44, 0xFA, 0x4C, 0xCA}};
890 static const guid_t asf_object_extended_stream_properties_guid =
891 {0x14E6A5CB, 0xC672, 0x4332, {0x83, 0x99, 0xA9, 0x69, 0x52, 0x06, 0x5B, 0x5A}};
892
893 /****************************************************************************
894  * Misc
895  ****************************************************************************/
896 static void asf_chunk_add( bo_t *bo,
897                            int i_type, int i_len, int i_flags, int i_seq )
898 {
899     bo_addle_u16( bo, i_type );
900     bo_addle_u16( bo, i_len + 8 );
901     bo_addle_u32( bo, i_seq );
902     bo_addle_u16( bo, i_flags );
903     bo_addle_u16( bo, i_len + 8 );
904 }
905
906 static block_t *asf_header_create( sout_mux_t *p_mux, bool b_broadcast )
907 {
908     sout_mux_sys_t *p_sys = p_mux->p_sys;
909     asf_track_t    *tk;
910     mtime_t i_duration = 0;
911     int i_size, i_header_ext_size, i;
912     int i_ci_size, i_cm_size = 0, i_cd_size = 0;
913     block_t *out;
914     bo_t bo;
915
916     msg_Dbg( p_mux, "Asf muxer creating header" );
917
918     if( p_sys->i_dts_first > 0 )
919     {
920         i_duration = p_sys->i_dts_last - p_sys->i_dts_first;
921         if( i_duration < 0 ) i_duration = 0;
922     }
923
924     /* calculate header size */
925     i_size = 30 + 104;
926     i_ci_size = 44;
927     for( i = 0; i < p_sys->i_track; i++ )
928     {
929         i_size += 78 + p_sys->track[i].i_extra;
930         i_ci_size += 8 + 2 * strlen( p_sys->track[i].psz_name );
931         if( p_sys->track[i].i_cat == AUDIO_ES ) i_ci_size += 4;
932         else if( p_sys->track[i].i_cat == VIDEO_ES ) i_ci_size += 6;
933
934         /* Error correction data field */
935         if( p_sys->track[i].b_audio_correction ) i_size += 8;
936     }
937
938     /* size of the content description object */
939     if( *p_sys->psz_title || *p_sys->psz_author || *p_sys->psz_copyright ||
940         *p_sys->psz_comment || *p_sys->psz_rating )
941     {
942         i_cd_size = 34 + 2 * ( strlen( p_sys->psz_title ) + 1 +
943                              strlen( p_sys->psz_author ) + 1 +
944                              strlen( p_sys->psz_copyright ) + 1 +
945                              strlen( p_sys->psz_comment ) + 1 +
946                              strlen( p_sys->psz_rating ) + 1 );
947     }
948
949     i_header_ext_size = 46;
950
951     /* size of the metadata object */
952     for( i = 0; i < p_sys->i_track; i++ )
953     {
954         const asf_track_t *p_track = &p_sys->track[i];
955         if( p_track->i_cat == VIDEO_ES && p_track->fmt.video.i_aspect != 0 )
956         {
957             i_cm_size = 26 + 2 * (16 + 2 * sizeof("AspectRatio?"));
958             break;
959         }
960         if( p_track->b_extended )
961             i_header_ext_size += 88;
962
963     }
964
965     i_header_ext_size += i_cm_size;
966
967     i_size += i_ci_size + i_cd_size + i_header_ext_size ;
968
969     if( p_sys->b_asf_http )
970     {
971         out = block_New( p_mux, i_size + 50 + 12 );
972         bo_init( &bo, out->p_buffer, i_size + 50 + 12 );
973         asf_chunk_add( &bo, 0x4824, i_size + 50, 0xc00, p_sys->i_seq++ );
974     }
975     else
976     {
977         out = block_New( p_mux, i_size + 50 );
978         bo_init( &bo, out->p_buffer, i_size + 50 );
979     }
980
981     /* header object */
982     bo_add_guid ( &bo, &asf_object_header_guid );
983     bo_addle_u64( &bo, i_size );
984     bo_addle_u32( &bo, 2 + p_sys->i_track +
985                   (i_cd_size ? 1 : 0) + (i_cm_size ? 1 : 0) );
986     bo_add_u8   ( &bo, 1 );
987     bo_add_u8   ( &bo, 2 );
988
989     /* sub object */
990
991     /* file properties */
992     bo_add_guid ( &bo, &asf_object_file_properties_guid );
993     bo_addle_u64( &bo, 104 );
994     bo_add_guid ( &bo, &p_sys->fid );
995     bo_addle_u64( &bo, i_size + 50 + p_sys->i_packet_count *
996                                 p_sys->i_packet_size ); /* file size */
997     bo_addle_u64( &bo, 0 );                 /* creation date */
998     bo_addle_u64( &bo, b_broadcast ? 0xffffffffLL : p_sys->i_packet_count );
999     bo_addle_u64( &bo, i_duration * 10 );   /* play duration (100ns) */
1000     bo_addle_u64( &bo, i_duration * 10 );   /* send duration (100ns) */
1001     bo_addle_u64( &bo, p_sys->i_preroll_time ); /* preroll duration (ms) */
1002     bo_addle_u32( &bo, b_broadcast ? 0x01 : 0x02 /* seekable */ ); /* flags */
1003     bo_addle_u32( &bo, p_sys->i_packet_size );  /* packet size min */
1004     bo_addle_u32( &bo, p_sys->i_packet_size );  /* packet size max */
1005     bo_addle_u32( &bo, p_sys->i_bitrate );      /* maxbitrate */
1006
1007     /* header extension */
1008     bo_add_guid ( &bo, &asf_object_header_extension_guid );
1009     bo_addle_u64( &bo, i_header_ext_size );
1010     bo_add_guid ( &bo, &asf_guid_reserved_1 );
1011     bo_addle_u16( &bo, 6 );
1012     bo_addle_u32( &bo, i_header_ext_size - 46 );
1013
1014     /* extended stream properties */
1015     for( i = 0; i < p_sys->i_track; i++ )
1016     {
1017         const asf_track_t *p_track = &p_sys->track[i];
1018         const es_format_t *p_fmt = &p_track->fmt;
1019
1020         if( !p_track->b_extended )
1021             continue;
1022
1023         uint64_t i_avg_duration = 0;
1024         if( p_fmt->i_cat == VIDEO_ES &&
1025             p_fmt->video.i_frame_rate > 0 && p_fmt->video.i_frame_rate_base > 0 )
1026             i_avg_duration = ( INT64_C(10000000) * p_fmt->video.i_frame_rate_base +
1027                                p_fmt->video.i_frame_rate/2 ) / p_fmt->video.i_frame_rate;
1028
1029         bo_add_guid ( &bo, &asf_object_extended_stream_properties_guid );
1030         bo_addle_u64( &bo, 88 );
1031         bo_addle_u64( &bo, 0 );
1032         bo_addle_u64( &bo, 0 );
1033         bo_addle_u32( &bo, p_fmt->i_bitrate );  /* Bitrate */
1034         bo_addle_u32( &bo, 0 );                 /* Buffer size */
1035         bo_addle_u32( &bo, 0 );                 /* Initial buffer fullness */
1036         bo_addle_u32( &bo, p_fmt->i_bitrate );  /* Alternate Bitrate */
1037         bo_addle_u32( &bo, 0 );                 /* Alternate Buffer size */
1038         bo_addle_u32( &bo, 0 );                 /* Alternate Initial buffer fullness */
1039         bo_addle_u32( &bo, 0 );                 /* Maximum object size (0 = unkown) */
1040         bo_addle_u32( &bo, 0x02 );              /* Flags (seekable) */
1041         bo_addle_u16( &bo, i+1 ); /* Stream number */
1042         bo_addle_u16( &bo, 0 ); /* Stream language index */
1043         bo_addle_u64( &bo, i_avg_duration );    /* Average time per frame */
1044         bo_addle_u16( &bo, 0 ); /* Stream name count */
1045         bo_addle_u16( &bo, 0 ); /* Payload extension system count */
1046     }
1047
1048     /* metadata object (part of header extension) */
1049     if( i_cm_size )
1050     {
1051         int64_t i_num, i_den;
1052         unsigned int i_dst_num, i_dst_den;
1053
1054         for( i = 0; i < p_sys->i_track; i++ )
1055             if( p_sys->track[i].i_cat == VIDEO_ES ) break;
1056
1057         i_num = p_sys->track[i].fmt.video.i_aspect *
1058             (int64_t)p_sys->track[i].fmt.video.i_height;
1059         i_den = VOUT_ASPECT_FACTOR * p_sys->track[i].fmt.video.i_width;
1060         vlc_ureduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
1061
1062         msg_Dbg( p_mux, "pixel aspect-ratio: %i/%i", i_dst_num, i_dst_den );
1063
1064         bo_add_guid ( &bo, &asf_object_metadata_guid );
1065         bo_addle_u64( &bo, i_cm_size );
1066         bo_addle_u16( &bo, 2 ); /* description records count */
1067         /* 1st description record */
1068         bo_addle_u16( &bo, 0 ); /* reserved */
1069         bo_addle_u16( &bo, i + 1 ); /* stream number (0 for the whole file) */
1070         bo_addle_u16( &bo, 2 * sizeof("AspectRatioX") ); /* name length */
1071         bo_addle_u16( &bo, 0x3 /* DWORD */ ); /* data type */
1072         bo_addle_u32( &bo, 4 ); /* data length */
1073         bo_addle_str16_nosize( &bo, "AspectRatioX" );
1074         bo_addle_u32( &bo, i_dst_num ); /* data */
1075         /* 2nd description record */
1076         bo_addle_u16( &bo, 0 ); /* reserved */
1077         bo_addle_u16( &bo, i + 1 ); /* stream number (0 for the whole file) */
1078         bo_addle_u16( &bo, 2 * sizeof("AspectRatioY") ); /* name length */
1079         bo_addle_u16( &bo, 0x3 /* DWORD */ ); /* data type */
1080         bo_addle_u32( &bo, 4 ); /* data length */
1081         bo_addle_str16_nosize( &bo, "AspectRatioY" );
1082         bo_addle_u32( &bo, i_dst_den ); /* data */
1083     }
1084
1085     /* content description header */
1086     if( i_cd_size > 0 )
1087     {
1088         bo_add_guid ( &bo, &asf_object_content_description_guid );
1089         bo_addle_u64( &bo, i_cd_size );
1090         bo_addle_u16( &bo, 2 * strlen( p_sys->psz_title ) + 2 );
1091         bo_addle_u16( &bo, 2 * strlen( p_sys->psz_author ) + 2 );
1092         bo_addle_u16( &bo, 2 * strlen( p_sys->psz_copyright ) + 2 );
1093         bo_addle_u16( &bo, 2 * strlen( p_sys->psz_comment ) + 2 );
1094         bo_addle_u16( &bo, 2 * strlen( p_sys->psz_rating ) + 2 );
1095
1096         bo_addle_str16_nosize( &bo, p_sys->psz_title );
1097         bo_addle_str16_nosize( &bo, p_sys->psz_author );
1098         bo_addle_str16_nosize( &bo, p_sys->psz_copyright );
1099         bo_addle_str16_nosize( &bo, p_sys->psz_comment );
1100         bo_addle_str16_nosize( &bo, p_sys->psz_rating );
1101     }
1102
1103     /* stream properties */
1104     for( i = 0; i < p_sys->i_track; i++ )
1105     {
1106         tk = &p_sys->track[i];
1107
1108         bo_add_guid ( &bo, &asf_object_stream_properties_guid );
1109         bo_addle_u64( &bo, 78 + tk->i_extra + (tk->b_audio_correction ? 8:0) );
1110
1111         if( tk->i_cat == AUDIO_ES )
1112         {
1113             bo_add_guid( &bo, &asf_object_stream_type_audio );
1114             if( tk->b_audio_correction )
1115                 bo_add_guid( &bo, &asf_guid_audio_conceal_spread );
1116             else
1117                 bo_add_guid( &bo, &asf_guid_audio_conceal_none );
1118         }
1119         else if( tk->i_cat == VIDEO_ES )
1120         {
1121             bo_add_guid( &bo, &asf_object_stream_type_video );
1122             bo_add_guid( &bo, &asf_guid_video_conceal_none );
1123         }
1124         bo_addle_u64( &bo, 0 );         /* time offset */
1125         bo_addle_u32( &bo, tk->i_extra );
1126         /* correction data length */
1127         bo_addle_u32( &bo, tk->b_audio_correction ? 8 : 0 );
1128         bo_addle_u16( &bo, tk->i_id );  /* stream number */
1129         bo_addle_u32( &bo, 0 );
1130         bo_add_mem  ( &bo, tk->p_extra, tk->i_extra );
1131
1132         /* Error correction data field */
1133         if( tk->b_audio_correction )
1134         {
1135             bo_add_u8( &bo, 0x1 ); /* span */
1136             bo_addle_u16( &bo, tk->i_blockalign );  /* virtual packet length */
1137             bo_addle_u16( &bo, tk->i_blockalign );  /* virtual chunck length */
1138             bo_addle_u16( &bo, 1 );  /* silence length */
1139             bo_add_u8( &bo, 0x0 ); /* data */
1140         }
1141     }
1142
1143     /* Codec Infos */
1144     bo_add_guid ( &bo, &asf_object_codec_list_guid );
1145     bo_addle_u64( &bo, i_ci_size );
1146     bo_add_guid ( &bo, &asf_object_codec_list_reserved_guid );
1147     bo_addle_u32( &bo, p_sys->i_track );
1148     for( i = 0; i < p_sys->i_track; i++ )
1149     {
1150         tk = &p_sys->track[i];
1151
1152         if( tk->i_cat == VIDEO_ES ) bo_addle_u16( &bo, 1 /* video */ );
1153         else if( tk->i_cat == AUDIO_ES ) bo_addle_u16( &bo, 2 /* audio */ );
1154         else bo_addle_u16( &bo, 0xFFFF /* unknown */ );
1155
1156         bo_addle_str16( &bo, tk->psz_name );
1157         bo_addle_u16( &bo, 0 );
1158         if( tk->i_cat == AUDIO_ES )
1159         {
1160             bo_addle_u16( &bo, 2 );
1161             bo_addle_u16( &bo, tk->i_tag );
1162         }
1163         else if( tk->i_cat == VIDEO_ES )
1164         {
1165             bo_addle_u16( &bo, 4 );
1166             bo_add_mem  ( &bo, (uint8_t*)&tk->i_fourcc, 4 );
1167         }
1168     }
1169
1170     /* data object */
1171     bo_add_guid ( &bo, &asf_object_data_guid );
1172     bo_addle_u64( &bo, 50 + p_sys->i_packet_count * p_sys->i_packet_size );
1173     bo_add_guid ( &bo, &p_sys->fid );
1174     bo_addle_u64( &bo, p_sys->i_packet_count );
1175     bo_addle_u16( &bo, 0x101 );
1176
1177     return out;
1178 }
1179
1180 /****************************************************************************
1181  *
1182  ****************************************************************************/
1183 static block_t *asf_packet_flush( sout_mux_t *p_mux )
1184 {
1185     sout_mux_sys_t *p_sys = p_mux->p_sys;
1186     int i_pad, i_preheader = p_sys->b_asf_http ? 12 : 0;
1187     block_t *pk;
1188     bo_t bo;
1189
1190     if( !p_sys->pk ) return 0;
1191
1192     i_pad = p_sys->i_packet_size - p_sys->i_pk_used;
1193     memset( p_sys->pk->p_buffer + p_sys->i_pk_used, 0, i_pad );
1194
1195     bo_init( &bo, p_sys->pk->p_buffer, 14 + i_preheader );
1196
1197     if( p_sys->b_asf_http )
1198         asf_chunk_add( &bo, 0x4424, p_sys->i_packet_size, 0x0, p_sys->i_seq++);
1199
1200     bo_add_u8   ( &bo, 0x82 );
1201     bo_addle_u16( &bo, 0 );
1202     bo_add_u8( &bo, 0x11 );
1203     bo_add_u8( &bo, 0x5d );
1204     bo_addle_u16( &bo, i_pad );
1205     bo_addle_u32( &bo, (p_sys->i_pk_dts - p_sys->i_dts_first) / 1000 +
1206                   p_sys->i_preroll_time );
1207     bo_addle_u16( &bo, 0 /* data->i_length */ );
1208     bo_add_u8( &bo, 0x80 | p_sys->i_pk_frame );
1209
1210     pk = p_sys->pk;
1211     p_sys->pk = NULL;
1212
1213     p_sys->i_packet_count++;
1214
1215     return pk;
1216 }
1217
1218 static block_t *asf_packet_create( sout_mux_t *p_mux,
1219                                    asf_track_t *tk, block_t *data )
1220 {
1221     sout_mux_sys_t *p_sys = p_mux->p_sys;
1222
1223     int     i_data = data->i_buffer;
1224     int     i_pos  = 0;
1225     uint8_t *p_data= data->p_buffer;
1226     block_t *first = NULL, **last = &first;
1227     int     i_preheader = p_sys->b_asf_http ? 12 : 0;
1228
1229     while( i_pos < i_data )
1230     {
1231         bo_t bo;
1232         int i_payload;
1233
1234         if( p_sys->pk == NULL )
1235         {
1236             p_sys->pk = block_New( p_mux, p_sys->i_packet_size + i_preheader );
1237             /* reserve 14 bytes for the packet header */
1238             p_sys->i_pk_used = 14 + i_preheader;
1239             p_sys->i_pk_frame = 0;
1240             p_sys->i_pk_dts = data->i_dts;
1241         }
1242
1243         bo_init( &bo, &p_sys->pk->p_buffer[p_sys->i_pk_used],
1244                  p_sys->i_packet_size - p_sys->i_pk_used );
1245
1246         /* add payload (header size = 17) */
1247         i_payload = __MIN( i_data - i_pos,
1248                            p_sys->i_packet_size - p_sys->i_pk_used - 17 );
1249
1250         if( tk->b_audio_correction && p_sys->i_pk_frame && i_payload < i_data )
1251         {
1252             /* Don't know why yet but WMP doesn't like splitted WMA packets */
1253             *last = asf_packet_flush( p_mux );
1254             last  = &(*last)->p_next;
1255             continue;
1256         }
1257
1258         bo_add_u8   ( &bo, !(data->i_flags & BLOCK_FLAG_TYPE_P ||
1259                       data->i_flags & BLOCK_FLAG_TYPE_B) ?
1260                       0x80 | tk->i_id : tk->i_id );
1261         bo_add_u8   ( &bo, tk->i_sequence );
1262         bo_addle_u32( &bo, i_pos );
1263         bo_add_u8   ( &bo, 0x08 );  /* flags */
1264         bo_addle_u32( &bo, i_data );
1265         bo_addle_u32( &bo, (data->i_dts - p_sys->i_dts_first) / 1000 +
1266                       p_sys->i_preroll_time );
1267         bo_addle_u16( &bo, i_payload );
1268         bo_add_mem  ( &bo, &p_data[i_pos], i_payload );
1269         i_pos += i_payload;
1270         p_sys->i_pk_used += 17 + i_payload;
1271
1272         p_sys->i_pk_frame++;
1273
1274         if( p_sys->i_pk_used + 17 >= p_sys->i_packet_size )
1275         {
1276             /* Not enough data for another payload, flush the packet */
1277             *last = asf_packet_flush( p_mux );
1278             last  = &(*last)->p_next;
1279         }
1280     }
1281
1282     tk->i_sequence++;
1283     block_Release( data );
1284
1285     return first;
1286 }
1287
1288 static block_t *asf_stream_end_create( sout_mux_t *p_mux )
1289 {
1290     sout_mux_sys_t *p_sys = p_mux->p_sys;
1291
1292     block_t *out = NULL;
1293     bo_t bo;
1294
1295     if( p_sys->b_asf_http )
1296     {
1297         out = block_New( p_mux, 12 );
1298         bo_init( &bo, out->p_buffer, 12 );
1299         asf_chunk_add( &bo, 0x4524, 0, 0x00, p_sys->i_seq++ );
1300     }
1301     else
1302     {
1303         /* Create index */
1304         out = block_New( p_mux, 56 );
1305         bo_init( &bo, out->p_buffer, 56 );
1306         bo_add_guid ( &bo, &asf_object_index_guid );
1307         bo_addle_u64( &bo, 56 );
1308         bo_add_guid ( &bo, &p_sys->fid );
1309         bo_addle_u64( &bo, 10000000 );
1310         bo_addle_u32( &bo, 5 );
1311         bo_addle_u32( &bo, 0 );
1312     }
1313
1314     return out;
1315 }