1 /*****************************************************************************
2 * asf.c: asf muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2003-2004, 2006 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
38 #include <vlc_block.h>
39 #include <vlc_codecs.h>
40 #include <vlc_arrays.h>
43 #include "../demux/asf/libasf_guid.h"
45 #define MAX_ASF_TRACKS 128
46 #define ASF_DATA_PACKET_SIZE 4096 // deprecated -- added sout-asf-packet-size
48 /*****************************************************************************
50 *****************************************************************************/
51 static int Open ( vlc_object_t
* );
52 static void Close ( vlc_object_t
* );
54 #define SOUT_CFG_PREFIX "sout-asf-"
56 #define TITLE_TEXT N_("Title")
57 #define TITLE_LONGTEXT N_("Title to put in ASF comments." )
58 #define AUTHOR_TEXT N_("Author")
59 #define AUTHOR_LONGTEXT N_("Author to put in ASF comments." )
60 #define COPYRIGHT_TEXT N_("Copyright")
61 #define COPYRIGHT_LONGTEXT N_("Copyright string to put in ASF comments." )
62 #define COMMENT_TEXT N_("Comment")
63 #define COMMENT_LONGTEXT N_("Comment to put in ASF comments." )
64 #define RATING_TEXT N_("Rating")
65 #define RATING_LONGTEXT N_("\"Rating\" to put in ASF comments." )
66 #define PACKETSIZE_TEXT N_("Packet Size")
67 #define PACKETSIZE_LONGTEXT N_("ASF packet size -- default is 4096 bytes")
68 #define BITRATE_TEXT N_("Bitrate override")
69 #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")
73 set_description( N_("ASF muxer") )
74 set_category( CAT_SOUT
)
75 set_subcategory( SUBCAT_SOUT_MUX
)
76 set_shortname( "ASF" )
78 set_capability( "sout mux", 5 )
79 add_shortcut( "asf", "asfh" )
80 set_callbacks( Open
, Close
)
82 add_string( SOUT_CFG_PREFIX
"title", "", TITLE_TEXT
, TITLE_LONGTEXT
,
84 add_string( SOUT_CFG_PREFIX
"author", "", AUTHOR_TEXT
,
85 AUTHOR_LONGTEXT
, true )
86 add_string( SOUT_CFG_PREFIX
"copyright","", COPYRIGHT_TEXT
,
87 COPYRIGHT_LONGTEXT
, true )
88 add_string( SOUT_CFG_PREFIX
"comment", "", COMMENT_TEXT
,
89 COMMENT_LONGTEXT
, true )
90 add_string( SOUT_CFG_PREFIX
"rating", "", RATING_TEXT
,
91 RATING_LONGTEXT
, true )
92 add_integer( SOUT_CFG_PREFIX
"packet-size", 4096, PACKETSIZE_TEXT
,
93 PACKETSIZE_LONGTEXT
, true )
94 add_integer( SOUT_CFG_PREFIX
"bitrate-override", 0, BITRATE_TEXT
,
95 BITRATE_LONGTEXT
, true )
99 /*****************************************************************************
101 *****************************************************************************/
102 static const char *const ppsz_sout_options
[] = {
103 "title", "author", "copyright", "comment", "rating", NULL
106 static int Control ( sout_mux_t
*, int, va_list );
107 static int AddStream( sout_mux_t
*, sout_input_t
* );
108 static void DelStream( sout_mux_t
*, sout_input_t
* );
109 static int Mux ( sout_mux_t
* );
114 enum es_format_category_e i_cat
;
116 /* codec information */
117 uint16_t i_tag
; /* for audio */
118 vlc_fourcc_t i_fourcc
; /* for video */
119 const char *psz_name
; /* codec name */
120 int i_blockalign
; /* for audio only */
121 bool b_audio_correction
;
133 struct sout_mux_sys_t
135 guid_t fid
; /* file id */
137 int64_t i_packet_count
;
140 mtime_t i_preroll_time
;
142 int64_t i_bitrate_override
;
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
* );
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 );
184 static void bo_addle_str16( bo_t
*, const char * );
186 /*****************************************************************************
188 *****************************************************************************/
189 static int Open( vlc_object_t
*p_this
)
191 sout_mux_t
*p_mux
= (sout_mux_t
*)p_this
;
192 sout_mux_sys_t
*p_sys
;
194 msg_Dbg( p_mux
, "asf muxer opened" );
195 config_ChainParse( p_mux
, SOUT_CFG_PREFIX
, ppsz_sout_options
, p_mux
->p_cfg
);
197 p_mux
->pf_control
= Control
;
198 p_mux
->pf_addstream
= AddStream
;
199 p_mux
->pf_delstream
= DelStream
;
202 p_mux
->p_sys
= p_sys
= malloc( sizeof( sout_mux_sys_t
) );
205 p_sys
->b_asf_http
= p_mux
->psz_mux
&& !strcmp( p_mux
->psz_mux
, "asfh" );
206 if( p_sys
->b_asf_http
)
208 msg_Dbg( p_mux
, "creating asf stream to be used with mmsh" );
211 p_sys
->i_pk_used
= 0;
212 p_sys
->i_pk_frame
= 0;
214 p_sys
->i_dts_last
= VLC_TS_INVALID
;
215 p_sys
->i_preroll_time
= 2000;
216 p_sys
->i_bitrate
= 0;
217 p_sys
->i_bitrate_override
= 0;
219 vlc_array_init( &p_sys
->tracks
);
221 p_sys
->b_write_header
= true;
222 p_sys
->i_packet_size
= var_InheritInteger( p_mux
, "sout-asf-packet-size" );
223 p_sys
->i_bitrate_override
= var_InheritInteger( p_mux
, "sout-asf-bitrate-override" );
224 msg_Dbg( p_mux
, "Packet size %d", p_sys
->i_packet_size
);
225 if (p_sys
->i_bitrate_override
)
226 msg_Dbg( p_mux
, "Bitrate override %"PRId64
, p_sys
->i_bitrate_override
);
227 p_sys
->i_packet_count
= 0;
229 /* Generate a random fid */
230 p_sys
->fid
.Data1
= 0xbabac001;
231 vlc_rand_bytes(&p_sys
->fid
.Data2
, sizeof(p_sys
->fid
.Data2
));
232 vlc_rand_bytes(&p_sys
->fid
.Data3
, sizeof(p_sys
->fid
.Data3
));
233 vlc_rand_bytes(p_sys
->fid
.Data4
, sizeof(p_sys
->fid
.Data4
));
236 p_sys
->psz_title
= var_GetString( p_mux
, SOUT_CFG_PREFIX
"title" );
237 p_sys
->psz_author
= var_GetString( p_mux
, SOUT_CFG_PREFIX
"author" );
238 p_sys
->psz_copyright
= var_GetString( p_mux
, SOUT_CFG_PREFIX
"copyright" );
239 p_sys
->psz_comment
= var_GetString( p_mux
, SOUT_CFG_PREFIX
"comment" );
240 p_sys
->psz_rating
= var_GetString( p_mux
, SOUT_CFG_PREFIX
"rating" );
242 msg_Dbg( p_mux
, "meta data: title='%s', author='%s', copyright='%s', "
243 "comment='%s', rating='%s'",
244 p_sys
->psz_title
, p_sys
->psz_author
, p_sys
->psz_copyright
,
245 p_sys
->psz_comment
, p_sys
->psz_rating
);
250 /*****************************************************************************
252 *****************************************************************************/
253 static void Close( vlc_object_t
* p_this
)
255 sout_mux_t
*p_mux
= (sout_mux_t
*)p_this
;
256 sout_mux_sys_t
*p_sys
= p_mux
->p_sys
;
259 msg_Dbg( p_mux
, "Asf muxer closed" );
261 /* Flush last packet if any */
262 if( (out
= asf_packet_flush( p_mux
) ) )
264 sout_AccessOutWrite( p_mux
->p_access
, out
);
267 if( ( out
= asf_stream_end_create( p_mux
) ) )
269 sout_AccessOutWrite( p_mux
->p_access
, out
);
273 if( sout_AccessOutSeek( p_mux
->p_access
, 0 ) == VLC_SUCCESS
)
275 out
= asf_header_create( p_mux
, false );
276 sout_AccessOutWrite( p_mux
->p_access
, out
);
280 for( size_t i
= 0; i
< vlc_array_count( &p_sys
->tracks
); i
++ )
282 asf_track_t
*track
= vlc_array_item_at_index( &p_sys
->tracks
, i
);
283 free( track
->p_extra
);
284 es_format_Clean( &track
->fmt
);
288 vlc_array_clear( &p_sys
->tracks
);
289 free( p_sys
->psz_title
);
290 free( p_sys
->psz_author
);
291 free( p_sys
->psz_copyright
);
292 free( p_sys
->psz_comment
);
293 free( p_sys
->psz_rating
);
297 /*****************************************************************************
299 *****************************************************************************/
300 static int Control( sout_mux_t
*p_mux
, int i_query
, va_list args
)
302 sout_mux_sys_t
*p_sys
= p_mux
->p_sys
;
308 case MUX_CAN_ADD_STREAM_WHILE_MUXING
:
309 pb_bool
= va_arg( args
, bool * );
310 if( p_sys
->b_asf_http
) *pb_bool
= true;
311 else *pb_bool
= false;
314 case MUX_GET_ADD_STREAM_WAIT
:
315 pb_bool
= va_arg( args
, bool * );
320 ppsz
= va_arg( args
, char ** );
321 if( p_sys
->b_asf_http
)
322 *ppsz
= strdup( "video/x-ms-asf-stream" );
324 *ppsz
= strdup( "video/x-ms-asf" );
332 /*****************************************************************************
334 *****************************************************************************/
335 static int AddStream( sout_mux_t
*p_mux
, sout_input_t
*p_input
)
337 sout_mux_sys_t
*p_sys
= p_mux
->p_sys
;
341 msg_Dbg( p_mux
, "adding input" );
342 if( vlc_array_count( &p_sys
->tracks
) >= MAX_ASF_TRACKS
)
344 msg_Dbg( p_mux
, "cannot add this track (too much tracks)" );
348 tk
= p_input
->p_sys
= malloc( sizeof( asf_track_t
) );
349 if( unlikely(p_input
->p_sys
== NULL
) )
351 memset( tk
, 0, sizeof( *tk
) );
352 tk
->i_cat
= p_input
->p_fmt
->i_cat
;
354 tk
->b_audio_correction
= 0;
355 tk
->b_extended
= false;
361 int i_blockalign
= p_input
->p_fmt
->audio
.i_blockalign
;
362 int i_bitspersample
= p_input
->p_fmt
->audio
.i_bitspersample
;
365 switch( p_input
->p_fmt
->i_codec
)
368 tk
->i_tag
= WAVE_FORMAT_A52
;
369 tk
->psz_name
= "A/52";
373 tk
->i_tag
= WAVE_FORMAT_AAC
;
374 tk
->psz_name
= "MPEG-4 Audio";
378 tk
->psz_name
= "MPEG Audio Layer 3";
379 tk
->i_tag
= WAVE_FORMAT_MPEGLAYER3
;
385 tk
->psz_name
= "MPEG Audio Layer 1/2";
386 tk
->i_tag
= WAVE_FORMAT_MPEG
;
392 tk
->psz_name
= "Windows Media Audio v1";
393 tk
->i_tag
= WAVE_FORMAT_WMA1
;
394 tk
->b_audio_correction
= true;
397 tk
->psz_name
= "Windows Media Audio (v2) 7, 8 and 9 Series";
398 tk
->i_tag
= WAVE_FORMAT_WMA2
;
399 tk
->b_audio_correction
= true;
402 tk
->psz_name
= "Windows Media Audio 9 Professional";
403 tk
->i_tag
= WAVE_FORMAT_WMAP
;
404 tk
->b_audio_correction
= true;
407 tk
->psz_name
= "Windows Media Audio 9 Lossless";
408 tk
->i_tag
= WAVE_FORMAT_WMAL
;
409 tk
->b_audio_correction
= true;
413 tk
->psz_name
= "Raw audio 8bits";
414 tk
->i_tag
= WAVE_FORMAT_PCM
;
415 i_blockalign
= p_input
->p_fmt
->audio
.i_channels
;
419 tk
->psz_name
= "Raw audio 16bits";
420 tk
->i_tag
= WAVE_FORMAT_PCM
;
421 i_blockalign
= 2 * p_input
->p_fmt
->audio
.i_channels
;
422 i_bitspersample
= 16;
425 tk
->psz_name
= "Raw audio 24bits";
426 tk
->i_tag
= WAVE_FORMAT_PCM
;
427 i_blockalign
= 3 * p_input
->p_fmt
->audio
.i_channels
;
428 i_bitspersample
= 24;
431 tk
->psz_name
= "Raw audio 32bits";
432 tk
->i_tag
= WAVE_FORMAT_PCM
;
433 i_blockalign
= 4 * p_input
->p_fmt
->audio
.i_channels
;
434 i_bitspersample
= 32;
440 tk
->i_extra
= sizeof( WAVEFORMATEX
) +
441 p_input
->p_fmt
->i_extra
+ i_extra
;
442 tk
->p_extra
= malloc( tk
->i_extra
);
445 bo_init( &bo
, tk
->p_extra
, tk
->i_extra
);
446 bo_addle_u16( &bo
, tk
->i_tag
);
447 bo_addle_u16( &bo
, p_input
->p_fmt
->audio
.i_channels
);
448 bo_addle_u32( &bo
, p_input
->p_fmt
->audio
.i_rate
);
449 bo_addle_u32( &bo
, p_input
->p_fmt
->i_bitrate
/ 8 );
450 bo_addle_u16( &bo
, i_blockalign
);
451 tk
->i_blockalign
= i_blockalign
;
452 bo_addle_u16( &bo
, i_bitspersample
);
453 if( p_input
->p_fmt
->i_extra
> 0 )
455 bo_addle_u16( &bo
, p_input
->p_fmt
->i_extra
);
456 bo_add_mem ( &bo
, p_input
->p_fmt
->p_extra
,
457 p_input
->p_fmt
->i_extra
);
461 bo_addle_u16( &bo
, i_extra
);
462 if( tk
->i_tag
== WAVE_FORMAT_MPEGLAYER3
)
464 msg_Dbg( p_mux
, "adding mp3 header" );
465 bo_addle_u16( &bo
, 1 ); /* wId */
466 bo_addle_u32( &bo
, 2 ); /* fdwFlags */
467 bo_addle_u16( &bo
, 1152 ); /* nBlockSize */
468 bo_addle_u16( &bo
, 1 ); /* nFramesPerBlock */
469 bo_addle_u16( &bo
, 1393 ); /* nCodecDelay */
471 else if( tk
->i_tag
== WAVE_FORMAT_MPEG
)
473 msg_Dbg( p_mux
, "adding mp2 header" );
474 bo_addle_u16( &bo
, 2 ); /* fwHeadLayer */
475 bo_addle_u32( &bo
, p_input
->p_fmt
->i_bitrate
);
476 bo_addle_u16( &bo
, p_input
->p_fmt
->audio
.i_channels
== 2 ?1:8 );
477 bo_addle_u16( &bo
, 0 ); /* fwHeadModeExt */
478 bo_addle_u16( &bo
, 1 ); /* wHeadEmphasis */
479 bo_addle_u16( &bo
, 16 ); /* fwHeadFlags */
480 bo_addle_u32( &bo
, 0 ); /* dwPTSLow */
481 bo_addle_u32( &bo
, 0 ); /* dwPTSHigh */
485 if( p_input
->p_fmt
->i_bitrate
> 24000 )
487 p_sys
->i_bitrate
+= p_input
->p_fmt
->i_bitrate
;
491 p_sys
->i_bitrate
+= 128000;
493 if (p_sys
->i_bitrate_override
)
494 p_sys
->i_bitrate
= p_sys
->i_bitrate_override
;
499 const es_format_t
*p_fmt
= p_input
->p_fmt
;
500 uint8_t *p_codec_extra
= NULL
;
501 int i_codec_extra
= 0;
503 if( p_fmt
->i_codec
== VLC_CODEC_MP4V
)
505 tk
->psz_name
= "MPEG-4 Video";
506 tk
->i_fourcc
= VLC_FOURCC( 'M', 'P', '4', 'S' );
508 else if( p_fmt
->i_codec
== VLC_CODEC_DIV3
)
510 tk
->psz_name
= "MSMPEG-4 V3 Video";
511 tk
->i_fourcc
= VLC_FOURCC( 'M', 'P', '4', '3' );
513 else if( p_fmt
->i_codec
== VLC_CODEC_DIV2
)
515 tk
->psz_name
= "MSMPEG-4 V2 Video";
516 tk
->i_fourcc
= VLC_FOURCC( 'M', 'P', '4', '2' );
518 else if( p_fmt
->i_codec
== VLC_CODEC_DIV1
)
520 tk
->psz_name
= "MSMPEG-4 V1 Video";
521 tk
->i_fourcc
= VLC_FOURCC( 'M', 'P', 'G', '4' );
523 else if( p_fmt
->i_codec
== VLC_CODEC_WMV1
)
525 tk
->psz_name
= "Windows Media Video 7";
526 tk
->i_fourcc
= VLC_FOURCC( 'W', 'M', 'V', '1' );
528 else if( p_fmt
->i_codec
== VLC_CODEC_WMV2
)
530 tk
->psz_name
= "Windows Media Video 8";
531 tk
->i_fourcc
= VLC_FOURCC( 'W', 'M', 'V', '2' );
533 else if( p_fmt
->i_codec
== VLC_CODEC_WMV3
)
535 tk
->psz_name
= "Windows Media Video 9";
536 tk
->i_fourcc
= VLC_FOURCC( 'W', 'M', 'V', '3' );
537 tk
->b_extended
= true;
539 else if( p_fmt
->i_codec
== VLC_CODEC_VC1
)
541 tk
->psz_name
= "Windows Media Video 9 Advanced Profile";
542 tk
->i_fourcc
= VLC_FOURCC( 'W', 'V', 'C', '1' );
543 tk
->b_extended
= true;
545 if( p_fmt
->i_extra
> 0 )
547 p_codec_extra
= malloc( 1 + p_fmt
->i_extra
);
550 i_codec_extra
= 1 + p_fmt
->i_extra
;
551 p_codec_extra
[0] = 0x01;
552 memcpy( &p_codec_extra
[1], p_fmt
->p_extra
, p_fmt
->i_extra
);
556 else if( p_fmt
->i_codec
== VLC_CODEC_H264
)
558 tk
->psz_name
= "H.264/MPEG-4 AVC";
559 tk
->i_fourcc
= VLC_FOURCC('h','2','6','4');
563 tk
->psz_name
= _("Unknown Video");
564 tk
->i_fourcc
= p_fmt
->i_original_fourcc
?: p_fmt
->i_codec
;
566 if( !i_codec_extra
&& p_fmt
->i_extra
> 0 )
568 p_codec_extra
= malloc( p_fmt
->i_extra
);
571 i_codec_extra
= p_fmt
->i_extra
;
572 memcpy( p_codec_extra
, p_fmt
->p_extra
, p_fmt
->i_extra
);
576 tk
->i_extra
= 11 + sizeof( VLC_BITMAPINFOHEADER
) + i_codec_extra
;
577 tk
->p_extra
= malloc( tk
->i_extra
);
580 free( p_codec_extra
);
583 bo_init( &bo
, tk
->p_extra
, tk
->i_extra
);
584 bo_addle_u32( &bo
, p_fmt
->video
.i_width
);
585 bo_addle_u32( &bo
, p_fmt
->video
.i_height
);
586 bo_add_u8 ( &bo
, 0x02 ); /* flags */
587 bo_addle_u16( &bo
, sizeof( VLC_BITMAPINFOHEADER
) + i_codec_extra
);
588 bo_addle_u32( &bo
, sizeof( VLC_BITMAPINFOHEADER
) + i_codec_extra
);
589 bo_addle_u32( &bo
, p_fmt
->video
.i_width
);
590 bo_addle_u32( &bo
, p_fmt
->video
.i_height
);
591 bo_addle_u16( &bo
, 1 );
592 bo_addle_u16( &bo
, 24 );
593 bo_add_mem( &bo
, (uint8_t*)&tk
->i_fourcc
, 4 );
594 bo_addle_u32( &bo
, 0 );
595 bo_addle_u32( &bo
, 0 );
596 bo_addle_u32( &bo
, 0 );
597 bo_addle_u32( &bo
, 0 );
598 bo_addle_u32( &bo
, 0 );
599 if( i_codec_extra
> 0 )
601 bo_add_mem( &bo
, p_codec_extra
, i_codec_extra
);
602 free( p_codec_extra
);
605 if( p_fmt
->i_bitrate
> 50000 )
607 p_sys
->i_bitrate
+= p_fmt
->i_bitrate
;
611 p_sys
->i_bitrate
+= 512000;
613 if( p_sys
->i_bitrate_override
)
614 p_sys
->i_bitrate
= p_sys
->i_bitrate_override
;
618 msg_Err(p_mux
, "unhandled track type" );
623 es_format_Copy( &tk
->fmt
, p_input
->p_fmt
);
625 vlc_array_append( &p_sys
->tracks
, tk
);
626 tk
->i_id
= vlc_array_index_of_item( &p_sys
->tracks
, tk
) + 1;
628 if( p_sys
->b_asf_http
)
629 p_sys
->b_write_header
= true;
634 /*****************************************************************************
636 *****************************************************************************/
637 static void DelStream( sout_mux_t
*p_mux
, sout_input_t
*p_input
)
639 /* if bitrate ain't defined in commandline, reduce it when tracks are deleted
641 sout_mux_sys_t
*p_sys
= p_mux
->p_sys
;
642 asf_track_t
*tk
= p_input
->p_sys
;
643 msg_Dbg( p_mux
, "removing input" );
644 if(!p_sys
->i_bitrate_override
)
646 if( tk
->i_cat
== AUDIO_ES
)
648 if( p_input
->p_fmt
->i_bitrate
> 24000 )
649 p_sys
->i_bitrate
-= p_input
->p_fmt
->i_bitrate
;
651 p_sys
->i_bitrate
-= 128000;
653 else if(tk
->i_cat
== VIDEO_ES
)
655 if( p_input
->p_fmt
->i_bitrate
> 50000 )
656 p_sys
->i_bitrate
-= p_input
->p_fmt
->i_bitrate
;
658 p_sys
->i_bitrate
-= 512000;
662 if( p_sys
->b_asf_http
)
664 vlc_array_remove( &p_sys
->tracks
, vlc_array_index_of_item( &p_sys
->tracks
, tk
) );
665 p_sys
->b_write_header
= true;
669 /*****************************************************************************
671 *****************************************************************************/
672 static int Mux( sout_mux_t
*p_mux
)
674 sout_mux_sys_t
*p_sys
= p_mux
->p_sys
;
676 if( p_sys
->b_write_header
)
678 block_t
*out
= asf_header_create( p_mux
, true );
680 out
->i_flags
|= BLOCK_FLAG_HEADER
;
681 sout_AccessOutWrite( p_mux
->p_access
, out
);
683 p_sys
->b_write_header
= false;
688 sout_input_t
*p_input
;
694 int i_stream
= sout_MuxGetStream( p_mux
, 1, &i_dts
);
697 /* not enough data */
701 if( p_sys
->i_dts_first
<= VLC_TS_INVALID
)
703 p_sys
->i_dts_first
= i_dts
;
705 if( p_sys
->i_dts_last
< i_dts
)
707 p_sys
->i_dts_last
= i_dts
;
710 p_input
= p_mux
->pp_inputs
[i_stream
];
711 tk
= (asf_track_t
*)p_input
->p_sys
;
713 data
= block_FifoGet( p_input
->p_fifo
);
715 /* Convert VC1 to ASF special format */
716 if( tk
->i_fourcc
== VLC_FOURCC( 'W', 'V', 'C', '1' ) )
718 while( data
->i_buffer
>= 4 &&
719 ( data
->p_buffer
[0] != 0x00 || data
->p_buffer
[1] != 0x00 ||
720 data
->p_buffer
[2] != 0x01 ||
721 ( data
->p_buffer
[3] != 0x0D && data
->p_buffer
[3] != 0x0C ) ) )
726 if( data
->i_buffer
>= 4 )
733 if( ( pk
= asf_packet_create( p_mux
, tk
, data
) ) )
735 sout_AccessOutWrite( p_mux
->p_access
, pk
);
742 /****************************************************************************
743 * Asf header construction
744 ****************************************************************************/
746 /****************************************************************************
748 ****************************************************************************/
749 static void bo_init( bo_t
*p_bo
, uint8_t *p_buffer
, int i_size
)
751 p_bo
->i_buffer_size
= i_size
;
753 p_bo
->p_buffer
= p_buffer
;
755 static void bo_add_u8( bo_t
*p_bo
, uint8_t i
)
757 if( p_bo
->i_buffer
< p_bo
->i_buffer_size
)
759 p_bo
->p_buffer
[p_bo
->i_buffer
] = i
;
763 static void bo_addle_u16( bo_t
*p_bo
, uint16_t i
)
765 bo_add_u8( p_bo
, i
&0xff );
766 bo_add_u8( p_bo
, ( ( i
>> 8) &0xff ) );
768 static void bo_addle_u32( bo_t
*p_bo
, uint32_t i
)
770 bo_addle_u16( p_bo
, i
&0xffff );
771 bo_addle_u16( p_bo
, ( ( i
>> 16) &0xffff ) );
773 static void bo_addle_u64( bo_t
*p_bo
, uint64_t i
)
775 bo_addle_u32( p_bo
, i
&0xffffffff );
776 bo_addle_u32( p_bo
, ( ( i
>> 32) &0xffffffff ) );
779 static void bo_add_mem( bo_t
*p_bo
, uint8_t *p_mem
, int i_size
)
781 int i_copy
= __MIN( i_size
, p_bo
->i_buffer_size
- p_bo
->i_buffer
);
785 memcpy( &p_bo
->p_buffer
[p_bo
->i_buffer
], p_mem
, i_copy
);
787 p_bo
->i_buffer
+= i_size
;
790 static void bo_addle_str16( bo_t
*bo
, const char *str
)
792 bo_addle_u16( bo
, strlen( str
) + 1 );
795 uint16_t c
= (uint8_t)*str
++;
796 bo_addle_u16( bo
, c
);
797 if( c
== '\0' ) break;
801 static void bo_addle_str16_nosize( bo_t
*bo
, const char *str
)
805 uint16_t c
= (uint8_t)*str
++;
806 bo_addle_u16( bo
, c
);
807 if( c
== '\0' ) break;
811 /****************************************************************************
813 ****************************************************************************/
814 static void bo_add_guid( bo_t
*p_bo
, const guid_t
*id
)
816 bo_addle_u32( p_bo
, id
->Data1
);
817 bo_addle_u16( p_bo
, id
->Data2
);
818 bo_addle_u16( p_bo
, id
->Data3
);
819 for( int i
= 0; i
< 8; i
++ )
821 bo_add_u8( p_bo
, id
->Data4
[i
] );
825 /****************************************************************************
827 ****************************************************************************/
828 static void asf_chunk_add( bo_t
*bo
,
829 int i_type
, int i_len
, int i_flags
, int i_seq
)
831 bo_addle_u16( bo
, i_type
);
832 bo_addle_u16( bo
, i_len
+ 8 );
833 bo_addle_u32( bo
, i_seq
);
834 bo_addle_u16( bo
, i_flags
);
835 bo_addle_u16( bo
, i_len
+ 8 );
838 static block_t
*asf_header_create( sout_mux_t
*p_mux
, bool b_broadcast
)
840 sout_mux_sys_t
*p_sys
= p_mux
->p_sys
;
842 mtime_t i_duration
= 0;
843 int i_size
, i_header_ext_size
;
844 int i_ci_size
, i_cm_size
= 0, i_cd_size
= 0;
849 msg_Dbg( p_mux
, "Asf muxer creating header" );
851 if( p_sys
->i_dts_first
> VLC_TS_INVALID
)
853 i_duration
= p_sys
->i_dts_last
- p_sys
->i_dts_first
;
854 if( i_duration
< 0 ) i_duration
= 0;
857 /* calculate header size */
860 for( size_t i
= 0; i
< vlc_array_count( &p_sys
->tracks
); i
++ )
862 tk
= vlc_array_item_at_index( &p_sys
->tracks
, i
);
863 /* update also track-id */
866 i_size
+= 78 + tk
->i_extra
;
867 i_ci_size
+= 8 + 2 * strlen( tk
->psz_name
);
868 if( tk
->i_cat
== AUDIO_ES
) i_ci_size
+= 4;
869 else if( tk
->i_cat
== VIDEO_ES
) i_ci_size
+= 6;
871 /* Error correction data field */
872 if( tk
->b_audio_correction
) i_size
+= 8;
875 /* size of the content description object */
876 if( *p_sys
->psz_title
|| *p_sys
->psz_author
|| *p_sys
->psz_copyright
||
877 *p_sys
->psz_comment
|| *p_sys
->psz_rating
)
879 i_cd_size
= 34 + 2 * ( strlen( p_sys
->psz_title
) + 1 +
880 strlen( p_sys
->psz_author
) + 1 +
881 strlen( p_sys
->psz_copyright
) + 1 +
882 strlen( p_sys
->psz_comment
) + 1 +
883 strlen( p_sys
->psz_rating
) + 1 );
886 i_header_ext_size
= 46;
888 /* size of the metadata object */
889 for( size_t i
= 0; i
< vlc_array_count( &p_sys
->tracks
); i
++ )
891 const asf_track_t
*p_track
= vlc_array_item_at_index( &p_sys
->tracks
, i
);
892 if( p_track
->i_cat
== VIDEO_ES
&&
893 p_track
->fmt
.video
.i_sar_num
!= 0 &&
894 p_track
->fmt
.video
.i_sar_den
!= 0 )
896 i_cm_size
= 26 + 2 * (16 + 2 * sizeof("AspectRatio?"));
898 if( p_track
->b_extended
)
899 i_header_ext_size
+= 88;
903 i_header_ext_size
+= i_cm_size
;
905 i_size
+= i_ci_size
+ i_cd_size
+ i_header_ext_size
;
907 if( p_sys
->b_asf_http
)
909 out
= block_Alloc( i_size
+ 50 + 12 );
910 bo_init( &bo
, out
->p_buffer
, i_size
+ 50 + 12 );
911 asf_chunk_add( &bo
, 0x4824, i_size
+ 50, 0xc00, p_sys
->i_seq
++ );
915 out
= block_Alloc( i_size
+ 50 );
916 bo_init( &bo
, out
->p_buffer
, i_size
+ 50 );
920 bo_add_guid ( &bo
, &asf_object_header_guid
);
921 bo_addle_u64( &bo
, i_size
);
922 bo_addle_u32( &bo
, 2 + vlc_array_count( &p_sys
->tracks
) + 1 +
923 (i_cd_size
? 1 : 0) + (i_cm_size
? 1 : 0) );
924 bo_add_u8 ( &bo
, 1 );
925 bo_add_u8 ( &bo
, 2 );
929 /* file properties */
930 bo_add_guid ( &bo
, &asf_object_file_properties_guid
);
931 bo_addle_u64( &bo
, 104 );
932 bo_add_guid ( &bo
, &p_sys
->fid
);
933 bo_addle_u64( &bo
, i_size
+ 50 + p_sys
->i_packet_count
*
934 p_sys
->i_packet_size
); /* file size */
935 bo_addle_u64( &bo
, 0 ); /* creation date */
936 bo_addle_u64( &bo
, b_broadcast
? 0xffffffffLL
: p_sys
->i_packet_count
);
937 bo_addle_u64( &bo
, i_duration
* 10 ); /* play duration (100ns) */
938 bo_addle_u64( &bo
, i_duration
* 10 ); /* send duration (100ns) */
939 bo_addle_u64( &bo
, p_sys
->i_preroll_time
); /* preroll duration (ms) */
940 bo_addle_u32( &bo
, b_broadcast
? 0x01 : 0x02 /* seekable */ ); /* flags */
941 bo_addle_u32( &bo
, p_sys
->i_packet_size
); /* packet size min */
942 bo_addle_u32( &bo
, p_sys
->i_packet_size
); /* packet size max */
943 /* NOTE: According to p6-9 of the ASF specification the bitrate cannot be 0,
944 * therefor apply this workaround to make sure it is not 0. If the bitrate is
945 * 0 the file will play in WMP11, but not in Sliverlight and WMP12 */
946 bo_addle_u32( &bo
, p_sys
->i_bitrate
> 0 ? p_sys
->i_bitrate
: 1 ); /* maxbitrate */
948 /* header extension */
949 bo_add_guid ( &bo
, &asf_object_header_extension_guid
);
950 bo_addle_u64( &bo
, i_header_ext_size
);
951 bo_add_guid ( &bo
, &asf_guid_reserved_1
);
952 bo_addle_u16( &bo
, 6 );
953 bo_addle_u32( &bo
, i_header_ext_size
- 46 );
955 /* extended stream properties */
956 for( size_t i
= 0; i
< vlc_array_count( &p_sys
->tracks
); i
++ )
958 const asf_track_t
*p_track
= vlc_array_item_at_index( &p_sys
->tracks
, i
);
959 const es_format_t
*p_fmt
= &p_track
->fmt
;
961 if( !p_track
->b_extended
)
964 uint64_t i_avg_duration
= 0;
965 if( p_fmt
->i_cat
== VIDEO_ES
&&
966 p_fmt
->video
.i_frame_rate
> 0 && p_fmt
->video
.i_frame_rate_base
> 0 )
967 i_avg_duration
= ( INT64_C(10000000) * p_fmt
->video
.i_frame_rate_base
+
968 p_fmt
->video
.i_frame_rate
/2 ) / p_fmt
->video
.i_frame_rate
;
970 bo_add_guid ( &bo
, &asf_object_extended_stream_properties_guid
);
971 bo_addle_u64( &bo
, 88 );
972 bo_addle_u64( &bo
, 0 );
973 bo_addle_u64( &bo
, 0 );
974 bo_addle_u32( &bo
, p_fmt
->i_bitrate
); /* Bitrate */
975 bo_addle_u32( &bo
, p_sys
->i_preroll_time
); /* Buffer size */
976 bo_addle_u32( &bo
, 0 ); /* Initial buffer fullness */
977 bo_addle_u32( &bo
, p_fmt
->i_bitrate
); /* Alternate Bitrate */
978 bo_addle_u32( &bo
, 0 ); /* Alternate Buffer size */
979 bo_addle_u32( &bo
, 0 ); /* Alternate Initial buffer fullness */
980 bo_addle_u32( &bo
, 0 ); /* Maximum object size (0 = unknown) */
981 bo_addle_u32( &bo
, 0x02 ); /* Flags (seekable) */
982 bo_addle_u16( &bo
, p_track
->i_id
); /* Stream number */
983 bo_addle_u16( &bo
, 0 ); /* Stream language index */
984 bo_addle_u64( &bo
, i_avg_duration
); /* Average time per frame */
985 bo_addle_u16( &bo
, 0 ); /* Stream name count */
986 bo_addle_u16( &bo
, 0 ); /* Payload extension system count */
989 /* metadata object (part of header extension) */
992 unsigned int i_dst_num
, i_dst_den
;
994 asf_track_t
*tk
= NULL
;
995 for( size_t i
= 0; i
< vlc_array_count( &p_sys
->tracks
); i
++ )
997 tk
= vlc_array_item_at_index( &p_sys
->tracks
, i
);
998 if( tk
->i_cat
== VIDEO_ES
&&
999 tk
->fmt
.video
.i_sar_num
!= 0 &&
1000 tk
->fmt
.video
.i_sar_den
!= 0 )
1002 vlc_ureduce( &i_dst_num
, &i_dst_den
,
1003 tk
->fmt
.video
.i_sar_num
,
1004 tk
->fmt
.video
.i_sar_den
, 0 );
1008 assert( tk
!= NULL
);
1010 msg_Dbg( p_mux
, "pixel aspect-ratio: %i/%i", i_dst_num
, i_dst_den
);
1012 bo_add_guid ( &bo
, &asf_object_metadata_guid
);
1013 bo_addle_u64( &bo
, i_cm_size
);
1014 bo_addle_u16( &bo
, 2 ); /* description records count */
1015 /* 1st description record */
1016 bo_addle_u16( &bo
, 0 ); /* reserved */
1017 bo_addle_u16( &bo
, tk
->i_id
); /* stream number (0 for the whole file) */
1018 bo_addle_u16( &bo
, 2 * sizeof("AspectRatioX") ); /* name length */
1019 bo_addle_u16( &bo
, 0x3 /* DWORD */ ); /* data type */
1020 bo_addle_u32( &bo
, 4 ); /* data length */
1021 bo_addle_str16_nosize( &bo
, "AspectRatioX" );
1022 bo_addle_u32( &bo
, i_dst_num
); /* data */
1023 /* 2nd description record */
1024 bo_addle_u16( &bo
, 0 ); /* reserved */
1025 bo_addle_u16( &bo
, tk
->i_id
); /* stream number (0 for the whole file) */
1026 bo_addle_u16( &bo
, 2 * sizeof("AspectRatioY") ); /* name length */
1027 bo_addle_u16( &bo
, 0x3 /* DWORD */ ); /* data type */
1028 bo_addle_u32( &bo
, 4 ); /* data length */
1029 bo_addle_str16_nosize( &bo
, "AspectRatioY" );
1030 bo_addle_u32( &bo
, i_dst_den
); /* data */
1033 /* content description header */
1036 bo_add_guid ( &bo
, &asf_object_content_description_guid
);
1037 bo_addle_u64( &bo
, i_cd_size
);
1038 bo_addle_u16( &bo
, 2 * strlen( p_sys
->psz_title
) + 2 );
1039 bo_addle_u16( &bo
, 2 * strlen( p_sys
->psz_author
) + 2 );
1040 bo_addle_u16( &bo
, 2 * strlen( p_sys
->psz_copyright
) + 2 );
1041 bo_addle_u16( &bo
, 2 * strlen( p_sys
->psz_comment
) + 2 );
1042 bo_addle_u16( &bo
, 2 * strlen( p_sys
->psz_rating
) + 2 );
1044 bo_addle_str16_nosize( &bo
, p_sys
->psz_title
);
1045 bo_addle_str16_nosize( &bo
, p_sys
->psz_author
);
1046 bo_addle_str16_nosize( &bo
, p_sys
->psz_copyright
);
1047 bo_addle_str16_nosize( &bo
, p_sys
->psz_comment
);
1048 bo_addle_str16_nosize( &bo
, p_sys
->psz_rating
);
1051 /* stream properties */
1052 for( size_t i
= 0; i
< vlc_array_count( &p_sys
->tracks
); i
++ )
1054 tk
= vlc_array_item_at_index( &p_sys
->tracks
, i
);
1056 bo_add_guid ( &bo
, &asf_object_stream_properties_guid
);
1057 bo_addle_u64( &bo
, 78 + tk
->i_extra
+ (tk
->b_audio_correction
? 8:0) );
1059 if( tk
->i_cat
== AUDIO_ES
)
1061 bo_add_guid( &bo
, &asf_object_stream_type_audio
);
1062 if( tk
->b_audio_correction
)
1063 bo_add_guid( &bo
, &asf_guid_audio_conceal_spread
);
1065 bo_add_guid( &bo
, &asf_no_error_correction_guid
);
1067 else if( tk
->i_cat
== VIDEO_ES
)
1069 bo_add_guid( &bo
, &asf_object_stream_type_video
);
1070 bo_add_guid( &bo
, &asf_no_error_correction_guid
);
1072 bo_addle_u64( &bo
, 0 ); /* time offset */
1073 bo_addle_u32( &bo
, tk
->i_extra
);
1074 /* correction data length */
1075 bo_addle_u32( &bo
, tk
->b_audio_correction
? 8 : 0 );
1076 bo_addle_u16( &bo
, tk
->i_id
); /* stream number */
1077 bo_addle_u32( &bo
, 0 );
1078 bo_add_mem ( &bo
, tk
->p_extra
, tk
->i_extra
);
1080 /* Error correction data field */
1081 if( tk
->b_audio_correction
)
1083 bo_add_u8( &bo
, 0x1 ); /* span */
1084 bo_addle_u16( &bo
, tk
->i_blockalign
); /* virtual packet length */
1085 bo_addle_u16( &bo
, tk
->i_blockalign
); /* virtual chunck length */
1086 bo_addle_u16( &bo
, 1 ); /* silence length */
1087 bo_add_u8( &bo
, 0x0 ); /* data */
1092 bo_add_guid ( &bo
, &asf_object_codec_list_guid
);
1093 bo_addle_u64( &bo
, i_ci_size
);
1094 bo_add_guid ( &bo
, &asf_guid_reserved_2
);
1095 bo_addle_u32( &bo
, vlc_array_count( &p_sys
->tracks
) );
1096 for( size_t i
= 0; i
< vlc_array_count( &p_sys
->tracks
); i
++ )
1098 tk
= vlc_array_item_at_index( &p_sys
->tracks
,i
);
1100 if( tk
->i_cat
== VIDEO_ES
) bo_addle_u16( &bo
, 1 /* video */ );
1101 else if( tk
->i_cat
== AUDIO_ES
) bo_addle_u16( &bo
, 2 /* audio */ );
1102 else bo_addle_u16( &bo
, 0xFFFF /* unknown */ );
1104 bo_addle_str16( &bo
, tk
->psz_name
);
1105 bo_addle_u16( &bo
, 0 );
1106 if( tk
->i_cat
== AUDIO_ES
)
1108 bo_addle_u16( &bo
, 2 );
1109 bo_addle_u16( &bo
, tk
->i_tag
);
1111 else if( tk
->i_cat
== VIDEO_ES
)
1113 bo_addle_u16( &bo
, 4 );
1114 bo_add_mem ( &bo
, (uint8_t*)&tk
->i_fourcc
, 4 );
1119 bo_add_guid ( &bo
, &asf_object_data_guid
);
1120 bo_addle_u64( &bo
, 50 + p_sys
->i_packet_count
* p_sys
->i_packet_size
);
1121 bo_add_guid ( &bo
, &p_sys
->fid
);
1122 bo_addle_u64( &bo
, p_sys
->i_packet_count
);
1123 bo_addle_u16( &bo
, 0x101 );
1128 /****************************************************************************
1130 ****************************************************************************/
1131 static block_t
*asf_packet_flush( sout_mux_t
*p_mux
)
1133 sout_mux_sys_t
*p_sys
= p_mux
->p_sys
;
1134 int i_pad
, i_preheader
= p_sys
->b_asf_http
? 12 : 0;
1138 if( !p_sys
->pk
) return 0;
1140 i_pad
= p_sys
->i_packet_size
- p_sys
->i_pk_used
;
1141 memset( p_sys
->pk
->p_buffer
+ p_sys
->i_pk_used
, 0, i_pad
);
1143 bo_init( &bo
, p_sys
->pk
->p_buffer
, 14 + i_preheader
);
1145 if( p_sys
->b_asf_http
)
1146 asf_chunk_add( &bo
, 0x4424, p_sys
->i_packet_size
, 0x0, p_sys
->i_seq
++);
1148 bo_add_u8 ( &bo
, 0x82 );
1149 bo_addle_u16( &bo
, 0 );
1150 bo_add_u8( &bo
, 0x11 );
1151 bo_add_u8( &bo
, 0x5d );
1152 bo_addle_u16( &bo
, i_pad
);
1153 bo_addle_u32( &bo
, (p_sys
->i_pk_dts
- p_sys
->i_dts_first
) / 1000 +
1154 p_sys
->i_preroll_time
);
1155 bo_addle_u16( &bo
, 0 /* data->i_length */ );
1156 bo_add_u8( &bo
, 0x80 | p_sys
->i_pk_frame
);
1161 p_sys
->i_packet_count
++;
1166 static block_t
*asf_packet_create( sout_mux_t
*p_mux
,
1167 asf_track_t
*tk
, block_t
*data
)
1169 sout_mux_sys_t
*p_sys
= p_mux
->p_sys
;
1171 int i_data
= data
->i_buffer
;
1173 uint8_t *p_data
= data
->p_buffer
;
1174 block_t
*first
= NULL
, **last
= &first
;
1175 int i_preheader
= p_sys
->b_asf_http
? 12 : 0;
1177 while( i_pos
< i_data
)
1182 if( p_sys
->pk
== NULL
)
1184 p_sys
->pk
= block_Alloc( p_sys
->i_packet_size
+ i_preheader
);
1185 /* reserve 14 bytes for the packet header */
1186 p_sys
->i_pk_used
= 14 + i_preheader
;
1187 p_sys
->i_pk_frame
= 0;
1188 p_sys
->i_pk_dts
= data
->i_dts
;
1191 bo_init( &bo
, &p_sys
->pk
->p_buffer
[p_sys
->i_pk_used
],
1192 p_sys
->i_packet_size
- p_sys
->i_pk_used
);
1194 /* add payload (header size = 17) */
1195 i_payload
= __MIN( i_data
- i_pos
,
1196 p_sys
->i_packet_size
- p_sys
->i_pk_used
- 17 );
1198 if( tk
->b_audio_correction
&& p_sys
->i_pk_frame
&& i_payload
< i_data
)
1200 /* Don't know why yet but WMP doesn't like splitted WMA packets */
1201 *last
= asf_packet_flush( p_mux
);
1202 last
= &(*last
)->p_next
;
1206 bo_add_u8 ( &bo
, !(data
->i_flags
& BLOCK_FLAG_TYPE_P
||
1207 data
->i_flags
& BLOCK_FLAG_TYPE_B
) ?
1208 0x80 | tk
->i_id
: tk
->i_id
);
1209 bo_add_u8 ( &bo
, tk
->i_sequence
);
1210 bo_addle_u32( &bo
, i_pos
);
1211 bo_add_u8 ( &bo
, 0x08 ); /* flags */
1212 bo_addle_u32( &bo
, i_data
);
1213 bo_addle_u32( &bo
, (data
->i_dts
- p_sys
->i_dts_first
) / 1000 +
1214 p_sys
->i_preroll_time
);
1215 bo_addle_u16( &bo
, i_payload
);
1216 bo_add_mem ( &bo
, &p_data
[i_pos
], i_payload
);
1218 p_sys
->i_pk_used
+= 17 + i_payload
;
1220 p_sys
->i_pk_frame
++;
1222 if( p_sys
->i_pk_used
+ 17 >= p_sys
->i_packet_size
)
1224 /* Not enough data for another payload, flush the packet */
1225 *last
= asf_packet_flush( p_mux
);
1226 last
= &(*last
)->p_next
;
1231 block_Release( data
);
1236 static block_t
*asf_stream_end_create( sout_mux_t
*p_mux
)
1238 sout_mux_sys_t
*p_sys
= p_mux
->p_sys
;
1240 block_t
*out
= NULL
;
1243 if( p_sys
->b_asf_http
)
1245 out
= block_Alloc( 12 );
1246 bo_init( &bo
, out
->p_buffer
, 12 );
1247 asf_chunk_add( &bo
, 0x4524, 0, 0x00, p_sys
->i_seq
++ );
1252 out
= block_Alloc( 56 );
1253 bo_init( &bo
, out
->p_buffer
, 56 );
1254 bo_add_guid ( &bo
, &asf_object_index_guid
);
1255 bo_addle_u64( &bo
, 56 );
1256 bo_add_guid ( &bo
, &p_sys
->fid
);
1257 bo_addle_u64( &bo
, 10000000 );
1258 bo_addle_u32( &bo
, 5 );
1259 bo_addle_u32( &bo
, 0 );