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