1 /*****************************************************************************
2 * asf.c: asf muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc/input.h>
36 #define MAX_ASF_TRACKS 128
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Open ( vlc_object_t * );
42 static void Close ( vlc_object_t * );
44 #define SOUT_CFG_PREFIX "sout-asf-"
46 #define TITLE_TEXT N_("Title")
47 #define TITLE_LONGTEXT N_("Allows you to define the title that will be put " \
49 #define AUTHOR_TEXT N_("Author")
50 #define AUTHOR_LONGTEXT N_("Allows you to define the author that will be put "\
52 #define COPYRIGHT_TEXT N_("Copyright")
53 #define COPYRIGHT_LONGTEXT N_("Allows you to define the copyright string " \
54 "that will be put in ASF comments.")
55 #define COMMENT_TEXT N_("Comment")
56 #define COMMENT_LONGTEXT N_("Allows you to define the comment that will be " \
57 "put in ASF comments.")
58 #define RATING_TEXT N_("Rating")
59 #define RATING_LONGTEXT N_("Allows you to define the \"rating\" that will " \
60 "be put in ASF comments.")
63 set_description( _("ASF muxer") );
64 set_capability( "sout mux", 5 );
65 add_shortcut( "asf" );
66 add_shortcut( "asfh" );
67 set_callbacks( Open, Close );
69 add_string( SOUT_CFG_PREFIX "title", "", NULL, TITLE_TEXT, TITLE_LONGTEXT,
71 add_string( SOUT_CFG_PREFIX "author", "", NULL, AUTHOR_TEXT,
72 AUTHOR_LONGTEXT, VLC_TRUE );
73 add_string( SOUT_CFG_PREFIX "copyright","", NULL, COPYRIGHT_TEXT,
74 COPYRIGHT_LONGTEXT, VLC_TRUE );
75 add_string( SOUT_CFG_PREFIX "comment", "", NULL, COMMENT_TEXT,
76 COMMENT_LONGTEXT, VLC_TRUE );
77 add_string( SOUT_CFG_PREFIX "rating", "", NULL, RATING_TEXT,
78 RATING_LONGTEXT, VLC_TRUE );
81 /*****************************************************************************
83 *****************************************************************************/
84 static const char *ppsz_sout_options[] = {
85 "title", "author", "copyright", "comment", "rating", NULL
88 static int Control ( sout_mux_t *, int, va_list );
89 static int AddStream( sout_mux_t *, sout_input_t * );
90 static int DelStream( sout_mux_t *, sout_input_t * );
91 static int Mux ( sout_mux_t * );
98 /* codec information */
99 uint16_t i_tag; /* for audio */
100 vlc_fourcc_t i_fourcc; /* for video */
101 char *psz_name; /* codec name */
110 struct sout_mux_sys_t
112 guid_t fid; /* file id */
114 int64_t i_packet_count;
117 mtime_t i_preroll_time;
121 asf_track_t track[MAX_ASF_TRACKS];
123 vlc_bool_t b_write_header;
130 vlc_bool_t b_asf_http;
141 static int MuxGetStream( sout_mux_t *, int *pi_stream, mtime_t *pi_dts );
143 static block_t *asf_header_create( sout_mux_t *, vlc_bool_t );
144 static block_t *asf_packet_create( sout_mux_t *, asf_track_t *, block_t * );
145 static block_t *asf_stream_end_create( sout_mux_t *);
146 static block_t *asf_packet_flush( sout_mux_t * );
156 static void bo_init ( bo_t *, uint8_t *, int );
157 static void bo_add_u8 ( bo_t *, uint8_t );
158 static void bo_addle_u16( bo_t *, uint16_t );
159 static void bo_addle_u32( bo_t *, uint32_t );
160 static void bo_addle_u64( bo_t *, uint64_t );
161 static void bo_add_mem ( bo_t *, uint8_t *, int );
163 static void bo_addle_str16( bo_t *, char * );
165 /*****************************************************************************
167 *****************************************************************************/
168 static int Open( vlc_object_t *p_this )
170 sout_mux_t *p_mux = (sout_mux_t*)p_this;
171 sout_mux_sys_t *p_sys;
175 msg_Dbg( p_mux, "Asf muxer opened" );
176 sout_CfgParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
178 p_mux->pf_control = Control;
179 p_mux->pf_addstream = AddStream;
180 p_mux->pf_delstream = DelStream;
183 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
184 p_sys->b_asf_http = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "asfh" );
185 if( p_sys->b_asf_http )
187 msg_Dbg( p_mux, "creating asf stream to be used with mmsh" );
190 p_sys->i_pk_used = 0;
191 p_sys->i_pk_frame = 0;
192 p_sys->i_dts_first = -1;
193 p_sys->i_dts_last = 0;
194 p_sys->i_preroll_time = 2000;
195 p_sys->i_bitrate = 0;
198 p_sys->b_write_header = VLC_TRUE;
200 p_sys->i_packet_size = 4096;
201 p_sys->i_packet_count= 0;
203 /* Generate a random fid */
204 srand( mdate() & 0xffffffff );
205 p_sys->fid.Data1 = 0xbabac001;
206 p_sys->fid.Data2 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
207 p_sys->fid.Data3 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
208 for( i = 0; i < 8; i++ )
210 p_sys->fid.Data4[i] = ( (uint64_t)rand() << 8 ) / RAND_MAX;
214 var_Get( p_mux, SOUT_CFG_PREFIX "title", &val );
215 p_sys->psz_title = val.psz_string;
217 var_Get( p_mux, SOUT_CFG_PREFIX "author", &val );
218 p_sys->psz_author = val.psz_string;
220 var_Get( p_mux, SOUT_CFG_PREFIX "copyright", &val );
221 p_sys->psz_copyright = val.psz_string;
223 var_Get( p_mux, SOUT_CFG_PREFIX "comment", &val );
224 p_sys->psz_comment = val.psz_string;
226 var_Get( p_mux, SOUT_CFG_PREFIX "rating", &val );
227 p_sys->psz_rating = val.psz_string;
229 msg_Dbg( p_mux, "meta data: title='%s' author='%s' copyright='%s' "
230 "comment='%s' rating='%s'",
231 p_sys->psz_title, p_sys->psz_author, p_sys->psz_copyright,
232 p_sys->psz_comment, p_sys->psz_rating );
237 /*****************************************************************************
239 *****************************************************************************/
240 static void Close( vlc_object_t * p_this )
242 sout_mux_t *p_mux = (sout_mux_t*)p_this;
243 sout_mux_sys_t *p_sys = p_mux->p_sys;
247 msg_Dbg( p_mux, "Asf muxer closed" );
249 /* Flush last packet if any */
250 if( (out = asf_packet_flush( p_mux ) ) )
252 sout_AccessOutWrite( p_mux->p_access, out );
255 if( ( out = asf_stream_end_create( p_mux ) ) )
257 sout_AccessOutWrite( p_mux->p_access, out );
261 if( !sout_AccessOutSeek( p_mux->p_access, 0 ) )
263 out = asf_header_create( p_mux, VLC_FALSE );
264 sout_AccessOutWrite( p_mux->p_access, out );
267 for( i = 0; i < p_sys->i_track; i++ )
269 free( p_sys->track[i].p_extra );
274 /*****************************************************************************
276 *****************************************************************************/
277 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
279 sout_mux_sys_t *p_sys = p_mux->p_sys;
285 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
286 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
287 if( p_sys->b_asf_http ) *pb_bool = VLC_TRUE;
288 else *pb_bool = VLC_FALSE;
291 case MUX_GET_ADD_STREAM_WAIT:
292 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
293 *pb_bool = VLC_FALSE;
297 ppsz = (char**)va_arg( args, char ** );
298 if( p_sys->b_asf_http )
299 *ppsz = strdup( "video/x-ms-asf-stream" );
301 *ppsz = strdup( "video/x-ms-asf" );
309 /*****************************************************************************
311 *****************************************************************************/
312 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
314 sout_mux_sys_t *p_sys = p_mux->p_sys;
318 msg_Dbg( p_mux, "adding input" );
319 if( p_sys->i_track >= MAX_ASF_TRACKS )
321 msg_Dbg( p_mux, "cannot add this track (too much track)" );
325 tk = p_input->p_sys = &p_sys->track[p_sys->i_track];
326 tk->i_id = p_sys->i_track;
327 tk->i_cat = p_input->p_fmt->i_cat;
334 int i_blockalign = p_input->p_fmt->audio.i_blockalign;
335 int i_bitspersample = 0;
338 switch( p_input->p_fmt->i_codec )
340 case VLC_FOURCC( 'a', '5', '2', ' ' ):
341 tk->i_tag = WAVE_FORMAT_A52;
342 tk->psz_name = "A/52";
344 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
346 tk->psz_name = "MPEG Audio Layer 3";
347 tk->i_tag = WAVE_FORMAT_MPEGLAYER3;
352 tk->psz_name = "MPEG Audio Layer 1/2";
353 tk->i_tag = WAVE_FORMAT_MPEG;
358 case VLC_FOURCC( 'w', 'm', 'a', '1' ):
359 tk->psz_name = "Windows Media Audio 1";
360 tk->i_tag = WAVE_FORMAT_WMA1;
362 case VLC_FOURCC( 'w', 'm', 'a', '2' ):
363 tk->psz_name = "Windows Media Audio 2";
364 tk->i_tag = WAVE_FORMAT_WMA2;
366 case VLC_FOURCC( 'w', 'm', 'a', '3' ):
367 tk->psz_name = "Windows Media Audio 3";
368 tk->i_tag = WAVE_FORMAT_WMA3;
371 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
372 tk->psz_name = "Raw audio 8bits";
373 tk->i_tag = WAVE_FORMAT_PCM;
374 i_blockalign= p_input->p_fmt->audio.i_channels;
377 case VLC_FOURCC( 's', '1', '6', 'l' ):
378 tk->psz_name = "Raw audio 16bits";
379 tk->i_tag = WAVE_FORMAT_PCM;
380 i_blockalign= 2 * p_input->p_fmt->audio.i_channels;
381 i_bitspersample = 16;
383 case VLC_FOURCC( 's', '2', '4', 'l' ):
384 tk->psz_name = "Raw audio 24bits";
385 tk->i_tag = WAVE_FORMAT_PCM;
386 i_blockalign= 3 * p_input->p_fmt->audio.i_channels;
387 i_bitspersample = 24;
389 case VLC_FOURCC( 's', '3', '2', 'l' ):
390 tk->psz_name = "Raw audio 32bits";
391 tk->i_tag = WAVE_FORMAT_PCM;
392 i_blockalign= 4 * p_input->p_fmt->audio.i_channels;
393 i_bitspersample = 32;
399 tk->i_extra = sizeof( WAVEFORMATEX ) +
400 p_input->p_fmt->i_extra + i_extra;
401 tk->p_extra = malloc( tk->i_extra );
402 bo_init( &bo, tk->p_extra, tk->i_extra );
403 bo_addle_u16( &bo, tk->i_tag );
404 bo_addle_u16( &bo, p_input->p_fmt->audio.i_channels );
405 bo_addle_u32( &bo, p_input->p_fmt->audio.i_rate );
406 bo_addle_u32( &bo, p_input->p_fmt->i_bitrate / 8 );
407 bo_addle_u16( &bo, i_blockalign );
408 bo_addle_u16( &bo, i_bitspersample );
409 if( p_input->p_fmt->i_extra > 0 )
411 bo_addle_u16( &bo, p_input->p_fmt->i_extra );
412 bo_add_mem ( &bo, p_input->p_fmt->p_extra,
413 p_input->p_fmt->i_extra );
417 bo_addle_u16( &bo, i_extra );
418 if( tk->i_tag == WAVE_FORMAT_MPEGLAYER3 )
420 msg_Dbg( p_mux, "adding mp3 header" );
421 bo_addle_u16( &bo, 1 ); /* wId */
422 bo_addle_u32( &bo, 2 ); /* fdwFlags */
423 bo_addle_u16( &bo, 1152 ); /* nBlockSize */
424 bo_addle_u16( &bo, 1 ); /* nFramesPerBlock */
425 bo_addle_u16( &bo, 1393 ); /* nCodecDelay */
427 else if( tk->i_tag == WAVE_FORMAT_MPEG )
429 msg_Dbg( p_mux, "adding mp2 header" );
430 bo_addle_u16( &bo, 2 ); /* fwHeadLayer */
431 bo_addle_u32( &bo, p_input->p_fmt->i_bitrate );
432 bo_addle_u16( &bo, p_input->p_fmt->audio.i_channels == 2 ?1:8 );
433 bo_addle_u16( &bo, 0 ); /* fwHeadModeExt */
434 bo_addle_u16( &bo, 1 ); /* wHeadEmphasis */
435 bo_addle_u16( &bo, 16 ); /* fwHeadFlags */
436 bo_addle_u32( &bo, 0 ); /* dwPTSLow */
437 bo_addle_u32( &bo, 0 ); /* dwPTSHigh */
441 if( p_input->p_fmt->i_bitrate > 24000 )
443 p_sys->i_bitrate += p_input->p_fmt->i_bitrate;
447 p_sys->i_bitrate += 512000;
453 tk->i_extra = 11 + sizeof( BITMAPINFOHEADER ) +
454 p_input->p_fmt->i_extra;
455 tk->p_extra = malloc( tk->i_extra );
456 bo_init( &bo, tk->p_extra, tk->i_extra );
457 bo_addle_u32( &bo, p_input->p_fmt->video.i_width );
458 bo_addle_u32( &bo, p_input->p_fmt->video.i_height );
459 bo_add_u8 ( &bo, 0x02 ); /* flags */
460 bo_addle_u16( &bo, sizeof( BITMAPINFOHEADER ) +
461 p_input->p_fmt->i_extra );
462 bo_addle_u32( &bo, sizeof( BITMAPINFOHEADER ) +
463 p_input->p_fmt->i_extra );
464 bo_addle_u32( &bo, p_input->p_fmt->video.i_width );
465 bo_addle_u32( &bo, p_input->p_fmt->video.i_height );
466 bo_addle_u16( &bo, 1 );
467 bo_addle_u16( &bo, 24 );
468 if( p_input->p_fmt->i_codec == VLC_FOURCC('m','p','4','v') )
470 tk->psz_name = "MPEG-4 Video";
471 tk->i_fourcc = VLC_FOURCC( 'M', 'P', '4', 'S' );
473 else if( p_input->p_fmt->i_codec == VLC_FOURCC('D','I','V','3') )
475 tk->psz_name = "MSMPEG-4 V3 Video";
476 tk->i_fourcc = VLC_FOURCC( 'M', 'P', '4', '3' );
478 else if( p_input->p_fmt->i_codec == VLC_FOURCC('D','I','V','2') )
480 tk->psz_name = "MSMPEG-4 V2 Video";
481 tk->i_fourcc = VLC_FOURCC( 'M', 'P', '4', '2' );
483 else if( p_input->p_fmt->i_codec == VLC_FOURCC('D','I','V','1') )
485 tk->psz_name = "MSMPEG-4 V1 Video";
486 tk->i_fourcc = VLC_FOURCC( 'M', 'P', 'G', '4' );
488 else if( p_input->p_fmt->i_codec == VLC_FOURCC('W','M','V','1') )
490 tk->psz_name = "Windows Media Video 1";
491 tk->i_fourcc = VLC_FOURCC( 'W', 'M', 'V', '1' );
493 else if( p_input->p_fmt->i_codec == VLC_FOURCC('W','M','V','2') )
495 tk->psz_name = "Windows Media Video 2";
496 tk->i_fourcc = VLC_FOURCC( 'W', 'M', 'V', '2' );
498 else if( p_input->p_fmt->i_codec == VLC_FOURCC('W','M','V','3') )
500 tk->psz_name = "Windows Media Video 3";
501 tk->i_fourcc = VLC_FOURCC( 'W', 'M', 'V', '3' );
505 tk->psz_name = _("Unknown Video");
506 tk->i_fourcc = p_input->p_fmt->i_codec;
508 bo_add_mem( &bo, (uint8_t*)&tk->i_fourcc, 4 );
509 bo_addle_u32( &bo, 0 );
510 bo_addle_u32( &bo, 0 );
511 bo_addle_u32( &bo, 0 );
512 bo_addle_u32( &bo, 0 );
513 bo_addle_u32( &bo, 0 );
514 if( p_input->p_fmt->i_extra > 0 )
516 bo_add_mem ( &bo, p_input->p_fmt->p_extra,
517 p_input->p_fmt->i_extra );
520 if( p_input->p_fmt->i_bitrate > 50000 )
522 p_sys->i_bitrate += p_input->p_fmt->i_bitrate;
526 p_sys->i_bitrate += 1000000;
531 msg_Err(p_mux, "unhandled track type" );
539 /*****************************************************************************
541 *****************************************************************************/
542 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
544 msg_Dbg( p_mux, "removing input" );
548 /*****************************************************************************
550 *****************************************************************************/
551 static int Mux( sout_mux_t *p_mux )
553 sout_mux_sys_t *p_sys = p_mux->p_sys;
555 if( p_sys->b_write_header )
557 block_t *out = asf_header_create( p_mux, VLC_TRUE );
559 out->i_flags |= BLOCK_FLAG_HEADER;
560 sout_AccessOutWrite( p_mux->p_access, out );
562 p_sys->b_write_header = VLC_FALSE;
567 sout_input_t *p_input;
574 if( MuxGetStream( p_mux, &i_stream, &i_dts ) )
576 /* not enough data */
580 if( p_sys->i_dts_first < 0 )
582 p_sys->i_dts_first = i_dts;
584 if( p_sys->i_dts_last < i_dts )
586 p_sys->i_dts_last = i_dts;
589 p_input = p_mux->pp_inputs[i_stream];
590 tk = (asf_track_t*)p_input->p_sys;
592 data = block_FifoGet( p_input->p_fifo );
594 if( ( pk = asf_packet_create( p_mux, tk, data ) ) )
596 sout_AccessOutWrite( p_mux->p_access, pk );
603 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
609 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
611 sout_input_t *p_input = p_mux->pp_inputs[i];
614 if( p_input->p_fifo->i_depth <= 0 )
616 if( p_input->p_fmt->i_cat == AUDIO_ES ||
617 p_input->p_fmt->i_cat == VIDEO_ES )
619 /* We need that audio+video fifo contain at least 1 packet */
626 p_data = block_FifoShow( p_input->p_fifo );
627 if( i_stream == -1 || p_data->i_dts < i_dts )
630 i_dts = p_data->i_dts;
634 *pi_stream = i_stream;
640 /****************************************************************************
641 * Asf header construction
642 ****************************************************************************/
644 /****************************************************************************
646 ****************************************************************************/
647 static void bo_init( bo_t *p_bo, uint8_t *p_buffer, int i_size )
649 p_bo->i_buffer_size = i_size;
651 p_bo->p_buffer = p_buffer;
653 static void bo_add_u8( bo_t *p_bo, uint8_t i )
655 if( p_bo->i_buffer < p_bo->i_buffer_size )
657 p_bo->p_buffer[p_bo->i_buffer] = i;
661 static void bo_addle_u16( bo_t *p_bo, uint16_t i )
663 bo_add_u8( p_bo, i &0xff );
664 bo_add_u8( p_bo, ( ( i >> 8) &0xff ) );
666 static void bo_addle_u32( bo_t *p_bo, uint32_t i )
668 bo_addle_u16( p_bo, i &0xffff );
669 bo_addle_u16( p_bo, ( ( i >> 16) &0xffff ) );
671 static void bo_addle_u64( bo_t *p_bo, uint64_t i )
673 bo_addle_u32( p_bo, i &0xffffffff );
674 bo_addle_u32( p_bo, ( ( i >> 32) &0xffffffff ) );
677 static void bo_add_mem( bo_t *p_bo, uint8_t *p_mem, int i_size )
679 int i_copy = __MIN( i_size, p_bo->i_buffer_size - p_bo->i_buffer );
683 memcpy( &p_bo->p_buffer[p_bo->i_buffer], p_mem, i_copy );
685 p_bo->i_buffer += i_size;
688 static void bo_addle_str16( bo_t *bo, char *str )
690 bo_addle_u16( bo, strlen( str ) + 1 );
693 uint16_t c = (uint8_t)*str++;
694 bo_addle_u16( bo, c );
695 if( c == '\0' ) break;
699 static void bo_addle_str16_nosize( bo_t *bo, char *str )
703 uint16_t c = (uint8_t)*str++;
704 bo_addle_u16( bo, c );
705 if( c == '\0' ) break;
709 /****************************************************************************
711 ****************************************************************************/
712 static void bo_add_guid( bo_t *p_bo, const guid_t *id )
715 bo_addle_u32( p_bo, id->Data1 );
716 bo_addle_u16( p_bo, id->Data2 );
717 bo_addle_u16( p_bo, id->Data3 );
718 for( i = 0; i < 8; i++ )
720 bo_add_u8( p_bo, id->Data4[i] );
724 static const guid_t asf_object_header_guid =
725 {0x75B22630, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
726 static const guid_t asf_object_data_guid =
727 {0x75B22636, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
728 static const guid_t asf_object_file_properties_guid =
729 {0x8cabdca1, 0xa947, 0x11cf, {0x8e, 0xe4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
730 static const guid_t asf_object_stream_properties_guid =
731 {0xB7DC0791, 0xA9B7, 0x11CF, {0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
732 static const guid_t asf_object_header_extention_guid =
733 {0x5FBF03B5, 0xA92E, 0x11CF, {0x8E, 0xE3, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
734 static const guid_t asf_object_stream_type_audio =
735 {0xF8699E40, 0x5B4D, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
736 static const guid_t asf_object_stream_type_video =
737 {0xbc19efc0, 0x5B4D, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
738 static const guid_t asf_guid_audio_conceal_none =
739 {0x20FB5700, 0x5B55, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
740 static const guid_t asf_guid_video_conceal_none =
741 {0x20FB5700, 0x5B55, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
742 static const guid_t asf_guid_reserved_1 =
743 {0xABD3D211, 0xA9BA, 0x11cf, {0x8E, 0xE6, 0x00, 0xC0, 0x0C ,0x20, 0x53, 0x65}};
744 static const guid_t asf_object_codec_comment_guid =
745 {0x86D15240, 0x311D, 0x11D0, {0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6}};
746 static const guid_t asf_object_codec_comment_reserved_guid =
747 {0x86D15241, 0x311D, 0x11D0, {0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6}};
748 static const guid_t asf_object_content_description_guid =
749 {0x75B22633, 0x668E, 0x11CF, {0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c}};
750 static const guid_t asf_object_index_guid =
751 {0x33000890, 0xE5B1, 0x11CF, {0x89, 0xF4, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xCB}};
753 /****************************************************************************
755 ****************************************************************************/
756 static void asf_chunk_add( bo_t *bo,
757 int i_type, int i_len, int i_flags, int i_seq )
759 bo_addle_u16( bo, i_type );
760 bo_addle_u16( bo, i_len + 8 );
761 bo_addle_u32( bo, i_seq );
762 bo_addle_u16( bo, i_flags );
763 bo_addle_u16( bo, i_len + 8 );
766 static block_t *asf_header_create( sout_mux_t *p_mux, vlc_bool_t b_broadcast )
768 sout_mux_sys_t *p_sys = p_mux->p_sys;
771 mtime_t i_duration = 0;
779 msg_Dbg( p_mux, "Asf muxer creating header" );
781 if( p_sys->i_dts_first > 0 )
783 i_duration = p_sys->i_dts_last - p_sys->i_dts_first;
784 if( i_duration < 0 ) i_duration = 0;
787 /* calculate header size */
788 i_size = 30 + 104 + 46;
790 for( i = 0; i < p_sys->i_track; i++ )
792 i_size += 78 + p_sys->track[i].i_extra;
793 i_ci_size += 8 + 2 * strlen( p_sys->track[i].psz_name );
794 if( p_sys->track[i].i_cat == AUDIO_ES )
798 else if( p_sys->track[i].i_cat == VIDEO_ES )
804 if( *p_sys->psz_title || *p_sys->psz_author || *p_sys->psz_copyright ||
805 *p_sys->psz_comment || *p_sys->psz_rating )
807 i_cd_size = 34 + 2 * ( strlen( p_sys->psz_title ) + 1 +
808 strlen( p_sys->psz_author ) + 1 +
809 strlen( p_sys->psz_copyright ) + 1 +
810 strlen( p_sys->psz_comment ) + 1 +
811 strlen( p_sys->psz_rating ) + 1 );
814 i_size += i_ci_size + i_cd_size;
816 if( p_sys->b_asf_http )
818 out = block_New( p_mux, i_size + 50 + 12 );
819 bo_init( &bo, out->p_buffer, i_size + 50 + 12 );
820 asf_chunk_add( &bo, 0x4824, i_size + 50, 0xc00, p_sys->i_seq++ );
824 out = block_New( p_mux, i_size + 50 );
825 bo_init( &bo, out->p_buffer, i_size + 50 );
829 bo_add_guid ( &bo, &asf_object_header_guid );
830 bo_addle_u64( &bo, i_size );
831 bo_addle_u32( &bo, 2 + p_sys->i_track );
832 bo_add_u8 ( &bo, 1 );
833 bo_add_u8 ( &bo, 2 );
837 /* file properties */
838 bo_add_guid ( &bo, &asf_object_file_properties_guid );
839 bo_addle_u64( &bo, 104 );
840 bo_add_guid ( &bo, &p_sys->fid );
841 bo_addle_u64( &bo, i_size + 50 + p_sys->i_packet_count *
842 p_sys->i_packet_size ); /* file size */
843 bo_addle_u64( &bo, 0 ); /* creation date */
844 bo_addle_u64( &bo, b_broadcast ? 0xffffffffLL : p_sys->i_packet_count );
845 bo_addle_u64( &bo, i_duration * 10 ); /* play duration (100ns) */
846 bo_addle_u64( &bo, i_duration * 10 ); /* send duration (100ns) */
847 bo_addle_u64( &bo, p_sys->i_preroll_time ); /* preroll duration (ms) */
848 bo_addle_u32( &bo, b_broadcast ? 0x01 : 0x00); /* flags */
849 bo_addle_u32( &bo, p_sys->i_packet_size ); /* packet size min */
850 bo_addle_u32( &bo, p_sys->i_packet_size ); /* packet size max */
851 bo_addle_u32( &bo, p_sys->i_bitrate ); /* maxbitrate */
853 /* header extention */
854 bo_add_guid ( &bo, &asf_object_header_extention_guid );
855 bo_addle_u64( &bo, 46 );
856 bo_add_guid ( &bo, &asf_guid_reserved_1 );
857 bo_addle_u16( &bo, 6 );
858 bo_addle_u32( &bo, 0 );
860 /* content description header */
863 bo_add_guid ( &bo, &asf_object_content_description_guid );
864 bo_addle_u64( &bo, i_cd_size );
865 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_title ) + 2 );
866 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_author ) + 2 );
867 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_copyright ) + 2 );
868 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_comment ) + 2 );
869 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_rating ) + 2 );
871 bo_addle_str16_nosize( &bo, p_sys->psz_title );
872 bo_addle_str16_nosize( &bo, p_sys->psz_author );
873 bo_addle_str16_nosize( &bo, p_sys->psz_copyright );
874 bo_addle_str16_nosize( &bo, p_sys->psz_comment );
875 bo_addle_str16_nosize( &bo, p_sys->psz_rating );
878 /* stream properties */
879 for( i = 0; i < p_sys->i_track; i++ )
881 tk = &p_sys->track[i];
883 bo_add_guid ( &bo, &asf_object_stream_properties_guid );
884 bo_addle_u64( &bo, 78 + tk->i_extra );
885 if( tk->i_cat == AUDIO_ES )
887 bo_add_guid( &bo, &asf_object_stream_type_audio );
888 bo_add_guid( &bo, &asf_guid_audio_conceal_none );
890 else if( tk->i_cat == VIDEO_ES )
892 bo_add_guid( &bo, &asf_object_stream_type_video );
893 bo_add_guid( &bo, &asf_guid_video_conceal_none );
895 bo_addle_u64( &bo, 0 ); /* time offset */
896 bo_addle_u32( &bo, tk->i_extra );
897 bo_addle_u32( &bo, 0 ); /* 0 */
898 bo_addle_u16( &bo, tk->i_id ); /* stream number */
899 bo_addle_u32( &bo, 0 );
900 bo_add_mem ( &bo, tk->p_extra, tk->i_extra );
904 bo_add_guid ( &bo, &asf_object_codec_comment_guid );
905 bo_addle_u64( &bo, i_ci_size );
906 bo_add_guid ( &bo, &asf_object_codec_comment_reserved_guid );
907 bo_addle_u32( &bo, p_sys->i_track );
908 for( i = 0; i < p_sys->i_track; i++ )
910 tk = &p_sys->track[i];
912 bo_addle_u16( &bo, tk->i_id );
913 bo_addle_str16( &bo, tk->psz_name );
914 bo_addle_u16( &bo, 0 );
915 if( tk->i_cat == AUDIO_ES )
917 bo_addle_u16( &bo, 2 );
918 bo_addle_u16( &bo, tk->i_tag );
920 else if( tk->i_cat == VIDEO_ES )
922 bo_addle_u16( &bo, 4 );
923 bo_add_mem ( &bo, (uint8_t*)&tk->i_fourcc, 4 );
929 bo_add_guid ( &bo, &asf_object_data_guid );
930 bo_addle_u64( &bo, 50 + p_sys->i_packet_count * p_sys->i_packet_size );
931 bo_add_guid ( &bo, &p_sys->fid );
932 bo_addle_u64( &bo, p_sys->i_packet_count );
933 bo_addle_u16( &bo, 0x101 );
938 /****************************************************************************
940 ****************************************************************************/
941 static block_t *asf_packet_flush( sout_mux_t *p_mux )
943 sout_mux_sys_t *p_sys = p_mux->p_sys;
944 int i_pad, i_preheader = p_sys->b_asf_http ? 12 : 0;
948 if( !p_sys->pk ) return 0;
950 i_pad = p_sys->i_packet_size - p_sys->i_pk_used;
951 memset( p_sys->pk->p_buffer + p_sys->i_pk_used, 0, i_pad );
953 bo_init( &bo, p_sys->pk->p_buffer, 14 + i_preheader );
955 if( p_sys->b_asf_http )
956 asf_chunk_add( &bo, 0x4424, p_sys->i_packet_size, 0x0, p_sys->i_seq++);
958 bo_add_u8 ( &bo, 0x82 );
959 bo_addle_u16( &bo, 0 );
960 bo_add_u8( &bo, 0x11 );
961 bo_add_u8( &bo, 0x5d );
962 bo_addle_u16( &bo, i_pad );
963 bo_addle_u32( &bo, (p_sys->i_pk_dts - p_sys->i_dts_first) / 1000 +
964 p_sys->i_preroll_time );
965 bo_addle_u16( &bo, 0 /* data->i_length */ );
966 bo_add_u8( &bo, 0x80 | p_sys->i_pk_frame );
971 p_sys->i_packet_count++;
976 static block_t *asf_packet_create( sout_mux_t *p_mux,
977 asf_track_t *tk, block_t *data )
979 sout_mux_sys_t *p_sys = p_mux->p_sys;
981 int i_data = data->i_buffer;
983 uint8_t *p_data= data->p_buffer;
984 block_t *first = NULL, **last = &first;
985 int i_preheader = p_sys->b_asf_http ? 12 : 0;
987 while( i_pos < i_data )
992 if( p_sys->pk == NULL )
994 p_sys->pk = block_New( p_mux, p_sys->i_packet_size + i_preheader );
995 /* reserve 14 bytes for the packet header */
996 p_sys->i_pk_used = 14 + i_preheader;
997 p_sys->i_pk_frame = 0;
998 p_sys->i_pk_dts = data->i_dts;
1001 bo_init( &bo, &p_sys->pk->p_buffer[p_sys->i_pk_used],
1002 p_sys->i_packet_size - p_sys->i_pk_used );
1004 /* add payload (header size = 17) */
1005 i_payload = __MIN( i_data - i_pos,
1006 p_sys->i_packet_size - p_sys->i_pk_used - 17 );
1007 bo_add_u8 ( &bo, !(data->i_flags & BLOCK_FLAG_TYPE_P ||
1008 data->i_flags & BLOCK_FLAG_TYPE_B) ?
1009 0x80 | tk->i_id : tk->i_id );
1010 bo_add_u8 ( &bo, tk->i_sequence );
1011 bo_addle_u32( &bo, i_pos );
1012 bo_add_u8 ( &bo, 0x08 ); /* flags */
1013 bo_addle_u32( &bo, i_data );
1014 bo_addle_u32( &bo, (data->i_dts - p_sys->i_dts_first) / 1000 +
1015 p_sys->i_preroll_time );
1016 bo_addle_u16( &bo, i_payload );
1017 bo_add_mem ( &bo, &p_data[i_pos], i_payload );
1019 p_sys->i_pk_used += 17 + i_payload;
1021 p_sys->i_pk_frame++;
1023 if( p_sys->i_pk_used + 17 >= p_sys->i_packet_size )
1025 /* Not enough data for another payload, flush the packet */
1026 *last = asf_packet_flush( p_mux );
1027 last = &(*last)->p_next;
1032 block_Release( data );
1037 static block_t *asf_stream_end_create( sout_mux_t *p_mux )
1039 sout_mux_sys_t *p_sys = p_mux->p_sys;
1041 block_t *out = NULL;
1044 if( p_sys->b_asf_http )
1046 out = block_New( p_mux, 12 );
1047 bo_init( &bo, out->p_buffer, 12 );
1048 asf_chunk_add( &bo, 0x4524, 0, 0x00, p_sys->i_seq++ );
1053 out = block_New( p_mux, 56 );
1054 bo_init( &bo, out->p_buffer, 56 );
1055 bo_add_guid ( &bo, &asf_object_index_guid );
1056 bo_addle_u64( &bo, 56 );
1057 bo_add_guid ( &bo, &p_sys->fid );
1058 bo_addle_u64( &bo, 10000000 );
1059 bo_addle_u32( &bo, 5 );
1060 bo_addle_u32( &bo, 0 );