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