+/*****************************************************************************
+ *
+ *****************************************************************************/
+static int DemuxInit( demux_t *p_demux )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+
+ /* init context */
+ p_sys->i_time = -1;
+ p_sys->i_length = 0;
+ p_sys->i_bitrate = 0;
+ p_sys->p_root = NULL;
+ p_sys->p_fp = NULL;
+ p_sys->b_index = 0;
+ p_sys->i_track = 0;
+ for( int i = 0; i < 128; i++ )
+ {
+ p_sys->track[i] = NULL;
+ }
+ p_sys->i_data_begin = -1;
+ p_sys->i_data_end = -1;
+ p_sys->meta = NULL;
+
+ /* Now load all object ( except raw data ) */
+ bool b_seekable;
+ stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
+ if( !(p_sys->p_root = ASF_ReadObjectRoot(p_demux->s, b_seekable)) )
+ {
+ msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
+ return VLC_EGENERIC;
+ }
+ p_sys->p_fp = p_sys->p_root->p_fp;
+
+ if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
+ {
+ msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
+ goto error;
+ }
+
+ p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
+ &asf_object_stream_properties_guid );
+ if( p_sys->i_track <= 0 )
+ {
+ msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
+ goto error;
+ }
+ msg_Dbg( p_demux, "found %d streams", p_sys->i_track );
+
+ /* check if index is available */
+ asf_object_index_t *p_index = ASF_FindObject( p_sys->p_root,
+ &asf_object_index_guid, 0 );
+ const bool b_index = p_index && p_index->i_index_entry_count;
+
+ /* Find the extended header if any */
+ asf_object_t *p_hdr_ext = ASF_FindObject( p_sys->p_root->p_hdr,
+ &asf_object_header_extension_guid, 0 );
+
+ asf_object_language_list_t *p_languages = NULL;
+ if( p_hdr_ext )
+ p_languages = ASF_FindObject( p_hdr_ext, &asf_object_language_list, 0 );
+
+ for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
+ {
+ asf_track_t *tk;
+ asf_object_stream_properties_t *p_sp;
+ asf_object_extended_stream_properties_t *p_esp;
+ bool b_access_selected;
+
+ p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
+ &asf_object_stream_properties_guid,
+ i_stream );
+ p_esp = NULL;
+
+ tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
+ memset( tk, 0, sizeof( asf_track_t ) );
+
+ tk->i_time = -1;
+ tk->p_sp = p_sp;
+ tk->p_es = NULL;
+ tk->p_frame = NULL;
+
+ /* Check (in case of mms) if this track is selected (ie will receive data) */
+ if( !stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_GET_PRIVATE_ID_STATE,
+ p_sp->i_stream_number, &b_access_selected ) &&
+ !b_access_selected )
+ {
+ tk->i_cat = UNKNOWN_ES;
+ msg_Dbg( p_demux, "ignoring not selected stream(ID:%d) (by access)",
+ p_sp->i_stream_number );
+ continue;
+ }
+
+ /* Find the associated extended_stream_properties if any */
+ if( p_hdr_ext )
+ {
+ int i_ext_stream = ASF_CountObject( p_hdr_ext,
+ &asf_object_extended_stream_properties );
+ for( int i = 0; i < i_ext_stream; i++ )
+ {
+ asf_object_t *p_tmp =
+ ASF_FindObject( p_hdr_ext,
+ &asf_object_extended_stream_properties, i );
+ if( p_tmp->ext_stream.i_stream_number == p_sp->i_stream_number )
+ {
+ p_esp = &p_tmp->ext_stream;
+ break;
+ }
+ }
+ }
+
+ es_format_t fmt;
+
+ if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
+ p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
+ {
+ uint8_t *p_data = p_sp->p_type_specific_data;
+ int i_format;
+
+ es_format_Init( &fmt, AUDIO_ES, 0 );
+ i_format = GetWLE( &p_data[0] );
+ wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
+ fmt.audio.i_channels = GetWLE( &p_data[2] );
+ fmt.audio.i_rate = GetDWLE( &p_data[4] );
+ fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
+ fmt.audio.i_blockalign = GetWLE( &p_data[12] );
+ fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
+
+ if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
+ i_format != WAVE_FORMAT_MPEGLAYER3 &&
+ i_format != WAVE_FORMAT_MPEG )
+ {
+ fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
+ p_sp->i_type_specific_data_length -
+ sizeof( WAVEFORMATEX ) );
+ fmt.p_extra = malloc( fmt.i_extra );
+ memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
+ fmt.i_extra );
+ }
+
+ msg_Dbg( p_demux, "added new audio stream(codec:0x%x,ID:%d)",
+ GetWLE( p_data ), p_sp->i_stream_number );
+ }
+ else if( ASF_CmpGUID( &p_sp->i_stream_type,
+ &asf_object_stream_type_video ) &&
+ p_sp->i_type_specific_data_length >= 11 +
+ sizeof( BITMAPINFOHEADER ) )
+ {
+ uint8_t *p_data = &p_sp->p_type_specific_data[11];
+
+ es_format_Init( &fmt, VIDEO_ES,
+ VLC_FOURCC( p_data[16], p_data[17],
+ p_data[18], p_data[19] ) );
+ fmt.video.i_width = GetDWLE( p_data + 4 );
+ fmt.video.i_height= GetDWLE( p_data + 8 );
+
+ if( p_esp && p_esp->i_average_time_per_frame > 0 )
+ {
+ fmt.video.i_frame_rate = 10000000;
+ fmt.video.i_frame_rate_base = p_esp->i_average_time_per_frame;
+ }
+
+ if( fmt.i_codec == VLC_FOURCC( 'D','V','R',' ') )
+ {
+ /* DVR-MS special ASF */
+ fmt.i_codec = VLC_FOURCC( 'm','p','g','2' ) ;
+ fmt.b_packetized = false;
+ }
+
+ if( p_sp->i_type_specific_data_length > 11 +
+ sizeof( BITMAPINFOHEADER ) )
+ {
+ fmt.i_extra = __MIN( GetDWLE( p_data ),
+ p_sp->i_type_specific_data_length - 11 -
+ sizeof( BITMAPINFOHEADER ) );
+ fmt.p_extra = malloc( fmt.i_extra );
+ memcpy( fmt.p_extra, &p_data[sizeof( BITMAPINFOHEADER )],
+ fmt.i_extra );
+ }
+
+ /* Look for an aspect ratio */
+ if( p_sys->p_root->p_metadata )
+ {
+ asf_object_metadata_t *p_meta = p_sys->p_root->p_metadata;
+ int i_aspect_x = 0, i_aspect_y = 0;
+ unsigned int i;
+
+ for( i = 0; i < p_meta->i_record_entries_count; i++ )
+ {
+ if( !strcmp( p_meta->record[i].psz_name, "AspectRatioX" ) )
+ {
+ if( (!i_aspect_x && !p_meta->record[i].i_stream) ||
+ p_meta->record[i].i_stream ==
+ p_sp->i_stream_number )
+ i_aspect_x = p_meta->record[i].i_val;
+ }
+ if( !strcmp( p_meta->record[i].psz_name, "AspectRatioY" ) )
+ {
+ if( (!i_aspect_y && !p_meta->record[i].i_stream) ||
+ p_meta->record[i].i_stream ==
+ p_sp->i_stream_number )
+ i_aspect_y = p_meta->record[i].i_val;
+ }
+ }
+
+ if( i_aspect_x && i_aspect_y )
+ {
+ fmt.video.i_sar_num = i_aspect_x;
+ fmt.video.i_sar_den = i_aspect_y;
+ }
+ }
+
+ /* If there is a video track then use the index for seeking */
+ p_sys->b_index = b_index;
+
+ msg_Dbg( p_demux, "added new video stream(ID:%d)",
+ p_sp->i_stream_number );
+ }
+ else if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_extended_stream_header ) &&
+ p_sp->i_type_specific_data_length >= 64 )
+ {
+ /* Now follows a 64 byte header of which we don't know much */
+ guid_t *p_ref = (guid_t *)p_sp->p_type_specific_data;
+ uint8_t *p_data = p_sp->p_type_specific_data + 64;
+ unsigned int i_data = p_sp->i_type_specific_data_length - 64;
+
+ msg_Dbg( p_demux, "Ext stream header detected. datasize = %d", p_sp->i_type_specific_data_length );
+ if( ASF_CmpGUID( p_ref, &asf_object_extended_stream_type_audio ) &&
+ i_data >= sizeof( WAVEFORMATEX ) - 2)
+ {
+ int i_format;
+ es_format_Init( &fmt, AUDIO_ES, 0 );
+ i_format = GetWLE( &p_data[0] );
+ if( i_format == 0 )
+ fmt.i_codec = VLC_CODEC_A52;
+ else
+ wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
+ fmt.audio.i_channels = GetWLE( &p_data[2] );
+ fmt.audio.i_rate = GetDWLE( &p_data[4] );
+ fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
+ fmt.audio.i_blockalign = GetWLE( &p_data[12] );
+ fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
+ fmt.b_packetized = true;
+
+ if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
+ i_format != WAVE_FORMAT_MPEGLAYER3 &&
+ i_format != WAVE_FORMAT_MPEG )
+ {
+ fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
+ p_sp->i_type_specific_data_length -
+ sizeof( WAVEFORMATEX ) );
+ fmt.p_extra = malloc( fmt.i_extra );
+ memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
+ fmt.i_extra );
+ }
+
+ msg_Dbg( p_demux, "added new audio stream (codec:0x%x,ID:%d)",
+ i_format, p_sp->i_stream_number );
+ }
+ else
+ {
+ es_format_Init( &fmt, UNKNOWN_ES, 0 );
+ }
+ }
+ else
+ {
+ es_format_Init( &fmt, UNKNOWN_ES, 0 );
+ }
+
+ tk->i_cat = fmt.i_cat;
+ if( fmt.i_cat != UNKNOWN_ES )
+ {
+ if( p_esp && p_languages &&
+ p_esp->i_language_index >= 0 &&
+ p_esp->i_language_index < p_languages->i_language )
+ {
+ fmt.psz_language = strdup( p_languages->ppsz_language[p_esp->i_language_index] );
+ char *p;
+ if( fmt.psz_language && (p = strchr( fmt.psz_language, '-' )) )
+ *p = '\0';
+ }
+
+ tk->p_es = es_out_Add( p_demux->out, &fmt );
+ }
+ else
+ {
+ msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
+ p_sp->i_stream_number );
+ }
+ es_format_Clean( &fmt );
+ }
+
+ p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
+ if( p_sys->p_root->p_data->i_object_size != 0 )
+ { /* local file */
+ p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
+ p_sys->p_root->p_data->i_object_size;
+ }
+ else
+ { /* live/broacast */
+ p_sys->i_data_end = -1;
+ }
+
+ /* go to first packet */
+ stream_Seek( p_demux->s, p_sys->i_data_begin );
+
+ /* try to calculate movie time */
+ if( p_sys->p_fp->i_data_packets_count > 0 )
+ {
+ int64_t i_count;
+ int64_t i_size = stream_Size( p_demux->s );
+
+ if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
+ {
+ i_size = p_sys->i_data_end;
+ }
+
+ /* real number of packets */
+ i_count = ( i_size - p_sys->i_data_begin ) /
+ p_sys->p_fp->i_min_data_packet_size;
+
+ /* calculate the time duration in micro-s */
+ p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
+ (mtime_t)i_count /
+ (mtime_t)p_sys->p_fp->i_data_packets_count - p_sys->p_fp->i_preroll * 1000;
+ if( p_sys->i_length < 0 )
+ p_sys->i_length = 0;
+
+ if( p_sys->i_length > 0 )
+ {
+ p_sys->i_bitrate = 8 * i_size * (int64_t)1000000 / p_sys->i_length;
+ }
+ }
+
+ /* Create meta information */
+ p_sys->meta = vlc_meta_New();
+
+ asf_object_content_description_t *p_cd;
+ if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
+ &asf_object_content_description_guid, 0 ) ) )
+ {
+ if( p_cd->psz_title && *p_cd->psz_title )
+ {
+ vlc_meta_SetTitle( p_sys->meta, p_cd->psz_title );
+ }
+ if( p_cd->psz_artist && *p_cd->psz_artist )
+ {
+ vlc_meta_SetArtist( p_sys->meta, p_cd->psz_artist );
+ }
+ if( p_cd->psz_copyright && *p_cd->psz_copyright )
+ {
+ vlc_meta_SetCopyright( p_sys->meta, p_cd->psz_copyright );
+ }
+ if( p_cd->psz_description && *p_cd->psz_description )
+ {
+ vlc_meta_SetDescription( p_sys->meta, p_cd->psz_description );
+ }
+ if( p_cd->psz_rating && *p_cd->psz_rating )
+ {
+ vlc_meta_SetRating( p_sys->meta, p_cd->psz_rating );
+ }
+ }
+ /// \tood Fix Child meta for ASF tracks
+#if 0
+ for( i_stream = 0, i = 0; i < 128; i++ )
+ {
+ asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
+ &asf_object_codec_list_guid, 0 );
+
+ if( p_sys->track[i] )
+ {
+ vlc_meta_t *tk = vlc_meta_New();
+ TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
+
+ if( p_cl && i_stream < p_cl->i_codec_entries_count )
+ {
+ if( p_cl->codec[i_stream].psz_name &&
+ *p_cl->codec[i_stream].psz_name )
+ {
+ vlc_meta_Add( tk, VLC_META_CODEC_NAME,
+ p_cl->codec[i_stream].psz_name );
+ }
+ if( p_cl->codec[i_stream].psz_description &&
+ *p_cl->codec[i_stream].psz_description )
+ {
+ vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
+ p_cl->codec[i_stream].psz_description );
+ }
+ }
+ i_stream++;
+ }
+ }
+#endif
+ return VLC_SUCCESS;
+
+error:
+ ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
+ return VLC_EGENERIC;
+}
+/*****************************************************************************
+ *
+ *****************************************************************************/
+static void DemuxEnd( demux_t *p_demux )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+ int i;
+
+ if( p_sys->p_root )
+ {
+ ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
+ p_sys->p_root = NULL;
+ }
+ if( p_sys->meta )
+ {
+ vlc_meta_Delete( p_sys->meta );
+ p_sys->meta = NULL;
+ }
+
+ for( i = 0; i < 128; i++ )
+ {
+ asf_track_t *tk = p_sys->track[i];
+
+ if( tk )
+ {
+ if( tk->p_frame )
+ {
+ block_ChainRelease( tk->p_frame );
+ }
+ if( tk->p_es )
+ {
+ es_out_Del( p_demux->out, tk->p_es );
+ }
+ free( tk );
+ }
+ p_sys->track[i] = 0;
+ }
+}