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];
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 );
152 if( p_demux->b_die ) break;
154 /* Check if we have concatenated files */
155 if( stream_Peek( p_demux->s, &p_peek, 16 ) == 16 )
159 ASF_GetGUID( &guid, p_peek );
160 if( ASF_CmpGUID( &guid, &asf_object_header_guid ) )
162 msg_Warn( p_demux, "found a new ASF header" );
163 /* We end this stream */
166 /* And we prepare to read the next one */
167 if( DemuxInit( p_demux ) )
169 msg_Err( p_demux, "failed to load the new header" );
170 intf_UserFatal( p_demux, VLC_FALSE, _("Could not demux ASF stream"),
171 _("VLC failed to load the ASF header.") );
178 /* Read and demux a packet */
179 if( ( i_result = DemuxPacket( p_demux ) ) <= 0 )
183 if( i_time_begin == -1 )
185 i_time_begin = GetMoviePTS( p_sys );
189 i_length = GetMoviePTS( p_sys ) - i_time_begin;
190 if( i_length < 0 || i_length >= 40 * 1000 ) break;
195 p_sys->i_time = GetMoviePTS( p_sys );
196 if( p_sys->i_time >= 0 )
198 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
204 /*****************************************************************************
205 * Close: frees unused data
206 *****************************************************************************/
207 static void Close( vlc_object_t * p_this )
209 demux_t *p_demux = (demux_t *)p_this;
213 free( p_demux->p_sys );
216 /*****************************************************************************
217 * SeekIndex: goto to i_date or i_percent
218 *****************************************************************************/
219 static int SeekIndex( demux_t *p_demux, mtime_t i_date, float f_pos )
221 demux_sys_t *p_sys = p_demux->p_sys;
222 asf_object_index_t *p_index;
225 msg_Dbg( p_demux, "seek with index: %i seconds, position %f",
226 (int)(i_date/1000000), f_pos );
228 p_index = ASF_FindObject( p_sys->p_root, &asf_object_index_guid, 0 );
230 if( i_date < 0 ) i_date = p_sys->i_length * f_pos;
232 i_pos = i_date * 10 / p_index->i_index_entry_time_interval;
233 i_pos = p_index->index_entry[i_pos].i_packet_number *
234 p_sys->p_fp->i_min_data_packet_size;
236 return stream_Seek( p_demux->s, p_sys->i_data_begin + i_pos );
239 /*****************************************************************************
241 *****************************************************************************/
242 static int Control( demux_t *p_demux, int i_query, va_list args )
244 demux_sys_t *p_sys = p_demux->p_sys;
252 case DEMUX_GET_LENGTH:
253 pi64 = (int64_t*)va_arg( args, int64_t * );
254 *pi64 = p_sys->i_length;
258 pi64 = (int64_t*)va_arg( args, int64_t * );
259 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
260 *pi64 = p_sys->i_time;
265 for( i = 0; i < 128 ; i++ )
266 if( p_sys->track[i] ) p_sys->track[i]->i_time = -1;
268 if( p_sys->b_index && p_sys->i_length > 0 )
270 i64 = (int64_t)va_arg( args, int64_t );
271 return SeekIndex( p_demux, i64, -1 );
275 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
276 p_sys->i_data_end, p_sys->i_bitrate,
277 p_sys->p_fp->i_min_data_packet_size,
281 case DEMUX_GET_POSITION:
282 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
283 if( p_sys->i_length > 0 )
285 pf = (double*)va_arg( args, double * );
286 *pf = p_sys->i_time / (double)p_sys->i_length;
289 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
290 p_sys->i_data_end, p_sys->i_bitrate,
291 p_sys->p_fp->i_min_data_packet_size,
294 case DEMUX_SET_POSITION:
296 for( i = 0; i < 128 ; i++ )
297 if( p_sys->track[i] ) p_sys->track[i]->i_time = -1;
299 if( p_sys->b_index && p_sys->i_length > 0 )
301 f = (double)va_arg( args, double );
302 return SeekIndex( p_demux, -1, f );
306 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
307 p_sys->i_data_end, p_sys->i_bitrate,
308 p_sys->p_fp->i_min_data_packet_size,
313 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
314 vlc_meta_Merge( p_meta, p_sys->meta );
318 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
319 p_sys->i_data_end, p_sys->i_bitrate,
320 p_sys->p_fp->i_min_data_packet_size,
325 /*****************************************************************************
327 *****************************************************************************/
328 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
333 for( i = 0; i < 128 ; i++ )
335 asf_track_t *tk = p_sys->track[i];
337 if( tk && tk->p_es && tk->i_time > 0)
339 if( i_time < 0 ) i_time = tk->i_time;
340 else i_time = __MIN( i_time, tk->i_time );
347 #define GETVALUE2b( bits, var, def ) \
348 switch( (bits)&0x03 ) \
350 case 1: var = p_peek[i_skip]; i_skip++; break; \
351 case 2: var = GetWLE( p_peek + i_skip ); i_skip+= 2; break; \
352 case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
354 default: var = def; break;\
357 static int DemuxPacket( demux_t *p_demux )
359 demux_sys_t *p_sys = p_demux->p_sys;
360 int i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
361 const uint8_t *p_peek;
364 int i_packet_size_left;
366 int i_packet_property;
368 int b_packet_multiple_payload;
370 int i_packet_sequence;
371 int i_packet_padding_length;
373 uint32_t i_packet_send_time;
374 uint16_t i_packet_duration;
377 int i_payload_length_type;
380 if( stream_Peek( p_demux->s, &p_peek,i_data_packet_min)<i_data_packet_min )
382 msg_Warn( p_demux, "cannot peek while getting new packet, EOF ?" );
387 /* *** parse error correction if present *** */
390 unsigned int i_error_correction_length_type;
391 unsigned int i_error_correction_data_length;
392 unsigned int i_opaque_data_present;
394 i_error_correction_data_length = p_peek[0] & 0x0f; // 4bits
395 i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01; // 1bit
396 i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
397 i_skip += 1; // skip error correction flags
399 if( i_error_correction_length_type != 0x00 ||
400 i_opaque_data_present != 0 ||
401 i_error_correction_data_length != 0x02 )
403 goto loop_error_recovery;
406 i_skip += i_error_correction_data_length;
410 msg_Warn( p_demux, "p_peek[0]&0x80 != 0x80" );
414 if( i_skip + 2 >= i_data_packet_min )
416 goto loop_error_recovery;
419 i_packet_flags = p_peek[i_skip]; i_skip++;
420 i_packet_property = p_peek[i_skip]; i_skip++;
422 b_packet_multiple_payload = i_packet_flags&0x01;
424 /* read some value */
425 GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
426 GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
427 GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
429 i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
430 i_packet_duration = GetWLE( p_peek + i_skip ); i_skip += 2;
432 // i_packet_size_left = i_packet_length; // XXX data really read
433 /* FIXME I have to do that for some file, I don't known why */
434 i_packet_size_left = i_data_packet_min;
436 if( b_packet_multiple_payload )
438 i_payload_count = p_peek[i_skip] & 0x3f;
439 i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
445 i_payload_length_type = 0x02; // unused
448 for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
452 int i_packet_keyframe;
454 int i_media_object_number;
455 int i_media_object_offset;
456 int i_replicated_data_length;
457 int i_payload_data_length;
458 int i_payload_data_pos;
459 int i_sub_payload_data_length;
465 if( i_skip >= i_packet_size_left )
467 /* prevent some segfault with invalid file */
471 i_packet_keyframe = p_peek[i_skip] >> 7;
472 i_stream_number = p_peek[i_skip++] & 0x7f;
474 GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
475 GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
476 GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
478 if( i_replicated_data_length > 1 ) // should be at least 8 bytes
480 i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
481 i_skip += i_replicated_data_length;
484 i_media_object_offset = i_tmp;
486 if( i_skip >= i_packet_size_left )
491 else if( i_replicated_data_length == 1 )
493 /* msg_Dbg( p_demux, "found compressed payload" ); */
495 i_pts = (mtime_t)i_tmp * 1000;
496 i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
498 i_media_object_offset = 0;
502 i_pts = (mtime_t)i_packet_send_time * 1000;
505 i_media_object_offset = i_tmp;
508 i_pts = __MAX( i_pts - p_sys->p_fp->i_preroll * 1000, 0 );
509 if( b_packet_multiple_payload )
511 GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
515 i_payload_data_length = i_packet_length -
516 i_packet_padding_length - i_skip;
519 if( i_payload_data_length < 0 || i_skip + i_payload_data_length > i_packet_size_left )
525 "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
526 i_payload + 1, i_payload_count, i_stream_number, i_media_object_number,
527 i_media_object_offset, i_replicated_data_length, i_payload_data_length );
530 if( ( tk = p_sys->track[i_stream_number] ) == NULL )
533 "undeclared stream[Id 0x%x]", i_stream_number );
534 i_skip += i_payload_data_length;
535 continue; // over payload
540 i_skip += i_payload_data_length;
545 for( i_payload_data_pos = 0;
546 i_payload_data_pos < i_payload_data_length &&
547 i_packet_size_left > 0;
548 i_payload_data_pos += i_sub_payload_data_length )
553 // read sub payload length
554 if( i_replicated_data_length == 1 )
556 i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
557 i_payload_data_pos++;
561 i_sub_payload_data_length = i_payload_data_length;
564 /* FIXME I don't use i_media_object_number, sould I ? */
565 if( tk->p_frame && i_media_object_offset == 0 )
567 /* send complete packet to decoder */
568 block_t *p_gather = block_ChainGather( tk->p_frame );
570 es_out_Send( p_demux->out, tk->p_es, p_gather );
575 i_read = i_sub_payload_data_length + i_skip;
576 if( ( p_frag = stream_Block( p_demux->s, i_read ) ) == NULL )
578 msg_Warn( p_demux, "cannot read data" );
581 i_packet_size_left -= i_read;
583 p_frag->p_buffer += i_skip;
584 p_frag->i_buffer -= i_skip;
586 if( tk->p_frame == NULL )
589 ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
591 p_frag->i_pts = tk->i_time;
593 if( tk->i_cat != VIDEO_ES )
594 p_frag->i_dts = p_frag->i_pts;
597 p_frag->i_dts = p_frag->i_pts;
602 block_ChainAppend( &tk->p_frame, p_frag );
605 if( i_packet_size_left > 0 )
607 if( stream_Peek( p_demux->s, &p_peek, i_packet_size_left )
608 < i_packet_size_left )
610 msg_Warn( p_demux, "cannot peek, EOF ?" );
617 if( i_packet_size_left > 0 )
619 if( stream_Read( p_demux->s, NULL, i_packet_size_left )
620 < i_packet_size_left )
622 msg_Warn( p_demux, "cannot skip data, EOF ?" );
630 msg_Warn( p_demux, "unsupported packet header" );
631 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
633 msg_Err( p_demux, "unsupported packet header, fatal error" );
636 stream_Read( p_demux->s, NULL, i_data_packet_min );
641 /*****************************************************************************
643 *****************************************************************************/
644 static int DemuxInit( demux_t *p_demux )
646 demux_sys_t *p_sys = p_demux->p_sys;
647 vlc_bool_t b_seekable;
648 unsigned int i_stream, i;
649 asf_object_content_description_t *p_cd;
650 asf_object_index_t *p_index;
656 p_sys->i_bitrate = 0;
657 p_sys->p_root = NULL;
661 for( i = 0; i < 128; i++ )
663 p_sys->track[i] = NULL;
665 p_sys->i_data_begin = -1;
666 p_sys->i_data_end = -1;
669 /* Now load all object ( except raw data ) */
670 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
671 if( !(p_sys->p_root = ASF_ReadObjectRoot(p_demux->s, b_seekable)) )
673 msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
676 p_sys->p_fp = p_sys->p_root->p_fp;
678 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
680 msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
684 p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
685 &asf_object_stream_properties_guid );
686 if( p_sys->i_track <= 0 )
688 msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
692 /* check if index is available */
693 p_index = ASF_FindObject( p_sys->p_root, &asf_object_index_guid, 0 );
694 b_index = p_index && p_index->i_index_entry_count;
696 msg_Dbg( p_demux, "found %d streams", p_sys->i_track );
698 for( i_stream = 0; i_stream < p_sys->i_track; i_stream ++ )
701 asf_object_stream_properties_t *p_sp;
702 vlc_bool_t b_access_selected;
704 p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
705 &asf_object_stream_properties_guid,
708 tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
709 memset( tk, 0, sizeof( asf_track_t ) );
716 /* Check (in case of mms) if this track is selected (ie will receive data) */
717 if( !stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_GET_PRIVATE_ID_STATE,
718 p_sp->i_stream_number, &b_access_selected ) &&
721 tk->i_cat = UNKNOWN_ES;
722 msg_Dbg( p_demux, "ignoring not selected stream(ID:%d) (by access)",
723 p_sp->i_stream_number );
727 if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
728 p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
731 uint8_t *p_data = p_sp->p_type_specific_data;
734 es_format_Init( &fmt, AUDIO_ES, 0 );
735 i_format = GetWLE( &p_data[0] );
736 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
737 fmt.audio.i_channels = GetWLE( &p_data[2] );
738 fmt.audio.i_rate = GetDWLE( &p_data[4] );
739 fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
740 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
741 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
743 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
744 i_format != WAVE_FORMAT_MPEGLAYER3 &&
745 i_format != WAVE_FORMAT_MPEG )
747 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
748 p_sp->i_type_specific_data_length -
749 sizeof( WAVEFORMATEX ) );
750 fmt.p_extra = malloc( fmt.i_extra );
751 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
755 tk->i_cat = AUDIO_ES;
756 tk->p_es = es_out_Add( p_demux->out, &fmt );
757 es_format_Clean( &fmt );
759 msg_Dbg( p_demux, "added new audio stream(codec:0x%x,ID:%d)",
760 GetWLE( p_data ), p_sp->i_stream_number );
762 else if( ASF_CmpGUID( &p_sp->i_stream_type,
763 &asf_object_stream_type_video ) &&
764 p_sp->i_type_specific_data_length >= 11 +
765 sizeof( BITMAPINFOHEADER ) )
768 uint8_t *p_data = &p_sp->p_type_specific_data[11];
770 es_format_Init( &fmt, VIDEO_ES,
771 VLC_FOURCC( p_data[16], p_data[17],
772 p_data[18], p_data[19] ) );
773 fmt.video.i_width = GetDWLE( p_data + 4 );
774 fmt.video.i_height= GetDWLE( p_data + 8 );
777 if( fmt.i_codec == VLC_FOURCC( 'D','V','R',' ') )
779 /* DVR-MS special ASF */
780 fmt.i_codec = VLC_FOURCC( 'm','p','g','2' ) ;
781 fmt.b_packetized = VLC_FALSE;
784 if( p_sp->i_type_specific_data_length > 11 +
785 sizeof( BITMAPINFOHEADER ) )
787 fmt.i_extra = __MIN( GetDWLE( p_data ),
788 p_sp->i_type_specific_data_length - 11 -
789 sizeof( BITMAPINFOHEADER ) );
790 fmt.p_extra = malloc( fmt.i_extra );
791 memcpy( fmt.p_extra, &p_data[sizeof( BITMAPINFOHEADER )],
795 /* Look for an aspect ratio */
796 if( p_sys->p_root->p_metadata )
798 asf_object_metadata_t *p_meta = p_sys->p_root->p_metadata;
799 int i_aspect_x = 0, i_aspect_y = 0;
802 for( i = 0; i < p_meta->i_record_entries_count; i++ )
804 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioX" ) )
806 if( (!i_aspect_x && !p_meta->record[i].i_stream) ||
807 p_meta->record[i].i_stream ==
808 p_sp->i_stream_number )
809 i_aspect_x = p_meta->record[i].i_val;
811 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioY" ) )
813 if( (!i_aspect_y && !p_meta->record[i].i_stream) ||
814 p_meta->record[i].i_stream ==
815 p_sp->i_stream_number )
816 i_aspect_y = p_meta->record[i].i_val;
820 if( i_aspect_x && i_aspect_y )
822 fmt.video.i_aspect = i_aspect_x *
823 (int64_t)fmt.video.i_width * VOUT_ASPECT_FACTOR /
824 fmt.video.i_height / i_aspect_y;
828 tk->i_cat = VIDEO_ES;
829 tk->p_es = es_out_Add( p_demux->out, &fmt );
830 es_format_Clean( &fmt );
832 /* If there is a video track then use the index for seeking */
833 p_sys->b_index = b_index;
835 msg_Dbg( p_demux, "added new video stream(ID:%d)",
836 p_sp->i_stream_number );
838 else if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_extended_stream_header ) &&
839 p_sp->i_type_specific_data_length >= 64 )
841 /* Now follows a 64 byte header of which we don't know much */
843 guid_t *p_ref = (guid_t *)p_sp->p_type_specific_data;
844 uint8_t *p_data = p_sp->p_type_specific_data + 64;
845 unsigned int i_data = p_sp->i_type_specific_data_length - 64;
847 msg_Dbg( p_demux, "Ext stream header detected. datasize = %d", p_sp->i_type_specific_data_length );
848 if( ASF_CmpGUID( p_ref, &asf_object_extended_stream_type_audio ) &&
849 i_data >= sizeof( WAVEFORMATEX ) - 2)
852 es_format_Init( &fmt, AUDIO_ES, 0 );
853 i_format = GetWLE( &p_data[0] );
855 fmt.i_codec = VLC_FOURCC( 'a','5','2',' ');
857 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
858 fmt.audio.i_channels = GetWLE( &p_data[2] );
859 fmt.audio.i_rate = GetDWLE( &p_data[4] );
860 fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
861 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
862 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
863 fmt.b_packetized = VLC_TRUE;
865 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
866 i_format != WAVE_FORMAT_MPEGLAYER3 &&
867 i_format != WAVE_FORMAT_MPEG )
869 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
870 p_sp->i_type_specific_data_length -
871 sizeof( WAVEFORMATEX ) );
872 fmt.p_extra = malloc( fmt.i_extra );
873 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
877 tk->i_cat = AUDIO_ES;
878 tk->p_es = es_out_Add( p_demux->out, &fmt );
879 es_format_Clean( &fmt );
881 msg_Dbg( p_demux, "added new audio stream (codec:0x%x,ID:%d)",
882 i_format, p_sp->i_stream_number );
887 tk->i_cat = UNKNOWN_ES;
888 msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
889 p_sp->i_stream_number );
893 p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
894 if( p_sys->p_root->p_data->i_object_size != 0 )
896 p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
897 p_sys->p_root->p_data->i_object_size;
900 { /* live/broacast */
901 p_sys->i_data_end = -1;
904 /* go to first packet */
905 stream_Seek( p_demux->s, p_sys->i_data_begin );
907 /* try to calculate movie time */
908 if( p_sys->p_fp->i_data_packets_count > 0 )
911 int64_t i_size = stream_Size( p_demux->s );
913 if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
915 i_size = p_sys->i_data_end;
918 /* real number of packets */
919 i_count = ( i_size - p_sys->i_data_begin ) /
920 p_sys->p_fp->i_min_data_packet_size;
922 /* calculate the time duration in micro-s */
923 p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
925 (mtime_t)p_sys->p_fp->i_data_packets_count;
927 if( p_sys->i_length > 0 )
929 p_sys->i_bitrate = 8 * i_size * (int64_t)1000000 / p_sys->i_length;
933 /* Create meta information */
934 p_sys->meta = vlc_meta_New();
936 if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
937 &asf_object_content_description_guid, 0 ) ) )
939 if( p_cd->psz_title && *p_cd->psz_title )
941 vlc_meta_SetTitle( p_sys->meta, p_cd->psz_title );
943 if( p_cd->psz_artist && *p_cd->psz_artist )
945 vlc_meta_SetArtist( p_sys->meta, p_cd->psz_artist );
947 if( p_cd->psz_copyright && *p_cd->psz_copyright )
949 vlc_meta_SetCopyright( p_sys->meta, p_cd->psz_copyright );
951 if( p_cd->psz_description && *p_cd->psz_description )
953 vlc_meta_SetDescription( p_sys->meta, p_cd->psz_description );
955 if( p_cd->psz_rating && *p_cd->psz_rating )
957 vlc_meta_SetRating( p_sys->meta, p_cd->psz_rating );
960 /// \tood Fix Child meta for ASF tracks
962 for( i_stream = 0, i = 0; i < 128; i++ )
964 asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
965 &asf_object_codec_list_guid, 0 );
967 if( p_sys->track[i] )
969 vlc_meta_t *tk = vlc_meta_New();
970 TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
972 if( p_cl && i_stream < p_cl->i_codec_entries_count )
974 if( p_cl->codec[i_stream].psz_name &&
975 *p_cl->codec[i_stream].psz_name )
977 vlc_meta_Add( tk, VLC_META_CODEC_NAME,
978 p_cl->codec[i_stream].psz_name );
980 if( p_cl->codec[i_stream].psz_description &&
981 *p_cl->codec[i_stream].psz_description )
983 vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
984 p_cl->codec[i_stream].psz_description );
992 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
996 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
999 /*****************************************************************************
1001 *****************************************************************************/
1002 static void DemuxEnd( demux_t *p_demux )
1004 demux_sys_t *p_sys = p_demux->p_sys;
1009 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1010 p_sys->p_root = NULL;
1014 vlc_meta_Delete( p_sys->meta );
1018 for( i = 0; i < 128; i++ )
1020 asf_track_t *tk = p_sys->track[i];
1026 block_ChainRelease( tk->p_frame );
1030 es_out_Del( p_demux->out, tk->p_es );
1034 p_sys->track[i] = 0;