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>
47 * - add support for the newly added object: language, bitrate,
48 * extended stream properties.
51 /*****************************************************************************
53 *****************************************************************************/
54 static int Open ( vlc_object_t * );
55 static void Close ( vlc_object_t * );
58 set_category( CAT_INPUT )
59 set_subcategory( SUBCAT_INPUT_DEMUX )
60 set_description( N_("ASF/WMV demuxer") )
61 set_capability( "demux", 200 )
62 set_callbacks( Open, Close )
63 add_shortcut( "asf", "wmv" )
67 /*****************************************************************************
69 *****************************************************************************/
70 static int Demux ( demux_t * );
71 static int Control( demux_t *, int i_query, va_list args );
72 static void FlushRemainingPackets( demux_t *p_demux );
74 #define MAX_ASF_TRACKS 128
81 es_format_t *p_fmt; /* format backup for video changes */
83 asf_object_stream_properties_t *p_sp;
84 asf_object_extended_stream_properties_t *p_esp;
88 block_t *p_frame; /* use to gather complete frame */
93 mtime_t i_time; /* s */
94 mtime_t i_length; /* length of file file */
95 uint64_t i_bitrate; /* global file bitrate */
97 asf_object_root_t *p_root;
98 asf_object_file_properties_t *p_fp;
100 unsigned int i_track;
101 asf_track_t *track[MAX_ASF_TRACKS]; /* track number is stored on 7 bits */
103 uint64_t i_data_begin;
108 uint8_t i_seek_track;
109 unsigned int i_wait_keyframe;
114 static mtime_t GetMoviePTS( demux_sys_t * );
115 static int DemuxInit( demux_t * );
116 static void DemuxEnd( demux_t * );
117 static int DemuxPacket( demux_t * );
119 /*****************************************************************************
120 * Open: check file and initializes ASF structures
121 *****************************************************************************/
122 static int Open( vlc_object_t * p_this )
124 demux_t *p_demux = (demux_t *)p_this;
127 const uint8_t *p_peek;
129 /* A little test to see if it could be a asf stream */
130 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 ) return VLC_EGENERIC;
132 ASF_GetGUID( &guid, p_peek );
133 if( !guidcmp( &guid, &asf_object_header_guid ) ) return VLC_EGENERIC;
135 /* Set p_demux fields */
136 p_demux->pf_demux = Demux;
137 p_demux->pf_control = Control;
138 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
140 /* Load the headers */
141 if( DemuxInit( p_demux ) )
149 /*****************************************************************************
150 * Demux: read packet and send them to decoders
151 *****************************************************************************/
152 static int Demux( demux_t *p_demux )
154 demux_sys_t *p_sys = p_demux->p_sys;
158 const uint8_t *p_peek;
160 mtime_t i_time_begin = GetMoviePTS( p_sys );
163 if( !vlc_object_alive (p_demux) )
166 /* FIXME: returns EOF too early for some mms streams */
167 if( p_sys->i_data_end >= 0 &&
168 stream_Tell( p_demux->s ) >= p_sys->i_data_end )
172 /* Check if we have concatenated files */
173 if( stream_Peek( p_demux->s, &p_peek, 16 ) == 16 )
177 ASF_GetGUID( &guid, p_peek );
178 if( guidcmp( &guid, &asf_object_header_guid ) )
180 msg_Warn( p_demux, "found a new ASF header" );
181 /* We end this stream */
184 /* And we prepare to read the next one */
185 if( DemuxInit( p_demux ) )
187 msg_Err( p_demux, "failed to load the new header" );
188 dialog_Fatal( p_demux, _("Could not demux ASF stream"), "%s",
189 _("VLC failed to load the ASF header.") );
192 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
197 /* Read and demux a packet */
198 if( ( i_result = DemuxPacket( p_demux ) ) <= 0 )
200 FlushRemainingPackets( p_demux );
203 if( i_time_begin == -1 )
205 i_time_begin = GetMoviePTS( p_sys );
209 i_length = GetMoviePTS( p_sys ) - i_time_begin;
210 if( i_length < 0 || i_length >= 40 * 1000 ) break;
215 p_sys->i_time = GetMoviePTS( p_sys );
216 if( p_sys->i_time >= 0 )
219 msg_Dbg( p_demux, "Setting PCR to %"PRId64, p_sys->i_time );
221 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time+1 );
227 /*****************************************************************************
228 * Close: frees unused data
229 *****************************************************************************/
230 static void Close( vlc_object_t * p_this )
232 demux_t *p_demux = (demux_t *)p_this;
236 free( p_demux->p_sys );
239 /*****************************************************************************
240 * WaitKeyframe: computes the number of frames to wait for a keyframe
241 *****************************************************************************/
242 static void WaitKeyframe( demux_t *p_demux )
244 demux_sys_t *p_sys = p_demux->p_sys;
245 if ( ! p_sys->i_seek_track )
247 for ( int i=0; i<MAX_ASF_TRACKS; i++ )
249 asf_track_t *tk = p_sys->track[i];
250 if ( tk && tk->p_sp && tk->i_cat == VIDEO_ES )
252 bool b_selected = false;
253 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
254 tk->p_es, &b_selected );
257 p_sys->i_seek_track = tk->p_sp->i_stream_number;
264 if ( p_sys->i_seek_track )
266 /* Skip forward at least 1 min */
267 asf_track_t *tk = p_sys->track[p_sys->i_seek_track];
268 if ( tk->p_esp && tk->p_esp->i_average_time_per_frame )
270 /* 1 min if fastseek, otherwise 5 sec */
271 /* That's a guess for bandwidth */
272 uint64_t i_maxwaittime = ( p_sys->b_canfastseek ) ? 600000000 : 50000000;
273 i_maxwaittime /= tk->p_esp->i_average_time_per_frame;
274 p_sys->i_wait_keyframe = __MIN( i_maxwaittime, UINT_MAX );
278 p_sys->i_wait_keyframe = ( p_sys->b_canfastseek ) ? 25 * 30 : 25 * 5;
283 p_sys->i_wait_keyframe = 0;
288 /*****************************************************************************
289 * SeekIndex: goto to i_date or i_percent
290 *****************************************************************************/
291 static int SeekPercent( demux_t *p_demux, int i_query, va_list args )
293 demux_sys_t *p_sys = p_demux->p_sys;
295 WaitKeyframe( p_demux );
297 msg_Dbg( p_demux, "seek with percent: waiting %i frames", p_sys->i_wait_keyframe );
298 return demux_vaControlHelper( p_demux->s, __MIN( INT64_MAX, p_sys->i_data_begin ),
299 __MIN( INT64_MAX, p_sys->i_data_end ),
300 __MIN( INT64_MAX, p_sys->i_bitrate ),
301 __MIN( INT16_MAX, p_sys->p_fp->i_min_data_packet_size ),
305 static int SeekIndex( demux_t *p_demux, mtime_t i_date, float f_pos )
307 demux_sys_t *p_sys = p_demux->p_sys;
308 asf_object_index_t *p_index;
310 msg_Dbg( p_demux, "seek with index: %i seconds, position %f",
311 i_date >= 0 ? (int)(i_date/1000000) : -1, f_pos );
314 i_date = p_sys->i_length * f_pos;
316 p_index = ASF_FindObject( p_sys->p_root, &asf_object_simple_index_guid, 0 );
318 uint64_t i_entry = i_date * 10 / p_index->i_index_entry_time_interval;
319 if( i_entry >= p_index->i_index_entry_count )
321 msg_Warn( p_demux, "Incomplete index" );
325 WaitKeyframe( p_demux );
327 uint64_t i_offset = (uint64_t)p_index->index_entry[i_entry].i_packet_number *
328 p_sys->p_fp->i_min_data_packet_size;
330 if ( stream_Seek( p_demux->s, i_offset + p_sys->i_data_begin ) == VLC_SUCCESS )
332 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, VLC_TS_0 + i_date );
335 else return VLC_EGENERIC;
338 static void SeekPrepare( demux_t *p_demux )
340 demux_sys_t *p_sys = p_demux->p_sys;
342 p_sys->i_time = VLC_TS_INVALID;
343 for( int i = 0; i < MAX_ASF_TRACKS ; i++ )
345 asf_track_t *tk = p_sys->track[i];
349 tk->i_time = VLC_TS_INVALID;
351 block_ChainRelease( tk->p_frame );
355 es_out_Control( p_demux->out, ES_OUT_RESET_PCR, VLC_TS_INVALID );
358 /*****************************************************************************
360 *****************************************************************************/
361 static int Control( demux_t *p_demux, int i_query, va_list args )
363 demux_sys_t *p_sys = p_demux->p_sys;
371 case DEMUX_GET_LENGTH:
372 pi64 = (int64_t*)va_arg( args, int64_t * );
373 *pi64 = p_sys->i_length;
377 pi64 = (int64_t*)va_arg( args, int64_t * );
378 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
379 *pi64 = p_sys->i_time;
384 ! ( p_sys->p_fp->i_flags & ASF_FILE_PROPERTIES_SEEKABLE ) )
387 SeekPrepare( p_demux );
389 if( p_sys->b_index && p_sys->i_length > 0 )
392 va_copy( acpy, args );
393 i64 = (int64_t)va_arg( acpy, int64_t );
396 if( !SeekIndex( p_demux, i64, -1 ) )
399 return SeekPercent( p_demux, i_query, args );
403 i = (int)va_arg( args, int );
404 int i_ret = stream_Control( p_demux->s,
405 STREAM_SET_PRIVATE_ID_STATE, i, true );
406 if ( i_ret == VLC_SUCCESS )
408 SeekPrepare( p_demux );
409 p_sys->i_seek_track = 0;
410 WaitKeyframe( p_demux );
415 case DEMUX_GET_POSITION:
416 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
417 if( p_sys->i_length > 0 )
419 pf = (double*)va_arg( args, double * );
420 *pf = p_sys->i_time / (double)p_sys->i_length;
423 return demux_vaControlHelper( p_demux->s,
424 __MIN( INT64_MAX, p_sys->i_data_begin ),
425 __MIN( INT64_MAX, p_sys->i_data_end ),
426 __MIN( INT64_MAX, p_sys->i_bitrate ),
427 __MIN( INT16_MAX, p_sys->p_fp->i_min_data_packet_size ),
430 case DEMUX_SET_POSITION:
432 ! ( p_sys->p_fp->i_flags & ASF_FILE_PROPERTIES_SEEKABLE ) )
435 SeekPrepare( p_demux );
437 if( p_sys->b_index && p_sys->i_length > 0 )
440 va_copy( acpy, args );
441 f = (double)va_arg( acpy, double );
444 if( !SeekIndex( p_demux, -1, f ) )
447 return SeekPercent( p_demux, i_query, args );
450 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
451 vlc_meta_Merge( p_meta, p_sys->meta );
456 ! ( p_sys->p_fp->i_flags & ASF_FILE_PROPERTIES_SEEKABLE ) )
458 bool *pb_bool = (bool*)va_arg( args, bool * );
465 return demux_vaControlHelper( p_demux->s,
466 __MIN( INT64_MAX, p_sys->i_data_begin ),
467 __MIN( INT64_MAX, p_sys->i_data_end),
468 __MIN( INT64_MAX, p_sys->i_bitrate ),
469 ( p_sys->p_fp ) ? __MIN( INT_MAX, p_sys->p_fp->i_min_data_packet_size ) : 1,
474 /*****************************************************************************
476 *****************************************************************************/
477 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
482 for( i = 0; i < MAX_ASF_TRACKS ; i++ )
484 asf_track_t *tk = p_sys->track[i];
486 if( tk && tk->p_es && tk->i_time > 0)
488 if( i_time < 0 ) i_time = tk->i_time;
489 else i_time = __MIN( i_time, tk->i_time );
496 static inline int GetValue2b(uint32_t *var, const uint8_t *p, unsigned int *skip, int left, int bits)
503 *var = p[*skip]; *skip += 1;
508 *var = GetWLE(&p[*skip]); *skip += 2;
513 *var = GetDWLE(&p[*skip]); *skip += 4;
525 uint32_t padding_length;
530 /* buffer handling for this ASF packet */
532 const uint8_t *p_peek;
536 static void SendPacket(demux_t *p_demux, asf_track_t *tk)
538 demux_sys_t *p_sys = p_demux->p_sys;
540 block_t *p_gather = block_ChainGather( tk->p_frame );
542 if( p_sys->i_time < VLC_TS_0 && tk->i_time > VLC_TS_INVALID )
544 p_sys->i_time = tk->i_time;
545 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
547 msg_Dbg( p_demux, " setting PCR to %"PRId64, p_sys->i_time );
552 msg_Dbg( p_demux, " sending packet dts %"PRId64" pts %"PRId64" pcr %"PRId64, p_gather->i_dts, p_gather->i_pts, p_sys->i_time );
555 es_out_Send( p_demux->out, tk->p_es, p_gather );
559 static int DemuxSubPayload(demux_t *p_demux, asf_track_t *tk,
560 uint32_t i_sub_payload_data_length, mtime_t i_pts, mtime_t i_dts,
561 uint32_t i_media_object_offset, bool b_keyframe )
563 /* FIXME I don't use i_media_object_number, sould I ? */
564 if( tk->p_frame && i_media_object_offset == 0 )
565 SendPacket(p_demux, tk);
567 block_t *p_frag = stream_Block( p_demux->s, i_sub_payload_data_length );
568 if( p_frag == NULL ) {
569 msg_Warn( p_demux, "cannot read data" );
573 p_frag->i_pts = VLC_TS_0 + i_pts;
574 p_frag->i_dts = VLC_TS_0 + i_dts;
576 p_frag->i_flags |= BLOCK_FLAG_TYPE_I;
578 block_ChainAppend( &tk->p_frame, p_frag );
583 static uint32_t SkipBytes( stream_t *s, uint32_t i_bytes )
586 int i_to_read = __MIN(i_bytes, INT_MAX);
587 uint32_t i_bytes_read = 0;
591 i_read = stream_Read( s, NULL, i_to_read );
593 i_bytes_read += i_read;
594 if ( i_read < i_to_read || i_bytes == 0 )
599 i_to_read = __MIN(i_bytes, INT_MAX);
605 static void ParsePayloadExtensions(demux_t *p_demux, asf_track_t *tk,
606 const struct asf_packet_t *pkt,
607 uint32_t i_length, bool *b_keyframe )
609 if ( !tk || !tk->p_esp || !tk->p_esp->p_ext ) return;
610 const uint8_t *p_data = pkt->p_peek + pkt->i_skip + 8;
612 uint16_t i_payload_extensions_size;
613 asf_payload_extension_system_t *p_ext = NULL;
615 /* Extensions always come in the declared order */
616 for ( int i=0; i< tk->p_esp->i_payload_extension_system_count; i++ )
618 p_ext = &tk->p_esp->p_ext[i];
619 if ( p_ext->i_data_size == 0xFFFF ) /* Variable length extension data */
621 if ( i_length < 2 ) return;
622 i_payload_extensions_size = GetWLE( p_data );
625 i_payload_extensions_size = 0;
629 i_payload_extensions_size = p_ext->i_data_size;
632 if ( i_length < i_payload_extensions_size ) return;
634 if ( guidcmp( &p_ext->i_extension_id, &mfasf_sampleextension_outputcleanpoint_guid ) )
636 if ( i_payload_extensions_size != sizeof(uint8_t) ) goto sizeerror;
637 *b_keyframe |= *p_data;
639 else if ( guidcmp( &p_ext->i_extension_id, &asf_dvr_sampleextension_videoframe_guid ) )
641 if ( i_payload_extensions_size != sizeof(uint32_t) ) goto sizeerror;
643 uint32_t i_val = GetDWLE( p_data );
644 /* Valid keyframe must be a split frame start fragment */
645 *b_keyframe = i_val & ASF_EXTENSION_VIDEOFRAME_NEWFRAME;
648 /* And flagged as IFRAME */
649 *b_keyframe |= ( ( i_val & ASF_EXTENSION_VIDEOFRAME_TYPE_MASK )
650 == ASF_EXTENSION_VIDEOFRAME_IFRAME );
653 else if ( guidcmp( &p_ext->i_extension_id, &mfasf_sampleextension_pixelaspectratio_guid ) )
655 if ( i_payload_extensions_size != sizeof(uint16_t) ) goto sizeerror;
657 uint8_t i_ratio_x = *p_data;
658 uint8_t i_ratio_y = *(p_data + 1);
659 if ( tk->p_fmt->video.i_sar_num != i_ratio_x || tk->p_fmt->video.i_sar_den != i_ratio_y )
661 /* Only apply if origin pixel size >= 1x1, due to broken yacast */
662 if ( tk->p_fmt->video.i_height * i_ratio_x > tk->p_fmt->video.i_width * i_ratio_y )
664 vout_thread_t *p_vout = input_GetVout( p_demux->p_input );
667 msg_Info( p_demux, "Changing aspect ratio to %i/%i", i_ratio_x, i_ratio_y );
668 vout_ChangeAspectRatio( p_vout, i_ratio_x, i_ratio_y );
669 vlc_object_release( p_vout );
672 tk->p_fmt->video.i_sar_num = i_ratio_x;
673 tk->p_fmt->video.i_sar_den = i_ratio_y;
676 i_length -= i_payload_extensions_size;
677 p_data += i_payload_extensions_size;
683 msg_Warn( p_demux, "Unknown extension " GUID_FMT " data size of %u",
684 GUID_PRINT( p_ext->i_extension_id ), i_payload_extensions_size );
687 static int DemuxPayload(demux_t *p_demux, struct asf_packet_t *pkt, int i_payload)
690 VLC_UNUSED( i_payload );
692 demux_sys_t *p_sys = p_demux->p_sys;
694 if( ! pkt->left || pkt->i_skip >= pkt->left )
697 bool b_packet_keyframe = pkt->p_peek[pkt->i_skip] >> 7;
698 uint8_t i_stream_number = pkt->p_peek[pkt->i_skip++] & 0x7f;
700 uint32_t i_media_object_number = 0;
701 if (GetValue2b(&i_media_object_number, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->property >> 4) < 0)
703 uint32_t i_media_object_offset = 0;
704 if (GetValue2b(&i_media_object_offset, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->property >> 2) < 0)
706 uint32_t i_replicated_data_length = 0;
707 if (GetValue2b(&i_replicated_data_length, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->property) < 0)
711 uint8_t i_pts_delta = 0;
712 uint32_t i_payload_data_length = 0;
713 uint32_t i_temp_payload_length = 0;
714 bool b_preroll_done = false;
715 p_sys->p_fp->i_preroll = __MIN( p_sys->p_fp->i_preroll, INT64_MAX );
718 if( i_replicated_data_length > 7 ) // should be at least 8 bytes
720 /* Followed by 2 optional DWORDS, offset in media and presentation time */
721 i_base_pts = (mtime_t)GetDWLE( pkt->p_peek + pkt->i_skip + 4 );
723 /* Parsing extensions, See 7.3.1 */
724 ParsePayloadExtensions( p_demux, p_sys->track[i_stream_number], pkt,
725 i_replicated_data_length, &b_packet_keyframe );
727 b_preroll_done = ( i_base_pts > (int64_t)p_sys->p_fp->i_preroll );
728 i_base_pts -= p_sys->p_fp->i_preroll;
729 pkt->i_skip += i_replicated_data_length;
731 if( ! pkt->left || pkt->i_skip >= pkt->left )
734 else if ( i_replicated_data_length == 0 )
736 /* optional DWORDS missing */
737 i_base_pts = (mtime_t)pkt->send_time;
738 b_preroll_done = ( i_base_pts > (int64_t)p_sys->p_fp->i_preroll );
740 /* Compressed payload */
741 else if( i_replicated_data_length == 1 )
743 /* i_media_object_offset is presentation time */
744 /* Next byte is Presentation Time Delta */
745 i_pts_delta = pkt->p_peek[pkt->i_skip];
746 i_base_pts = (mtime_t)i_media_object_offset;
747 b_preroll_done = ( i_base_pts > (int64_t)p_sys->p_fp->i_preroll );
748 i_base_pts -= p_sys->p_fp->i_preroll;
750 i_media_object_offset = 0;
754 /* >1 && <8 Invalid replicated length ! */
755 msg_Warn( p_demux, "Invalid replicated data length detected." );
756 i_payload_data_length = pkt->length - pkt->padding_length - pkt->i_skip;
760 if (i_base_pts < 0) i_base_pts = 0; // FIXME?
763 if( pkt->multiple ) {
764 if (GetValue2b(&i_temp_payload_length, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->length_type) < 0)
767 i_temp_payload_length = pkt->length - pkt->padding_length - pkt->i_skip;
769 i_payload_data_length = i_temp_payload_length;
773 "payload(%d) stream_number:%"PRIu8" media_object_number:%d media_object_offset:%"PRIu32" replicated_data_length:%"PRIu32" payload_data_length %"PRIu32,
774 i_payload + 1, i_stream_number, i_media_object_number,
775 i_media_object_offset, i_replicated_data_length, i_payload_data_length );
777 " pts=%"PRId64" st=%"PRIu32, i_base_pts, pkt->send_time );
780 if( ! i_payload_data_length || i_payload_data_length > pkt->left )
782 msg_Dbg( p_demux, " payload length problem %d %"PRIu32" %"PRIu32, pkt->multiple, i_payload_data_length, pkt->left );
786 asf_track_t *tk = p_sys->track[i_stream_number];
789 msg_Warn( p_demux, "undeclared stream[Id 0x%x]", i_stream_number );
793 if( p_sys->i_wait_keyframe )
795 if ( i_stream_number == p_sys->i_seek_track )
797 if ( !b_packet_keyframe )
799 p_sys->i_wait_keyframe--;
803 p_sys->i_wait_keyframe = 0;
812 if ( b_preroll_done )
814 tk->i_time = INT64_C(1000) * pkt->send_time;
815 tk->i_time -= p_sys->p_fp->i_preroll * 1000;
816 tk->i_time -= tk->p_sp->i_time_offset * 10;
819 uint32_t i_subpayload_count = 0;
820 while (i_payload_data_length)
822 uint32_t i_sub_payload_data_length = i_payload_data_length;
823 if( i_replicated_data_length == 1 )
825 i_sub_payload_data_length = pkt->p_peek[pkt->i_skip++];
826 i_payload_data_length--;
829 SkipBytes( p_demux->s, pkt->i_skip );
831 mtime_t i_payload_pts = i_base_pts + (mtime_t)i_pts_delta * i_subpayload_count * 1000;
832 i_payload_pts -= tk->p_sp->i_time_offset * 10;
833 mtime_t i_payload_dts = INT64_C(1000) * pkt->send_time;
834 i_payload_dts -= p_sys->p_fp->i_preroll * 1000;
835 i_payload_dts -= tk->p_sp->i_time_offset * 10;
837 if ( i_sub_payload_data_length &&
838 DemuxSubPayload(p_demux, tk, i_sub_payload_data_length,
839 i_payload_pts, i_payload_dts, i_media_object_offset,
840 b_packet_keyframe ) < 0)
843 if ( pkt->left > pkt->i_skip + i_sub_payload_data_length )
844 pkt->left -= pkt->i_skip + i_sub_payload_data_length;
850 int i_return = stream_Peek( p_demux->s, &pkt->p_peek, __MIN(pkt->left, INT_MAX) );
851 if ( i_return <= 0 || (unsigned int) i_return < __MIN(pkt->left, INT_MAX) )
853 msg_Warn( p_demux, "cannot peek, EOF ?" );
858 if ( i_sub_payload_data_length <= i_payload_data_length )
859 i_payload_data_length -= i_sub_payload_data_length;
861 i_payload_data_length = 0;
863 i_subpayload_count++;
869 pkt->i_skip += i_payload_data_length;
873 static int DemuxPacket( demux_t *p_demux )
875 demux_sys_t *p_sys = p_demux->p_sys;
877 uint32_t i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
879 const uint8_t *p_peek;
880 int i_return = stream_Peek( p_demux->s, &p_peek,i_data_packet_min );
881 if( i_return <= 0 || ((unsigned int) i_return) < i_data_packet_min )
883 msg_Warn( p_demux, "cannot peek while getting new packet, EOF ?" );
886 unsigned int i_skip = 0;
888 /* *** parse error correction if present *** */
891 unsigned int i_error_correction_data_length = p_peek[0] & 0x0f;
892 unsigned int i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01;
893 unsigned int i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03;
894 i_skip += 1; // skip error correction flags
896 if( i_error_correction_length_type != 0x00 ||
897 i_opaque_data_present != 0 ||
898 i_error_correction_data_length != 0x02 )
900 goto loop_error_recovery;
903 i_skip += i_error_correction_data_length;
906 msg_Warn( p_demux, "no error correction" );
909 if( i_skip + 2 >= i_data_packet_min )
910 goto loop_error_recovery;
912 struct asf_packet_t pkt;
913 int i_packet_flags = p_peek[i_skip]; i_skip++;
914 pkt.property = p_peek[i_skip]; i_skip++;
915 pkt.multiple = !!(i_packet_flags&0x01);
917 pkt.length = i_data_packet_min;
918 pkt.padding_length = 0;
920 if (GetValue2b(&pkt.length, p_peek, &i_skip, i_data_packet_min - i_skip, i_packet_flags >> 5) < 0)
921 goto loop_error_recovery;
922 uint32_t i_packet_sequence;
923 if (GetValue2b(&i_packet_sequence, p_peek, &i_skip, i_data_packet_min - i_skip, i_packet_flags >> 1) < 0)
924 goto loop_error_recovery;
925 if (GetValue2b(&pkt.padding_length, p_peek, &i_skip, i_data_packet_min - i_skip, i_packet_flags >> 3) < 0)
926 goto loop_error_recovery;
928 if( pkt.padding_length > pkt.length )
930 msg_Warn( p_demux, "Too large padding: %"PRIu32, pkt.padding_length );
931 goto loop_error_recovery;
934 if( pkt.length < i_data_packet_min )
936 /* if packet length too short, there is extra padding */
937 pkt.padding_length += i_data_packet_min - pkt.length;
938 pkt.length = i_data_packet_min;
941 pkt.send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
942 /* uint16_t i_packet_duration = GetWLE( p_peek + i_skip ); */ i_skip += 2;
944 i_return = stream_Peek( p_demux->s, &p_peek, pkt.length );
945 if( i_return <= 0 || pkt.length == 0 || (unsigned int)i_return < pkt.length )
947 msg_Warn( p_demux, "cannot peek, EOF ?" );
951 int i_payload_count = 1;
952 pkt.length_type = 0x02; //unused
955 i_payload_count = p_peek[i_skip] & 0x3f;
956 pkt.length_type = ( p_peek[i_skip] >> 6 )&0x03;
961 msg_Dbg(p_demux, "%d payloads", i_payload_count);
966 pkt.left = pkt.length;
968 for( int i_payload = 0; i_payload < i_payload_count ; i_payload++ )
969 if (DemuxPayload(p_demux, &pkt, i_payload) < 0)
971 msg_Warn( p_demux, "payload err %d / %d", i_payload + 1, i_payload_count );
978 if( pkt.left > pkt.padding_length )
979 msg_Warn( p_demux, "Didn't read %"PRIu32" bytes in the packet",
980 pkt.left - pkt.padding_length );
981 else if( pkt.left < pkt.padding_length )
982 msg_Warn( p_demux, "Read %"PRIu32" too much bytes in the packet",
983 pkt.padding_length - pkt.left );
985 int i_return = stream_Read( p_demux->s, NULL, pkt.left );
986 if( i_return < 0 || (unsigned int) i_return < pkt.left )
988 msg_Err( p_demux, "cannot skip data, EOF ?" );
996 msg_Warn( p_demux, "unsupported packet header" );
997 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
999 msg_Err( p_demux, "unsupported packet header, fatal error" );
1002 i_return = stream_Read( p_demux->s, NULL, i_data_packet_min );
1003 if( i_return <= 0 || (unsigned int) i_return != i_data_packet_min )
1005 msg_Warn( p_demux, "cannot skip data, EOF ?" );
1012 /*****************************************************************************
1014 *****************************************************************************/
1015 typedef struct asf_es_priorities_t
1017 uint16_t *pi_stream_numbers;
1019 } asf_es_priorities_t;
1021 /* Fills up our exclusion list */
1022 static void ASF_fillup_es_priorities_ex( demux_sys_t *p_sys, void *p_hdr,
1023 asf_es_priorities_t *p_prios )
1025 /* Find stream exclusions */
1026 asf_object_advanced_mutual_exclusion_t *p_mutex =
1027 ASF_FindObject( p_hdr, &asf_object_advanced_mutual_exclusion, 0 );
1028 if (! p_mutex ) return;
1030 #if ( UINT_MAX > SIZE_MAX / 2 )
1031 if ( p_sys->i_track > (size_t)SIZE_MAX / sizeof(uint16_t) )
1034 p_prios->pi_stream_numbers = malloc( (size_t)p_sys->i_track * sizeof(uint16_t) );
1035 if ( !p_prios->pi_stream_numbers ) return;
1037 if ( p_mutex->i_stream_number_count )
1039 /* Just set highest prio on highest in the group */
1040 for ( uint16_t i = 1; i < p_mutex->i_stream_number_count; i++ )
1042 if ( p_prios->i_count > p_sys->i_track ) break;
1043 p_prios->pi_stream_numbers[ p_prios->i_count++ ] = p_mutex->pi_stream_number[ i ];
1048 /* Fills up our bitrate exclusion list */
1049 static void ASF_fillup_es_bitrate_priorities_ex( demux_sys_t *p_sys, void *p_hdr,
1050 asf_es_priorities_t *p_prios )
1052 /* Find bitrate exclusions */
1053 asf_object_bitrate_mutual_exclusion_t *p_bitrate_mutex =
1054 ASF_FindObject( p_hdr, &asf_object_bitrate_mutual_exclusion_guid, 0 );
1055 if (! p_bitrate_mutex ) return;
1057 #if ( UINT_MAX > SIZE_MAX / 2 )
1058 if ( p_sys->i_track > (size_t)SIZE_MAX / sizeof(uint16_t) )
1061 p_prios->pi_stream_numbers = malloc( (size_t)p_sys->i_track * sizeof( uint16_t ) );
1062 if ( !p_prios->pi_stream_numbers ) return;
1064 if ( p_bitrate_mutex->i_stream_number_count )
1066 /* Just remove < highest */
1067 for ( uint16_t i = 1; i < p_bitrate_mutex->i_stream_number_count; i++ )
1069 if ( p_prios->i_count > p_sys->i_track ) break;
1070 p_prios->pi_stream_numbers[ p_prios->i_count++ ] = p_bitrate_mutex->pi_stream_numbers[ i ];
1076 #define GET_CHECKED( target, getter, maxtarget, temp ) \
1078 temp i_temp = getter;\
1079 if ( i_temp > maxtarget ) {\
1080 msg_Warn( p_demux, "rejecting stream %u : " #target " overflow", i_stream );\
1081 es_format_Clean( &fmt );\
1088 static int DemuxInit( demux_t *p_demux )
1090 demux_sys_t *p_sys = p_demux->p_sys;
1094 p_sys->i_length = 0;
1095 p_sys->i_bitrate = 0;
1096 p_sys->p_root = NULL;
1100 p_sys->i_seek_track = 0;
1101 p_sys->i_wait_keyframe = 0;
1102 for( int i = 0; i < MAX_ASF_TRACKS; i++ )
1104 p_sys->track[i] = NULL;
1106 p_sys->i_data_begin = 0;
1107 p_sys->i_data_end = 0;
1110 /* Now load all object ( except raw data ) */
1111 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_canfastseek );
1112 if( !(p_sys->p_root = ASF_ReadObjectRoot(p_demux->s, p_sys->b_canfastseek)) )
1114 msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
1115 return VLC_EGENERIC;
1117 p_sys->p_fp = p_sys->p_root->p_fp;
1119 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
1121 msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
1125 if ( ASF_FindObject( p_sys->p_root->p_hdr,
1126 &asf_object_content_encryption_guid, 0 ) != NULL
1127 || ASF_FindObject( p_sys->p_root->p_hdr,
1128 &asf_object_extended_content_encryption_guid, 0 ) != NULL
1129 || ASF_FindObject( p_sys->p_root->p_hdr,
1130 &asf_object_advanced_content_encryption_guid, 0 ) != NULL )
1132 dialog_Fatal( p_demux, _("Could not demux ASF stream"), "%s",
1133 _("DRM protected streams are not supported.") );
1137 p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
1138 &asf_object_stream_properties_guid );
1139 if( p_sys->i_track == 0 )
1141 msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
1144 msg_Dbg( p_demux, "found %u streams", p_sys->i_track );
1146 /* check if index is available */
1147 asf_object_index_t *p_index = ASF_FindObject( p_sys->p_root,
1148 &asf_object_simple_index_guid, 0 );
1149 const bool b_index = p_index && p_index->i_index_entry_count;
1151 /* Find the extended header if any */
1152 asf_object_t *p_hdr_ext = ASF_FindObject( p_sys->p_root->p_hdr,
1153 &asf_object_header_extension_guid, 0 );
1155 asf_object_language_list_t *p_languages = NULL;
1156 asf_es_priorities_t fmt_priorities_ex = { NULL, 0 };
1157 asf_es_priorities_t fmt_priorities_bitrate_ex = { NULL, 0 };
1161 p_languages = ASF_FindObject( p_hdr_ext, &asf_object_language_list, 0 );
1163 ASF_fillup_es_priorities_ex( p_sys, p_hdr_ext, &fmt_priorities_ex );
1164 ASF_fillup_es_bitrate_priorities_ex( p_sys, p_hdr_ext, &fmt_priorities_bitrate_ex );
1167 for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1170 asf_object_stream_properties_t *p_sp;
1171 asf_object_extended_stream_properties_t *p_esp;
1172 bool b_access_selected;
1174 p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
1175 &asf_object_stream_properties_guid,
1179 tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
1180 memset( tk, 0, sizeof( asf_track_t ) );
1188 if ( strncmp( p_demux->psz_access, "mms", 3 ) )
1190 /* Check (not mms) if this track is selected (ie will receive data) */
1191 if( !stream_Control( p_demux->s, STREAM_GET_PRIVATE_ID_STATE,
1192 (int) p_sp->i_stream_number, &b_access_selected ) &&
1193 !b_access_selected )
1195 tk->i_cat = UNKNOWN_ES;
1196 msg_Dbg( p_demux, "ignoring not selected stream(ID:%u) (by access)",
1197 p_sp->i_stream_number );
1202 /* Find the associated extended_stream_properties if any */
1205 int i_ext_stream = ASF_CountObject( p_hdr_ext,
1206 &asf_object_extended_stream_properties_guid );
1207 for( int i = 0; i < i_ext_stream; i++ )
1209 asf_object_t *p_tmp =
1210 ASF_FindObject( p_hdr_ext,
1211 &asf_object_extended_stream_properties_guid, i );
1212 if( p_tmp->ext_stream.i_stream_number == p_sp->i_stream_number )
1214 p_esp = &p_tmp->ext_stream;
1223 if( guidcmp( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
1224 p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
1226 uint8_t *p_data = p_sp->p_type_specific_data;
1229 es_format_Init( &fmt, AUDIO_ES, 0 );
1230 i_format = GetWLE( &p_data[0] );
1231 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
1233 GET_CHECKED( fmt.audio.i_channels, GetWLE( &p_data[2] ),
1235 GET_CHECKED( fmt.audio.i_rate, GetDWLE( &p_data[4] ),
1236 UINT_MAX, uint32_t );
1237 GET_CHECKED( fmt.i_bitrate, GetDWLE( &p_data[8] ) * 8,
1238 UINT_MAX, uint32_t );
1239 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
1240 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
1242 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
1243 i_format != WAVE_FORMAT_MPEGLAYER3 &&
1244 i_format != WAVE_FORMAT_MPEG )
1246 GET_CHECKED( fmt.i_extra, __MIN( GetWLE( &p_data[16] ),
1247 p_sp->i_type_specific_data_length -
1248 sizeof( WAVEFORMATEX ) ),
1249 INT_MAX, uint32_t );
1250 fmt.p_extra = malloc( fmt.i_extra );
1251 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
1255 msg_Dbg( p_demux, "added new audio stream(codec:0x%x,ID:%d)",
1256 GetWLE( p_data ), p_sp->i_stream_number );
1258 else if( guidcmp( &p_sp->i_stream_type,
1259 &asf_object_stream_type_video ) &&
1260 p_sp->i_type_specific_data_length >= 11 +
1261 sizeof( VLC_BITMAPINFOHEADER ) )
1263 uint8_t *p_data = &p_sp->p_type_specific_data[11];
1265 es_format_Init( &fmt, VIDEO_ES,
1266 VLC_FOURCC( p_data[16], p_data[17],
1267 p_data[18], p_data[19] ) );
1269 GET_CHECKED( fmt.video.i_width, GetDWLE( p_data + 4 ),
1270 UINT_MAX, uint32_t );
1271 GET_CHECKED( fmt.video.i_height, GetDWLE( p_data + 8 ),
1272 UINT_MAX, uint32_t );
1274 if( p_esp && p_esp->i_average_time_per_frame > 0 )
1276 fmt.video.i_frame_rate = 10000000;
1277 GET_CHECKED( fmt.video.i_frame_rate_base,
1278 p_esp->i_average_time_per_frame,
1279 UINT_MAX, uint64_t );
1282 if( fmt.i_codec == VLC_FOURCC( 'D','V','R',' ') )
1284 /* DVR-MS special ASF */
1285 fmt.i_codec = VLC_FOURCC( 'm','p','g','2' ) ;
1286 fmt.b_packetized = false;
1289 if( p_sp->i_type_specific_data_length > 11 +
1290 sizeof( VLC_BITMAPINFOHEADER ) )
1292 GET_CHECKED( fmt.i_extra, __MIN( GetDWLE( p_data ),
1293 p_sp->i_type_specific_data_length - 11 -
1294 sizeof( VLC_BITMAPINFOHEADER ) ),
1295 UINT_MAX, uint32_t );
1296 fmt.p_extra = malloc( fmt.i_extra );
1297 memcpy( fmt.p_extra, &p_data[sizeof( VLC_BITMAPINFOHEADER )],
1301 /* Look for an aspect ratio */
1302 if( p_sys->p_root->p_metadata )
1304 asf_object_metadata_t *p_meta = p_sys->p_root->p_metadata;
1305 unsigned int i_aspect_x = 0, i_aspect_y = 0;
1307 for( i = 0; i < p_meta->i_record_entries_count; i++ )
1309 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioX" ) )
1311 if( (!i_aspect_x && !p_meta->record[i].i_stream) ||
1312 p_meta->record[i].i_stream ==
1313 p_sp->i_stream_number )
1314 GET_CHECKED( i_aspect_x, p_meta->record[i].i_val,
1315 UINT_MAX, uint64_t );
1317 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioY" ) )
1319 if( (!i_aspect_y && !p_meta->record[i].i_stream) ||
1320 p_meta->record[i].i_stream ==
1321 p_sp->i_stream_number )
1322 GET_CHECKED( i_aspect_y, p_meta->record[i].i_val,
1323 UINT_MAX, uint64_t );
1327 if( i_aspect_x && i_aspect_y )
1329 fmt.video.i_sar_num = i_aspect_x;
1330 fmt.video.i_sar_den = i_aspect_y;
1334 /* If there is a video track then use the index for seeking */
1335 p_sys->b_index = b_index;
1337 msg_Dbg( p_demux, "added new video stream(ID:%d)",
1338 p_sp->i_stream_number );
1340 else if( guidcmp( &p_sp->i_stream_type, &asf_object_extended_stream_header ) &&
1341 p_sp->i_type_specific_data_length >= 64 )
1343 /* Now follows a 64 byte header of which we don't know much */
1344 guid_t *p_ref = (guid_t *)p_sp->p_type_specific_data;
1345 uint8_t *p_data = p_sp->p_type_specific_data + 64;
1346 unsigned int i_data = p_sp->i_type_specific_data_length - 64;
1348 msg_Dbg( p_demux, "Ext stream header detected. datasize = %d", p_sp->i_type_specific_data_length );
1349 if( guidcmp( p_ref, &asf_object_extended_stream_type_audio ) &&
1350 i_data >= sizeof( WAVEFORMATEX ) - 2)
1353 es_format_Init( &fmt, AUDIO_ES, 0 );
1354 i_format = GetWLE( &p_data[0] );
1356 fmt.i_codec = VLC_CODEC_A52;
1358 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
1359 GET_CHECKED( fmt.audio.i_channels, GetWLE( &p_data[2] ),
1361 GET_CHECKED( fmt.audio.i_rate, GetDWLE( &p_data[4] ),
1362 UINT_MAX, uint32_t );
1363 GET_CHECKED( fmt.i_bitrate, GetDWLE( &p_data[8] ) * 8,
1364 UINT_MAX, uint32_t );
1365 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
1366 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
1367 fmt.b_packetized = true;
1369 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
1370 i_format != WAVE_FORMAT_MPEGLAYER3 &&
1371 i_format != WAVE_FORMAT_MPEG )
1373 GET_CHECKED( fmt.i_extra, __MIN( GetWLE( &p_data[16] ),
1374 p_sp->i_type_specific_data_length -
1375 sizeof( WAVEFORMATEX ) ),
1376 INT_MAX, uint32_t );
1377 fmt.p_extra = malloc( fmt.i_extra );
1378 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
1382 msg_Dbg( p_demux, "added new audio stream (codec:0x%x,ID:%d)",
1383 i_format, p_sp->i_stream_number );
1387 es_format_Init( &fmt, UNKNOWN_ES, 0 );
1392 es_format_Init( &fmt, UNKNOWN_ES, 0 );
1395 tk->i_cat = fmt.i_cat;
1396 if( fmt.i_cat != UNKNOWN_ES )
1398 if( p_esp && p_languages &&
1399 p_esp->i_language_index < p_languages->i_language )
1401 fmt.psz_language = strdup( p_languages->ppsz_language[p_esp->i_language_index] );
1403 if( fmt.psz_language && (p = strchr( fmt.psz_language, '-' )) )
1407 /* Set our priority so we won't get multiple videos */
1408 int i_priority = ES_PRIORITY_SELECTABLE_MIN;
1409 for( uint16_t i = 0; i < fmt_priorities_ex.i_count; i++ )
1411 if ( fmt_priorities_ex.pi_stream_numbers[i] == p_sp->i_stream_number )
1413 i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
1417 for( uint16_t i = 0; i < fmt_priorities_bitrate_ex.i_count; i++ )
1419 if ( fmt_priorities_bitrate_ex.pi_stream_numbers[i] == p_sp->i_stream_number )
1421 i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
1425 fmt.i_priority = i_priority;
1427 if ( i_stream <= INT_MAX )
1428 fmt.i_id = i_stream;
1430 msg_Warn( p_demux, "Can't set fmt.i_id to match stream id %u", i_stream );
1432 if ( fmt.i_cat == VIDEO_ES )
1434 /* Backup our video format */
1435 tk->p_fmt = malloc( sizeof( es_format_t ) );
1437 es_format_Copy( tk->p_fmt, &fmt );
1440 tk->p_es = es_out_Add( p_demux->out, &fmt );
1444 msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
1445 p_sp->i_stream_number );
1448 es_format_Clean( &fmt );
1451 free( fmt_priorities_ex.pi_stream_numbers );
1452 free( fmt_priorities_bitrate_ex.pi_stream_numbers );
1454 p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
1455 if( p_sys->p_root->p_data->i_object_size != 0 )
1457 p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
1458 p_sys->p_root->p_data->i_object_size;
1459 p_sys->i_data_end = __MIN( (uint64_t)stream_Size( p_demux->s ), p_sys->i_data_end );
1462 { /* live/broacast */
1463 p_sys->i_data_end = 0;
1466 /* go to first packet */
1467 stream_Seek( p_demux->s, p_sys->i_data_begin );
1469 /* try to calculate movie time */
1470 if( p_sys->p_fp->i_data_packets_count > 0 )
1473 uint64_t i_size = stream_Size( p_demux->s );
1475 if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
1477 i_size = p_sys->i_data_end;
1480 /* real number of packets */
1481 i_count = ( i_size - p_sys->i_data_begin ) /
1482 p_sys->p_fp->i_min_data_packet_size;
1484 /* calculate the time duration in micro-s */
1485 p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
1487 (mtime_t)p_sys->p_fp->i_data_packets_count - p_sys->p_fp->i_preroll * 1000;
1488 if( p_sys->i_length < 0 )
1489 p_sys->i_length = 0;
1491 if( p_sys->i_length > 0 )
1493 p_sys->i_bitrate = 8 * i_size * 1000000 / p_sys->i_length;
1497 /* Create meta information */
1498 p_sys->meta = vlc_meta_New();
1500 asf_object_content_description_t *p_cd;
1501 if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
1502 &asf_object_content_description_guid, 0 ) ) )
1504 if( p_cd->psz_title && *p_cd->psz_title )
1506 vlc_meta_SetTitle( p_sys->meta, p_cd->psz_title );
1508 if( p_cd->psz_artist && *p_cd->psz_artist )
1510 vlc_meta_SetArtist( p_sys->meta, p_cd->psz_artist );
1512 if( p_cd->psz_copyright && *p_cd->psz_copyright )
1514 vlc_meta_SetCopyright( p_sys->meta, p_cd->psz_copyright );
1516 if( p_cd->psz_description && *p_cd->psz_description )
1518 vlc_meta_SetDescription( p_sys->meta, p_cd->psz_description );
1520 if( p_cd->psz_rating && *p_cd->psz_rating )
1522 vlc_meta_SetRating( p_sys->meta, p_cd->psz_rating );
1525 /// \tood Fix Child meta for ASF tracks
1527 for( i_stream = 0, i = 0; i < MAX_ASF_TRACKS; i++ )
1529 asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
1530 &asf_object_codec_list_guid, 0 );
1532 if( p_sys->track[i] )
1534 vlc_meta_t *tk = vlc_meta_New();
1535 TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
1537 if( p_cl && i_stream < p_cl->i_codec_entries_count )
1539 if( p_cl->codec[i_stream].psz_name &&
1540 *p_cl->codec[i_stream].psz_name )
1542 vlc_meta_Add( tk, VLC_META_CODEC_NAME,
1543 p_cl->codec[i_stream].psz_name );
1545 if( p_cl->codec[i_stream].psz_description &&
1546 *p_cl->codec[i_stream].psz_description )
1548 vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
1549 p_cl->codec[i_stream].psz_description );
1559 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1560 return VLC_EGENERIC;
1563 /*****************************************************************************
1564 * FlushRemainingPackets: flushes tail packets
1565 *****************************************************************************/
1567 static void FlushRemainingPackets( demux_t *p_demux )
1569 demux_sys_t *p_sys = p_demux->p_sys;
1570 for ( unsigned int i = 0; i < MAX_ASF_TRACKS; i++ )
1572 asf_track_t *tk = p_sys->track[i];
1573 if( tk && tk->p_frame )
1574 SendPacket( p_demux, tk );
1578 /*****************************************************************************
1580 *****************************************************************************/
1581 static void DemuxEnd( demux_t *p_demux )
1583 demux_sys_t *p_sys = p_demux->p_sys;
1587 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1588 p_sys->p_root = NULL;
1592 vlc_meta_Delete( p_sys->meta );
1596 for( int i = 0; i < MAX_ASF_TRACKS; i++ )
1598 asf_track_t *tk = p_sys->track[i];
1603 block_ChainRelease( tk->p_frame );
1607 es_out_Del( p_demux->out, tk->p_es );
1611 es_format_Clean( tk->p_fmt );
1616 p_sys->track[i] = 0;