1 /*****************************************************************************
2 * asf.c : ASF demux module
3 *****************************************************************************
4 * Copyright (C) 2002-2003 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc_demux.h>
31 #include <vlc_interface.h>
34 #include <vlc_access.h> /* GET_PRIVATE_ID_STATE */
35 #include <vlc_codecs.h> /* BITMAPINFOHEADER, WAVEFORMATEX */
39 * - add support for the newly added object: language, bitrate,
40 * extended stream properties.
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close ( vlc_object_t * );
50 set_category( CAT_INPUT );
51 set_subcategory( SUBCAT_INPUT_DEMUX );
52 set_description( _("ASF v1.0 demuxer") );
53 set_capability( "demux2", 200 );
54 set_callbacks( Open, Close );
55 add_shortcut( "asf" );
59 /*****************************************************************************
61 *****************************************************************************/
62 static int Demux ( demux_t * );
63 static int Control( demux_t *, int i_query, va_list args );
71 asf_object_stream_properties_t *p_sp;
75 block_t *p_frame; /* use to gather complete frame */
81 mtime_t i_time; /* s */
82 mtime_t i_length; /* length of file file */
83 int64_t i_bitrate; /* global file bitrate */
85 asf_object_root_t *p_root;
86 asf_object_file_properties_t *p_fp;
89 asf_track_t *track[128];
98 static mtime_t GetMoviePTS( demux_sys_t * );
99 static int DemuxInit( demux_t * );
100 static void DemuxEnd( demux_t * );
101 static int DemuxPacket( demux_t * );
103 /*****************************************************************************
104 * Open: check file and initializes ASF structures
105 *****************************************************************************/
106 static int Open( vlc_object_t * p_this )
108 demux_t *p_demux = (demux_t *)p_this;
113 /* A little test to see if it could be a asf stream */
114 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 ) return VLC_EGENERIC;
116 ASF_GetGUID( &guid, p_peek );
117 if( !ASF_CmpGUID( &guid, &asf_object_header_guid ) ) return VLC_EGENERIC;
119 /* Set p_demux fields */
120 p_demux->pf_demux = Demux;
121 p_demux->pf_control = Control;
122 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
123 memset( p_sys, 0, sizeof( demux_sys_t ) );
125 /* Load the headers */
126 if( DemuxInit( p_demux ) )
135 /*****************************************************************************
136 * Demux: read packet and send them to decoders
137 *****************************************************************************/
138 static int Demux( demux_t *p_demux )
140 demux_sys_t *p_sys = p_demux->p_sys;
146 mtime_t i_time_begin = GetMoviePTS( p_sys );
149 if( p_demux->b_die ) break;
151 /* Check if we have concatenated files */
152 if( stream_Peek( p_demux->s, &p_peek, 16 ) == 16 )
156 ASF_GetGUID( &guid, p_peek );
157 if( ASF_CmpGUID( &guid, &asf_object_header_guid ) )
159 msg_Warn( p_demux, "found a new ASF header" );
160 /* We end this stream */
163 /* And we prepare to read the next one */
164 if( DemuxInit( p_demux ) )
166 msg_Err( p_demux, "failed to load the new header" );
167 intf_UserFatal( p_demux, VLC_FALSE, _("Could not demux ASF stream"),
168 _("VLC failed to load the ASF header.") );
175 /* Read and demux a packet */
176 if( ( i_result = DemuxPacket( p_demux ) ) <= 0 )
180 if( i_time_begin == -1 )
182 i_time_begin = GetMoviePTS( p_sys );
186 i_length = GetMoviePTS( p_sys ) - i_time_begin;
187 if( i_length < 0 || i_length >= 40 * 1000 ) break;
192 p_sys->i_time = GetMoviePTS( p_sys );
193 if( p_sys->i_time >= 0 )
195 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
201 /*****************************************************************************
202 * Close: frees unused data
203 *****************************************************************************/
204 static void Close( vlc_object_t * p_this )
206 demux_t *p_demux = (demux_t *)p_this;
210 free( p_demux->p_sys );
213 /*****************************************************************************
214 * SeekIndex: goto to i_date or i_percent
215 *****************************************************************************/
216 static int SeekIndex( demux_t *p_demux, mtime_t i_date, float f_pos )
218 demux_sys_t *p_sys = p_demux->p_sys;
219 asf_object_index_t *p_index;
222 msg_Dbg( p_demux, "seek with index: %i seconds, position %f",
223 (int)(i_date/1000000), f_pos );
225 p_index = ASF_FindObject( p_sys->p_root, &asf_object_index_guid, 0 );
227 if( i_date < 0 ) i_date = p_sys->i_length * f_pos;
229 i_pos = i_date * 10 / p_index->i_index_entry_time_interval;
230 i_pos = p_index->index_entry[i_pos].i_packet_number *
231 p_sys->p_fp->i_min_data_packet_size;
233 return stream_Seek( p_demux->s, p_sys->i_data_begin + i_pos );
236 /*****************************************************************************
238 *****************************************************************************/
239 static int Control( demux_t *p_demux, int i_query, va_list args )
241 demux_sys_t *p_sys = p_demux->p_sys;
249 case DEMUX_GET_LENGTH:
250 pi64 = (int64_t*)va_arg( args, int64_t * );
251 *pi64 = p_sys->i_length;
255 pi64 = (int64_t*)va_arg( args, int64_t * );
256 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
257 *pi64 = p_sys->i_time;
262 for( i = 0; i < 128 ; i++ )
263 if( p_sys->track[i] ) p_sys->track[i]->i_time = -1;
265 if( p_sys->b_index && p_sys->i_length > 0 )
267 i64 = (int64_t)va_arg( args, int64_t );
268 return SeekIndex( p_demux, i64, -1 );
272 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
273 p_sys->i_data_end, p_sys->i_bitrate,
274 p_sys->p_fp->i_min_data_packet_size,
278 case DEMUX_GET_POSITION:
279 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
280 if( p_sys->i_length > 0 )
282 pf = (double*)va_arg( args, double * );
283 *pf = p_sys->i_time / (double)p_sys->i_length;
286 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
287 p_sys->i_data_end, p_sys->i_bitrate,
288 p_sys->p_fp->i_min_data_packet_size,
291 case DEMUX_SET_POSITION:
293 for( i = 0; i < 128 ; i++ )
294 if( p_sys->track[i] ) p_sys->track[i]->i_time = -1;
296 if( p_sys->b_index && p_sys->i_length > 0 )
298 f = (double)va_arg( args, double );
299 return SeekIndex( p_demux, -1, f );
303 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
304 p_sys->i_data_end, p_sys->i_bitrate,
305 p_sys->p_fp->i_min_data_packet_size,
310 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
311 vlc_meta_Merge( p_meta, p_sys->meta );
315 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
316 p_sys->i_data_end, p_sys->i_bitrate,
317 p_sys->p_fp->i_min_data_packet_size,
322 /*****************************************************************************
324 *****************************************************************************/
325 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
330 for( i = 0; i < 128 ; i++ )
332 asf_track_t *tk = p_sys->track[i];
334 if( tk && tk->p_es && tk->i_time > 0)
336 if( i_time < 0 ) i_time = tk->i_time;
337 else i_time = __MIN( i_time, tk->i_time );
344 #define GETVALUE2b( bits, var, def ) \
345 switch( (bits)&0x03 ) \
347 case 1: var = p_peek[i_skip]; i_skip++; break; \
348 case 2: var = GetWLE( p_peek + i_skip ); i_skip+= 2; break; \
349 case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
351 default: var = def; break;\
354 static int DemuxPacket( demux_t *p_demux )
356 demux_sys_t *p_sys = p_demux->p_sys;
357 int i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
361 int i_packet_size_left;
363 int i_packet_property;
365 int b_packet_multiple_payload;
367 int i_packet_sequence;
368 int i_packet_padding_length;
370 uint32_t i_packet_send_time;
371 uint16_t i_packet_duration;
374 int i_payload_length_type;
377 if( stream_Peek( p_demux->s, &p_peek,i_data_packet_min)<i_data_packet_min )
379 msg_Warn( p_demux, "cannot peek while getting new packet, EOF ?" );
384 /* *** parse error correction if present *** */
387 unsigned int i_error_correction_length_type;
388 unsigned int i_error_correction_data_length;
389 unsigned int i_opaque_data_present;
391 i_error_correction_data_length = p_peek[0] & 0x0f; // 4bits
392 i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01; // 1bit
393 i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
394 i_skip += 1; // skip error correction flags
396 if( i_error_correction_length_type != 0x00 ||
397 i_opaque_data_present != 0 ||
398 i_error_correction_data_length != 0x02 )
400 goto loop_error_recovery;
403 i_skip += i_error_correction_data_length;
407 msg_Warn( p_demux, "p_peek[0]&0x80 != 0x80" );
411 if( i_skip + 2 >= i_data_packet_min )
413 goto loop_error_recovery;
416 i_packet_flags = p_peek[i_skip]; i_skip++;
417 i_packet_property = p_peek[i_skip]; i_skip++;
419 b_packet_multiple_payload = i_packet_flags&0x01;
421 /* read some value */
422 GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
423 GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
424 GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
426 i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
427 i_packet_duration = GetWLE( p_peek + i_skip ); i_skip += 2;
429 // i_packet_size_left = i_packet_length; // XXX data really read
430 /* FIXME I have to do that for some file, I don't known why */
431 i_packet_size_left = i_data_packet_min;
433 if( b_packet_multiple_payload )
435 i_payload_count = p_peek[i_skip] & 0x3f;
436 i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
442 i_payload_length_type = 0x02; // unused
445 for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
449 int i_packet_keyframe;
451 int i_media_object_number;
452 int i_media_object_offset;
453 int i_replicated_data_length;
454 int i_payload_data_length;
455 int i_payload_data_pos;
456 int i_sub_payload_data_length;
462 if( i_skip >= i_packet_size_left )
464 /* prevent some segfault with invalid file */
468 i_packet_keyframe = p_peek[i_skip] >> 7;
469 i_stream_number = p_peek[i_skip++] & 0x7f;
471 GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
472 GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
473 GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
475 if( i_replicated_data_length > 1 ) // should be at least 8 bytes
477 i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
478 i_skip += i_replicated_data_length;
481 i_media_object_offset = i_tmp;
483 if( i_skip >= i_packet_size_left )
488 else if( i_replicated_data_length == 1 )
490 /* msg_Dbg( p_demux, "found compressed payload" ); */
492 i_pts = (mtime_t)i_tmp * 1000;
493 i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
495 i_media_object_offset = 0;
499 i_pts = (mtime_t)i_packet_send_time * 1000;
502 i_media_object_offset = i_tmp;
505 i_pts = __MAX( i_pts - p_sys->p_fp->i_preroll * 1000, 0 );
506 if( b_packet_multiple_payload )
508 GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
512 i_payload_data_length = i_packet_length -
513 i_packet_padding_length - i_skip;
516 if( i_payload_data_length < 0 || i_skip + i_payload_data_length > i_packet_size_left )
522 "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
523 i_payload + 1, i_payload_count, i_stream_number, i_media_object_number,
524 i_media_object_offset, i_replicated_data_length, i_payload_data_length );
527 if( ( tk = p_sys->track[i_stream_number] ) == NULL )
530 "undeclared stream[Id 0x%x]", i_stream_number );
531 i_skip += i_payload_data_length;
532 continue; // over payload
537 i_skip += i_payload_data_length;
542 for( i_payload_data_pos = 0;
543 i_payload_data_pos < i_payload_data_length &&
544 i_packet_size_left > 0;
545 i_payload_data_pos += i_sub_payload_data_length )
550 // read sub payload length
551 if( i_replicated_data_length == 1 )
553 i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
554 i_payload_data_pos++;
558 i_sub_payload_data_length = i_payload_data_length;
561 /* FIXME I don't use i_media_object_number, sould I ? */
562 if( tk->p_frame && i_media_object_offset == 0 )
564 /* send complete packet to decoder */
565 block_t *p_gather = block_ChainGather( tk->p_frame );
567 es_out_Send( p_demux->out, tk->p_es, p_gather );
572 i_read = i_sub_payload_data_length + i_skip;
573 if( ( p_frag = stream_Block( p_demux->s, i_read ) ) == NULL )
575 msg_Warn( p_demux, "cannot read data" );
578 i_packet_size_left -= i_read;
580 p_frag->p_buffer += i_skip;
581 p_frag->i_buffer -= i_skip;
583 if( tk->p_frame == NULL )
586 ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
588 p_frag->i_pts = tk->i_time;
590 if( tk->i_cat != VIDEO_ES )
591 p_frag->i_dts = p_frag->i_pts;
594 p_frag->i_dts = p_frag->i_pts;
599 block_ChainAppend( &tk->p_frame, p_frag );
602 if( i_packet_size_left > 0 )
604 if( stream_Peek( p_demux->s, &p_peek, i_packet_size_left )
605 < i_packet_size_left )
607 msg_Warn( p_demux, "cannot peek, EOF ?" );
614 if( i_packet_size_left > 0 )
616 if( stream_Read( p_demux->s, NULL, i_packet_size_left )
617 < i_packet_size_left )
619 msg_Warn( p_demux, "cannot skip data, EOF ?" );
627 msg_Warn( p_demux, "unsupported packet header" );
628 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
630 msg_Err( p_demux, "unsupported packet header, fatal error" );
633 stream_Read( p_demux->s, NULL, i_data_packet_min );
638 /*****************************************************************************
640 *****************************************************************************/
641 static int DemuxInit( demux_t *p_demux )
643 demux_sys_t *p_sys = p_demux->p_sys;
644 vlc_bool_t b_seekable;
645 unsigned int i_stream, i;
646 asf_object_content_description_t *p_cd;
647 asf_object_index_t *p_index;
653 p_sys->i_bitrate = 0;
654 p_sys->p_root = NULL;
658 for( i = 0; i < 128; i++ )
660 p_sys->track[i] = NULL;
662 p_sys->i_data_begin = -1;
663 p_sys->i_data_end = -1;
666 /* Now load all object ( except raw data ) */
667 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
668 if( !(p_sys->p_root = ASF_ReadObjectRoot(p_demux->s, b_seekable)) )
670 msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
673 p_sys->p_fp = p_sys->p_root->p_fp;
675 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
677 msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
681 p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
682 &asf_object_stream_properties_guid );
683 if( p_sys->i_track <= 0 )
685 msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
689 /* check if index is available */
690 p_index = ASF_FindObject( p_sys->p_root, &asf_object_index_guid, 0 );
691 b_index = p_index && p_index->i_index_entry_count;
693 msg_Dbg( p_demux, "found %d streams", p_sys->i_track );
695 for( i_stream = 0; i_stream < p_sys->i_track; i_stream ++ )
698 asf_object_stream_properties_t *p_sp;
699 vlc_bool_t b_access_selected;
701 p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
702 &asf_object_stream_properties_guid,
705 tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
706 memset( tk, 0, sizeof( asf_track_t ) );
713 /* Check (in case of mms) if this track is selected (ie will receive data) */
714 if( !stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_GET_PRIVATE_ID_STATE,
715 p_sp->i_stream_number, &b_access_selected ) &&
718 tk->i_cat = UNKNOWN_ES;
719 msg_Dbg( p_demux, "ignoring not selected stream(ID:%d) (by access)",
720 p_sp->i_stream_number );
724 if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
725 p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
728 uint8_t *p_data = p_sp->p_type_specific_data;
731 es_format_Init( &fmt, AUDIO_ES, 0 );
732 i_format = GetWLE( &p_data[0] );
733 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
734 fmt.audio.i_channels = GetWLE( &p_data[2] );
735 fmt.audio.i_rate = GetDWLE( &p_data[4] );
736 fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
737 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
738 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
740 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
741 i_format != WAVE_FORMAT_MPEGLAYER3 &&
742 i_format != WAVE_FORMAT_MPEG )
744 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
745 p_sp->i_type_specific_data_length -
746 sizeof( WAVEFORMATEX ) );
747 fmt.p_extra = malloc( fmt.i_extra );
748 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
752 tk->i_cat = AUDIO_ES;
753 tk->p_es = es_out_Add( p_demux->out, &fmt );
754 es_format_Clean( &fmt );
756 msg_Dbg( p_demux, "added new audio stream(codec:0x%x,ID:%d)",
757 GetWLE( p_data ), p_sp->i_stream_number );
759 else if( ASF_CmpGUID( &p_sp->i_stream_type,
760 &asf_object_stream_type_video ) &&
761 p_sp->i_type_specific_data_length >= 11 +
762 sizeof( BITMAPINFOHEADER ) )
765 uint8_t *p_data = &p_sp->p_type_specific_data[11];
767 es_format_Init( &fmt, VIDEO_ES,
768 VLC_FOURCC( p_data[16], p_data[17],
769 p_data[18], p_data[19] ) );
770 fmt.video.i_width = GetDWLE( p_data + 4 );
771 fmt.video.i_height= GetDWLE( p_data + 8 );
774 if( fmt.i_codec == VLC_FOURCC( 'D','V','R',' ') )
776 /* DVR-MS special ASF */
777 fmt.i_codec = VLC_FOURCC( 'm','p','g','2' ) ;
778 fmt.b_packetized = VLC_FALSE;
781 if( p_sp->i_type_specific_data_length > 11 +
782 sizeof( BITMAPINFOHEADER ) )
784 fmt.i_extra = __MIN( GetDWLE( p_data ),
785 p_sp->i_type_specific_data_length - 11 -
786 sizeof( BITMAPINFOHEADER ) );
787 fmt.p_extra = malloc( fmt.i_extra );
788 memcpy( fmt.p_extra, &p_data[sizeof( BITMAPINFOHEADER )],
792 /* Look for an aspect ratio */
793 if( p_sys->p_root->p_metadata )
795 asf_object_metadata_t *p_meta = p_sys->p_root->p_metadata;
796 int i_aspect_x = 0, i_aspect_y = 0;
799 for( i = 0; i < p_meta->i_record_entries_count; i++ )
801 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioX" ) )
803 if( (!i_aspect_x && !p_meta->record[i].i_stream) ||
804 p_meta->record[i].i_stream ==
805 p_sp->i_stream_number )
806 i_aspect_x = p_meta->record[i].i_val;
808 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioY" ) )
810 if( (!i_aspect_y && !p_meta->record[i].i_stream) ||
811 p_meta->record[i].i_stream ==
812 p_sp->i_stream_number )
813 i_aspect_y = p_meta->record[i].i_val;
817 if( i_aspect_x && i_aspect_y )
819 fmt.video.i_aspect = i_aspect_x *
820 (int64_t)fmt.video.i_width * VOUT_ASPECT_FACTOR /
821 fmt.video.i_height / i_aspect_y;
825 tk->i_cat = VIDEO_ES;
826 tk->p_es = es_out_Add( p_demux->out, &fmt );
827 es_format_Clean( &fmt );
829 /* If there is a video track then use the index for seeking */
830 p_sys->b_index = b_index;
832 msg_Dbg( p_demux, "added new video stream(ID:%d)",
833 p_sp->i_stream_number );
835 else if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_extended_stream_header ) &&
836 p_sp->i_type_specific_data_length >= 64 )
838 /* Now follows a 64 byte header of which we don't know much */
840 guid_t *p_ref = (guid_t *)p_sp->p_type_specific_data;
841 uint8_t *p_data = p_sp->p_type_specific_data + 64;
842 unsigned int i_data = p_sp->i_type_specific_data_length - 64;
844 msg_Dbg( p_demux, "Ext stream header detected. datasize = %d", p_sp->i_type_specific_data_length );
845 if( ASF_CmpGUID( p_ref, &asf_object_extended_stream_type_audio ) &&
846 i_data >= sizeof( WAVEFORMATEX ) - 2)
849 es_format_Init( &fmt, AUDIO_ES, 0 );
850 i_format = GetWLE( &p_data[0] );
852 fmt.i_codec = VLC_FOURCC( 'a','5','2',' ');
854 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
855 fmt.audio.i_channels = GetWLE( &p_data[2] );
856 fmt.audio.i_rate = GetDWLE( &p_data[4] );
857 fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
858 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
859 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
860 fmt.b_packetized = VLC_TRUE;
862 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
863 i_format != WAVE_FORMAT_MPEGLAYER3 &&
864 i_format != WAVE_FORMAT_MPEG )
866 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
867 p_sp->i_type_specific_data_length -
868 sizeof( WAVEFORMATEX ) );
869 fmt.p_extra = malloc( fmt.i_extra );
870 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
874 tk->i_cat = AUDIO_ES;
875 tk->p_es = es_out_Add( p_demux->out, &fmt );
876 es_format_Clean( &fmt );
878 msg_Dbg( p_demux, "added new audio stream (codec:0x%x,ID:%d)",
879 i_format, p_sp->i_stream_number );
884 tk->i_cat = UNKNOWN_ES;
885 msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
886 p_sp->i_stream_number );
890 p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
891 if( p_sys->p_root->p_data->i_object_size != 0 )
893 p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
894 p_sys->p_root->p_data->i_object_size;
897 { /* live/broacast */
898 p_sys->i_data_end = -1;
901 /* go to first packet */
902 stream_Seek( p_demux->s, p_sys->i_data_begin );
904 /* try to calculate movie time */
905 if( p_sys->p_fp->i_data_packets_count > 0 )
908 int64_t i_size = stream_Size( p_demux->s );
910 if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
912 i_size = p_sys->i_data_end;
915 /* real number of packets */
916 i_count = ( i_size - p_sys->i_data_begin ) /
917 p_sys->p_fp->i_min_data_packet_size;
919 /* calculate the time duration in micro-s */
920 p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
922 (mtime_t)p_sys->p_fp->i_data_packets_count;
924 if( p_sys->i_length > 0 )
926 p_sys->i_bitrate = 8 * i_size * (int64_t)1000000 / p_sys->i_length;
930 /* Create meta information */
931 p_sys->meta = vlc_meta_New();
933 if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
934 &asf_object_content_description_guid, 0 ) ) )
936 if( p_cd->psz_title && *p_cd->psz_title )
938 vlc_meta_SetTitle( p_sys->meta, p_cd->psz_title );
940 if( p_cd->psz_artist && *p_cd->psz_artist )
942 vlc_meta_SetArtist( p_sys->meta, p_cd->psz_artist );
944 if( p_cd->psz_copyright && *p_cd->psz_copyright )
946 vlc_meta_SetCopyright( p_sys->meta, p_cd->psz_copyright );
948 if( p_cd->psz_description && *p_cd->psz_description )
950 vlc_meta_SetDescription( p_sys->meta, p_cd->psz_description );
952 if( p_cd->psz_rating && *p_cd->psz_rating )
954 vlc_meta_SetRating( p_sys->meta, p_cd->psz_rating );
957 /// \tood Fix Child meta for ASF tracks
959 for( i_stream = 0, i = 0; i < 128; i++ )
961 asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
962 &asf_object_codec_list_guid, 0 );
964 if( p_sys->track[i] )
966 vlc_meta_t *tk = vlc_meta_New();
967 TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
969 if( p_cl && i_stream < p_cl->i_codec_entries_count )
971 if( p_cl->codec[i_stream].psz_name &&
972 *p_cl->codec[i_stream].psz_name )
974 vlc_meta_Add( tk, VLC_META_CODEC_NAME,
975 p_cl->codec[i_stream].psz_name );
977 if( p_cl->codec[i_stream].psz_description &&
978 *p_cl->codec[i_stream].psz_description )
980 vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
981 p_cl->codec[i_stream].psz_description );
989 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
993 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
996 /*****************************************************************************
998 *****************************************************************************/
999 static void DemuxEnd( demux_t *p_demux )
1001 demux_sys_t *p_sys = p_demux->p_sys;
1006 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1007 p_sys->p_root = NULL;
1011 vlc_meta_Delete( p_sys->meta );
1015 for( i = 0; i < 128; i++ )
1017 asf_track_t *tk = p_sys->track[i];
1023 block_ChainRelease( tk->p_frame );
1027 es_out_Del( p_demux->out, tk->p_es );
1031 p_sys->track[i] = 0;