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