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 *****************************************************************************/
33 #include <vlc_demux.h>
34 #include <vlc_interface.h>
37 #include <vlc_access.h> /* GET_PRIVATE_ID_STATE */
38 #include <vlc_codecs.h> /* BITMAPINFOHEADER, WAVEFORMATEX */
42 * - add support for the newly added object: language, bitrate,
43 * extended stream properties.
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Open ( vlc_object_t * );
50 static void Close ( vlc_object_t * );
53 set_category( CAT_INPUT );
54 set_subcategory( SUBCAT_INPUT_DEMUX );
55 set_description( _("ASF v1.0 demuxer") );
56 set_capability( "demux2", 200 );
57 set_callbacks( Open, Close );
58 add_shortcut( "asf" );
62 /*****************************************************************************
64 *****************************************************************************/
65 static int Demux ( demux_t * );
66 static int Control( demux_t *, int i_query, va_list args );
74 asf_object_stream_properties_t *p_sp;
78 block_t *p_frame; /* use to gather complete frame */
84 mtime_t i_time; /* s */
85 mtime_t i_length; /* length of file file */
86 int64_t i_bitrate; /* global file bitrate */
88 asf_object_root_t *p_root;
89 asf_object_file_properties_t *p_fp;
92 asf_track_t *track[128]; /* track number is stored on 7 bits */
101 static mtime_t GetMoviePTS( demux_sys_t * );
102 static int DemuxInit( demux_t * );
103 static void DemuxEnd( demux_t * );
104 static int DemuxPacket( demux_t * );
106 /*****************************************************************************
107 * Open: check file and initializes ASF structures
108 *****************************************************************************/
109 static int Open( vlc_object_t * p_this )
111 demux_t *p_demux = (demux_t *)p_this;
114 const uint8_t *p_peek;
116 /* A little test to see if it could be a asf stream */
117 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 ) return VLC_EGENERIC;
119 ASF_GetGUID( &guid, p_peek );
120 if( !ASF_CmpGUID( &guid, &asf_object_header_guid ) ) return VLC_EGENERIC;
122 /* Set p_demux fields */
123 p_demux->pf_demux = Demux;
124 p_demux->pf_control = Control;
125 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
126 memset( p_sys, 0, sizeof( demux_sys_t ) );
128 /* Load the headers */
129 if( DemuxInit( p_demux ) )
138 /*****************************************************************************
139 * Demux: read packet and send them to decoders
140 *****************************************************************************/
141 static int Demux( demux_t *p_demux )
143 demux_sys_t *p_sys = p_demux->p_sys;
147 const uint8_t *p_peek;
149 mtime_t i_time_begin = GetMoviePTS( p_sys );
154 if( p_sys->i_data_end >= 0 &&
155 stream_Tell( p_demux->s ) >= p_sys->i_data_end )
158 /* Check if we have concatenated files */
159 if( stream_Peek( p_demux->s, &p_peek, 16 ) == 16 )
163 ASF_GetGUID( &guid, p_peek );
164 if( ASF_CmpGUID( &guid, &asf_object_header_guid ) )
166 msg_Warn( p_demux, "found a new ASF header" );
167 /* We end this stream */
170 /* And we prepare to read the next one */
171 if( DemuxInit( p_demux ) )
173 msg_Err( p_demux, "failed to load the new header" );
174 intf_UserFatal( p_demux, VLC_FALSE, _("Could not demux ASF stream"),
175 _("VLC failed to load the ASF header.") );
182 /* Read and demux a packet */
183 if( ( i_result = DemuxPacket( p_demux ) ) <= 0 )
187 if( i_time_begin == -1 )
189 i_time_begin = GetMoviePTS( p_sys );
193 i_length = GetMoviePTS( p_sys ) - i_time_begin;
194 if( i_length < 0 || i_length >= 40 * 1000 ) break;
199 p_sys->i_time = GetMoviePTS( p_sys );
200 if( p_sys->i_time >= 0 )
202 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
208 /*****************************************************************************
209 * Close: frees unused data
210 *****************************************************************************/
211 static void Close( vlc_object_t * p_this )
213 demux_t *p_demux = (demux_t *)p_this;
217 free( p_demux->p_sys );
220 /*****************************************************************************
221 * SeekIndex: goto to i_date or i_percent
222 *****************************************************************************/
223 static int SeekIndex( demux_t *p_demux, mtime_t i_date, float f_pos )
225 demux_sys_t *p_sys = p_demux->p_sys;
226 asf_object_index_t *p_index;
229 msg_Dbg( p_demux, "seek with index: %i seconds, position %f",
230 (int)(i_date/1000000), f_pos );
232 p_index = ASF_FindObject( p_sys->p_root, &asf_object_index_guid, 0 );
234 if( i_date < 0 ) i_date = p_sys->i_length * f_pos;
236 i_pos = i_date * 10 / p_index->i_index_entry_time_interval;
237 i_pos = p_index->index_entry[i_pos].i_packet_number *
238 p_sys->p_fp->i_min_data_packet_size;
240 return stream_Seek( p_demux->s, p_sys->i_data_begin + i_pos );
243 /*****************************************************************************
245 *****************************************************************************/
246 static int Control( demux_t *p_demux, int i_query, va_list args )
248 demux_sys_t *p_sys = p_demux->p_sys;
256 case DEMUX_GET_LENGTH:
257 pi64 = (int64_t*)va_arg( args, int64_t * );
258 *pi64 = p_sys->i_length;
262 pi64 = (int64_t*)va_arg( args, int64_t * );
263 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
264 *pi64 = p_sys->i_time;
269 for( i = 0; i < 128 ; i++ )
270 if( p_sys->track[i] ) p_sys->track[i]->i_time = -1;
272 if( p_sys->b_index && p_sys->i_length > 0 )
274 i64 = (int64_t)va_arg( args, int64_t );
275 return SeekIndex( p_demux, i64, -1 );
279 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
280 p_sys->i_data_end, p_sys->i_bitrate,
281 p_sys->p_fp->i_min_data_packet_size,
285 case DEMUX_GET_POSITION:
286 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
287 if( p_sys->i_length > 0 )
289 pf = (double*)va_arg( args, double * );
290 *pf = p_sys->i_time / (double)p_sys->i_length;
293 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
294 p_sys->i_data_end, p_sys->i_bitrate,
295 p_sys->p_fp->i_min_data_packet_size,
298 case DEMUX_SET_POSITION:
300 for( i = 0; i < 128 ; i++ )
301 if( p_sys->track[i] ) p_sys->track[i]->i_time = -1;
303 if( p_sys->b_index && p_sys->i_length > 0 )
305 f = (double)va_arg( args, double );
306 return SeekIndex( p_demux, -1, f );
310 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
311 p_sys->i_data_end, p_sys->i_bitrate,
312 p_sys->p_fp->i_min_data_packet_size,
317 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
318 vlc_meta_Merge( p_meta, p_sys->meta );
322 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
323 p_sys->i_data_end, p_sys->i_bitrate,
324 p_sys->p_fp->i_min_data_packet_size,
329 /*****************************************************************************
331 *****************************************************************************/
332 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
337 for( i = 0; i < 128 ; i++ )
339 asf_track_t *tk = p_sys->track[i];
341 if( tk && tk->p_es && tk->i_time > 0)
343 if( i_time < 0 ) i_time = tk->i_time;
344 else i_time = __MIN( i_time, tk->i_time );
351 #define GETVALUE2b( bits, var, def ) \
352 switch( (bits)&0x03 ) \
354 case 1: var = p_peek[i_skip]; i_skip++; break; \
355 case 2: var = GetWLE( p_peek + i_skip ); i_skip+= 2; break; \
356 case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
358 default: var = def; break;\
361 static int DemuxPacket( demux_t *p_demux )
363 demux_sys_t *p_sys = p_demux->p_sys;
364 int i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
365 const uint8_t *p_peek;
368 int i_packet_size_left;
370 int i_packet_property;
372 int b_packet_multiple_payload;
374 int i_packet_sequence;
375 int i_packet_padding_length;
377 uint32_t i_packet_send_time;
378 uint16_t i_packet_duration;
381 int i_payload_length_type;
384 if( stream_Peek( p_demux->s, &p_peek,i_data_packet_min)<i_data_packet_min )
386 msg_Warn( p_demux, "cannot peek while getting new packet, EOF ?" );
391 /* *** parse error correction if present *** */
394 unsigned int i_error_correction_length_type;
395 unsigned int i_error_correction_data_length;
396 unsigned int i_opaque_data_present;
398 i_error_correction_data_length = p_peek[0] & 0x0f; // 4bits
399 i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01; // 1bit
400 i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
401 i_skip += 1; // skip error correction flags
403 if( i_error_correction_length_type != 0x00 ||
404 i_opaque_data_present != 0 ||
405 i_error_correction_data_length != 0x02 )
407 goto loop_error_recovery;
410 i_skip += i_error_correction_data_length;
414 msg_Warn( p_demux, "p_peek[0]&0x80 != 0x80" );
418 if( i_skip + 2 >= i_data_packet_min )
420 goto loop_error_recovery;
423 i_packet_flags = p_peek[i_skip]; i_skip++;
424 i_packet_property = p_peek[i_skip]; i_skip++;
426 b_packet_multiple_payload = i_packet_flags&0x01;
428 /* read some value */
429 GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
430 GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
431 GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
433 if( i_packet_padding_length > i_packet_length )
435 msg_Warn( p_demux, "Too large padding: %d", i_packet_padding_length );
436 goto loop_error_recovery;
439 i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
440 i_packet_duration = GetWLE( p_peek + i_skip ); i_skip += 2;
442 i_packet_size_left = i_packet_length - i_packet_padding_length;
444 if( b_packet_multiple_payload )
446 i_payload_count = p_peek[i_skip] & 0x3f;
447 i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
453 i_payload_length_type = 0x02; // unused
456 for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
460 int i_packet_keyframe;
462 int i_media_object_number;
463 int i_media_object_offset;
464 int i_replicated_data_length;
465 int i_payload_data_length;
466 int i_payload_data_pos;
467 int i_sub_payload_data_length;
473 if( i_skip >= i_packet_size_left )
475 /* prevent some segfault with invalid file */
479 i_packet_keyframe = p_peek[i_skip] >> 7;
480 i_stream_number = p_peek[i_skip++] & 0x7f;
482 GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
483 GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
484 GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
486 if( i_replicated_data_length > 1 ) // should be at least 8 bytes
488 i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
489 i_skip += i_replicated_data_length;
492 i_media_object_offset = i_tmp;
494 if( i_skip >= i_packet_size_left )
499 else if( i_replicated_data_length == 1 )
501 /* msg_Dbg( p_demux, "found compressed payload" ); */
503 i_pts = (mtime_t)i_tmp * 1000;
504 i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
506 i_media_object_offset = 0;
510 i_pts = (mtime_t)i_packet_send_time * 1000;
513 i_media_object_offset = i_tmp;
516 i_pts = __MAX( i_pts - p_sys->p_fp->i_preroll * 1000, 0 );
517 if( b_packet_multiple_payload )
519 GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
523 i_payload_data_length = i_packet_length - i_skip;
526 if( i_payload_data_length < 0 || i_payload_data_length > i_packet_size_left )
532 "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
533 i_payload + 1, i_payload_count, i_stream_number, i_media_object_number,
534 i_media_object_offset, i_replicated_data_length, i_payload_data_length );
537 if( ( tk = p_sys->track[i_stream_number] ) == NULL )
540 "undeclared stream[Id 0x%x]", i_stream_number );
541 i_skip += i_payload_data_length;
542 continue; // over payload
547 i_skip += i_payload_data_length;
552 for( i_payload_data_pos = 0;
553 i_payload_data_pos < i_payload_data_length &&
554 i_packet_size_left > 0;
555 i_payload_data_pos += i_sub_payload_data_length )
560 // read sub payload length
561 if( i_replicated_data_length == 1 )
563 i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
564 i_payload_data_pos++;
568 i_sub_payload_data_length = i_payload_data_length;
571 /* FIXME I don't use i_media_object_number, sould I ? */
572 if( tk->p_frame && i_media_object_offset == 0 )
574 /* send complete packet to decoder */
575 block_t *p_gather = block_ChainGather( tk->p_frame );
577 es_out_Send( p_demux->out, tk->p_es, p_gather );
582 i_read = i_sub_payload_data_length + i_skip;
583 if( ( p_frag = stream_Block( p_demux->s, i_read ) ) == NULL )
585 msg_Warn( p_demux, "cannot read data" );
588 i_packet_size_left -= i_read;
590 p_frag->p_buffer += i_skip;
591 p_frag->i_buffer -= i_skip;
593 if( tk->p_frame == NULL )
596 ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
598 p_frag->i_pts = tk->i_time;
600 if( tk->i_cat != VIDEO_ES )
601 p_frag->i_dts = p_frag->i_pts;
604 p_frag->i_dts = p_frag->i_pts;
609 block_ChainAppend( &tk->p_frame, p_frag );
612 if( i_packet_size_left > 0 )
614 if( stream_Peek( p_demux->s, &p_peek, i_packet_size_left )
615 < i_packet_size_left )
617 msg_Warn( p_demux, "cannot peek, EOF ?" );
624 if( i_packet_size_left > 0 )
626 msg_Warn( p_demux, "Didn't read %d bytes in the packet",
627 i_packet_size_left );
628 if( stream_Read( p_demux->s, NULL, i_packet_size_left )
629 < i_packet_size_left )
631 msg_Err( p_demux, "cannot skip data, EOF ?" );
636 if( i_packet_padding_length > 0 )
637 if( stream_Read( p_demux->s, NULL, i_packet_padding_length )
638 < i_packet_padding_length )
640 msg_Err( p_demux, "cannot skip padding data, EOF ?" );
647 msg_Warn( p_demux, "unsupported packet header" );
648 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
650 msg_Err( p_demux, "unsupported packet header, fatal error" );
653 stream_Read( p_demux->s, NULL, i_data_packet_min );
658 /*****************************************************************************
660 *****************************************************************************/
661 static int DemuxInit( demux_t *p_demux )
663 demux_sys_t *p_sys = p_demux->p_sys;
664 vlc_bool_t b_seekable;
665 unsigned int i_stream, i;
666 asf_object_content_description_t *p_cd;
667 asf_object_index_t *p_index;
673 p_sys->i_bitrate = 0;
674 p_sys->p_root = NULL;
678 for( i = 0; i < 128; i++ )
680 p_sys->track[i] = NULL;
682 p_sys->i_data_begin = -1;
683 p_sys->i_data_end = -1;
686 /* Now load all object ( except raw data ) */
687 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
688 if( !(p_sys->p_root = ASF_ReadObjectRoot(p_demux->s, b_seekable)) )
690 msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
693 p_sys->p_fp = p_sys->p_root->p_fp;
695 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
697 msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
701 p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
702 &asf_object_stream_properties_guid );
703 if( p_sys->i_track <= 0 )
705 msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
709 /* check if index is available */
710 p_index = ASF_FindObject( p_sys->p_root, &asf_object_index_guid, 0 );
711 b_index = p_index && p_index->i_index_entry_count;
713 msg_Dbg( p_demux, "found %d streams", p_sys->i_track );
715 for( i_stream = 0; i_stream < p_sys->i_track; i_stream ++ )
718 asf_object_stream_properties_t *p_sp;
719 vlc_bool_t b_access_selected;
721 p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
722 &asf_object_stream_properties_guid,
725 tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
726 memset( tk, 0, sizeof( asf_track_t ) );
733 /* Check (in case of mms) if this track is selected (ie will receive data) */
734 if( !stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_GET_PRIVATE_ID_STATE,
735 p_sp->i_stream_number, &b_access_selected ) &&
738 tk->i_cat = UNKNOWN_ES;
739 msg_Dbg( p_demux, "ignoring not selected stream(ID:%d) (by access)",
740 p_sp->i_stream_number );
744 if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
745 p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
748 uint8_t *p_data = p_sp->p_type_specific_data;
751 es_format_Init( &fmt, AUDIO_ES, 0 );
752 i_format = GetWLE( &p_data[0] );
753 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
754 fmt.audio.i_channels = GetWLE( &p_data[2] );
755 fmt.audio.i_rate = GetDWLE( &p_data[4] );
756 fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
757 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
758 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
760 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
761 i_format != WAVE_FORMAT_MPEGLAYER3 &&
762 i_format != WAVE_FORMAT_MPEG )
764 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
765 p_sp->i_type_specific_data_length -
766 sizeof( WAVEFORMATEX ) );
767 fmt.p_extra = malloc( fmt.i_extra );
768 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
772 tk->i_cat = AUDIO_ES;
773 tk->p_es = es_out_Add( p_demux->out, &fmt );
774 es_format_Clean( &fmt );
776 msg_Dbg( p_demux, "added new audio stream(codec:0x%x,ID:%d)",
777 GetWLE( p_data ), p_sp->i_stream_number );
779 else if( ASF_CmpGUID( &p_sp->i_stream_type,
780 &asf_object_stream_type_video ) &&
781 p_sp->i_type_specific_data_length >= 11 +
782 sizeof( BITMAPINFOHEADER ) )
785 uint8_t *p_data = &p_sp->p_type_specific_data[11];
787 es_format_Init( &fmt, VIDEO_ES,
788 VLC_FOURCC( p_data[16], p_data[17],
789 p_data[18], p_data[19] ) );
790 fmt.video.i_width = GetDWLE( p_data + 4 );
791 fmt.video.i_height= GetDWLE( p_data + 8 );
794 if( fmt.i_codec == VLC_FOURCC( 'D','V','R',' ') )
796 /* DVR-MS special ASF */
797 fmt.i_codec = VLC_FOURCC( 'm','p','g','2' ) ;
798 fmt.b_packetized = VLC_FALSE;
801 if( p_sp->i_type_specific_data_length > 11 +
802 sizeof( BITMAPINFOHEADER ) )
804 fmt.i_extra = __MIN( GetDWLE( p_data ),
805 p_sp->i_type_specific_data_length - 11 -
806 sizeof( BITMAPINFOHEADER ) );
807 fmt.p_extra = malloc( fmt.i_extra );
808 memcpy( fmt.p_extra, &p_data[sizeof( BITMAPINFOHEADER )],
812 /* Look for an aspect ratio */
813 if( p_sys->p_root->p_metadata )
815 asf_object_metadata_t *p_meta = p_sys->p_root->p_metadata;
816 int i_aspect_x = 0, i_aspect_y = 0;
819 for( i = 0; i < p_meta->i_record_entries_count; i++ )
821 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioX" ) )
823 if( (!i_aspect_x && !p_meta->record[i].i_stream) ||
824 p_meta->record[i].i_stream ==
825 p_sp->i_stream_number )
826 i_aspect_x = p_meta->record[i].i_val;
828 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioY" ) )
830 if( (!i_aspect_y && !p_meta->record[i].i_stream) ||
831 p_meta->record[i].i_stream ==
832 p_sp->i_stream_number )
833 i_aspect_y = p_meta->record[i].i_val;
837 if( i_aspect_x && i_aspect_y )
839 fmt.video.i_aspect = i_aspect_x *
840 (int64_t)fmt.video.i_width * VOUT_ASPECT_FACTOR /
841 fmt.video.i_height / i_aspect_y;
845 tk->i_cat = VIDEO_ES;
846 tk->p_es = es_out_Add( p_demux->out, &fmt );
847 es_format_Clean( &fmt );
849 /* If there is a video track then use the index for seeking */
850 p_sys->b_index = b_index;
852 msg_Dbg( p_demux, "added new video stream(ID:%d)",
853 p_sp->i_stream_number );
855 else if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_extended_stream_header ) &&
856 p_sp->i_type_specific_data_length >= 64 )
858 /* Now follows a 64 byte header of which we don't know much */
860 guid_t *p_ref = (guid_t *)p_sp->p_type_specific_data;
861 uint8_t *p_data = p_sp->p_type_specific_data + 64;
862 unsigned int i_data = p_sp->i_type_specific_data_length - 64;
864 msg_Dbg( p_demux, "Ext stream header detected. datasize = %d", p_sp->i_type_specific_data_length );
865 if( ASF_CmpGUID( p_ref, &asf_object_extended_stream_type_audio ) &&
866 i_data >= sizeof( WAVEFORMATEX ) - 2)
869 es_format_Init( &fmt, AUDIO_ES, 0 );
870 i_format = GetWLE( &p_data[0] );
872 fmt.i_codec = VLC_FOURCC( 'a','5','2',' ');
874 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
875 fmt.audio.i_channels = GetWLE( &p_data[2] );
876 fmt.audio.i_rate = GetDWLE( &p_data[4] );
877 fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
878 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
879 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
880 fmt.b_packetized = VLC_TRUE;
882 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
883 i_format != WAVE_FORMAT_MPEGLAYER3 &&
884 i_format != WAVE_FORMAT_MPEG )
886 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
887 p_sp->i_type_specific_data_length -
888 sizeof( WAVEFORMATEX ) );
889 fmt.p_extra = malloc( fmt.i_extra );
890 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
894 tk->i_cat = AUDIO_ES;
895 tk->p_es = es_out_Add( p_demux->out, &fmt );
896 es_format_Clean( &fmt );
898 msg_Dbg( p_demux, "added new audio stream (codec:0x%x,ID:%d)",
899 i_format, p_sp->i_stream_number );
904 tk->i_cat = UNKNOWN_ES;
905 msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
906 p_sp->i_stream_number );
910 p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
911 if( p_sys->p_root->p_data->i_object_size != 0 )
913 p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
914 p_sys->p_root->p_data->i_object_size;
917 { /* live/broacast */
918 p_sys->i_data_end = -1;
921 /* go to first packet */
922 stream_Seek( p_demux->s, p_sys->i_data_begin );
924 /* try to calculate movie time */
925 if( p_sys->p_fp->i_data_packets_count > 0 )
928 int64_t i_size = stream_Size( p_demux->s );
930 if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
932 i_size = p_sys->i_data_end;
935 /* real number of packets */
936 i_count = ( i_size - p_sys->i_data_begin ) /
937 p_sys->p_fp->i_min_data_packet_size;
939 /* calculate the time duration in micro-s */
940 p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
942 (mtime_t)p_sys->p_fp->i_data_packets_count;
944 if( p_sys->i_length > 0 )
946 p_sys->i_bitrate = 8 * i_size * (int64_t)1000000 / p_sys->i_length;
950 /* Create meta information */
951 p_sys->meta = vlc_meta_New();
953 if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
954 &asf_object_content_description_guid, 0 ) ) )
956 if( p_cd->psz_title && *p_cd->psz_title )
958 vlc_meta_SetTitle( p_sys->meta, p_cd->psz_title );
960 if( p_cd->psz_artist && *p_cd->psz_artist )
962 vlc_meta_SetArtist( p_sys->meta, p_cd->psz_artist );
964 if( p_cd->psz_copyright && *p_cd->psz_copyright )
966 vlc_meta_SetCopyright( p_sys->meta, p_cd->psz_copyright );
968 if( p_cd->psz_description && *p_cd->psz_description )
970 vlc_meta_SetDescription( p_sys->meta, p_cd->psz_description );
972 if( p_cd->psz_rating && *p_cd->psz_rating )
974 vlc_meta_SetRating( p_sys->meta, p_cd->psz_rating );
977 /// \tood Fix Child meta for ASF tracks
979 for( i_stream = 0, i = 0; i < 128; i++ )
981 asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
982 &asf_object_codec_list_guid, 0 );
984 if( p_sys->track[i] )
986 vlc_meta_t *tk = vlc_meta_New();
987 TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
989 if( p_cl && i_stream < p_cl->i_codec_entries_count )
991 if( p_cl->codec[i_stream].psz_name &&
992 *p_cl->codec[i_stream].psz_name )
994 vlc_meta_Add( tk, VLC_META_CODEC_NAME,
995 p_cl->codec[i_stream].psz_name );
997 if( p_cl->codec[i_stream].psz_description &&
998 *p_cl->codec[i_stream].psz_description )
1000 vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
1001 p_cl->codec[i_stream].psz_description );
1009 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1013 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1014 return VLC_EGENERIC;
1016 /*****************************************************************************
1018 *****************************************************************************/
1019 static void DemuxEnd( demux_t *p_demux )
1021 demux_sys_t *p_sys = p_demux->p_sys;
1026 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1027 p_sys->p_root = NULL;
1031 vlc_meta_Delete( p_sys->meta );
1035 for( i = 0; i < 128; i++ )
1037 asf_track_t *tk = p_sys->track[i];
1043 block_ChainRelease( tk->p_frame );
1047 es_out_Del( p_demux->out, tk->p_es );
1051 p_sys->track[i] = 0;