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 );
75 add_shortcut( "asf" );
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;
129 struct sout_mux_sys_t
131 guid_t fid; /* file id */
133 int64_t i_packet_count;
136 mtime_t i_preroll_time;
138 int64_t i_bitrate_override;
141 asf_track_t track[MAX_ASF_TRACKS];
161 static int MuxGetStream( sout_mux_t *, int *pi_stream, mtime_t *pi_dts );
163 static block_t *asf_header_create( sout_mux_t *, bool );
164 static block_t *asf_packet_create( sout_mux_t *, asf_track_t *, block_t * );
165 static block_t *asf_stream_end_create( sout_mux_t *);
166 static block_t *asf_packet_flush( sout_mux_t * );
176 static void bo_init ( bo_t *, uint8_t *, int );
177 static void bo_add_u8 ( bo_t *, uint8_t );
178 static void bo_addle_u16( bo_t *, uint16_t );
179 static void bo_addle_u32( bo_t *, uint32_t );
180 static void bo_addle_u64( bo_t *, uint64_t );
181 static void bo_add_mem ( bo_t *, uint8_t *, int );
183 static void bo_addle_str16( bo_t *, const char * );
185 /*****************************************************************************
187 *****************************************************************************/
188 static int Open( vlc_object_t *p_this )
190 sout_mux_t *p_mux = (sout_mux_t*)p_this;
191 sout_mux_sys_t *p_sys;
195 msg_Dbg( p_mux, "asf muxer opened" );
196 config_ChainParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
198 p_mux->pf_control = Control;
199 p_mux->pf_addstream = AddStream;
200 p_mux->pf_delstream = DelStream;
203 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
204 p_sys->b_asf_http = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "asfh" );
205 if( p_sys->b_asf_http )
207 msg_Dbg( p_mux, "creating asf stream to be used with mmsh" );
210 p_sys->i_pk_used = 0;
211 p_sys->i_pk_frame = 0;
212 p_sys->i_dts_first = -1;
213 p_sys->i_dts_last = 0;
214 p_sys->i_preroll_time = 2000;
215 p_sys->i_bitrate = 0;
216 p_sys->i_bitrate_override = 0;
219 p_sys->b_write_header = true;
221 p_sys->i_packet_size = config_GetInt( p_mux, "sout-asf-packet-size" );
222 p_sys->i_bitrate_override = config_GetInt( p_mux, "sout-asf-bitrate-override" );
223 msg_Dbg( p_mux, "Packet size %d", p_sys->i_packet_size);
224 if (p_sys->i_bitrate_override)
225 msg_Dbg( p_mux, "Bitrate override %"PRId64, p_sys->i_bitrate_override);
226 p_sys->i_packet_count= 0;
228 /* Generate a random fid */
229 srand( mdate() & 0xffffffff );
230 p_sys->fid.Data1 = 0xbabac001;
231 p_sys->fid.Data2 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
232 p_sys->fid.Data3 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
233 for( i = 0; i < 8; i++ )
235 p_sys->fid.Data4[i] = ( (uint64_t)rand() << 8 ) / RAND_MAX;
239 var_Get( p_mux, SOUT_CFG_PREFIX "title", &val );
240 p_sys->psz_title = val.psz_string;
242 var_Get( p_mux, SOUT_CFG_PREFIX "author", &val );
243 p_sys->psz_author = val.psz_string;
245 var_Get( p_mux, SOUT_CFG_PREFIX "copyright", &val );
246 p_sys->psz_copyright = val.psz_string;
248 var_Get( p_mux, SOUT_CFG_PREFIX "comment", &val );
249 p_sys->psz_comment = val.psz_string;
251 var_Get( p_mux, SOUT_CFG_PREFIX "rating", &val );
252 p_sys->psz_rating = val.psz_string;
254 msg_Dbg( p_mux, "meta data: title='%s', author='%s', copyright='%s', "
255 "comment='%s', rating='%s'",
256 p_sys->psz_title, p_sys->psz_author, p_sys->psz_copyright,
257 p_sys->psz_comment, p_sys->psz_rating );
262 /*****************************************************************************
264 *****************************************************************************/
265 static void Close( vlc_object_t * p_this )
267 sout_mux_t *p_mux = (sout_mux_t*)p_this;
268 sout_mux_sys_t *p_sys = p_mux->p_sys;
272 msg_Dbg( p_mux, "Asf muxer closed" );
274 /* Flush last packet if any */
275 if( (out = asf_packet_flush( p_mux ) ) )
277 sout_AccessOutWrite( p_mux->p_access, out );
280 if( ( out = asf_stream_end_create( p_mux ) ) )
282 sout_AccessOutWrite( p_mux->p_access, out );
286 if( sout_AccessOutSeek( p_mux->p_access, 0 ) == VLC_SUCCESS )
288 out = asf_header_create( p_mux, false );
289 sout_AccessOutWrite( p_mux->p_access, out );
292 for( i = 0; i < p_sys->i_track; i++ )
294 free( p_sys->track[i].p_extra );
295 es_format_Clean( &p_sys->track[i].fmt );
300 /*****************************************************************************
302 *****************************************************************************/
303 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
305 sout_mux_sys_t *p_sys = p_mux->p_sys;
311 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
312 pb_bool = (bool*)va_arg( args, bool * );
313 if( p_sys->b_asf_http ) *pb_bool = true;
314 else *pb_bool = false;
317 case MUX_GET_ADD_STREAM_WAIT:
318 pb_bool = (bool*)va_arg( args, bool * );
323 ppsz = (char**)va_arg( args, char ** );
324 if( p_sys->b_asf_http )
325 *ppsz = strdup( "video/x-ms-asf-stream" );
327 *ppsz = strdup( "video/x-ms-asf" );
335 /*****************************************************************************
337 *****************************************************************************/
338 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
340 sout_mux_sys_t *p_sys = p_mux->p_sys;
344 msg_Dbg( p_mux, "adding input" );
345 if( p_sys->i_track >= MAX_ASF_TRACKS )
347 msg_Dbg( p_mux, "cannot add this track (too much tracks)" );
351 tk = p_input->p_sys = &p_sys->track[p_sys->i_track];
352 tk->i_id = p_sys->i_track + 1;
353 tk->i_cat = p_input->p_fmt->i_cat;
355 tk->b_audio_correction = 0;
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 )
367 case VLC_FOURCC( 'a', '5', '2', ' ' ):
368 tk->i_tag = WAVE_FORMAT_A52;
369 tk->psz_name = "A/52";
372 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
373 tk->i_tag = WAVE_FORMAT_AAC;
374 tk->psz_name = "MPEG-4 Audio";
377 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
379 tk->psz_name = "MPEG Audio Layer 3";
380 tk->i_tag = WAVE_FORMAT_MPEGLAYER3;
386 tk->psz_name = "MPEG Audio Layer 1/2";
387 tk->i_tag = WAVE_FORMAT_MPEG;
393 case VLC_FOURCC( 'w', 'm', 'a', '1' ):
394 tk->psz_name = "Windows Media Audio v1";
395 tk->i_tag = WAVE_FORMAT_WMA1;
396 tk->b_audio_correction = true;
398 case VLC_FOURCC( 'w', 'm', 'a', ' ' ):
399 case VLC_FOURCC( 'w', 'm', 'a', '2' ):
400 tk->psz_name= "Windows Media Audio (v2) 7, 8 and 9 Series";
401 tk->i_tag = WAVE_FORMAT_WMA2;
402 tk->b_audio_correction = true;
404 case VLC_FOURCC( 'w', 'm', 'a', 'p' ):
405 tk->psz_name = "Windows Media Audio 9 Professional";
406 tk->i_tag = WAVE_FORMAT_WMAP;
407 tk->b_audio_correction = true;
409 case VLC_FOURCC( 'w', 'm', 'a', 'l' ):
410 tk->psz_name = "Windows Media Audio 9 Lossless";
411 tk->i_tag = WAVE_FORMAT_WMAL;
412 tk->b_audio_correction = true;
415 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
416 tk->psz_name = "Raw audio 8bits";
417 tk->i_tag = WAVE_FORMAT_PCM;
418 i_blockalign= p_input->p_fmt->audio.i_channels;
421 case VLC_FOURCC( 's', '1', '6', 'l' ):
422 tk->psz_name = "Raw audio 16bits";
423 tk->i_tag = WAVE_FORMAT_PCM;
424 i_blockalign= 2 * p_input->p_fmt->audio.i_channels;
425 i_bitspersample = 16;
427 case VLC_FOURCC( 's', '2', '4', 'l' ):
428 tk->psz_name = "Raw audio 24bits";
429 tk->i_tag = WAVE_FORMAT_PCM;
430 i_blockalign= 3 * p_input->p_fmt->audio.i_channels;
431 i_bitspersample = 24;
433 case VLC_FOURCC( 's', '3', '2', 'l' ):
434 tk->psz_name = "Raw audio 32bits";
435 tk->i_tag = WAVE_FORMAT_PCM;
436 i_blockalign= 4 * p_input->p_fmt->audio.i_channels;
437 i_bitspersample = 32;
443 tk->i_extra = sizeof( WAVEFORMATEX ) +
444 p_input->p_fmt->i_extra + i_extra;
445 tk->p_extra = malloc( tk->i_extra );
446 bo_init( &bo, tk->p_extra, tk->i_extra );
447 bo_addle_u16( &bo, tk->i_tag );
448 bo_addle_u16( &bo, p_input->p_fmt->audio.i_channels );
449 bo_addle_u32( &bo, p_input->p_fmt->audio.i_rate );
450 bo_addle_u32( &bo, p_input->p_fmt->i_bitrate / 8 );
451 bo_addle_u16( &bo, i_blockalign );
452 tk->i_blockalign = i_blockalign;
453 bo_addle_u16( &bo, i_bitspersample );
454 if( p_input->p_fmt->i_extra > 0 )
456 bo_addle_u16( &bo, p_input->p_fmt->i_extra );
457 bo_add_mem ( &bo, p_input->p_fmt->p_extra,
458 p_input->p_fmt->i_extra );
462 bo_addle_u16( &bo, i_extra );
463 if( tk->i_tag == WAVE_FORMAT_MPEGLAYER3 )
465 msg_Dbg( p_mux, "adding mp3 header" );
466 bo_addle_u16( &bo, 1 ); /* wId */
467 bo_addle_u32( &bo, 2 ); /* fdwFlags */
468 bo_addle_u16( &bo, 1152 ); /* nBlockSize */
469 bo_addle_u16( &bo, 1 ); /* nFramesPerBlock */
470 bo_addle_u16( &bo, 1393 ); /* nCodecDelay */
472 else if( tk->i_tag == WAVE_FORMAT_MPEG )
474 msg_Dbg( p_mux, "adding mp2 header" );
475 bo_addle_u16( &bo, 2 ); /* fwHeadLayer */
476 bo_addle_u32( &bo, p_input->p_fmt->i_bitrate );
477 bo_addle_u16( &bo, p_input->p_fmt->audio.i_channels == 2 ?1:8 );
478 bo_addle_u16( &bo, 0 ); /* fwHeadModeExt */
479 bo_addle_u16( &bo, 1 ); /* wHeadEmphasis */
480 bo_addle_u16( &bo, 16 ); /* fwHeadFlags */
481 bo_addle_u32( &bo, 0 ); /* dwPTSLow */
482 bo_addle_u32( &bo, 0 ); /* dwPTSHigh */
486 if( p_input->p_fmt->i_bitrate > 24000 )
488 p_sys->i_bitrate += p_input->p_fmt->i_bitrate;
492 p_sys->i_bitrate += 512000;
494 if (p_sys->i_bitrate_override)
495 p_sys->i_bitrate = p_sys->i_bitrate_override;
500 tk->i_extra = 11 + sizeof( BITMAPINFOHEADER ) +
501 p_input->p_fmt->i_extra;
502 tk->p_extra = malloc( tk->i_extra );
503 bo_init( &bo, tk->p_extra, tk->i_extra );
504 bo_addle_u32( &bo, p_input->p_fmt->video.i_width );
505 bo_addle_u32( &bo, p_input->p_fmt->video.i_height );
506 bo_add_u8 ( &bo, 0x02 ); /* flags */
507 bo_addle_u16( &bo, sizeof( BITMAPINFOHEADER ) +
508 p_input->p_fmt->i_extra );
509 bo_addle_u32( &bo, sizeof( BITMAPINFOHEADER ) +
510 p_input->p_fmt->i_extra );
511 bo_addle_u32( &bo, p_input->p_fmt->video.i_width );
512 bo_addle_u32( &bo, p_input->p_fmt->video.i_height );
513 bo_addle_u16( &bo, 1 );
514 bo_addle_u16( &bo, 24 );
515 if( p_input->p_fmt->i_codec == VLC_FOURCC('m','p','4','v') )
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_FOURCC('D','I','V','3') )
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_FOURCC('D','I','V','2') )
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_FOURCC('D','I','V','1') )
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_FOURCC('W','M','V','1') )
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_FOURCC('W','M','V','2') )
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_FOURCC('W','M','V','3') )
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_FOURCC('h','2','6','4') )
552 tk->psz_name = "H.264/MPEG-4 AVC";
553 tk->i_fourcc = VLC_FOURCC('h','2','6','4');
557 tk->psz_name = _("Unknown Video");
558 tk->i_fourcc = p_input->p_fmt->i_codec;
560 bo_add_mem( &bo, (uint8_t*)&tk->i_fourcc, 4 );
561 bo_addle_u32( &bo, 0 );
562 bo_addle_u32( &bo, 0 );
563 bo_addle_u32( &bo, 0 );
564 bo_addle_u32( &bo, 0 );
565 bo_addle_u32( &bo, 0 );
566 if( p_input->p_fmt->i_extra > 0 )
568 bo_add_mem ( &bo, p_input->p_fmt->p_extra,
569 p_input->p_fmt->i_extra );
572 if( p_input->p_fmt->i_bitrate > 50000 )
574 p_sys->i_bitrate += p_input->p_fmt->i_bitrate;
578 p_sys->i_bitrate += 1000000;
580 if (p_sys->i_bitrate_override)
581 p_sys->i_bitrate = p_sys->i_bitrate_override;
585 msg_Err(p_mux, "unhandled track type" );
589 es_format_Copy( &tk->fmt, p_input->p_fmt );
595 /*****************************************************************************
597 *****************************************************************************/
598 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
600 /* if bitrate ain't defined in commanline, reduce it when tracks are deleted
602 sout_mux_sys_t *p_sys = p_mux->p_sys;
603 asf_track_t *tk = p_input->p_sys;
604 if(!p_sys->i_bitrate_override)
606 if( tk->i_cat == AUDIO_ES )
608 if( p_input->p_fmt->i_bitrate > 24000 )
609 p_sys->i_bitrate -= p_input->p_fmt->i_bitrate;
611 p_sys->i_bitrate -= 512000;
613 else if(tk->i_cat == VIDEO_ES )
615 if( p_input->p_fmt->i_bitrate > 50000 )
616 p_sys->i_bitrate -= p_input->p_fmt->i_bitrate;
618 p_sys->i_bitrate -= 1000000;
621 msg_Dbg( p_mux, "removing input" );
625 /*****************************************************************************
627 *****************************************************************************/
628 static int Mux( sout_mux_t *p_mux )
630 sout_mux_sys_t *p_sys = p_mux->p_sys;
632 if( p_sys->b_write_header )
634 block_t *out = asf_header_create( p_mux, true );
636 out->i_flags |= BLOCK_FLAG_HEADER;
637 sout_AccessOutWrite( p_mux->p_access, out );
639 p_sys->b_write_header = false;
644 sout_input_t *p_input;
651 if( MuxGetStream( p_mux, &i_stream, &i_dts ) )
653 /* not enough data */
657 if( p_sys->i_dts_first < 0 )
659 p_sys->i_dts_first = i_dts;
661 if( p_sys->i_dts_last < i_dts )
663 p_sys->i_dts_last = i_dts;
666 p_input = p_mux->pp_inputs[i_stream];
667 tk = (asf_track_t*)p_input->p_sys;
669 data = block_FifoGet( p_input->p_fifo );
671 if( ( pk = asf_packet_create( p_mux, tk, data ) ) )
673 sout_AccessOutWrite( p_mux->p_access, pk );
680 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
686 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
688 sout_input_t *p_input = p_mux->pp_inputs[i];
691 if( block_FifoCount( p_input->p_fifo ) <= 0 )
693 if( p_input->p_fmt->i_cat == AUDIO_ES ||
694 p_input->p_fmt->i_cat == VIDEO_ES )
696 /* We need that audio+video fifo contain at least 1 packet */
703 p_data = block_FifoShow( p_input->p_fifo );
704 if( i_stream == -1 || p_data->i_dts < i_dts )
707 i_dts = p_data->i_dts;
711 *pi_stream = i_stream;
717 /****************************************************************************
718 * Asf header construction
719 ****************************************************************************/
721 /****************************************************************************
723 ****************************************************************************/
724 static void bo_init( bo_t *p_bo, uint8_t *p_buffer, int i_size )
726 p_bo->i_buffer_size = i_size;
728 p_bo->p_buffer = p_buffer;
730 static void bo_add_u8( bo_t *p_bo, uint8_t i )
732 if( p_bo->i_buffer < p_bo->i_buffer_size )
734 p_bo->p_buffer[p_bo->i_buffer] = i;
738 static void bo_addle_u16( bo_t *p_bo, uint16_t i )
740 bo_add_u8( p_bo, i &0xff );
741 bo_add_u8( p_bo, ( ( i >> 8) &0xff ) );
743 static void bo_addle_u32( bo_t *p_bo, uint32_t i )
745 bo_addle_u16( p_bo, i &0xffff );
746 bo_addle_u16( p_bo, ( ( i >> 16) &0xffff ) );
748 static void bo_addle_u64( bo_t *p_bo, uint64_t i )
750 bo_addle_u32( p_bo, i &0xffffffff );
751 bo_addle_u32( p_bo, ( ( i >> 32) &0xffffffff ) );
754 static void bo_add_mem( bo_t *p_bo, uint8_t *p_mem, int i_size )
756 int i_copy = __MIN( i_size, p_bo->i_buffer_size - p_bo->i_buffer );
760 memcpy( &p_bo->p_buffer[p_bo->i_buffer], p_mem, i_copy );
762 p_bo->i_buffer += i_size;
765 static void bo_addle_str16( bo_t *bo, const char *str )
767 bo_addle_u16( bo, strlen( str ) + 1 );
770 uint16_t c = (uint8_t)*str++;
771 bo_addle_u16( bo, c );
772 if( c == '\0' ) break;
776 static void bo_addle_str16_nosize( bo_t *bo, const char *str )
780 uint16_t c = (uint8_t)*str++;
781 bo_addle_u16( bo, c );
782 if( c == '\0' ) break;
786 /****************************************************************************
788 ****************************************************************************/
789 static void bo_add_guid( bo_t *p_bo, const guid_t *id )
792 bo_addle_u32( p_bo, id->Data1 );
793 bo_addle_u16( p_bo, id->Data2 );
794 bo_addle_u16( p_bo, id->Data3 );
795 for( i = 0; i < 8; i++ )
797 bo_add_u8( p_bo, id->Data4[i] );
801 static const guid_t asf_object_header_guid =
802 {0x75B22630, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
803 static const guid_t asf_object_data_guid =
804 {0x75B22636, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
805 static const guid_t asf_object_file_properties_guid =
806 {0x8cabdca1, 0xa947, 0x11cf, {0x8e, 0xe4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
807 static const guid_t asf_object_stream_properties_guid =
808 {0xB7DC0791, 0xA9B7, 0x11CF, {0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
809 static const guid_t asf_object_header_extension_guid =
810 {0x5FBF03B5, 0xA92E, 0x11CF, {0x8E, 0xE3, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
811 static const guid_t asf_object_stream_type_audio =
812 {0xF8699E40, 0x5B4D, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
813 static const guid_t asf_object_stream_type_video =
814 {0xbc19efc0, 0x5B4D, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
815 static const guid_t asf_guid_audio_conceal_none =
816 {0x20FB5700, 0x5B55, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
817 static const guid_t asf_guid_audio_conceal_spread =
818 {0xBFC3CD50, 0x618F, 0x11CF, {0x8B, 0xB2, 0x00, 0xAA, 0x00, 0xB4, 0xE2, 0x20}};
819 static const guid_t asf_guid_video_conceal_none =
820 {0x20FB5700, 0x5B55, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
821 static const guid_t asf_guid_reserved_1 =
822 {0xABD3D211, 0xA9BA, 0x11cf, {0x8E, 0xE6, 0x00, 0xC0, 0x0C ,0x20, 0x53, 0x65}};
823 static const guid_t asf_object_codec_list_guid =
824 {0x86D15240, 0x311D, 0x11D0, {0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6}};
825 static const guid_t asf_object_codec_list_reserved_guid =
826 {0x86D15241, 0x311D, 0x11D0, {0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6}};
827 static const guid_t asf_object_content_description_guid =
828 {0x75B22633, 0x668E, 0x11CF, {0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c}};
829 static const guid_t asf_object_index_guid =
830 {0x33000890, 0xE5B1, 0x11CF, {0x89, 0xF4, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xCB}};
831 static const guid_t asf_object_metadata_guid =
832 {0xC5F8CBEA, 0x5BAF, 0x4877, {0x84, 0x67, 0xAA, 0x8C, 0x44, 0xFA, 0x4C, 0xCA}};
834 /****************************************************************************
836 ****************************************************************************/
837 static void asf_chunk_add( bo_t *bo,
838 int i_type, int i_len, int i_flags, int i_seq )
840 bo_addle_u16( bo, i_type );
841 bo_addle_u16( bo, i_len + 8 );
842 bo_addle_u32( bo, i_seq );
843 bo_addle_u16( bo, i_flags );
844 bo_addle_u16( bo, i_len + 8 );
847 static block_t *asf_header_create( sout_mux_t *p_mux, bool b_broadcast )
849 sout_mux_sys_t *p_sys = p_mux->p_sys;
851 mtime_t i_duration = 0;
852 int i_size, i_header_ext_size, i;
853 int i_ci_size, i_cm_size = 0, i_cd_size = 0;
857 msg_Dbg( p_mux, "Asf muxer creating header" );
859 if( p_sys->i_dts_first > 0 )
861 i_duration = p_sys->i_dts_last - p_sys->i_dts_first;
862 if( i_duration < 0 ) i_duration = 0;
865 /* calculate header size */
868 for( i = 0; i < p_sys->i_track; i++ )
870 i_size += 78 + p_sys->track[i].i_extra;
871 i_ci_size += 8 + 2 * strlen( p_sys->track[i].psz_name );
872 if( p_sys->track[i].i_cat == AUDIO_ES ) i_ci_size += 4;
873 else if( p_sys->track[i].i_cat == VIDEO_ES ) i_ci_size += 6;
875 /* Error correction data field */
876 if( p_sys->track[i].b_audio_correction ) i_size += 8;
879 /* size of the content description object */
880 if( *p_sys->psz_title || *p_sys->psz_author || *p_sys->psz_copyright ||
881 *p_sys->psz_comment || *p_sys->psz_rating )
883 i_cd_size = 34 + 2 * ( strlen( p_sys->psz_title ) + 1 +
884 strlen( p_sys->psz_author ) + 1 +
885 strlen( p_sys->psz_copyright ) + 1 +
886 strlen( p_sys->psz_comment ) + 1 +
887 strlen( p_sys->psz_rating ) + 1 );
890 /* size of the metadata object */
891 for( i = 0; i < p_sys->i_track; i++ )
893 if( p_sys->track[i].i_cat == VIDEO_ES )
895 i_cm_size = 26 + 2 * (16 + 2 * sizeof("AspectRatio?"));
900 i_header_ext_size = i_cm_size ? i_cm_size + 46 : 0;
901 i_size += i_ci_size + i_cd_size + i_header_ext_size ;
903 if( p_sys->b_asf_http )
905 out = block_New( p_mux, i_size + 50 + 12 );
906 bo_init( &bo, out->p_buffer, i_size + 50 + 12 );
907 asf_chunk_add( &bo, 0x4824, i_size + 50, 0xc00, p_sys->i_seq++ );
911 out = block_New( p_mux, i_size + 50 );
912 bo_init( &bo, out->p_buffer, i_size + 50 );
916 bo_add_guid ( &bo, &asf_object_header_guid );
917 bo_addle_u64( &bo, i_size );
918 bo_addle_u32( &bo, 2 + p_sys->i_track +
919 (i_cd_size ? 1 : 0) + (i_cm_size ? 1 : 0) );
920 bo_add_u8 ( &bo, 1 );
921 bo_add_u8 ( &bo, 2 );
925 /* file properties */
926 bo_add_guid ( &bo, &asf_object_file_properties_guid );
927 bo_addle_u64( &bo, 104 );
928 bo_add_guid ( &bo, &p_sys->fid );
929 bo_addle_u64( &bo, i_size + 50 + p_sys->i_packet_count *
930 p_sys->i_packet_size ); /* file size */
931 bo_addle_u64( &bo, 0 ); /* creation date */
932 bo_addle_u64( &bo, b_broadcast ? 0xffffffffLL : p_sys->i_packet_count );
933 bo_addle_u64( &bo, i_duration * 10 ); /* play duration (100ns) */
934 bo_addle_u64( &bo, i_duration * 10 ); /* send duration (100ns) */
935 bo_addle_u64( &bo, p_sys->i_preroll_time ); /* preroll duration (ms) */
936 bo_addle_u32( &bo, b_broadcast ? 0x01 : 0x02 /* seekable */ ); /* flags */
937 bo_addle_u32( &bo, p_sys->i_packet_size ); /* packet size min */
938 bo_addle_u32( &bo, p_sys->i_packet_size ); /* packet size max */
939 bo_addle_u32( &bo, p_sys->i_bitrate ); /* maxbitrate */
941 /* header extension */
942 if( i_header_ext_size )
944 bo_add_guid ( &bo, &asf_object_header_extension_guid );
945 bo_addle_u64( &bo, i_header_ext_size );
946 bo_add_guid ( &bo, &asf_guid_reserved_1 );
947 bo_addle_u16( &bo, 6 );
948 bo_addle_u32( &bo, i_header_ext_size - 46 );
951 /* metadata object (part of header extension) */
954 int64_t i_num, i_den;
955 unsigned int i_dst_num, i_dst_den;
957 for( i = 0; i < p_sys->i_track; i++ )
958 if( p_sys->track[i].i_cat == VIDEO_ES ) break;
960 i_num = p_sys->track[i].fmt.video.i_aspect *
961 (int64_t)p_sys->track[i].fmt.video.i_height;
962 i_den = VOUT_ASPECT_FACTOR * p_sys->track[i].fmt.video.i_width;
963 vlc_ureduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
965 msg_Dbg( p_mux, "pixel aspect-ratio: %i/%i", i_dst_num, i_dst_den );
967 bo_add_guid ( &bo, &asf_object_metadata_guid );
968 bo_addle_u64( &bo, i_cm_size );
969 bo_addle_u16( &bo, 2 ); /* description records count */
970 /* 1st description record */
971 bo_addle_u16( &bo, 0 ); /* reserved */
972 bo_addle_u16( &bo, i + 1 ); /* stream number (0 for the whole file) */
973 bo_addle_u16( &bo, 2 * sizeof("AspectRatioX") ); /* name length */
974 bo_addle_u16( &bo, 0x3 /* DWORD */ ); /* data type */
975 bo_addle_u32( &bo, 4 ); /* data length */
976 bo_addle_str16_nosize( &bo, "AspectRatioX" );
977 bo_addle_u32( &bo, i_dst_num ); /* data */
978 /* 2nd description record */
979 bo_addle_u16( &bo, 0 ); /* reserved */
980 bo_addle_u16( &bo, i + 1 ); /* stream number (0 for the whole file) */
981 bo_addle_u16( &bo, 2 * sizeof("AspectRatioY") ); /* name length */
982 bo_addle_u16( &bo, 0x3 /* DWORD */ ); /* data type */
983 bo_addle_u32( &bo, 4 ); /* data length */
984 bo_addle_str16_nosize( &bo, "AspectRatioY" );
985 bo_addle_u32( &bo, i_dst_den ); /* data */
988 /* content description header */
991 bo_add_guid ( &bo, &asf_object_content_description_guid );
992 bo_addle_u64( &bo, i_cd_size );
993 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_title ) + 2 );
994 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_author ) + 2 );
995 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_copyright ) + 2 );
996 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_comment ) + 2 );
997 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_rating ) + 2 );
999 bo_addle_str16_nosize( &bo, p_sys->psz_title );
1000 bo_addle_str16_nosize( &bo, p_sys->psz_author );
1001 bo_addle_str16_nosize( &bo, p_sys->psz_copyright );
1002 bo_addle_str16_nosize( &bo, p_sys->psz_comment );
1003 bo_addle_str16_nosize( &bo, p_sys->psz_rating );
1006 /* stream properties */
1007 for( i = 0; i < p_sys->i_track; i++ )
1009 tk = &p_sys->track[i];
1011 bo_add_guid ( &bo, &asf_object_stream_properties_guid );
1012 bo_addle_u64( &bo, 78 + tk->i_extra + (tk->b_audio_correction ? 8:0) );
1014 if( tk->i_cat == AUDIO_ES )
1016 bo_add_guid( &bo, &asf_object_stream_type_audio );
1017 if( tk->b_audio_correction )
1018 bo_add_guid( &bo, &asf_guid_audio_conceal_spread );
1020 bo_add_guid( &bo, &asf_guid_audio_conceal_none );
1022 else if( tk->i_cat == VIDEO_ES )
1024 bo_add_guid( &bo, &asf_object_stream_type_video );
1025 bo_add_guid( &bo, &asf_guid_video_conceal_none );
1027 bo_addle_u64( &bo, 0 ); /* time offset */
1028 bo_addle_u32( &bo, tk->i_extra );
1029 /* correction data length */
1030 bo_addle_u32( &bo, tk->b_audio_correction ? 8 : 0 );
1031 bo_addle_u16( &bo, tk->i_id ); /* stream number */
1032 bo_addle_u32( &bo, 0 );
1033 bo_add_mem ( &bo, tk->p_extra, tk->i_extra );
1035 /* Error correction data field */
1036 if( tk->b_audio_correction )
1038 bo_add_u8( &bo, 0x1 ); /* span */
1039 bo_addle_u16( &bo, tk->i_blockalign ); /* virtual packet length */
1040 bo_addle_u16( &bo, tk->i_blockalign ); /* virtual chunck length */
1041 bo_addle_u16( &bo, 1 ); /* silence length */
1042 bo_add_u8( &bo, 0x0 ); /* data */
1047 bo_add_guid ( &bo, &asf_object_codec_list_guid );
1048 bo_addle_u64( &bo, i_ci_size );
1049 bo_add_guid ( &bo, &asf_object_codec_list_reserved_guid );
1050 bo_addle_u32( &bo, p_sys->i_track );
1051 for( i = 0; i < p_sys->i_track; i++ )
1053 tk = &p_sys->track[i];
1055 if( tk->i_cat == VIDEO_ES ) bo_addle_u16( &bo, 1 /* video */ );
1056 else if( tk->i_cat == AUDIO_ES ) bo_addle_u16( &bo, 2 /* audio */ );
1057 else bo_addle_u16( &bo, 0xFFFF /* unknown */ );
1059 bo_addle_str16( &bo, tk->psz_name );
1060 bo_addle_u16( &bo, 0 );
1061 if( tk->i_cat == AUDIO_ES )
1063 bo_addle_u16( &bo, 2 );
1064 bo_addle_u16( &bo, tk->i_tag );
1066 else if( tk->i_cat == VIDEO_ES )
1068 bo_addle_u16( &bo, 4 );
1069 bo_add_mem ( &bo, (uint8_t*)&tk->i_fourcc, 4 );
1074 bo_add_guid ( &bo, &asf_object_data_guid );
1075 bo_addle_u64( &bo, 50 + p_sys->i_packet_count * p_sys->i_packet_size );
1076 bo_add_guid ( &bo, &p_sys->fid );
1077 bo_addle_u64( &bo, p_sys->i_packet_count );
1078 bo_addle_u16( &bo, 0x101 );
1083 /****************************************************************************
1085 ****************************************************************************/
1086 static block_t *asf_packet_flush( sout_mux_t *p_mux )
1088 sout_mux_sys_t *p_sys = p_mux->p_sys;
1089 int i_pad, i_preheader = p_sys->b_asf_http ? 12 : 0;
1093 if( !p_sys->pk ) return 0;
1095 i_pad = p_sys->i_packet_size - p_sys->i_pk_used;
1096 memset( p_sys->pk->p_buffer + p_sys->i_pk_used, 0, i_pad );
1098 bo_init( &bo, p_sys->pk->p_buffer, 14 + i_preheader );
1100 if( p_sys->b_asf_http )
1101 asf_chunk_add( &bo, 0x4424, p_sys->i_packet_size, 0x0, p_sys->i_seq++);
1103 bo_add_u8 ( &bo, 0x82 );
1104 bo_addle_u16( &bo, 0 );
1105 bo_add_u8( &bo, 0x11 );
1106 bo_add_u8( &bo, 0x5d );
1107 bo_addle_u16( &bo, i_pad );
1108 bo_addle_u32( &bo, (p_sys->i_pk_dts - p_sys->i_dts_first) / 1000 +
1109 p_sys->i_preroll_time );
1110 bo_addle_u16( &bo, 0 /* data->i_length */ );
1111 bo_add_u8( &bo, 0x80 | p_sys->i_pk_frame );
1116 p_sys->i_packet_count++;
1121 static block_t *asf_packet_create( sout_mux_t *p_mux,
1122 asf_track_t *tk, block_t *data )
1124 sout_mux_sys_t *p_sys = p_mux->p_sys;
1126 int i_data = data->i_buffer;
1128 uint8_t *p_data= data->p_buffer;
1129 block_t *first = NULL, **last = &first;
1130 int i_preheader = p_sys->b_asf_http ? 12 : 0;
1132 while( i_pos < i_data )
1137 if( p_sys->pk == NULL )
1139 p_sys->pk = block_New( p_mux, p_sys->i_packet_size + i_preheader );
1140 /* reserve 14 bytes for the packet header */
1141 p_sys->i_pk_used = 14 + i_preheader;
1142 p_sys->i_pk_frame = 0;
1143 p_sys->i_pk_dts = data->i_dts;
1146 bo_init( &bo, &p_sys->pk->p_buffer[p_sys->i_pk_used],
1147 p_sys->i_packet_size - p_sys->i_pk_used );
1149 /* add payload (header size = 17) */
1150 i_payload = __MIN( i_data - i_pos,
1151 p_sys->i_packet_size - p_sys->i_pk_used - 17 );
1153 if( tk->b_audio_correction && p_sys->i_pk_frame && i_payload < i_data )
1155 /* Don't know why yet but WMP doesn't like splitted WMA packets */
1156 *last = asf_packet_flush( p_mux );
1157 last = &(*last)->p_next;
1161 bo_add_u8 ( &bo, !(data->i_flags & BLOCK_FLAG_TYPE_P ||
1162 data->i_flags & BLOCK_FLAG_TYPE_B) ?
1163 0x80 | tk->i_id : tk->i_id );
1164 bo_add_u8 ( &bo, tk->i_sequence );
1165 bo_addle_u32( &bo, i_pos );
1166 bo_add_u8 ( &bo, 0x08 ); /* flags */
1167 bo_addle_u32( &bo, i_data );
1168 bo_addle_u32( &bo, (data->i_dts - p_sys->i_dts_first) / 1000 +
1169 p_sys->i_preroll_time );
1170 bo_addle_u16( &bo, i_payload );
1171 bo_add_mem ( &bo, &p_data[i_pos], i_payload );
1173 p_sys->i_pk_used += 17 + i_payload;
1175 p_sys->i_pk_frame++;
1177 if( p_sys->i_pk_used + 17 >= p_sys->i_packet_size )
1179 /* Not enough data for another payload, flush the packet */
1180 *last = asf_packet_flush( p_mux );
1181 last = &(*last)->p_next;
1186 block_Release( data );
1191 static block_t *asf_stream_end_create( sout_mux_t *p_mux )
1193 sout_mux_sys_t *p_sys = p_mux->p_sys;
1195 block_t *out = NULL;
1198 if( p_sys->b_asf_http )
1200 out = block_New( p_mux, 12 );
1201 bo_init( &bo, out->p_buffer, 12 );
1202 asf_chunk_add( &bo, 0x4524, 0, 0x00, p_sys->i_seq++ );
1207 out = block_New( p_mux, 56 );
1208 bo_init( &bo, out->p_buffer, 56 );
1209 bo_add_guid ( &bo, &asf_object_index_guid );
1210 bo_addle_u64( &bo, 56 );
1211 bo_add_guid ( &bo, &p_sys->fid );
1212 bo_addle_u64( &bo, 10000000 );
1213 bo_addle_u32( &bo, 5 );
1214 bo_addle_u32( &bo, 0 );