1 /*****************************************************************************
2 * asf.c: asf muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2003-2004, 2006 the VideoLAN team
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
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <vlc_block.h>
37 #include <vlc_codecs.h>
41 #define MAX_ASF_TRACKS 128
42 #define ASF_DATA_PACKET_SIZE 4096 // deprecated -- added sout-asf-packet-size
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Open ( vlc_object_t * );
48 static void Close ( vlc_object_t * );
50 #define SOUT_CFG_PREFIX "sout-asf-"
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")
69 set_description( N_("ASF muxer") )
70 set_category( CAT_SOUT )
71 set_subcategory( SUBCAT_SOUT_MUX )
72 set_shortname( "ASF" )
74 set_capability( "sout mux", 5 )
76 add_shortcut( "asfh" )
77 set_callbacks( Open, Close )
79 add_string( SOUT_CFG_PREFIX "title", "", NULL, TITLE_TEXT, TITLE_LONGTEXT,
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 )
96 /*****************************************************************************
98 *****************************************************************************/
99 static const char *const ppsz_sout_options[] = {
100 "title", "author", "copyright", "comment", "rating", NULL
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 * );
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;
130 struct sout_mux_sys_t
132 guid_t fid; /* file id */
134 int64_t i_packet_count;
137 mtime_t i_preroll_time;
139 int64_t i_bitrate_override;
142 asf_track_t track[MAX_ASF_TRACKS];
162 static int MuxGetStream( sout_mux_t *, int *pi_stream, mtime_t *pi_dts );
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;
196 msg_Dbg( p_mux, "asf muxer opened" );
197 config_ChainParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
199 p_mux->pf_control = Control;
200 p_mux->pf_addstream = AddStream;
201 p_mux->pf_delstream = DelStream;
204 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
207 p_sys->b_asf_http = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "asfh" );
208 if( p_sys->b_asf_http )
210 msg_Dbg( p_mux, "creating asf stream to be used with mmsh" );
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;
222 p_sys->b_write_header = true;
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;
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++ )
238 p_sys->fid.Data4[i] = ( (uint64_t)rand() << 8 ) / RAND_MAX;
242 var_Get( p_mux, SOUT_CFG_PREFIX "title", &val );
243 p_sys->psz_title = val.psz_string;
245 var_Get( p_mux, SOUT_CFG_PREFIX "author", &val );
246 p_sys->psz_author = val.psz_string;
248 var_Get( p_mux, SOUT_CFG_PREFIX "copyright", &val );
249 p_sys->psz_copyright = val.psz_string;
251 var_Get( p_mux, SOUT_CFG_PREFIX "comment", &val );
252 p_sys->psz_comment = val.psz_string;
254 var_Get( p_mux, SOUT_CFG_PREFIX "rating", &val );
255 p_sys->psz_rating = val.psz_string;
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 );
265 /*****************************************************************************
267 *****************************************************************************/
268 static void Close( vlc_object_t * p_this )
270 sout_mux_t *p_mux = (sout_mux_t*)p_this;
271 sout_mux_sys_t *p_sys = p_mux->p_sys;
275 msg_Dbg( p_mux, "Asf muxer closed" );
277 /* Flush last packet if any */
278 if( (out = asf_packet_flush( p_mux ) ) )
280 sout_AccessOutWrite( p_mux->p_access, out );
283 if( ( out = asf_stream_end_create( p_mux ) ) )
285 sout_AccessOutWrite( p_mux->p_access, out );
289 if( sout_AccessOutSeek( p_mux->p_access, 0 ) == VLC_SUCCESS )
291 out = asf_header_create( p_mux, false );
292 sout_AccessOutWrite( p_mux->p_access, out );
295 for( i = 0; i < p_sys->i_track; i++ )
297 free( p_sys->track[i].p_extra );
298 es_format_Clean( &p_sys->track[i].fmt );
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 );
309 /*****************************************************************************
311 *****************************************************************************/
312 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
314 sout_mux_sys_t *p_sys = p_mux->p_sys;
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;
326 case MUX_GET_ADD_STREAM_WAIT:
327 pb_bool = (bool*)va_arg( args, bool * );
332 ppsz = (char**)va_arg( args, char ** );
333 if( p_sys->b_asf_http )
334 *ppsz = strdup( "video/x-ms-asf-stream" );
336 *ppsz = strdup( "video/x-ms-asf" );
344 /*****************************************************************************
346 *****************************************************************************/
347 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
349 sout_mux_sys_t *p_sys = p_mux->p_sys;
353 msg_Dbg( p_mux, "adding input" );
354 if( p_sys->i_track >= MAX_ASF_TRACKS )
356 msg_Dbg( p_mux, "cannot add this track (too much tracks)" );
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;
364 tk->b_audio_correction = 0;
365 tk->b_extended = false;
371 int i_blockalign = p_input->p_fmt->audio.i_blockalign;
372 int i_bitspersample = p_input->p_fmt->audio.i_bitspersample;
375 switch( p_input->p_fmt->i_codec )
378 tk->i_tag = WAVE_FORMAT_A52;
379 tk->psz_name = "A/52";
383 tk->i_tag = WAVE_FORMAT_AAC;
384 tk->psz_name = "MPEG-4 Audio";
389 tk->psz_name = "MPEG Audio Layer 3";
390 tk->i_tag = WAVE_FORMAT_MPEGLAYER3;
396 tk->psz_name = "MPEG Audio Layer 1/2";
397 tk->i_tag = WAVE_FORMAT_MPEG;
404 tk->psz_name = "Windows Media Audio v1";
405 tk->i_tag = WAVE_FORMAT_WMA1;
406 tk->b_audio_correction = true;
409 tk->psz_name= "Windows Media Audio (v2) 7, 8 and 9 Series";
410 tk->i_tag = WAVE_FORMAT_WMA2;
411 tk->b_audio_correction = true;
414 tk->psz_name = "Windows Media Audio 9 Professional";
415 tk->i_tag = WAVE_FORMAT_WMAP;
416 tk->b_audio_correction = true;
419 tk->psz_name = "Windows Media Audio 9 Lossless";
420 tk->i_tag = WAVE_FORMAT_WMAL;
421 tk->b_audio_correction = true;
425 tk->psz_name = "Raw audio 8bits";
426 tk->i_tag = WAVE_FORMAT_PCM;
427 i_blockalign= p_input->p_fmt->audio.i_channels;
431 tk->psz_name = "Raw audio 16bits";
432 tk->i_tag = WAVE_FORMAT_PCM;
433 i_blockalign= 2 * p_input->p_fmt->audio.i_channels;
434 i_bitspersample = 16;
437 tk->psz_name = "Raw audio 24bits";
438 tk->i_tag = WAVE_FORMAT_PCM;
439 i_blockalign= 3 * p_input->p_fmt->audio.i_channels;
440 i_bitspersample = 24;
443 tk->psz_name = "Raw audio 32bits";
444 tk->i_tag = WAVE_FORMAT_PCM;
445 i_blockalign= 4 * p_input->p_fmt->audio.i_channels;
446 i_bitspersample = 32;
452 tk->i_extra = sizeof( WAVEFORMATEX ) +
453 p_input->p_fmt->i_extra + i_extra;
454 tk->p_extra = malloc( tk->i_extra );
457 bo_init( &bo, tk->p_extra, tk->i_extra );
458 bo_addle_u16( &bo, tk->i_tag );
459 bo_addle_u16( &bo, p_input->p_fmt->audio.i_channels );
460 bo_addle_u32( &bo, p_input->p_fmt->audio.i_rate );
461 bo_addle_u32( &bo, p_input->p_fmt->i_bitrate / 8 );
462 bo_addle_u16( &bo, i_blockalign );
463 tk->i_blockalign = i_blockalign;
464 bo_addle_u16( &bo, i_bitspersample );
465 if( p_input->p_fmt->i_extra > 0 )
467 bo_addle_u16( &bo, p_input->p_fmt->i_extra );
468 bo_add_mem ( &bo, p_input->p_fmt->p_extra,
469 p_input->p_fmt->i_extra );
473 bo_addle_u16( &bo, i_extra );
474 if( tk->i_tag == WAVE_FORMAT_MPEGLAYER3 )
476 msg_Dbg( p_mux, "adding mp3 header" );
477 bo_addle_u16( &bo, 1 ); /* wId */
478 bo_addle_u32( &bo, 2 ); /* fdwFlags */
479 bo_addle_u16( &bo, 1152 ); /* nBlockSize */
480 bo_addle_u16( &bo, 1 ); /* nFramesPerBlock */
481 bo_addle_u16( &bo, 1393 ); /* nCodecDelay */
483 else if( tk->i_tag == WAVE_FORMAT_MPEG )
485 msg_Dbg( p_mux, "adding mp2 header" );
486 bo_addle_u16( &bo, 2 ); /* fwHeadLayer */
487 bo_addle_u32( &bo, p_input->p_fmt->i_bitrate );
488 bo_addle_u16( &bo, p_input->p_fmt->audio.i_channels == 2 ?1:8 );
489 bo_addle_u16( &bo, 0 ); /* fwHeadModeExt */
490 bo_addle_u16( &bo, 1 ); /* wHeadEmphasis */
491 bo_addle_u16( &bo, 16 ); /* fwHeadFlags */
492 bo_addle_u32( &bo, 0 ); /* dwPTSLow */
493 bo_addle_u32( &bo, 0 ); /* dwPTSHigh */
497 if( p_input->p_fmt->i_bitrate > 24000 )
499 p_sys->i_bitrate += p_input->p_fmt->i_bitrate;
503 p_sys->i_bitrate += 128000;
505 if (p_sys->i_bitrate_override)
506 p_sys->i_bitrate = p_sys->i_bitrate_override;
511 const es_format_t *p_fmt = p_input->p_fmt;
512 uint8_t *p_codec_extra = NULL;
513 int i_codec_extra = 0;
515 if( p_input->p_fmt->i_codec == VLC_CODEC_MP4V )
517 tk->psz_name = "MPEG-4 Video";
518 tk->i_fourcc = VLC_FOURCC( 'M', 'P', '4', 'S' );
520 else if( p_input->p_fmt->i_codec == VLC_CODEC_DIV3 )
522 tk->psz_name = "MSMPEG-4 V3 Video";
523 tk->i_fourcc = VLC_FOURCC( 'M', 'P', '4', '3' );
525 else if( p_input->p_fmt->i_codec == VLC_CODEC_DIV2 )
527 tk->psz_name = "MSMPEG-4 V2 Video";
528 tk->i_fourcc = VLC_FOURCC( 'M', 'P', '4', '2' );
530 else if( p_input->p_fmt->i_codec == VLC_CODEC_DIV2 )
532 tk->psz_name = "MSMPEG-4 V1 Video";
533 tk->i_fourcc = VLC_FOURCC( 'M', 'P', 'G', '4' );
535 else if( p_input->p_fmt->i_codec == VLC_CODEC_WMV1 )
537 tk->psz_name = "Windows Media Video 7";
538 tk->i_fourcc = VLC_FOURCC( 'W', 'M', 'V', '1' );
540 else if( p_input->p_fmt->i_codec == VLC_CODEC_WMV2 )
542 tk->psz_name = "Windows Media Video 8";
543 tk->i_fourcc = VLC_FOURCC( 'W', 'M', 'V', '2' );
545 else if( p_input->p_fmt->i_codec == VLC_CODEC_WMV3 )
547 tk->psz_name = "Windows Media Video 9";
548 tk->i_fourcc = VLC_FOURCC( 'W', 'M', 'V', '3' );
550 else if( p_input->p_fmt->i_codec == VLC_CODEC_VC1 )
552 tk->psz_name = "Windows Media Video 9 Advanced Profile";
553 tk->i_fourcc = VLC_FOURCC( 'W', 'V', 'C', '1' );
554 tk->b_extended = true;
556 if( p_fmt->i_extra > 0 )
558 p_codec_extra = malloc( 1 + p_fmt->i_extra );
561 i_codec_extra = 1 + p_fmt->i_extra;
562 p_codec_extra[0] = 0x01;
563 memcpy( &p_codec_extra[1], p_fmt->p_extra, p_fmt->i_extra );
567 else if( p_input->p_fmt->i_codec == VLC_CODEC_H264 )
569 tk->psz_name = "H.264/MPEG-4 AVC";
570 tk->i_fourcc = VLC_FOURCC('h','2','6','4');
574 tk->psz_name = _("Unknown Video");
575 tk->i_fourcc = p_input->p_fmt->i_original_fourcc ?: p_input->p_fmt->i_codec;
577 if( !i_codec_extra && p_fmt->i_extra > 0 )
579 p_codec_extra = malloc( p_fmt->i_extra );
582 i_codec_extra = p_fmt->i_extra;
583 memcpy( p_codec_extra, p_fmt->p_extra, p_fmt->i_extra );
587 tk->i_extra = 11 + sizeof( BITMAPINFOHEADER ) + i_codec_extra;
588 tk->p_extra = malloc( tk->i_extra );
591 free( p_codec_extra );
594 bo_init( &bo, tk->p_extra, tk->i_extra );
595 bo_addle_u32( &bo, p_input->p_fmt->video.i_width );
596 bo_addle_u32( &bo, p_input->p_fmt->video.i_height );
597 bo_add_u8 ( &bo, 0x02 ); /* flags */
598 bo_addle_u16( &bo, sizeof( BITMAPINFOHEADER ) + i_codec_extra );
599 bo_addle_u32( &bo, sizeof( BITMAPINFOHEADER ) + i_codec_extra );
600 bo_addle_u32( &bo, p_input->p_fmt->video.i_width );
601 bo_addle_u32( &bo, p_input->p_fmt->video.i_height );
602 bo_addle_u16( &bo, 1 );
603 bo_addle_u16( &bo, 24 );
604 bo_add_mem( &bo, (uint8_t*)&tk->i_fourcc, 4 );
605 bo_addle_u32( &bo, 0 );
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 if( i_codec_extra > 0 )
612 bo_add_mem( &bo, p_codec_extra, i_codec_extra );
613 free( p_codec_extra );
616 if( p_input->p_fmt->i_bitrate > 50000 )
618 p_sys->i_bitrate += p_input->p_fmt->i_bitrate;
622 p_sys->i_bitrate += 512000;
624 if( p_sys->i_bitrate_override )
625 p_sys->i_bitrate = p_sys->i_bitrate_override;
629 msg_Err(p_mux, "unhandled track type" );
633 es_format_Copy( &tk->fmt, p_input->p_fmt );
639 /*****************************************************************************
641 *****************************************************************************/
642 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
644 /* if bitrate ain't defined in commanline, reduce it when tracks are deleted
646 sout_mux_sys_t *p_sys = p_mux->p_sys;
647 asf_track_t *tk = p_input->p_sys;
648 if(!p_sys->i_bitrate_override)
650 if( tk->i_cat == AUDIO_ES )
652 if( p_input->p_fmt->i_bitrate > 24000 )
653 p_sys->i_bitrate -= p_input->p_fmt->i_bitrate;
655 p_sys->i_bitrate -= 128000;
657 else if(tk->i_cat == VIDEO_ES )
659 if( p_input->p_fmt->i_bitrate > 50000 )
660 p_sys->i_bitrate -= p_input->p_fmt->i_bitrate;
662 p_sys->i_bitrate -= 512000;
665 msg_Dbg( p_mux, "removing input" );
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;
695 if( MuxGetStream( p_mux, &i_stream, &i_dts ) )
697 /* not enough data */
701 if( p_sys->i_dts_first < 0 )
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 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
748 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
750 sout_input_t *p_input = p_mux->pp_inputs[i];
753 if( block_FifoCount( p_input->p_fifo ) <= 0 )
755 if( p_input->p_fmt->i_cat == AUDIO_ES ||
756 p_input->p_fmt->i_cat == VIDEO_ES )
758 /* We need that audio+video fifo contain at least 1 packet */
765 p_data = block_FifoShow( p_input->p_fifo );
766 if( i_stream == -1 || p_data->i_dts < i_dts )
769 i_dts = p_data->i_dts;
773 *pi_stream = i_stream;
779 /****************************************************************************
780 * Asf header construction
781 ****************************************************************************/
783 /****************************************************************************
785 ****************************************************************************/
786 static void bo_init( bo_t *p_bo, uint8_t *p_buffer, int i_size )
788 p_bo->i_buffer_size = i_size;
790 p_bo->p_buffer = p_buffer;
792 static void bo_add_u8( bo_t *p_bo, uint8_t i )
794 if( p_bo->i_buffer < p_bo->i_buffer_size )
796 p_bo->p_buffer[p_bo->i_buffer] = i;
800 static void bo_addle_u16( bo_t *p_bo, uint16_t i )
802 bo_add_u8( p_bo, i &0xff );
803 bo_add_u8( p_bo, ( ( i >> 8) &0xff ) );
805 static void bo_addle_u32( bo_t *p_bo, uint32_t i )
807 bo_addle_u16( p_bo, i &0xffff );
808 bo_addle_u16( p_bo, ( ( i >> 16) &0xffff ) );
810 static void bo_addle_u64( bo_t *p_bo, uint64_t i )
812 bo_addle_u32( p_bo, i &0xffffffff );
813 bo_addle_u32( p_bo, ( ( i >> 32) &0xffffffff ) );
816 static void bo_add_mem( bo_t *p_bo, uint8_t *p_mem, int i_size )
818 int i_copy = __MIN( i_size, p_bo->i_buffer_size - p_bo->i_buffer );
822 memcpy( &p_bo->p_buffer[p_bo->i_buffer], p_mem, i_copy );
824 p_bo->i_buffer += i_size;
827 static void bo_addle_str16( bo_t *bo, const char *str )
829 bo_addle_u16( bo, strlen( str ) + 1 );
832 uint16_t c = (uint8_t)*str++;
833 bo_addle_u16( bo, c );
834 if( c == '\0' ) break;
838 static void bo_addle_str16_nosize( bo_t *bo, const char *str )
842 uint16_t c = (uint8_t)*str++;
843 bo_addle_u16( bo, c );
844 if( c == '\0' ) break;
848 /****************************************************************************
850 ****************************************************************************/
851 static void bo_add_guid( bo_t *p_bo, const guid_t *id )
854 bo_addle_u32( p_bo, id->Data1 );
855 bo_addle_u16( p_bo, id->Data2 );
856 bo_addle_u16( p_bo, id->Data3 );
857 for( i = 0; i < 8; i++ )
859 bo_add_u8( p_bo, id->Data4[i] );
863 static const guid_t asf_object_header_guid =
864 {0x75B22630, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
865 static const guid_t asf_object_data_guid =
866 {0x75B22636, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
867 static const guid_t asf_object_file_properties_guid =
868 {0x8cabdca1, 0xa947, 0x11cf, {0x8e, 0xe4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
869 static const guid_t asf_object_stream_properties_guid =
870 {0xB7DC0791, 0xA9B7, 0x11CF, {0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
871 static const guid_t asf_object_header_extension_guid =
872 {0x5FBF03B5, 0xA92E, 0x11CF, {0x8E, 0xE3, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
873 static const guid_t asf_object_stream_type_audio =
874 {0xF8699E40, 0x5B4D, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
875 static const guid_t asf_object_stream_type_video =
876 {0xbc19efc0, 0x5B4D, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
877 static const guid_t asf_guid_audio_conceal_none =
878 {0x20FB5700, 0x5B55, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
879 static const guid_t asf_guid_audio_conceal_spread =
880 {0xBFC3CD50, 0x618F, 0x11CF, {0x8B, 0xB2, 0x00, 0xAA, 0x00, 0xB4, 0xE2, 0x20}};
881 static const guid_t asf_guid_video_conceal_none =
882 {0x20FB5700, 0x5B55, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
883 static const guid_t asf_guid_reserved_1 =
884 {0xABD3D211, 0xA9BA, 0x11cf, {0x8E, 0xE6, 0x00, 0xC0, 0x0C ,0x20, 0x53, 0x65}};
885 static const guid_t asf_object_codec_list_guid =
886 {0x86D15240, 0x311D, 0x11D0, {0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6}};
887 static const guid_t asf_object_codec_list_reserved_guid =
888 {0x86D15241, 0x311D, 0x11D0, {0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6}};
889 static const guid_t asf_object_content_description_guid =
890 {0x75B22633, 0x668E, 0x11CF, {0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c}};
891 static const guid_t asf_object_index_guid =
892 {0x33000890, 0xE5B1, 0x11CF, {0x89, 0xF4, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xCB}};
893 static const guid_t asf_object_metadata_guid =
894 {0xC5F8CBEA, 0x5BAF, 0x4877, {0x84, 0x67, 0xAA, 0x8C, 0x44, 0xFA, 0x4C, 0xCA}};
895 static const guid_t asf_object_extended_stream_properties_guid =
896 {0x14E6A5CB, 0xC672, 0x4332, {0x83, 0x99, 0xA9, 0x69, 0x52, 0x06, 0x5B, 0x5A}};
898 /****************************************************************************
900 ****************************************************************************/
901 static void asf_chunk_add( bo_t *bo,
902 int i_type, int i_len, int i_flags, int i_seq )
904 bo_addle_u16( bo, i_type );
905 bo_addle_u16( bo, i_len + 8 );
906 bo_addle_u32( bo, i_seq );
907 bo_addle_u16( bo, i_flags );
908 bo_addle_u16( bo, i_len + 8 );
911 static block_t *asf_header_create( sout_mux_t *p_mux, bool b_broadcast )
913 sout_mux_sys_t *p_sys = p_mux->p_sys;
915 mtime_t i_duration = 0;
916 int i_size, i_header_ext_size, i;
917 int i_ci_size, i_cm_size = 0, i_cd_size = 0;
921 msg_Dbg( p_mux, "Asf muxer creating header" );
923 if( p_sys->i_dts_first > 0 )
925 i_duration = p_sys->i_dts_last - p_sys->i_dts_first;
926 if( i_duration < 0 ) i_duration = 0;
929 /* calculate header size */
932 for( i = 0; i < p_sys->i_track; i++ )
934 i_size += 78 + p_sys->track[i].i_extra;
935 i_ci_size += 8 + 2 * strlen( p_sys->track[i].psz_name );
936 if( p_sys->track[i].i_cat == AUDIO_ES ) i_ci_size += 4;
937 else if( p_sys->track[i].i_cat == VIDEO_ES ) i_ci_size += 6;
939 /* Error correction data field */
940 if( p_sys->track[i].b_audio_correction ) i_size += 8;
943 /* size of the content description object */
944 if( *p_sys->psz_title || *p_sys->psz_author || *p_sys->psz_copyright ||
945 *p_sys->psz_comment || *p_sys->psz_rating )
947 i_cd_size = 34 + 2 * ( strlen( p_sys->psz_title ) + 1 +
948 strlen( p_sys->psz_author ) + 1 +
949 strlen( p_sys->psz_copyright ) + 1 +
950 strlen( p_sys->psz_comment ) + 1 +
951 strlen( p_sys->psz_rating ) + 1 );
954 i_header_ext_size = 46;
956 /* size of the metadata object */
957 for( i = 0; i < p_sys->i_track; i++ )
959 const asf_track_t *p_track = &p_sys->track[i];
960 if( p_track->i_cat == VIDEO_ES && p_track->fmt.video.i_aspect != 0 )
962 i_cm_size = 26 + 2 * (16 + 2 * sizeof("AspectRatio?"));
965 if( p_track->b_extended )
966 i_header_ext_size += 88;
970 i_header_ext_size += i_cm_size;
972 i_size += i_ci_size + i_cd_size + i_header_ext_size ;
974 if( p_sys->b_asf_http )
976 out = block_New( p_mux, i_size + 50 + 12 );
977 bo_init( &bo, out->p_buffer, i_size + 50 + 12 );
978 asf_chunk_add( &bo, 0x4824, i_size + 50, 0xc00, p_sys->i_seq++ );
982 out = block_New( p_mux, i_size + 50 );
983 bo_init( &bo, out->p_buffer, i_size + 50 );
987 bo_add_guid ( &bo, &asf_object_header_guid );
988 bo_addle_u64( &bo, i_size );
989 bo_addle_u32( &bo, 2 + p_sys->i_track +
990 (i_cd_size ? 1 : 0) + (i_cm_size ? 1 : 0) );
991 bo_add_u8 ( &bo, 1 );
992 bo_add_u8 ( &bo, 2 );
996 /* file properties */
997 bo_add_guid ( &bo, &asf_object_file_properties_guid );
998 bo_addle_u64( &bo, 104 );
999 bo_add_guid ( &bo, &p_sys->fid );
1000 bo_addle_u64( &bo, i_size + 50 + p_sys->i_packet_count *
1001 p_sys->i_packet_size ); /* file size */
1002 bo_addle_u64( &bo, 0 ); /* creation date */
1003 bo_addle_u64( &bo, b_broadcast ? 0xffffffffLL : p_sys->i_packet_count );
1004 bo_addle_u64( &bo, i_duration * 10 ); /* play duration (100ns) */
1005 bo_addle_u64( &bo, i_duration * 10 ); /* send duration (100ns) */
1006 bo_addle_u64( &bo, p_sys->i_preroll_time ); /* preroll duration (ms) */
1007 bo_addle_u32( &bo, b_broadcast ? 0x01 : 0x02 /* seekable */ ); /* flags */
1008 bo_addle_u32( &bo, p_sys->i_packet_size ); /* packet size min */
1009 bo_addle_u32( &bo, p_sys->i_packet_size ); /* packet size max */
1010 bo_addle_u32( &bo, p_sys->i_bitrate ); /* maxbitrate */
1012 /* header extension */
1013 bo_add_guid ( &bo, &asf_object_header_extension_guid );
1014 bo_addle_u64( &bo, i_header_ext_size );
1015 bo_add_guid ( &bo, &asf_guid_reserved_1 );
1016 bo_addle_u16( &bo, 6 );
1017 bo_addle_u32( &bo, i_header_ext_size - 46 );
1019 /* extended stream properties */
1020 for( i = 0; i < p_sys->i_track; i++ )
1022 const asf_track_t *p_track = &p_sys->track[i];
1023 const es_format_t *p_fmt = &p_track->fmt;
1025 if( !p_track->b_extended )
1028 uint64_t i_avg_duration = 0;
1029 if( p_fmt->i_cat == VIDEO_ES &&
1030 p_fmt->video.i_frame_rate > 0 && p_fmt->video.i_frame_rate_base > 0 )
1031 i_avg_duration = ( INT64_C(10000000) * p_fmt->video.i_frame_rate_base +
1032 p_fmt->video.i_frame_rate/2 ) / p_fmt->video.i_frame_rate;
1034 bo_add_guid ( &bo, &asf_object_extended_stream_properties_guid );
1035 bo_addle_u64( &bo, 88 );
1036 bo_addle_u64( &bo, 0 );
1037 bo_addle_u64( &bo, 0 );
1038 bo_addle_u32( &bo, p_fmt->i_bitrate ); /* Bitrate */
1039 bo_addle_u32( &bo, 0 ); /* Buffer size */
1040 bo_addle_u32( &bo, 0 ); /* Initial buffer fullness */
1041 bo_addle_u32( &bo, p_fmt->i_bitrate ); /* Alternate Bitrate */
1042 bo_addle_u32( &bo, 0 ); /* Alternate Buffer size */
1043 bo_addle_u32( &bo, 0 ); /* Alternate Initial buffer fullness */
1044 bo_addle_u32( &bo, 0 ); /* Maximum object size (0 = unkown) */
1045 bo_addle_u32( &bo, 0x02 ); /* Flags (seekable) */
1046 bo_addle_u16( &bo, i+1 ); /* Stream number */
1047 bo_addle_u16( &bo, 0 ); /* Stream language index */
1048 bo_addle_u64( &bo, i_avg_duration ); /* Average time per frame */
1049 bo_addle_u16( &bo, 0 ); /* Stream name count */
1050 bo_addle_u16( &bo, 0 ); /* Payload extension system count */
1053 /* metadata object (part of header extension) */
1056 int64_t i_num, i_den;
1057 unsigned int i_dst_num, i_dst_den;
1059 for( i = 0; i < p_sys->i_track; i++ )
1060 if( p_sys->track[i].i_cat == VIDEO_ES ) break;
1062 i_num = p_sys->track[i].fmt.video.i_aspect *
1063 (int64_t)p_sys->track[i].fmt.video.i_height;
1064 i_den = VOUT_ASPECT_FACTOR * p_sys->track[i].fmt.video.i_width;
1065 vlc_ureduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
1067 msg_Dbg( p_mux, "pixel aspect-ratio: %i/%i", i_dst_num, i_dst_den );
1069 bo_add_guid ( &bo, &asf_object_metadata_guid );
1070 bo_addle_u64( &bo, i_cm_size );
1071 bo_addle_u16( &bo, 2 ); /* description records count */
1072 /* 1st description record */
1073 bo_addle_u16( &bo, 0 ); /* reserved */
1074 bo_addle_u16( &bo, i + 1 ); /* stream number (0 for the whole file) */
1075 bo_addle_u16( &bo, 2 * sizeof("AspectRatioX") ); /* name length */
1076 bo_addle_u16( &bo, 0x3 /* DWORD */ ); /* data type */
1077 bo_addle_u32( &bo, 4 ); /* data length */
1078 bo_addle_str16_nosize( &bo, "AspectRatioX" );
1079 bo_addle_u32( &bo, i_dst_num ); /* data */
1080 /* 2nd description record */
1081 bo_addle_u16( &bo, 0 ); /* reserved */
1082 bo_addle_u16( &bo, i + 1 ); /* stream number (0 for the whole file) */
1083 bo_addle_u16( &bo, 2 * sizeof("AspectRatioY") ); /* name length */
1084 bo_addle_u16( &bo, 0x3 /* DWORD */ ); /* data type */
1085 bo_addle_u32( &bo, 4 ); /* data length */
1086 bo_addle_str16_nosize( &bo, "AspectRatioY" );
1087 bo_addle_u32( &bo, i_dst_den ); /* data */
1090 /* content description header */
1093 bo_add_guid ( &bo, &asf_object_content_description_guid );
1094 bo_addle_u64( &bo, i_cd_size );
1095 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_title ) + 2 );
1096 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_author ) + 2 );
1097 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_copyright ) + 2 );
1098 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_comment ) + 2 );
1099 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_rating ) + 2 );
1101 bo_addle_str16_nosize( &bo, p_sys->psz_title );
1102 bo_addle_str16_nosize( &bo, p_sys->psz_author );
1103 bo_addle_str16_nosize( &bo, p_sys->psz_copyright );
1104 bo_addle_str16_nosize( &bo, p_sys->psz_comment );
1105 bo_addle_str16_nosize( &bo, p_sys->psz_rating );
1108 /* stream properties */
1109 for( i = 0; i < p_sys->i_track; i++ )
1111 tk = &p_sys->track[i];
1113 bo_add_guid ( &bo, &asf_object_stream_properties_guid );
1114 bo_addle_u64( &bo, 78 + tk->i_extra + (tk->b_audio_correction ? 8:0) );
1116 if( tk->i_cat == AUDIO_ES )
1118 bo_add_guid( &bo, &asf_object_stream_type_audio );
1119 if( tk->b_audio_correction )
1120 bo_add_guid( &bo, &asf_guid_audio_conceal_spread );
1122 bo_add_guid( &bo, &asf_guid_audio_conceal_none );
1124 else if( tk->i_cat == VIDEO_ES )
1126 bo_add_guid( &bo, &asf_object_stream_type_video );
1127 bo_add_guid( &bo, &asf_guid_video_conceal_none );
1129 bo_addle_u64( &bo, 0 ); /* time offset */
1130 bo_addle_u32( &bo, tk->i_extra );
1131 /* correction data length */
1132 bo_addle_u32( &bo, tk->b_audio_correction ? 8 : 0 );
1133 bo_addle_u16( &bo, tk->i_id ); /* stream number */
1134 bo_addle_u32( &bo, 0 );
1135 bo_add_mem ( &bo, tk->p_extra, tk->i_extra );
1137 /* Error correction data field */
1138 if( tk->b_audio_correction )
1140 bo_add_u8( &bo, 0x1 ); /* span */
1141 bo_addle_u16( &bo, tk->i_blockalign ); /* virtual packet length */
1142 bo_addle_u16( &bo, tk->i_blockalign ); /* virtual chunck length */
1143 bo_addle_u16( &bo, 1 ); /* silence length */
1144 bo_add_u8( &bo, 0x0 ); /* data */
1149 bo_add_guid ( &bo, &asf_object_codec_list_guid );
1150 bo_addle_u64( &bo, i_ci_size );
1151 bo_add_guid ( &bo, &asf_object_codec_list_reserved_guid );
1152 bo_addle_u32( &bo, p_sys->i_track );
1153 for( i = 0; i < p_sys->i_track; i++ )
1155 tk = &p_sys->track[i];
1157 if( tk->i_cat == VIDEO_ES ) bo_addle_u16( &bo, 1 /* video */ );
1158 else if( tk->i_cat == AUDIO_ES ) bo_addle_u16( &bo, 2 /* audio */ );
1159 else bo_addle_u16( &bo, 0xFFFF /* unknown */ );
1161 bo_addle_str16( &bo, tk->psz_name );
1162 bo_addle_u16( &bo, 0 );
1163 if( tk->i_cat == AUDIO_ES )
1165 bo_addle_u16( &bo, 2 );
1166 bo_addle_u16( &bo, tk->i_tag );
1168 else if( tk->i_cat == VIDEO_ES )
1170 bo_addle_u16( &bo, 4 );
1171 bo_add_mem ( &bo, (uint8_t*)&tk->i_fourcc, 4 );
1176 bo_add_guid ( &bo, &asf_object_data_guid );
1177 bo_addle_u64( &bo, 50 + p_sys->i_packet_count * p_sys->i_packet_size );
1178 bo_add_guid ( &bo, &p_sys->fid );
1179 bo_addle_u64( &bo, p_sys->i_packet_count );
1180 bo_addle_u16( &bo, 0x101 );
1185 /****************************************************************************
1187 ****************************************************************************/
1188 static block_t *asf_packet_flush( sout_mux_t *p_mux )
1190 sout_mux_sys_t *p_sys = p_mux->p_sys;
1191 int i_pad, i_preheader = p_sys->b_asf_http ? 12 : 0;
1195 if( !p_sys->pk ) return 0;
1197 i_pad = p_sys->i_packet_size - p_sys->i_pk_used;
1198 memset( p_sys->pk->p_buffer + p_sys->i_pk_used, 0, i_pad );
1200 bo_init( &bo, p_sys->pk->p_buffer, 14 + i_preheader );
1202 if( p_sys->b_asf_http )
1203 asf_chunk_add( &bo, 0x4424, p_sys->i_packet_size, 0x0, p_sys->i_seq++);
1205 bo_add_u8 ( &bo, 0x82 );
1206 bo_addle_u16( &bo, 0 );
1207 bo_add_u8( &bo, 0x11 );
1208 bo_add_u8( &bo, 0x5d );
1209 bo_addle_u16( &bo, i_pad );
1210 bo_addle_u32( &bo, (p_sys->i_pk_dts - p_sys->i_dts_first) / 1000 +
1211 p_sys->i_preroll_time );
1212 bo_addle_u16( &bo, 0 /* data->i_length */ );
1213 bo_add_u8( &bo, 0x80 | p_sys->i_pk_frame );
1218 p_sys->i_packet_count++;
1223 static block_t *asf_packet_create( sout_mux_t *p_mux,
1224 asf_track_t *tk, block_t *data )
1226 sout_mux_sys_t *p_sys = p_mux->p_sys;
1228 int i_data = data->i_buffer;
1230 uint8_t *p_data= data->p_buffer;
1231 block_t *first = NULL, **last = &first;
1232 int i_preheader = p_sys->b_asf_http ? 12 : 0;
1234 while( i_pos < i_data )
1239 if( p_sys->pk == NULL )
1241 p_sys->pk = block_New( p_mux, p_sys->i_packet_size + i_preheader );
1242 /* reserve 14 bytes for the packet header */
1243 p_sys->i_pk_used = 14 + i_preheader;
1244 p_sys->i_pk_frame = 0;
1245 p_sys->i_pk_dts = data->i_dts;
1248 bo_init( &bo, &p_sys->pk->p_buffer[p_sys->i_pk_used],
1249 p_sys->i_packet_size - p_sys->i_pk_used );
1251 /* add payload (header size = 17) */
1252 i_payload = __MIN( i_data - i_pos,
1253 p_sys->i_packet_size - p_sys->i_pk_used - 17 );
1255 if( tk->b_audio_correction && p_sys->i_pk_frame && i_payload < i_data )
1257 /* Don't know why yet but WMP doesn't like splitted WMA packets */
1258 *last = asf_packet_flush( p_mux );
1259 last = &(*last)->p_next;
1263 bo_add_u8 ( &bo, !(data->i_flags & BLOCK_FLAG_TYPE_P ||
1264 data->i_flags & BLOCK_FLAG_TYPE_B) ?
1265 0x80 | tk->i_id : tk->i_id );
1266 bo_add_u8 ( &bo, tk->i_sequence );
1267 bo_addle_u32( &bo, i_pos );
1268 bo_add_u8 ( &bo, 0x08 ); /* flags */
1269 bo_addle_u32( &bo, i_data );
1270 bo_addle_u32( &bo, (data->i_dts - p_sys->i_dts_first) / 1000 +
1271 p_sys->i_preroll_time );
1272 bo_addle_u16( &bo, i_payload );
1273 bo_add_mem ( &bo, &p_data[i_pos], i_payload );
1275 p_sys->i_pk_used += 17 + i_payload;
1277 p_sys->i_pk_frame++;
1279 if( p_sys->i_pk_used + 17 >= p_sys->i_packet_size )
1281 /* Not enough data for another payload, flush the packet */
1282 *last = asf_packet_flush( p_mux );
1283 last = &(*last)->p_next;
1288 block_Release( data );
1293 static block_t *asf_stream_end_create( sout_mux_t *p_mux )
1295 sout_mux_sys_t *p_sys = p_mux->p_sys;
1297 block_t *out = NULL;
1300 if( p_sys->b_asf_http )
1302 out = block_New( p_mux, 12 );
1303 bo_init( &bo, out->p_buffer, 12 );
1304 asf_chunk_add( &bo, 0x4524, 0, 0x00, p_sys->i_seq++ );
1309 out = block_New( p_mux, 56 );
1310 bo_init( &bo, out->p_buffer, 56 );
1311 bo_add_guid ( &bo, &asf_object_index_guid );
1312 bo_addle_u64( &bo, 56 );
1313 bo_add_guid ( &bo, &p_sys->fid );
1314 bo_addle_u64( &bo, 10000000 );
1315 bo_addle_u32( &bo, 5 );
1316 bo_addle_u32( &bo, 0 );