1 /*****************************************************************************
2 * asf.c: asf muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2003-2004, 2006 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <vlc_block.h>
37 #include <vlc_codecs.h>
41 #define MAX_ASF_TRACKS 128
42 #define ASF_DATA_PACKET_SIZE 4096 // deprecated -- added sout-asf-packet-size
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Open ( vlc_object_t * );
48 static void Close ( vlc_object_t * );
50 #define SOUT_CFG_PREFIX "sout-asf-"
52 #define TITLE_TEXT N_("Title")
53 #define TITLE_LONGTEXT N_("Title to put in ASF comments." )
54 #define AUTHOR_TEXT N_("Author")
55 #define AUTHOR_LONGTEXT N_("Author to put in ASF comments." )
56 #define COPYRIGHT_TEXT N_("Copyright")
57 #define COPYRIGHT_LONGTEXT N_("Copyright string to put in ASF comments." )
58 #define COMMENT_TEXT N_("Comment")
59 #define COMMENT_LONGTEXT N_("Comment to put in ASF comments." )
60 #define RATING_TEXT N_("Rating")
61 #define RATING_LONGTEXT N_("\"Rating\" to put in ASF comments." )
62 #define PACKETSIZE_TEXT N_("Packet Size")
63 #define PACKETSIZE_LONGTEXT N_("ASF packet size -- default is 4096 bytes")
64 #define BITRATE_TEXT N_("Bitrate override")
65 #define BITRATE_LONGTEXT N_("Do not try to guess ASF bitrate. Setting this, allows you to control how Windows Media Player will cache streamed content. Set to audio+video bitrate in bytes")
69 set_description( N_("ASF muxer") )
70 set_category( CAT_SOUT )
71 set_subcategory( SUBCAT_SOUT_MUX )
72 set_shortname( "ASF" )
74 set_capability( "sout mux", 5 )
76 add_shortcut( "asfh" )
77 set_callbacks( Open, Close )
79 add_string( SOUT_CFG_PREFIX "title", "", NULL, TITLE_TEXT, TITLE_LONGTEXT,
81 add_string( SOUT_CFG_PREFIX "author", "", NULL, AUTHOR_TEXT,
82 AUTHOR_LONGTEXT, true )
83 add_string( SOUT_CFG_PREFIX "copyright","", NULL, COPYRIGHT_TEXT,
84 COPYRIGHT_LONGTEXT, true )
85 add_string( SOUT_CFG_PREFIX "comment", "", NULL, COMMENT_TEXT,
86 COMMENT_LONGTEXT, true )
87 add_string( SOUT_CFG_PREFIX "rating", "", NULL, RATING_TEXT,
88 RATING_LONGTEXT, true )
89 add_integer( SOUT_CFG_PREFIX "packet-size", 4096, NULL, PACKETSIZE_TEXT,
90 PACKETSIZE_LONGTEXT, true )
91 add_integer( SOUT_CFG_PREFIX "bitrate-override", 0, NULL, BITRATE_TEXT,
92 BITRATE_LONGTEXT, true )
96 /*****************************************************************************
98 *****************************************************************************/
99 static const char *const ppsz_sout_options[] = {
100 "title", "author", "copyright", "comment", "rating", NULL
103 static int Control ( sout_mux_t *, int, va_list );
104 static int AddStream( sout_mux_t *, sout_input_t * );
105 static int DelStream( sout_mux_t *, sout_input_t * );
106 static int Mux ( sout_mux_t * );
113 /* codec information */
114 uint16_t i_tag; /* for audio */
115 vlc_fourcc_t i_fourcc; /* for video */
116 const char *psz_name; /* codec name */
117 int i_blockalign; /* for audio only */
118 bool b_audio_correction;
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 ) );
206 p_sys->b_asf_http = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "asfh" );
207 if( p_sys->b_asf_http )
209 msg_Dbg( p_mux, "creating asf stream to be used with mmsh" );
212 p_sys->i_pk_used = 0;
213 p_sys->i_pk_frame = 0;
214 p_sys->i_dts_first = -1;
215 p_sys->i_dts_last = 0;
216 p_sys->i_preroll_time = 2000;
217 p_sys->i_bitrate = 0;
218 p_sys->i_bitrate_override = 0;
221 p_sys->b_write_header = true;
223 p_sys->i_packet_size = config_GetInt( p_mux, "sout-asf-packet-size" );
224 p_sys->i_bitrate_override = config_GetInt( p_mux, "sout-asf-bitrate-override" );
225 msg_Dbg( p_mux, "Packet size %d", p_sys->i_packet_size);
226 if (p_sys->i_bitrate_override)
227 msg_Dbg( p_mux, "Bitrate override %"PRId64, p_sys->i_bitrate_override);
228 p_sys->i_packet_count= 0;
230 /* Generate a random fid */
231 srand( mdate() & 0xffffffff );
232 p_sys->fid.Data1 = 0xbabac001;
233 p_sys->fid.Data2 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
234 p_sys->fid.Data3 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
235 for( i = 0; i < 8; i++ )
237 p_sys->fid.Data4[i] = ( (uint64_t)rand() << 8 ) / RAND_MAX;
241 var_Get( p_mux, SOUT_CFG_PREFIX "title", &val );
242 p_sys->psz_title = val.psz_string;
244 var_Get( p_mux, SOUT_CFG_PREFIX "author", &val );
245 p_sys->psz_author = val.psz_string;
247 var_Get( p_mux, SOUT_CFG_PREFIX "copyright", &val );
248 p_sys->psz_copyright = val.psz_string;
250 var_Get( p_mux, SOUT_CFG_PREFIX "comment", &val );
251 p_sys->psz_comment = val.psz_string;
253 var_Get( p_mux, SOUT_CFG_PREFIX "rating", &val );
254 p_sys->psz_rating = val.psz_string;
256 msg_Dbg( p_mux, "meta data: title='%s', author='%s', copyright='%s', "
257 "comment='%s', rating='%s'",
258 p_sys->psz_title, p_sys->psz_author, p_sys->psz_copyright,
259 p_sys->psz_comment, p_sys->psz_rating );
264 /*****************************************************************************
266 *****************************************************************************/
267 static void Close( vlc_object_t * p_this )
269 sout_mux_t *p_mux = (sout_mux_t*)p_this;
270 sout_mux_sys_t *p_sys = p_mux->p_sys;
274 msg_Dbg( p_mux, "Asf muxer closed" );
276 /* Flush last packet if any */
277 if( (out = asf_packet_flush( p_mux ) ) )
279 sout_AccessOutWrite( p_mux->p_access, out );
282 if( ( out = asf_stream_end_create( p_mux ) ) )
284 sout_AccessOutWrite( p_mux->p_access, out );
288 if( sout_AccessOutSeek( p_mux->p_access, 0 ) == VLC_SUCCESS )
290 out = asf_header_create( p_mux, false );
291 sout_AccessOutWrite( p_mux->p_access, out );
294 for( i = 0; i < p_sys->i_track; i++ )
296 free( p_sys->track[i].p_extra );
297 es_format_Clean( &p_sys->track[i].fmt );
300 free( p_sys->psz_title );
301 free( p_sys->psz_author );
302 free( p_sys->psz_copyright );
303 free( p_sys->psz_comment );
304 free( p_sys->psz_rating );
308 /*****************************************************************************
310 *****************************************************************************/
311 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
313 sout_mux_sys_t *p_sys = p_mux->p_sys;
319 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
320 pb_bool = (bool*)va_arg( args, bool * );
321 if( p_sys->b_asf_http ) *pb_bool = true;
322 else *pb_bool = false;
325 case MUX_GET_ADD_STREAM_WAIT:
326 pb_bool = (bool*)va_arg( args, bool * );
331 ppsz = (char**)va_arg( args, char ** );
332 if( p_sys->b_asf_http )
333 *ppsz = strdup( "video/x-ms-asf-stream" );
335 *ppsz = strdup( "video/x-ms-asf" );
343 /*****************************************************************************
345 *****************************************************************************/
346 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
348 sout_mux_sys_t *p_sys = p_mux->p_sys;
352 msg_Dbg( p_mux, "adding input" );
353 if( p_sys->i_track >= MAX_ASF_TRACKS )
355 msg_Dbg( p_mux, "cannot add this track (too much tracks)" );
359 tk = p_input->p_sys = &p_sys->track[p_sys->i_track];
360 tk->i_id = p_sys->i_track + 1;
361 tk->i_cat = p_input->p_fmt->i_cat;
363 tk->b_audio_correction = 0;
369 int i_blockalign = p_input->p_fmt->audio.i_blockalign;
370 int i_bitspersample = p_input->p_fmt->audio.i_bitspersample;
373 switch( p_input->p_fmt->i_codec )
375 case VLC_FOURCC( 'a', '5', '2', ' ' ):
376 tk->i_tag = WAVE_FORMAT_A52;
377 tk->psz_name = "A/52";
380 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
381 tk->i_tag = WAVE_FORMAT_AAC;
382 tk->psz_name = "MPEG-4 Audio";
385 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
387 tk->psz_name = "MPEG Audio Layer 3";
388 tk->i_tag = WAVE_FORMAT_MPEGLAYER3;
394 tk->psz_name = "MPEG Audio Layer 1/2";
395 tk->i_tag = WAVE_FORMAT_MPEG;
401 case VLC_FOURCC( 'w', 'm', 'a', '1' ):
402 tk->psz_name = "Windows Media Audio v1";
403 tk->i_tag = WAVE_FORMAT_WMA1;
404 tk->b_audio_correction = true;
406 case VLC_FOURCC( 'w', 'm', 'a', ' ' ):
407 case VLC_FOURCC( 'w', 'm', 'a', '2' ):
408 tk->psz_name= "Windows Media Audio (v2) 7, 8 and 9 Series";
409 tk->i_tag = WAVE_FORMAT_WMA2;
410 tk->b_audio_correction = true;
412 case VLC_FOURCC( 'w', 'm', 'a', 'p' ):
413 tk->psz_name = "Windows Media Audio 9 Professional";
414 tk->i_tag = WAVE_FORMAT_WMAP;
415 tk->b_audio_correction = true;
417 case VLC_FOURCC( 'w', 'm', 'a', 'l' ):
418 tk->psz_name = "Windows Media Audio 9 Lossless";
419 tk->i_tag = WAVE_FORMAT_WMAL;
420 tk->b_audio_correction = true;
423 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
424 tk->psz_name = "Raw audio 8bits";
425 tk->i_tag = WAVE_FORMAT_PCM;
426 i_blockalign= p_input->p_fmt->audio.i_channels;
429 case VLC_FOURCC( 's', '1', '6', 'l' ):
430 tk->psz_name = "Raw audio 16bits";
431 tk->i_tag = WAVE_FORMAT_PCM;
432 i_blockalign= 2 * p_input->p_fmt->audio.i_channels;
433 i_bitspersample = 16;
435 case VLC_FOURCC( 's', '2', '4', 'l' ):
436 tk->psz_name = "Raw audio 24bits";
437 tk->i_tag = WAVE_FORMAT_PCM;
438 i_blockalign= 3 * p_input->p_fmt->audio.i_channels;
439 i_bitspersample = 24;
441 case VLC_FOURCC( 's', '3', '2', 'l' ):
442 tk->psz_name = "Raw audio 32bits";
443 tk->i_tag = WAVE_FORMAT_PCM;
444 i_blockalign= 4 * p_input->p_fmt->audio.i_channels;
445 i_bitspersample = 32;
451 tk->i_extra = sizeof( WAVEFORMATEX ) +
452 p_input->p_fmt->i_extra + i_extra;
453 tk->p_extra = malloc( tk->i_extra );
456 bo_init( &bo, tk->p_extra, tk->i_extra );
457 bo_addle_u16( &bo, tk->i_tag );
458 bo_addle_u16( &bo, p_input->p_fmt->audio.i_channels );
459 bo_addle_u32( &bo, p_input->p_fmt->audio.i_rate );
460 bo_addle_u32( &bo, p_input->p_fmt->i_bitrate / 8 );
461 bo_addle_u16( &bo, i_blockalign );
462 tk->i_blockalign = i_blockalign;
463 bo_addle_u16( &bo, i_bitspersample );
464 if( p_input->p_fmt->i_extra > 0 )
466 bo_addle_u16( &bo, p_input->p_fmt->i_extra );
467 bo_add_mem ( &bo, p_input->p_fmt->p_extra,
468 p_input->p_fmt->i_extra );
472 bo_addle_u16( &bo, i_extra );
473 if( tk->i_tag == WAVE_FORMAT_MPEGLAYER3 )
475 msg_Dbg( p_mux, "adding mp3 header" );
476 bo_addle_u16( &bo, 1 ); /* wId */
477 bo_addle_u32( &bo, 2 ); /* fdwFlags */
478 bo_addle_u16( &bo, 1152 ); /* nBlockSize */
479 bo_addle_u16( &bo, 1 ); /* nFramesPerBlock */
480 bo_addle_u16( &bo, 1393 ); /* nCodecDelay */
482 else if( tk->i_tag == WAVE_FORMAT_MPEG )
484 msg_Dbg( p_mux, "adding mp2 header" );
485 bo_addle_u16( &bo, 2 ); /* fwHeadLayer */
486 bo_addle_u32( &bo, p_input->p_fmt->i_bitrate );
487 bo_addle_u16( &bo, p_input->p_fmt->audio.i_channels == 2 ?1:8 );
488 bo_addle_u16( &bo, 0 ); /* fwHeadModeExt */
489 bo_addle_u16( &bo, 1 ); /* wHeadEmphasis */
490 bo_addle_u16( &bo, 16 ); /* fwHeadFlags */
491 bo_addle_u32( &bo, 0 ); /* dwPTSLow */
492 bo_addle_u32( &bo, 0 ); /* dwPTSHigh */
496 if( p_input->p_fmt->i_bitrate > 24000 )
498 p_sys->i_bitrate += p_input->p_fmt->i_bitrate;
502 p_sys->i_bitrate += 128000;
504 if (p_sys->i_bitrate_override)
505 p_sys->i_bitrate = p_sys->i_bitrate_override;
510 tk->i_extra = 11 + sizeof( BITMAPINFOHEADER ) +
511 p_input->p_fmt->i_extra;
512 tk->p_extra = malloc( tk->i_extra );
515 bo_init( &bo, tk->p_extra, tk->i_extra );
516 bo_addle_u32( &bo, p_input->p_fmt->video.i_width );
517 bo_addle_u32( &bo, p_input->p_fmt->video.i_height );
518 bo_add_u8 ( &bo, 0x02 ); /* flags */
519 bo_addle_u16( &bo, sizeof( BITMAPINFOHEADER ) +
520 p_input->p_fmt->i_extra );
521 bo_addle_u32( &bo, sizeof( BITMAPINFOHEADER ) +
522 p_input->p_fmt->i_extra );
523 bo_addle_u32( &bo, p_input->p_fmt->video.i_width );
524 bo_addle_u32( &bo, p_input->p_fmt->video.i_height );
525 bo_addle_u16( &bo, 1 );
526 bo_addle_u16( &bo, 24 );
527 if( p_input->p_fmt->i_codec == VLC_FOURCC('m','p','4','v') )
529 tk->psz_name = "MPEG-4 Video";
530 tk->i_fourcc = VLC_FOURCC( 'M', 'P', '4', 'S' );
532 else if( p_input->p_fmt->i_codec == VLC_FOURCC('D','I','V','3') )
534 tk->psz_name = "MSMPEG-4 V3 Video";
535 tk->i_fourcc = VLC_FOURCC( 'M', 'P', '4', '3' );
537 else if( p_input->p_fmt->i_codec == VLC_FOURCC('D','I','V','2') )
539 tk->psz_name = "MSMPEG-4 V2 Video";
540 tk->i_fourcc = VLC_FOURCC( 'M', 'P', '4', '2' );
542 else if( p_input->p_fmt->i_codec == VLC_FOURCC('D','I','V','1') )
544 tk->psz_name = "MSMPEG-4 V1 Video";
545 tk->i_fourcc = VLC_FOURCC( 'M', 'P', 'G', '4' );
547 else if( p_input->p_fmt->i_codec == VLC_FOURCC('W','M','V','1') )
549 tk->psz_name = "Windows Media Video 7";
550 tk->i_fourcc = VLC_FOURCC( 'W', 'M', 'V', '1' );
552 else if( p_input->p_fmt->i_codec == VLC_FOURCC('W','M','V','2') )
554 tk->psz_name = "Windows Media Video 8";
555 tk->i_fourcc = VLC_FOURCC( 'W', 'M', 'V', '2' );
557 else if( p_input->p_fmt->i_codec == VLC_FOURCC('W','M','V','3') )
559 tk->psz_name = "Windows Media Video 9";
560 tk->i_fourcc = VLC_FOURCC( 'W', 'M', 'V', '3' );
562 else if( p_input->p_fmt->i_codec == VLC_FOURCC('h','2','6','4') )
564 tk->psz_name = "H.264/MPEG-4 AVC";
565 tk->i_fourcc = VLC_FOURCC('h','2','6','4');
569 tk->psz_name = _("Unknown Video");
570 tk->i_fourcc = p_input->p_fmt->i_codec;
572 bo_add_mem( &bo, (uint8_t*)&tk->i_fourcc, 4 );
573 bo_addle_u32( &bo, 0 );
574 bo_addle_u32( &bo, 0 );
575 bo_addle_u32( &bo, 0 );
576 bo_addle_u32( &bo, 0 );
577 bo_addle_u32( &bo, 0 );
578 if( p_input->p_fmt->i_extra > 0 )
580 bo_add_mem ( &bo, p_input->p_fmt->p_extra,
581 p_input->p_fmt->i_extra );
584 if( p_input->p_fmt->i_bitrate > 50000 )
586 p_sys->i_bitrate += p_input->p_fmt->i_bitrate;
590 p_sys->i_bitrate += 512000;
592 if (p_sys->i_bitrate_override)
593 p_sys->i_bitrate = p_sys->i_bitrate_override;
597 msg_Err(p_mux, "unhandled track type" );
601 es_format_Copy( &tk->fmt, p_input->p_fmt );
607 /*****************************************************************************
609 *****************************************************************************/
610 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
612 /* if bitrate ain't defined in commanline, reduce it when tracks are deleted
614 sout_mux_sys_t *p_sys = p_mux->p_sys;
615 asf_track_t *tk = p_input->p_sys;
616 if(!p_sys->i_bitrate_override)
618 if( tk->i_cat == AUDIO_ES )
620 if( p_input->p_fmt->i_bitrate > 24000 )
621 p_sys->i_bitrate -= p_input->p_fmt->i_bitrate;
623 p_sys->i_bitrate -= 128000;
625 else if(tk->i_cat == VIDEO_ES )
627 if( p_input->p_fmt->i_bitrate > 50000 )
628 p_sys->i_bitrate -= p_input->p_fmt->i_bitrate;
630 p_sys->i_bitrate -= 512000;
633 msg_Dbg( p_mux, "removing input" );
637 /*****************************************************************************
639 *****************************************************************************/
640 static int Mux( sout_mux_t *p_mux )
642 sout_mux_sys_t *p_sys = p_mux->p_sys;
644 if( p_sys->b_write_header )
646 block_t *out = asf_header_create( p_mux, true );
648 out->i_flags |= BLOCK_FLAG_HEADER;
649 sout_AccessOutWrite( p_mux->p_access, out );
651 p_sys->b_write_header = false;
656 sout_input_t *p_input;
663 if( MuxGetStream( p_mux, &i_stream, &i_dts ) )
665 /* not enough data */
669 if( p_sys->i_dts_first < 0 )
671 p_sys->i_dts_first = i_dts;
673 if( p_sys->i_dts_last < i_dts )
675 p_sys->i_dts_last = i_dts;
678 p_input = p_mux->pp_inputs[i_stream];
679 tk = (asf_track_t*)p_input->p_sys;
681 data = block_FifoGet( p_input->p_fifo );
683 if( ( pk = asf_packet_create( p_mux, tk, data ) ) )
685 sout_AccessOutWrite( p_mux->p_access, pk );
692 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
698 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
700 sout_input_t *p_input = p_mux->pp_inputs[i];
703 if( block_FifoCount( p_input->p_fifo ) <= 0 )
705 if( p_input->p_fmt->i_cat == AUDIO_ES ||
706 p_input->p_fmt->i_cat == VIDEO_ES )
708 /* We need that audio+video fifo contain at least 1 packet */
715 p_data = block_FifoShow( p_input->p_fifo );
716 if( i_stream == -1 || p_data->i_dts < i_dts )
719 i_dts = p_data->i_dts;
723 *pi_stream = i_stream;
729 /****************************************************************************
730 * Asf header construction
731 ****************************************************************************/
733 /****************************************************************************
735 ****************************************************************************/
736 static void bo_init( bo_t *p_bo, uint8_t *p_buffer, int i_size )
738 p_bo->i_buffer_size = i_size;
740 p_bo->p_buffer = p_buffer;
742 static void bo_add_u8( bo_t *p_bo, uint8_t i )
744 if( p_bo->i_buffer < p_bo->i_buffer_size )
746 p_bo->p_buffer[p_bo->i_buffer] = i;
750 static void bo_addle_u16( bo_t *p_bo, uint16_t i )
752 bo_add_u8( p_bo, i &0xff );
753 bo_add_u8( p_bo, ( ( i >> 8) &0xff ) );
755 static void bo_addle_u32( bo_t *p_bo, uint32_t i )
757 bo_addle_u16( p_bo, i &0xffff );
758 bo_addle_u16( p_bo, ( ( i >> 16) &0xffff ) );
760 static void bo_addle_u64( bo_t *p_bo, uint64_t i )
762 bo_addle_u32( p_bo, i &0xffffffff );
763 bo_addle_u32( p_bo, ( ( i >> 32) &0xffffffff ) );
766 static void bo_add_mem( bo_t *p_bo, uint8_t *p_mem, int i_size )
768 int i_copy = __MIN( i_size, p_bo->i_buffer_size - p_bo->i_buffer );
772 memcpy( &p_bo->p_buffer[p_bo->i_buffer], p_mem, i_copy );
774 p_bo->i_buffer += i_size;
777 static void bo_addle_str16( bo_t *bo, const char *str )
779 bo_addle_u16( bo, strlen( str ) + 1 );
782 uint16_t c = (uint8_t)*str++;
783 bo_addle_u16( bo, c );
784 if( c == '\0' ) break;
788 static void bo_addle_str16_nosize( bo_t *bo, const char *str )
792 uint16_t c = (uint8_t)*str++;
793 bo_addle_u16( bo, c );
794 if( c == '\0' ) break;
798 /****************************************************************************
800 ****************************************************************************/
801 static void bo_add_guid( bo_t *p_bo, const guid_t *id )
804 bo_addle_u32( p_bo, id->Data1 );
805 bo_addle_u16( p_bo, id->Data2 );
806 bo_addle_u16( p_bo, id->Data3 );
807 for( i = 0; i < 8; i++ )
809 bo_add_u8( p_bo, id->Data4[i] );
813 static const guid_t asf_object_header_guid =
814 {0x75B22630, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
815 static const guid_t asf_object_data_guid =
816 {0x75B22636, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
817 static const guid_t asf_object_file_properties_guid =
818 {0x8cabdca1, 0xa947, 0x11cf, {0x8e, 0xe4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
819 static const guid_t asf_object_stream_properties_guid =
820 {0xB7DC0791, 0xA9B7, 0x11CF, {0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
821 static const guid_t asf_object_header_extension_guid =
822 {0x5FBF03B5, 0xA92E, 0x11CF, {0x8E, 0xE3, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
823 static const guid_t asf_object_stream_type_audio =
824 {0xF8699E40, 0x5B4D, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
825 static const guid_t asf_object_stream_type_video =
826 {0xbc19efc0, 0x5B4D, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
827 static const guid_t asf_guid_audio_conceal_none =
828 {0x20FB5700, 0x5B55, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
829 static const guid_t asf_guid_audio_conceal_spread =
830 {0xBFC3CD50, 0x618F, 0x11CF, {0x8B, 0xB2, 0x00, 0xAA, 0x00, 0xB4, 0xE2, 0x20}};
831 static const guid_t asf_guid_video_conceal_none =
832 {0x20FB5700, 0x5B55, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
833 static const guid_t asf_guid_reserved_1 =
834 {0xABD3D211, 0xA9BA, 0x11cf, {0x8E, 0xE6, 0x00, 0xC0, 0x0C ,0x20, 0x53, 0x65}};
835 static const guid_t asf_object_codec_list_guid =
836 {0x86D15240, 0x311D, 0x11D0, {0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6}};
837 static const guid_t asf_object_codec_list_reserved_guid =
838 {0x86D15241, 0x311D, 0x11D0, {0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6}};
839 static const guid_t asf_object_content_description_guid =
840 {0x75B22633, 0x668E, 0x11CF, {0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c}};
841 static const guid_t asf_object_index_guid =
842 {0x33000890, 0xE5B1, 0x11CF, {0x89, 0xF4, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xCB}};
843 static const guid_t asf_object_metadata_guid =
844 {0xC5F8CBEA, 0x5BAF, 0x4877, {0x84, 0x67, 0xAA, 0x8C, 0x44, 0xFA, 0x4C, 0xCA}};
846 /****************************************************************************
848 ****************************************************************************/
849 static void asf_chunk_add( bo_t *bo,
850 int i_type, int i_len, int i_flags, int i_seq )
852 bo_addle_u16( bo, i_type );
853 bo_addle_u16( bo, i_len + 8 );
854 bo_addle_u32( bo, i_seq );
855 bo_addle_u16( bo, i_flags );
856 bo_addle_u16( bo, i_len + 8 );
859 static block_t *asf_header_create( sout_mux_t *p_mux, bool b_broadcast )
861 sout_mux_sys_t *p_sys = p_mux->p_sys;
863 mtime_t i_duration = 0;
864 int i_size, i_header_ext_size, i;
865 int i_ci_size, i_cm_size = 0, i_cd_size = 0;
869 msg_Dbg( p_mux, "Asf muxer creating header" );
871 if( p_sys->i_dts_first > 0 )
873 i_duration = p_sys->i_dts_last - p_sys->i_dts_first;
874 if( i_duration < 0 ) i_duration = 0;
877 /* calculate header size */
880 for( i = 0; i < p_sys->i_track; i++ )
882 i_size += 78 + p_sys->track[i].i_extra;
883 i_ci_size += 8 + 2 * strlen( p_sys->track[i].psz_name );
884 if( p_sys->track[i].i_cat == AUDIO_ES ) i_ci_size += 4;
885 else if( p_sys->track[i].i_cat == VIDEO_ES ) i_ci_size += 6;
887 /* Error correction data field */
888 if( p_sys->track[i].b_audio_correction ) i_size += 8;
891 /* size of the content description object */
892 if( *p_sys->psz_title || *p_sys->psz_author || *p_sys->psz_copyright ||
893 *p_sys->psz_comment || *p_sys->psz_rating )
895 i_cd_size = 34 + 2 * ( strlen( p_sys->psz_title ) + 1 +
896 strlen( p_sys->psz_author ) + 1 +
897 strlen( p_sys->psz_copyright ) + 1 +
898 strlen( p_sys->psz_comment ) + 1 +
899 strlen( p_sys->psz_rating ) + 1 );
902 /* size of the metadata object */
903 for( i = 0; i < p_sys->i_track; i++ )
905 if( p_sys->track[i].i_cat == VIDEO_ES )
907 i_cm_size = 26 + 2 * (16 + 2 * sizeof("AspectRatio?"));
912 i_header_ext_size = i_cm_size ? i_cm_size + 46 : 0;
913 i_size += i_ci_size + i_cd_size + i_header_ext_size ;
915 if( p_sys->b_asf_http )
917 out = block_New( p_mux, i_size + 50 + 12 );
918 bo_init( &bo, out->p_buffer, i_size + 50 + 12 );
919 asf_chunk_add( &bo, 0x4824, i_size + 50, 0xc00, p_sys->i_seq++ );
923 out = block_New( p_mux, i_size + 50 );
924 bo_init( &bo, out->p_buffer, i_size + 50 );
928 bo_add_guid ( &bo, &asf_object_header_guid );
929 bo_addle_u64( &bo, i_size );
930 bo_addle_u32( &bo, 2 + p_sys->i_track +
931 (i_cd_size ? 1 : 0) + (i_cm_size ? 1 : 0) );
932 bo_add_u8 ( &bo, 1 );
933 bo_add_u8 ( &bo, 2 );
937 /* file properties */
938 bo_add_guid ( &bo, &asf_object_file_properties_guid );
939 bo_addle_u64( &bo, 104 );
940 bo_add_guid ( &bo, &p_sys->fid );
941 bo_addle_u64( &bo, i_size + 50 + p_sys->i_packet_count *
942 p_sys->i_packet_size ); /* file size */
943 bo_addle_u64( &bo, 0 ); /* creation date */
944 bo_addle_u64( &bo, b_broadcast ? 0xffffffffLL : p_sys->i_packet_count );
945 bo_addle_u64( &bo, i_duration * 10 ); /* play duration (100ns) */
946 bo_addle_u64( &bo, i_duration * 10 ); /* send duration (100ns) */
947 bo_addle_u64( &bo, p_sys->i_preroll_time ); /* preroll duration (ms) */
948 bo_addle_u32( &bo, b_broadcast ? 0x01 : 0x02 /* seekable */ ); /* flags */
949 bo_addle_u32( &bo, p_sys->i_packet_size ); /* packet size min */
950 bo_addle_u32( &bo, p_sys->i_packet_size ); /* packet size max */
951 bo_addle_u32( &bo, p_sys->i_bitrate ); /* maxbitrate */
953 /* header extension */
954 if( i_header_ext_size )
956 bo_add_guid ( &bo, &asf_object_header_extension_guid );
957 bo_addle_u64( &bo, i_header_ext_size );
958 bo_add_guid ( &bo, &asf_guid_reserved_1 );
959 bo_addle_u16( &bo, 6 );
960 bo_addle_u32( &bo, i_header_ext_size - 46 );
963 /* metadata object (part of header extension) */
966 int64_t i_num, i_den;
967 unsigned int i_dst_num, i_dst_den;
969 for( i = 0; i < p_sys->i_track; i++ )
970 if( p_sys->track[i].i_cat == VIDEO_ES ) break;
972 i_num = p_sys->track[i].fmt.video.i_aspect *
973 (int64_t)p_sys->track[i].fmt.video.i_height;
974 i_den = VOUT_ASPECT_FACTOR * p_sys->track[i].fmt.video.i_width;
975 vlc_ureduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
977 msg_Dbg( p_mux, "pixel aspect-ratio: %i/%i", i_dst_num, i_dst_den );
979 bo_add_guid ( &bo, &asf_object_metadata_guid );
980 bo_addle_u64( &bo, i_cm_size );
981 bo_addle_u16( &bo, 2 ); /* description records count */
982 /* 1st description record */
983 bo_addle_u16( &bo, 0 ); /* reserved */
984 bo_addle_u16( &bo, i + 1 ); /* stream number (0 for the whole file) */
985 bo_addle_u16( &bo, 2 * sizeof("AspectRatioX") ); /* name length */
986 bo_addle_u16( &bo, 0x3 /* DWORD */ ); /* data type */
987 bo_addle_u32( &bo, 4 ); /* data length */
988 bo_addle_str16_nosize( &bo, "AspectRatioX" );
989 bo_addle_u32( &bo, i_dst_num ); /* data */
990 /* 2nd description record */
991 bo_addle_u16( &bo, 0 ); /* reserved */
992 bo_addle_u16( &bo, i + 1 ); /* stream number (0 for the whole file) */
993 bo_addle_u16( &bo, 2 * sizeof("AspectRatioY") ); /* name length */
994 bo_addle_u16( &bo, 0x3 /* DWORD */ ); /* data type */
995 bo_addle_u32( &bo, 4 ); /* data length */
996 bo_addle_str16_nosize( &bo, "AspectRatioY" );
997 bo_addle_u32( &bo, i_dst_den ); /* data */
1000 /* content description header */
1003 bo_add_guid ( &bo, &asf_object_content_description_guid );
1004 bo_addle_u64( &bo, i_cd_size );
1005 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_title ) + 2 );
1006 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_author ) + 2 );
1007 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_copyright ) + 2 );
1008 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_comment ) + 2 );
1009 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_rating ) + 2 );
1011 bo_addle_str16_nosize( &bo, p_sys->psz_title );
1012 bo_addle_str16_nosize( &bo, p_sys->psz_author );
1013 bo_addle_str16_nosize( &bo, p_sys->psz_copyright );
1014 bo_addle_str16_nosize( &bo, p_sys->psz_comment );
1015 bo_addle_str16_nosize( &bo, p_sys->psz_rating );
1018 /* stream properties */
1019 for( i = 0; i < p_sys->i_track; i++ )
1021 tk = &p_sys->track[i];
1023 bo_add_guid ( &bo, &asf_object_stream_properties_guid );
1024 bo_addle_u64( &bo, 78 + tk->i_extra + (tk->b_audio_correction ? 8:0) );
1026 if( tk->i_cat == AUDIO_ES )
1028 bo_add_guid( &bo, &asf_object_stream_type_audio );
1029 if( tk->b_audio_correction )
1030 bo_add_guid( &bo, &asf_guid_audio_conceal_spread );
1032 bo_add_guid( &bo, &asf_guid_audio_conceal_none );
1034 else if( tk->i_cat == VIDEO_ES )
1036 bo_add_guid( &bo, &asf_object_stream_type_video );
1037 bo_add_guid( &bo, &asf_guid_video_conceal_none );
1039 bo_addle_u64( &bo, 0 ); /* time offset */
1040 bo_addle_u32( &bo, tk->i_extra );
1041 /* correction data length */
1042 bo_addle_u32( &bo, tk->b_audio_correction ? 8 : 0 );
1043 bo_addle_u16( &bo, tk->i_id ); /* stream number */
1044 bo_addle_u32( &bo, 0 );
1045 bo_add_mem ( &bo, tk->p_extra, tk->i_extra );
1047 /* Error correction data field */
1048 if( tk->b_audio_correction )
1050 bo_add_u8( &bo, 0x1 ); /* span */
1051 bo_addle_u16( &bo, tk->i_blockalign ); /* virtual packet length */
1052 bo_addle_u16( &bo, tk->i_blockalign ); /* virtual chunck length */
1053 bo_addle_u16( &bo, 1 ); /* silence length */
1054 bo_add_u8( &bo, 0x0 ); /* data */
1059 bo_add_guid ( &bo, &asf_object_codec_list_guid );
1060 bo_addle_u64( &bo, i_ci_size );
1061 bo_add_guid ( &bo, &asf_object_codec_list_reserved_guid );
1062 bo_addle_u32( &bo, p_sys->i_track );
1063 for( i = 0; i < p_sys->i_track; i++ )
1065 tk = &p_sys->track[i];
1067 if( tk->i_cat == VIDEO_ES ) bo_addle_u16( &bo, 1 /* video */ );
1068 else if( tk->i_cat == AUDIO_ES ) bo_addle_u16( &bo, 2 /* audio */ );
1069 else bo_addle_u16( &bo, 0xFFFF /* unknown */ );
1071 bo_addle_str16( &bo, tk->psz_name );
1072 bo_addle_u16( &bo, 0 );
1073 if( tk->i_cat == AUDIO_ES )
1075 bo_addle_u16( &bo, 2 );
1076 bo_addle_u16( &bo, tk->i_tag );
1078 else if( tk->i_cat == VIDEO_ES )
1080 bo_addle_u16( &bo, 4 );
1081 bo_add_mem ( &bo, (uint8_t*)&tk->i_fourcc, 4 );
1086 bo_add_guid ( &bo, &asf_object_data_guid );
1087 bo_addle_u64( &bo, 50 + p_sys->i_packet_count * p_sys->i_packet_size );
1088 bo_add_guid ( &bo, &p_sys->fid );
1089 bo_addle_u64( &bo, p_sys->i_packet_count );
1090 bo_addle_u16( &bo, 0x101 );
1095 /****************************************************************************
1097 ****************************************************************************/
1098 static block_t *asf_packet_flush( sout_mux_t *p_mux )
1100 sout_mux_sys_t *p_sys = p_mux->p_sys;
1101 int i_pad, i_preheader = p_sys->b_asf_http ? 12 : 0;
1105 if( !p_sys->pk ) return 0;
1107 i_pad = p_sys->i_packet_size - p_sys->i_pk_used;
1108 memset( p_sys->pk->p_buffer + p_sys->i_pk_used, 0, i_pad );
1110 bo_init( &bo, p_sys->pk->p_buffer, 14 + i_preheader );
1112 if( p_sys->b_asf_http )
1113 asf_chunk_add( &bo, 0x4424, p_sys->i_packet_size, 0x0, p_sys->i_seq++);
1115 bo_add_u8 ( &bo, 0x82 );
1116 bo_addle_u16( &bo, 0 );
1117 bo_add_u8( &bo, 0x11 );
1118 bo_add_u8( &bo, 0x5d );
1119 bo_addle_u16( &bo, i_pad );
1120 bo_addle_u32( &bo, (p_sys->i_pk_dts - p_sys->i_dts_first) / 1000 +
1121 p_sys->i_preroll_time );
1122 bo_addle_u16( &bo, 0 /* data->i_length */ );
1123 bo_add_u8( &bo, 0x80 | p_sys->i_pk_frame );
1128 p_sys->i_packet_count++;
1133 static block_t *asf_packet_create( sout_mux_t *p_mux,
1134 asf_track_t *tk, block_t *data )
1136 sout_mux_sys_t *p_sys = p_mux->p_sys;
1138 int i_data = data->i_buffer;
1140 uint8_t *p_data= data->p_buffer;
1141 block_t *first = NULL, **last = &first;
1142 int i_preheader = p_sys->b_asf_http ? 12 : 0;
1144 while( i_pos < i_data )
1149 if( p_sys->pk == NULL )
1151 p_sys->pk = block_New( p_mux, p_sys->i_packet_size + i_preheader );
1152 /* reserve 14 bytes for the packet header */
1153 p_sys->i_pk_used = 14 + i_preheader;
1154 p_sys->i_pk_frame = 0;
1155 p_sys->i_pk_dts = data->i_dts;
1158 bo_init( &bo, &p_sys->pk->p_buffer[p_sys->i_pk_used],
1159 p_sys->i_packet_size - p_sys->i_pk_used );
1161 /* add payload (header size = 17) */
1162 i_payload = __MIN( i_data - i_pos,
1163 p_sys->i_packet_size - p_sys->i_pk_used - 17 );
1165 if( tk->b_audio_correction && p_sys->i_pk_frame && i_payload < i_data )
1167 /* Don't know why yet but WMP doesn't like splitted WMA packets */
1168 *last = asf_packet_flush( p_mux );
1169 last = &(*last)->p_next;
1173 bo_add_u8 ( &bo, !(data->i_flags & BLOCK_FLAG_TYPE_P ||
1174 data->i_flags & BLOCK_FLAG_TYPE_B) ?
1175 0x80 | tk->i_id : tk->i_id );
1176 bo_add_u8 ( &bo, tk->i_sequence );
1177 bo_addle_u32( &bo, i_pos );
1178 bo_add_u8 ( &bo, 0x08 ); /* flags */
1179 bo_addle_u32( &bo, i_data );
1180 bo_addle_u32( &bo, (data->i_dts - p_sys->i_dts_first) / 1000 +
1181 p_sys->i_preroll_time );
1182 bo_addle_u16( &bo, i_payload );
1183 bo_add_mem ( &bo, &p_data[i_pos], i_payload );
1185 p_sys->i_pk_used += 17 + i_payload;
1187 p_sys->i_pk_frame++;
1189 if( p_sys->i_pk_used + 17 >= p_sys->i_packet_size )
1191 /* Not enough data for another payload, flush the packet */
1192 *last = asf_packet_flush( p_mux );
1193 last = &(*last)->p_next;
1198 block_Release( data );
1203 static block_t *asf_stream_end_create( sout_mux_t *p_mux )
1205 sout_mux_sys_t *p_sys = p_mux->p_sys;
1207 block_t *out = NULL;
1210 if( p_sys->b_asf_http )
1212 out = block_New( p_mux, 12 );
1213 bo_init( &bo, out->p_buffer, 12 );
1214 asf_chunk_add( &bo, 0x4524, 0, 0x00, p_sys->i_seq++ );
1219 out = block_New( p_mux, 56 );
1220 bo_init( &bo, out->p_buffer, 56 );
1221 bo_add_guid ( &bo, &asf_object_index_guid );
1222 bo_addle_u64( &bo, 56 );
1223 bo_add_guid ( &bo, &p_sys->fid );
1224 bo_addle_u64( &bo, 10000000 );
1225 bo_addle_u32( &bo, 5 );
1226 bo_addle_u32( &bo, 0 );