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 */
39 #include <vlc_input.h>
48 * - add support for the newly added object: language, bitrate,
49 * extended stream properties.
52 /*****************************************************************************
54 *****************************************************************************/
55 static int Open ( vlc_object_t * );
56 static void Close ( vlc_object_t * );
59 set_category( CAT_INPUT )
60 set_subcategory( SUBCAT_INPUT_DEMUX )
61 set_description( N_("ASF/WMV demuxer") )
62 set_capability( "demux", 200 )
63 set_callbacks( Open, Close )
64 add_shortcut( "asf", "wmv" )
68 /*****************************************************************************
70 *****************************************************************************/
71 static int Demux ( demux_t * );
72 static int Control( demux_t *, int i_query, va_list args );
73 static void FlushRemainingPackets( demux_t *p_demux );
75 #define MAX_ASF_TRACKS 128
82 es_format_t *p_fmt; /* format backup for video changes */
85 asf_object_stream_properties_t *p_sp;
86 asf_object_extended_stream_properties_t *p_esp;
90 block_t *p_frame; /* use to gather complete frame */
95 mtime_t i_time; /* s */
96 mtime_t i_length; /* length of file file */
97 uint64_t i_bitrate; /* global file bitrate */
99 asf_object_root_t *p_root;
100 asf_object_file_properties_t *p_fp;
102 unsigned int i_track;
103 asf_track_t *track[MAX_ASF_TRACKS]; /* track number is stored on 7 bits */
105 uint64_t i_data_begin;
110 uint8_t i_seek_track;
111 uint8_t i_access_selected_track[ES_CATEGORY_COUNT]; /* mms, depends on access algorithm */
112 unsigned int i_wait_keyframe;
117 static mtime_t GetMoviePTS( demux_sys_t * );
118 static int DemuxInit( demux_t * );
119 static void DemuxEnd( demux_t * );
120 static int DemuxPacket( demux_t * );
122 /*****************************************************************************
123 * Open: check file and initializes ASF structures
124 *****************************************************************************/
125 static int Open( vlc_object_t * p_this )
127 demux_t *p_demux = (demux_t *)p_this;
130 const uint8_t *p_peek;
132 /* A little test to see if it could be a asf stream */
133 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 ) return VLC_EGENERIC;
135 ASF_GetGUID( &guid, p_peek );
136 if( !guidcmp( &guid, &asf_object_header_guid ) ) return VLC_EGENERIC;
138 /* Set p_demux fields */
139 p_demux->pf_demux = Demux;
140 p_demux->pf_control = Control;
141 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
143 /* Load the headers */
144 if( DemuxInit( p_demux ) )
152 /*****************************************************************************
153 * Demux: read packet and send them to decoders
154 *****************************************************************************/
155 static int Demux( demux_t *p_demux )
157 demux_sys_t *p_sys = p_demux->p_sys;
159 for( int i=0; i<ES_CATEGORY_COUNT; i++ )
161 if ( p_sys->i_access_selected_track[i] > 0 )
163 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
164 p_sys->track[p_sys->i_access_selected_track[i]]->p_es, true );
165 p_sys->i_access_selected_track[i] = 0;
169 /* Get selected tracks, especially for computing PCR */
170 for( int i=0; i<MAX_ASF_TRACKS; i++ )
172 asf_track_t *tk = p_sys->track[i];
175 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, & tk->b_selected );
177 tk->b_selected = false;
182 const uint8_t *p_peek;
184 mtime_t i_time_begin = GetMoviePTS( p_sys );
187 if( !vlc_object_alive (p_demux) )
190 /* FIXME: returns EOF too early for some mms streams */
191 if( p_sys->i_data_end >= 0 &&
192 stream_Tell( p_demux->s ) >= p_sys->i_data_end )
196 /* Check if we have concatenated files */
197 if( stream_Peek( p_demux->s, &p_peek, 16 ) == 16 )
201 ASF_GetGUID( &guid, p_peek );
202 if( guidcmp( &guid, &asf_object_header_guid ) )
204 msg_Warn( p_demux, "found a new ASF header" );
205 /* We end this stream */
208 /* And we prepare to read the next one */
209 if( DemuxInit( p_demux ) )
211 msg_Err( p_demux, "failed to load the new header" );
212 dialog_Fatal( p_demux, _("Could not demux ASF stream"), "%s",
213 _("VLC failed to load the ASF header.") );
216 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
221 /* Read and demux a packet */
222 if( ( i_result = DemuxPacket( p_demux ) ) <= 0 )
224 FlushRemainingPackets( p_demux );
227 if( i_time_begin == -1 )
229 i_time_begin = GetMoviePTS( p_sys );
233 i_length = GetMoviePTS( p_sys ) - i_time_begin;
234 if( i_length < 0 || i_length >= 40 * 1000 ) break;
239 p_sys->i_time = GetMoviePTS( p_sys );
240 if( p_sys->i_time >= 0 )
243 msg_Dbg( p_demux, "Setting PCR to %"PRId64, p_sys->i_time );
245 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time+1 );
251 /*****************************************************************************
252 * Close: frees unused data
253 *****************************************************************************/
254 static void Close( vlc_object_t * p_this )
256 demux_t *p_demux = (demux_t *)p_this;
260 free( p_demux->p_sys );
263 /*****************************************************************************
264 * WaitKeyframe: computes the number of frames to wait for a keyframe
265 *****************************************************************************/
266 static void WaitKeyframe( demux_t *p_demux )
268 demux_sys_t *p_sys = p_demux->p_sys;
269 if ( ! p_sys->i_seek_track )
271 for ( int i=0; i<MAX_ASF_TRACKS; i++ )
273 asf_track_t *tk = p_sys->track[i];
274 if ( tk && tk->p_sp && tk->i_cat == VIDEO_ES && tk->b_selected )
276 p_sys->i_seek_track = tk->p_sp->i_stream_number;
282 if ( p_sys->i_seek_track )
284 /* Skip forward at least 1 min */
285 asf_track_t *tk = p_sys->track[p_sys->i_seek_track];
286 if ( tk->p_esp && tk->p_esp->i_average_time_per_frame )
288 /* 1 min if fastseek, otherwise 5 sec */
289 /* That's a guess for bandwidth */
290 uint64_t i_maxwaittime = ( p_sys->b_canfastseek ) ? 600000000 : 50000000;
291 i_maxwaittime /= tk->p_esp->i_average_time_per_frame;
292 p_sys->i_wait_keyframe = __MIN( i_maxwaittime, UINT_MAX );
296 p_sys->i_wait_keyframe = ( p_sys->b_canfastseek ) ? 25 * 30 : 25 * 5;
301 p_sys->i_wait_keyframe = 0;
306 /*****************************************************************************
307 * SeekIndex: goto to i_date or i_percent
308 *****************************************************************************/
309 static int SeekPercent( demux_t *p_demux, int i_query, va_list args )
311 demux_sys_t *p_sys = p_demux->p_sys;
313 WaitKeyframe( p_demux );
315 msg_Dbg( p_demux, "seek with percent: waiting %i frames", p_sys->i_wait_keyframe );
316 return demux_vaControlHelper( p_demux->s, __MIN( INT64_MAX, p_sys->i_data_begin ),
317 __MIN( INT64_MAX, p_sys->i_data_end ),
318 __MIN( INT64_MAX, p_sys->i_bitrate ),
319 __MIN( INT16_MAX, p_sys->p_fp->i_min_data_packet_size ),
323 static int SeekIndex( demux_t *p_demux, mtime_t i_date, float f_pos )
325 demux_sys_t *p_sys = p_demux->p_sys;
326 asf_object_index_t *p_index;
328 msg_Dbg( p_demux, "seek with index: %i seconds, position %f",
329 i_date >= 0 ? (int)(i_date/1000000) : -1, f_pos );
332 i_date = p_sys->i_length * f_pos;
334 p_index = ASF_FindObject( p_sys->p_root, &asf_object_simple_index_guid, 0 );
336 uint64_t i_entry = i_date * 10 / p_index->i_index_entry_time_interval;
337 if( i_entry >= p_index->i_index_entry_count )
339 msg_Warn( p_demux, "Incomplete index" );
343 WaitKeyframe( p_demux );
345 uint64_t i_offset = (uint64_t)p_index->index_entry[i_entry].i_packet_number *
346 p_sys->p_fp->i_min_data_packet_size;
348 if ( stream_Seek( p_demux->s, i_offset + p_sys->i_data_begin ) == VLC_SUCCESS )
350 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, VLC_TS_0 + i_date );
353 else return VLC_EGENERIC;
356 static void SeekPrepare( demux_t *p_demux )
358 demux_sys_t *p_sys = p_demux->p_sys;
360 p_sys->i_time = VLC_TS_INVALID;
361 for( int i = 0; i < MAX_ASF_TRACKS ; i++ )
363 asf_track_t *tk = p_sys->track[i];
367 tk->i_time = VLC_TS_INVALID;
369 block_ChainRelease( tk->p_frame );
373 es_out_Control( p_demux->out, ES_OUT_RESET_PCR, VLC_TS_INVALID );
376 /*****************************************************************************
378 *****************************************************************************/
379 static int Control( demux_t *p_demux, int i_query, va_list args )
381 demux_sys_t *p_sys = p_demux->p_sys;
389 case DEMUX_GET_LENGTH:
390 pi64 = (int64_t*)va_arg( args, int64_t * );
391 *pi64 = p_sys->i_length;
395 pi64 = (int64_t*)va_arg( args, int64_t * );
396 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
397 *pi64 = p_sys->i_time;
402 ! ( p_sys->p_fp->i_flags & ASF_FILE_PROPERTIES_SEEKABLE ) )
405 SeekPrepare( p_demux );
407 if( p_sys->b_index && p_sys->i_length > 0 )
410 va_copy( acpy, args );
411 i64 = (int64_t)va_arg( acpy, int64_t );
414 if( !SeekIndex( p_demux, i64, -1 ) )
417 return SeekPercent( p_demux, i_query, args );
421 i = (int)va_arg( args, int );
425 i++; /* video/audio-es variable starts 0 */
426 msg_Dbg( p_demux, "Requesting access to enable stream %d", i );
427 i_ret = stream_Control( p_demux->s, STREAM_SET_PRIVATE_ID_STATE, i, true );
430 { /* i contains -1 * es_category */
431 msg_Dbg( p_demux, "Requesting access to disable stream %d", i );
432 i_ret = stream_Control( p_demux->s, STREAM_SET_PRIVATE_ID_STATE, i, false );
435 if ( i_ret == VLC_SUCCESS )
437 SeekPrepare( p_demux );
438 p_sys->i_seek_track = 0;
439 WaitKeyframe( p_demux );
441 assert( i_ret == VLC_SUCCESS );
445 case DEMUX_GET_POSITION:
446 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
447 if( p_sys->i_length > 0 )
449 pf = (double*)va_arg( args, double * );
450 *pf = p_sys->i_time / (double)p_sys->i_length;
453 return demux_vaControlHelper( p_demux->s,
454 __MIN( INT64_MAX, p_sys->i_data_begin ),
455 __MIN( INT64_MAX, p_sys->i_data_end ),
456 __MIN( INT64_MAX, p_sys->i_bitrate ),
457 __MIN( INT16_MAX, p_sys->p_fp->i_min_data_packet_size ),
460 case DEMUX_SET_POSITION:
462 ! ( p_sys->p_fp->i_flags & ASF_FILE_PROPERTIES_SEEKABLE ) )
465 SeekPrepare( p_demux );
467 if( p_sys->b_index && p_sys->i_length > 0 )
470 va_copy( acpy, args );
471 f = (double)va_arg( acpy, double );
474 if( !SeekIndex( p_demux, -1, f ) )
477 return SeekPercent( p_demux, i_query, args );
480 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
481 vlc_meta_Merge( p_meta, p_sys->meta );
486 ! ( p_sys->p_fp->i_flags & ASF_FILE_PROPERTIES_SEEKABLE ) )
488 bool *pb_bool = (bool*)va_arg( args, bool * );
495 return demux_vaControlHelper( p_demux->s,
496 __MIN( INT64_MAX, p_sys->i_data_begin ),
497 __MIN( INT64_MAX, p_sys->i_data_end),
498 __MIN( INT64_MAX, p_sys->i_bitrate ),
499 ( p_sys->p_fp ) ? __MIN( INT_MAX, p_sys->p_fp->i_min_data_packet_size ) : 1,
504 /*****************************************************************************
506 *****************************************************************************/
507 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
511 /* As some tracks might have been deselected by access, the PCR might
513 for( i = 0; i < MAX_ASF_TRACKS ; i++ )
515 const asf_track_t *tk = p_sys->track[i];
517 if( tk && tk->p_es && tk->i_time > 0 && tk->b_selected )
519 if( i_time < 0 ) i_time = tk->i_time;
520 else i_time = __MIN( i_time, tk->i_time );
527 static inline int GetValue2b(uint32_t *var, const uint8_t *p, unsigned int *skip, int left, int bits)
534 *var = p[*skip]; *skip += 1;
539 *var = GetWLE(&p[*skip]); *skip += 2;
544 *var = GetDWLE(&p[*skip]); *skip += 4;
556 uint32_t padding_length;
561 /* buffer handling for this ASF packet */
563 const uint8_t *p_peek;
567 static void SendPacket(demux_t *p_demux, asf_track_t *tk)
569 demux_sys_t *p_sys = p_demux->p_sys;
571 block_t *p_gather = block_ChainGather( tk->p_frame );
573 if( p_sys->i_time < VLC_TS_0 && tk->i_time > VLC_TS_INVALID )
575 p_sys->i_time = tk->i_time;
576 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
578 msg_Dbg( p_demux, " setting PCR to %"PRId64, p_sys->i_time );
583 msg_Dbg( p_demux, " sending packet dts %"PRId64" pts %"PRId64" pcr %"PRId64, p_gather->i_dts, p_gather->i_pts, p_sys->i_time );
586 es_out_Send( p_demux->out, tk->p_es, p_gather );
590 static int DemuxSubPayload(demux_t *p_demux, asf_track_t *tk,
591 uint32_t i_sub_payload_data_length, mtime_t i_pts, mtime_t i_dts,
592 uint32_t i_media_object_offset, bool b_keyframe )
594 /* FIXME I don't use i_media_object_number, sould I ? */
595 if( tk->p_frame && i_media_object_offset == 0 )
596 SendPacket(p_demux, tk);
598 block_t *p_frag = stream_Block( p_demux->s, i_sub_payload_data_length );
599 if( p_frag == NULL ) {
600 msg_Warn( p_demux, "cannot read data" );
604 p_frag->i_pts = VLC_TS_0 + i_pts;
605 p_frag->i_dts = VLC_TS_0 + i_dts;
607 p_frag->i_flags |= BLOCK_FLAG_TYPE_I;
609 block_ChainAppend( &tk->p_frame, p_frag );
614 static uint32_t SkipBytes( stream_t *s, uint32_t i_bytes )
617 int i_to_read = __MIN(i_bytes, INT_MAX);
618 uint32_t i_bytes_read = 0;
622 i_read = stream_Read( s, NULL, i_to_read );
624 i_bytes_read += i_read;
625 if ( i_read < i_to_read || i_bytes == 0 )
630 i_to_read = __MIN(i_bytes, INT_MAX);
636 static void ParsePayloadExtensions(demux_t *p_demux, asf_track_t *tk,
637 const struct asf_packet_t *pkt,
638 uint32_t i_length, bool *b_keyframe )
640 if ( !tk || !tk->p_esp || !tk->p_esp->p_ext ) return;
641 const uint8_t *p_data = pkt->p_peek + pkt->i_skip + 8;
643 uint16_t i_payload_extensions_size;
644 asf_payload_extension_system_t *p_ext = NULL;
646 /* Extensions always come in the declared order */
647 for ( int i=0; i< tk->p_esp->i_payload_extension_system_count; i++ )
649 p_ext = &tk->p_esp->p_ext[i];
650 if ( p_ext->i_data_size == 0xFFFF ) /* Variable length extension data */
652 if ( i_length < 2 ) return;
653 i_payload_extensions_size = GetWLE( p_data );
656 i_payload_extensions_size = 0;
660 i_payload_extensions_size = p_ext->i_data_size;
663 if ( i_length < i_payload_extensions_size ) return;
665 if ( guidcmp( &p_ext->i_extension_id, &mfasf_sampleextension_outputcleanpoint_guid ) )
667 if ( i_payload_extensions_size != sizeof(uint8_t) ) goto sizeerror;
668 *b_keyframe |= *p_data;
670 else if ( guidcmp( &p_ext->i_extension_id, &asf_dvr_sampleextension_videoframe_guid ) )
672 if ( i_payload_extensions_size != sizeof(uint32_t) ) goto sizeerror;
674 uint32_t i_val = GetDWLE( p_data );
675 /* Valid keyframe must be a split frame start fragment */
676 *b_keyframe = i_val & ASF_EXTENSION_VIDEOFRAME_NEWFRAME;
679 /* And flagged as IFRAME */
680 *b_keyframe |= ( ( i_val & ASF_EXTENSION_VIDEOFRAME_TYPE_MASK )
681 == ASF_EXTENSION_VIDEOFRAME_IFRAME );
684 else if ( guidcmp( &p_ext->i_extension_id, &mfasf_sampleextension_pixelaspectratio_guid ) )
686 if ( i_payload_extensions_size != sizeof(uint16_t) ) goto sizeerror;
688 uint8_t i_ratio_x = *p_data;
689 uint8_t i_ratio_y = *(p_data + 1);
690 if ( tk->p_fmt->video.i_sar_num != i_ratio_x || tk->p_fmt->video.i_sar_den != i_ratio_y )
692 /* Only apply if origin pixel size >= 1x1, due to broken yacast */
693 if ( tk->p_fmt->video.i_height * i_ratio_x > tk->p_fmt->video.i_width * i_ratio_y )
695 vout_thread_t *p_vout = input_GetVout( p_demux->p_input );
698 msg_Info( p_demux, "Changing aspect ratio to %i/%i", i_ratio_x, i_ratio_y );
699 vout_ChangeAspectRatio( p_vout, i_ratio_x, i_ratio_y );
700 vlc_object_release( p_vout );
703 tk->p_fmt->video.i_sar_num = i_ratio_x;
704 tk->p_fmt->video.i_sar_den = i_ratio_y;
707 i_length -= i_payload_extensions_size;
708 p_data += i_payload_extensions_size;
714 msg_Warn( p_demux, "Unknown extension " GUID_FMT " data size of %u",
715 GUID_PRINT( p_ext->i_extension_id ), i_payload_extensions_size );
718 static int DemuxPayload(demux_t *p_demux, struct asf_packet_t *pkt, int i_payload)
721 VLC_UNUSED( i_payload );
723 demux_sys_t *p_sys = p_demux->p_sys;
725 if( ! pkt->left || pkt->i_skip >= pkt->left )
728 bool b_packet_keyframe = pkt->p_peek[pkt->i_skip] >> 7;
729 uint8_t i_stream_number = pkt->p_peek[pkt->i_skip++] & 0x7f;
731 uint32_t i_media_object_number = 0;
732 if (GetValue2b(&i_media_object_number, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->property >> 4) < 0)
734 uint32_t i_media_object_offset = 0;
735 if (GetValue2b(&i_media_object_offset, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->property >> 2) < 0)
737 uint32_t i_replicated_data_length = 0;
738 if (GetValue2b(&i_replicated_data_length, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->property) < 0)
742 uint8_t i_pts_delta = 0;
743 uint32_t i_payload_data_length = 0;
744 uint32_t i_temp_payload_length = 0;
745 bool b_preroll_done = false;
746 p_sys->p_fp->i_preroll = __MIN( p_sys->p_fp->i_preroll, INT64_MAX );
749 if( i_replicated_data_length > 7 ) // should be at least 8 bytes
751 /* Followed by 2 optional DWORDS, offset in media and presentation time */
752 i_base_pts = (mtime_t)GetDWLE( pkt->p_peek + pkt->i_skip + 4 );
754 /* Parsing extensions, See 7.3.1 */
755 ParsePayloadExtensions( p_demux, p_sys->track[i_stream_number], pkt,
756 i_replicated_data_length, &b_packet_keyframe );
758 b_preroll_done = ( i_base_pts > (int64_t)p_sys->p_fp->i_preroll );
759 i_base_pts -= p_sys->p_fp->i_preroll;
760 pkt->i_skip += i_replicated_data_length;
762 if( ! pkt->left || pkt->i_skip >= pkt->left )
765 else if ( i_replicated_data_length == 0 )
767 /* optional DWORDS missing */
768 i_base_pts = (mtime_t)pkt->send_time;
769 b_preroll_done = ( i_base_pts > (int64_t)p_sys->p_fp->i_preroll );
771 /* Compressed payload */
772 else if( i_replicated_data_length == 1 )
774 /* i_media_object_offset is presentation time */
775 /* Next byte is Presentation Time Delta */
776 i_pts_delta = pkt->p_peek[pkt->i_skip];
777 i_base_pts = (mtime_t)i_media_object_offset;
778 b_preroll_done = ( i_base_pts > (int64_t)p_sys->p_fp->i_preroll );
779 i_base_pts -= p_sys->p_fp->i_preroll;
781 i_media_object_offset = 0;
785 /* >1 && <8 Invalid replicated length ! */
786 msg_Warn( p_demux, "Invalid replicated data length detected." );
787 i_payload_data_length = pkt->length - pkt->padding_length - pkt->i_skip;
791 if (i_base_pts < 0) i_base_pts = 0; // FIXME?
794 if( pkt->multiple ) {
795 if (GetValue2b(&i_temp_payload_length, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->length_type) < 0)
798 i_temp_payload_length = pkt->length - pkt->padding_length - pkt->i_skip;
800 i_payload_data_length = i_temp_payload_length;
804 "payload(%d) stream_number:%"PRIu8" media_object_number:%d media_object_offset:%"PRIu32" replicated_data_length:%"PRIu32" payload_data_length %"PRIu32,
805 i_payload + 1, i_stream_number, i_media_object_number,
806 i_media_object_offset, i_replicated_data_length, i_payload_data_length );
808 " pts=%"PRId64" st=%"PRIu32, i_base_pts, pkt->send_time );
811 if( ! i_payload_data_length || i_payload_data_length > pkt->left )
813 msg_Dbg( p_demux, " payload length problem %d %"PRIu32" %"PRIu32, pkt->multiple, i_payload_data_length, pkt->left );
817 asf_track_t *tk = p_sys->track[i_stream_number];
820 msg_Warn( p_demux, "undeclared stream[Id 0x%x]", i_stream_number );
824 if( p_sys->i_wait_keyframe )
826 if ( i_stream_number == p_sys->i_seek_track )
828 if ( !b_packet_keyframe )
830 p_sys->i_wait_keyframe--;
834 p_sys->i_wait_keyframe = 0;
843 if ( b_preroll_done )
845 tk->i_time = INT64_C(1000) * pkt->send_time;
846 tk->i_time -= p_sys->p_fp->i_preroll * 1000;
847 tk->i_time -= tk->p_sp->i_time_offset * 10;
850 uint32_t i_subpayload_count = 0;
851 while (i_payload_data_length)
853 uint32_t i_sub_payload_data_length = i_payload_data_length;
854 if( i_replicated_data_length == 1 )
856 i_sub_payload_data_length = pkt->p_peek[pkt->i_skip++];
857 i_payload_data_length--;
860 SkipBytes( p_demux->s, pkt->i_skip );
862 mtime_t i_payload_pts = i_base_pts + (mtime_t)i_pts_delta * i_subpayload_count * 1000;
863 i_payload_pts -= tk->p_sp->i_time_offset * 10;
864 mtime_t i_payload_dts = INT64_C(1000) * pkt->send_time;
865 i_payload_dts -= p_sys->p_fp->i_preroll * 1000;
866 i_payload_dts -= tk->p_sp->i_time_offset * 10;
868 if ( i_sub_payload_data_length &&
869 DemuxSubPayload(p_demux, tk, i_sub_payload_data_length,
870 i_payload_pts, i_payload_dts, i_media_object_offset,
871 b_packet_keyframe ) < 0)
874 if ( pkt->left > pkt->i_skip + i_sub_payload_data_length )
875 pkt->left -= pkt->i_skip + i_sub_payload_data_length;
881 int i_return = stream_Peek( p_demux->s, &pkt->p_peek, __MIN(pkt->left, INT_MAX) );
882 if ( i_return <= 0 || (unsigned int) i_return < __MIN(pkt->left, INT_MAX) )
884 msg_Warn( p_demux, "cannot peek, EOF ?" );
889 if ( i_sub_payload_data_length <= i_payload_data_length )
890 i_payload_data_length -= i_sub_payload_data_length;
892 i_payload_data_length = 0;
894 i_subpayload_count++;
900 pkt->i_skip += i_payload_data_length;
904 static int DemuxPacket( demux_t *p_demux )
906 demux_sys_t *p_sys = p_demux->p_sys;
908 uint32_t i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
910 const uint8_t *p_peek;
911 int i_return = stream_Peek( p_demux->s, &p_peek,i_data_packet_min );
912 if( i_return <= 0 || ((unsigned int) i_return) < i_data_packet_min )
914 msg_Warn( p_demux, "cannot peek while getting new packet, EOF ?" );
917 unsigned int i_skip = 0;
919 /* *** parse error correction if present *** */
922 unsigned int i_error_correction_data_length = p_peek[0] & 0x0f;
923 unsigned int i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01;
924 unsigned int i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03;
925 i_skip += 1; // skip error correction flags
927 if( i_error_correction_length_type != 0x00 ||
928 i_opaque_data_present != 0 ||
929 i_error_correction_data_length != 0x02 )
931 goto loop_error_recovery;
934 i_skip += i_error_correction_data_length;
937 msg_Warn( p_demux, "no error correction" );
940 if( i_skip + 2 >= i_data_packet_min )
941 goto loop_error_recovery;
943 struct asf_packet_t pkt;
944 int i_packet_flags = p_peek[i_skip]; i_skip++;
945 pkt.property = p_peek[i_skip]; i_skip++;
946 pkt.multiple = !!(i_packet_flags&0x01);
948 pkt.length = i_data_packet_min;
949 pkt.padding_length = 0;
951 if (GetValue2b(&pkt.length, p_peek, &i_skip, i_data_packet_min - i_skip, i_packet_flags >> 5) < 0)
952 goto loop_error_recovery;
953 uint32_t i_packet_sequence;
954 if (GetValue2b(&i_packet_sequence, p_peek, &i_skip, i_data_packet_min - i_skip, i_packet_flags >> 1) < 0)
955 goto loop_error_recovery;
956 if (GetValue2b(&pkt.padding_length, p_peek, &i_skip, i_data_packet_min - i_skip, i_packet_flags >> 3) < 0)
957 goto loop_error_recovery;
959 if( pkt.padding_length > pkt.length )
961 msg_Warn( p_demux, "Too large padding: %"PRIu32, pkt.padding_length );
962 goto loop_error_recovery;
965 if( pkt.length < i_data_packet_min )
967 /* if packet length too short, there is extra padding */
968 pkt.padding_length += i_data_packet_min - pkt.length;
969 pkt.length = i_data_packet_min;
972 pkt.send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
973 /* uint16_t i_packet_duration = GetWLE( p_peek + i_skip ); */ i_skip += 2;
975 i_return = stream_Peek( p_demux->s, &p_peek, pkt.length );
976 if( i_return <= 0 || pkt.length == 0 || (unsigned int)i_return < pkt.length )
978 msg_Warn( p_demux, "cannot peek, EOF ?" );
982 int i_payload_count = 1;
983 pkt.length_type = 0x02; //unused
986 i_payload_count = p_peek[i_skip] & 0x3f;
987 pkt.length_type = ( p_peek[i_skip] >> 6 )&0x03;
992 msg_Dbg(p_demux, "%d payloads", i_payload_count);
997 pkt.left = pkt.length;
999 for( int i_payload = 0; i_payload < i_payload_count ; i_payload++ )
1000 if (DemuxPayload(p_demux, &pkt, i_payload) < 0)
1002 msg_Warn( p_demux, "payload err %d / %d", i_payload + 1, i_payload_count );
1009 if( pkt.left > pkt.padding_length )
1010 msg_Warn( p_demux, "Didn't read %"PRIu32" bytes in the packet",
1011 pkt.left - pkt.padding_length );
1012 else if( pkt.left < pkt.padding_length )
1013 msg_Warn( p_demux, "Read %"PRIu32" too much bytes in the packet",
1014 pkt.padding_length - pkt.left );
1016 int i_return = stream_Read( p_demux->s, NULL, pkt.left );
1017 if( i_return < 0 || (unsigned int) i_return < pkt.left )
1019 msg_Err( p_demux, "cannot skip data, EOF ?" );
1026 loop_error_recovery:
1027 msg_Warn( p_demux, "unsupported packet header" );
1028 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
1030 msg_Err( p_demux, "unsupported packet header, fatal error" );
1033 i_return = stream_Read( p_demux->s, NULL, i_data_packet_min );
1034 if( i_return <= 0 || (unsigned int) i_return != i_data_packet_min )
1036 msg_Warn( p_demux, "cannot skip data, EOF ?" );
1043 /*****************************************************************************
1045 *****************************************************************************/
1046 typedef struct asf_es_priorities_t
1048 uint16_t *pi_stream_numbers;
1050 } asf_es_priorities_t;
1052 /* Fills up our exclusion list */
1053 static void ASF_fillup_es_priorities_ex( demux_sys_t *p_sys, void *p_hdr,
1054 asf_es_priorities_t *p_prios )
1056 /* Find stream exclusions */
1057 asf_object_advanced_mutual_exclusion_t *p_mutex =
1058 ASF_FindObject( p_hdr, &asf_object_advanced_mutual_exclusion, 0 );
1059 if (! p_mutex ) return;
1061 #if ( UINT_MAX > SIZE_MAX / 2 )
1062 if ( p_sys->i_track > (size_t)SIZE_MAX / sizeof(uint16_t) )
1065 p_prios->pi_stream_numbers = malloc( (size_t)p_sys->i_track * sizeof(uint16_t) );
1066 if ( !p_prios->pi_stream_numbers ) return;
1068 if ( p_mutex->i_stream_number_count )
1070 /* Just set highest prio on highest in the group */
1071 for ( uint16_t i = 1; i < p_mutex->i_stream_number_count; i++ )
1073 if ( p_prios->i_count > p_sys->i_track ) break;
1074 p_prios->pi_stream_numbers[ p_prios->i_count++ ] = p_mutex->pi_stream_number[ i ];
1079 /* Fills up our bitrate exclusion list */
1080 static void ASF_fillup_es_bitrate_priorities_ex( demux_sys_t *p_sys, void *p_hdr,
1081 asf_es_priorities_t *p_prios )
1083 /* Find bitrate exclusions */
1084 asf_object_bitrate_mutual_exclusion_t *p_bitrate_mutex =
1085 ASF_FindObject( p_hdr, &asf_object_bitrate_mutual_exclusion_guid, 0 );
1086 if (! p_bitrate_mutex ) return;
1088 #if ( UINT_MAX > SIZE_MAX / 2 )
1089 if ( p_sys->i_track > (size_t)SIZE_MAX / sizeof(uint16_t) )
1092 p_prios->pi_stream_numbers = malloc( (size_t)p_sys->i_track * sizeof( uint16_t ) );
1093 if ( !p_prios->pi_stream_numbers ) return;
1095 if ( p_bitrate_mutex->i_stream_number_count )
1097 /* Just remove < highest */
1098 for ( uint16_t i = 1; i < p_bitrate_mutex->i_stream_number_count; i++ )
1100 if ( p_prios->i_count > p_sys->i_track ) break;
1101 p_prios->pi_stream_numbers[ p_prios->i_count++ ] = p_bitrate_mutex->pi_stream_numbers[ i ];
1107 #define GET_CHECKED( target, getter, maxtarget, temp ) \
1109 temp i_temp = getter;\
1110 if ( i_temp > maxtarget ) {\
1111 msg_Warn( p_demux, "rejecting stream %u : " #target " overflow", i_stream );\
1112 es_format_Clean( &fmt );\
1119 static int DemuxInit( demux_t *p_demux )
1121 demux_sys_t *p_sys = p_demux->p_sys;
1125 p_sys->i_length = 0;
1126 p_sys->i_bitrate = 0;
1127 p_sys->p_root = NULL;
1131 p_sys->i_seek_track = 0;
1132 p_sys->i_wait_keyframe = 0;
1133 for( int i = 0; i < MAX_ASF_TRACKS; i++ )
1135 p_sys->track[i] = NULL;
1137 p_sys->i_data_begin = 0;
1138 p_sys->i_data_end = 0;
1141 /* Now load all object ( except raw data ) */
1142 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_canfastseek );
1143 if( !(p_sys->p_root = ASF_ReadObjectRoot(p_demux->s, p_sys->b_canfastseek)) )
1145 msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
1146 return VLC_EGENERIC;
1148 p_sys->p_fp = p_sys->p_root->p_fp;
1150 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
1152 msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
1156 if ( ASF_FindObject( p_sys->p_root->p_hdr,
1157 &asf_object_content_encryption_guid, 0 ) != NULL
1158 || ASF_FindObject( p_sys->p_root->p_hdr,
1159 &asf_object_extended_content_encryption_guid, 0 ) != NULL
1160 || ASF_FindObject( p_sys->p_root->p_hdr,
1161 &asf_object_advanced_content_encryption_guid, 0 ) != NULL )
1163 dialog_Fatal( p_demux, _("Could not demux ASF stream"), "%s",
1164 _("DRM protected streams are not supported.") );
1168 p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
1169 &asf_object_stream_properties_guid );
1170 if( p_sys->i_track == 0 )
1172 msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
1175 msg_Dbg( p_demux, "found %u streams", p_sys->i_track );
1177 /* check if index is available */
1178 asf_object_index_t *p_index = ASF_FindObject( p_sys->p_root,
1179 &asf_object_simple_index_guid, 0 );
1180 const bool b_index = p_index && p_index->i_index_entry_count;
1182 /* Find the extended header if any */
1183 asf_object_t *p_hdr_ext = ASF_FindObject( p_sys->p_root->p_hdr,
1184 &asf_object_header_extension_guid, 0 );
1186 asf_object_language_list_t *p_languages = NULL;
1187 asf_es_priorities_t fmt_priorities_ex = { NULL, 0 };
1188 asf_es_priorities_t fmt_priorities_bitrate_ex = { NULL, 0 };
1192 p_languages = ASF_FindObject( p_hdr_ext, &asf_object_language_list, 0 );
1194 ASF_fillup_es_priorities_ex( p_sys, p_hdr_ext, &fmt_priorities_ex );
1195 ASF_fillup_es_bitrate_priorities_ex( p_sys, p_hdr_ext, &fmt_priorities_bitrate_ex );
1198 for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1201 asf_object_stream_properties_t *p_sp;
1202 asf_object_extended_stream_properties_t *p_esp;
1203 bool b_access_selected;
1205 p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
1206 &asf_object_stream_properties_guid,
1210 tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
1211 memset( tk, 0, sizeof( asf_track_t ) );
1219 if ( strncmp( p_demux->psz_access, "mms", 3 ) )
1221 /* Check (not mms) if this track is selected (ie will receive data) */
1222 if( !stream_Control( p_demux->s, STREAM_GET_PRIVATE_ID_STATE,
1223 (int) p_sp->i_stream_number, &b_access_selected ) &&
1224 !b_access_selected )
1226 tk->i_cat = UNKNOWN_ES;
1227 msg_Dbg( p_demux, "ignoring not selected stream(ID:%u) (by access)",
1228 p_sp->i_stream_number );
1233 /* Find the associated extended_stream_properties if any */
1236 int i_ext_stream = ASF_CountObject( p_hdr_ext,
1237 &asf_object_extended_stream_properties_guid );
1238 for( int i = 0; i < i_ext_stream; i++ )
1240 asf_object_t *p_tmp =
1241 ASF_FindObject( p_hdr_ext,
1242 &asf_object_extended_stream_properties_guid, i );
1243 if( p_tmp->ext_stream.i_stream_number == p_sp->i_stream_number )
1245 p_esp = &p_tmp->ext_stream;
1254 if( guidcmp( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
1255 p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
1257 uint8_t *p_data = p_sp->p_type_specific_data;
1260 es_format_Init( &fmt, AUDIO_ES, 0 );
1261 i_format = GetWLE( &p_data[0] );
1262 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
1264 GET_CHECKED( fmt.audio.i_channels, GetWLE( &p_data[2] ),
1266 GET_CHECKED( fmt.audio.i_rate, GetDWLE( &p_data[4] ),
1267 UINT_MAX, uint32_t );
1268 GET_CHECKED( fmt.i_bitrate, GetDWLE( &p_data[8] ) * 8,
1269 UINT_MAX, uint32_t );
1270 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
1271 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
1273 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
1274 i_format != WAVE_FORMAT_MPEGLAYER3 &&
1275 i_format != WAVE_FORMAT_MPEG )
1277 GET_CHECKED( fmt.i_extra, __MIN( GetWLE( &p_data[16] ),
1278 p_sp->i_type_specific_data_length -
1279 sizeof( WAVEFORMATEX ) ),
1280 INT_MAX, uint32_t );
1281 fmt.p_extra = malloc( fmt.i_extra );
1282 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
1286 msg_Dbg( p_demux, "added new audio stream(codec:0x%x,ID:%d)",
1287 GetWLE( p_data ), p_sp->i_stream_number );
1289 else if( guidcmp( &p_sp->i_stream_type,
1290 &asf_object_stream_type_video ) &&
1291 p_sp->i_type_specific_data_length >= 11 +
1292 sizeof( VLC_BITMAPINFOHEADER ) )
1294 uint8_t *p_data = &p_sp->p_type_specific_data[11];
1296 es_format_Init( &fmt, VIDEO_ES,
1297 VLC_FOURCC( p_data[16], p_data[17],
1298 p_data[18], p_data[19] ) );
1300 GET_CHECKED( fmt.video.i_width, GetDWLE( p_data + 4 ),
1301 UINT_MAX, uint32_t );
1302 GET_CHECKED( fmt.video.i_height, GetDWLE( p_data + 8 ),
1303 UINT_MAX, uint32_t );
1305 if( p_esp && p_esp->i_average_time_per_frame > 0 )
1307 fmt.video.i_frame_rate = 10000000;
1308 GET_CHECKED( fmt.video.i_frame_rate_base,
1309 p_esp->i_average_time_per_frame,
1310 UINT_MAX, uint64_t );
1313 if( fmt.i_codec == VLC_FOURCC( 'D','V','R',' ') )
1315 /* DVR-MS special ASF */
1316 fmt.i_codec = VLC_FOURCC( 'm','p','g','2' ) ;
1317 fmt.b_packetized = false;
1320 if( p_sp->i_type_specific_data_length > 11 +
1321 sizeof( VLC_BITMAPINFOHEADER ) )
1323 GET_CHECKED( fmt.i_extra, __MIN( GetDWLE( p_data ),
1324 p_sp->i_type_specific_data_length - 11 -
1325 sizeof( VLC_BITMAPINFOHEADER ) ),
1326 UINT_MAX, uint32_t );
1327 fmt.p_extra = malloc( fmt.i_extra );
1328 memcpy( fmt.p_extra, &p_data[sizeof( VLC_BITMAPINFOHEADER )],
1332 /* Look for an aspect ratio */
1333 if( p_sys->p_root->p_metadata )
1335 asf_object_metadata_t *p_meta = p_sys->p_root->p_metadata;
1336 unsigned int i_aspect_x = 0, i_aspect_y = 0;
1338 for( i = 0; i < p_meta->i_record_entries_count; i++ )
1340 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioX" ) )
1342 if( (!i_aspect_x && !p_meta->record[i].i_stream) ||
1343 p_meta->record[i].i_stream ==
1344 p_sp->i_stream_number )
1345 GET_CHECKED( i_aspect_x, p_meta->record[i].i_val,
1346 UINT_MAX, uint64_t );
1348 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioY" ) )
1350 if( (!i_aspect_y && !p_meta->record[i].i_stream) ||
1351 p_meta->record[i].i_stream ==
1352 p_sp->i_stream_number )
1353 GET_CHECKED( i_aspect_y, p_meta->record[i].i_val,
1354 UINT_MAX, uint64_t );
1358 if( i_aspect_x && i_aspect_y )
1360 fmt.video.i_sar_num = i_aspect_x;
1361 fmt.video.i_sar_den = i_aspect_y;
1365 /* If there is a video track then use the index for seeking */
1366 p_sys->b_index = b_index;
1368 msg_Dbg( p_demux, "added new video stream(ID:%d)",
1369 p_sp->i_stream_number );
1371 else if( guidcmp( &p_sp->i_stream_type, &asf_object_extended_stream_header ) &&
1372 p_sp->i_type_specific_data_length >= 64 )
1374 /* Now follows a 64 byte header of which we don't know much */
1375 guid_t *p_ref = (guid_t *)p_sp->p_type_specific_data;
1376 uint8_t *p_data = p_sp->p_type_specific_data + 64;
1377 unsigned int i_data = p_sp->i_type_specific_data_length - 64;
1379 msg_Dbg( p_demux, "Ext stream header detected. datasize = %d", p_sp->i_type_specific_data_length );
1380 if( guidcmp( p_ref, &asf_object_extended_stream_type_audio ) &&
1381 i_data >= sizeof( WAVEFORMATEX ) - 2)
1384 es_format_Init( &fmt, AUDIO_ES, 0 );
1385 i_format = GetWLE( &p_data[0] );
1387 fmt.i_codec = VLC_CODEC_A52;
1389 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
1390 GET_CHECKED( fmt.audio.i_channels, GetWLE( &p_data[2] ),
1392 GET_CHECKED( fmt.audio.i_rate, GetDWLE( &p_data[4] ),
1393 UINT_MAX, uint32_t );
1394 GET_CHECKED( fmt.i_bitrate, GetDWLE( &p_data[8] ) * 8,
1395 UINT_MAX, uint32_t );
1396 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
1397 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
1398 fmt.b_packetized = true;
1400 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
1401 i_format != WAVE_FORMAT_MPEGLAYER3 &&
1402 i_format != WAVE_FORMAT_MPEG )
1404 GET_CHECKED( fmt.i_extra, __MIN( GetWLE( &p_data[16] ),
1405 p_sp->i_type_specific_data_length -
1406 sizeof( WAVEFORMATEX ) ),
1407 INT_MAX, uint32_t );
1408 fmt.p_extra = malloc( fmt.i_extra );
1409 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
1413 msg_Dbg( p_demux, "added new audio stream (codec:0x%x,ID:%d)",
1414 i_format, p_sp->i_stream_number );
1418 es_format_Init( &fmt, UNKNOWN_ES, 0 );
1423 es_format_Init( &fmt, UNKNOWN_ES, 0 );
1426 tk->i_cat = fmt.i_cat;
1427 if( fmt.i_cat != UNKNOWN_ES )
1429 if( p_esp && p_languages &&
1430 p_esp->i_language_index < p_languages->i_language )
1432 fmt.psz_language = strdup( p_languages->ppsz_language[p_esp->i_language_index] );
1434 if( fmt.psz_language && (p = strchr( fmt.psz_language, '-' )) )
1438 /* Set our priority so we won't get multiple videos */
1439 int i_priority = ES_PRIORITY_SELECTABLE_MIN;
1440 for( uint16_t i = 0; i < fmt_priorities_ex.i_count; i++ )
1442 if ( fmt_priorities_ex.pi_stream_numbers[i] == p_sp->i_stream_number )
1444 i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
1448 for( uint16_t i = 0; i < fmt_priorities_bitrate_ex.i_count; i++ )
1450 if ( fmt_priorities_bitrate_ex.pi_stream_numbers[i] == p_sp->i_stream_number )
1452 i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
1456 fmt.i_priority = i_priority;
1458 if ( i_stream <= INT_MAX )
1459 fmt.i_id = i_stream;
1461 msg_Warn( p_demux, "Can't set fmt.i_id to match stream id %u", i_stream );
1463 if ( fmt.i_cat == VIDEO_ES )
1465 /* Backup our video format */
1466 tk->p_fmt = malloc( sizeof( es_format_t ) );
1468 es_format_Copy( tk->p_fmt, &fmt );
1471 tk->p_es = es_out_Add( p_demux->out, &fmt );
1473 if( !stream_Control( p_demux->s, STREAM_GET_PRIVATE_ID_STATE,
1474 (int) p_sp->i_stream_number, &b_access_selected ) &&
1477 p_sys->i_access_selected_track[fmt.i_cat] = p_sp->i_stream_number;
1483 msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
1484 p_sp->i_stream_number );
1487 es_format_Clean( &fmt );
1490 free( fmt_priorities_ex.pi_stream_numbers );
1491 free( fmt_priorities_bitrate_ex.pi_stream_numbers );
1493 p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
1494 if( p_sys->p_root->p_data->i_object_size != 0 )
1496 p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
1497 p_sys->p_root->p_data->i_object_size;
1498 p_sys->i_data_end = __MIN( (uint64_t)stream_Size( p_demux->s ), p_sys->i_data_end );
1501 { /* live/broacast */
1502 p_sys->i_data_end = 0;
1505 /* go to first packet */
1506 stream_Seek( p_demux->s, p_sys->i_data_begin );
1508 /* try to calculate movie time */
1509 if( p_sys->p_fp->i_data_packets_count > 0 )
1512 uint64_t i_size = stream_Size( p_demux->s );
1514 if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
1516 i_size = p_sys->i_data_end;
1519 /* real number of packets */
1520 i_count = ( i_size - p_sys->i_data_begin ) /
1521 p_sys->p_fp->i_min_data_packet_size;
1523 /* calculate the time duration in micro-s */
1524 p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
1526 (mtime_t)p_sys->p_fp->i_data_packets_count - p_sys->p_fp->i_preroll * 1000;
1527 if( p_sys->i_length < 0 )
1528 p_sys->i_length = 0;
1530 if( p_sys->i_length > 0 )
1532 p_sys->i_bitrate = 8 * i_size * 1000000 / p_sys->i_length;
1536 /* Create meta information */
1537 p_sys->meta = vlc_meta_New();
1539 asf_object_content_description_t *p_cd;
1540 if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
1541 &asf_object_content_description_guid, 0 ) ) )
1543 if( p_cd->psz_title && *p_cd->psz_title )
1545 vlc_meta_SetTitle( p_sys->meta, p_cd->psz_title );
1547 if( p_cd->psz_artist && *p_cd->psz_artist )
1549 vlc_meta_SetArtist( p_sys->meta, p_cd->psz_artist );
1551 if( p_cd->psz_copyright && *p_cd->psz_copyright )
1553 vlc_meta_SetCopyright( p_sys->meta, p_cd->psz_copyright );
1555 if( p_cd->psz_description && *p_cd->psz_description )
1557 vlc_meta_SetDescription( p_sys->meta, p_cd->psz_description );
1559 if( p_cd->psz_rating && *p_cd->psz_rating )
1561 vlc_meta_SetRating( p_sys->meta, p_cd->psz_rating );
1564 /// \tood Fix Child meta for ASF tracks
1566 for( i_stream = 0, i = 0; i < MAX_ASF_TRACKS; i++ )
1568 asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
1569 &asf_object_codec_list_guid, 0 );
1571 if( p_sys->track[i] )
1573 vlc_meta_t *tk = vlc_meta_New();
1574 TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
1576 if( p_cl && i_stream < p_cl->i_codec_entries_count )
1578 if( p_cl->codec[i_stream].psz_name &&
1579 *p_cl->codec[i_stream].psz_name )
1581 vlc_meta_Add( tk, VLC_META_CODEC_NAME,
1582 p_cl->codec[i_stream].psz_name );
1584 if( p_cl->codec[i_stream].psz_description &&
1585 *p_cl->codec[i_stream].psz_description )
1587 vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
1588 p_cl->codec[i_stream].psz_description );
1598 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1599 return VLC_EGENERIC;
1602 /*****************************************************************************
1603 * FlushRemainingPackets: flushes tail packets
1604 *****************************************************************************/
1606 static void FlushRemainingPackets( demux_t *p_demux )
1608 demux_sys_t *p_sys = p_demux->p_sys;
1609 for ( unsigned int i = 0; i < MAX_ASF_TRACKS; i++ )
1611 asf_track_t *tk = p_sys->track[i];
1612 if( tk && tk->p_frame )
1613 SendPacket( p_demux, tk );
1617 /*****************************************************************************
1619 *****************************************************************************/
1620 static void DemuxEnd( demux_t *p_demux )
1622 demux_sys_t *p_sys = p_demux->p_sys;
1626 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1627 p_sys->p_root = NULL;
1631 vlc_meta_Delete( p_sys->meta );
1635 for( int i = 0; i < MAX_ASF_TRACKS; i++ )
1637 asf_track_t *tk = p_sys->track[i];
1642 block_ChainRelease( tk->p_frame );
1646 es_out_Del( p_demux->out, tk->p_es );
1650 es_format_Clean( tk->p_fmt );
1655 p_sys->track[i] = 0;