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