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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35 #include <vlc_interface.h>
38 #include <vlc_access.h> /* GET_PRIVATE_ID_STATE */
39 #include <vlc_codecs.h> /* BITMAPINFOHEADER, WAVEFORMATEX */
43 * - add support for the newly added object: language, bitrate,
44 * extended stream properties.
47 /*****************************************************************************
49 *****************************************************************************/
50 static int Open ( vlc_object_t * );
51 static void Close ( vlc_object_t * );
54 set_category( CAT_INPUT );
55 set_subcategory( SUBCAT_INPUT_DEMUX );
56 set_description( N_("ASF v1.0 demuxer") );
57 set_capability( "demux", 200 );
58 set_callbacks( Open, Close );
59 add_shortcut( "asf" );
63 /*****************************************************************************
65 *****************************************************************************/
66 static int Demux ( demux_t * );
67 static int Control( demux_t *, int i_query, va_list args );
75 asf_object_stream_properties_t *p_sp;
79 block_t *p_frame; /* use to gather complete frame */
85 mtime_t i_time; /* s */
86 mtime_t i_length; /* length of file file */
87 int64_t i_bitrate; /* global file bitrate */
89 asf_object_root_t *p_root;
90 asf_object_file_properties_t *p_fp;
93 asf_track_t *track[128]; /* track number is stored on 7 bits */
102 static mtime_t GetMoviePTS( demux_sys_t * );
103 static int DemuxInit( demux_t * );
104 static void DemuxEnd( demux_t * );
105 static int DemuxPacket( demux_t * );
107 /*****************************************************************************
108 * Open: check file and initializes ASF structures
109 *****************************************************************************/
110 static int Open( vlc_object_t * p_this )
112 demux_t *p_demux = (demux_t *)p_this;
115 const uint8_t *p_peek;
117 /* A little test to see if it could be a asf stream */
118 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 ) return VLC_EGENERIC;
120 ASF_GetGUID( &guid, p_peek );
121 if( !ASF_CmpGUID( &guid, &asf_object_header_guid ) ) return VLC_EGENERIC;
123 /* Set p_demux fields */
124 p_demux->pf_demux = Demux;
125 p_demux->pf_control = Control;
126 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
127 memset( p_sys, 0, sizeof( demux_sys_t ) );
129 /* Load the headers */
130 if( DemuxInit( p_demux ) )
139 /*****************************************************************************
140 * Demux: read packet and send them to decoders
141 *****************************************************************************/
142 static int Demux( demux_t *p_demux )
144 demux_sys_t *p_sys = p_demux->p_sys;
148 const uint8_t *p_peek;
150 mtime_t i_time_begin = GetMoviePTS( p_sys );
153 if( !vlc_object_alive (p_demux) )
156 /* FIXME: returns EOF too early for some mms streams */
157 if( p_sys->i_data_end >= 0 &&
158 stream_Tell( p_demux->s ) >= p_sys->i_data_end )
162 /* Check if we have concatenated files */
163 if( stream_Peek( p_demux->s, &p_peek, 16 ) == 16 )
167 ASF_GetGUID( &guid, p_peek );
168 if( ASF_CmpGUID( &guid, &asf_object_header_guid ) )
170 msg_Warn( p_demux, "found a new ASF header" );
171 /* We end this stream */
174 /* And we prepare to read the next one */
175 if( DemuxInit( p_demux ) )
177 msg_Err( p_demux, "failed to load the new header" );
178 intf_UserFatal( p_demux, false, _("Could not demux ASF stream"),
179 _("VLC failed to load the ASF header.") );
186 /* Read and demux a packet */
187 if( ( i_result = DemuxPacket( p_demux ) ) <= 0 )
191 if( i_time_begin == -1 )
193 i_time_begin = GetMoviePTS( p_sys );
197 i_length = GetMoviePTS( p_sys ) - i_time_begin;
198 if( i_length < 0 || i_length >= 40 * 1000 ) break;
203 p_sys->i_time = GetMoviePTS( p_sys );
204 if( p_sys->i_time >= 0 )
206 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
212 /*****************************************************************************
213 * Close: frees unused data
214 *****************************************************************************/
215 static void Close( vlc_object_t * p_this )
217 demux_t *p_demux = (demux_t *)p_this;
221 free( p_demux->p_sys );
224 /*****************************************************************************
225 * SeekIndex: goto to i_date or i_percent
226 *****************************************************************************/
227 static int SeekIndex( demux_t *p_demux, mtime_t i_date, float f_pos )
229 demux_sys_t *p_sys = p_demux->p_sys;
230 asf_object_index_t *p_index;
233 msg_Dbg( p_demux, "seek with index: %i seconds, position %f",
234 (int)(i_date/1000000), f_pos );
236 p_index = ASF_FindObject( p_sys->p_root, &asf_object_index_guid, 0 );
238 if( i_date < 0 ) i_date = p_sys->i_length * f_pos;
240 i_pos = i_date * 10 / p_index->i_index_entry_time_interval;
241 i_pos = p_index->index_entry[i_pos].i_packet_number *
242 p_sys->p_fp->i_min_data_packet_size;
244 return stream_Seek( p_demux->s, p_sys->i_data_begin + i_pos );
247 /*****************************************************************************
249 *****************************************************************************/
250 static int Control( demux_t *p_demux, int i_query, va_list args )
252 demux_sys_t *p_sys = p_demux->p_sys;
260 case DEMUX_GET_LENGTH:
261 pi64 = (int64_t*)va_arg( args, int64_t * );
262 *pi64 = p_sys->i_length;
266 pi64 = (int64_t*)va_arg( args, int64_t * );
267 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
268 *pi64 = p_sys->i_time;
273 for( i = 0; i < 128 ; i++ )
274 if( p_sys->track[i] ) p_sys->track[i]->i_time = -1;
276 if( p_sys->b_index && p_sys->i_length > 0 )
278 i64 = (int64_t)va_arg( args, int64_t );
279 return SeekIndex( p_demux, i64, -1 );
283 return demux_vaControlHelper( p_demux->s, p_sys->i_data_begin,
284 p_sys->i_data_end, p_sys->i_bitrate,
285 p_sys->p_fp->i_min_data_packet_size,
289 case DEMUX_GET_POSITION:
290 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
291 if( p_sys->i_length > 0 )
293 pf = (double*)va_arg( args, double * );
294 *pf = p_sys->i_time / (double)p_sys->i_length;
297 return demux_vaControlHelper( p_demux->s, p_sys->i_data_begin,
298 p_sys->i_data_end, p_sys->i_bitrate,
299 p_sys->p_fp->i_min_data_packet_size,
302 case DEMUX_SET_POSITION:
304 for( i = 0; i < 128 ; i++ )
305 if( p_sys->track[i] ) p_sys->track[i]->i_time = -1;
307 if( p_sys->b_index && p_sys->i_length > 0 )
309 f = (double)va_arg( args, double );
310 return SeekIndex( p_demux, -1, f );
314 return demux_vaControlHelper( p_demux->s, p_sys->i_data_begin,
315 p_sys->i_data_end, p_sys->i_bitrate,
316 p_sys->p_fp->i_min_data_packet_size,
321 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
322 vlc_meta_Merge( p_meta, p_sys->meta );
326 return demux_vaControlHelper( p_demux->s, p_sys->i_data_begin,
327 p_sys->i_data_end, p_sys->i_bitrate,
328 p_sys->p_fp->i_min_data_packet_size,
333 /*****************************************************************************
335 *****************************************************************************/
336 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
341 for( i = 0; i < 128 ; i++ )
343 asf_track_t *tk = p_sys->track[i];
345 if( tk && tk->p_es && tk->i_time > 0)
347 if( i_time < 0 ) i_time = tk->i_time;
348 else i_time = __MIN( i_time, tk->i_time );
355 #define GETVALUE2b( bits, var, def ) \
356 switch( (bits)&0x03 ) \
358 case 1: var = p_peek[i_skip]; i_skip++; break; \
359 case 2: var = GetWLE( p_peek + i_skip ); i_skip+= 2; break; \
360 case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
362 default: var = def; break;\
365 static int DemuxPacket( demux_t *p_demux )
367 demux_sys_t *p_sys = p_demux->p_sys;
368 int i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
369 const uint8_t *p_peek;
372 int i_packet_size_left;
374 int i_packet_property;
376 int b_packet_multiple_payload;
378 int i_packet_sequence;
379 int i_packet_padding_length;
381 uint32_t i_packet_send_time;
382 uint16_t i_packet_duration;
385 int i_payload_length_type;
388 if( stream_Peek( p_demux->s, &p_peek,i_data_packet_min)<i_data_packet_min )
390 msg_Warn( p_demux, "cannot peek while getting new packet, EOF ?" );
395 /* *** parse error correction if present *** */
398 unsigned int i_error_correction_length_type;
399 unsigned int i_error_correction_data_length;
400 unsigned int i_opaque_data_present;
402 i_error_correction_data_length = p_peek[0] & 0x0f; // 4bits
403 i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01; // 1bit
404 i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
405 i_skip += 1; // skip error correction flags
407 if( i_error_correction_length_type != 0x00 ||
408 i_opaque_data_present != 0 ||
409 i_error_correction_data_length != 0x02 )
411 goto loop_error_recovery;
414 i_skip += i_error_correction_data_length;
418 msg_Warn( p_demux, "p_peek[0]&0x80 != 0x80" );
422 if( i_skip + 2 >= i_data_packet_min )
424 goto loop_error_recovery;
427 i_packet_flags = p_peek[i_skip]; i_skip++;
428 i_packet_property = p_peek[i_skip]; i_skip++;
430 b_packet_multiple_payload = i_packet_flags&0x01;
432 /* read some value */
433 GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
434 GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
435 GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
437 if( i_packet_padding_length > i_packet_length )
439 msg_Warn( p_demux, "Too large padding: %d", i_packet_padding_length );
440 goto loop_error_recovery;
443 i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
444 i_packet_duration = GetWLE( p_peek + i_skip ); i_skip += 2;
446 /* FIXME I have to do that for some file, I don't known why */
447 i_packet_size_left = i_data_packet_min /*i_packet_length*/ ;
449 if( b_packet_multiple_payload )
451 i_payload_count = p_peek[i_skip] & 0x3f;
452 i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
458 i_payload_length_type = 0x02; // unused
461 for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
465 int i_packet_keyframe;
467 int i_media_object_number;
468 int i_media_object_offset;
469 int i_replicated_data_length;
470 int i_payload_data_length;
471 int i_payload_data_pos;
472 int i_sub_payload_data_length;
478 if( i_skip >= i_packet_size_left )
480 /* prevent some segfault with invalid file */
484 i_packet_keyframe = p_peek[i_skip] >> 7;
485 i_stream_number = p_peek[i_skip++] & 0x7f;
487 GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
488 GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
489 GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
491 if( i_replicated_data_length > 1 ) // should be at least 8 bytes
493 i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
494 i_skip += i_replicated_data_length;
497 i_media_object_offset = i_tmp;
499 if( i_skip >= i_packet_size_left )
504 else if( i_replicated_data_length == 1 )
506 /* msg_Dbg( p_demux, "found compressed payload" ); */
508 i_pts = (mtime_t)i_tmp * 1000;
509 i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
511 i_media_object_offset = 0;
515 i_pts = (mtime_t)i_packet_send_time * 1000;
518 i_media_object_offset = i_tmp;
521 i_pts = __MAX( i_pts - p_sys->p_fp->i_preroll * 1000, 0 );
522 if( b_packet_multiple_payload )
524 GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
528 i_payload_data_length = i_packet_length -
529 i_packet_padding_length - i_skip;
532 if( i_payload_data_length < 0 || i_payload_data_length > i_packet_size_left )
538 "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
539 i_payload + 1, i_payload_count, i_stream_number, i_media_object_number,
540 i_media_object_offset, i_replicated_data_length, i_payload_data_length );
543 if( ( tk = p_sys->track[i_stream_number] ) == NULL )
546 "undeclared stream[Id 0x%x]", i_stream_number );
547 i_skip += i_payload_data_length;
548 continue; // over payload
553 i_skip += i_payload_data_length;
558 for( i_payload_data_pos = 0;
559 i_payload_data_pos < i_payload_data_length &&
560 i_packet_size_left > 0;
561 i_payload_data_pos += i_sub_payload_data_length )
566 // read sub payload length
567 if( i_replicated_data_length == 1 )
569 i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
570 i_payload_data_pos++;
574 i_sub_payload_data_length = i_payload_data_length;
577 /* FIXME I don't use i_media_object_number, sould I ? */
578 if( tk->p_frame && i_media_object_offset == 0 )
580 /* send complete packet to decoder */
581 block_t *p_gather = block_ChainGather( tk->p_frame );
583 es_out_Send( p_demux->out, tk->p_es, p_gather );
588 i_read = i_sub_payload_data_length + i_skip;
589 if( ( p_frag = stream_Block( p_demux->s, i_read ) ) == NULL )
591 msg_Warn( p_demux, "cannot read data" );
594 i_packet_size_left -= i_read;
596 p_frag->p_buffer += i_skip;
597 p_frag->i_buffer -= i_skip;
599 if( tk->p_frame == NULL )
602 ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
604 p_frag->i_pts = tk->i_time;
606 if( tk->i_cat != VIDEO_ES )
607 p_frag->i_dts = p_frag->i_pts;
610 p_frag->i_dts = p_frag->i_pts;
615 block_ChainAppend( &tk->p_frame, p_frag );
618 if( i_packet_size_left > 0 )
620 if( stream_Peek( p_demux->s, &p_peek, i_packet_size_left )
621 < i_packet_size_left )
623 msg_Warn( p_demux, "cannot peek, EOF ?" );
630 if( i_packet_size_left > 0 )
633 if( i_packet_size_left > i_packet_padding_length )
634 msg_Warn( p_demux, "Didn't read %d bytes in the packet",
635 i_packet_size_left - i_packet_padding_length );
636 else if( i_packet_size_left < i_packet_padding_length )
637 msg_Warn( p_demux, "Read %d too much bytes in the packet",
638 i_packet_padding_length - i_packet_size_left );
640 if( stream_Read( p_demux->s, NULL, i_packet_size_left )
641 < i_packet_size_left )
643 msg_Err( p_demux, "cannot skip data, EOF ?" );
651 msg_Warn( p_demux, "unsupported packet header" );
652 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
654 msg_Err( p_demux, "unsupported packet header, fatal error" );
657 if( stream_Read( p_demux->s, NULL, i_data_packet_min ) != i_data_packet_min )
659 msg_Warn( p_demux, "cannot skip data, EOF ?" );
666 /*****************************************************************************
668 *****************************************************************************/
669 static int DemuxInit( demux_t *p_demux )
671 demux_sys_t *p_sys = p_demux->p_sys;
673 unsigned int i_stream, i;
674 asf_object_content_description_t *p_cd;
675 asf_object_index_t *p_index;
681 p_sys->i_bitrate = 0;
682 p_sys->p_root = NULL;
686 for( i = 0; i < 128; i++ )
688 p_sys->track[i] = NULL;
690 p_sys->i_data_begin = -1;
691 p_sys->i_data_end = -1;
694 /* Now load all object ( except raw data ) */
695 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
696 if( !(p_sys->p_root = ASF_ReadObjectRoot(p_demux->s, b_seekable)) )
698 msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
701 p_sys->p_fp = p_sys->p_root->p_fp;
703 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
705 msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
709 p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
710 &asf_object_stream_properties_guid );
711 if( p_sys->i_track <= 0 )
713 msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
717 /* check if index is available */
718 p_index = ASF_FindObject( p_sys->p_root, &asf_object_index_guid, 0 );
719 b_index = p_index && p_index->i_index_entry_count;
721 msg_Dbg( p_demux, "found %d streams", p_sys->i_track );
723 for( i_stream = 0; i_stream < p_sys->i_track; i_stream ++ )
726 asf_object_stream_properties_t *p_sp;
727 bool b_access_selected;
729 p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
730 &asf_object_stream_properties_guid,
733 tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
734 memset( tk, 0, sizeof( asf_track_t ) );
741 /* Check (in case of mms) if this track is selected (ie will receive data) */
742 if( !stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_GET_PRIVATE_ID_STATE,
743 p_sp->i_stream_number, &b_access_selected ) &&
746 tk->i_cat = UNKNOWN_ES;
747 msg_Dbg( p_demux, "ignoring not selected stream(ID:%d) (by access)",
748 p_sp->i_stream_number );
752 if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
753 p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
756 uint8_t *p_data = p_sp->p_type_specific_data;
759 es_format_Init( &fmt, AUDIO_ES, 0 );
760 i_format = GetWLE( &p_data[0] );
761 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
762 fmt.audio.i_channels = GetWLE( &p_data[2] );
763 fmt.audio.i_rate = GetDWLE( &p_data[4] );
764 fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
765 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
766 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
768 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
769 i_format != WAVE_FORMAT_MPEGLAYER3 &&
770 i_format != WAVE_FORMAT_MPEG )
772 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
773 p_sp->i_type_specific_data_length -
774 sizeof( WAVEFORMATEX ) );
775 fmt.p_extra = malloc( fmt.i_extra );
776 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
780 tk->i_cat = AUDIO_ES;
781 tk->p_es = es_out_Add( p_demux->out, &fmt );
782 es_format_Clean( &fmt );
784 msg_Dbg( p_demux, "added new audio stream(codec:0x%x,ID:%d)",
785 GetWLE( p_data ), p_sp->i_stream_number );
787 else if( ASF_CmpGUID( &p_sp->i_stream_type,
788 &asf_object_stream_type_video ) &&
789 p_sp->i_type_specific_data_length >= 11 +
790 sizeof( BITMAPINFOHEADER ) )
793 uint8_t *p_data = &p_sp->p_type_specific_data[11];
795 es_format_Init( &fmt, VIDEO_ES,
796 VLC_FOURCC( p_data[16], p_data[17],
797 p_data[18], p_data[19] ) );
798 fmt.video.i_width = GetDWLE( p_data + 4 );
799 fmt.video.i_height= GetDWLE( p_data + 8 );
802 if( fmt.i_codec == VLC_FOURCC( 'D','V','R',' ') )
804 /* DVR-MS special ASF */
805 fmt.i_codec = VLC_FOURCC( 'm','p','g','2' ) ;
806 fmt.b_packetized = false;
809 if( p_sp->i_type_specific_data_length > 11 +
810 sizeof( BITMAPINFOHEADER ) )
812 fmt.i_extra = __MIN( GetDWLE( p_data ),
813 p_sp->i_type_specific_data_length - 11 -
814 sizeof( BITMAPINFOHEADER ) );
815 fmt.p_extra = malloc( fmt.i_extra );
816 memcpy( fmt.p_extra, &p_data[sizeof( BITMAPINFOHEADER )],
820 /* Look for an aspect ratio */
821 if( p_sys->p_root->p_metadata )
823 asf_object_metadata_t *p_meta = p_sys->p_root->p_metadata;
824 int i_aspect_x = 0, i_aspect_y = 0;
827 for( i = 0; i < p_meta->i_record_entries_count; i++ )
829 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioX" ) )
831 if( (!i_aspect_x && !p_meta->record[i].i_stream) ||
832 p_meta->record[i].i_stream ==
833 p_sp->i_stream_number )
834 i_aspect_x = p_meta->record[i].i_val;
836 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioY" ) )
838 if( (!i_aspect_y && !p_meta->record[i].i_stream) ||
839 p_meta->record[i].i_stream ==
840 p_sp->i_stream_number )
841 i_aspect_y = p_meta->record[i].i_val;
845 if( i_aspect_x && i_aspect_y )
847 fmt.video.i_aspect = i_aspect_x *
848 (int64_t)fmt.video.i_width * VOUT_ASPECT_FACTOR /
849 fmt.video.i_height / i_aspect_y;
853 tk->i_cat = VIDEO_ES;
854 tk->p_es = es_out_Add( p_demux->out, &fmt );
855 es_format_Clean( &fmt );
857 /* If there is a video track then use the index for seeking */
858 p_sys->b_index = b_index;
860 msg_Dbg( p_demux, "added new video stream(ID:%d)",
861 p_sp->i_stream_number );
863 else if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_extended_stream_header ) &&
864 p_sp->i_type_specific_data_length >= 64 )
866 /* Now follows a 64 byte header of which we don't know much */
868 guid_t *p_ref = (guid_t *)p_sp->p_type_specific_data;
869 uint8_t *p_data = p_sp->p_type_specific_data + 64;
870 unsigned int i_data = p_sp->i_type_specific_data_length - 64;
872 msg_Dbg( p_demux, "Ext stream header detected. datasize = %d", p_sp->i_type_specific_data_length );
873 if( ASF_CmpGUID( p_ref, &asf_object_extended_stream_type_audio ) &&
874 i_data >= sizeof( WAVEFORMATEX ) - 2)
877 es_format_Init( &fmt, AUDIO_ES, 0 );
878 i_format = GetWLE( &p_data[0] );
880 fmt.i_codec = VLC_FOURCC( 'a','5','2',' ');
882 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
883 fmt.audio.i_channels = GetWLE( &p_data[2] );
884 fmt.audio.i_rate = GetDWLE( &p_data[4] );
885 fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
886 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
887 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
888 fmt.b_packetized = true;
890 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
891 i_format != WAVE_FORMAT_MPEGLAYER3 &&
892 i_format != WAVE_FORMAT_MPEG )
894 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
895 p_sp->i_type_specific_data_length -
896 sizeof( WAVEFORMATEX ) );
897 fmt.p_extra = malloc( fmt.i_extra );
898 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
902 tk->i_cat = AUDIO_ES;
903 tk->p_es = es_out_Add( p_demux->out, &fmt );
904 es_format_Clean( &fmt );
906 msg_Dbg( p_demux, "added new audio stream (codec:0x%x,ID:%d)",
907 i_format, p_sp->i_stream_number );
912 tk->i_cat = UNKNOWN_ES;
913 msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
914 p_sp->i_stream_number );
918 p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
919 if( p_sys->p_root->p_data->i_object_size != 0 )
921 p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
922 p_sys->p_root->p_data->i_object_size;
925 { /* live/broacast */
926 p_sys->i_data_end = -1;
929 /* go to first packet */
930 stream_Seek( p_demux->s, p_sys->i_data_begin );
932 /* try to calculate movie time */
933 if( p_sys->p_fp->i_data_packets_count > 0 )
936 int64_t i_size = stream_Size( p_demux->s );
938 if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
940 i_size = p_sys->i_data_end;
943 /* real number of packets */
944 i_count = ( i_size - p_sys->i_data_begin ) /
945 p_sys->p_fp->i_min_data_packet_size;
947 /* calculate the time duration in micro-s */
948 p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
950 (mtime_t)p_sys->p_fp->i_data_packets_count;
952 if( p_sys->i_length > 0 )
954 p_sys->i_bitrate = 8 * i_size * (int64_t)1000000 / p_sys->i_length;
958 /* Create meta information */
959 p_sys->meta = vlc_meta_New();
961 if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
962 &asf_object_content_description_guid, 0 ) ) )
964 if( p_cd->psz_title && *p_cd->psz_title )
966 vlc_meta_SetTitle( p_sys->meta, p_cd->psz_title );
968 if( p_cd->psz_artist && *p_cd->psz_artist )
970 vlc_meta_SetArtist( p_sys->meta, p_cd->psz_artist );
972 if( p_cd->psz_copyright && *p_cd->psz_copyright )
974 vlc_meta_SetCopyright( p_sys->meta, p_cd->psz_copyright );
976 if( p_cd->psz_description && *p_cd->psz_description )
978 vlc_meta_SetDescription( p_sys->meta, p_cd->psz_description );
980 if( p_cd->psz_rating && *p_cd->psz_rating )
982 vlc_meta_SetRating( p_sys->meta, p_cd->psz_rating );
985 /// \tood Fix Child meta for ASF tracks
987 for( i_stream = 0, i = 0; i < 128; i++ )
989 asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
990 &asf_object_codec_list_guid, 0 );
992 if( p_sys->track[i] )
994 vlc_meta_t *tk = vlc_meta_New();
995 TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
997 if( p_cl && i_stream < p_cl->i_codec_entries_count )
999 if( p_cl->codec[i_stream].psz_name &&
1000 *p_cl->codec[i_stream].psz_name )
1002 vlc_meta_Add( tk, VLC_META_CODEC_NAME,
1003 p_cl->codec[i_stream].psz_name );
1005 if( p_cl->codec[i_stream].psz_description &&
1006 *p_cl->codec[i_stream].psz_description )
1008 vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
1009 p_cl->codec[i_stream].psz_description );
1017 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1021 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1022 return VLC_EGENERIC;
1024 /*****************************************************************************
1026 *****************************************************************************/
1027 static void DemuxEnd( demux_t *p_demux )
1029 demux_sys_t *p_sys = p_demux->p_sys;
1034 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1035 p_sys->p_root = NULL;
1039 vlc_meta_Delete( p_sys->meta );
1043 for( i = 0; i < 128; i++ )
1045 asf_track_t *tk = p_sys->track[i];
1051 block_ChainRelease( tk->p_frame );
1055 es_out_Del( p_demux->out, tk->p_es );
1059 p_sys->track[i] = 0;