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;
88 mtime_t i_time; /* track time*/
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->b_selected )
519 /* Skip discrete tracks */
520 if ( tk->i_cat != VIDEO_ES && tk->i_cat != AUDIO_ES )
523 /* We need to have all ES seen once, as they might have lower DTS */
524 if ( tk->i_time + (int64_t)p_sys->p_fp->i_preroll * 1000 < 0 )
529 else if ( i_time == -1 || i_time > tk->i_time )
539 static inline int GetValue2b(uint32_t *var, const uint8_t *p, unsigned int *skip, int left, int bits)
546 *var = p[*skip]; *skip += 1;
551 *var = GetWLE(&p[*skip]); *skip += 2;
556 *var = GetDWLE(&p[*skip]); *skip += 4;
568 uint32_t padding_length;
573 /* buffer handling for this ASF packet */
575 const uint8_t *p_peek;
579 static void SendPacket(demux_t *p_demux, asf_track_t *tk)
581 demux_sys_t *p_sys = p_demux->p_sys;
583 block_t *p_gather = block_ChainGather( tk->p_frame );
585 if( p_sys->i_time < VLC_TS_0 && tk->i_time > VLC_TS_INVALID )
587 p_sys->i_time = tk->i_time;
588 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
590 msg_Dbg( p_demux, " setting PCR to %"PRId64, p_sys->i_time );
595 msg_Dbg( p_demux, " sending packet dts %"PRId64" pts %"PRId64" pcr %"PRId64, p_gather->i_dts, p_gather->i_pts, p_sys->i_time );
598 es_out_Send( p_demux->out, tk->p_es, p_gather );
602 static int DemuxSubPayload(demux_t *p_demux, asf_track_t *tk,
603 uint32_t i_sub_payload_data_length, mtime_t i_pts, mtime_t i_dts,
604 uint32_t i_media_object_offset, bool b_keyframe )
606 /* FIXME I don't use i_media_object_number, sould I ? */
607 if( tk->p_frame && i_media_object_offset == 0 )
608 SendPacket(p_demux, tk);
610 block_t *p_frag = stream_Block( p_demux->s, i_sub_payload_data_length );
611 if( p_frag == NULL ) {
612 msg_Warn( p_demux, "cannot read data" );
616 p_frag->i_pts = VLC_TS_0 + i_pts;
617 p_frag->i_dts = VLC_TS_0 + i_dts;
619 p_frag->i_flags |= BLOCK_FLAG_TYPE_I;
621 block_ChainAppend( &tk->p_frame, p_frag );
626 static uint32_t SkipBytes( stream_t *s, uint32_t i_bytes )
629 int i_to_read = __MIN(i_bytes, INT_MAX);
630 uint32_t i_bytes_read = 0;
634 i_read = stream_Read( s, NULL, i_to_read );
636 i_bytes_read += i_read;
637 if ( i_read < i_to_read || i_bytes == 0 )
642 i_to_read = __MIN(i_bytes, INT_MAX);
648 static void ParsePayloadExtensions(demux_t *p_demux, asf_track_t *tk,
649 const struct asf_packet_t *pkt,
650 uint32_t i_length, bool *b_keyframe )
652 if ( !tk || !tk->p_esp || !tk->p_esp->p_ext ) return;
653 const uint8_t *p_data = pkt->p_peek + pkt->i_skip + 8;
655 uint16_t i_payload_extensions_size;
656 asf_payload_extension_system_t *p_ext = NULL;
658 /* Extensions always come in the declared order */
659 for ( int i=0; i< tk->p_esp->i_payload_extension_system_count; i++ )
661 p_ext = &tk->p_esp->p_ext[i];
662 if ( p_ext->i_data_size == 0xFFFF ) /* Variable length extension data */
664 if ( i_length < 2 ) return;
665 i_payload_extensions_size = GetWLE( p_data );
668 i_payload_extensions_size = 0;
672 i_payload_extensions_size = p_ext->i_data_size;
675 if ( i_length < i_payload_extensions_size ) return;
677 if ( guidcmp( &p_ext->i_extension_id, &mfasf_sampleextension_outputcleanpoint_guid ) )
679 if ( i_payload_extensions_size != sizeof(uint8_t) ) goto sizeerror;
680 *b_keyframe |= *p_data;
682 else if ( guidcmp( &p_ext->i_extension_id, &asf_dvr_sampleextension_videoframe_guid ) )
684 if ( i_payload_extensions_size != sizeof(uint32_t) ) goto sizeerror;
686 uint32_t i_val = GetDWLE( p_data );
687 /* Valid keyframe must be a split frame start fragment */
688 *b_keyframe = i_val & ASF_EXTENSION_VIDEOFRAME_NEWFRAME;
691 /* And flagged as IFRAME */
692 *b_keyframe |= ( ( i_val & ASF_EXTENSION_VIDEOFRAME_TYPE_MASK )
693 == ASF_EXTENSION_VIDEOFRAME_IFRAME );
696 else if ( guidcmp( &p_ext->i_extension_id, &mfasf_sampleextension_pixelaspectratio_guid ) )
698 if ( i_payload_extensions_size != sizeof(uint16_t) ) goto sizeerror;
700 uint8_t i_ratio_x = *p_data;
701 uint8_t i_ratio_y = *(p_data + 1);
702 if ( tk->p_fmt->video.i_sar_num != i_ratio_x || tk->p_fmt->video.i_sar_den != i_ratio_y )
704 /* Only apply if origin pixel size >= 1x1, due to broken yacast */
705 if ( tk->p_fmt->video.i_height * i_ratio_x > tk->p_fmt->video.i_width * i_ratio_y )
707 vout_thread_t *p_vout = input_GetVout( p_demux->p_input );
710 msg_Info( p_demux, "Changing aspect ratio to %i/%i", i_ratio_x, i_ratio_y );
711 vout_ChangeAspectRatio( p_vout, i_ratio_x, i_ratio_y );
712 vlc_object_release( p_vout );
715 tk->p_fmt->video.i_sar_num = i_ratio_x;
716 tk->p_fmt->video.i_sar_den = i_ratio_y;
719 i_length -= i_payload_extensions_size;
720 p_data += i_payload_extensions_size;
726 msg_Warn( p_demux, "Unknown extension " GUID_FMT " data size of %u",
727 GUID_PRINT( p_ext->i_extension_id ), i_payload_extensions_size );
730 static int DemuxPayload(demux_t *p_demux, struct asf_packet_t *pkt, int i_payload)
733 VLC_UNUSED( i_payload );
735 demux_sys_t *p_sys = p_demux->p_sys;
737 if( ! pkt->left || pkt->i_skip >= pkt->left )
740 bool b_packet_keyframe = pkt->p_peek[pkt->i_skip] >> 7;
741 uint8_t i_stream_number = pkt->p_peek[pkt->i_skip++] & 0x7f;
743 uint32_t i_media_object_number = 0;
744 if (GetValue2b(&i_media_object_number, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->property >> 4) < 0)
746 uint32_t i_media_object_offset = 0;
747 if (GetValue2b(&i_media_object_offset, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->property >> 2) < 0)
749 uint32_t i_replicated_data_length = 0;
750 if (GetValue2b(&i_replicated_data_length, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->property) < 0)
754 uint8_t i_pts_delta = 0;
755 uint32_t i_payload_data_length = 0;
756 uint32_t i_temp_payload_length = 0;
757 bool b_preroll_done = false;
758 p_sys->p_fp->i_preroll = __MIN( p_sys->p_fp->i_preroll, INT64_MAX );
761 if( i_replicated_data_length > 7 ) // should be at least 8 bytes
763 /* Followed by 2 optional DWORDS, offset in media and presentation time */
764 i_base_pts = (mtime_t)GetDWLE( pkt->p_peek + pkt->i_skip + 4 );
766 /* Parsing extensions, See 7.3.1 */
767 ParsePayloadExtensions( p_demux, p_sys->track[i_stream_number], pkt,
768 i_replicated_data_length, &b_packet_keyframe );
770 b_preroll_done = ( i_base_pts > (int64_t)p_sys->p_fp->i_preroll );
771 i_base_pts -= p_sys->p_fp->i_preroll;
772 pkt->i_skip += i_replicated_data_length;
774 if( ! pkt->left || pkt->i_skip >= pkt->left )
777 else if ( i_replicated_data_length == 0 )
779 /* optional DWORDS missing */
780 i_base_pts = (mtime_t)pkt->send_time;
781 b_preroll_done = ( i_base_pts > (int64_t)p_sys->p_fp->i_preroll );
783 /* Compressed payload */
784 else if( i_replicated_data_length == 1 )
786 /* i_media_object_offset is presentation time */
787 /* Next byte is Presentation Time Delta */
788 i_pts_delta = pkt->p_peek[pkt->i_skip];
789 i_base_pts = (mtime_t)i_media_object_offset;
790 b_preroll_done = ( i_base_pts > (int64_t)p_sys->p_fp->i_preroll );
791 i_base_pts -= p_sys->p_fp->i_preroll;
793 i_media_object_offset = 0;
797 /* >1 && <8 Invalid replicated length ! */
798 msg_Warn( p_demux, "Invalid replicated data length detected." );
799 i_payload_data_length = pkt->length - pkt->padding_length - pkt->i_skip;
803 if (i_base_pts < 0) i_base_pts = 0; // FIXME?
806 if( pkt->multiple ) {
807 if (GetValue2b(&i_temp_payload_length, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->length_type) < 0)
810 i_temp_payload_length = pkt->length - pkt->padding_length - pkt->i_skip;
812 i_payload_data_length = i_temp_payload_length;
816 "payload(%d) stream_number:%"PRIu8" media_object_number:%d media_object_offset:%"PRIu32" replicated_data_length:%"PRIu32" payload_data_length %"PRIu32,
817 i_payload + 1, i_stream_number, i_media_object_number,
818 i_media_object_offset, i_replicated_data_length, i_payload_data_length );
820 " pts=%"PRId64" st=%"PRIu32, i_base_pts, pkt->send_time );
823 if( ! i_payload_data_length || i_payload_data_length > pkt->left )
825 msg_Dbg( p_demux, " payload length problem %d %"PRIu32" %"PRIu32, pkt->multiple, i_payload_data_length, pkt->left );
829 asf_track_t *tk = p_sys->track[i_stream_number];
832 msg_Warn( p_demux, "undeclared stream[Id 0x%x]", i_stream_number );
836 if( p_sys->i_wait_keyframe )
838 if ( i_stream_number == p_sys->i_seek_track )
840 if ( !b_packet_keyframe )
842 p_sys->i_wait_keyframe--;
846 p_sys->i_wait_keyframe = 0;
855 if ( b_preroll_done )
857 tk->i_time = INT64_C(1000) * pkt->send_time;
858 tk->i_time -= p_sys->p_fp->i_preroll * 1000;
859 tk->i_time -= tk->p_sp->i_time_offset * 10;
862 uint32_t i_subpayload_count = 0;
863 while (i_payload_data_length)
865 uint32_t i_sub_payload_data_length = i_payload_data_length;
866 if( i_replicated_data_length == 1 )
868 i_sub_payload_data_length = pkt->p_peek[pkt->i_skip++];
869 i_payload_data_length--;
872 SkipBytes( p_demux->s, pkt->i_skip );
874 mtime_t i_payload_pts = i_base_pts + (mtime_t)i_pts_delta * i_subpayload_count * 1000;
875 i_payload_pts -= tk->p_sp->i_time_offset * 10;
876 mtime_t i_payload_dts = INT64_C(1000) * pkt->send_time;
877 i_payload_dts -= p_sys->p_fp->i_preroll * 1000;
878 i_payload_dts -= tk->p_sp->i_time_offset * 10;
880 if ( i_sub_payload_data_length &&
881 DemuxSubPayload(p_demux, tk, i_sub_payload_data_length,
882 i_payload_pts, i_payload_dts, i_media_object_offset,
883 b_packet_keyframe ) < 0)
886 if ( pkt->left > pkt->i_skip + i_sub_payload_data_length )
887 pkt->left -= pkt->i_skip + i_sub_payload_data_length;
893 int i_return = stream_Peek( p_demux->s, &pkt->p_peek, __MIN(pkt->left, INT_MAX) );
894 if ( i_return <= 0 || (unsigned int) i_return < __MIN(pkt->left, INT_MAX) )
896 msg_Warn( p_demux, "cannot peek, EOF ?" );
901 if ( i_sub_payload_data_length <= i_payload_data_length )
902 i_payload_data_length -= i_sub_payload_data_length;
904 i_payload_data_length = 0;
906 i_subpayload_count++;
912 pkt->i_skip += i_payload_data_length;
916 static int DemuxPacket( demux_t *p_demux )
918 demux_sys_t *p_sys = p_demux->p_sys;
920 uint32_t i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
922 const uint8_t *p_peek;
923 int i_return = stream_Peek( p_demux->s, &p_peek,i_data_packet_min );
924 if( i_return <= 0 || ((unsigned int) i_return) < i_data_packet_min )
926 msg_Warn( p_demux, "cannot peek while getting new packet, EOF ?" );
929 unsigned int i_skip = 0;
931 /* *** parse error correction if present *** */
934 unsigned int i_error_correction_data_length = p_peek[0] & 0x0f;
935 unsigned int i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01;
936 unsigned int i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03;
937 i_skip += 1; // skip error correction flags
939 if( i_error_correction_length_type != 0x00 ||
940 i_opaque_data_present != 0 ||
941 i_error_correction_data_length != 0x02 )
943 goto loop_error_recovery;
946 i_skip += i_error_correction_data_length;
949 msg_Warn( p_demux, "no error correction" );
952 if( i_skip + 2 >= i_data_packet_min )
953 goto loop_error_recovery;
955 struct asf_packet_t pkt;
956 int i_packet_flags = p_peek[i_skip]; i_skip++;
957 pkt.property = p_peek[i_skip]; i_skip++;
958 pkt.multiple = !!(i_packet_flags&0x01);
960 pkt.length = i_data_packet_min;
961 pkt.padding_length = 0;
963 if (GetValue2b(&pkt.length, p_peek, &i_skip, i_data_packet_min - i_skip, i_packet_flags >> 5) < 0)
964 goto loop_error_recovery;
965 uint32_t i_packet_sequence;
966 if (GetValue2b(&i_packet_sequence, p_peek, &i_skip, i_data_packet_min - i_skip, i_packet_flags >> 1) < 0)
967 goto loop_error_recovery;
968 if (GetValue2b(&pkt.padding_length, p_peek, &i_skip, i_data_packet_min - i_skip, i_packet_flags >> 3) < 0)
969 goto loop_error_recovery;
971 if( pkt.padding_length > pkt.length )
973 msg_Warn( p_demux, "Too large padding: %"PRIu32, pkt.padding_length );
974 goto loop_error_recovery;
977 if( pkt.length < i_data_packet_min )
979 /* if packet length too short, there is extra padding */
980 pkt.padding_length += i_data_packet_min - pkt.length;
981 pkt.length = i_data_packet_min;
984 pkt.send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
985 /* uint16_t i_packet_duration = GetWLE( p_peek + i_skip ); */ i_skip += 2;
987 i_return = stream_Peek( p_demux->s, &p_peek, pkt.length );
988 if( i_return <= 0 || pkt.length == 0 || (unsigned int)i_return < pkt.length )
990 msg_Warn( p_demux, "cannot peek, EOF ?" );
994 int i_payload_count = 1;
995 pkt.length_type = 0x02; //unused
998 i_payload_count = p_peek[i_skip] & 0x3f;
999 pkt.length_type = ( p_peek[i_skip] >> 6 )&0x03;
1004 msg_Dbg(p_demux, "%d payloads", i_payload_count);
1007 pkt.i_skip = i_skip;
1008 pkt.p_peek = p_peek;
1009 pkt.left = pkt.length;
1011 for( int i_payload = 0; i_payload < i_payload_count ; i_payload++ )
1012 if (DemuxPayload(p_demux, &pkt, i_payload) < 0)
1014 msg_Warn( p_demux, "payload err %d / %d", i_payload + 1, i_payload_count );
1021 if( pkt.left > pkt.padding_length )
1022 msg_Warn( p_demux, "Didn't read %"PRIu32" bytes in the packet",
1023 pkt.left - pkt.padding_length );
1024 else if( pkt.left < pkt.padding_length )
1025 msg_Warn( p_demux, "Read %"PRIu32" too much bytes in the packet",
1026 pkt.padding_length - pkt.left );
1028 int i_return = stream_Read( p_demux->s, NULL, pkt.left );
1029 if( i_return < 0 || (unsigned int) i_return < pkt.left )
1031 msg_Err( p_demux, "cannot skip data, EOF ?" );
1038 loop_error_recovery:
1039 msg_Warn( p_demux, "unsupported packet header" );
1040 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
1042 msg_Err( p_demux, "unsupported packet header, fatal error" );
1045 i_return = stream_Read( p_demux->s, NULL, i_data_packet_min );
1046 if( i_return <= 0 || (unsigned int) i_return != i_data_packet_min )
1048 msg_Warn( p_demux, "cannot skip data, EOF ?" );
1055 /*****************************************************************************
1057 *****************************************************************************/
1058 typedef struct asf_es_priorities_t
1060 uint16_t *pi_stream_numbers;
1062 } asf_es_priorities_t;
1064 /* Fills up our exclusion list */
1065 static void ASF_fillup_es_priorities_ex( demux_sys_t *p_sys, void *p_hdr,
1066 asf_es_priorities_t *p_prios )
1068 /* Find stream exclusions */
1069 asf_object_advanced_mutual_exclusion_t *p_mutex =
1070 ASF_FindObject( p_hdr, &asf_object_advanced_mutual_exclusion, 0 );
1071 if (! p_mutex ) return;
1073 #if ( UINT_MAX > SIZE_MAX / 2 )
1074 if ( p_sys->i_track > (size_t)SIZE_MAX / sizeof(uint16_t) )
1077 p_prios->pi_stream_numbers = malloc( (size_t)p_sys->i_track * sizeof(uint16_t) );
1078 if ( !p_prios->pi_stream_numbers ) return;
1080 if ( p_mutex->i_stream_number_count )
1082 /* Just set highest prio on highest in the group */
1083 for ( uint16_t i = 1; i < p_mutex->i_stream_number_count; i++ )
1085 if ( p_prios->i_count > p_sys->i_track ) break;
1086 p_prios->pi_stream_numbers[ p_prios->i_count++ ] = p_mutex->pi_stream_number[ i ];
1091 /* Fills up our bitrate exclusion list */
1092 static void ASF_fillup_es_bitrate_priorities_ex( demux_sys_t *p_sys, void *p_hdr,
1093 asf_es_priorities_t *p_prios )
1095 /* Find bitrate exclusions */
1096 asf_object_bitrate_mutual_exclusion_t *p_bitrate_mutex =
1097 ASF_FindObject( p_hdr, &asf_object_bitrate_mutual_exclusion_guid, 0 );
1098 if (! p_bitrate_mutex ) return;
1100 #if ( UINT_MAX > SIZE_MAX / 2 )
1101 if ( p_sys->i_track > (size_t)SIZE_MAX / sizeof(uint16_t) )
1104 p_prios->pi_stream_numbers = malloc( (size_t)p_sys->i_track * sizeof( uint16_t ) );
1105 if ( !p_prios->pi_stream_numbers ) return;
1107 if ( p_bitrate_mutex->i_stream_number_count )
1109 /* Just remove < highest */
1110 for ( uint16_t i = 1; i < p_bitrate_mutex->i_stream_number_count; i++ )
1112 if ( p_prios->i_count > p_sys->i_track ) break;
1113 p_prios->pi_stream_numbers[ p_prios->i_count++ ] = p_bitrate_mutex->pi_stream_numbers[ i ];
1119 #define GET_CHECKED( target, getter, maxtarget, temp ) \
1121 temp i_temp = getter;\
1122 if ( i_temp > maxtarget ) {\
1123 msg_Warn( p_demux, "rejecting stream %u : " #target " overflow", i_stream );\
1124 es_format_Clean( &fmt );\
1131 static int DemuxInit( demux_t *p_demux )
1133 demux_sys_t *p_sys = p_demux->p_sys;
1137 p_sys->i_length = 0;
1138 p_sys->i_bitrate = 0;
1139 p_sys->p_root = NULL;
1143 p_sys->i_seek_track = 0;
1144 p_sys->i_wait_keyframe = 0;
1145 for( int i = 0; i < MAX_ASF_TRACKS; i++ )
1147 p_sys->track[i] = NULL;
1149 p_sys->i_data_begin = 0;
1150 p_sys->i_data_end = 0;
1153 /* Now load all object ( except raw data ) */
1154 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_canfastseek );
1155 if( !(p_sys->p_root = ASF_ReadObjectRoot(p_demux->s, p_sys->b_canfastseek)) )
1157 msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
1158 return VLC_EGENERIC;
1160 p_sys->p_fp = p_sys->p_root->p_fp;
1162 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
1164 msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
1168 if ( ASF_FindObject( p_sys->p_root->p_hdr,
1169 &asf_object_content_encryption_guid, 0 ) != NULL
1170 || ASF_FindObject( p_sys->p_root->p_hdr,
1171 &asf_object_extended_content_encryption_guid, 0 ) != NULL
1172 || ASF_FindObject( p_sys->p_root->p_hdr,
1173 &asf_object_advanced_content_encryption_guid, 0 ) != NULL )
1175 dialog_Fatal( p_demux, _("Could not demux ASF stream"), "%s",
1176 _("DRM protected streams are not supported.") );
1180 p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
1181 &asf_object_stream_properties_guid );
1182 if( p_sys->i_track == 0 )
1184 msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
1187 msg_Dbg( p_demux, "found %u streams", p_sys->i_track );
1189 /* check if index is available */
1190 asf_object_index_t *p_index = ASF_FindObject( p_sys->p_root,
1191 &asf_object_simple_index_guid, 0 );
1192 const bool b_index = p_index && p_index->i_index_entry_count;
1194 /* Find the extended header if any */
1195 asf_object_t *p_hdr_ext = ASF_FindObject( p_sys->p_root->p_hdr,
1196 &asf_object_header_extension_guid, 0 );
1198 asf_object_language_list_t *p_languages = NULL;
1199 asf_es_priorities_t fmt_priorities_ex = { NULL, 0 };
1200 asf_es_priorities_t fmt_priorities_bitrate_ex = { NULL, 0 };
1204 p_languages = ASF_FindObject( p_hdr_ext, &asf_object_language_list, 0 );
1206 ASF_fillup_es_priorities_ex( p_sys, p_hdr_ext, &fmt_priorities_ex );
1207 ASF_fillup_es_bitrate_priorities_ex( p_sys, p_hdr_ext, &fmt_priorities_bitrate_ex );
1210 for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1213 asf_object_stream_properties_t *p_sp;
1214 asf_object_extended_stream_properties_t *p_esp;
1215 bool b_access_selected;
1217 p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
1218 &asf_object_stream_properties_guid,
1222 tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
1223 memset( tk, 0, sizeof( asf_track_t ) );
1231 if ( strncmp( p_demux->psz_access, "mms", 3 ) )
1233 /* Check (not mms) if this track is selected (ie will receive data) */
1234 if( !stream_Control( p_demux->s, STREAM_GET_PRIVATE_ID_STATE,
1235 (int) p_sp->i_stream_number, &b_access_selected ) &&
1236 !b_access_selected )
1238 tk->i_cat = UNKNOWN_ES;
1239 msg_Dbg( p_demux, "ignoring not selected stream(ID:%u) (by access)",
1240 p_sp->i_stream_number );
1245 /* Find the associated extended_stream_properties if any */
1248 int i_ext_stream = ASF_CountObject( p_hdr_ext,
1249 &asf_object_extended_stream_properties_guid );
1250 for( int i = 0; i < i_ext_stream; i++ )
1252 asf_object_t *p_tmp =
1253 ASF_FindObject( p_hdr_ext,
1254 &asf_object_extended_stream_properties_guid, i );
1255 if( p_tmp->ext_stream.i_stream_number == p_sp->i_stream_number )
1257 p_esp = &p_tmp->ext_stream;
1266 if( guidcmp( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
1267 p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
1269 uint8_t *p_data = p_sp->p_type_specific_data;
1272 es_format_Init( &fmt, AUDIO_ES, 0 );
1273 i_format = GetWLE( &p_data[0] );
1274 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
1276 GET_CHECKED( fmt.audio.i_channels, GetWLE( &p_data[2] ),
1278 GET_CHECKED( fmt.audio.i_rate, GetDWLE( &p_data[4] ),
1279 UINT_MAX, uint32_t );
1280 GET_CHECKED( fmt.i_bitrate, GetDWLE( &p_data[8] ) * 8,
1281 UINT_MAX, uint32_t );
1282 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
1283 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
1285 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
1286 i_format != WAVE_FORMAT_MPEGLAYER3 &&
1287 i_format != WAVE_FORMAT_MPEG )
1289 GET_CHECKED( fmt.i_extra, __MIN( GetWLE( &p_data[16] ),
1290 p_sp->i_type_specific_data_length -
1291 sizeof( WAVEFORMATEX ) ),
1292 INT_MAX, uint32_t );
1293 fmt.p_extra = malloc( fmt.i_extra );
1294 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
1298 msg_Dbg( p_demux, "added new audio stream(codec:0x%x,ID:%d)",
1299 GetWLE( p_data ), p_sp->i_stream_number );
1301 else if( guidcmp( &p_sp->i_stream_type,
1302 &asf_object_stream_type_video ) &&
1303 p_sp->i_type_specific_data_length >= 11 +
1304 sizeof( VLC_BITMAPINFOHEADER ) )
1306 uint8_t *p_data = &p_sp->p_type_specific_data[11];
1308 es_format_Init( &fmt, VIDEO_ES,
1309 VLC_FOURCC( p_data[16], p_data[17],
1310 p_data[18], p_data[19] ) );
1312 GET_CHECKED( fmt.video.i_width, GetDWLE( p_data + 4 ),
1313 UINT_MAX, uint32_t );
1314 GET_CHECKED( fmt.video.i_height, GetDWLE( p_data + 8 ),
1315 UINT_MAX, uint32_t );
1317 if( p_esp && p_esp->i_average_time_per_frame > 0 )
1319 fmt.video.i_frame_rate = 10000000;
1320 GET_CHECKED( fmt.video.i_frame_rate_base,
1321 p_esp->i_average_time_per_frame,
1322 UINT_MAX, uint64_t );
1325 if( fmt.i_codec == VLC_FOURCC( 'D','V','R',' ') )
1327 /* DVR-MS special ASF */
1328 fmt.i_codec = VLC_FOURCC( 'm','p','g','2' ) ;
1329 fmt.b_packetized = false;
1332 if( p_sp->i_type_specific_data_length > 11 +
1333 sizeof( VLC_BITMAPINFOHEADER ) )
1335 GET_CHECKED( fmt.i_extra, __MIN( GetDWLE( p_data ),
1336 p_sp->i_type_specific_data_length - 11 -
1337 sizeof( VLC_BITMAPINFOHEADER ) ),
1338 UINT_MAX, uint32_t );
1339 fmt.p_extra = malloc( fmt.i_extra );
1340 memcpy( fmt.p_extra, &p_data[sizeof( VLC_BITMAPINFOHEADER )],
1344 /* Look for an aspect ratio */
1345 if( p_sys->p_root->p_metadata )
1347 asf_object_metadata_t *p_meta = p_sys->p_root->p_metadata;
1348 unsigned int i_aspect_x = 0, i_aspect_y = 0;
1350 for( i = 0; i < p_meta->i_record_entries_count; i++ )
1352 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioX" ) )
1354 if( (!i_aspect_x && !p_meta->record[i].i_stream) ||
1355 p_meta->record[i].i_stream ==
1356 p_sp->i_stream_number )
1357 GET_CHECKED( i_aspect_x, p_meta->record[i].i_val,
1358 UINT_MAX, uint64_t );
1360 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioY" ) )
1362 if( (!i_aspect_y && !p_meta->record[i].i_stream) ||
1363 p_meta->record[i].i_stream ==
1364 p_sp->i_stream_number )
1365 GET_CHECKED( i_aspect_y, p_meta->record[i].i_val,
1366 UINT_MAX, uint64_t );
1370 if( i_aspect_x && i_aspect_y )
1372 fmt.video.i_sar_num = i_aspect_x;
1373 fmt.video.i_sar_den = i_aspect_y;
1377 /* If there is a video track then use the index for seeking */
1378 p_sys->b_index = b_index;
1380 msg_Dbg( p_demux, "added new video stream(ID:%d)",
1381 p_sp->i_stream_number );
1383 else if( guidcmp( &p_sp->i_stream_type, &asf_object_extended_stream_header ) &&
1384 p_sp->i_type_specific_data_length >= 64 )
1386 /* Now follows a 64 byte header of which we don't know much */
1387 guid_t *p_ref = (guid_t *)p_sp->p_type_specific_data;
1388 uint8_t *p_data = p_sp->p_type_specific_data + 64;
1389 unsigned int i_data = p_sp->i_type_specific_data_length - 64;
1391 msg_Dbg( p_demux, "Ext stream header detected. datasize = %d", p_sp->i_type_specific_data_length );
1392 if( guidcmp( p_ref, &asf_object_extended_stream_type_audio ) &&
1393 i_data >= sizeof( WAVEFORMATEX ) - 2)
1396 es_format_Init( &fmt, AUDIO_ES, 0 );
1397 i_format = GetWLE( &p_data[0] );
1399 fmt.i_codec = VLC_CODEC_A52;
1401 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
1402 GET_CHECKED( fmt.audio.i_channels, GetWLE( &p_data[2] ),
1404 GET_CHECKED( fmt.audio.i_rate, GetDWLE( &p_data[4] ),
1405 UINT_MAX, uint32_t );
1406 GET_CHECKED( fmt.i_bitrate, GetDWLE( &p_data[8] ) * 8,
1407 UINT_MAX, uint32_t );
1408 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
1409 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
1410 fmt.b_packetized = true;
1412 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
1413 i_format != WAVE_FORMAT_MPEGLAYER3 &&
1414 i_format != WAVE_FORMAT_MPEG )
1416 GET_CHECKED( fmt.i_extra, __MIN( GetWLE( &p_data[16] ),
1417 p_sp->i_type_specific_data_length -
1418 sizeof( WAVEFORMATEX ) ),
1419 INT_MAX, uint32_t );
1420 fmt.p_extra = malloc( fmt.i_extra );
1421 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
1425 msg_Dbg( p_demux, "added new audio stream (codec:0x%x,ID:%d)",
1426 i_format, p_sp->i_stream_number );
1430 es_format_Init( &fmt, UNKNOWN_ES, 0 );
1435 es_format_Init( &fmt, UNKNOWN_ES, 0 );
1438 tk->i_cat = fmt.i_cat;
1439 if( fmt.i_cat != UNKNOWN_ES )
1441 if( p_esp && p_languages &&
1442 p_esp->i_language_index < p_languages->i_language )
1444 fmt.psz_language = strdup( p_languages->ppsz_language[p_esp->i_language_index] );
1446 if( fmt.psz_language && (p = strchr( fmt.psz_language, '-' )) )
1450 /* Set our priority so we won't get multiple videos */
1451 int i_priority = ES_PRIORITY_SELECTABLE_MIN;
1452 for( uint16_t i = 0; i < fmt_priorities_ex.i_count; i++ )
1454 if ( fmt_priorities_ex.pi_stream_numbers[i] == p_sp->i_stream_number )
1456 i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
1460 for( uint16_t i = 0; i < fmt_priorities_bitrate_ex.i_count; i++ )
1462 if ( fmt_priorities_bitrate_ex.pi_stream_numbers[i] == p_sp->i_stream_number )
1464 i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
1468 fmt.i_priority = i_priority;
1470 if ( i_stream <= INT_MAX )
1471 fmt.i_id = i_stream;
1473 msg_Warn( p_demux, "Can't set fmt.i_id to match stream id %u", i_stream );
1475 if ( fmt.i_cat == VIDEO_ES )
1477 /* Backup our video format */
1478 tk->p_fmt = malloc( sizeof( es_format_t ) );
1480 es_format_Copy( tk->p_fmt, &fmt );
1483 tk->p_es = es_out_Add( p_demux->out, &fmt );
1485 if( !stream_Control( p_demux->s, STREAM_GET_PRIVATE_ID_STATE,
1486 (int) p_sp->i_stream_number, &b_access_selected ) &&
1489 p_sys->i_access_selected_track[fmt.i_cat] = p_sp->i_stream_number;
1495 msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
1496 p_sp->i_stream_number );
1499 es_format_Clean( &fmt );
1502 free( fmt_priorities_ex.pi_stream_numbers );
1503 free( fmt_priorities_bitrate_ex.pi_stream_numbers );
1505 p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
1506 if( p_sys->p_root->p_data->i_object_size != 0 )
1508 p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
1509 p_sys->p_root->p_data->i_object_size;
1510 p_sys->i_data_end = __MIN( (uint64_t)stream_Size( p_demux->s ), p_sys->i_data_end );
1513 { /* live/broacast */
1514 p_sys->i_data_end = 0;
1517 /* go to first packet */
1518 stream_Seek( p_demux->s, p_sys->i_data_begin );
1520 /* try to calculate movie time */
1521 if( p_sys->p_fp->i_data_packets_count > 0 )
1524 uint64_t i_size = stream_Size( p_demux->s );
1526 if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
1528 i_size = p_sys->i_data_end;
1531 /* real number of packets */
1532 i_count = ( i_size - p_sys->i_data_begin ) /
1533 p_sys->p_fp->i_min_data_packet_size;
1535 /* calculate the time duration in micro-s */
1536 p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
1538 (mtime_t)p_sys->p_fp->i_data_packets_count - p_sys->p_fp->i_preroll * 1000;
1539 if( p_sys->i_length < 0 )
1540 p_sys->i_length = 0;
1542 if( p_sys->i_length > 0 )
1544 p_sys->i_bitrate = 8 * i_size * 1000000 / p_sys->i_length;
1548 /* Create meta information */
1549 p_sys->meta = vlc_meta_New();
1551 asf_object_content_description_t *p_cd;
1552 if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
1553 &asf_object_content_description_guid, 0 ) ) )
1555 if( p_cd->psz_title && *p_cd->psz_title )
1557 vlc_meta_SetTitle( p_sys->meta, p_cd->psz_title );
1559 if( p_cd->psz_artist && *p_cd->psz_artist )
1561 vlc_meta_SetArtist( p_sys->meta, p_cd->psz_artist );
1563 if( p_cd->psz_copyright && *p_cd->psz_copyright )
1565 vlc_meta_SetCopyright( p_sys->meta, p_cd->psz_copyright );
1567 if( p_cd->psz_description && *p_cd->psz_description )
1569 vlc_meta_SetDescription( p_sys->meta, p_cd->psz_description );
1571 if( p_cd->psz_rating && *p_cd->psz_rating )
1573 vlc_meta_SetRating( p_sys->meta, p_cd->psz_rating );
1576 /// \tood Fix Child meta for ASF tracks
1578 for( i_stream = 0, i = 0; i < MAX_ASF_TRACKS; i++ )
1580 asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
1581 &asf_object_codec_list_guid, 0 );
1583 if( p_sys->track[i] )
1585 vlc_meta_t *tk = vlc_meta_New();
1586 TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
1588 if( p_cl && i_stream < p_cl->i_codec_entries_count )
1590 if( p_cl->codec[i_stream].psz_name &&
1591 *p_cl->codec[i_stream].psz_name )
1593 vlc_meta_Add( tk, VLC_META_CODEC_NAME,
1594 p_cl->codec[i_stream].psz_name );
1596 if( p_cl->codec[i_stream].psz_description &&
1597 *p_cl->codec[i_stream].psz_description )
1599 vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
1600 p_cl->codec[i_stream].psz_description );
1610 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1611 return VLC_EGENERIC;
1614 /*****************************************************************************
1615 * FlushRemainingPackets: flushes tail packets
1616 *****************************************************************************/
1618 static void FlushRemainingPackets( demux_t *p_demux )
1620 demux_sys_t *p_sys = p_demux->p_sys;
1621 for ( unsigned int i = 0; i < MAX_ASF_TRACKS; i++ )
1623 asf_track_t *tk = p_sys->track[i];
1624 if( tk && tk->p_frame )
1625 SendPacket( p_demux, tk );
1629 /*****************************************************************************
1631 *****************************************************************************/
1632 static void DemuxEnd( demux_t *p_demux )
1634 demux_sys_t *p_sys = p_demux->p_sys;
1638 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1639 p_sys->p_root = NULL;
1643 vlc_meta_Delete( p_sys->meta );
1647 for( int i = 0; i < MAX_ASF_TRACKS; i++ )
1649 asf_track_t *tk = p_sys->track[i];
1654 block_ChainRelease( tk->p_frame );
1658 es_out_Del( p_demux->out, tk->p_es );
1662 es_format_Clean( tk->p_fmt );
1667 p_sys->track[i] = 0;