1 /*****************************************************************************
2 * asf.c : ASF demux module
3 *****************************************************************************
4 * Copyright © 2002-2004, 2006-2008, 2010 VLC authors and VideoLAN
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_demux.h>
34 #include <vlc_dialog.h>
36 #include <vlc_meta.h> /* vlc_meta_Set*, vlc_meta_New */
37 #include <vlc_access.h> /* GET_PRIVATE_ID_STATE */
38 #include <vlc_codecs.h> /* VLC_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/WMV demuxer") )
57 set_capability( "demux", 200 )
58 set_callbacks( Open, Close )
59 add_shortcut( "asf", "wmv" )
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 */
99 unsigned int i_seek_track;
100 unsigned int i_wait_keyframe;
105 static mtime_t GetMoviePTS( demux_sys_t * );
106 static int DemuxInit( demux_t * );
107 static void DemuxEnd( demux_t * );
108 static int DemuxPacket( demux_t * );
110 /*****************************************************************************
111 * Open: check file and initializes ASF structures
112 *****************************************************************************/
113 static int Open( vlc_object_t * p_this )
115 demux_t *p_demux = (demux_t *)p_this;
118 const uint8_t *p_peek;
120 /* A little test to see if it could be a asf stream */
121 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 ) return VLC_EGENERIC;
123 ASF_GetGUID( &guid, p_peek );
124 if( !guidcmp( &guid, &asf_object_header_guid ) ) return VLC_EGENERIC;
126 /* Set p_demux fields */
127 p_demux->pf_demux = Demux;
128 p_demux->pf_control = Control;
129 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
131 /* Load the headers */
132 if( DemuxInit( p_demux ) )
141 /*****************************************************************************
142 * Demux: read packet and send them to decoders
143 *****************************************************************************/
144 static int Demux( demux_t *p_demux )
146 demux_sys_t *p_sys = p_demux->p_sys;
150 const uint8_t *p_peek;
152 mtime_t i_time_begin = GetMoviePTS( p_sys );
155 if( !vlc_object_alive (p_demux) )
158 /* FIXME: returns EOF too early for some mms streams */
159 if( p_sys->i_data_end >= 0 &&
160 stream_Tell( p_demux->s ) >= p_sys->i_data_end )
164 /* Check if we have concatenated files */
165 if( stream_Peek( p_demux->s, &p_peek, 16 ) == 16 )
169 ASF_GetGUID( &guid, p_peek );
170 if( guidcmp( &guid, &asf_object_header_guid ) )
172 msg_Warn( p_demux, "found a new ASF header" );
173 /* We end this stream */
176 /* And we prepare to read the next one */
177 if( DemuxInit( p_demux ) )
179 msg_Err( p_demux, "failed to load the new header" );
180 dialog_Fatal( p_demux, _("Could not demux ASF stream"), "%s",
181 _("VLC failed to load the ASF header.") );
184 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
189 /* Read and demux a packet */
190 if( ( i_result = DemuxPacket( p_demux ) ) <= 0 )
194 if( i_time_begin == -1 )
196 i_time_begin = GetMoviePTS( p_sys );
200 i_length = GetMoviePTS( p_sys ) - i_time_begin;
201 if( i_length < 0 || i_length >= 40 * 1000 ) break;
206 p_sys->i_time = GetMoviePTS( p_sys );
207 if( p_sys->i_time >= 0 )
209 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time+1 );
215 /*****************************************************************************
216 * Close: frees unused data
217 *****************************************************************************/
218 static void Close( vlc_object_t * p_this )
220 demux_t *p_demux = (demux_t *)p_this;
224 free( p_demux->p_sys );
227 /*****************************************************************************
228 * SeekIndex: goto to i_date or i_percent
229 *****************************************************************************/
230 static int SeekPercent( demux_t *p_demux, int i_query, va_list args )
232 demux_sys_t *p_sys = p_demux->p_sys;
233 p_sys->i_wait_keyframe = p_sys->i_seek_track ? 50 : 0;
234 return demux_vaControlHelper( p_demux->s, p_sys->i_data_begin,
235 p_sys->i_data_end, p_sys->i_bitrate,
236 p_sys->p_fp->i_min_data_packet_size,
240 static int SeekIndex( demux_t *p_demux, mtime_t i_date, float f_pos )
242 demux_sys_t *p_sys = p_demux->p_sys;
243 asf_object_index_t *p_index;
245 msg_Dbg( p_demux, "seek with index: %i seconds, position %f",
246 i_date >= 0 ? (int)(i_date/1000000) : -1, f_pos );
249 i_date = p_sys->i_length * f_pos;
251 p_index = ASF_FindObject( p_sys->p_root, &asf_object_simple_index_guid, 0 );
253 uint64_t i_entry = i_date * 10 / p_index->i_index_entry_time_interval;
254 if( i_entry >= p_index->i_index_entry_count )
256 msg_Warn( p_demux, "Incomplete index" );
260 p_sys->i_wait_keyframe = p_sys->i_seek_track ? 50 : 0;
262 uint64_t i_offset = (uint64_t)p_index->index_entry[i_entry].i_packet_number *
263 p_sys->p_fp->i_min_data_packet_size;
264 return stream_Seek( p_demux->s, p_sys->i_data_begin + i_offset );
267 static void SeekPrepare( demux_t *p_demux )
269 demux_sys_t *p_sys = p_demux->p_sys;
272 for( int i = 0; i < 128 ; i++ )
274 asf_track_t *tk = p_sys->track[i];
280 block_ChainRelease( tk->p_frame );
285 /*****************************************************************************
287 *****************************************************************************/
288 static int Control( demux_t *p_demux, int i_query, va_list args )
290 demux_sys_t *p_sys = p_demux->p_sys;
297 case DEMUX_GET_LENGTH:
298 pi64 = (int64_t*)va_arg( args, int64_t * );
299 *pi64 = p_sys->i_length;
303 pi64 = (int64_t*)va_arg( args, int64_t * );
304 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
305 *pi64 = p_sys->i_time;
309 SeekPrepare( p_demux );
311 if( p_sys->b_index && p_sys->i_length > 0 )
314 va_copy( acpy, args );
315 i64 = (int64_t)va_arg( acpy, int64_t );
318 if( !SeekIndex( p_demux, i64, -1 ) )
321 return SeekPercent( p_demux, i_query, args );
323 case DEMUX_GET_POSITION:
324 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
325 if( p_sys->i_length > 0 )
327 pf = (double*)va_arg( args, double * );
328 *pf = p_sys->i_time / (double)p_sys->i_length;
331 return demux_vaControlHelper( p_demux->s, p_sys->i_data_begin,
332 p_sys->i_data_end, p_sys->i_bitrate,
333 p_sys->p_fp->i_min_data_packet_size,
336 case DEMUX_SET_POSITION:
337 SeekPrepare( p_demux );
339 if( p_sys->b_index && p_sys->i_length > 0 )
342 va_copy( acpy, args );
343 f = (double)va_arg( acpy, double );
346 if( !SeekIndex( p_demux, -1, f ) )
349 return SeekPercent( p_demux, i_query, args );
352 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
353 vlc_meta_Merge( p_meta, p_sys->meta );
357 return demux_vaControlHelper( p_demux->s, p_sys->i_data_begin,
358 p_sys->i_data_end, p_sys->i_bitrate,
359 p_sys->p_fp->i_min_data_packet_size,
364 /*****************************************************************************
366 *****************************************************************************/
367 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
372 for( i = 0; i < 128 ; i++ )
374 asf_track_t *tk = p_sys->track[i];
376 if( tk && tk->p_es && tk->i_time > 0)
378 if( i_time < 0 ) i_time = tk->i_time;
379 else i_time = __MIN( i_time, tk->i_time );
386 static inline int GetValue2b(int *var, const uint8_t *p, int *skip, int left, int bits)
393 *var = p[*skip]; *skip += 1;
398 *var = GetWLE(&p[*skip]); *skip += 2;
403 *var = GetDWLE(&p[*skip]); *skip += 4;
420 /* buffer handling for this ASF packet */
422 const uint8_t *p_peek;
426 static void SendPacket(demux_t *p_demux, asf_track_t *tk)
428 demux_sys_t *p_sys = p_demux->p_sys;
430 block_t *p_gather = block_ChainGather( tk->p_frame );
432 if( p_gather->i_dts > VLC_TS_INVALID )
433 tk->i_time = p_gather->i_dts - VLC_TS_0;
435 if( p_sys->i_time < 0 )
436 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_gather->i_dts );
438 es_out_Send( p_demux->out, tk->p_es, p_gather );
443 static int DemuxSubPayload(demux_t *p_demux, asf_track_t *tk,
444 int i_sub_payload_data_length, mtime_t i_pts, int i_media_object_offset)
446 /* FIXME I don't use i_media_object_number, sould I ? */
447 if( tk->p_frame && i_media_object_offset == 0 )
448 SendPacket(p_demux, tk);
450 block_t *p_frag = stream_Block( p_demux->s, i_sub_payload_data_length );
451 if( p_frag == NULL ) {
452 msg_Warn( p_demux, "cannot read data" );
456 if( tk->p_frame == NULL ) {
457 p_frag->i_pts = VLC_TS_0 + i_pts;
458 p_frag->i_dts = VLC_TS_0 + p_frag->i_pts; //FIXME: VLC_TS_0 * 2 ?
459 if( tk->i_cat == VIDEO_ES )
460 p_frag->i_pts = VLC_TS_INVALID;
463 block_ChainAppend( &tk->p_frame, p_frag );
468 static int DemuxPayload(demux_t *p_demux, struct asf_packet_t *pkt, int i_payload)
470 demux_sys_t *p_sys = p_demux->p_sys;
472 if( pkt->i_skip >= pkt->left )
475 int i_packet_keyframe = pkt->p_peek[pkt->i_skip] >> 7;
476 unsigned int i_stream_number = pkt->p_peek[pkt->i_skip++] & 0x7f;
478 int i_media_object_number = 0;
479 if (GetValue2b(&i_media_object_number, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->property >> 4) < 0)
481 int i_media_object_offset = 0;
482 if (GetValue2b(&i_media_object_offset, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->property >> 2) < 0)
484 int i_replicated_data_length = 0;
485 if (GetValue2b(&i_replicated_data_length, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->property) < 0)
489 if( i_replicated_data_length > 1 ) // should be at least 8 bytes
491 i_pts = (mtime_t)GetDWLE( pkt->p_peek + pkt->i_skip + 4 );
492 pkt->i_skip += i_replicated_data_length;
494 if( pkt->i_skip >= pkt->left )
497 else if( i_replicated_data_length == 1 )
499 i_pts = (mtime_t)i_media_object_offset + (mtime_t)pkt->p_peek[pkt->i_skip] * i_payload;
501 i_media_object_offset = 0;
505 i_pts = (mtime_t)pkt->send_time * 1000;
508 i_pts -= p_sys->p_fp->i_preroll;
509 if (i_pts < 0) i_pts = 0; // FIXME?
510 i_pts *= 1000; // FIXME ?
512 int i_payload_data_length = 0;
513 if( pkt->multiple ) {
514 if (GetValue2b(&i_payload_data_length, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->length_type) < 0)
517 i_payload_data_length = pkt->length - pkt->padding_length - pkt->i_skip;
519 if( i_payload_data_length < 0 || i_payload_data_length > pkt->left )
524 "payload(%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
525 i_payload + 1, i_stream_number, i_media_object_number,
526 i_media_object_offset, i_replicated_data_length, i_payload_data_length );
529 asf_track_t *tk = p_sys->track[i_stream_number];
532 msg_Warn( p_demux, "undeclared stream[Id 0x%x]", i_stream_number );
536 if( p_sys->i_wait_keyframe && !i_media_object_offset &&
537 (i_stream_number != p_sys->i_seek_track || !i_packet_keyframe) )
539 p_sys->i_wait_keyframe--;
542 p_sys->i_wait_keyframe = 0;
547 while (i_payload_data_length)
549 int i_sub_payload_data_length = i_payload_data_length;
550 if( i_replicated_data_length == 1 )
551 i_sub_payload_data_length = pkt->p_peek[pkt->i_skip++];
553 stream_Read(p_demux->s, NULL, pkt->i_skip);
555 if (DemuxSubPayload(p_demux, tk, i_sub_payload_data_length, i_pts,
556 i_media_object_offset) < 0)
559 pkt->left -= pkt->i_skip + i_sub_payload_data_length;
561 if( pkt->left > 0 && stream_Peek( p_demux->s, &pkt->p_peek, pkt->left ) < pkt->left ) {
562 msg_Warn( p_demux, "cannot peek, EOF ?" );
566 i_payload_data_length -= i_sub_payload_data_length;
572 pkt->i_skip += i_payload_data_length;
576 static int DemuxPacket( demux_t *p_demux )
578 demux_sys_t *p_sys = p_demux->p_sys;
580 int i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
582 const uint8_t *p_peek;
583 if( stream_Peek( p_demux->s, &p_peek,i_data_packet_min)<i_data_packet_min )
585 msg_Warn( p_demux, "cannot peek while getting new packet, EOF ?" );
590 /* *** parse error correction if present *** */
593 unsigned int i_error_correction_data_length = p_peek[0] & 0x0f;
594 unsigned int i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01;
595 unsigned int i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03;
596 i_skip += 1; // skip error correction flags
598 if( i_error_correction_length_type != 0x00 ||
599 i_opaque_data_present != 0 ||
600 i_error_correction_data_length != 0x02 )
602 goto loop_error_recovery;
605 i_skip += i_error_correction_data_length;
608 msg_Warn( p_demux, "no error correction" );
611 if( i_skip + 2 >= i_data_packet_min )
612 goto loop_error_recovery;
614 struct asf_packet_t pkt;
615 int i_packet_flags = p_peek[i_skip]; i_skip++;
616 pkt.property = p_peek[i_skip]; i_skip++;
617 pkt.multiple = !!(i_packet_flags&0x01);
619 pkt.length = i_data_packet_min;
620 pkt.padding_length = 0;
622 if (GetValue2b(&pkt.length, p_peek, &i_skip, i_data_packet_min - i_skip, i_packet_flags >> 5) < 0)
623 goto loop_error_recovery;
624 int i_packet_sequence;
625 if (GetValue2b(&i_packet_sequence, p_peek, &i_skip, i_data_packet_min - i_skip, i_packet_flags >> 1) < 0)
626 goto loop_error_recovery;
627 if (GetValue2b(&pkt.padding_length, p_peek, &i_skip, i_data_packet_min - i_skip, i_packet_flags >> 3) < 0)
628 goto loop_error_recovery;
630 if( pkt.padding_length > pkt.length )
632 msg_Warn( p_demux, "Too large padding: %d", pkt.padding_length );
633 goto loop_error_recovery;
636 if( pkt.length < i_data_packet_min )
638 /* if packet length too short, there is extra padding */
639 pkt.padding_length += i_data_packet_min - pkt.length;
640 pkt.length = i_data_packet_min;
643 pkt.send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
644 /* uint16_t i_packet_duration = GetWLE( p_peek + i_skip ); */ i_skip += 2;
646 if( pkt.length <= 0 || stream_Peek( p_demux->s, &p_peek, pkt.length ) < pkt.length)
648 msg_Warn( p_demux, "cannot peek, EOF ?" );
652 int i_payload_count = 1;
653 pkt.length_type = 0x02; //unused
656 i_payload_count = p_peek[i_skip] & 0x3f;
657 pkt.length_type = ( p_peek[i_skip] >> 6 )&0x03;
662 msg_Dbg(p_demux, "%d payloads", i_payload_count);
667 pkt.left = pkt.length;
669 for( int i_payload = 0; i_payload < i_payload_count ; i_payload++ )
670 if (DemuxPayload(p_demux, &pkt, i_payload) < 0)
676 if( pkt.left > pkt.padding_length )
677 msg_Warn( p_demux, "Didn't read %d bytes in the packet",
678 pkt.left - pkt.padding_length );
679 else if( pkt.left < pkt.padding_length )
680 msg_Warn( p_demux, "Read %d too much bytes in the packet",
681 pkt.padding_length - pkt.left );
683 if( stream_Read( p_demux->s, NULL, pkt.left ) < pkt.left )
685 msg_Err( p_demux, "cannot skip data, EOF ?" );
693 msg_Warn( p_demux, "unsupported packet header" );
694 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
696 msg_Err( p_demux, "unsupported packet header, fatal error" );
699 if( stream_Read( p_demux->s, NULL, i_data_packet_min ) != i_data_packet_min )
701 msg_Warn( p_demux, "cannot skip data, EOF ?" );
708 /*****************************************************************************
710 *****************************************************************************/
711 static int DemuxInit( demux_t *p_demux )
713 demux_sys_t *p_sys = p_demux->p_sys;
718 p_sys->i_bitrate = 0;
719 p_sys->p_root = NULL;
723 p_sys->i_seek_track = 0;
724 p_sys->i_wait_keyframe = 0;
725 for( int i = 0; i < 128; i++ )
727 p_sys->track[i] = NULL;
729 p_sys->i_data_begin = -1;
730 p_sys->i_data_end = -1;
733 /* Now load all object ( except raw data ) */
735 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
736 if( !(p_sys->p_root = ASF_ReadObjectRoot(p_demux->s, b_seekable)) )
738 msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
741 p_sys->p_fp = p_sys->p_root->p_fp;
743 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
745 msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
749 p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
750 &asf_object_stream_properties_guid );
751 if( p_sys->i_track <= 0 )
753 msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
756 msg_Dbg( p_demux, "found %d streams", p_sys->i_track );
758 /* check if index is available */
759 asf_object_index_t *p_index = ASF_FindObject( p_sys->p_root,
760 &asf_object_simple_index_guid, 0 );
761 const bool b_index = p_index && p_index->i_index_entry_count;
763 /* Find the extended header if any */
764 asf_object_t *p_hdr_ext = ASF_FindObject( p_sys->p_root->p_hdr,
765 &asf_object_header_extension_guid, 0 );
767 asf_object_language_list_t *p_languages = NULL;
769 p_languages = ASF_FindObject( p_hdr_ext, &asf_object_language_list, 0 );
771 for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
774 asf_object_stream_properties_t *p_sp;
775 asf_object_extended_stream_properties_t *p_esp;
776 bool b_access_selected;
778 p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
779 &asf_object_stream_properties_guid,
783 tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
784 memset( tk, 0, sizeof( asf_track_t ) );
791 /* Check (in case of mms) if this track is selected (ie will receive data) */
792 if( !stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_GET_PRIVATE_ID_STATE,
793 p_sp->i_stream_number, &b_access_selected ) &&
796 tk->i_cat = UNKNOWN_ES;
797 msg_Dbg( p_demux, "ignoring not selected stream(ID:%d) (by access)",
798 p_sp->i_stream_number );
802 /* Find the associated extended_stream_properties if any */
805 int i_ext_stream = ASF_CountObject( p_hdr_ext,
806 &asf_object_extended_stream_properties_guid );
807 for( int i = 0; i < i_ext_stream; i++ )
809 asf_object_t *p_tmp =
810 ASF_FindObject( p_hdr_ext,
811 &asf_object_extended_stream_properties_guid, i );
812 if( p_tmp->ext_stream.i_stream_number == p_sp->i_stream_number )
814 p_esp = &p_tmp->ext_stream;
822 if( guidcmp( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
823 p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
825 uint8_t *p_data = p_sp->p_type_specific_data;
828 es_format_Init( &fmt, AUDIO_ES, 0 );
829 i_format = GetWLE( &p_data[0] );
830 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
831 fmt.audio.i_channels = GetWLE( &p_data[2] );
832 fmt.audio.i_rate = GetDWLE( &p_data[4] );
833 fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
834 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
835 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
837 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
838 i_format != WAVE_FORMAT_MPEGLAYER3 &&
839 i_format != WAVE_FORMAT_MPEG )
841 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
842 p_sp->i_type_specific_data_length -
843 sizeof( WAVEFORMATEX ) );
844 fmt.p_extra = malloc( fmt.i_extra );
845 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
849 msg_Dbg( p_demux, "added new audio stream(codec:0x%x,ID:%d)",
850 GetWLE( p_data ), p_sp->i_stream_number );
852 else if( guidcmp( &p_sp->i_stream_type,
853 &asf_object_stream_type_video ) &&
854 p_sp->i_type_specific_data_length >= 11 +
855 sizeof( VLC_BITMAPINFOHEADER ) )
857 uint8_t *p_data = &p_sp->p_type_specific_data[11];
859 es_format_Init( &fmt, VIDEO_ES,
860 VLC_FOURCC( p_data[16], p_data[17],
861 p_data[18], p_data[19] ) );
862 fmt.video.i_width = GetDWLE( p_data + 4 );
863 fmt.video.i_height= GetDWLE( p_data + 8 );
865 if( p_esp && p_esp->i_average_time_per_frame > 0 )
867 fmt.video.i_frame_rate = 10000000;
868 fmt.video.i_frame_rate_base = p_esp->i_average_time_per_frame;
871 if( fmt.i_codec == VLC_FOURCC( 'D','V','R',' ') )
873 /* DVR-MS special ASF */
874 fmt.i_codec = VLC_FOURCC( 'm','p','g','2' ) ;
875 fmt.b_packetized = false;
878 if( p_sp->i_type_specific_data_length > 11 +
879 sizeof( VLC_BITMAPINFOHEADER ) )
881 fmt.i_extra = __MIN( GetDWLE( p_data ),
882 p_sp->i_type_specific_data_length - 11 -
883 sizeof( VLC_BITMAPINFOHEADER ) );
884 fmt.p_extra = malloc( fmt.i_extra );
885 memcpy( fmt.p_extra, &p_data[sizeof( VLC_BITMAPINFOHEADER )],
889 /* Look for an aspect ratio */
890 if( p_sys->p_root->p_metadata )
892 asf_object_metadata_t *p_meta = p_sys->p_root->p_metadata;
893 int i_aspect_x = 0, i_aspect_y = 0;
896 for( i = 0; i < p_meta->i_record_entries_count; i++ )
898 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioX" ) )
900 if( (!i_aspect_x && !p_meta->record[i].i_stream) ||
901 p_meta->record[i].i_stream ==
902 p_sp->i_stream_number )
903 i_aspect_x = p_meta->record[i].i_val;
905 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioY" ) )
907 if( (!i_aspect_y && !p_meta->record[i].i_stream) ||
908 p_meta->record[i].i_stream ==
909 p_sp->i_stream_number )
910 i_aspect_y = p_meta->record[i].i_val;
914 if( i_aspect_x && i_aspect_y )
916 fmt.video.i_sar_num = i_aspect_x;
917 fmt.video.i_sar_den = i_aspect_y;
921 /* If there is a video track then use the index for seeking */
922 p_sys->b_index = b_index;
924 msg_Dbg( p_demux, "added new video stream(ID:%d)",
925 p_sp->i_stream_number );
927 else if( guidcmp( &p_sp->i_stream_type, &asf_object_extended_stream_header ) &&
928 p_sp->i_type_specific_data_length >= 64 )
930 /* Now follows a 64 byte header of which we don't know much */
931 guid_t *p_ref = (guid_t *)p_sp->p_type_specific_data;
932 uint8_t *p_data = p_sp->p_type_specific_data + 64;
933 unsigned int i_data = p_sp->i_type_specific_data_length - 64;
935 msg_Dbg( p_demux, "Ext stream header detected. datasize = %d", p_sp->i_type_specific_data_length );
936 if( guidcmp( p_ref, &asf_object_extended_stream_type_audio ) &&
937 i_data >= sizeof( WAVEFORMATEX ) - 2)
940 es_format_Init( &fmt, AUDIO_ES, 0 );
941 i_format = GetWLE( &p_data[0] );
943 fmt.i_codec = VLC_CODEC_A52;
945 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
946 fmt.audio.i_channels = GetWLE( &p_data[2] );
947 fmt.audio.i_rate = GetDWLE( &p_data[4] );
948 fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
949 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
950 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
951 fmt.b_packetized = true;
953 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
954 i_format != WAVE_FORMAT_MPEGLAYER3 &&
955 i_format != WAVE_FORMAT_MPEG )
957 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
958 p_sp->i_type_specific_data_length -
959 sizeof( WAVEFORMATEX ) );
960 fmt.p_extra = malloc( fmt.i_extra );
961 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
965 msg_Dbg( p_demux, "added new audio stream (codec:0x%x,ID:%d)",
966 i_format, p_sp->i_stream_number );
970 es_format_Init( &fmt, UNKNOWN_ES, 0 );
975 es_format_Init( &fmt, UNKNOWN_ES, 0 );
978 tk->i_cat = fmt.i_cat;
979 if( fmt.i_cat != UNKNOWN_ES )
981 if( p_esp && p_languages &&
982 p_esp->i_language_index >= 0 &&
983 p_esp->i_language_index < p_languages->i_language )
985 fmt.psz_language = strdup( p_languages->ppsz_language[p_esp->i_language_index] );
987 if( fmt.psz_language && (p = strchr( fmt.psz_language, '-' )) )
991 /* Set the track on which we'll do our seeking to the first video track */
992 if(!p_sys->i_seek_track && fmt.i_cat == VIDEO_ES)
993 p_sys->i_seek_track = p_sp->i_stream_number;
995 tk->p_es = es_out_Add( p_demux->out, &fmt );
999 msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
1000 p_sp->i_stream_number );
1002 es_format_Clean( &fmt );
1005 p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
1006 if( p_sys->p_root->p_data->i_object_size != 0 )
1008 p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
1009 p_sys->p_root->p_data->i_object_size;
1012 { /* live/broacast */
1013 p_sys->i_data_end = -1;
1016 /* go to first packet */
1017 stream_Seek( p_demux->s, p_sys->i_data_begin );
1019 /* try to calculate movie time */
1020 if( p_sys->p_fp->i_data_packets_count > 0 )
1023 int64_t i_size = stream_Size( p_demux->s );
1025 if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
1027 i_size = p_sys->i_data_end;
1030 /* real number of packets */
1031 i_count = ( i_size - p_sys->i_data_begin ) /
1032 p_sys->p_fp->i_min_data_packet_size;
1034 /* calculate the time duration in micro-s */
1035 p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
1037 (mtime_t)p_sys->p_fp->i_data_packets_count - p_sys->p_fp->i_preroll * 1000;
1038 if( p_sys->i_length < 0 )
1039 p_sys->i_length = 0;
1041 if( p_sys->i_length > 0 )
1043 p_sys->i_bitrate = 8 * i_size * (int64_t)1000000 / p_sys->i_length;
1047 /* Create meta information */
1048 p_sys->meta = vlc_meta_New();
1050 asf_object_content_description_t *p_cd;
1051 if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
1052 &asf_object_content_description_guid, 0 ) ) )
1054 if( p_cd->psz_title && *p_cd->psz_title )
1056 vlc_meta_SetTitle( p_sys->meta, p_cd->psz_title );
1058 if( p_cd->psz_artist && *p_cd->psz_artist )
1060 vlc_meta_SetArtist( p_sys->meta, p_cd->psz_artist );
1062 if( p_cd->psz_copyright && *p_cd->psz_copyright )
1064 vlc_meta_SetCopyright( p_sys->meta, p_cd->psz_copyright );
1066 if( p_cd->psz_description && *p_cd->psz_description )
1068 vlc_meta_SetDescription( p_sys->meta, p_cd->psz_description );
1070 if( p_cd->psz_rating && *p_cd->psz_rating )
1072 vlc_meta_SetRating( p_sys->meta, p_cd->psz_rating );
1075 /// \tood Fix Child meta for ASF tracks
1077 for( i_stream = 0, i = 0; i < 128; i++ )
1079 asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
1080 &asf_object_codec_list_guid, 0 );
1082 if( p_sys->track[i] )
1084 vlc_meta_t *tk = vlc_meta_New();
1085 TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
1087 if( p_cl && i_stream < p_cl->i_codec_entries_count )
1089 if( p_cl->codec[i_stream].psz_name &&
1090 *p_cl->codec[i_stream].psz_name )
1092 vlc_meta_Add( tk, VLC_META_CODEC_NAME,
1093 p_cl->codec[i_stream].psz_name );
1095 if( p_cl->codec[i_stream].psz_description &&
1096 *p_cl->codec[i_stream].psz_description )
1098 vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
1099 p_cl->codec[i_stream].psz_description );
1109 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1110 return VLC_EGENERIC;
1112 /*****************************************************************************
1114 *****************************************************************************/
1115 static void DemuxEnd( demux_t *p_demux )
1117 demux_sys_t *p_sys = p_demux->p_sys;
1121 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1122 p_sys->p_root = NULL;
1126 vlc_meta_Delete( p_sys->meta );
1130 for( int i = 0; i < 128; i++ )
1132 asf_track_t *tk = p_sys->track[i];
1138 block_ChainRelease( tk->p_frame );
1142 es_out_Del( p_demux->out, tk->p_es );
1146 p_sys->track[i] = 0;