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 *****************************************************************************/
29 #include <vlc_demux.h>
30 #include <vlc_interface.h>
33 #include <vlc_access.h> /* GET_PRIVATE_ID_STATE */
34 #include <vlc_codecs.h> /* BITMAPINFOHEADER, WAVEFORMATEX */
38 * - add support for the newly added object: language, bitrate,
39 * extended stream properties.
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close ( vlc_object_t * );
49 set_category( CAT_INPUT );
50 set_subcategory( SUBCAT_INPUT_DEMUX );
51 set_description( _("ASF v1.0 demuxer") );
52 set_capability( "demux2", 200 );
53 set_callbacks( Open, Close );
54 add_shortcut( "asf" );
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Demux ( demux_t * );
62 static int Control( demux_t *, int i_query, va_list args );
70 asf_object_stream_properties_t *p_sp;
74 block_t *p_frame; /* use to gather complete frame */
80 mtime_t i_time; /* s */
81 mtime_t i_length; /* length of file file */
82 int64_t i_bitrate; /* global file bitrate */
84 asf_object_root_t *p_root;
85 asf_object_file_properties_t *p_fp;
88 asf_track_t *track[128];
97 static mtime_t GetMoviePTS( demux_sys_t * );
98 static int DemuxInit( demux_t * );
99 static void DemuxEnd( demux_t * );
100 static int DemuxPacket( demux_t * );
102 /*****************************************************************************
103 * Open: check file and initializes ASF structures
104 *****************************************************************************/
105 static int Open( vlc_object_t * p_this )
107 demux_t *p_demux = (demux_t *)p_this;
110 const uint8_t *p_peek;
112 /* A little test to see if it could be a asf stream */
113 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 ) return VLC_EGENERIC;
115 ASF_GetGUID( &guid, p_peek );
116 if( !ASF_CmpGUID( &guid, &asf_object_header_guid ) ) return VLC_EGENERIC;
118 /* Set p_demux fields */
119 p_demux->pf_demux = Demux;
120 p_demux->pf_control = Control;
121 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
122 memset( p_sys, 0, sizeof( demux_sys_t ) );
124 /* Load the headers */
125 if( DemuxInit( p_demux ) )
134 /*****************************************************************************
135 * Demux: read packet and send them to decoders
136 *****************************************************************************/
137 static int Demux( demux_t *p_demux )
139 demux_sys_t *p_sys = p_demux->p_sys;
143 const uint8_t *p_peek;
145 mtime_t i_time_begin = GetMoviePTS( p_sys );
148 if( p_demux->b_die ) break;
150 /* Check if we have concatenated files */
151 if( stream_Peek( p_demux->s, &p_peek, 16 ) == 16 )
155 ASF_GetGUID( &guid, p_peek );
156 if( ASF_CmpGUID( &guid, &asf_object_header_guid ) )
158 msg_Warn( p_demux, "found a new ASF header" );
159 /* We end this stream */
162 /* And we prepare to read the next one */
163 if( DemuxInit( p_demux ) )
165 msg_Err( p_demux, "failed to load the new header" );
166 intf_UserFatal( p_demux, VLC_FALSE, _("Could not demux ASF stream"),
167 _("VLC failed to load the ASF header.") );
174 /* Read and demux a packet */
175 if( ( i_result = DemuxPacket( p_demux ) ) <= 0 )
179 if( i_time_begin == -1 )
181 i_time_begin = GetMoviePTS( p_sys );
185 i_length = GetMoviePTS( p_sys ) - i_time_begin;
186 if( i_length < 0 || i_length >= 40 * 1000 ) break;
191 p_sys->i_time = GetMoviePTS( p_sys );
192 if( p_sys->i_time >= 0 )
194 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
200 /*****************************************************************************
201 * Close: frees unused data
202 *****************************************************************************/
203 static void Close( vlc_object_t * p_this )
205 demux_t *p_demux = (demux_t *)p_this;
209 free( p_demux->p_sys );
212 /*****************************************************************************
213 * SeekIndex: goto to i_date or i_percent
214 *****************************************************************************/
215 static int SeekIndex( demux_t *p_demux, mtime_t i_date, float f_pos )
217 demux_sys_t *p_sys = p_demux->p_sys;
218 asf_object_index_t *p_index;
221 msg_Dbg( p_demux, "seek with index: %i seconds, position %f",
222 (int)(i_date/1000000), f_pos );
224 p_index = ASF_FindObject( p_sys->p_root, &asf_object_index_guid, 0 );
226 if( i_date < 0 ) i_date = p_sys->i_length * f_pos;
228 i_pos = i_date * 10 / p_index->i_index_entry_time_interval;
229 i_pos = p_index->index_entry[i_pos].i_packet_number *
230 p_sys->p_fp->i_min_data_packet_size;
232 return stream_Seek( p_demux->s, p_sys->i_data_begin + i_pos );
235 /*****************************************************************************
237 *****************************************************************************/
238 static int Control( demux_t *p_demux, int i_query, va_list args )
240 demux_sys_t *p_sys = p_demux->p_sys;
248 case DEMUX_GET_LENGTH:
249 pi64 = (int64_t*)va_arg( args, int64_t * );
250 *pi64 = p_sys->i_length;
254 pi64 = (int64_t*)va_arg( args, int64_t * );
255 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
256 *pi64 = p_sys->i_time;
261 for( i = 0; i < 128 ; i++ )
262 if( p_sys->track[i] ) p_sys->track[i]->i_time = -1;
264 if( p_sys->b_index && p_sys->i_length > 0 )
266 i64 = (int64_t)va_arg( args, int64_t );
267 return SeekIndex( p_demux, i64, -1 );
271 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
272 p_sys->i_data_end, p_sys->i_bitrate,
273 p_sys->p_fp->i_min_data_packet_size,
277 case DEMUX_GET_POSITION:
278 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
279 if( p_sys->i_length > 0 )
281 pf = (double*)va_arg( args, double * );
282 *pf = p_sys->i_time / (double)p_sys->i_length;
285 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
286 p_sys->i_data_end, p_sys->i_bitrate,
287 p_sys->p_fp->i_min_data_packet_size,
290 case DEMUX_SET_POSITION:
292 for( i = 0; i < 128 ; i++ )
293 if( p_sys->track[i] ) p_sys->track[i]->i_time = -1;
295 if( p_sys->b_index && p_sys->i_length > 0 )
297 f = (double)va_arg( args, double );
298 return SeekIndex( p_demux, -1, f );
302 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
303 p_sys->i_data_end, p_sys->i_bitrate,
304 p_sys->p_fp->i_min_data_packet_size,
309 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
310 vlc_meta_Merge( p_meta, p_sys->meta );
314 return demux2_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 /*****************************************************************************
323 *****************************************************************************/
324 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
329 for( i = 0; i < 128 ; i++ )
331 asf_track_t *tk = p_sys->track[i];
333 if( tk && tk->p_es && tk->i_time > 0)
335 if( i_time < 0 ) i_time = tk->i_time;
336 else i_time = __MIN( i_time, tk->i_time );
343 #define GETVALUE2b( bits, var, def ) \
344 switch( (bits)&0x03 ) \
346 case 1: var = p_peek[i_skip]; i_skip++; break; \
347 case 2: var = GetWLE( p_peek + i_skip ); i_skip+= 2; break; \
348 case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
350 default: var = def; break;\
353 static int DemuxPacket( demux_t *p_demux )
355 demux_sys_t *p_sys = p_demux->p_sys;
356 int i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
357 const uint8_t *p_peek;
360 int i_packet_size_left;
362 int i_packet_property;
364 int b_packet_multiple_payload;
366 int i_packet_sequence;
367 int i_packet_padding_length;
369 uint32_t i_packet_send_time;
370 uint16_t i_packet_duration;
373 int i_payload_length_type;
376 if( stream_Peek( p_demux->s, &p_peek,i_data_packet_min)<i_data_packet_min )
378 msg_Warn( p_demux, "cannot peek while getting new packet, EOF ?" );
383 /* *** parse error correction if present *** */
386 unsigned int i_error_correction_length_type;
387 unsigned int i_error_correction_data_length;
388 unsigned int i_opaque_data_present;
390 i_error_correction_data_length = p_peek[0] & 0x0f; // 4bits
391 i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01; // 1bit
392 i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
393 i_skip += 1; // skip error correction flags
395 if( i_error_correction_length_type != 0x00 ||
396 i_opaque_data_present != 0 ||
397 i_error_correction_data_length != 0x02 )
399 goto loop_error_recovery;
402 i_skip += i_error_correction_data_length;
406 msg_Warn( p_demux, "p_peek[0]&0x80 != 0x80" );
410 if( i_skip + 2 >= i_data_packet_min )
412 goto loop_error_recovery;
415 i_packet_flags = p_peek[i_skip]; i_skip++;
416 i_packet_property = p_peek[i_skip]; i_skip++;
418 b_packet_multiple_payload = i_packet_flags&0x01;
420 /* read some value */
421 GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
422 GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
423 GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
425 i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
426 i_packet_duration = GetWLE( p_peek + i_skip ); i_skip += 2;
428 // i_packet_size_left = i_packet_length; // XXX data really read
429 /* FIXME I have to do that for some file, I don't known why */
430 i_packet_size_left = i_data_packet_min;
432 if( b_packet_multiple_payload )
434 i_payload_count = p_peek[i_skip] & 0x3f;
435 i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
441 i_payload_length_type = 0x02; // unused
444 for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
448 int i_packet_keyframe;
450 int i_media_object_number;
451 int i_media_object_offset;
452 int i_replicated_data_length;
453 int i_payload_data_length;
454 int i_payload_data_pos;
455 int i_sub_payload_data_length;
461 if( i_skip >= i_packet_size_left )
463 /* prevent some segfault with invalid file */
467 i_packet_keyframe = p_peek[i_skip] >> 7;
468 i_stream_number = p_peek[i_skip++] & 0x7f;
470 GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
471 GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
472 GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
474 if( i_replicated_data_length > 1 ) // should be at least 8 bytes
476 i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
477 i_skip += i_replicated_data_length;
480 i_media_object_offset = i_tmp;
482 if( i_skip >= i_packet_size_left )
487 else if( i_replicated_data_length == 1 )
489 /* msg_Dbg( p_demux, "found compressed payload" ); */
491 i_pts = (mtime_t)i_tmp * 1000;
492 i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
494 i_media_object_offset = 0;
498 i_pts = (mtime_t)i_packet_send_time * 1000;
501 i_media_object_offset = i_tmp;
504 i_pts = __MAX( i_pts - p_sys->p_fp->i_preroll * 1000, 0 );
505 if( b_packet_multiple_payload )
507 GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
511 i_payload_data_length = i_packet_length -
512 i_packet_padding_length - i_skip;
515 if( i_payload_data_length < 0 || i_skip + i_payload_data_length > i_packet_size_left )
521 "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
522 i_payload + 1, i_payload_count, i_stream_number, i_media_object_number,
523 i_media_object_offset, i_replicated_data_length, i_payload_data_length );
526 if( ( tk = p_sys->track[i_stream_number] ) == NULL )
529 "undeclared stream[Id 0x%x]", i_stream_number );
530 i_skip += i_payload_data_length;
531 continue; // over payload
536 i_skip += i_payload_data_length;
541 for( i_payload_data_pos = 0;
542 i_payload_data_pos < i_payload_data_length &&
543 i_packet_size_left > 0;
544 i_payload_data_pos += i_sub_payload_data_length )
549 // read sub payload length
550 if( i_replicated_data_length == 1 )
552 i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
553 i_payload_data_pos++;
557 i_sub_payload_data_length = i_payload_data_length;
560 /* FIXME I don't use i_media_object_number, sould I ? */
561 if( tk->p_frame && i_media_object_offset == 0 )
563 /* send complete packet to decoder */
564 block_t *p_gather = block_ChainGather( tk->p_frame );
566 es_out_Send( p_demux->out, tk->p_es, p_gather );
571 i_read = i_sub_payload_data_length + i_skip;
572 if( ( p_frag = stream_Block( p_demux->s, i_read ) ) == NULL )
574 msg_Warn( p_demux, "cannot read data" );
577 i_packet_size_left -= i_read;
579 p_frag->p_buffer += i_skip;
580 p_frag->i_buffer -= i_skip;
582 if( tk->p_frame == NULL )
585 ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
587 p_frag->i_pts = tk->i_time;
589 if( tk->i_cat != VIDEO_ES )
590 p_frag->i_dts = p_frag->i_pts;
593 p_frag->i_dts = p_frag->i_pts;
598 block_ChainAppend( &tk->p_frame, p_frag );
601 if( i_packet_size_left > 0 )
603 if( stream_Peek( p_demux->s, &p_peek, i_packet_size_left )
604 < i_packet_size_left )
606 msg_Warn( p_demux, "cannot peek, EOF ?" );
613 if( i_packet_size_left > 0 )
615 if( stream_Read( p_demux->s, NULL, i_packet_size_left )
616 < i_packet_size_left )
618 msg_Warn( p_demux, "cannot skip data, EOF ?" );
626 msg_Warn( p_demux, "unsupported packet header" );
627 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
629 msg_Err( p_demux, "unsupported packet header, fatal error" );
632 stream_Read( p_demux->s, NULL, i_data_packet_min );
637 /*****************************************************************************
639 *****************************************************************************/
640 static int DemuxInit( demux_t *p_demux )
642 demux_sys_t *p_sys = p_demux->p_sys;
643 vlc_bool_t b_seekable;
644 unsigned int i_stream, i;
645 asf_object_content_description_t *p_cd;
646 asf_object_index_t *p_index;
652 p_sys->i_bitrate = 0;
653 p_sys->p_root = NULL;
657 for( i = 0; i < 128; i++ )
659 p_sys->track[i] = NULL;
661 p_sys->i_data_begin = -1;
662 p_sys->i_data_end = -1;
665 /* Now load all object ( except raw data ) */
666 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
667 if( !(p_sys->p_root = ASF_ReadObjectRoot(p_demux->s, b_seekable)) )
669 msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
672 p_sys->p_fp = p_sys->p_root->p_fp;
674 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
676 msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
680 p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
681 &asf_object_stream_properties_guid );
682 if( p_sys->i_track <= 0 )
684 msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
688 /* check if index is available */
689 p_index = ASF_FindObject( p_sys->p_root, &asf_object_index_guid, 0 );
690 b_index = p_index && p_index->i_index_entry_count;
692 msg_Dbg( p_demux, "found %d streams", p_sys->i_track );
694 for( i_stream = 0; i_stream < p_sys->i_track; i_stream ++ )
697 asf_object_stream_properties_t *p_sp;
698 vlc_bool_t b_access_selected;
700 p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
701 &asf_object_stream_properties_guid,
704 tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
705 memset( tk, 0, sizeof( asf_track_t ) );
712 /* Check (in case of mms) if this track is selected (ie will receive data) */
713 if( !stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_GET_PRIVATE_ID_STATE,
714 p_sp->i_stream_number, &b_access_selected ) &&
717 tk->i_cat = UNKNOWN_ES;
718 msg_Dbg( p_demux, "ignoring not selected stream(ID:%d) (by access)",
719 p_sp->i_stream_number );
723 if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
724 p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
727 uint8_t *p_data = p_sp->p_type_specific_data;
730 es_format_Init( &fmt, AUDIO_ES, 0 );
731 i_format = GetWLE( &p_data[0] );
732 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
733 fmt.audio.i_channels = GetWLE( &p_data[2] );
734 fmt.audio.i_rate = GetDWLE( &p_data[4] );
735 fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
736 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
737 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
739 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
740 i_format != WAVE_FORMAT_MPEGLAYER3 &&
741 i_format != WAVE_FORMAT_MPEG )
743 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
744 p_sp->i_type_specific_data_length -
745 sizeof( WAVEFORMATEX ) );
746 fmt.p_extra = malloc( fmt.i_extra );
747 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
751 tk->i_cat = AUDIO_ES;
752 tk->p_es = es_out_Add( p_demux->out, &fmt );
753 es_format_Clean( &fmt );
755 msg_Dbg( p_demux, "added new audio stream(codec:0x%x,ID:%d)",
756 GetWLE( p_data ), p_sp->i_stream_number );
758 else if( ASF_CmpGUID( &p_sp->i_stream_type,
759 &asf_object_stream_type_video ) &&
760 p_sp->i_type_specific_data_length >= 11 +
761 sizeof( BITMAPINFOHEADER ) )
764 uint8_t *p_data = &p_sp->p_type_specific_data[11];
766 es_format_Init( &fmt, VIDEO_ES,
767 VLC_FOURCC( p_data[16], p_data[17],
768 p_data[18], p_data[19] ) );
769 fmt.video.i_width = GetDWLE( p_data + 4 );
770 fmt.video.i_height= GetDWLE( p_data + 8 );
773 if( fmt.i_codec == VLC_FOURCC( 'D','V','R',' ') )
775 /* DVR-MS special ASF */
776 fmt.i_codec = VLC_FOURCC( 'm','p','g','2' ) ;
777 fmt.b_packetized = VLC_FALSE;
780 if( p_sp->i_type_specific_data_length > 11 +
781 sizeof( BITMAPINFOHEADER ) )
783 fmt.i_extra = __MIN( GetDWLE( p_data ),
784 p_sp->i_type_specific_data_length - 11 -
785 sizeof( BITMAPINFOHEADER ) );
786 fmt.p_extra = malloc( fmt.i_extra );
787 memcpy( fmt.p_extra, &p_data[sizeof( BITMAPINFOHEADER )],
791 /* Look for an aspect ratio */
792 if( p_sys->p_root->p_metadata )
794 asf_object_metadata_t *p_meta = p_sys->p_root->p_metadata;
795 int i_aspect_x = 0, i_aspect_y = 0;
798 for( i = 0; i < p_meta->i_record_entries_count; i++ )
800 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioX" ) )
802 if( (!i_aspect_x && !p_meta->record[i].i_stream) ||
803 p_meta->record[i].i_stream ==
804 p_sp->i_stream_number )
805 i_aspect_x = p_meta->record[i].i_val;
807 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioY" ) )
809 if( (!i_aspect_y && !p_meta->record[i].i_stream) ||
810 p_meta->record[i].i_stream ==
811 p_sp->i_stream_number )
812 i_aspect_y = p_meta->record[i].i_val;
816 if( i_aspect_x && i_aspect_y )
818 fmt.video.i_aspect = i_aspect_x *
819 (int64_t)fmt.video.i_width * VOUT_ASPECT_FACTOR /
820 fmt.video.i_height / i_aspect_y;
824 tk->i_cat = VIDEO_ES;
825 tk->p_es = es_out_Add( p_demux->out, &fmt );
826 es_format_Clean( &fmt );
828 /* If there is a video track then use the index for seeking */
829 p_sys->b_index = b_index;
831 msg_Dbg( p_demux, "added new video stream(ID:%d)",
832 p_sp->i_stream_number );
834 else if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_extended_stream_header ) &&
835 p_sp->i_type_specific_data_length >= 64 )
837 /* Now follows a 64 byte header of which we don't know much */
839 guid_t *p_ref = (guid_t *)p_sp->p_type_specific_data;
840 uint8_t *p_data = p_sp->p_type_specific_data + 64;
841 unsigned int i_data = p_sp->i_type_specific_data_length - 64;
843 msg_Dbg( p_demux, "Ext stream header detected. datasize = %d", p_sp->i_type_specific_data_length );
844 if( ASF_CmpGUID( p_ref, &asf_object_extended_stream_type_audio ) &&
845 i_data >= sizeof( WAVEFORMATEX ) - 2)
848 es_format_Init( &fmt, AUDIO_ES, 0 );
849 i_format = GetWLE( &p_data[0] );
851 fmt.i_codec = VLC_FOURCC( 'a','5','2',' ');
853 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
854 fmt.audio.i_channels = GetWLE( &p_data[2] );
855 fmt.audio.i_rate = GetDWLE( &p_data[4] );
856 fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
857 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
858 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
859 fmt.b_packetized = VLC_TRUE;
861 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
862 i_format != WAVE_FORMAT_MPEGLAYER3 &&
863 i_format != WAVE_FORMAT_MPEG )
865 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
866 p_sp->i_type_specific_data_length -
867 sizeof( WAVEFORMATEX ) );
868 fmt.p_extra = malloc( fmt.i_extra );
869 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
873 tk->i_cat = AUDIO_ES;
874 tk->p_es = es_out_Add( p_demux->out, &fmt );
875 es_format_Clean( &fmt );
877 msg_Dbg( p_demux, "added new audio stream (codec:0x%x,ID:%d)",
878 i_format, p_sp->i_stream_number );
883 tk->i_cat = UNKNOWN_ES;
884 msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
885 p_sp->i_stream_number );
889 p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
890 if( p_sys->p_root->p_data->i_object_size != 0 )
892 p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
893 p_sys->p_root->p_data->i_object_size;
896 { /* live/broacast */
897 p_sys->i_data_end = -1;
900 /* go to first packet */
901 stream_Seek( p_demux->s, p_sys->i_data_begin );
903 /* try to calculate movie time */
904 if( p_sys->p_fp->i_data_packets_count > 0 )
907 int64_t i_size = stream_Size( p_demux->s );
909 if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
911 i_size = p_sys->i_data_end;
914 /* real number of packets */
915 i_count = ( i_size - p_sys->i_data_begin ) /
916 p_sys->p_fp->i_min_data_packet_size;
918 /* calculate the time duration in micro-s */
919 p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
921 (mtime_t)p_sys->p_fp->i_data_packets_count;
923 if( p_sys->i_length > 0 )
925 p_sys->i_bitrate = 8 * i_size * (int64_t)1000000 / p_sys->i_length;
929 /* Create meta information */
930 p_sys->meta = vlc_meta_New();
932 if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
933 &asf_object_content_description_guid, 0 ) ) )
935 if( p_cd->psz_title && *p_cd->psz_title )
937 vlc_meta_SetTitle( p_sys->meta, p_cd->psz_title );
939 if( p_cd->psz_artist && *p_cd->psz_artist )
941 vlc_meta_SetArtist( p_sys->meta, p_cd->psz_artist );
943 if( p_cd->psz_copyright && *p_cd->psz_copyright )
945 vlc_meta_SetCopyright( p_sys->meta, p_cd->psz_copyright );
947 if( p_cd->psz_description && *p_cd->psz_description )
949 vlc_meta_SetDescription( p_sys->meta, p_cd->psz_description );
951 if( p_cd->psz_rating && *p_cd->psz_rating )
953 vlc_meta_SetRating( p_sys->meta, p_cd->psz_rating );
956 /// \tood Fix Child meta for ASF tracks
958 for( i_stream = 0, i = 0; i < 128; i++ )
960 asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
961 &asf_object_codec_list_guid, 0 );
963 if( p_sys->track[i] )
965 vlc_meta_t *tk = vlc_meta_New();
966 TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
968 if( p_cl && i_stream < p_cl->i_codec_entries_count )
970 if( p_cl->codec[i_stream].psz_name &&
971 *p_cl->codec[i_stream].psz_name )
973 vlc_meta_Add( tk, VLC_META_CODEC_NAME,
974 p_cl->codec[i_stream].psz_name );
976 if( p_cl->codec[i_stream].psz_description &&
977 *p_cl->codec[i_stream].psz_description )
979 vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
980 p_cl->codec[i_stream].psz_description );
988 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
992 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
995 /*****************************************************************************
997 *****************************************************************************/
998 static void DemuxEnd( demux_t *p_demux )
1000 demux_sys_t *p_sys = p_demux->p_sys;
1005 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1006 p_sys->p_root = NULL;
1010 vlc_meta_Delete( p_sys->meta );
1014 for( i = 0; i < 128; i++ )
1016 asf_track_t *tk = p_sys->track[i];
1022 block_ChainRelease( tk->p_frame );
1026 es_out_Del( p_demux->out, tk->p_es );
1030 p_sys->track[i] = 0;