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