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_dialog.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 )
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 dialog_Fatal( p_demux, _("Could not demux ASF stream"), "%s",
179 _("VLC failed to load the ASF header.") );
182 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
187 /* Read and demux a packet */
188 if( ( i_result = DemuxPacket( p_demux ) ) <= 0 )
192 if( i_time_begin == -1 )
194 i_time_begin = GetMoviePTS( p_sys );
198 i_length = GetMoviePTS( p_sys ) - i_time_begin;
199 if( i_length < 0 || i_length >= 40 * 1000 ) break;
204 p_sys->i_time = GetMoviePTS( p_sys );
205 if( p_sys->i_time >= 0 )
207 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time+1 );
213 /*****************************************************************************
214 * Close: frees unused data
215 *****************************************************************************/
216 static void Close( vlc_object_t * p_this )
218 demux_t *p_demux = (demux_t *)p_this;
222 free( p_demux->p_sys );
225 /*****************************************************************************
226 * SeekIndex: goto to i_date or i_percent
227 *****************************************************************************/
228 static int SeekPercent( demux_t *p_demux, int i_query, va_list args )
230 demux_sys_t *p_sys = p_demux->p_sys;
231 return demux_vaControlHelper( p_demux->s, p_sys->i_data_begin,
232 p_sys->i_data_end, p_sys->i_bitrate,
233 p_sys->p_fp->i_min_data_packet_size,
237 static int SeekIndex( demux_t *p_demux, mtime_t i_date, float f_pos )
239 demux_sys_t *p_sys = p_demux->p_sys;
240 asf_object_index_t *p_index;
242 msg_Dbg( p_demux, "seek with index: %i seconds, position %f",
243 i_date >= 0 ? (int)(i_date/1000000) : -1, f_pos );
246 i_date = p_sys->i_length * f_pos;
248 p_index = ASF_FindObject( p_sys->p_root, &asf_object_index_guid, 0 );
250 uint64_t i_entry = i_date * 10 / p_index->i_index_entry_time_interval;
251 if( i_entry >= p_index->i_index_entry_count )
253 msg_Warn( p_demux, "Incomplete index" );
257 uint64_t i_offset = (uint64_t)p_index->index_entry[i_entry].i_packet_number *
258 p_sys->p_fp->i_min_data_packet_size;
259 return stream_Seek( p_demux->s, p_sys->i_data_begin + i_offset );
262 static void SeekPrepare( demux_t *p_demux )
264 demux_sys_t *p_sys = p_demux->p_sys;
267 for( int i = 0; i < 128 ; i++ )
269 asf_track_t *tk = p_sys->track[i];
275 block_ChainRelease( tk->p_frame );
280 /*****************************************************************************
282 *****************************************************************************/
283 static int Control( demux_t *p_demux, int i_query, va_list args )
285 demux_sys_t *p_sys = p_demux->p_sys;
292 case DEMUX_GET_LENGTH:
293 pi64 = (int64_t*)va_arg( args, int64_t * );
294 *pi64 = p_sys->i_length;
298 pi64 = (int64_t*)va_arg( args, int64_t * );
299 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
300 *pi64 = p_sys->i_time;
304 SeekPrepare( p_demux );
306 if( p_sys->b_index && p_sys->i_length > 0 )
309 va_copy( acpy, args );
310 i64 = (int64_t)va_arg( acpy, int64_t );
313 if( !SeekIndex( p_demux, i64, -1 ) )
316 return SeekPercent( p_demux, i_query, args );
318 case DEMUX_GET_POSITION:
319 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
320 if( p_sys->i_length > 0 )
322 pf = (double*)va_arg( args, double * );
323 *pf = p_sys->i_time / (double)p_sys->i_length;
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,
331 case DEMUX_SET_POSITION:
332 SeekPrepare( p_demux );
334 if( p_sys->b_index && p_sys->i_length > 0 )
337 va_copy( acpy, args );
338 f = (double)va_arg( acpy, double );
341 if( !SeekIndex( p_demux, -1, f ) )
344 return SeekPercent( p_demux, i_query, args );
347 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
348 vlc_meta_Merge( p_meta, p_sys->meta );
352 return demux_vaControlHelper( p_demux->s, p_sys->i_data_begin,
353 p_sys->i_data_end, p_sys->i_bitrate,
354 p_sys->p_fp->i_min_data_packet_size,
359 /*****************************************************************************
361 *****************************************************************************/
362 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
367 for( i = 0; i < 128 ; i++ )
369 asf_track_t *tk = p_sys->track[i];
371 if( tk && tk->p_es && tk->i_time > 0)
373 if( i_time < 0 ) i_time = tk->i_time;
374 else i_time = __MIN( i_time, tk->i_time );
381 #define GETVALUE2b( bits, var, def ) \
382 switch( (bits)&0x03 ) \
384 case 1: var = p_peek[i_skip]; i_skip++; break; \
385 case 2: var = GetWLE( p_peek + i_skip ); i_skip+= 2; break; \
386 case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
388 default: var = def; break;\
391 static int DemuxPacket( demux_t *p_demux )
393 demux_sys_t *p_sys = p_demux->p_sys;
394 int i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
395 const uint8_t *p_peek;
398 int i_packet_size_left;
400 int i_packet_property;
402 int b_packet_multiple_payload;
404 int i_packet_sequence;
405 int i_packet_padding_length;
407 uint32_t i_packet_send_time;
408 uint16_t i_packet_duration;
411 int i_payload_length_type;
414 if( stream_Peek( p_demux->s, &p_peek,i_data_packet_min)<i_data_packet_min )
416 msg_Warn( p_demux, "cannot peek while getting new packet, EOF ?" );
421 /* *** parse error correction if present *** */
424 unsigned int i_error_correction_length_type;
425 unsigned int i_error_correction_data_length;
426 unsigned int i_opaque_data_present;
428 i_error_correction_data_length = p_peek[0] & 0x0f; // 4bits
429 i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01; // 1bit
430 i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
431 i_skip += 1; // skip error correction flags
433 if( i_error_correction_length_type != 0x00 ||
434 i_opaque_data_present != 0 ||
435 i_error_correction_data_length != 0x02 )
437 goto loop_error_recovery;
440 i_skip += i_error_correction_data_length;
444 msg_Warn( p_demux, "p_peek[0]&0x80 != 0x80" );
448 if( i_skip + 2 >= i_data_packet_min )
450 goto loop_error_recovery;
453 i_packet_flags = p_peek[i_skip]; i_skip++;
454 i_packet_property = p_peek[i_skip]; i_skip++;
456 b_packet_multiple_payload = i_packet_flags&0x01;
458 /* read some value */
459 GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
460 GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
461 GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
463 if( i_packet_padding_length > i_packet_length )
465 msg_Warn( p_demux, "Too large padding: %d", i_packet_padding_length );
466 goto loop_error_recovery;
469 if( i_packet_length < i_data_packet_min )
471 /* if packet length too short, there is extra padding */
472 i_packet_padding_length += i_data_packet_min - i_packet_length;
473 i_packet_length = i_data_packet_min;
476 i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
477 i_packet_duration = GetWLE( p_peek + i_skip ); i_skip += 2;
479 i_packet_size_left = i_packet_length;
481 if( b_packet_multiple_payload )
483 i_payload_count = p_peek[i_skip] & 0x3f;
484 i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
490 i_payload_length_type = 0x02; // unused
493 for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
497 int i_packet_keyframe;
499 int i_media_object_number;
500 int i_media_object_offset;
501 int i_replicated_data_length;
502 int i_payload_data_length;
503 int i_payload_data_pos;
504 int i_sub_payload_data_length;
510 if( i_skip >= i_packet_size_left )
512 /* prevent some segfault with invalid file */
516 i_packet_keyframe = p_peek[i_skip] >> 7;
517 i_stream_number = p_peek[i_skip++] & 0x7f;
519 GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
520 GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
521 GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
523 if( i_replicated_data_length > 1 ) // should be at least 8 bytes
525 i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
526 i_skip += i_replicated_data_length;
529 i_media_object_offset = i_tmp;
531 if( i_skip >= i_packet_size_left )
536 else if( i_replicated_data_length == 1 )
538 /* msg_Dbg( p_demux, "found compressed payload" ); */
540 i_pts = (mtime_t)i_tmp * 1000;
541 i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
543 i_media_object_offset = 0;
547 i_pts = (mtime_t)i_packet_send_time * 1000;
550 i_media_object_offset = i_tmp;
553 i_pts = __MAX( i_pts - p_sys->p_fp->i_preroll * 1000, 0 );
554 if( b_packet_multiple_payload )
556 GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
560 i_payload_data_length = i_packet_length -
561 i_packet_padding_length - i_skip;
564 if( i_payload_data_length < 0 || i_payload_data_length > i_packet_size_left )
570 "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
571 i_payload + 1, i_payload_count, i_stream_number, i_media_object_number,
572 i_media_object_offset, i_replicated_data_length, i_payload_data_length );
575 if( ( tk = p_sys->track[i_stream_number] ) == NULL )
578 "undeclared stream[Id 0x%x]", i_stream_number );
579 i_skip += i_payload_data_length;
580 continue; // over payload
585 i_skip += i_payload_data_length;
590 for( i_payload_data_pos = 0;
591 i_payload_data_pos < i_payload_data_length &&
592 i_packet_size_left > 0;
593 i_payload_data_pos += i_sub_payload_data_length )
598 // read sub payload length
599 if( i_replicated_data_length == 1 )
601 i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
602 i_payload_data_pos++;
606 i_sub_payload_data_length = i_payload_data_length;
609 /* FIXME I don't use i_media_object_number, sould I ? */
610 if( tk->p_frame && i_media_object_offset == 0 )
612 /* send complete packet to decoder */
613 block_t *p_gather = block_ChainGather( tk->p_frame );
615 if( p_gather->i_dts > VLC_TS_INVALID )
616 tk->i_time = p_gather->i_dts - VLC_TS_0;
618 if( p_sys->i_time < 0 )
619 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + tk->i_time );
621 es_out_Send( p_demux->out, tk->p_es, p_gather );
626 i_read = i_sub_payload_data_length + i_skip;
627 if( ( p_frag = stream_Block( p_demux->s, i_read ) ) == NULL )
629 msg_Warn( p_demux, "cannot read data" );
632 i_packet_size_left -= i_read;
634 p_frag->p_buffer += i_skip;
635 p_frag->i_buffer -= i_skip;
637 if( tk->p_frame == NULL )
639 p_frag->i_pts = VLC_TS_0 + i_pts + i_payload * (mtime_t)i_pts_delta;
640 if( tk->i_cat != VIDEO_ES )
641 p_frag->i_dts = VLC_TS_0 + p_frag->i_pts;
644 p_frag->i_dts = VLC_TS_0 + p_frag->i_pts;
645 p_frag->i_pts = VLC_TS_INVALID;
649 block_ChainAppend( &tk->p_frame, p_frag );
652 if( i_packet_size_left > 0 )
654 if( stream_Peek( p_demux->s, &p_peek, i_packet_size_left )
655 < i_packet_size_left )
657 msg_Warn( p_demux, "cannot peek, EOF ?" );
664 if( i_packet_size_left > 0 )
667 if( i_packet_size_left > i_packet_padding_length )
668 msg_Warn( p_demux, "Didn't read %d bytes in the packet",
669 i_packet_size_left - i_packet_padding_length );
670 else if( i_packet_size_left < i_packet_padding_length )
671 msg_Warn( p_demux, "Read %d too much bytes in the packet",
672 i_packet_padding_length - i_packet_size_left );
674 if( stream_Read( p_demux->s, NULL, i_packet_size_left )
675 < i_packet_size_left )
677 msg_Err( p_demux, "cannot skip data, EOF ?" );
685 msg_Warn( p_demux, "unsupported packet header" );
686 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
688 msg_Err( p_demux, "unsupported packet header, fatal error" );
691 if( stream_Read( p_demux->s, NULL, i_data_packet_min ) != i_data_packet_min )
693 msg_Warn( p_demux, "cannot skip data, EOF ?" );
700 /*****************************************************************************
702 *****************************************************************************/
703 static int DemuxInit( demux_t *p_demux )
705 demux_sys_t *p_sys = p_demux->p_sys;
710 p_sys->i_bitrate = 0;
711 p_sys->p_root = NULL;
715 for( int i = 0; i < 128; i++ )
717 p_sys->track[i] = NULL;
719 p_sys->i_data_begin = -1;
720 p_sys->i_data_end = -1;
723 /* Now load all object ( except raw data ) */
725 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
726 if( !(p_sys->p_root = ASF_ReadObjectRoot(p_demux->s, b_seekable)) )
728 msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
731 p_sys->p_fp = p_sys->p_root->p_fp;
733 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
735 msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
739 p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
740 &asf_object_stream_properties_guid );
741 if( p_sys->i_track <= 0 )
743 msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
746 msg_Dbg( p_demux, "found %d streams", p_sys->i_track );
748 /* check if index is available */
749 asf_object_index_t *p_index = ASF_FindObject( p_sys->p_root,
750 &asf_object_index_guid, 0 );
751 const bool b_index = p_index && p_index->i_index_entry_count;
753 /* Find the extended header if any */
754 asf_object_t *p_hdr_ext = ASF_FindObject( p_sys->p_root->p_hdr,
755 &asf_object_header_extension_guid, 0 );
757 asf_object_language_list_t *p_languages = NULL;
759 p_languages = ASF_FindObject( p_hdr_ext, &asf_object_language_list, 0 );
761 for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
764 asf_object_stream_properties_t *p_sp;
765 asf_object_extended_stream_properties_t *p_esp;
766 bool b_access_selected;
768 p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
769 &asf_object_stream_properties_guid,
773 tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
774 memset( tk, 0, sizeof( asf_track_t ) );
781 /* Check (in case of mms) if this track is selected (ie will receive data) */
782 if( !stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_GET_PRIVATE_ID_STATE,
783 p_sp->i_stream_number, &b_access_selected ) &&
786 tk->i_cat = UNKNOWN_ES;
787 msg_Dbg( p_demux, "ignoring not selected stream(ID:%d) (by access)",
788 p_sp->i_stream_number );
792 /* Find the associated extended_stream_properties if any */
795 int i_ext_stream = ASF_CountObject( p_hdr_ext,
796 &asf_object_extended_stream_properties );
797 for( int i = 0; i < i_ext_stream; i++ )
799 asf_object_t *p_tmp =
800 ASF_FindObject( p_hdr_ext,
801 &asf_object_extended_stream_properties, i );
802 if( p_tmp->ext_stream.i_stream_number == p_sp->i_stream_number )
804 p_esp = &p_tmp->ext_stream;
812 if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
813 p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
815 uint8_t *p_data = p_sp->p_type_specific_data;
818 es_format_Init( &fmt, AUDIO_ES, 0 );
819 i_format = GetWLE( &p_data[0] );
820 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
821 fmt.audio.i_channels = GetWLE( &p_data[2] );
822 fmt.audio.i_rate = GetDWLE( &p_data[4] );
823 fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
824 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
825 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
827 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
828 i_format != WAVE_FORMAT_MPEGLAYER3 &&
829 i_format != WAVE_FORMAT_MPEG )
831 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
832 p_sp->i_type_specific_data_length -
833 sizeof( WAVEFORMATEX ) );
834 fmt.p_extra = malloc( fmt.i_extra );
835 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
839 msg_Dbg( p_demux, "added new audio stream(codec:0x%x,ID:%d)",
840 GetWLE( p_data ), p_sp->i_stream_number );
842 else if( ASF_CmpGUID( &p_sp->i_stream_type,
843 &asf_object_stream_type_video ) &&
844 p_sp->i_type_specific_data_length >= 11 +
845 sizeof( BITMAPINFOHEADER ) )
847 uint8_t *p_data = &p_sp->p_type_specific_data[11];
849 es_format_Init( &fmt, VIDEO_ES,
850 VLC_FOURCC( p_data[16], p_data[17],
851 p_data[18], p_data[19] ) );
852 fmt.video.i_width = GetDWLE( p_data + 4 );
853 fmt.video.i_height= GetDWLE( p_data + 8 );
855 if( p_esp && p_esp->i_average_time_per_frame > 0 )
857 fmt.video.i_frame_rate = 10000000;
858 fmt.video.i_frame_rate_base = p_esp->i_average_time_per_frame;
861 if( fmt.i_codec == VLC_FOURCC( 'D','V','R',' ') )
863 /* DVR-MS special ASF */
864 fmt.i_codec = VLC_FOURCC( 'm','p','g','2' ) ;
865 fmt.b_packetized = false;
868 if( p_sp->i_type_specific_data_length > 11 +
869 sizeof( BITMAPINFOHEADER ) )
871 fmt.i_extra = __MIN( GetDWLE( p_data ),
872 p_sp->i_type_specific_data_length - 11 -
873 sizeof( BITMAPINFOHEADER ) );
874 fmt.p_extra = malloc( fmt.i_extra );
875 memcpy( fmt.p_extra, &p_data[sizeof( BITMAPINFOHEADER )],
879 /* Look for an aspect ratio */
880 if( p_sys->p_root->p_metadata )
882 asf_object_metadata_t *p_meta = p_sys->p_root->p_metadata;
883 int i_aspect_x = 0, i_aspect_y = 0;
886 for( i = 0; i < p_meta->i_record_entries_count; i++ )
888 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioX" ) )
890 if( (!i_aspect_x && !p_meta->record[i].i_stream) ||
891 p_meta->record[i].i_stream ==
892 p_sp->i_stream_number )
893 i_aspect_x = p_meta->record[i].i_val;
895 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioY" ) )
897 if( (!i_aspect_y && !p_meta->record[i].i_stream) ||
898 p_meta->record[i].i_stream ==
899 p_sp->i_stream_number )
900 i_aspect_y = p_meta->record[i].i_val;
904 if( i_aspect_x && i_aspect_y )
906 fmt.video.i_sar_num = i_aspect_x;
907 fmt.video.i_sar_den = i_aspect_y;
911 /* If there is a video track then use the index for seeking */
912 p_sys->b_index = b_index;
914 msg_Dbg( p_demux, "added new video stream(ID:%d)",
915 p_sp->i_stream_number );
917 else if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_extended_stream_header ) &&
918 p_sp->i_type_specific_data_length >= 64 )
920 /* Now follows a 64 byte header of which we don't know much */
921 guid_t *p_ref = (guid_t *)p_sp->p_type_specific_data;
922 uint8_t *p_data = p_sp->p_type_specific_data + 64;
923 unsigned int i_data = p_sp->i_type_specific_data_length - 64;
925 msg_Dbg( p_demux, "Ext stream header detected. datasize = %d", p_sp->i_type_specific_data_length );
926 if( ASF_CmpGUID( p_ref, &asf_object_extended_stream_type_audio ) &&
927 i_data >= sizeof( WAVEFORMATEX ) - 2)
930 es_format_Init( &fmt, AUDIO_ES, 0 );
931 i_format = GetWLE( &p_data[0] );
933 fmt.i_codec = VLC_CODEC_A52;
935 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
936 fmt.audio.i_channels = GetWLE( &p_data[2] );
937 fmt.audio.i_rate = GetDWLE( &p_data[4] );
938 fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
939 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
940 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
941 fmt.b_packetized = true;
943 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
944 i_format != WAVE_FORMAT_MPEGLAYER3 &&
945 i_format != WAVE_FORMAT_MPEG )
947 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
948 p_sp->i_type_specific_data_length -
949 sizeof( WAVEFORMATEX ) );
950 fmt.p_extra = malloc( fmt.i_extra );
951 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
955 msg_Dbg( p_demux, "added new audio stream (codec:0x%x,ID:%d)",
956 i_format, p_sp->i_stream_number );
960 es_format_Init( &fmt, UNKNOWN_ES, 0 );
965 es_format_Init( &fmt, UNKNOWN_ES, 0 );
968 tk->i_cat = fmt.i_cat;
969 if( fmt.i_cat != UNKNOWN_ES )
971 if( p_esp && p_languages &&
972 p_esp->i_language_index >= 0 &&
973 p_esp->i_language_index < p_languages->i_language )
975 fmt.psz_language = strdup( p_languages->ppsz_language[p_esp->i_language_index] );
977 if( fmt.psz_language && (p = strchr( fmt.psz_language, '-' )) )
981 tk->p_es = es_out_Add( p_demux->out, &fmt );
985 msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
986 p_sp->i_stream_number );
988 es_format_Clean( &fmt );
991 p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
992 if( p_sys->p_root->p_data->i_object_size != 0 )
994 p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
995 p_sys->p_root->p_data->i_object_size;
998 { /* live/broacast */
999 p_sys->i_data_end = -1;
1002 /* go to first packet */
1003 stream_Seek( p_demux->s, p_sys->i_data_begin );
1005 /* try to calculate movie time */
1006 if( p_sys->p_fp->i_data_packets_count > 0 )
1009 int64_t i_size = stream_Size( p_demux->s );
1011 if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
1013 i_size = p_sys->i_data_end;
1016 /* real number of packets */
1017 i_count = ( i_size - p_sys->i_data_begin ) /
1018 p_sys->p_fp->i_min_data_packet_size;
1020 /* calculate the time duration in micro-s */
1021 p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
1023 (mtime_t)p_sys->p_fp->i_data_packets_count - p_sys->p_fp->i_preroll * 1000;
1024 if( p_sys->i_length < 0 )
1025 p_sys->i_length = 0;
1027 if( p_sys->i_length > 0 )
1029 p_sys->i_bitrate = 8 * i_size * (int64_t)1000000 / p_sys->i_length;
1033 /* Create meta information */
1034 p_sys->meta = vlc_meta_New();
1036 asf_object_content_description_t *p_cd;
1037 if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
1038 &asf_object_content_description_guid, 0 ) ) )
1040 if( p_cd->psz_title && *p_cd->psz_title )
1042 vlc_meta_SetTitle( p_sys->meta, p_cd->psz_title );
1044 if( p_cd->psz_artist && *p_cd->psz_artist )
1046 vlc_meta_SetArtist( p_sys->meta, p_cd->psz_artist );
1048 if( p_cd->psz_copyright && *p_cd->psz_copyright )
1050 vlc_meta_SetCopyright( p_sys->meta, p_cd->psz_copyright );
1052 if( p_cd->psz_description && *p_cd->psz_description )
1054 vlc_meta_SetDescription( p_sys->meta, p_cd->psz_description );
1056 if( p_cd->psz_rating && *p_cd->psz_rating )
1058 vlc_meta_SetRating( p_sys->meta, p_cd->psz_rating );
1061 /// \tood Fix Child meta for ASF tracks
1063 for( i_stream = 0, i = 0; i < 128; i++ )
1065 asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
1066 &asf_object_codec_list_guid, 0 );
1068 if( p_sys->track[i] )
1070 vlc_meta_t *tk = vlc_meta_New();
1071 TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
1073 if( p_cl && i_stream < p_cl->i_codec_entries_count )
1075 if( p_cl->codec[i_stream].psz_name &&
1076 *p_cl->codec[i_stream].psz_name )
1078 vlc_meta_Add( tk, VLC_META_CODEC_NAME,
1079 p_cl->codec[i_stream].psz_name );
1081 if( p_cl->codec[i_stream].psz_description &&
1082 *p_cl->codec[i_stream].psz_description )
1084 vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
1085 p_cl->codec[i_stream].psz_description );
1095 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1096 return VLC_EGENERIC;
1098 /*****************************************************************************
1100 *****************************************************************************/
1101 static void DemuxEnd( demux_t *p_demux )
1103 demux_sys_t *p_sys = p_demux->p_sys;
1108 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1109 p_sys->p_root = NULL;
1113 vlc_meta_Delete( p_sys->meta );
1117 for( i = 0; i < 128; i++ )
1119 asf_track_t *tk = p_sys->track[i];
1125 block_ChainRelease( tk->p_frame );
1129 es_out_Del( p_demux->out, tk->p_es );
1133 p_sys->track[i] = 0;