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 *****************************************************************************/
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")
66 set_description( N_("ASF muxer") );
67 set_category( CAT_SOUT );
68 set_subcategory( SUBCAT_SOUT_MUX );
69 set_shortname( "ASF" );
71 set_capability( "sout mux", 5 );
72 add_shortcut( "asf" );
73 add_shortcut( "asfh" );
74 set_callbacks( Open, Close );
76 add_string( SOUT_CFG_PREFIX "title", "", NULL, TITLE_TEXT, TITLE_LONGTEXT,
78 add_string( SOUT_CFG_PREFIX "author", "", NULL, AUTHOR_TEXT,
79 AUTHOR_LONGTEXT, true );
80 add_string( SOUT_CFG_PREFIX "copyright","", NULL, COPYRIGHT_TEXT,
81 COPYRIGHT_LONGTEXT, true );
82 add_string( SOUT_CFG_PREFIX "comment", "", NULL, COMMENT_TEXT,
83 COMMENT_LONGTEXT, true );
84 add_string( SOUT_CFG_PREFIX "rating", "", NULL, RATING_TEXT,
85 RATING_LONGTEXT, true );
86 add_integer( "sout-asf-packet-size", 4096, NULL, PACKETSIZE_TEXT, PACKETSIZE_LONGTEXT, true );
90 /*****************************************************************************
92 *****************************************************************************/
93 static const char *ppsz_sout_options[] = {
94 "title", "author", "copyright", "comment", "rating", NULL
97 static int Control ( sout_mux_t *, int, va_list );
98 static int AddStream( sout_mux_t *, sout_input_t * );
99 static int DelStream( sout_mux_t *, sout_input_t * );
100 static int Mux ( sout_mux_t * );
107 /* codec information */
108 uint16_t i_tag; /* for audio */
109 vlc_fourcc_t i_fourcc; /* for video */
110 const char *psz_name; /* codec name */
111 int i_blockalign; /* for audio only */
112 bool b_audio_correction;
123 struct sout_mux_sys_t
125 guid_t fid; /* file id */
127 int64_t i_packet_count;
130 mtime_t i_preroll_time;
134 asf_track_t track[MAX_ASF_TRACKS];
154 static int MuxGetStream( sout_mux_t *, int *pi_stream, mtime_t *pi_dts );
156 static block_t *asf_header_create( sout_mux_t *, bool );
157 static block_t *asf_packet_create( sout_mux_t *, asf_track_t *, block_t * );
158 static block_t *asf_stream_end_create( sout_mux_t *);
159 static block_t *asf_packet_flush( sout_mux_t * );
169 static void bo_init ( bo_t *, uint8_t *, int );
170 static void bo_add_u8 ( bo_t *, uint8_t );
171 static void bo_addle_u16( bo_t *, uint16_t );
172 static void bo_addle_u32( bo_t *, uint32_t );
173 static void bo_addle_u64( bo_t *, uint64_t );
174 static void bo_add_mem ( bo_t *, uint8_t *, int );
176 static void bo_addle_str16( bo_t *, const char * );
178 /*****************************************************************************
180 *****************************************************************************/
181 static int Open( vlc_object_t *p_this )
183 sout_mux_t *p_mux = (sout_mux_t*)p_this;
184 sout_mux_sys_t *p_sys;
188 msg_Dbg( p_mux, "asf muxer opened" );
189 config_ChainParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
191 p_mux->pf_control = Control;
192 p_mux->pf_addstream = AddStream;
193 p_mux->pf_delstream = DelStream;
196 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
197 p_sys->b_asf_http = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "asfh" );
198 if( p_sys->b_asf_http )
200 msg_Dbg( p_mux, "creating asf stream to be used with mmsh" );
203 p_sys->i_pk_used = 0;
204 p_sys->i_pk_frame = 0;
205 p_sys->i_dts_first = -1;
206 p_sys->i_dts_last = 0;
207 p_sys->i_preroll_time = 2000;
208 p_sys->i_bitrate = 0;
211 p_sys->b_write_header = true;
213 p_sys->i_packet_size = config_GetInt( p_mux, "sout-asf-packet-size" );
214 msg_Dbg( p_mux, "Packet size %d", p_sys->i_packet_size);
215 p_sys->i_packet_count= 0;
217 /* Generate a random fid */
218 srand( mdate() & 0xffffffff );
219 p_sys->fid.Data1 = 0xbabac001;
220 p_sys->fid.Data2 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
221 p_sys->fid.Data3 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
222 for( i = 0; i < 8; i++ )
224 p_sys->fid.Data4[i] = ( (uint64_t)rand() << 8 ) / RAND_MAX;
228 var_Get( p_mux, SOUT_CFG_PREFIX "title", &val );
229 p_sys->psz_title = val.psz_string;
231 var_Get( p_mux, SOUT_CFG_PREFIX "author", &val );
232 p_sys->psz_author = val.psz_string;
234 var_Get( p_mux, SOUT_CFG_PREFIX "copyright", &val );
235 p_sys->psz_copyright = val.psz_string;
237 var_Get( p_mux, SOUT_CFG_PREFIX "comment", &val );
238 p_sys->psz_comment = val.psz_string;
240 var_Get( p_mux, SOUT_CFG_PREFIX "rating", &val );
241 p_sys->psz_rating = val.psz_string;
243 msg_Dbg( p_mux, "meta data: title='%s', author='%s', copyright='%s', "
244 "comment='%s', rating='%s'",
245 p_sys->psz_title, p_sys->psz_author, p_sys->psz_copyright,
246 p_sys->psz_comment, p_sys->psz_rating );
251 /*****************************************************************************
253 *****************************************************************************/
254 static void Close( vlc_object_t * p_this )
256 sout_mux_t *p_mux = (sout_mux_t*)p_this;
257 sout_mux_sys_t *p_sys = p_mux->p_sys;
261 msg_Dbg( p_mux, "Asf muxer closed" );
263 /* Flush last packet if any */
264 if( (out = asf_packet_flush( p_mux ) ) )
266 sout_AccessOutWrite( p_mux->p_access, out );
269 if( ( out = asf_stream_end_create( p_mux ) ) )
271 sout_AccessOutWrite( p_mux->p_access, out );
275 if( sout_AccessOutSeek( p_mux->p_access, 0 ) == VLC_SUCCESS )
277 out = asf_header_create( p_mux, false );
278 sout_AccessOutWrite( p_mux->p_access, out );
281 for( i = 0; i < p_sys->i_track; i++ )
283 free( p_sys->track[i].p_extra );
284 es_format_Clean( &p_sys->track[i].fmt );
289 /*****************************************************************************
291 *****************************************************************************/
292 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
294 sout_mux_sys_t *p_sys = p_mux->p_sys;
300 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
301 pb_bool = (bool*)va_arg( args, bool * );
302 if( p_sys->b_asf_http ) *pb_bool = true;
303 else *pb_bool = false;
306 case MUX_GET_ADD_STREAM_WAIT:
307 pb_bool = (bool*)va_arg( args, bool * );
312 ppsz = (char**)va_arg( args, char ** );
313 if( p_sys->b_asf_http )
314 *ppsz = strdup( "video/x-ms-asf-stream" );
316 *ppsz = strdup( "video/x-ms-asf" );
324 /*****************************************************************************
326 *****************************************************************************/
327 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
329 sout_mux_sys_t *p_sys = p_mux->p_sys;
333 msg_Dbg( p_mux, "adding input" );
334 if( p_sys->i_track >= MAX_ASF_TRACKS )
336 msg_Dbg( p_mux, "cannot add this track (too much tracks)" );
340 tk = p_input->p_sys = &p_sys->track[p_sys->i_track];
341 tk->i_id = p_sys->i_track + 1;
342 tk->i_cat = p_input->p_fmt->i_cat;
344 tk->b_audio_correction = 0;
350 int i_blockalign = p_input->p_fmt->audio.i_blockalign;
351 int i_bitspersample = p_input->p_fmt->audio.i_bitspersample;
354 switch( p_input->p_fmt->i_codec )
356 case VLC_FOURCC( 'a', '5', '2', ' ' ):
357 tk->i_tag = WAVE_FORMAT_A52;
358 tk->psz_name = "A/52";
361 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
362 tk->i_tag = WAVE_FORMAT_AAC;
363 tk->psz_name = "MPEG-4 Audio";
366 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
368 tk->psz_name = "MPEG Audio Layer 3";
369 tk->i_tag = WAVE_FORMAT_MPEGLAYER3;
375 tk->psz_name = "MPEG Audio Layer 1/2";
376 tk->i_tag = WAVE_FORMAT_MPEG;
382 case VLC_FOURCC( 'w', 'm', 'a', '1' ):
383 tk->psz_name = "Windows Media Audio v1";
384 tk->i_tag = WAVE_FORMAT_WMA1;
385 tk->b_audio_correction = true;
387 case VLC_FOURCC( 'w', 'm', 'a', ' ' ):
388 case VLC_FOURCC( 'w', 'm', 'a', '2' ):
389 tk->psz_name= "Windows Media Audio (v2) 7, 8 and 9 Series";
390 tk->i_tag = WAVE_FORMAT_WMA2;
391 tk->b_audio_correction = true;
393 case VLC_FOURCC( 'w', 'm', 'a', 'p' ):
394 tk->psz_name = "Windows Media Audio 9 Professional";
395 tk->i_tag = WAVE_FORMAT_WMAP;
396 tk->b_audio_correction = true;
398 case VLC_FOURCC( 'w', 'm', 'a', 'l' ):
399 tk->psz_name = "Windows Media Audio 9 Lossless";
400 tk->i_tag = WAVE_FORMAT_WMAL;
401 tk->b_audio_correction = true;
404 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
405 tk->psz_name = "Raw audio 8bits";
406 tk->i_tag = WAVE_FORMAT_PCM;
407 i_blockalign= p_input->p_fmt->audio.i_channels;
410 case VLC_FOURCC( 's', '1', '6', 'l' ):
411 tk->psz_name = "Raw audio 16bits";
412 tk->i_tag = WAVE_FORMAT_PCM;
413 i_blockalign= 2 * p_input->p_fmt->audio.i_channels;
414 i_bitspersample = 16;
416 case VLC_FOURCC( 's', '2', '4', 'l' ):
417 tk->psz_name = "Raw audio 24bits";
418 tk->i_tag = WAVE_FORMAT_PCM;
419 i_blockalign= 3 * p_input->p_fmt->audio.i_channels;
420 i_bitspersample = 24;
422 case VLC_FOURCC( 's', '3', '2', 'l' ):
423 tk->psz_name = "Raw audio 32bits";
424 tk->i_tag = WAVE_FORMAT_PCM;
425 i_blockalign= 4 * p_input->p_fmt->audio.i_channels;
426 i_bitspersample = 32;
432 tk->i_extra = sizeof( WAVEFORMATEX ) +
433 p_input->p_fmt->i_extra + i_extra;
434 tk->p_extra = malloc( tk->i_extra );
435 bo_init( &bo, tk->p_extra, tk->i_extra );
436 bo_addle_u16( &bo, tk->i_tag );
437 bo_addle_u16( &bo, p_input->p_fmt->audio.i_channels );
438 bo_addle_u32( &bo, p_input->p_fmt->audio.i_rate );
439 bo_addle_u32( &bo, p_input->p_fmt->i_bitrate / 8 );
440 bo_addle_u16( &bo, i_blockalign );
441 tk->i_blockalign = i_blockalign;
442 bo_addle_u16( &bo, i_bitspersample );
443 if( p_input->p_fmt->i_extra > 0 )
445 bo_addle_u16( &bo, p_input->p_fmt->i_extra );
446 bo_add_mem ( &bo, p_input->p_fmt->p_extra,
447 p_input->p_fmt->i_extra );
451 bo_addle_u16( &bo, i_extra );
452 if( tk->i_tag == WAVE_FORMAT_MPEGLAYER3 )
454 msg_Dbg( p_mux, "adding mp3 header" );
455 bo_addle_u16( &bo, 1 ); /* wId */
456 bo_addle_u32( &bo, 2 ); /* fdwFlags */
457 bo_addle_u16( &bo, 1152 ); /* nBlockSize */
458 bo_addle_u16( &bo, 1 ); /* nFramesPerBlock */
459 bo_addle_u16( &bo, 1393 ); /* nCodecDelay */
461 else if( tk->i_tag == WAVE_FORMAT_MPEG )
463 msg_Dbg( p_mux, "adding mp2 header" );
464 bo_addle_u16( &bo, 2 ); /* fwHeadLayer */
465 bo_addle_u32( &bo, p_input->p_fmt->i_bitrate );
466 bo_addle_u16( &bo, p_input->p_fmt->audio.i_channels == 2 ?1:8 );
467 bo_addle_u16( &bo, 0 ); /* fwHeadModeExt */
468 bo_addle_u16( &bo, 1 ); /* wHeadEmphasis */
469 bo_addle_u16( &bo, 16 ); /* fwHeadFlags */
470 bo_addle_u32( &bo, 0 ); /* dwPTSLow */
471 bo_addle_u32( &bo, 0 ); /* dwPTSHigh */
475 if( p_input->p_fmt->i_bitrate > 24000 )
477 p_sys->i_bitrate += p_input->p_fmt->i_bitrate;
481 p_sys->i_bitrate += 512000;
487 tk->i_extra = 11 + sizeof( BITMAPINFOHEADER ) +
488 p_input->p_fmt->i_extra;
489 tk->p_extra = malloc( tk->i_extra );
490 bo_init( &bo, tk->p_extra, tk->i_extra );
491 bo_addle_u32( &bo, p_input->p_fmt->video.i_width );
492 bo_addle_u32( &bo, p_input->p_fmt->video.i_height );
493 bo_add_u8 ( &bo, 0x02 ); /* flags */
494 bo_addle_u16( &bo, sizeof( BITMAPINFOHEADER ) +
495 p_input->p_fmt->i_extra );
496 bo_addle_u32( &bo, sizeof( BITMAPINFOHEADER ) +
497 p_input->p_fmt->i_extra );
498 bo_addle_u32( &bo, p_input->p_fmt->video.i_width );
499 bo_addle_u32( &bo, p_input->p_fmt->video.i_height );
500 bo_addle_u16( &bo, 1 );
501 bo_addle_u16( &bo, 24 );
502 if( p_input->p_fmt->i_codec == VLC_FOURCC('m','p','4','v') )
504 tk->psz_name = "MPEG-4 Video";
505 tk->i_fourcc = VLC_FOURCC( 'M', 'P', '4', 'S' );
507 else if( p_input->p_fmt->i_codec == VLC_FOURCC('D','I','V','3') )
509 tk->psz_name = "MSMPEG-4 V3 Video";
510 tk->i_fourcc = VLC_FOURCC( 'M', 'P', '4', '3' );
512 else if( p_input->p_fmt->i_codec == VLC_FOURCC('D','I','V','2') )
514 tk->psz_name = "MSMPEG-4 V2 Video";
515 tk->i_fourcc = VLC_FOURCC( 'M', 'P', '4', '2' );
517 else if( p_input->p_fmt->i_codec == VLC_FOURCC('D','I','V','1') )
519 tk->psz_name = "MSMPEG-4 V1 Video";
520 tk->i_fourcc = VLC_FOURCC( 'M', 'P', 'G', '4' );
522 else if( p_input->p_fmt->i_codec == VLC_FOURCC('W','M','V','1') )
524 tk->psz_name = "Windows Media Video 7";
525 tk->i_fourcc = VLC_FOURCC( 'W', 'M', 'V', '1' );
527 else if( p_input->p_fmt->i_codec == VLC_FOURCC('W','M','V','2') )
529 tk->psz_name = "Windows Media Video 8";
530 tk->i_fourcc = VLC_FOURCC( 'W', 'M', 'V', '2' );
532 else if( p_input->p_fmt->i_codec == VLC_FOURCC('W','M','V','3') )
534 tk->psz_name = "Windows Media Video 9";
535 tk->i_fourcc = VLC_FOURCC( 'W', 'M', 'V', '3' );
537 else if( p_input->p_fmt->i_codec == VLC_FOURCC('h','2','6','4') )
539 tk->psz_name = "H.264/MPEG-4 AVC";
540 tk->i_fourcc = VLC_FOURCC('h','2','6','4');
544 tk->psz_name = _("Unknown Video");
545 tk->i_fourcc = p_input->p_fmt->i_codec;
547 bo_add_mem( &bo, (uint8_t*)&tk->i_fourcc, 4 );
548 bo_addle_u32( &bo, 0 );
549 bo_addle_u32( &bo, 0 );
550 bo_addle_u32( &bo, 0 );
551 bo_addle_u32( &bo, 0 );
552 bo_addle_u32( &bo, 0 );
553 if( p_input->p_fmt->i_extra > 0 )
555 bo_add_mem ( &bo, p_input->p_fmt->p_extra,
556 p_input->p_fmt->i_extra );
559 if( p_input->p_fmt->i_bitrate > 50000 )
561 p_sys->i_bitrate += p_input->p_fmt->i_bitrate;
565 p_sys->i_bitrate += 1000000;
570 msg_Err(p_mux, "unhandled track type" );
574 es_format_Copy( &tk->fmt, p_input->p_fmt );
580 /*****************************************************************************
582 *****************************************************************************/
583 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
586 msg_Dbg( p_mux, "removing input" );
590 /*****************************************************************************
592 *****************************************************************************/
593 static int Mux( sout_mux_t *p_mux )
595 sout_mux_sys_t *p_sys = p_mux->p_sys;
597 if( p_sys->b_write_header )
599 block_t *out = asf_header_create( p_mux, true );
601 out->i_flags |= BLOCK_FLAG_HEADER;
602 sout_AccessOutWrite( p_mux->p_access, out );
604 p_sys->b_write_header = false;
609 sout_input_t *p_input;
616 if( MuxGetStream( p_mux, &i_stream, &i_dts ) )
618 /* not enough data */
622 if( p_sys->i_dts_first < 0 )
624 p_sys->i_dts_first = i_dts;
626 if( p_sys->i_dts_last < i_dts )
628 p_sys->i_dts_last = i_dts;
631 p_input = p_mux->pp_inputs[i_stream];
632 tk = (asf_track_t*)p_input->p_sys;
634 data = block_FifoGet( p_input->p_fifo );
636 if( ( pk = asf_packet_create( p_mux, tk, data ) ) )
638 sout_AccessOutWrite( p_mux->p_access, pk );
645 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
651 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
653 sout_input_t *p_input = p_mux->pp_inputs[i];
656 if( block_FifoCount( p_input->p_fifo ) <= 0 )
658 if( p_input->p_fmt->i_cat == AUDIO_ES ||
659 p_input->p_fmt->i_cat == VIDEO_ES )
661 /* We need that audio+video fifo contain at least 1 packet */
668 p_data = block_FifoShow( p_input->p_fifo );
669 if( i_stream == -1 || p_data->i_dts < i_dts )
672 i_dts = p_data->i_dts;
676 *pi_stream = i_stream;
682 /****************************************************************************
683 * Asf header construction
684 ****************************************************************************/
686 /****************************************************************************
688 ****************************************************************************/
689 static void bo_init( bo_t *p_bo, uint8_t *p_buffer, int i_size )
691 p_bo->i_buffer_size = i_size;
693 p_bo->p_buffer = p_buffer;
695 static void bo_add_u8( bo_t *p_bo, uint8_t i )
697 if( p_bo->i_buffer < p_bo->i_buffer_size )
699 p_bo->p_buffer[p_bo->i_buffer] = i;
703 static void bo_addle_u16( bo_t *p_bo, uint16_t i )
705 bo_add_u8( p_bo, i &0xff );
706 bo_add_u8( p_bo, ( ( i >> 8) &0xff ) );
708 static void bo_addle_u32( bo_t *p_bo, uint32_t i )
710 bo_addle_u16( p_bo, i &0xffff );
711 bo_addle_u16( p_bo, ( ( i >> 16) &0xffff ) );
713 static void bo_addle_u64( bo_t *p_bo, uint64_t i )
715 bo_addle_u32( p_bo, i &0xffffffff );
716 bo_addle_u32( p_bo, ( ( i >> 32) &0xffffffff ) );
719 static void bo_add_mem( bo_t *p_bo, uint8_t *p_mem, int i_size )
721 int i_copy = __MIN( i_size, p_bo->i_buffer_size - p_bo->i_buffer );
725 memcpy( &p_bo->p_buffer[p_bo->i_buffer], p_mem, i_copy );
727 p_bo->i_buffer += i_size;
730 static void bo_addle_str16( bo_t *bo, const char *str )
732 bo_addle_u16( bo, strlen( str ) + 1 );
735 uint16_t c = (uint8_t)*str++;
736 bo_addle_u16( bo, c );
737 if( c == '\0' ) break;
741 static void bo_addle_str16_nosize( bo_t *bo, const char *str )
745 uint16_t c = (uint8_t)*str++;
746 bo_addle_u16( bo, c );
747 if( c == '\0' ) break;
751 /****************************************************************************
753 ****************************************************************************/
754 static void bo_add_guid( bo_t *p_bo, const guid_t *id )
757 bo_addle_u32( p_bo, id->Data1 );
758 bo_addle_u16( p_bo, id->Data2 );
759 bo_addle_u16( p_bo, id->Data3 );
760 for( i = 0; i < 8; i++ )
762 bo_add_u8( p_bo, id->Data4[i] );
766 static const guid_t asf_object_header_guid =
767 {0x75B22630, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
768 static const guid_t asf_object_data_guid =
769 {0x75B22636, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
770 static const guid_t asf_object_file_properties_guid =
771 {0x8cabdca1, 0xa947, 0x11cf, {0x8e, 0xe4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
772 static const guid_t asf_object_stream_properties_guid =
773 {0xB7DC0791, 0xA9B7, 0x11CF, {0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
774 static const guid_t asf_object_header_extension_guid =
775 {0x5FBF03B5, 0xA92E, 0x11CF, {0x8E, 0xE3, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
776 static const guid_t asf_object_stream_type_audio =
777 {0xF8699E40, 0x5B4D, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
778 static const guid_t asf_object_stream_type_video =
779 {0xbc19efc0, 0x5B4D, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
780 static const guid_t asf_guid_audio_conceal_none =
781 {0x20FB5700, 0x5B55, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
782 static const guid_t asf_guid_audio_conceal_spread =
783 {0xBFC3CD50, 0x618F, 0x11CF, {0x8B, 0xB2, 0x00, 0xAA, 0x00, 0xB4, 0xE2, 0x20}};
784 static const guid_t asf_guid_video_conceal_none =
785 {0x20FB5700, 0x5B55, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
786 static const guid_t asf_guid_reserved_1 =
787 {0xABD3D211, 0xA9BA, 0x11cf, {0x8E, 0xE6, 0x00, 0xC0, 0x0C ,0x20, 0x53, 0x65}};
788 static const guid_t asf_object_codec_list_guid =
789 {0x86D15240, 0x311D, 0x11D0, {0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6}};
790 static const guid_t asf_object_codec_list_reserved_guid =
791 {0x86D15241, 0x311D, 0x11D0, {0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6}};
792 static const guid_t asf_object_content_description_guid =
793 {0x75B22633, 0x668E, 0x11CF, {0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c}};
794 static const guid_t asf_object_index_guid =
795 {0x33000890, 0xE5B1, 0x11CF, {0x89, 0xF4, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xCB}};
796 static const guid_t asf_object_metadata_guid =
797 {0xC5F8CBEA, 0x5BAF, 0x4877, {0x84, 0x67, 0xAA, 0x8C, 0x44, 0xFA, 0x4C, 0xCA}};
799 /****************************************************************************
801 ****************************************************************************/
802 static void asf_chunk_add( bo_t *bo,
803 int i_type, int i_len, int i_flags, int i_seq )
805 bo_addle_u16( bo, i_type );
806 bo_addle_u16( bo, i_len + 8 );
807 bo_addle_u32( bo, i_seq );
808 bo_addle_u16( bo, i_flags );
809 bo_addle_u16( bo, i_len + 8 );
812 static block_t *asf_header_create( sout_mux_t *p_mux, bool b_broadcast )
814 sout_mux_sys_t *p_sys = p_mux->p_sys;
816 mtime_t i_duration = 0;
817 int i_size, i_header_ext_size, i;
818 int i_ci_size, i_cm_size = 0, i_cd_size = 0;
822 msg_Dbg( p_mux, "Asf muxer creating header" );
824 if( p_sys->i_dts_first > 0 )
826 i_duration = p_sys->i_dts_last - p_sys->i_dts_first;
827 if( i_duration < 0 ) i_duration = 0;
830 /* calculate header size */
833 for( i = 0; i < p_sys->i_track; i++ )
835 i_size += 78 + p_sys->track[i].i_extra;
836 i_ci_size += 8 + 2 * strlen( p_sys->track[i].psz_name );
837 if( p_sys->track[i].i_cat == AUDIO_ES ) i_ci_size += 4;
838 else if( p_sys->track[i].i_cat == VIDEO_ES ) i_ci_size += 6;
840 /* Error correction data field */
841 if( p_sys->track[i].b_audio_correction ) i_size += 8;
844 /* size of the content description object */
845 if( *p_sys->psz_title || *p_sys->psz_author || *p_sys->psz_copyright ||
846 *p_sys->psz_comment || *p_sys->psz_rating )
848 i_cd_size = 34 + 2 * ( strlen( p_sys->psz_title ) + 1 +
849 strlen( p_sys->psz_author ) + 1 +
850 strlen( p_sys->psz_copyright ) + 1 +
851 strlen( p_sys->psz_comment ) + 1 +
852 strlen( p_sys->psz_rating ) + 1 );
855 /* size of the metadata object */
856 for( i = 0; i < p_sys->i_track; i++ )
858 if( p_sys->track[i].i_cat == VIDEO_ES )
860 i_cm_size = 26 + 2 * (16 + 2 * sizeof("AspectRatio?"));
865 i_header_ext_size = i_cm_size ? i_cm_size + 46 : 0;
866 i_size += i_ci_size + i_cd_size + i_header_ext_size ;
868 if( p_sys->b_asf_http )
870 out = block_New( p_mux, i_size + 50 + 12 );
871 bo_init( &bo, out->p_buffer, i_size + 50 + 12 );
872 asf_chunk_add( &bo, 0x4824, i_size + 50, 0xc00, p_sys->i_seq++ );
876 out = block_New( p_mux, i_size + 50 );
877 bo_init( &bo, out->p_buffer, i_size + 50 );
881 bo_add_guid ( &bo, &asf_object_header_guid );
882 bo_addle_u64( &bo, i_size );
883 bo_addle_u32( &bo, 2 + p_sys->i_track +
884 (i_cd_size ? 1 : 0) + (i_cm_size ? 1 : 0) );
885 bo_add_u8 ( &bo, 1 );
886 bo_add_u8 ( &bo, 2 );
890 /* file properties */
891 bo_add_guid ( &bo, &asf_object_file_properties_guid );
892 bo_addle_u64( &bo, 104 );
893 bo_add_guid ( &bo, &p_sys->fid );
894 bo_addle_u64( &bo, i_size + 50 + p_sys->i_packet_count *
895 p_sys->i_packet_size ); /* file size */
896 bo_addle_u64( &bo, 0 ); /* creation date */
897 bo_addle_u64( &bo, b_broadcast ? 0xffffffffLL : p_sys->i_packet_count );
898 bo_addle_u64( &bo, i_duration * 10 ); /* play duration (100ns) */
899 bo_addle_u64( &bo, i_duration * 10 ); /* send duration (100ns) */
900 bo_addle_u64( &bo, p_sys->i_preroll_time ); /* preroll duration (ms) */
901 bo_addle_u32( &bo, b_broadcast ? 0x01 : 0x02 /* seekable */ ); /* flags */
902 bo_addle_u32( &bo, p_sys->i_packet_size ); /* packet size min */
903 bo_addle_u32( &bo, p_sys->i_packet_size ); /* packet size max */
904 bo_addle_u32( &bo, p_sys->i_bitrate ); /* maxbitrate */
906 /* header extension */
907 if( i_header_ext_size )
909 bo_add_guid ( &bo, &asf_object_header_extension_guid );
910 bo_addle_u64( &bo, i_header_ext_size );
911 bo_add_guid ( &bo, &asf_guid_reserved_1 );
912 bo_addle_u16( &bo, 6 );
913 bo_addle_u32( &bo, i_header_ext_size - 46 );
916 /* metadata object (part of header extension) */
919 int64_t i_num, i_den;
920 unsigned int i_dst_num, i_dst_den;
922 for( i = 0; i < p_sys->i_track; i++ )
923 if( p_sys->track[i].i_cat == VIDEO_ES ) break;
925 i_num = p_sys->track[i].fmt.video.i_aspect *
926 (int64_t)p_sys->track[i].fmt.video.i_height;
927 i_den = VOUT_ASPECT_FACTOR * p_sys->track[i].fmt.video.i_width;
928 vlc_ureduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
930 msg_Dbg( p_mux, "pixel aspect-ratio: %i/%i", i_dst_num, i_dst_den );
932 bo_add_guid ( &bo, &asf_object_metadata_guid );
933 bo_addle_u64( &bo, i_cm_size );
934 bo_addle_u16( &bo, 2 ); /* description records count */
935 /* 1st description record */
936 bo_addle_u16( &bo, 0 ); /* reserved */
937 bo_addle_u16( &bo, i + 1 ); /* stream number (0 for the whole file) */
938 bo_addle_u16( &bo, 2 * sizeof("AspectRatioX") ); /* name length */
939 bo_addle_u16( &bo, 0x3 /* DWORD */ ); /* data type */
940 bo_addle_u32( &bo, 4 ); /* data length */
941 bo_addle_str16_nosize( &bo, "AspectRatioX" );
942 bo_addle_u32( &bo, i_dst_num ); /* data */
943 /* 2nd description record */
944 bo_addle_u16( &bo, 0 ); /* reserved */
945 bo_addle_u16( &bo, i + 1 ); /* stream number (0 for the whole file) */
946 bo_addle_u16( &bo, 2 * sizeof("AspectRatioY") ); /* name length */
947 bo_addle_u16( &bo, 0x3 /* DWORD */ ); /* data type */
948 bo_addle_u32( &bo, 4 ); /* data length */
949 bo_addle_str16_nosize( &bo, "AspectRatioY" );
950 bo_addle_u32( &bo, i_dst_den ); /* data */
953 /* content description header */
956 bo_add_guid ( &bo, &asf_object_content_description_guid );
957 bo_addle_u64( &bo, i_cd_size );
958 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_title ) + 2 );
959 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_author ) + 2 );
960 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_copyright ) + 2 );
961 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_comment ) + 2 );
962 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_rating ) + 2 );
964 bo_addle_str16_nosize( &bo, p_sys->psz_title );
965 bo_addle_str16_nosize( &bo, p_sys->psz_author );
966 bo_addle_str16_nosize( &bo, p_sys->psz_copyright );
967 bo_addle_str16_nosize( &bo, p_sys->psz_comment );
968 bo_addle_str16_nosize( &bo, p_sys->psz_rating );
971 /* stream properties */
972 for( i = 0; i < p_sys->i_track; i++ )
974 tk = &p_sys->track[i];
976 bo_add_guid ( &bo, &asf_object_stream_properties_guid );
977 bo_addle_u64( &bo, 78 + tk->i_extra + (tk->b_audio_correction ? 8:0) );
979 if( tk->i_cat == AUDIO_ES )
981 bo_add_guid( &bo, &asf_object_stream_type_audio );
982 if( tk->b_audio_correction )
983 bo_add_guid( &bo, &asf_guid_audio_conceal_spread );
985 bo_add_guid( &bo, &asf_guid_audio_conceal_none );
987 else if( tk->i_cat == VIDEO_ES )
989 bo_add_guid( &bo, &asf_object_stream_type_video );
990 bo_add_guid( &bo, &asf_guid_video_conceal_none );
992 bo_addle_u64( &bo, 0 ); /* time offset */
993 bo_addle_u32( &bo, tk->i_extra );
994 /* correction data length */
995 bo_addle_u32( &bo, tk->b_audio_correction ? 8 : 0 );
996 bo_addle_u16( &bo, tk->i_id ); /* stream number */
997 bo_addle_u32( &bo, 0 );
998 bo_add_mem ( &bo, tk->p_extra, tk->i_extra );
1000 /* Error correction data field */
1001 if( tk->b_audio_correction )
1003 bo_add_u8( &bo, 0x1 ); /* span */
1004 bo_addle_u16( &bo, tk->i_blockalign ); /* virtual packet length */
1005 bo_addle_u16( &bo, tk->i_blockalign ); /* virtual chunck length */
1006 bo_addle_u16( &bo, 1 ); /* silence length */
1007 bo_add_u8( &bo, 0x0 ); /* data */
1012 bo_add_guid ( &bo, &asf_object_codec_list_guid );
1013 bo_addle_u64( &bo, i_ci_size );
1014 bo_add_guid ( &bo, &asf_object_codec_list_reserved_guid );
1015 bo_addle_u32( &bo, p_sys->i_track );
1016 for( i = 0; i < p_sys->i_track; i++ )
1018 tk = &p_sys->track[i];
1020 if( tk->i_cat == VIDEO_ES ) bo_addle_u16( &bo, 1 /* video */ );
1021 else if( tk->i_cat == AUDIO_ES ) bo_addle_u16( &bo, 2 /* audio */ );
1022 else bo_addle_u16( &bo, 0xFFFF /* unknown */ );
1024 bo_addle_str16( &bo, tk->psz_name );
1025 bo_addle_u16( &bo, 0 );
1026 if( tk->i_cat == AUDIO_ES )
1028 bo_addle_u16( &bo, 2 );
1029 bo_addle_u16( &bo, tk->i_tag );
1031 else if( tk->i_cat == VIDEO_ES )
1033 bo_addle_u16( &bo, 4 );
1034 bo_add_mem ( &bo, (uint8_t*)&tk->i_fourcc, 4 );
1039 bo_add_guid ( &bo, &asf_object_data_guid );
1040 bo_addle_u64( &bo, 50 + p_sys->i_packet_count * p_sys->i_packet_size );
1041 bo_add_guid ( &bo, &p_sys->fid );
1042 bo_addle_u64( &bo, p_sys->i_packet_count );
1043 bo_addle_u16( &bo, 0x101 );
1048 /****************************************************************************
1050 ****************************************************************************/
1051 static block_t *asf_packet_flush( sout_mux_t *p_mux )
1053 sout_mux_sys_t *p_sys = p_mux->p_sys;
1054 int i_pad, i_preheader = p_sys->b_asf_http ? 12 : 0;
1058 if( !p_sys->pk ) return 0;
1060 i_pad = p_sys->i_packet_size - p_sys->i_pk_used;
1061 memset( p_sys->pk->p_buffer + p_sys->i_pk_used, 0, i_pad );
1063 bo_init( &bo, p_sys->pk->p_buffer, 14 + i_preheader );
1065 if( p_sys->b_asf_http )
1066 asf_chunk_add( &bo, 0x4424, p_sys->i_packet_size, 0x0, p_sys->i_seq++);
1068 bo_add_u8 ( &bo, 0x82 );
1069 bo_addle_u16( &bo, 0 );
1070 bo_add_u8( &bo, 0x11 );
1071 bo_add_u8( &bo, 0x5d );
1072 bo_addle_u16( &bo, i_pad );
1073 bo_addle_u32( &bo, (p_sys->i_pk_dts - p_sys->i_dts_first) / 1000 +
1074 p_sys->i_preroll_time );
1075 bo_addle_u16( &bo, 0 /* data->i_length */ );
1076 bo_add_u8( &bo, 0x80 | p_sys->i_pk_frame );
1081 p_sys->i_packet_count++;
1086 static block_t *asf_packet_create( sout_mux_t *p_mux,
1087 asf_track_t *tk, block_t *data )
1089 sout_mux_sys_t *p_sys = p_mux->p_sys;
1091 int i_data = data->i_buffer;
1093 uint8_t *p_data= data->p_buffer;
1094 block_t *first = NULL, **last = &first;
1095 int i_preheader = p_sys->b_asf_http ? 12 : 0;
1097 while( i_pos < i_data )
1102 if( p_sys->pk == NULL )
1104 p_sys->pk = block_New( p_mux, p_sys->i_packet_size + i_preheader );
1105 /* reserve 14 bytes for the packet header */
1106 p_sys->i_pk_used = 14 + i_preheader;
1107 p_sys->i_pk_frame = 0;
1108 p_sys->i_pk_dts = data->i_dts;
1111 bo_init( &bo, &p_sys->pk->p_buffer[p_sys->i_pk_used],
1112 p_sys->i_packet_size - p_sys->i_pk_used );
1114 /* add payload (header size = 17) */
1115 i_payload = __MIN( i_data - i_pos,
1116 p_sys->i_packet_size - p_sys->i_pk_used - 17 );
1118 if( tk->b_audio_correction && p_sys->i_pk_frame && i_payload < i_data )
1120 /* Don't know why yet but WMP doesn't like splitted WMA packets */
1121 *last = asf_packet_flush( p_mux );
1122 last = &(*last)->p_next;
1126 bo_add_u8 ( &bo, !(data->i_flags & BLOCK_FLAG_TYPE_P ||
1127 data->i_flags & BLOCK_FLAG_TYPE_B) ?
1128 0x80 | tk->i_id : tk->i_id );
1129 bo_add_u8 ( &bo, tk->i_sequence );
1130 bo_addle_u32( &bo, i_pos );
1131 bo_add_u8 ( &bo, 0x08 ); /* flags */
1132 bo_addle_u32( &bo, i_data );
1133 bo_addle_u32( &bo, (data->i_dts - p_sys->i_dts_first) / 1000 +
1134 p_sys->i_preroll_time );
1135 bo_addle_u16( &bo, i_payload );
1136 bo_add_mem ( &bo, &p_data[i_pos], i_payload );
1138 p_sys->i_pk_used += 17 + i_payload;
1140 p_sys->i_pk_frame++;
1142 if( p_sys->i_pk_used + 17 >= p_sys->i_packet_size )
1144 /* Not enough data for another payload, flush the packet */
1145 *last = asf_packet_flush( p_mux );
1146 last = &(*last)->p_next;
1151 block_Release( data );
1156 static block_t *asf_stream_end_create( sout_mux_t *p_mux )
1158 sout_mux_sys_t *p_sys = p_mux->p_sys;
1160 block_t *out = NULL;
1163 if( p_sys->b_asf_http )
1165 out = block_New( p_mux, 12 );
1166 bo_init( &bo, out->p_buffer, 12 );
1167 asf_chunk_add( &bo, 0x4524, 0, 0x00, p_sys->i_seq++ );
1172 out = block_New( p_mux, 56 );
1173 bo_init( &bo, out->p_buffer, 56 );
1174 bo_add_guid ( &bo, &asf_object_index_guid );
1175 bo_addle_u64( &bo, 56 );
1176 bo_add_guid ( &bo, &p_sys->fid );
1177 bo_addle_u64( &bo, 10000000 );
1178 bo_addle_u32( &bo, 5 );
1179 bo_addle_u32( &bo, 0 );