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