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