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 );
155 /* FIXME: returns EOF too early for some mms streams */
156 if( p_sys->i_data_end >= 0 &&
157 stream_Tell( p_demux->s ) >= p_sys->i_data_end )
161 /* Check if we have concatenated files */
162 if( stream_Peek( p_demux->s, &p_peek, 16 ) == 16 )
166 ASF_GetGUID( &guid, p_peek );
167 if( ASF_CmpGUID( &guid, &asf_object_header_guid ) )
169 msg_Warn( p_demux, "found a new ASF header" );
170 /* We end this stream */
173 /* And we prepare to read the next one */
174 if( DemuxInit( p_demux ) )
176 msg_Err( p_demux, "failed to load the new header" );
177 intf_UserFatal( p_demux, VLC_FALSE, _("Could not demux ASF stream"),
178 _("VLC failed to load the ASF header.") );
185 /* Read and demux a packet */
186 if( ( i_result = DemuxPacket( p_demux ) ) <= 0 )
190 if( i_time_begin == -1 )
192 i_time_begin = GetMoviePTS( p_sys );
196 i_length = GetMoviePTS( p_sys ) - i_time_begin;
197 if( i_length < 0 || i_length >= 40 * 1000 ) break;
202 p_sys->i_time = GetMoviePTS( p_sys );
203 if( p_sys->i_time >= 0 )
205 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
211 /*****************************************************************************
212 * Close: frees unused data
213 *****************************************************************************/
214 static void Close( vlc_object_t * p_this )
216 demux_t *p_demux = (demux_t *)p_this;
220 free( p_demux->p_sys );
223 /*****************************************************************************
224 * SeekIndex: goto to i_date or i_percent
225 *****************************************************************************/
226 static int SeekIndex( demux_t *p_demux, mtime_t i_date, float f_pos )
228 demux_sys_t *p_sys = p_demux->p_sys;
229 asf_object_index_t *p_index;
232 msg_Dbg( p_demux, "seek with index: %i seconds, position %f",
233 (int)(i_date/1000000), f_pos );
235 p_index = ASF_FindObject( p_sys->p_root, &asf_object_index_guid, 0 );
237 if( i_date < 0 ) i_date = p_sys->i_length * f_pos;
239 i_pos = i_date * 10 / p_index->i_index_entry_time_interval;
240 i_pos = p_index->index_entry[i_pos].i_packet_number *
241 p_sys->p_fp->i_min_data_packet_size;
243 return stream_Seek( p_demux->s, p_sys->i_data_begin + i_pos );
246 /*****************************************************************************
248 *****************************************************************************/
249 static int Control( demux_t *p_demux, int i_query, va_list args )
251 demux_sys_t *p_sys = p_demux->p_sys;
259 case DEMUX_GET_LENGTH:
260 pi64 = (int64_t*)va_arg( args, int64_t * );
261 *pi64 = p_sys->i_length;
265 pi64 = (int64_t*)va_arg( args, int64_t * );
266 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
267 *pi64 = p_sys->i_time;
272 for( i = 0; i < 128 ; i++ )
273 if( p_sys->track[i] ) p_sys->track[i]->i_time = -1;
275 if( p_sys->b_index && p_sys->i_length > 0 )
277 i64 = (int64_t)va_arg( args, int64_t );
278 return SeekIndex( p_demux, i64, -1 );
282 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
283 p_sys->i_data_end, p_sys->i_bitrate,
284 p_sys->p_fp->i_min_data_packet_size,
288 case DEMUX_GET_POSITION:
289 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
290 if( p_sys->i_length > 0 )
292 pf = (double*)va_arg( args, double * );
293 *pf = p_sys->i_time / (double)p_sys->i_length;
296 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
297 p_sys->i_data_end, p_sys->i_bitrate,
298 p_sys->p_fp->i_min_data_packet_size,
301 case DEMUX_SET_POSITION:
303 for( i = 0; i < 128 ; i++ )
304 if( p_sys->track[i] ) p_sys->track[i]->i_time = -1;
306 if( p_sys->b_index && p_sys->i_length > 0 )
308 f = (double)va_arg( args, double );
309 return SeekIndex( p_demux, -1, f );
313 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
314 p_sys->i_data_end, p_sys->i_bitrate,
315 p_sys->p_fp->i_min_data_packet_size,
320 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
321 vlc_meta_Merge( p_meta, p_sys->meta );
325 return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin,
326 p_sys->i_data_end, p_sys->i_bitrate,
327 p_sys->p_fp->i_min_data_packet_size,
332 /*****************************************************************************
334 *****************************************************************************/
335 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
340 for( i = 0; i < 128 ; i++ )
342 asf_track_t *tk = p_sys->track[i];
344 if( tk && tk->p_es && tk->i_time > 0)
346 if( i_time < 0 ) i_time = tk->i_time;
347 else i_time = __MIN( i_time, tk->i_time );
354 #define GETVALUE2b( bits, var, def ) \
355 switch( (bits)&0x03 ) \
357 case 1: var = p_peek[i_skip]; i_skip++; break; \
358 case 2: var = GetWLE( p_peek + i_skip ); i_skip+= 2; break; \
359 case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
361 default: var = def; break;\
364 static int DemuxPacket( demux_t *p_demux )
366 demux_sys_t *p_sys = p_demux->p_sys;
367 int i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
368 const uint8_t *p_peek;
371 int i_packet_size_left;
373 int i_packet_property;
375 int b_packet_multiple_payload;
377 int i_packet_sequence;
378 int i_packet_padding_length;
380 uint32_t i_packet_send_time;
381 uint16_t i_packet_duration;
384 int i_payload_length_type;
387 if( stream_Peek( p_demux->s, &p_peek,i_data_packet_min)<i_data_packet_min )
389 msg_Warn( p_demux, "cannot peek while getting new packet, EOF ?" );
394 /* *** parse error correction if present *** */
397 unsigned int i_error_correction_length_type;
398 unsigned int i_error_correction_data_length;
399 unsigned int i_opaque_data_present;
401 i_error_correction_data_length = p_peek[0] & 0x0f; // 4bits
402 i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01; // 1bit
403 i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
404 i_skip += 1; // skip error correction flags
406 if( i_error_correction_length_type != 0x00 ||
407 i_opaque_data_present != 0 ||
408 i_error_correction_data_length != 0x02 )
410 goto loop_error_recovery;
413 i_skip += i_error_correction_data_length;
417 msg_Warn( p_demux, "p_peek[0]&0x80 != 0x80" );
421 if( i_skip + 2 >= i_data_packet_min )
423 goto loop_error_recovery;
426 i_packet_flags = p_peek[i_skip]; i_skip++;
427 i_packet_property = p_peek[i_skip]; i_skip++;
429 b_packet_multiple_payload = i_packet_flags&0x01;
431 /* read some value */
432 GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
433 GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
434 GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
436 if( i_packet_padding_length > i_packet_length )
438 msg_Warn( p_demux, "Too large padding: %d", i_packet_padding_length );
439 goto loop_error_recovery;
442 i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
443 i_packet_duration = GetWLE( p_peek + i_skip ); i_skip += 2;
445 /* FIXME I have to do that for some file, I don't known why */
446 i_packet_size_left = i_data_packet_min /*i_packet_length*/ ;
448 if( b_packet_multiple_payload )
450 i_payload_count = p_peek[i_skip] & 0x3f;
451 i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
457 i_payload_length_type = 0x02; // unused
460 for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
464 int i_packet_keyframe;
466 int i_media_object_number;
467 int i_media_object_offset;
468 int i_replicated_data_length;
469 int i_payload_data_length;
470 int i_payload_data_pos;
471 int i_sub_payload_data_length;
477 if( i_skip >= i_packet_size_left )
479 /* prevent some segfault with invalid file */
483 i_packet_keyframe = p_peek[i_skip] >> 7;
484 i_stream_number = p_peek[i_skip++] & 0x7f;
486 GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
487 GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
488 GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
490 if( i_replicated_data_length > 1 ) // should be at least 8 bytes
492 i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
493 i_skip += i_replicated_data_length;
496 i_media_object_offset = i_tmp;
498 if( i_skip >= i_packet_size_left )
503 else if( i_replicated_data_length == 1 )
505 /* msg_Dbg( p_demux, "found compressed payload" ); */
507 i_pts = (mtime_t)i_tmp * 1000;
508 i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
510 i_media_object_offset = 0;
514 i_pts = (mtime_t)i_packet_send_time * 1000;
517 i_media_object_offset = i_tmp;
520 i_pts = __MAX( i_pts - p_sys->p_fp->i_preroll * 1000, 0 );
521 if( b_packet_multiple_payload )
523 GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
527 i_payload_data_length = i_packet_length -
528 i_packet_padding_length - i_skip;
531 if( i_payload_data_length < 0 || i_payload_data_length > i_packet_size_left )
537 "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
538 i_payload + 1, i_payload_count, i_stream_number, i_media_object_number,
539 i_media_object_offset, i_replicated_data_length, i_payload_data_length );
542 if( ( tk = p_sys->track[i_stream_number] ) == NULL )
545 "undeclared stream[Id 0x%x]", i_stream_number );
546 i_skip += i_payload_data_length;
547 continue; // over payload
552 i_skip += i_payload_data_length;
557 for( i_payload_data_pos = 0;
558 i_payload_data_pos < i_payload_data_length &&
559 i_packet_size_left > 0;
560 i_payload_data_pos += i_sub_payload_data_length )
565 // read sub payload length
566 if( i_replicated_data_length == 1 )
568 i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
569 i_payload_data_pos++;
573 i_sub_payload_data_length = i_payload_data_length;
576 /* FIXME I don't use i_media_object_number, sould I ? */
577 if( tk->p_frame && i_media_object_offset == 0 )
579 /* send complete packet to decoder */
580 block_t *p_gather = block_ChainGather( tk->p_frame );
582 es_out_Send( p_demux->out, tk->p_es, p_gather );
587 i_read = i_sub_payload_data_length + i_skip;
588 if( ( p_frag = stream_Block( p_demux->s, i_read ) ) == NULL )
590 msg_Warn( p_demux, "cannot read data" );
593 i_packet_size_left -= i_read;
595 p_frag->p_buffer += i_skip;
596 p_frag->i_buffer -= i_skip;
598 if( tk->p_frame == NULL )
601 ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
603 p_frag->i_pts = tk->i_time;
605 if( tk->i_cat != VIDEO_ES )
606 p_frag->i_dts = p_frag->i_pts;
609 p_frag->i_dts = p_frag->i_pts;
614 block_ChainAppend( &tk->p_frame, p_frag );
617 if( i_packet_size_left > 0 )
619 if( stream_Peek( p_demux->s, &p_peek, i_packet_size_left )
620 < i_packet_size_left )
622 msg_Warn( p_demux, "cannot peek, EOF ?" );
629 if( i_packet_size_left > 0 )
631 if( i_packet_size_left > i_packet_padding_length )
632 msg_Warn( p_demux, "Didn't read %d bytes in the packet",
633 i_packet_size_left - i_packet_padding_length );
634 else if( i_packet_size_left < i_packet_padding_length )
635 msg_Warn( p_demux, "Read %d too much bytes in the packet",
636 i_packet_padding_length - i_packet_size_left );
638 if( stream_Read( p_demux->s, NULL, i_packet_size_left )
639 < i_packet_size_left )
641 msg_Err( p_demux, "cannot skip data, EOF ?" );
649 msg_Warn( p_demux, "unsupported packet header" );
650 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
652 msg_Err( p_demux, "unsupported packet header, fatal error" );
655 stream_Read( p_demux->s, NULL, i_data_packet_min );
660 /*****************************************************************************
662 *****************************************************************************/
663 static int DemuxInit( demux_t *p_demux )
665 demux_sys_t *p_sys = p_demux->p_sys;
666 vlc_bool_t b_seekable;
667 unsigned int i_stream, i;
668 asf_object_content_description_t *p_cd;
669 asf_object_index_t *p_index;
675 p_sys->i_bitrate = 0;
676 p_sys->p_root = NULL;
680 for( i = 0; i < 128; i++ )
682 p_sys->track[i] = NULL;
684 p_sys->i_data_begin = -1;
685 p_sys->i_data_end = -1;
688 /* Now load all object ( except raw data ) */
689 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
690 if( !(p_sys->p_root = ASF_ReadObjectRoot(p_demux->s, b_seekable)) )
692 msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
695 p_sys->p_fp = p_sys->p_root->p_fp;
697 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
699 msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
703 p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
704 &asf_object_stream_properties_guid );
705 if( p_sys->i_track <= 0 )
707 msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
711 /* check if index is available */
712 p_index = ASF_FindObject( p_sys->p_root, &asf_object_index_guid, 0 );
713 b_index = p_index && p_index->i_index_entry_count;
715 msg_Dbg( p_demux, "found %d streams", p_sys->i_track );
717 for( i_stream = 0; i_stream < p_sys->i_track; i_stream ++ )
720 asf_object_stream_properties_t *p_sp;
721 vlc_bool_t b_access_selected;
723 p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
724 &asf_object_stream_properties_guid,
727 tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
728 memset( tk, 0, sizeof( asf_track_t ) );
735 /* Check (in case of mms) if this track is selected (ie will receive data) */
736 if( !stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_GET_PRIVATE_ID_STATE,
737 p_sp->i_stream_number, &b_access_selected ) &&
740 tk->i_cat = UNKNOWN_ES;
741 msg_Dbg( p_demux, "ignoring not selected stream(ID:%d) (by access)",
742 p_sp->i_stream_number );
746 if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
747 p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
750 uint8_t *p_data = p_sp->p_type_specific_data;
753 es_format_Init( &fmt, AUDIO_ES, 0 );
754 i_format = GetWLE( &p_data[0] );
755 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
756 fmt.audio.i_channels = GetWLE( &p_data[2] );
757 fmt.audio.i_rate = GetDWLE( &p_data[4] );
758 fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
759 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
760 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
762 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
763 i_format != WAVE_FORMAT_MPEGLAYER3 &&
764 i_format != WAVE_FORMAT_MPEG )
766 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
767 p_sp->i_type_specific_data_length -
768 sizeof( WAVEFORMATEX ) );
769 fmt.p_extra = malloc( fmt.i_extra );
770 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
774 tk->i_cat = AUDIO_ES;
775 tk->p_es = es_out_Add( p_demux->out, &fmt );
776 es_format_Clean( &fmt );
778 msg_Dbg( p_demux, "added new audio stream(codec:0x%x,ID:%d)",
779 GetWLE( p_data ), p_sp->i_stream_number );
781 else if( ASF_CmpGUID( &p_sp->i_stream_type,
782 &asf_object_stream_type_video ) &&
783 p_sp->i_type_specific_data_length >= 11 +
784 sizeof( BITMAPINFOHEADER ) )
787 uint8_t *p_data = &p_sp->p_type_specific_data[11];
789 es_format_Init( &fmt, VIDEO_ES,
790 VLC_FOURCC( p_data[16], p_data[17],
791 p_data[18], p_data[19] ) );
792 fmt.video.i_width = GetDWLE( p_data + 4 );
793 fmt.video.i_height= GetDWLE( p_data + 8 );
796 if( fmt.i_codec == VLC_FOURCC( 'D','V','R',' ') )
798 /* DVR-MS special ASF */
799 fmt.i_codec = VLC_FOURCC( 'm','p','g','2' ) ;
800 fmt.b_packetized = VLC_FALSE;
803 if( p_sp->i_type_specific_data_length > 11 +
804 sizeof( BITMAPINFOHEADER ) )
806 fmt.i_extra = __MIN( GetDWLE( p_data ),
807 p_sp->i_type_specific_data_length - 11 -
808 sizeof( BITMAPINFOHEADER ) );
809 fmt.p_extra = malloc( fmt.i_extra );
810 memcpy( fmt.p_extra, &p_data[sizeof( BITMAPINFOHEADER )],
814 /* Look for an aspect ratio */
815 if( p_sys->p_root->p_metadata )
817 asf_object_metadata_t *p_meta = p_sys->p_root->p_metadata;
818 int i_aspect_x = 0, i_aspect_y = 0;
821 for( i = 0; i < p_meta->i_record_entries_count; i++ )
823 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioX" ) )
825 if( (!i_aspect_x && !p_meta->record[i].i_stream) ||
826 p_meta->record[i].i_stream ==
827 p_sp->i_stream_number )
828 i_aspect_x = p_meta->record[i].i_val;
830 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioY" ) )
832 if( (!i_aspect_y && !p_meta->record[i].i_stream) ||
833 p_meta->record[i].i_stream ==
834 p_sp->i_stream_number )
835 i_aspect_y = p_meta->record[i].i_val;
839 if( i_aspect_x && i_aspect_y )
841 fmt.video.i_aspect = i_aspect_x *
842 (int64_t)fmt.video.i_width * VOUT_ASPECT_FACTOR /
843 fmt.video.i_height / i_aspect_y;
847 tk->i_cat = VIDEO_ES;
848 tk->p_es = es_out_Add( p_demux->out, &fmt );
849 es_format_Clean( &fmt );
851 /* If there is a video track then use the index for seeking */
852 p_sys->b_index = b_index;
854 msg_Dbg( p_demux, "added new video stream(ID:%d)",
855 p_sp->i_stream_number );
857 else if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_extended_stream_header ) &&
858 p_sp->i_type_specific_data_length >= 64 )
860 /* Now follows a 64 byte header of which we don't know much */
862 guid_t *p_ref = (guid_t *)p_sp->p_type_specific_data;
863 uint8_t *p_data = p_sp->p_type_specific_data + 64;
864 unsigned int i_data = p_sp->i_type_specific_data_length - 64;
866 msg_Dbg( p_demux, "Ext stream header detected. datasize = %d", p_sp->i_type_specific_data_length );
867 if( ASF_CmpGUID( p_ref, &asf_object_extended_stream_type_audio ) &&
868 i_data >= sizeof( WAVEFORMATEX ) - 2)
871 es_format_Init( &fmt, AUDIO_ES, 0 );
872 i_format = GetWLE( &p_data[0] );
874 fmt.i_codec = VLC_FOURCC( 'a','5','2',' ');
876 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
877 fmt.audio.i_channels = GetWLE( &p_data[2] );
878 fmt.audio.i_rate = GetDWLE( &p_data[4] );
879 fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
880 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
881 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
882 fmt.b_packetized = VLC_TRUE;
884 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
885 i_format != WAVE_FORMAT_MPEGLAYER3 &&
886 i_format != WAVE_FORMAT_MPEG )
888 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
889 p_sp->i_type_specific_data_length -
890 sizeof( WAVEFORMATEX ) );
891 fmt.p_extra = malloc( fmt.i_extra );
892 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
896 tk->i_cat = AUDIO_ES;
897 tk->p_es = es_out_Add( p_demux->out, &fmt );
898 es_format_Clean( &fmt );
900 msg_Dbg( p_demux, "added new audio stream (codec:0x%x,ID:%d)",
901 i_format, p_sp->i_stream_number );
906 tk->i_cat = UNKNOWN_ES;
907 msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
908 p_sp->i_stream_number );
912 p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
913 if( p_sys->p_root->p_data->i_object_size != 0 )
915 p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
916 p_sys->p_root->p_data->i_object_size;
919 { /* live/broacast */
920 p_sys->i_data_end = -1;
923 /* go to first packet */
924 stream_Seek( p_demux->s, p_sys->i_data_begin );
926 /* try to calculate movie time */
927 if( p_sys->p_fp->i_data_packets_count > 0 )
930 int64_t i_size = stream_Size( p_demux->s );
932 if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
934 i_size = p_sys->i_data_end;
937 /* real number of packets */
938 i_count = ( i_size - p_sys->i_data_begin ) /
939 p_sys->p_fp->i_min_data_packet_size;
941 /* calculate the time duration in micro-s */
942 p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
944 (mtime_t)p_sys->p_fp->i_data_packets_count;
946 if( p_sys->i_length > 0 )
948 p_sys->i_bitrate = 8 * i_size * (int64_t)1000000 / p_sys->i_length;
952 /* Create meta information */
953 p_sys->meta = vlc_meta_New();
955 if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
956 &asf_object_content_description_guid, 0 ) ) )
958 if( p_cd->psz_title && *p_cd->psz_title )
960 vlc_meta_SetTitle( p_sys->meta, p_cd->psz_title );
962 if( p_cd->psz_artist && *p_cd->psz_artist )
964 vlc_meta_SetArtist( p_sys->meta, p_cd->psz_artist );
966 if( p_cd->psz_copyright && *p_cd->psz_copyright )
968 vlc_meta_SetCopyright( p_sys->meta, p_cd->psz_copyright );
970 if( p_cd->psz_description && *p_cd->psz_description )
972 vlc_meta_SetDescription( p_sys->meta, p_cd->psz_description );
974 if( p_cd->psz_rating && *p_cd->psz_rating )
976 vlc_meta_SetRating( p_sys->meta, p_cd->psz_rating );
979 /// \tood Fix Child meta for ASF tracks
981 for( i_stream = 0, i = 0; i < 128; i++ )
983 asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
984 &asf_object_codec_list_guid, 0 );
986 if( p_sys->track[i] )
988 vlc_meta_t *tk = vlc_meta_New();
989 TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
991 if( p_cl && i_stream < p_cl->i_codec_entries_count )
993 if( p_cl->codec[i_stream].psz_name &&
994 *p_cl->codec[i_stream].psz_name )
996 vlc_meta_Add( tk, VLC_META_CODEC_NAME,
997 p_cl->codec[i_stream].psz_name );
999 if( p_cl->codec[i_stream].psz_description &&
1000 *p_cl->codec[i_stream].psz_description )
1002 vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
1003 p_cl->codec[i_stream].psz_description );
1011 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1015 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1016 return VLC_EGENERIC;
1018 /*****************************************************************************
1020 *****************************************************************************/
1021 static void DemuxEnd( demux_t *p_demux )
1023 demux_sys_t *p_sys = p_demux->p_sys;
1028 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1029 p_sys->p_root = NULL;
1033 vlc_meta_Delete( p_sys->meta );
1037 for( i = 0; i < 128; i++ )
1039 asf_track_t *tk = p_sys->track[i];
1045 block_ChainRelease( tk->p_frame );
1049 es_out_Del( p_demux->out, tk->p_es );
1053 p_sys->track[i] = 0;