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
;
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 if( vlc_array_append( &p_sys
->tracks
, tk
) )
630 es_format_Copy( &tk
->fmt
, p_input
->p_fmt
);
632 tk
->i_id
= vlc_array_index_of_item( &p_sys
->tracks
, tk
) + 1;
634 if( p_sys
->b_asf_http
)
635 p_sys
->b_write_header
= true;
640 /*****************************************************************************
642 *****************************************************************************/
643 static void DelStream( sout_mux_t
*p_mux
, sout_input_t
*p_input
)
645 /* if bitrate ain't defined in commandline, reduce it when tracks are deleted
647 sout_mux_sys_t
*p_sys
= p_mux
->p_sys
;
648 asf_track_t
*tk
= p_input
->p_sys
;
649 msg_Dbg( p_mux
, "removing input" );
650 if(!p_sys
->i_bitrate_override
)
652 if( tk
->i_cat
== AUDIO_ES
)
654 if( p_input
->p_fmt
->i_bitrate
> 24000 )
655 p_sys
->i_bitrate
-= p_input
->p_fmt
->i_bitrate
;
657 p_sys
->i_bitrate
-= 128000;
659 else if(tk
->i_cat
== VIDEO_ES
)
661 if( p_input
->p_fmt
->i_bitrate
> 50000 )
662 p_sys
->i_bitrate
-= p_input
->p_fmt
->i_bitrate
;
664 p_sys
->i_bitrate
-= 512000;
668 if( p_sys
->b_asf_http
)
670 vlc_array_remove( &p_sys
->tracks
, vlc_array_index_of_item( &p_sys
->tracks
, tk
) );
671 p_sys
->b_write_header
= true;
675 /*****************************************************************************
677 *****************************************************************************/
678 static int Mux( sout_mux_t
*p_mux
)
680 sout_mux_sys_t
*p_sys
= p_mux
->p_sys
;
682 if( p_sys
->b_write_header
)
684 block_t
*out
= asf_header_create( p_mux
, true );
686 out
->i_flags
|= BLOCK_FLAG_HEADER
;
687 sout_AccessOutWrite( p_mux
->p_access
, out
);
689 p_sys
->b_write_header
= false;
694 sout_input_t
*p_input
;
700 int i_stream
= sout_MuxGetStream( p_mux
, 1, &i_dts
);
703 /* not enough data */
707 if( p_sys
->i_dts_first
== VLC_TS_INVALID
)
709 p_sys
->i_dts_first
= i_dts
;
711 if( p_sys
->i_dts_last
< i_dts
)
713 p_sys
->i_dts_last
= i_dts
;
716 p_input
= p_mux
->pp_inputs
[i_stream
];
717 tk
= (asf_track_t
*)p_input
->p_sys
;
719 data
= block_FifoGet( p_input
->p_fifo
);
721 /* Convert VC1 to ASF special format */
722 if( tk
->i_fourcc
== VLC_FOURCC( 'W', 'V', 'C', '1' ) )
724 while( data
->i_buffer
>= 4 &&
725 ( data
->p_buffer
[0] != 0x00 || data
->p_buffer
[1] != 0x00 ||
726 data
->p_buffer
[2] != 0x01 ||
727 ( data
->p_buffer
[3] != 0x0D && data
->p_buffer
[3] != 0x0C ) ) )
732 if( data
->i_buffer
>= 4 )
739 if( ( pk
= asf_packet_create( p_mux
, tk
, data
) ) )
741 sout_AccessOutWrite( p_mux
->p_access
, pk
);
748 /****************************************************************************
749 * Asf header construction
750 ****************************************************************************/
752 /****************************************************************************
754 ****************************************************************************/
755 static void bo_init( bo_t
*p_bo
, uint8_t *p_buffer
, int i_size
)
757 p_bo
->i_buffer_size
= i_size
;
759 p_bo
->p_buffer
= p_buffer
;
761 static void bo_add_u8( bo_t
*p_bo
, uint8_t i
)
763 if( p_bo
->i_buffer
< p_bo
->i_buffer_size
)
765 p_bo
->p_buffer
[p_bo
->i_buffer
] = i
;
769 static void bo_addle_u16( bo_t
*p_bo
, uint16_t i
)
771 bo_add_u8( p_bo
, i
&0xff );
772 bo_add_u8( p_bo
, ( ( i
>> 8) &0xff ) );
774 static void bo_addle_u32( bo_t
*p_bo
, uint32_t i
)
776 bo_addle_u16( p_bo
, i
&0xffff );
777 bo_addle_u16( p_bo
, ( ( i
>> 16) &0xffff ) );
779 static void bo_addle_u64( bo_t
*p_bo
, uint64_t i
)
781 bo_addle_u32( p_bo
, i
&0xffffffff );
782 bo_addle_u32( p_bo
, ( ( i
>> 32) &0xffffffff ) );
785 static void bo_add_mem( bo_t
*p_bo
, uint8_t *p_mem
, int i_size
)
787 int i_copy
= __MIN( i_size
, p_bo
->i_buffer_size
- p_bo
->i_buffer
);
791 memcpy( &p_bo
->p_buffer
[p_bo
->i_buffer
], p_mem
, i_copy
);
793 p_bo
->i_buffer
+= i_size
;
796 static void bo_addle_str16( bo_t
*bo
, const char *str
)
798 bo_addle_u16( bo
, strlen( str
) + 1 );
801 uint16_t c
= (uint8_t)*str
++;
802 bo_addle_u16( bo
, c
);
803 if( c
== '\0' ) break;
807 static void bo_addle_str16_nosize( bo_t
*bo
, const char *str
)
811 uint16_t c
= (uint8_t)*str
++;
812 bo_addle_u16( bo
, c
);
813 if( c
== '\0' ) break;
817 /****************************************************************************
819 ****************************************************************************/
820 static void bo_add_guid( bo_t
*p_bo
, const guid_t
*id
)
822 bo_addle_u32( p_bo
, id
->Data1
);
823 bo_addle_u16( p_bo
, id
->Data2
);
824 bo_addle_u16( p_bo
, id
->Data3
);
825 for( int i
= 0; i
< 8; i
++ )
827 bo_add_u8( p_bo
, id
->Data4
[i
] );
831 /****************************************************************************
833 ****************************************************************************/
834 static void asf_chunk_add( bo_t
*bo
,
835 int i_type
, int i_len
, int i_flags
, int i_seq
)
837 bo_addle_u16( bo
, i_type
);
838 bo_addle_u16( bo
, i_len
+ 8 );
839 bo_addle_u32( bo
, i_seq
);
840 bo_addle_u16( bo
, i_flags
);
841 bo_addle_u16( bo
, i_len
+ 8 );
844 static block_t
*asf_header_create( sout_mux_t
*p_mux
, bool b_broadcast
)
846 sout_mux_sys_t
*p_sys
= p_mux
->p_sys
;
848 mtime_t i_duration
= 0;
849 int i_size
, i_header_ext_size
;
850 int i_ci_size
, i_cm_size
= 0, i_cd_size
= 0;
855 msg_Dbg( p_mux
, "Asf muxer creating header" );
857 if( p_sys
->i_dts_first
!= VLC_TS_INVALID
)
859 i_duration
= p_sys
->i_dts_last
- p_sys
->i_dts_first
;
860 if( i_duration
< 0 ) i_duration
= 0;
863 /* calculate header size */
866 for( size_t i
= 0; i
< vlc_array_count( &p_sys
->tracks
); i
++ )
868 tk
= vlc_array_item_at_index( &p_sys
->tracks
, i
);
869 /* update also track-id */
872 i_size
+= 78 + tk
->i_extra
;
873 i_ci_size
+= 8 + 2 * strlen( tk
->psz_name
);
874 if( tk
->i_cat
== AUDIO_ES
) i_ci_size
+= 4;
875 else if( tk
->i_cat
== VIDEO_ES
) i_ci_size
+= 6;
877 /* Error correction data field */
878 if( tk
->b_audio_correction
) i_size
+= 8;
881 /* size of the content description object */
882 if( *p_sys
->psz_title
|| *p_sys
->psz_author
|| *p_sys
->psz_copyright
||
883 *p_sys
->psz_comment
|| *p_sys
->psz_rating
)
885 i_cd_size
= 34 + 2 * ( strlen( p_sys
->psz_title
) + 1 +
886 strlen( p_sys
->psz_author
) + 1 +
887 strlen( p_sys
->psz_copyright
) + 1 +
888 strlen( p_sys
->psz_comment
) + 1 +
889 strlen( p_sys
->psz_rating
) + 1 );
892 i_header_ext_size
= 46;
894 /* size of the metadata object */
895 for( size_t i
= 0; i
< vlc_array_count( &p_sys
->tracks
); i
++ )
897 const asf_track_t
*p_track
= vlc_array_item_at_index( &p_sys
->tracks
, i
);
898 if( p_track
->i_cat
== VIDEO_ES
&&
899 p_track
->fmt
.video
.i_sar_num
!= 0 &&
900 p_track
->fmt
.video
.i_sar_den
!= 0 )
902 i_cm_size
= 26 + 2 * (16 + 2 * sizeof("AspectRatio?"));
904 if( p_track
->b_extended
)
905 i_header_ext_size
+= 88;
909 i_header_ext_size
+= i_cm_size
;
911 i_size
+= i_ci_size
+ i_cd_size
+ i_header_ext_size
;
913 if( p_sys
->b_asf_http
)
915 out
= block_Alloc( i_size
+ 50 + 12 );
916 bo_init( &bo
, out
->p_buffer
, i_size
+ 50 + 12 );
917 asf_chunk_add( &bo
, 0x4824, i_size
+ 50, 0xc00, p_sys
->i_seq
++ );
921 out
= block_Alloc( i_size
+ 50 );
922 bo_init( &bo
, out
->p_buffer
, i_size
+ 50 );
926 bo_add_guid ( &bo
, &asf_object_header_guid
);
927 bo_addle_u64( &bo
, i_size
);
928 bo_addle_u32( &bo
, 2 + vlc_array_count( &p_sys
->tracks
) + 1 +
929 (i_cd_size
? 1 : 0) + (i_cm_size
? 1 : 0) );
930 bo_add_u8 ( &bo
, 1 );
931 bo_add_u8 ( &bo
, 2 );
935 /* file properties */
936 bo_add_guid ( &bo
, &asf_object_file_properties_guid
);
937 bo_addle_u64( &bo
, 104 );
938 bo_add_guid ( &bo
, &p_sys
->fid
);
939 bo_addle_u64( &bo
, i_size
+ 50 + p_sys
->i_packet_count
*
940 p_sys
->i_packet_size
); /* file size */
941 bo_addle_u64( &bo
, 0 ); /* creation date */
942 bo_addle_u64( &bo
, b_broadcast
? 0xffffffffLL
: p_sys
->i_packet_count
);
943 bo_addle_u64( &bo
, i_duration
* 10 ); /* play duration (100ns) */
944 bo_addle_u64( &bo
, i_duration
* 10 ); /* send duration (100ns) */
945 bo_addle_u64( &bo
, p_sys
->i_preroll_time
); /* preroll duration (ms) */
946 bo_addle_u32( &bo
, b_broadcast
? 0x01 : 0x02 /* seekable */ ); /* flags */
947 bo_addle_u32( &bo
, p_sys
->i_packet_size
); /* packet size min */
948 bo_addle_u32( &bo
, p_sys
->i_packet_size
); /* packet size max */
949 /* NOTE: According to p6-9 of the ASF specification the bitrate cannot be 0,
950 * therefor apply this workaround to make sure it is not 0. If the bitrate is
951 * 0 the file will play in WMP11, but not in Sliverlight and WMP12 */
952 bo_addle_u32( &bo
, p_sys
->i_bitrate
> 0 ? p_sys
->i_bitrate
: 1 ); /* maxbitrate */
954 /* header extension */
955 bo_add_guid ( &bo
, &asf_object_header_extension_guid
);
956 bo_addle_u64( &bo
, i_header_ext_size
);
957 bo_add_guid ( &bo
, &asf_guid_reserved_1
);
958 bo_addle_u16( &bo
, 6 );
959 bo_addle_u32( &bo
, i_header_ext_size
- 46 );
961 /* extended stream properties */
962 for( size_t i
= 0; i
< vlc_array_count( &p_sys
->tracks
); i
++ )
964 const asf_track_t
*p_track
= vlc_array_item_at_index( &p_sys
->tracks
, i
);
965 const es_format_t
*p_fmt
= &p_track
->fmt
;
967 if( !p_track
->b_extended
)
970 uint64_t i_avg_duration
= 0;
971 if( p_fmt
->i_cat
== VIDEO_ES
&&
972 p_fmt
->video
.i_frame_rate
> 0 && p_fmt
->video
.i_frame_rate_base
> 0 )
973 i_avg_duration
= ( INT64_C(10000000) * p_fmt
->video
.i_frame_rate_base
+
974 p_fmt
->video
.i_frame_rate
/2 ) / p_fmt
->video
.i_frame_rate
;
976 bo_add_guid ( &bo
, &asf_object_extended_stream_properties_guid
);
977 bo_addle_u64( &bo
, 88 );
978 bo_addle_u64( &bo
, 0 );
979 bo_addle_u64( &bo
, 0 );
980 bo_addle_u32( &bo
, p_fmt
->i_bitrate
); /* Bitrate */
981 bo_addle_u32( &bo
, p_sys
->i_preroll_time
); /* Buffer size */
982 bo_addle_u32( &bo
, 0 ); /* Initial buffer fullness */
983 bo_addle_u32( &bo
, p_fmt
->i_bitrate
); /* Alternate Bitrate */
984 bo_addle_u32( &bo
, 0 ); /* Alternate Buffer size */
985 bo_addle_u32( &bo
, 0 ); /* Alternate Initial buffer fullness */
986 bo_addle_u32( &bo
, 0 ); /* Maximum object size (0 = unknown) */
987 bo_addle_u32( &bo
, 0x02 ); /* Flags (seekable) */
988 bo_addle_u16( &bo
, p_track
->i_id
); /* Stream number */
989 bo_addle_u16( &bo
, 0 ); /* Stream language index */
990 bo_addle_u64( &bo
, i_avg_duration
); /* Average time per frame */
991 bo_addle_u16( &bo
, 0 ); /* Stream name count */
992 bo_addle_u16( &bo
, 0 ); /* Payload extension system count */
995 /* metadata object (part of header extension) */
998 unsigned int i_dst_num
, i_dst_den
;
1000 asf_track_t
*tk
= NULL
;
1001 for( size_t i
= 0; i
< vlc_array_count( &p_sys
->tracks
); i
++ )
1003 tk
= vlc_array_item_at_index( &p_sys
->tracks
, i
);
1004 if( tk
->i_cat
== VIDEO_ES
&&
1005 tk
->fmt
.video
.i_sar_num
!= 0 &&
1006 tk
->fmt
.video
.i_sar_den
!= 0 )
1008 vlc_ureduce( &i_dst_num
, &i_dst_den
,
1009 tk
->fmt
.video
.i_sar_num
,
1010 tk
->fmt
.video
.i_sar_den
, 0 );
1014 assert( tk
!= NULL
);
1016 msg_Dbg( p_mux
, "pixel aspect-ratio: %i/%i", i_dst_num
, i_dst_den
);
1018 bo_add_guid ( &bo
, &asf_object_metadata_guid
);
1019 bo_addle_u64( &bo
, i_cm_size
);
1020 bo_addle_u16( &bo
, 2 ); /* description records count */
1021 /* 1st description record */
1022 bo_addle_u16( &bo
, 0 ); /* reserved */
1023 bo_addle_u16( &bo
, tk
->i_id
); /* stream number (0 for the whole file) */
1024 bo_addle_u16( &bo
, 2 * sizeof("AspectRatioX") ); /* name length */
1025 bo_addle_u16( &bo
, 0x3 /* DWORD */ ); /* data type */
1026 bo_addle_u32( &bo
, 4 ); /* data length */
1027 bo_addle_str16_nosize( &bo
, "AspectRatioX" );
1028 bo_addle_u32( &bo
, i_dst_num
); /* data */
1029 /* 2nd description record */
1030 bo_addle_u16( &bo
, 0 ); /* reserved */
1031 bo_addle_u16( &bo
, tk
->i_id
); /* stream number (0 for the whole file) */
1032 bo_addle_u16( &bo
, 2 * sizeof("AspectRatioY") ); /* name length */
1033 bo_addle_u16( &bo
, 0x3 /* DWORD */ ); /* data type */
1034 bo_addle_u32( &bo
, 4 ); /* data length */
1035 bo_addle_str16_nosize( &bo
, "AspectRatioY" );
1036 bo_addle_u32( &bo
, i_dst_den
); /* data */
1039 /* content description header */
1042 bo_add_guid ( &bo
, &asf_object_content_description_guid
);
1043 bo_addle_u64( &bo
, i_cd_size
);
1044 bo_addle_u16( &bo
, 2 * strlen( p_sys
->psz_title
) + 2 );
1045 bo_addle_u16( &bo
, 2 * strlen( p_sys
->psz_author
) + 2 );
1046 bo_addle_u16( &bo
, 2 * strlen( p_sys
->psz_copyright
) + 2 );
1047 bo_addle_u16( &bo
, 2 * strlen( p_sys
->psz_comment
) + 2 );
1048 bo_addle_u16( &bo
, 2 * strlen( p_sys
->psz_rating
) + 2 );
1050 bo_addle_str16_nosize( &bo
, p_sys
->psz_title
);
1051 bo_addle_str16_nosize( &bo
, p_sys
->psz_author
);
1052 bo_addle_str16_nosize( &bo
, p_sys
->psz_copyright
);
1053 bo_addle_str16_nosize( &bo
, p_sys
->psz_comment
);
1054 bo_addle_str16_nosize( &bo
, p_sys
->psz_rating
);
1057 /* stream properties */
1058 for( size_t i
= 0; i
< vlc_array_count( &p_sys
->tracks
); i
++ )
1060 tk
= vlc_array_item_at_index( &p_sys
->tracks
, i
);
1062 bo_add_guid ( &bo
, &asf_object_stream_properties_guid
);
1063 bo_addle_u64( &bo
, 78 + tk
->i_extra
+ (tk
->b_audio_correction
? 8:0) );
1065 if( tk
->i_cat
== AUDIO_ES
)
1067 bo_add_guid( &bo
, &asf_object_stream_type_audio
);
1068 if( tk
->b_audio_correction
)
1069 bo_add_guid( &bo
, &asf_guid_audio_conceal_spread
);
1071 bo_add_guid( &bo
, &asf_no_error_correction_guid
);
1073 else if( tk
->i_cat
== VIDEO_ES
)
1075 bo_add_guid( &bo
, &asf_object_stream_type_video
);
1076 bo_add_guid( &bo
, &asf_no_error_correction_guid
);
1078 bo_addle_u64( &bo
, 0 ); /* time offset */
1079 bo_addle_u32( &bo
, tk
->i_extra
);
1080 /* correction data length */
1081 bo_addle_u32( &bo
, tk
->b_audio_correction
? 8 : 0 );
1082 bo_addle_u16( &bo
, tk
->i_id
); /* stream number */
1083 bo_addle_u32( &bo
, 0 );
1084 bo_add_mem ( &bo
, tk
->p_extra
, tk
->i_extra
);
1086 /* Error correction data field */
1087 if( tk
->b_audio_correction
)
1089 bo_add_u8( &bo
, 0x1 ); /* span */
1090 bo_addle_u16( &bo
, tk
->i_blockalign
); /* virtual packet length */
1091 bo_addle_u16( &bo
, tk
->i_blockalign
); /* virtual chunck length */
1092 bo_addle_u16( &bo
, 1 ); /* silence length */
1093 bo_add_u8( &bo
, 0x0 ); /* data */
1098 bo_add_guid ( &bo
, &asf_object_codec_list_guid
);
1099 bo_addle_u64( &bo
, i_ci_size
);
1100 bo_add_guid ( &bo
, &asf_guid_reserved_2
);
1101 bo_addle_u32( &bo
, vlc_array_count( &p_sys
->tracks
) );
1102 for( size_t i
= 0; i
< vlc_array_count( &p_sys
->tracks
); i
++ )
1104 tk
= vlc_array_item_at_index( &p_sys
->tracks
,i
);
1106 if( tk
->i_cat
== VIDEO_ES
) bo_addle_u16( &bo
, 1 /* video */ );
1107 else if( tk
->i_cat
== AUDIO_ES
) bo_addle_u16( &bo
, 2 /* audio */ );
1108 else bo_addle_u16( &bo
, 0xFFFF /* unknown */ );
1110 bo_addle_str16( &bo
, tk
->psz_name
);
1111 bo_addle_u16( &bo
, 0 );
1112 if( tk
->i_cat
== AUDIO_ES
)
1114 bo_addle_u16( &bo
, 2 );
1115 bo_addle_u16( &bo
, tk
->i_tag
);
1117 else if( tk
->i_cat
== VIDEO_ES
)
1119 bo_addle_u16( &bo
, 4 );
1120 bo_add_mem ( &bo
, (uint8_t*)&tk
->i_fourcc
, 4 );
1125 bo_add_guid ( &bo
, &asf_object_data_guid
);
1126 bo_addle_u64( &bo
, 50 + p_sys
->i_packet_count
* p_sys
->i_packet_size
);
1127 bo_add_guid ( &bo
, &p_sys
->fid
);
1128 bo_addle_u64( &bo
, p_sys
->i_packet_count
);
1129 bo_addle_u16( &bo
, 0x101 );
1134 /****************************************************************************
1136 ****************************************************************************/
1137 static block_t
*asf_packet_flush( sout_mux_t
*p_mux
)
1139 sout_mux_sys_t
*p_sys
= p_mux
->p_sys
;
1140 int i_pad
, i_preheader
= p_sys
->b_asf_http
? 12 : 0;
1144 if( !p_sys
->pk
) return 0;
1146 i_pad
= p_sys
->i_packet_size
- p_sys
->i_pk_used
;
1147 memset( p_sys
->pk
->p_buffer
+ p_sys
->i_pk_used
, 0, i_pad
);
1149 bo_init( &bo
, p_sys
->pk
->p_buffer
, 14 + i_preheader
);
1151 if( p_sys
->b_asf_http
)
1152 asf_chunk_add( &bo
, 0x4424, p_sys
->i_packet_size
, 0x0, p_sys
->i_seq
++);
1154 bo_add_u8 ( &bo
, 0x82 );
1155 bo_addle_u16( &bo
, 0 );
1156 bo_add_u8( &bo
, 0x11 );
1157 bo_add_u8( &bo
, 0x5d );
1158 bo_addle_u16( &bo
, i_pad
);
1159 bo_addle_u32( &bo
, (p_sys
->i_pk_dts
- p_sys
->i_dts_first
) / 1000 +
1160 p_sys
->i_preroll_time
);
1161 bo_addle_u16( &bo
, 0 /* data->i_length */ );
1162 bo_add_u8( &bo
, 0x80 | p_sys
->i_pk_frame
);
1167 p_sys
->i_packet_count
++;
1172 static block_t
*asf_packet_create( sout_mux_t
*p_mux
,
1173 asf_track_t
*tk
, block_t
*data
)
1175 sout_mux_sys_t
*p_sys
= p_mux
->p_sys
;
1177 int i_data
= data
->i_buffer
;
1179 uint8_t *p_data
= data
->p_buffer
;
1180 block_t
*first
= NULL
, **last
= &first
;
1181 int i_preheader
= p_sys
->b_asf_http
? 12 : 0;
1183 while( i_pos
< i_data
)
1188 if( p_sys
->pk
== NULL
)
1190 p_sys
->pk
= block_Alloc( p_sys
->i_packet_size
+ i_preheader
);
1191 /* reserve 14 bytes for the packet header */
1192 p_sys
->i_pk_used
= 14 + i_preheader
;
1193 p_sys
->i_pk_frame
= 0;
1194 p_sys
->i_pk_dts
= data
->i_dts
;
1197 bo_init( &bo
, &p_sys
->pk
->p_buffer
[p_sys
->i_pk_used
],
1198 p_sys
->i_packet_size
- p_sys
->i_pk_used
);
1200 /* add payload (header size = 17) */
1201 i_payload
= __MIN( i_data
- i_pos
,
1202 p_sys
->i_packet_size
- p_sys
->i_pk_used
- 17 );
1204 if( tk
->b_audio_correction
&& p_sys
->i_pk_frame
&& i_payload
< i_data
)
1206 /* Don't know why yet but WMP doesn't like splitted WMA packets */
1207 *last
= asf_packet_flush( p_mux
);
1208 last
= &(*last
)->p_next
;
1212 bo_add_u8 ( &bo
, !(data
->i_flags
& BLOCK_FLAG_TYPE_P
||
1213 data
->i_flags
& BLOCK_FLAG_TYPE_B
) ?
1214 0x80 | tk
->i_id
: tk
->i_id
);
1215 bo_add_u8 ( &bo
, tk
->i_sequence
);
1216 bo_addle_u32( &bo
, i_pos
);
1217 bo_add_u8 ( &bo
, 0x08 ); /* flags */
1218 bo_addle_u32( &bo
, i_data
);
1219 bo_addle_u32( &bo
, (data
->i_dts
- p_sys
->i_dts_first
) / 1000 +
1220 p_sys
->i_preroll_time
);
1221 bo_addle_u16( &bo
, i_payload
);
1222 bo_add_mem ( &bo
, &p_data
[i_pos
], i_payload
);
1224 p_sys
->i_pk_used
+= 17 + i_payload
;
1226 p_sys
->i_pk_frame
++;
1228 if( p_sys
->i_pk_used
+ 17 >= p_sys
->i_packet_size
)
1230 /* Not enough data for another payload, flush the packet */
1231 *last
= asf_packet_flush( p_mux
);
1232 last
= &(*last
)->p_next
;
1237 block_Release( data
);
1242 static block_t
*asf_stream_end_create( sout_mux_t
*p_mux
)
1244 sout_mux_sys_t
*p_sys
= p_mux
->p_sys
;
1246 block_t
*out
= NULL
;
1249 if( p_sys
->b_asf_http
)
1251 out
= block_Alloc( 12 );
1252 bo_init( &bo
, out
->p_buffer
, 12 );
1253 asf_chunk_add( &bo
, 0x4524, 0, 0x00, p_sys
->i_seq
++ );
1258 out
= block_Alloc( 56 );
1259 bo_init( &bo
, out
->p_buffer
, 56 );
1260 bo_add_guid ( &bo
, &asf_object_index_guid
);
1261 bo_addle_u64( &bo
, 56 );
1262 bo_add_guid ( &bo
, &p_sys
->fid
);
1263 bo_addle_u64( &bo
, 10000000 );
1264 bo_addle_u32( &bo
, 5 );
1265 bo_addle_u32( &bo
, 0 );