]> git.sesse.net Git - vlc/blob - modules/mux/asf.c
Note to self: test compilation before applying patches.
[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 %d", 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     VLC_UNUSED(p_input);
601     msg_Dbg( p_mux, "removing input" );
602     return VLC_SUCCESS;
603 }
604
605 /*****************************************************************************
606  * Mux:
607  *****************************************************************************/
608 static int Mux( sout_mux_t *p_mux )
609 {
610     sout_mux_sys_t *p_sys = p_mux->p_sys;
611
612     if( p_sys->b_write_header )
613     {
614         block_t *out = asf_header_create( p_mux, true );
615
616         out->i_flags |= BLOCK_FLAG_HEADER;
617         sout_AccessOutWrite( p_mux->p_access, out );
618
619         p_sys->b_write_header = false;
620     }
621
622     for( ;; )
623     {
624         sout_input_t  *p_input;
625         asf_track_t   *tk;
626         int           i_stream;
627         mtime_t       i_dts;
628         block_t *data;
629         block_t *pk;
630
631         if( MuxGetStream( p_mux, &i_stream, &i_dts ) )
632         {
633             /* not enough data */
634             return VLC_SUCCESS;
635         }
636
637         if( p_sys->i_dts_first < 0 )
638         {
639             p_sys->i_dts_first = i_dts;
640         }
641         if( p_sys->i_dts_last < i_dts )
642         {
643             p_sys->i_dts_last = i_dts;
644         }
645
646         p_input = p_mux->pp_inputs[i_stream];
647         tk      = (asf_track_t*)p_input->p_sys;
648
649         data = block_FifoGet( p_input->p_fifo );
650
651         if( ( pk = asf_packet_create( p_mux, tk, data ) ) )
652         {
653             sout_AccessOutWrite( p_mux->p_access, pk );
654         }
655     }
656
657     return VLC_SUCCESS;
658 }
659
660 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
661 {
662     mtime_t i_dts;
663     int     i_stream;
664     int     i;
665
666     for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
667     {
668         sout_input_t  *p_input = p_mux->pp_inputs[i];
669         block_t *p_data;
670
671         if( block_FifoCount( p_input->p_fifo ) <= 0 )
672         {
673             if( p_input->p_fmt->i_cat == AUDIO_ES ||
674                 p_input->p_fmt->i_cat == VIDEO_ES )
675             {
676                 /* We need that audio+video fifo contain at least 1 packet */
677                 return VLC_EGENERIC;
678             }
679             /* SPU */
680             continue;
681         }
682
683         p_data = block_FifoShow( p_input->p_fifo );
684         if( i_stream == -1 || p_data->i_dts < i_dts )
685         {
686             i_stream = i;
687             i_dts    = p_data->i_dts;
688         }
689     }
690
691     *pi_stream = i_stream;
692     *pi_dts = i_dts;
693
694     return VLC_SUCCESS;
695 }
696
697 /****************************************************************************
698  * Asf header construction
699  ****************************************************************************/
700
701 /****************************************************************************
702  * Buffer out
703  ****************************************************************************/
704 static void bo_init( bo_t *p_bo, uint8_t *p_buffer, int i_size )
705 {
706     p_bo->i_buffer_size = i_size;
707     p_bo->i_buffer = 0;
708     p_bo->p_buffer = p_buffer;
709 }
710 static void bo_add_u8( bo_t *p_bo, uint8_t i )
711 {
712     if( p_bo->i_buffer < p_bo->i_buffer_size )
713     {
714         p_bo->p_buffer[p_bo->i_buffer] = i;
715     }
716     p_bo->i_buffer++;
717 }
718 static void bo_addle_u16( bo_t *p_bo, uint16_t i )
719 {
720     bo_add_u8( p_bo, i &0xff );
721     bo_add_u8( p_bo, ( ( i >> 8) &0xff ) );
722 }
723 static void bo_addle_u32( bo_t *p_bo, uint32_t i )
724 {
725     bo_addle_u16( p_bo, i &0xffff );
726     bo_addle_u16( p_bo, ( ( i >> 16) &0xffff ) );
727 }
728 static void bo_addle_u64( bo_t *p_bo, uint64_t i )
729 {
730     bo_addle_u32( p_bo, i &0xffffffff );
731     bo_addle_u32( p_bo, ( ( i >> 32) &0xffffffff ) );
732 }
733
734 static void bo_add_mem( bo_t *p_bo, uint8_t *p_mem, int i_size )
735 {
736     int i_copy = __MIN( i_size, p_bo->i_buffer_size - p_bo->i_buffer );
737
738     if( i_copy > 0 )
739     {
740         memcpy( &p_bo->p_buffer[p_bo->i_buffer], p_mem, i_copy );
741     }
742     p_bo->i_buffer += i_size;
743 }
744
745 static void bo_addle_str16( bo_t *bo, const char *str )
746 {
747     bo_addle_u16( bo, strlen( str ) + 1 );
748     for( ;; )
749     {
750         uint16_t c = (uint8_t)*str++;
751         bo_addle_u16( bo, c );
752         if( c == '\0' ) break;
753     }
754 }
755
756 static void bo_addle_str16_nosize( bo_t *bo, const char *str )
757 {
758     for( ;; )
759     {
760         uint16_t c = (uint8_t)*str++;
761         bo_addle_u16( bo, c );
762         if( c == '\0' ) break;
763     }
764 }
765
766 /****************************************************************************
767  * GUID definitions
768  ****************************************************************************/
769 static void bo_add_guid( bo_t *p_bo, const guid_t *id )
770 {
771     int i;
772     bo_addle_u32( p_bo, id->Data1 );
773     bo_addle_u16( p_bo, id->Data2 );
774     bo_addle_u16( p_bo, id->Data3 );
775     for( i = 0; i < 8; i++ )
776     {
777         bo_add_u8( p_bo, id->Data4[i] );
778     }
779 }
780
781 static const guid_t asf_object_header_guid =
782 {0x75B22630, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
783 static const guid_t asf_object_data_guid =
784 {0x75B22636, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
785 static const guid_t asf_object_file_properties_guid =
786 {0x8cabdca1, 0xa947, 0x11cf, {0x8e, 0xe4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
787 static const guid_t asf_object_stream_properties_guid =
788 {0xB7DC0791, 0xA9B7, 0x11CF, {0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
789 static const guid_t asf_object_header_extension_guid =
790 {0x5FBF03B5, 0xA92E, 0x11CF, {0x8E, 0xE3, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
791 static const guid_t asf_object_stream_type_audio =
792 {0xF8699E40, 0x5B4D, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
793 static const guid_t asf_object_stream_type_video =
794 {0xbc19efc0, 0x5B4D, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
795 static const guid_t asf_guid_audio_conceal_none =
796 {0x20FB5700, 0x5B55, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
797 static const guid_t asf_guid_audio_conceal_spread =
798 {0xBFC3CD50, 0x618F, 0x11CF, {0x8B, 0xB2, 0x00, 0xAA, 0x00, 0xB4, 0xE2, 0x20}};
799 static const guid_t asf_guid_video_conceal_none =
800 {0x20FB5700, 0x5B55, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
801 static const guid_t asf_guid_reserved_1 =
802 {0xABD3D211, 0xA9BA, 0x11cf, {0x8E, 0xE6, 0x00, 0xC0, 0x0C ,0x20, 0x53, 0x65}};
803 static const guid_t asf_object_codec_list_guid =
804 {0x86D15240, 0x311D, 0x11D0, {0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6}};
805 static const guid_t asf_object_codec_list_reserved_guid =
806 {0x86D15241, 0x311D, 0x11D0, {0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6}};
807 static const guid_t asf_object_content_description_guid =
808 {0x75B22633, 0x668E, 0x11CF, {0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c}};
809 static const guid_t asf_object_index_guid =
810 {0x33000890, 0xE5B1, 0x11CF, {0x89, 0xF4, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xCB}};
811 static const guid_t asf_object_metadata_guid =
812 {0xC5F8CBEA, 0x5BAF, 0x4877, {0x84, 0x67, 0xAA, 0x8C, 0x44, 0xFA, 0x4C, 0xCA}};
813
814 /****************************************************************************
815  * Misc
816  ****************************************************************************/
817 static void asf_chunk_add( bo_t *bo,
818                            int i_type, int i_len, int i_flags, int i_seq )
819 {
820     bo_addle_u16( bo, i_type );
821     bo_addle_u16( bo, i_len + 8 );
822     bo_addle_u32( bo, i_seq );
823     bo_addle_u16( bo, i_flags );
824     bo_addle_u16( bo, i_len + 8 );
825 }
826
827 static block_t *asf_header_create( sout_mux_t *p_mux, bool b_broadcast )
828 {
829     sout_mux_sys_t *p_sys = p_mux->p_sys;
830     asf_track_t    *tk;
831     mtime_t i_duration = 0;
832     int i_size, i_header_ext_size, i;
833     int i_ci_size, i_cm_size = 0, i_cd_size = 0;
834     block_t *out;
835     bo_t bo;
836
837     msg_Dbg( p_mux, "Asf muxer creating header" );
838
839     if( p_sys->i_dts_first > 0 )
840     {
841         i_duration = p_sys->i_dts_last - p_sys->i_dts_first;
842         if( i_duration < 0 ) i_duration = 0;
843     }
844
845     /* calculate header size */
846     i_size = 30 + 104;
847     i_ci_size = 44;
848     for( i = 0; i < p_sys->i_track; i++ )
849     {
850         i_size += 78 + p_sys->track[i].i_extra;
851         i_ci_size += 8 + 2 * strlen( p_sys->track[i].psz_name );
852         if( p_sys->track[i].i_cat == AUDIO_ES ) i_ci_size += 4;
853         else if( p_sys->track[i].i_cat == VIDEO_ES ) i_ci_size += 6;
854
855         /* Error correction data field */
856         if( p_sys->track[i].b_audio_correction ) i_size += 8;
857     }
858
859     /* size of the content description object */
860     if( *p_sys->psz_title || *p_sys->psz_author || *p_sys->psz_copyright ||
861         *p_sys->psz_comment || *p_sys->psz_rating )
862     {
863         i_cd_size = 34 + 2 * ( strlen( p_sys->psz_title ) + 1 +
864                              strlen( p_sys->psz_author ) + 1 +
865                              strlen( p_sys->psz_copyright ) + 1 +
866                              strlen( p_sys->psz_comment ) + 1 +
867                              strlen( p_sys->psz_rating ) + 1 );
868     }
869
870     /* size of the metadata object */
871     for( i = 0; i < p_sys->i_track; i++ )
872     {
873         if( p_sys->track[i].i_cat == VIDEO_ES )
874         {
875             i_cm_size = 26 + 2 * (16 + 2 * sizeof("AspectRatio?"));
876             break;
877         }
878     }
879
880     i_header_ext_size = i_cm_size ? i_cm_size + 46 : 0;
881     i_size += i_ci_size + i_cd_size + i_header_ext_size ;
882
883     if( p_sys->b_asf_http )
884     {
885         out = block_New( p_mux, i_size + 50 + 12 );
886         bo_init( &bo, out->p_buffer, i_size + 50 + 12 );
887         asf_chunk_add( &bo, 0x4824, i_size + 50, 0xc00, p_sys->i_seq++ );
888     }
889     else
890     {
891         out = block_New( p_mux, i_size + 50 );
892         bo_init( &bo, out->p_buffer, i_size + 50 );
893     }
894
895     /* header object */
896     bo_add_guid ( &bo, &asf_object_header_guid );
897     bo_addle_u64( &bo, i_size );
898     bo_addle_u32( &bo, 2 + p_sys->i_track +
899                   (i_cd_size ? 1 : 0) + (i_cm_size ? 1 : 0) );
900     bo_add_u8   ( &bo, 1 );
901     bo_add_u8   ( &bo, 2 );
902
903     /* sub object */
904
905     /* file properties */
906     bo_add_guid ( &bo, &asf_object_file_properties_guid );
907     bo_addle_u64( &bo, 104 );
908     bo_add_guid ( &bo, &p_sys->fid );
909     bo_addle_u64( &bo, i_size + 50 + p_sys->i_packet_count *
910                                 p_sys->i_packet_size ); /* file size */
911     bo_addle_u64( &bo, 0 );                 /* creation date */
912     bo_addle_u64( &bo, b_broadcast ? 0xffffffffLL : p_sys->i_packet_count );
913     bo_addle_u64( &bo, i_duration * 10 );   /* play duration (100ns) */
914     bo_addle_u64( &bo, i_duration * 10 );   /* send duration (100ns) */
915     bo_addle_u64( &bo, p_sys->i_preroll_time ); /* preroll duration (ms) */
916     bo_addle_u32( &bo, b_broadcast ? 0x01 : 0x02 /* seekable */ ); /* flags */
917     bo_addle_u32( &bo, p_sys->i_packet_size );  /* packet size min */
918     bo_addle_u32( &bo, p_sys->i_packet_size );  /* packet size max */
919     bo_addle_u32( &bo, p_sys->i_bitrate );      /* maxbitrate */
920
921     /* header extension */
922     if( i_header_ext_size )
923     {
924         bo_add_guid ( &bo, &asf_object_header_extension_guid );
925         bo_addle_u64( &bo, i_header_ext_size );
926         bo_add_guid ( &bo, &asf_guid_reserved_1 );
927         bo_addle_u16( &bo, 6 );
928         bo_addle_u32( &bo, i_header_ext_size - 46 );
929     }
930
931     /* metadata object (part of header extension) */
932     if( i_cm_size )
933     {
934         int64_t i_num, i_den;
935         unsigned int i_dst_num, i_dst_den;
936
937         for( i = 0; i < p_sys->i_track; i++ )
938             if( p_sys->track[i].i_cat == VIDEO_ES ) break;
939
940         i_num = p_sys->track[i].fmt.video.i_aspect *
941             (int64_t)p_sys->track[i].fmt.video.i_height;
942         i_den = VOUT_ASPECT_FACTOR * p_sys->track[i].fmt.video.i_width;
943         vlc_ureduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
944
945         msg_Dbg( p_mux, "pixel aspect-ratio: %i/%i", i_dst_num, i_dst_den );
946
947         bo_add_guid ( &bo, &asf_object_metadata_guid );
948         bo_addle_u64( &bo, i_cm_size );
949         bo_addle_u16( &bo, 2 ); /* description records count */
950         /* 1st description record */
951         bo_addle_u16( &bo, 0 ); /* reserved */
952         bo_addle_u16( &bo, i + 1 ); /* stream number (0 for the whole file) */
953         bo_addle_u16( &bo, 2 * sizeof("AspectRatioX") ); /* name length */
954         bo_addle_u16( &bo, 0x3 /* DWORD */ ); /* data type */
955         bo_addle_u32( &bo, 4 ); /* data length */
956         bo_addle_str16_nosize( &bo, "AspectRatioX" );
957         bo_addle_u32( &bo, i_dst_num ); /* data */
958         /* 2nd description record */
959         bo_addle_u16( &bo, 0 ); /* reserved */
960         bo_addle_u16( &bo, i + 1 ); /* stream number (0 for the whole file) */
961         bo_addle_u16( &bo, 2 * sizeof("AspectRatioY") ); /* name length */
962         bo_addle_u16( &bo, 0x3 /* DWORD */ ); /* data type */
963         bo_addle_u32( &bo, 4 ); /* data length */
964         bo_addle_str16_nosize( &bo, "AspectRatioY" );
965         bo_addle_u32( &bo, i_dst_den ); /* data */
966     }
967
968     /* content description header */
969     if( i_cd_size > 0 )
970     {
971         bo_add_guid ( &bo, &asf_object_content_description_guid );
972         bo_addle_u64( &bo, i_cd_size );
973         bo_addle_u16( &bo, 2 * strlen( p_sys->psz_title ) + 2 );
974         bo_addle_u16( &bo, 2 * strlen( p_sys->psz_author ) + 2 );
975         bo_addle_u16( &bo, 2 * strlen( p_sys->psz_copyright ) + 2 );
976         bo_addle_u16( &bo, 2 * strlen( p_sys->psz_comment ) + 2 );
977         bo_addle_u16( &bo, 2 * strlen( p_sys->psz_rating ) + 2 );
978
979         bo_addle_str16_nosize( &bo, p_sys->psz_title );
980         bo_addle_str16_nosize( &bo, p_sys->psz_author );
981         bo_addle_str16_nosize( &bo, p_sys->psz_copyright );
982         bo_addle_str16_nosize( &bo, p_sys->psz_comment );
983         bo_addle_str16_nosize( &bo, p_sys->psz_rating );
984     }
985
986     /* stream properties */
987     for( i = 0; i < p_sys->i_track; i++ )
988     {
989         tk = &p_sys->track[i];
990
991         bo_add_guid ( &bo, &asf_object_stream_properties_guid );
992         bo_addle_u64( &bo, 78 + tk->i_extra + (tk->b_audio_correction ? 8:0) );
993
994         if( tk->i_cat == AUDIO_ES )
995         {
996             bo_add_guid( &bo, &asf_object_stream_type_audio );
997             if( tk->b_audio_correction )
998                 bo_add_guid( &bo, &asf_guid_audio_conceal_spread );
999             else
1000                 bo_add_guid( &bo, &asf_guid_audio_conceal_none );
1001         }
1002         else if( tk->i_cat == VIDEO_ES )
1003         {
1004             bo_add_guid( &bo, &asf_object_stream_type_video );
1005             bo_add_guid( &bo, &asf_guid_video_conceal_none );
1006         }
1007         bo_addle_u64( &bo, 0 );         /* time offset */
1008         bo_addle_u32( &bo, tk->i_extra );
1009         /* correction data length */
1010         bo_addle_u32( &bo, tk->b_audio_correction ? 8 : 0 );
1011         bo_addle_u16( &bo, tk->i_id );  /* stream number */
1012         bo_addle_u32( &bo, 0 );
1013         bo_add_mem  ( &bo, tk->p_extra, tk->i_extra );
1014
1015         /* Error correction data field */
1016         if( tk->b_audio_correction )
1017         {
1018             bo_add_u8( &bo, 0x1 ); /* span */
1019             bo_addle_u16( &bo, tk->i_blockalign );  /* virtual packet length */
1020             bo_addle_u16( &bo, tk->i_blockalign );  /* virtual chunck length */
1021             bo_addle_u16( &bo, 1 );  /* silence length */
1022             bo_add_u8( &bo, 0x0 ); /* data */
1023         }
1024     }
1025
1026     /* Codec Infos */
1027     bo_add_guid ( &bo, &asf_object_codec_list_guid );
1028     bo_addle_u64( &bo, i_ci_size );
1029     bo_add_guid ( &bo, &asf_object_codec_list_reserved_guid );
1030     bo_addle_u32( &bo, p_sys->i_track );
1031     for( i = 0; i < p_sys->i_track; i++ )
1032     {
1033         tk = &p_sys->track[i];
1034
1035         if( tk->i_cat == VIDEO_ES ) bo_addle_u16( &bo, 1 /* video */ );
1036         else if( tk->i_cat == AUDIO_ES ) bo_addle_u16( &bo, 2 /* audio */ );
1037         else bo_addle_u16( &bo, 0xFFFF /* unknown */ );
1038
1039         bo_addle_str16( &bo, tk->psz_name );
1040         bo_addle_u16( &bo, 0 );
1041         if( tk->i_cat == AUDIO_ES )
1042         {
1043             bo_addle_u16( &bo, 2 );
1044             bo_addle_u16( &bo, tk->i_tag );
1045         }
1046         else if( tk->i_cat == VIDEO_ES )
1047         {
1048             bo_addle_u16( &bo, 4 );
1049             bo_add_mem  ( &bo, (uint8_t*)&tk->i_fourcc, 4 );
1050         }
1051     }
1052
1053     /* data object */
1054     bo_add_guid ( &bo, &asf_object_data_guid );
1055     bo_addle_u64( &bo, 50 + p_sys->i_packet_count * p_sys->i_packet_size );
1056     bo_add_guid ( &bo, &p_sys->fid );
1057     bo_addle_u64( &bo, p_sys->i_packet_count );
1058     bo_addle_u16( &bo, 0x101 );
1059
1060     return out;
1061 }
1062
1063 /****************************************************************************
1064  *
1065  ****************************************************************************/
1066 static block_t *asf_packet_flush( sout_mux_t *p_mux )
1067 {
1068     sout_mux_sys_t *p_sys = p_mux->p_sys;
1069     int i_pad, i_preheader = p_sys->b_asf_http ? 12 : 0;
1070     block_t *pk;
1071     bo_t bo;
1072
1073     if( !p_sys->pk ) return 0;
1074
1075     i_pad = p_sys->i_packet_size - p_sys->i_pk_used;
1076     memset( p_sys->pk->p_buffer + p_sys->i_pk_used, 0, i_pad );
1077
1078     bo_init( &bo, p_sys->pk->p_buffer, 14 + i_preheader );
1079
1080     if( p_sys->b_asf_http )
1081         asf_chunk_add( &bo, 0x4424, p_sys->i_packet_size, 0x0, p_sys->i_seq++);
1082
1083     bo_add_u8   ( &bo, 0x82 );
1084     bo_addle_u16( &bo, 0 );
1085     bo_add_u8( &bo, 0x11 );
1086     bo_add_u8( &bo, 0x5d );
1087     bo_addle_u16( &bo, i_pad );
1088     bo_addle_u32( &bo, (p_sys->i_pk_dts - p_sys->i_dts_first) / 1000 +
1089                   p_sys->i_preroll_time );
1090     bo_addle_u16( &bo, 0 /* data->i_length */ );
1091     bo_add_u8( &bo, 0x80 | p_sys->i_pk_frame );
1092
1093     pk = p_sys->pk;
1094     p_sys->pk = NULL;
1095
1096     p_sys->i_packet_count++;
1097
1098     return pk;
1099 }
1100
1101 static block_t *asf_packet_create( sout_mux_t *p_mux,
1102                                    asf_track_t *tk, block_t *data )
1103 {
1104     sout_mux_sys_t *p_sys = p_mux->p_sys;
1105
1106     int     i_data = data->i_buffer;
1107     int     i_pos  = 0;
1108     uint8_t *p_data= data->p_buffer;
1109     block_t *first = NULL, **last = &first;
1110     int     i_preheader = p_sys->b_asf_http ? 12 : 0;
1111
1112     while( i_pos < i_data )
1113     {
1114         bo_t bo;
1115         int i_payload;
1116
1117         if( p_sys->pk == NULL )
1118         {
1119             p_sys->pk = block_New( p_mux, p_sys->i_packet_size + i_preheader );
1120             /* reserve 14 bytes for the packet header */
1121             p_sys->i_pk_used = 14 + i_preheader;
1122             p_sys->i_pk_frame = 0;
1123             p_sys->i_pk_dts = data->i_dts;
1124         }
1125
1126         bo_init( &bo, &p_sys->pk->p_buffer[p_sys->i_pk_used],
1127                  p_sys->i_packet_size - p_sys->i_pk_used );
1128
1129         /* add payload (header size = 17) */
1130         i_payload = __MIN( i_data - i_pos,
1131                            p_sys->i_packet_size - p_sys->i_pk_used - 17 );
1132
1133         if( tk->b_audio_correction && p_sys->i_pk_frame && i_payload < i_data )
1134         {
1135             /* Don't know why yet but WMP doesn't like splitted WMA packets */
1136             *last = asf_packet_flush( p_mux );
1137             last  = &(*last)->p_next;
1138             continue;
1139         }
1140
1141         bo_add_u8   ( &bo, !(data->i_flags & BLOCK_FLAG_TYPE_P ||
1142                       data->i_flags & BLOCK_FLAG_TYPE_B) ?
1143                       0x80 | tk->i_id : tk->i_id );
1144         bo_add_u8   ( &bo, tk->i_sequence );
1145         bo_addle_u32( &bo, i_pos );
1146         bo_add_u8   ( &bo, 0x08 );  /* flags */
1147         bo_addle_u32( &bo, i_data );
1148         bo_addle_u32( &bo, (data->i_dts - p_sys->i_dts_first) / 1000 +
1149                       p_sys->i_preroll_time );
1150         bo_addle_u16( &bo, i_payload );
1151         bo_add_mem  ( &bo, &p_data[i_pos], i_payload );
1152         i_pos += i_payload;
1153         p_sys->i_pk_used += 17 + i_payload;
1154
1155         p_sys->i_pk_frame++;
1156
1157         if( p_sys->i_pk_used + 17 >= p_sys->i_packet_size )
1158         {
1159             /* Not enough data for another payload, flush the packet */
1160             *last = asf_packet_flush( p_mux );
1161             last  = &(*last)->p_next;
1162         }
1163     }
1164
1165     tk->i_sequence++;
1166     block_Release( data );
1167
1168     return first;
1169 }
1170
1171 static block_t *asf_stream_end_create( sout_mux_t *p_mux )
1172 {
1173     sout_mux_sys_t *p_sys = p_mux->p_sys;
1174
1175     block_t *out = NULL;
1176     bo_t bo;
1177
1178     if( p_sys->b_asf_http )
1179     {
1180         out = block_New( p_mux, 12 );
1181         bo_init( &bo, out->p_buffer, 12 );
1182         asf_chunk_add( &bo, 0x4524, 0, 0x00, p_sys->i_seq++ );
1183     }
1184     else
1185     {
1186         /* Create index */
1187         out = block_New( p_mux, 56 );
1188         bo_init( &bo, out->p_buffer, 56 );
1189         bo_add_guid ( &bo, &asf_object_index_guid );
1190         bo_addle_u64( &bo, 56 );
1191         bo_add_guid ( &bo, &p_sys->fid );
1192         bo_addle_u64( &bo, 10000000 );
1193         bo_addle_u32( &bo, 5 );
1194         bo_addle_u32( &bo, 0 );
1195     }
1196
1197     return out;
1198 }