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