demux: mp4: add missing coreaudio enumerated mappings
[vlc.git] / modules / mux / asf.c
blob09de797445a63309e4766a3c1c2c2d703d382650
1 /*****************************************************************************
2 * asf.c: asf muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2003-2004, 2006 VLC authors and VideoLAN
5 * $Id$
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <assert.h>
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_sout.h>
38 #include <vlc_block.h>
39 #include <vlc_codecs.h>
40 #include <vlc_arrays.h>
41 #include <vlc_rand.h>
43 #include "../demux/asf/libasf_guid.h"
45 #define MAX_ASF_TRACKS 128
46 #define ASF_DATA_PACKET_SIZE 4096 // deprecated -- added sout-asf-packet-size
48 /*****************************************************************************
49 * Module descriptor
50 *****************************************************************************/
51 static int Open ( vlc_object_t * );
52 static void Close ( vlc_object_t * );
54 #define SOUT_CFG_PREFIX "sout-asf-"
56 #define TITLE_TEXT N_("Title")
57 #define TITLE_LONGTEXT N_("Title to put in ASF comments." )
58 #define AUTHOR_TEXT N_("Author")
59 #define AUTHOR_LONGTEXT N_("Author to put in ASF comments." )
60 #define COPYRIGHT_TEXT N_("Copyright")
61 #define COPYRIGHT_LONGTEXT N_("Copyright string to put in ASF comments." )
62 #define COMMENT_TEXT N_("Comment")
63 #define COMMENT_LONGTEXT N_("Comment to put in ASF comments." )
64 #define RATING_TEXT N_("Rating")
65 #define RATING_LONGTEXT N_("\"Rating\" to put in ASF comments." )
66 #define PACKETSIZE_TEXT N_("Packet Size")
67 #define PACKETSIZE_LONGTEXT N_("ASF packet size -- default is 4096 bytes")
68 #define BITRATE_TEXT N_("Bitrate override")
69 #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")
72 vlc_module_begin ()
73 set_description( N_("ASF muxer") )
74 set_category( CAT_SOUT )
75 set_subcategory( SUBCAT_SOUT_MUX )
76 set_shortname( "ASF" )
78 set_capability( "sout mux", 5 )
79 add_shortcut( "asf", "asfh" )
80 set_callbacks( Open, Close )
82 add_string( SOUT_CFG_PREFIX "title", "", TITLE_TEXT, TITLE_LONGTEXT,
83 true )
84 add_string( SOUT_CFG_PREFIX "author", "", AUTHOR_TEXT,
85 AUTHOR_LONGTEXT, true )
86 add_string( SOUT_CFG_PREFIX "copyright","", COPYRIGHT_TEXT,
87 COPYRIGHT_LONGTEXT, true )
88 add_string( SOUT_CFG_PREFIX "comment", "", COMMENT_TEXT,
89 COMMENT_LONGTEXT, true )
90 add_string( SOUT_CFG_PREFIX "rating", "", RATING_TEXT,
91 RATING_LONGTEXT, true )
92 add_integer( SOUT_CFG_PREFIX "packet-size", 4096, PACKETSIZE_TEXT,
93 PACKETSIZE_LONGTEXT, true )
94 add_integer( SOUT_CFG_PREFIX "bitrate-override", 0, BITRATE_TEXT,
95 BITRATE_LONGTEXT, true )
97 vlc_module_end ()
99 /*****************************************************************************
100 * Locales prototypes
101 *****************************************************************************/
102 static const char *const ppsz_sout_options[] = {
103 "title", "author", "copyright", "comment", "rating", NULL
106 static int Control ( sout_mux_t *, int, va_list );
107 static int AddStream( sout_mux_t *, sout_input_t * );
108 static void DelStream( sout_mux_t *, sout_input_t * );
109 static int Mux ( sout_mux_t * );
111 typedef struct
113 int i_id;
114 enum es_format_category_e i_cat;
116 /* codec information */
117 uint16_t i_tag; /* for audio */
118 vlc_fourcc_t i_fourcc; /* for video */
119 const char *psz_name; /* codec name */
120 int i_blockalign; /* for audio only */
121 bool b_audio_correction;
123 int i_sequence;
125 int i_extra;
126 uint8_t *p_extra;
127 bool b_extended;
129 es_format_t fmt;
131 } asf_track_t;
133 typedef struct
135 vlc_guid_t fid; /* file id */
136 int i_packet_size;
137 int64_t i_packet_count;
138 vlc_tick_t i_dts_first;
139 vlc_tick_t i_dts_last;
140 int64_t i_preroll_time; /* in milliseconds */
141 int64_t i_bitrate;
142 int64_t i_bitrate_override;
144 vlc_array_t tracks;
146 bool b_write_header;
148 block_t *pk;
149 int i_pk_used;
150 int i_pk_frame;
151 vlc_tick_t i_pk_dts;
153 bool b_asf_http;
154 int i_seq;
156 /* meta data */
157 char *psz_title;
158 char *psz_author;
159 char *psz_copyright;
160 char *psz_comment;
161 char *psz_rating;
162 } sout_mux_sys_t;
164 static block_t *asf_header_create( sout_mux_t *, bool );
165 static block_t *asf_packet_create( sout_mux_t *, asf_track_t *, block_t * );
166 static block_t *asf_stream_end_create( sout_mux_t *);
167 static block_t *asf_packet_flush( sout_mux_t * );
169 typedef struct
171 int i_buffer_size;
172 int i_buffer;
173 uint8_t *p_buffer;
175 } bo_t;
177 static void bo_init ( bo_t *, uint8_t *, int );
178 static void bo_add_u8 ( bo_t *, uint8_t );
179 static void bo_addle_u16( bo_t *, uint16_t );
180 static void bo_addle_u32( bo_t *, uint32_t );
181 static void bo_addle_u64( bo_t *, uint64_t );
182 static void bo_add_mem ( bo_t *, uint8_t *, int );
184 static void bo_addle_str16( bo_t *, const char * );
186 /*****************************************************************************
187 * Open:
188 *****************************************************************************/
189 static int Open( vlc_object_t *p_this )
191 sout_mux_t *p_mux = (sout_mux_t*)p_this;
192 sout_mux_sys_t *p_sys;
194 msg_Dbg( p_mux, "asf muxer opened" );
195 config_ChainParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
197 p_mux->pf_control = Control;
198 p_mux->pf_addstream = AddStream;
199 p_mux->pf_delstream = DelStream;
200 p_mux->pf_mux = Mux;
202 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
203 if( !p_sys )
204 return VLC_ENOMEM;
205 p_sys->b_asf_http = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "asfh" );
206 if( p_sys->b_asf_http )
208 msg_Dbg( p_mux, "creating asf stream to be used with mmsh" );
210 p_sys->pk = NULL;
211 p_sys->i_pk_used = 0;
212 p_sys->i_pk_frame = 0;
213 p_sys->i_dts_first =
214 p_sys->i_dts_last = VLC_TICK_INVALID;
215 p_sys->i_preroll_time = 2000;
216 p_sys->i_bitrate = 0;
217 p_sys->i_bitrate_override = 0;
218 p_sys->i_seq = 0;
219 vlc_array_init( &p_sys->tracks );
221 p_sys->b_write_header = true;
222 p_sys->i_packet_size = var_InheritInteger( p_mux, "sout-asf-packet-size" );
223 p_sys->i_bitrate_override = var_InheritInteger( p_mux, "sout-asf-bitrate-override" );
224 msg_Dbg( p_mux, "Packet size %d", p_sys->i_packet_size);
225 if (p_sys->i_bitrate_override)
226 msg_Dbg( p_mux, "Bitrate override %"PRId64, p_sys->i_bitrate_override);
227 p_sys->i_packet_count= 0;
229 /* Generate a random fid */
230 p_sys->fid.Data1 = 0xbabac001;
231 vlc_rand_bytes(&p_sys->fid.Data2, sizeof(p_sys->fid.Data2));
232 vlc_rand_bytes(&p_sys->fid.Data3, sizeof(p_sys->fid.Data3));
233 vlc_rand_bytes(p_sys->fid.Data4, sizeof(p_sys->fid.Data4));
235 /* Meta data */
236 p_sys->psz_title = var_GetString( p_mux, SOUT_CFG_PREFIX "title" );
237 p_sys->psz_author = var_GetString( p_mux, SOUT_CFG_PREFIX "author" );
238 p_sys->psz_copyright = var_GetString( p_mux, SOUT_CFG_PREFIX "copyright" );
239 p_sys->psz_comment = var_GetString( p_mux, SOUT_CFG_PREFIX "comment" );
240 p_sys->psz_rating = var_GetString( p_mux, SOUT_CFG_PREFIX "rating" );
242 msg_Dbg( p_mux, "meta data: title='%s', author='%s', copyright='%s', "
243 "comment='%s', rating='%s'",
244 p_sys->psz_title, p_sys->psz_author, p_sys->psz_copyright,
245 p_sys->psz_comment, p_sys->psz_rating );
247 return VLC_SUCCESS;
250 /*****************************************************************************
251 * Close:
252 *****************************************************************************/
253 static void Close( vlc_object_t * p_this )
255 sout_mux_t *p_mux = (sout_mux_t*)p_this;
256 sout_mux_sys_t *p_sys = p_mux->p_sys;
257 block_t *out;
259 msg_Dbg( p_mux, "Asf muxer closed" );
261 /* Flush last packet if any */
262 if( (out = asf_packet_flush( p_mux ) ) )
264 sout_AccessOutWrite( p_mux->p_access, out );
267 if( ( out = asf_stream_end_create( p_mux ) ) )
269 sout_AccessOutWrite( p_mux->p_access, out );
272 /* rewrite header */
273 if( sout_AccessOutSeek( p_mux->p_access, 0 ) == VLC_SUCCESS )
275 out = asf_header_create( p_mux, false );
276 sout_AccessOutWrite( p_mux->p_access, out );
280 for( size_t i = 0; i < vlc_array_count( &p_sys->tracks ); i++ )
282 asf_track_t *track = vlc_array_item_at_index( &p_sys->tracks, i );
283 free( track->p_extra );
284 es_format_Clean( &track->fmt );
285 free( track );
288 vlc_array_clear( &p_sys->tracks );
289 free( p_sys->psz_title );
290 free( p_sys->psz_author );
291 free( p_sys->psz_copyright );
292 free( p_sys->psz_comment );
293 free( p_sys->psz_rating );
294 free( p_sys );
297 /*****************************************************************************
298 * Capability:
299 *****************************************************************************/
300 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
302 sout_mux_sys_t *p_sys = p_mux->p_sys;
303 bool *pb_bool;
304 char **ppsz;
306 switch( i_query )
308 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
309 pb_bool = va_arg( args, bool * );
310 if( p_sys->b_asf_http ) *pb_bool = true;
311 else *pb_bool = false;
312 return VLC_SUCCESS;
314 case MUX_GET_ADD_STREAM_WAIT:
315 pb_bool = va_arg( args, bool * );
316 *pb_bool = true;
317 return VLC_SUCCESS;
319 case MUX_GET_MIME:
320 ppsz = va_arg( args, char ** );
321 if( p_sys->b_asf_http )
322 *ppsz = strdup( "video/x-ms-asf-stream" );
323 else
324 *ppsz = strdup( "video/x-ms-asf" );
325 return VLC_SUCCESS;
327 default:
328 return VLC_EGENERIC;
332 /*****************************************************************************
333 * AddStream:
334 *****************************************************************************/
335 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
337 sout_mux_sys_t *p_sys = p_mux->p_sys;
338 asf_track_t *tk;
339 bo_t bo;
341 msg_Dbg( p_mux, "adding input" );
342 if( vlc_array_count( &p_sys->tracks ) >= MAX_ASF_TRACKS )
344 msg_Dbg( p_mux, "cannot add this track (too much tracks)" );
345 return VLC_EGENERIC;
348 tk = p_input->p_sys = malloc( sizeof( asf_track_t ) );
349 if( unlikely(p_input->p_sys == NULL) )
350 return VLC_ENOMEM;
351 memset( tk, 0, sizeof( *tk ) );
352 tk->i_cat = p_input->p_fmt->i_cat;
353 tk->i_sequence = 0;
354 tk->b_audio_correction = 0;
355 tk->b_extended = false;
357 switch( tk->i_cat )
359 case AUDIO_ES:
361 int i_blockalign = p_input->p_fmt->audio.i_blockalign;
362 int i_bitspersample = p_input->p_fmt->audio.i_bitspersample;
363 int i_extra = 0;
365 switch( p_input->p_fmt->i_codec )
367 case VLC_CODEC_A52:
368 tk->i_tag = WAVE_FORMAT_A52;
369 tk->psz_name = "A/52";
370 i_bitspersample = 0;
371 break;
372 case VLC_CODEC_MP4A:
373 tk->i_tag = WAVE_FORMAT_AAC;
374 tk->psz_name = "MPEG-4 Audio";
375 i_bitspersample = 0;
376 break;
377 case VLC_CODEC_MP3:
378 tk->psz_name = "MPEG Audio Layer 3";
379 tk->i_tag = WAVE_FORMAT_MPEGLAYER3;
380 i_bitspersample = 0;
381 i_blockalign = 1;
382 i_extra = 12;
383 break;
384 case VLC_CODEC_MPGA:
385 tk->psz_name = "MPEG Audio Layer 1/2";
386 tk->i_tag = WAVE_FORMAT_MPEG;
387 i_bitspersample = 0;
388 i_blockalign = 1;
389 i_extra = 22;
390 break;
391 case VLC_CODEC_WMA1:
392 tk->psz_name = "Windows Media Audio v1";
393 tk->i_tag = WAVE_FORMAT_WMA1;
394 tk->b_audio_correction = true;
395 break;
396 case VLC_CODEC_WMA2:
397 tk->psz_name= "Windows Media Audio (v2) 7, 8 and 9 Series";
398 tk->i_tag = WAVE_FORMAT_WMA2;
399 tk->b_audio_correction = true;
400 break;
401 case VLC_CODEC_WMAP:
402 tk->psz_name = "Windows Media Audio 9 Professional";
403 tk->i_tag = WAVE_FORMAT_WMAP;
404 tk->b_audio_correction = true;
405 break;
406 case VLC_CODEC_WMAL:
407 tk->psz_name = "Windows Media Audio 9 Lossless";
408 tk->i_tag = WAVE_FORMAT_WMAL;
409 tk->b_audio_correction = true;
410 break;
411 /* raw codec */
412 case VLC_CODEC_U8:
413 tk->psz_name = "Raw audio 8bits";
414 tk->i_tag = WAVE_FORMAT_PCM;
415 i_blockalign= p_input->p_fmt->audio.i_channels;
416 i_bitspersample = 8;
417 break;
418 case VLC_CODEC_S16L:
419 tk->psz_name = "Raw audio 16bits";
420 tk->i_tag = WAVE_FORMAT_PCM;
421 i_blockalign= 2 * p_input->p_fmt->audio.i_channels;
422 i_bitspersample = 16;
423 break;
424 case VLC_CODEC_S24L:
425 tk->psz_name = "Raw audio 24bits";
426 tk->i_tag = WAVE_FORMAT_PCM;
427 i_blockalign= 3 * p_input->p_fmt->audio.i_channels;
428 i_bitspersample = 24;
429 break;
430 case VLC_CODEC_S32L:
431 tk->psz_name = "Raw audio 32bits";
432 tk->i_tag = WAVE_FORMAT_PCM;
433 i_blockalign= 4 * p_input->p_fmt->audio.i_channels;
434 i_bitspersample = 32;
435 break;
436 default:
437 return VLC_EGENERIC;
440 tk->i_extra = sizeof( WAVEFORMATEX ) +
441 p_input->p_fmt->i_extra + i_extra;
442 tk->p_extra = malloc( tk->i_extra );
443 if( !tk->p_extra )
444 return VLC_ENOMEM;
445 bo_init( &bo, tk->p_extra, tk->i_extra );
446 bo_addle_u16( &bo, tk->i_tag );
447 bo_addle_u16( &bo, p_input->p_fmt->audio.i_channels );
448 bo_addle_u32( &bo, p_input->p_fmt->audio.i_rate );
449 bo_addle_u32( &bo, p_input->p_fmt->i_bitrate / 8 );
450 bo_addle_u16( &bo, i_blockalign );
451 tk->i_blockalign = i_blockalign;
452 bo_addle_u16( &bo, i_bitspersample );
453 if( p_input->p_fmt->i_extra > 0 )
455 bo_addle_u16( &bo, p_input->p_fmt->i_extra );
456 bo_add_mem ( &bo, p_input->p_fmt->p_extra,
457 p_input->p_fmt->i_extra );
459 else
461 bo_addle_u16( &bo, i_extra );
462 if( tk->i_tag == WAVE_FORMAT_MPEGLAYER3 )
464 msg_Dbg( p_mux, "adding mp3 header" );
465 bo_addle_u16( &bo, 1 ); /* wId */
466 bo_addle_u32( &bo, 2 ); /* fdwFlags */
467 bo_addle_u16( &bo, 1152 ); /* nBlockSize */
468 bo_addle_u16( &bo, 1 ); /* nFramesPerBlock */
469 bo_addle_u16( &bo, 1393 ); /* nCodecDelay */
471 else if( tk->i_tag == WAVE_FORMAT_MPEG )
473 msg_Dbg( p_mux, "adding mp2 header" );
474 bo_addle_u16( &bo, 2 ); /* fwHeadLayer */
475 bo_addle_u32( &bo, p_input->p_fmt->i_bitrate );
476 bo_addle_u16( &bo, p_input->p_fmt->audio.i_channels == 2 ?1:8 );
477 bo_addle_u16( &bo, 0 ); /* fwHeadModeExt */
478 bo_addle_u16( &bo, 1 ); /* wHeadEmphasis */
479 bo_addle_u16( &bo, 16 ); /* fwHeadFlags */
480 bo_addle_u32( &bo, 0 ); /* dwPTSLow */
481 bo_addle_u32( &bo, 0 ); /* dwPTSHigh */
485 if( p_input->p_fmt->i_bitrate > 24000 )
487 p_sys->i_bitrate += p_input->p_fmt->i_bitrate;
489 else
491 p_sys->i_bitrate += 128000;
493 if (p_sys->i_bitrate_override)
494 p_sys->i_bitrate = p_sys->i_bitrate_override;
495 break;
497 case VIDEO_ES:
499 const es_format_t *p_fmt = p_input->p_fmt;
500 uint8_t *p_codec_extra = NULL;
501 int i_codec_extra = 0;
503 if( p_fmt->i_codec == VLC_CODEC_MP4V )
505 tk->psz_name = "MPEG-4 Video";
506 tk->i_fourcc = VLC_FOURCC( 'M', 'P', '4', 'S' );
508 else if( p_fmt->i_codec == VLC_CODEC_DIV3 )
510 tk->psz_name = "MSMPEG-4 V3 Video";
511 tk->i_fourcc = VLC_FOURCC( 'M', 'P', '4', '3' );
513 else if( p_fmt->i_codec == VLC_CODEC_DIV2 )
515 tk->psz_name = "MSMPEG-4 V2 Video";
516 tk->i_fourcc = VLC_FOURCC( 'M', 'P', '4', '2' );
518 else if( p_fmt->i_codec == VLC_CODEC_DIV1 )
520 tk->psz_name = "MSMPEG-4 V1 Video";
521 tk->i_fourcc = VLC_FOURCC( 'M', 'P', 'G', '4' );
523 else if( p_fmt->i_codec == VLC_CODEC_WMV1 )
525 tk->psz_name = "Windows Media Video 7";
526 tk->i_fourcc = VLC_FOURCC( 'W', 'M', 'V', '1' );
528 else if( p_fmt->i_codec == VLC_CODEC_WMV2 )
530 tk->psz_name = "Windows Media Video 8";
531 tk->i_fourcc = VLC_FOURCC( 'W', 'M', 'V', '2' );
533 else if( p_fmt->i_codec == VLC_CODEC_WMV3 )
535 tk->psz_name = "Windows Media Video 9";
536 tk->i_fourcc = VLC_FOURCC( 'W', 'M', 'V', '3' );
537 tk->b_extended = true;
539 else if( p_fmt->i_codec == VLC_CODEC_VC1 )
541 tk->psz_name = "Windows Media Video 9 Advanced Profile";
542 tk->i_fourcc = VLC_FOURCC( 'W', 'V', 'C', '1' );
543 tk->b_extended = true;
545 if( p_fmt->i_extra > 0 )
547 p_codec_extra = malloc( 1 + p_fmt->i_extra );
548 if( p_codec_extra )
550 i_codec_extra = 1 + p_fmt->i_extra;
551 p_codec_extra[0] = 0x01;
552 memcpy( &p_codec_extra[1], p_fmt->p_extra, p_fmt->i_extra );
556 else if( p_fmt->i_codec == VLC_CODEC_H264 )
558 tk->psz_name = "H.264/MPEG-4 AVC";
559 tk->i_fourcc = VLC_FOURCC('h','2','6','4');
561 else
563 tk->psz_name = _("Unknown Video");
564 tk->i_fourcc = p_fmt->i_original_fourcc ? p_fmt->i_original_fourcc : p_fmt->i_codec;
566 if( !i_codec_extra && p_fmt->i_extra > 0 )
568 p_codec_extra = malloc( p_fmt->i_extra );
569 if( p_codec_extra )
571 i_codec_extra = p_fmt->i_extra;
572 memcpy( p_codec_extra, p_fmt->p_extra, p_fmt->i_extra );
576 tk->i_extra = 11 + sizeof( VLC_BITMAPINFOHEADER ) + i_codec_extra;
577 tk->p_extra = malloc( tk->i_extra );
578 if( !tk->p_extra )
580 free( p_codec_extra );
581 return VLC_ENOMEM;
583 bo_init( &bo, tk->p_extra, tk->i_extra );
584 bo_addle_u32( &bo, p_fmt->video.i_width );
585 bo_addle_u32( &bo, p_fmt->video.i_height );
586 bo_add_u8 ( &bo, 0x02 ); /* flags */
587 bo_addle_u16( &bo, sizeof( VLC_BITMAPINFOHEADER ) + i_codec_extra );
588 bo_addle_u32( &bo, sizeof( VLC_BITMAPINFOHEADER ) + i_codec_extra );
589 bo_addle_u32( &bo, p_fmt->video.i_width );
590 bo_addle_u32( &bo, p_fmt->video.i_height );
591 bo_addle_u16( &bo, 1 );
592 bo_addle_u16( &bo, 24 );
593 bo_add_mem( &bo, (uint8_t*)&tk->i_fourcc, 4 );
594 bo_addle_u32( &bo, 0 );
595 bo_addle_u32( &bo, 0 );
596 bo_addle_u32( &bo, 0 );
597 bo_addle_u32( &bo, 0 );
598 bo_addle_u32( &bo, 0 );
599 if( i_codec_extra > 0 )
601 bo_add_mem( &bo, p_codec_extra, i_codec_extra );
602 free( p_codec_extra );
605 if( p_fmt->i_bitrate > 50000 )
607 p_sys->i_bitrate += p_fmt->i_bitrate;
609 else
611 p_sys->i_bitrate += 512000;
613 if( p_sys->i_bitrate_override )
614 p_sys->i_bitrate = p_sys->i_bitrate_override;
615 break;
617 default:
618 msg_Err(p_mux, "unhandled track type" );
619 free( tk );
620 return VLC_EGENERIC;
623 if( vlc_array_append( &p_sys->tracks, tk ) )
625 free( tk->p_extra );
626 free( tk );
627 return VLC_EGENERIC;
630 es_format_Copy( &tk->fmt, p_input->p_fmt );
632 tk->i_id = vlc_array_index_of_item( &p_sys->tracks, tk ) + 1;
634 if( p_sys->b_asf_http )
635 p_sys->b_write_header = true;
637 return VLC_SUCCESS;
640 /*****************************************************************************
641 * DelStream:
642 *****************************************************************************/
643 static void DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
645 /* if bitrate ain't defined in commandline, reduce it when tracks are deleted
647 sout_mux_sys_t *p_sys = p_mux->p_sys;
648 asf_track_t *tk = p_input->p_sys;
649 msg_Dbg( p_mux, "removing input" );
650 if(!p_sys->i_bitrate_override)
652 if( tk->i_cat == AUDIO_ES )
654 if( p_input->p_fmt->i_bitrate > 24000 )
655 p_sys->i_bitrate -= p_input->p_fmt->i_bitrate;
656 else
657 p_sys->i_bitrate -= 128000;
659 else if(tk->i_cat == VIDEO_ES )
661 if( p_input->p_fmt->i_bitrate > 50000 )
662 p_sys->i_bitrate -= p_input->p_fmt->i_bitrate;
663 else
664 p_sys->i_bitrate -= 512000;
668 if( p_sys->b_asf_http )
670 vlc_array_remove( &p_sys->tracks, vlc_array_index_of_item( &p_sys->tracks, tk ) );
671 p_sys->b_write_header = true;
675 /*****************************************************************************
676 * Mux:
677 *****************************************************************************/
678 static int Mux( sout_mux_t *p_mux )
680 sout_mux_sys_t *p_sys = p_mux->p_sys;
682 if( p_sys->b_write_header )
684 block_t *out = asf_header_create( p_mux, true );
686 out->i_flags |= BLOCK_FLAG_HEADER;
687 sout_AccessOutWrite( p_mux->p_access, out );
689 p_sys->b_write_header = false;
692 for( ;; )
694 sout_input_t *p_input;
695 asf_track_t *tk;
696 vlc_tick_t i_dts;
697 block_t *data;
698 block_t *pk;
700 int i_stream = sout_MuxGetStream( p_mux, 1, &i_dts );
701 if( i_stream < 0 )
703 /* not enough data */
704 return VLC_SUCCESS;
707 if( p_sys->i_dts_first == VLC_TICK_INVALID )
709 p_sys->i_dts_first = i_dts;
711 if( p_sys->i_dts_last < i_dts )
713 p_sys->i_dts_last = i_dts;
716 p_input = p_mux->pp_inputs[i_stream];
717 tk = (asf_track_t*)p_input->p_sys;
719 data = block_FifoGet( p_input->p_fifo );
721 /* Convert VC1 to ASF special format */
722 if( tk->i_fourcc == VLC_FOURCC( 'W', 'V', 'C', '1' ) )
724 while( data->i_buffer >= 4 &&
725 ( data->p_buffer[0] != 0x00 || data->p_buffer[1] != 0x00 ||
726 data->p_buffer[2] != 0x01 ||
727 ( data->p_buffer[3] != 0x0D && data->p_buffer[3] != 0x0C ) ) )
729 data->i_buffer--;
730 data->p_buffer++;
732 if( data->i_buffer >= 4 )
734 data->i_buffer -= 4;
735 data->p_buffer += 4;
739 if( ( pk = asf_packet_create( p_mux, tk, data ) ) )
741 sout_AccessOutWrite( p_mux->p_access, pk );
745 return VLC_SUCCESS;
748 /****************************************************************************
749 * Asf header construction
750 ****************************************************************************/
752 /****************************************************************************
753 * Buffer out
754 ****************************************************************************/
755 static void bo_init( bo_t *p_bo, uint8_t *p_buffer, int i_size )
757 p_bo->i_buffer_size = i_size;
758 p_bo->i_buffer = 0;
759 p_bo->p_buffer = p_buffer;
761 static void bo_add_u8( bo_t *p_bo, uint8_t i )
763 if( p_bo->i_buffer < p_bo->i_buffer_size )
765 p_bo->p_buffer[p_bo->i_buffer] = i;
767 p_bo->i_buffer++;
769 static void bo_addle_u16( bo_t *p_bo, uint16_t i )
771 bo_add_u8( p_bo, i &0xff );
772 bo_add_u8( p_bo, ( ( i >> 8) &0xff ) );
774 static void bo_addle_u32( bo_t *p_bo, uint32_t i )
776 bo_addle_u16( p_bo, i &0xffff );
777 bo_addle_u16( p_bo, ( ( i >> 16) &0xffff ) );
779 static void bo_addle_u64( bo_t *p_bo, uint64_t i )
781 bo_addle_u32( p_bo, i &0xffffffff );
782 bo_addle_u32( p_bo, ( ( i >> 32) &0xffffffff ) );
785 static void bo_add_mem( bo_t *p_bo, uint8_t *p_mem, int i_size )
787 int i_copy = __MIN( i_size, p_bo->i_buffer_size - p_bo->i_buffer );
789 if( i_copy > 0 )
791 memcpy( &p_bo->p_buffer[p_bo->i_buffer], p_mem, i_copy );
793 p_bo->i_buffer += i_size;
796 static void bo_addle_str16( bo_t *bo, const char *str )
798 bo_addle_u16( bo, strlen( str ) + 1 );
799 for( ;; )
801 uint16_t c = (uint8_t)*str++;
802 bo_addle_u16( bo, c );
803 if( c == '\0' ) break;
807 static void bo_addle_str16_nosize( bo_t *bo, const char *str )
809 for( ;; )
811 uint16_t c = (uint8_t)*str++;
812 bo_addle_u16( bo, c );
813 if( c == '\0' ) break;
817 /****************************************************************************
818 * GUID definitions
819 ****************************************************************************/
820 static void bo_add_guid( bo_t *p_bo, const vlc_guid_t *id )
822 bo_addle_u32( p_bo, id->Data1 );
823 bo_addle_u16( p_bo, id->Data2 );
824 bo_addle_u16( p_bo, id->Data3 );
825 for( int i = 0; i < 8; i++ )
827 bo_add_u8( p_bo, id->Data4[i] );
831 /****************************************************************************
832 * Misc
833 ****************************************************************************/
834 static void asf_chunk_add( bo_t *bo,
835 int i_type, int i_len, int i_flags, int i_seq )
837 bo_addle_u16( bo, i_type );
838 bo_addle_u16( bo, i_len + 8 );
839 bo_addle_u32( bo, i_seq );
840 bo_addle_u16( bo, i_flags );
841 bo_addle_u16( bo, i_len + 8 );
844 static block_t *asf_header_create( sout_mux_t *p_mux, bool b_broadcast )
846 sout_mux_sys_t *p_sys = p_mux->p_sys;
847 asf_track_t *tk;
848 vlc_tick_t i_duration = 0;
849 int i_size, i_header_ext_size;
850 int i_ci_size, i_cm_size = 0, i_cd_size = 0;
851 block_t *out;
852 bo_t bo;
853 tk=NULL;
855 msg_Dbg( p_mux, "Asf muxer creating header" );
857 if( p_sys->i_dts_first != VLC_TICK_INVALID && p_sys->i_dts_last > p_sys->i_dts_first )
859 i_duration = p_sys->i_dts_last - p_sys->i_dts_first;
862 /* calculate header size */
863 i_size = 30 + 104;
864 i_ci_size = 44;
865 for( size_t i = 0; i < vlc_array_count( &p_sys->tracks ); i++ )
867 tk = vlc_array_item_at_index( &p_sys->tracks, i );
868 /* update also track-id */
869 tk->i_id = i + 1;
871 i_size += 78 + tk->i_extra;
872 i_ci_size += 8 + 2 * strlen( tk->psz_name );
873 if( tk->i_cat == AUDIO_ES ) i_ci_size += 4;
874 else if( tk->i_cat == VIDEO_ES ) i_ci_size += 6;
876 /* Error correction data field */
877 if( tk->b_audio_correction ) i_size += 8;
880 /* size of the content description object */
881 if( *p_sys->psz_title || *p_sys->psz_author || *p_sys->psz_copyright ||
882 *p_sys->psz_comment || *p_sys->psz_rating )
884 i_cd_size = 34 + 2 * ( strlen( p_sys->psz_title ) + 1 +
885 strlen( p_sys->psz_author ) + 1 +
886 strlen( p_sys->psz_copyright ) + 1 +
887 strlen( p_sys->psz_comment ) + 1 +
888 strlen( p_sys->psz_rating ) + 1 );
891 i_header_ext_size = 46;
893 /* size of the metadata object */
894 for( size_t i = 0; i < vlc_array_count( &p_sys->tracks ); i++ )
896 const asf_track_t *p_track = vlc_array_item_at_index( &p_sys->tracks, i );
897 if( p_track->i_cat == VIDEO_ES &&
898 p_track->fmt.video.i_sar_num != 0 &&
899 p_track->fmt.video.i_sar_den != 0 )
901 i_cm_size = 26 + 2 * (16 + 2 * sizeof("AspectRatio?"));
903 if( p_track->b_extended )
904 i_header_ext_size += 88;
908 i_header_ext_size += i_cm_size;
910 i_size += i_ci_size + i_cd_size + i_header_ext_size ;
912 if( p_sys->b_asf_http )
914 out = block_Alloc( i_size + 50 + 12 );
915 bo_init( &bo, out->p_buffer, i_size + 50 + 12 );
916 asf_chunk_add( &bo, 0x4824, i_size + 50, 0xc00, p_sys->i_seq++ );
918 else
920 out = block_Alloc( i_size + 50 );
921 bo_init( &bo, out->p_buffer, i_size + 50 );
924 /* header object */
925 bo_add_guid ( &bo, &asf_object_header_guid );
926 bo_addle_u64( &bo, i_size );
927 bo_addle_u32( &bo, 2 + vlc_array_count( &p_sys->tracks ) + 1 +
928 (i_cd_size ? 1 : 0) + (i_cm_size ? 1 : 0) );
929 bo_add_u8 ( &bo, 1 );
930 bo_add_u8 ( &bo, 2 );
932 /* sub object */
934 /* file properties */
935 bo_add_guid ( &bo, &asf_object_file_properties_guid );
936 bo_addle_u64( &bo, 104 );
937 bo_add_guid ( &bo, &p_sys->fid );
938 bo_addle_u64( &bo, i_size + 50 + p_sys->i_packet_count *
939 p_sys->i_packet_size ); /* file size */
940 bo_addle_u64( &bo, 0 ); /* creation date */
941 bo_addle_u64( &bo, b_broadcast ? 0xffffffffLL : p_sys->i_packet_count );
942 bo_addle_u64( &bo, MSFTIME_FROM_VLC_TICK(i_duration) ); /* play duration (100ns) */
943 bo_addle_u64( &bo, MSFTIME_FROM_VLC_TICK(i_duration) ); /* send duration (100ns) */
944 bo_addle_u64( &bo, p_sys->i_preroll_time ); /* preroll duration (ms) */
945 bo_addle_u32( &bo, b_broadcast ? 0x01 : 0x02 /* seekable */ ); /* flags */
946 bo_addle_u32( &bo, p_sys->i_packet_size ); /* packet size min */
947 bo_addle_u32( &bo, p_sys->i_packet_size ); /* packet size max */
948 /* NOTE: According to p6-9 of the ASF specification the bitrate cannot be 0,
949 * therefor apply this workaround to make sure it is not 0. If the bitrate is
950 * 0 the file will play in WMP11, but not in Sliverlight and WMP12 */
951 bo_addle_u32( &bo, p_sys->i_bitrate > 0 ? p_sys->i_bitrate : 1 ); /* maxbitrate */
953 /* header extension */
954 bo_add_guid ( &bo, &asf_object_header_extension_guid );
955 bo_addle_u64( &bo, i_header_ext_size );
956 bo_add_guid ( &bo, &asf_guid_reserved_1 );
957 bo_addle_u16( &bo, 6 );
958 bo_addle_u32( &bo, i_header_ext_size - 46 );
960 /* extended stream properties */
961 for( size_t i = 0; i < vlc_array_count( &p_sys->tracks ); i++ )
963 const asf_track_t *p_track = vlc_array_item_at_index( &p_sys->tracks, i );
964 const es_format_t *p_fmt = &p_track->fmt;
966 if( !p_track->b_extended )
967 continue;
969 uint64_t i_avg_duration = 0;
970 if( p_fmt->i_cat == VIDEO_ES &&
971 p_fmt->video.i_frame_rate > 0 && p_fmt->video.i_frame_rate_base > 0 )
972 i_avg_duration = ( INT64_C(10000000) * p_fmt->video.i_frame_rate_base +
973 p_fmt->video.i_frame_rate/2 ) / p_fmt->video.i_frame_rate;
975 bo_add_guid ( &bo, &asf_object_extended_stream_properties_guid );
976 bo_addle_u64( &bo, 88 );
977 bo_addle_u64( &bo, 0 );
978 bo_addle_u64( &bo, 0 );
979 bo_addle_u32( &bo, p_fmt->i_bitrate ); /* Bitrate */
980 bo_addle_u32( &bo, p_sys->i_preroll_time ); /* Buffer size */
981 bo_addle_u32( &bo, 0 ); /* Initial buffer fullness */
982 bo_addle_u32( &bo, p_fmt->i_bitrate ); /* Alternate Bitrate */
983 bo_addle_u32( &bo, 0 ); /* Alternate Buffer size */
984 bo_addle_u32( &bo, 0 ); /* Alternate Initial buffer fullness */
985 bo_addle_u32( &bo, 0 ); /* Maximum object size (0 = unknown) */
986 bo_addle_u32( &bo, 0x02 ); /* Flags (seekable) */
987 bo_addle_u16( &bo, p_track->i_id ); /* Stream number */
988 bo_addle_u16( &bo, 0 ); /* Stream language index */
989 bo_addle_u64( &bo, i_avg_duration ); /* Average time per frame */
990 bo_addle_u16( &bo, 0 ); /* Stream name count */
991 bo_addle_u16( &bo, 0 ); /* Payload extension system count */
994 /* metadata object (part of header extension) */
995 if( i_cm_size )
997 unsigned int i_dst_num, i_dst_den;
999 asf_track_t *tk = NULL;
1000 for( size_t i = 0; i < vlc_array_count( &p_sys->tracks ); i++ )
1002 tk = vlc_array_item_at_index( &p_sys->tracks, i );
1003 if( tk->i_cat == VIDEO_ES &&
1004 tk->fmt.video.i_sar_num != 0 &&
1005 tk->fmt.video.i_sar_den != 0 )
1007 vlc_ureduce( &i_dst_num, &i_dst_den,
1008 tk->fmt.video.i_sar_num,
1009 tk->fmt.video.i_sar_den, 0 );
1010 break;
1013 assert( tk != NULL );
1015 msg_Dbg( p_mux, "pixel aspect-ratio: %i/%i", i_dst_num, i_dst_den );
1017 bo_add_guid ( &bo, &asf_object_metadata_guid );
1018 bo_addle_u64( &bo, i_cm_size );
1019 bo_addle_u16( &bo, 2 ); /* description records count */
1020 /* 1st description record */
1021 bo_addle_u16( &bo, 0 ); /* reserved */
1022 bo_addle_u16( &bo, tk->i_id ); /* stream number (0 for the whole file) */
1023 bo_addle_u16( &bo, 2 * sizeof("AspectRatioX") ); /* name length */
1024 bo_addle_u16( &bo, 0x3 /* DWORD */ ); /* data type */
1025 bo_addle_u32( &bo, 4 ); /* data length */
1026 bo_addle_str16_nosize( &bo, "AspectRatioX" );
1027 bo_addle_u32( &bo, i_dst_num ); /* data */
1028 /* 2nd description record */
1029 bo_addle_u16( &bo, 0 ); /* reserved */
1030 bo_addle_u16( &bo, tk->i_id ); /* stream number (0 for the whole file) */
1031 bo_addle_u16( &bo, 2 * sizeof("AspectRatioY") ); /* name length */
1032 bo_addle_u16( &bo, 0x3 /* DWORD */ ); /* data type */
1033 bo_addle_u32( &bo, 4 ); /* data length */
1034 bo_addle_str16_nosize( &bo, "AspectRatioY" );
1035 bo_addle_u32( &bo, i_dst_den ); /* data */
1038 /* content description header */
1039 if( i_cd_size > 0 )
1041 bo_add_guid ( &bo, &asf_object_content_description_guid );
1042 bo_addle_u64( &bo, i_cd_size );
1043 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_title ) + 2 );
1044 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_author ) + 2 );
1045 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_copyright ) + 2 );
1046 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_comment ) + 2 );
1047 bo_addle_u16( &bo, 2 * strlen( p_sys->psz_rating ) + 2 );
1049 bo_addle_str16_nosize( &bo, p_sys->psz_title );
1050 bo_addle_str16_nosize( &bo, p_sys->psz_author );
1051 bo_addle_str16_nosize( &bo, p_sys->psz_copyright );
1052 bo_addle_str16_nosize( &bo, p_sys->psz_comment );
1053 bo_addle_str16_nosize( &bo, p_sys->psz_rating );
1056 /* stream properties */
1057 for( size_t i = 0; i < vlc_array_count( &p_sys->tracks ); i++ )
1059 tk = vlc_array_item_at_index( &p_sys->tracks, i);
1061 bo_add_guid ( &bo, &asf_object_stream_properties_guid );
1062 bo_addle_u64( &bo, 78 + tk->i_extra + (tk->b_audio_correction ? 8:0) );
1064 if( tk->i_cat == AUDIO_ES )
1066 bo_add_guid( &bo, &asf_object_stream_type_audio );
1067 if( tk->b_audio_correction )
1068 bo_add_guid( &bo, &asf_guid_audio_conceal_spread );
1069 else
1070 bo_add_guid( &bo, &asf_no_error_correction_guid );
1072 else if( tk->i_cat == VIDEO_ES )
1074 bo_add_guid( &bo, &asf_object_stream_type_video );
1075 bo_add_guid( &bo, &asf_no_error_correction_guid );
1077 bo_addle_u64( &bo, 0 ); /* time offset */
1078 bo_addle_u32( &bo, tk->i_extra );
1079 /* correction data length */
1080 bo_addle_u32( &bo, tk->b_audio_correction ? 8 : 0 );
1081 bo_addle_u16( &bo, tk->i_id ); /* stream number */
1082 bo_addle_u32( &bo, 0 );
1083 bo_add_mem ( &bo, tk->p_extra, tk->i_extra );
1085 /* Error correction data field */
1086 if( tk->b_audio_correction )
1088 bo_add_u8( &bo, 0x1 ); /* span */
1089 bo_addle_u16( &bo, tk->i_blockalign ); /* virtual packet length */
1090 bo_addle_u16( &bo, tk->i_blockalign ); /* virtual chunck length */
1091 bo_addle_u16( &bo, 1 ); /* silence length */
1092 bo_add_u8( &bo, 0x0 ); /* data */
1096 /* Codec Infos */
1097 bo_add_guid ( &bo, &asf_object_codec_list_guid );
1098 bo_addle_u64( &bo, i_ci_size );
1099 bo_add_guid ( &bo, &asf_guid_reserved_2 );
1100 bo_addle_u32( &bo, vlc_array_count( &p_sys->tracks ) );
1101 for( size_t i = 0; i < vlc_array_count( &p_sys->tracks ); i++ )
1103 tk = vlc_array_item_at_index( &p_sys->tracks ,i);
1105 if( tk->i_cat == VIDEO_ES ) bo_addle_u16( &bo, 1 /* video */ );
1106 else if( tk->i_cat == AUDIO_ES ) bo_addle_u16( &bo, 2 /* audio */ );
1107 else bo_addle_u16( &bo, 0xFFFF /* unknown */ );
1109 bo_addle_str16( &bo, tk->psz_name );
1110 bo_addle_u16( &bo, 0 );
1111 if( tk->i_cat == AUDIO_ES )
1113 bo_addle_u16( &bo, 2 );
1114 bo_addle_u16( &bo, tk->i_tag );
1116 else if( tk->i_cat == VIDEO_ES )
1118 bo_addle_u16( &bo, 4 );
1119 bo_add_mem ( &bo, (uint8_t*)&tk->i_fourcc, 4 );
1123 /* data object */
1124 bo_add_guid ( &bo, &asf_object_data_guid );
1125 bo_addle_u64( &bo, 50 + p_sys->i_packet_count * p_sys->i_packet_size );
1126 bo_add_guid ( &bo, &p_sys->fid );
1127 bo_addle_u64( &bo, p_sys->i_packet_count );
1128 bo_addle_u16( &bo, 0x101 );
1130 return out;
1133 /****************************************************************************
1135 ****************************************************************************/
1136 static block_t *asf_packet_flush( sout_mux_t *p_mux )
1138 sout_mux_sys_t *p_sys = p_mux->p_sys;
1139 int i_pad, i_preheader = p_sys->b_asf_http ? 12 : 0;
1140 block_t *pk;
1141 bo_t bo;
1143 if( !p_sys->pk ) return 0;
1145 i_pad = p_sys->i_packet_size - p_sys->i_pk_used;
1146 memset( p_sys->pk->p_buffer + p_sys->i_pk_used, 0, i_pad );
1148 bo_init( &bo, p_sys->pk->p_buffer, 14 + i_preheader );
1150 if( p_sys->b_asf_http )
1151 asf_chunk_add( &bo, 0x4424, p_sys->i_packet_size, 0x0, p_sys->i_seq++);
1153 bo_add_u8 ( &bo, 0x82 );
1154 bo_addle_u16( &bo, 0 );
1155 bo_add_u8( &bo, 0x11 );
1156 bo_add_u8( &bo, 0x5d );
1157 bo_addle_u16( &bo, i_pad );
1158 bo_addle_u32( &bo, MS_FROM_VLC_TICK(p_sys->i_pk_dts - p_sys->i_dts_first) +
1159 p_sys->i_preroll_time );
1160 bo_addle_u16( &bo, 0 /* data->i_length */ );
1161 bo_add_u8( &bo, 0x80 | p_sys->i_pk_frame );
1163 pk = p_sys->pk;
1164 p_sys->pk = NULL;
1166 p_sys->i_packet_count++;
1168 return pk;
1171 static block_t *asf_packet_create( sout_mux_t *p_mux,
1172 asf_track_t *tk, block_t *data )
1174 sout_mux_sys_t *p_sys = p_mux->p_sys;
1176 int i_data = data->i_buffer;
1177 int i_pos = 0;
1178 uint8_t *p_data= data->p_buffer;
1179 block_t *first = NULL, **last = &first;
1180 int i_preheader = p_sys->b_asf_http ? 12 : 0;
1182 while( i_pos < i_data )
1184 bo_t bo;
1185 int i_payload;
1187 if( p_sys->pk == NULL )
1189 p_sys->pk = block_Alloc( p_sys->i_packet_size + i_preheader );
1190 /* reserve 14 bytes for the packet header */
1191 p_sys->i_pk_used = 14 + i_preheader;
1192 p_sys->i_pk_frame = 0;
1193 p_sys->i_pk_dts = data->i_dts;
1196 bo_init( &bo, &p_sys->pk->p_buffer[p_sys->i_pk_used],
1197 p_sys->i_packet_size - p_sys->i_pk_used );
1199 /* add payload (header size = 17) */
1200 i_payload = __MIN( i_data - i_pos,
1201 p_sys->i_packet_size - p_sys->i_pk_used - 17 );
1203 if( tk->b_audio_correction && p_sys->i_pk_frame && i_payload < i_data )
1205 /* Don't know why yet but WMP doesn't like splitted WMA packets */
1206 *last = asf_packet_flush( p_mux );
1207 last = &(*last)->p_next;
1208 continue;
1211 bo_add_u8 ( &bo, !(data->i_flags & BLOCK_FLAG_TYPE_P ||
1212 data->i_flags & BLOCK_FLAG_TYPE_B) ?
1213 0x80 | tk->i_id : tk->i_id );
1214 bo_add_u8 ( &bo, tk->i_sequence );
1215 bo_addle_u32( &bo, i_pos );
1216 bo_add_u8 ( &bo, 0x08 ); /* flags */
1217 bo_addle_u32( &bo, i_data );
1218 bo_addle_u32( &bo, MS_FROM_VLC_TICK(data->i_dts - p_sys->i_dts_first) +
1219 p_sys->i_preroll_time );
1220 bo_addle_u16( &bo, i_payload );
1221 bo_add_mem ( &bo, &p_data[i_pos], i_payload );
1222 i_pos += i_payload;
1223 p_sys->i_pk_used += 17 + i_payload;
1225 p_sys->i_pk_frame++;
1227 if( p_sys->i_pk_used + 17 >= p_sys->i_packet_size )
1229 /* Not enough data for another payload, flush the packet */
1230 *last = asf_packet_flush( p_mux );
1231 last = &(*last)->p_next;
1235 tk->i_sequence++;
1236 block_Release( data );
1238 return first;
1241 static block_t *asf_stream_end_create( sout_mux_t *p_mux )
1243 sout_mux_sys_t *p_sys = p_mux->p_sys;
1245 block_t *out = NULL;
1246 bo_t bo;
1248 if( p_sys->b_asf_http )
1250 out = block_Alloc( 12 );
1251 bo_init( &bo, out->p_buffer, 12 );
1252 asf_chunk_add( &bo, 0x4524, 0, 0x00, p_sys->i_seq++ );
1254 else
1256 /* Create index */
1257 out = block_Alloc( 56 );
1258 bo_init( &bo, out->p_buffer, 56 );
1259 bo_add_guid ( &bo, &asf_object_index_guid );
1260 bo_addle_u64( &bo, 56 );
1261 bo_add_guid ( &bo, &p_sys->fid );
1262 bo_addle_u64( &bo, 10000000 );
1263 bo_addle_u32( &bo, 5 );
1264 bo_addle_u32( &bo, 0 );
1267 return out;