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>
44 #include "asfpacket.h"
49 * - add support for the newly added object: language, bitrate,
50 * extended stream properties.
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Open ( vlc_object_t * );
57 static void Close ( vlc_object_t * );
60 set_category( CAT_INPUT )
61 set_subcategory( SUBCAT_INPUT_DEMUX )
62 set_description( N_("ASF/WMV demuxer") )
63 set_capability( "demux", 200 )
64 set_callbacks( Open, Close )
65 add_shortcut( "asf", "wmv" )
69 /*****************************************************************************
71 *****************************************************************************/
72 static int Demux ( demux_t * );
73 static int Control( demux_t *, int i_query, va_list args );
74 static void FlushRemainingPackets( demux_t *p_demux );
76 #define MAX_ASF_TRACKS (ASF_MAX_STREAMNUMBER + 1)
77 #define ASF_PREROLL_FROM_CURRENT -1
79 /* callbacks for packet parser */
80 static void Packet_UpdateTime( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number,
82 static asf_track_info_t * Packet_GetTrackInfo( asf_packet_sys_t *p_packetsys,
83 uint8_t i_stream_number );
84 static bool Packet_DoSkip( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number, bool b_packet_keyframe );
85 static void Packet_Send(asf_packet_sys_t *p_packetsys, uint8_t i_stream_number, block_t **pp_frame);
86 static void Packet_SetAR( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number,
87 uint8_t i_ratio_x, uint8_t i_ratio_y );
94 es_format_t *p_fmt; /* format backup for video changes */
97 mtime_t i_time; /* track time*/
99 asf_track_info_t info;
105 mtime_t i_time; /* s */
106 mtime_t i_length; /* length of file file */
107 uint64_t i_bitrate; /* global file bitrate */
109 asf_object_root_t *p_root;
110 asf_object_file_properties_t *p_fp;
112 unsigned int i_track;
113 asf_track_t *track[MAX_ASF_TRACKS]; /* track number is stored on 7 bits */
115 uint64_t i_data_begin;
120 uint8_t i_seek_track;
121 uint8_t i_access_selected_track[ES_CATEGORY_COUNT]; /* mms, depends on access algorithm */
122 unsigned int i_wait_keyframe;
124 mtime_t i_preroll_start;
126 asf_packet_sys_t packet_sys;
131 static mtime_t GetMoviePTS( demux_sys_t * );
132 static int DemuxInit( demux_t * );
133 static void DemuxEnd( demux_t * );
135 /*****************************************************************************
136 * Open: check file and initializes ASF structures
137 *****************************************************************************/
138 static int Open( vlc_object_t * p_this )
140 demux_t *p_demux = (demux_t *)p_this;
143 const uint8_t *p_peek;
145 /* A little test to see if it could be a asf stream */
146 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 ) return VLC_EGENERIC;
148 ASF_GetGUID( &guid, p_peek );
149 if( !guidcmp( &guid, &asf_object_header_guid ) ) return VLC_EGENERIC;
151 /* Set p_demux fields */
152 p_demux->pf_demux = Demux;
153 p_demux->pf_control = Control;
154 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
156 /* Load the headers */
157 if( DemuxInit( p_demux ) )
163 p_sys->packet_sys.p_demux = p_demux;
164 p_sys->packet_sys.pf_doskip = Packet_DoSkip;
165 p_sys->packet_sys.pf_send = Packet_Send;
166 p_sys->packet_sys.pf_gettrackinfo = Packet_GetTrackInfo;
167 p_sys->packet_sys.pf_updatetime = Packet_UpdateTime;
168 p_sys->packet_sys.pf_setaspectratio = Packet_SetAR;
173 /*****************************************************************************
174 * Demux: read packet and send them to decoders
175 *****************************************************************************/
176 static int Demux( demux_t *p_demux )
178 demux_sys_t *p_sys = p_demux->p_sys;
180 for( int i=0; i<ES_CATEGORY_COUNT; i++ )
182 if ( p_sys->i_access_selected_track[i] > 0 )
184 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
185 p_sys->track[p_sys->i_access_selected_track[i]]->p_es, true );
186 p_sys->i_access_selected_track[i] = 0;
190 /* Get selected tracks, especially for computing PCR */
191 for( int i=0; i<MAX_ASF_TRACKS; i++ )
193 asf_track_t *tk = p_sys->track[i];
196 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, & tk->b_selected );
198 tk->b_selected = false;
203 const uint8_t *p_peek;
205 mtime_t i_time_begin = GetMoviePTS( p_sys );
208 if( !vlc_object_alive (p_demux) )
211 /* FIXME: returns EOF too early for some mms streams */
212 if( p_sys->i_data_end >= 0 &&
213 stream_Tell( p_demux->s ) >= p_sys->i_data_end )
217 /* Check if we have concatenated files */
218 if( stream_Peek( p_demux->s, &p_peek, 16 ) == 16 )
222 ASF_GetGUID( &guid, p_peek );
223 if( guidcmp( &guid, &asf_object_header_guid ) )
225 msg_Warn( p_demux, "found a new ASF header" );
226 /* We end this stream */
229 /* And we prepare to read the next one */
230 if( DemuxInit( p_demux ) )
232 msg_Err( p_demux, "failed to load the new header" );
233 dialog_Fatal( p_demux, _("Could not demux ASF stream"), "%s",
234 _("VLC failed to load the ASF header.") );
237 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
242 /* Read and demux a packet */
243 if( ( i_result = DemuxASFPacket( &p_sys->packet_sys,
244 p_sys->p_fp->i_min_data_packet_size,
245 p_sys->p_fp->i_max_data_packet_size ) ) <= 0 )
247 FlushRemainingPackets( p_demux );
250 if( i_time_begin == -1 )
252 i_time_begin = GetMoviePTS( p_sys );
256 i_length = GetMoviePTS( p_sys ) - i_time_begin;
257 if( i_length < 0 || i_length >= 40 * 1000 ) break;
262 /* WARN: Don't move it before the end of the whole chunk */
263 p_sys->i_time = GetMoviePTS( p_sys );
264 if( p_sys->i_time >= 0 )
267 msg_Dbg( p_demux, "Demux Loop Setting PCR to %"PRId64, VLC_TS_0 + p_sys->i_time );
269 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_time );
275 /*****************************************************************************
276 * Close: frees unused data
277 *****************************************************************************/
278 static void Close( vlc_object_t * p_this )
280 demux_t *p_demux = (demux_t *)p_this;
284 free( p_demux->p_sys );
287 /*****************************************************************************
288 * WaitKeyframe: computes the number of frames to wait for a keyframe
289 *****************************************************************************/
290 static void WaitKeyframe( demux_t *p_demux )
292 demux_sys_t *p_sys = p_demux->p_sys;
293 if ( ! p_sys->i_seek_track )
295 for ( int i=0; i<MAX_ASF_TRACKS; i++ )
297 asf_track_t *tk = p_sys->track[i];
298 if ( tk && tk->info.p_sp && tk->i_cat == VIDEO_ES && tk->b_selected )
300 p_sys->i_seek_track = tk->info.p_sp->i_stream_number;
306 if ( p_sys->i_seek_track )
308 /* Skip forward at least 1 min */
309 asf_track_t *tk = p_sys->track[p_sys->i_seek_track];
310 if ( tk->info.p_esp && tk->info.p_esp->i_average_time_per_frame )
312 /* 1 min if fastseek, otherwise 5 sec */
313 /* That's a guess for bandwidth */
314 uint64_t i_maxwaittime = ( p_sys->b_canfastseek ) ? 600000000 : 50000000;
315 i_maxwaittime /= tk->info.p_esp->i_average_time_per_frame;
316 p_sys->i_wait_keyframe = __MIN( i_maxwaittime, UINT_MAX );
320 p_sys->i_wait_keyframe = ( p_sys->b_canfastseek ) ? 25 * 30 : 25 * 5;
325 p_sys->i_wait_keyframe = 0;
330 /*****************************************************************************
331 * SeekIndex: goto to i_date or i_percent
332 *****************************************************************************/
333 static int SeekPercent( demux_t *p_demux, int i_query, va_list args )
335 demux_sys_t *p_sys = p_demux->p_sys;
337 WaitKeyframe( p_demux );
339 msg_Dbg( p_demux, "seek with percent: waiting %i frames", p_sys->i_wait_keyframe );
340 return demux_vaControlHelper( p_demux->s, __MIN( INT64_MAX, p_sys->i_data_begin ),
341 __MIN( INT64_MAX, p_sys->i_data_end ),
342 __MIN( INT64_MAX, p_sys->i_bitrate ),
343 __MIN( INT16_MAX, p_sys->p_fp->i_min_data_packet_size ),
347 static int SeekIndex( demux_t *p_demux, mtime_t i_date, float f_pos )
349 demux_sys_t *p_sys = p_demux->p_sys;
350 asf_object_index_t *p_index;
352 msg_Dbg( p_demux, "seek with index: %i seconds, position %f",
353 i_date >= 0 ? (int)(i_date/1000000) : -1, f_pos );
356 i_date = p_sys->i_length * f_pos;
358 p_sys->i_preroll_start = i_date - (int64_t) p_sys->p_fp->i_preroll;
359 if ( p_sys->i_preroll_start < 0 ) p_sys->i_preroll_start = 0;
361 p_index = ASF_FindObject( p_sys->p_root, &asf_object_simple_index_guid, 0 );
363 uint64_t i_entry = p_sys->i_preroll_start * 10 / p_index->i_index_entry_time_interval;
364 if( i_entry >= p_index->i_index_entry_count )
366 msg_Warn( p_demux, "Incomplete index" );
370 WaitKeyframe( p_demux );
372 uint64_t i_offset = (uint64_t)p_index->index_entry[i_entry].i_packet_number *
373 p_sys->p_fp->i_min_data_packet_size;
375 if ( stream_Seek( p_demux->s, i_offset + p_sys->i_data_begin ) == VLC_SUCCESS )
377 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, VLC_TS_0 + i_date );
380 else return VLC_EGENERIC;
383 static void SeekPrepare( demux_t *p_demux )
385 demux_sys_t *p_sys = p_demux->p_sys;
387 p_sys->i_time = VLC_TS_INVALID;
388 p_sys->i_preroll_start = ASFPACKET_PREROLL_FROM_CURRENT;
389 for( int i = 0; i < MAX_ASF_TRACKS ; i++ )
391 asf_track_t *tk = p_sys->track[i];
396 if( tk->info.p_frame )
397 block_ChainRelease( tk->info.p_frame );
398 tk->info.p_frame = NULL;
401 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
404 /*****************************************************************************
406 *****************************************************************************/
407 static int Control( demux_t *p_demux, int i_query, va_list args )
409 demux_sys_t *p_sys = p_demux->p_sys;
417 case DEMUX_GET_LENGTH:
418 pi64 = (int64_t*)va_arg( args, int64_t * );
419 *pi64 = p_sys->i_length;
423 pi64 = (int64_t*)va_arg( args, int64_t * );
424 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
425 *pi64 = p_sys->i_time;
430 ! ( p_sys->p_fp->i_flags & ASF_FILE_PROPERTIES_SEEKABLE ) )
433 SeekPrepare( p_demux );
435 if( p_sys->b_index && p_sys->i_length > 0 )
438 va_copy( acpy, args );
439 i64 = (int64_t)va_arg( acpy, int64_t );
442 if( !SeekIndex( p_demux, i64, -1 ) )
445 return SeekPercent( p_demux, i_query, args );
449 i = (int)va_arg( args, int );
453 i++; /* video/audio-es variable starts 0 */
454 msg_Dbg( p_demux, "Requesting access to enable stream %d", i );
455 i_ret = stream_Control( p_demux->s, STREAM_SET_PRIVATE_ID_STATE, i, true );
458 { /* i contains -1 * es_category */
459 msg_Dbg( p_demux, "Requesting access to disable stream %d", i );
460 i_ret = stream_Control( p_demux->s, STREAM_SET_PRIVATE_ID_STATE, i, false );
463 if ( i_ret == VLC_SUCCESS )
465 SeekPrepare( p_demux );
466 p_sys->i_seek_track = 0;
467 WaitKeyframe( p_demux );
472 case DEMUX_GET_POSITION:
473 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
474 if( p_sys->i_length > 0 )
476 pf = (double*)va_arg( args, double * );
477 *pf = p_sys->i_time / (double)p_sys->i_length;
480 return demux_vaControlHelper( p_demux->s,
481 __MIN( INT64_MAX, p_sys->i_data_begin ),
482 __MIN( INT64_MAX, p_sys->i_data_end ),
483 __MIN( INT64_MAX, p_sys->i_bitrate ),
484 __MIN( INT16_MAX, p_sys->p_fp->i_min_data_packet_size ),
487 case DEMUX_SET_POSITION:
489 ! ( p_sys->p_fp->i_flags & ASF_FILE_PROPERTIES_SEEKABLE ) )
492 SeekPrepare( p_demux );
494 if( p_sys->b_index && p_sys->i_length > 0 )
497 va_copy( acpy, args );
498 f = (double)va_arg( acpy, double );
501 if( !SeekIndex( p_demux, -1, f ) )
504 return SeekPercent( p_demux, i_query, args );
507 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
508 vlc_meta_Merge( p_meta, p_sys->meta );
513 ! ( p_sys->p_fp->i_flags & ASF_FILE_PROPERTIES_SEEKABLE ) )
515 bool *pb_bool = (bool*)va_arg( args, bool * );
522 return demux_vaControlHelper( p_demux->s,
523 __MIN( INT64_MAX, p_sys->i_data_begin ),
524 __MIN( INT64_MAX, p_sys->i_data_end),
525 __MIN( INT64_MAX, p_sys->i_bitrate ),
526 ( p_sys->p_fp ) ? __MIN( INT_MAX, p_sys->p_fp->i_min_data_packet_size ) : 1,
531 /*****************************************************************************
533 *****************************************************************************/
534 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
538 /* As some tracks might have been deselected by access, the PCR might
540 for( i = 0; i < MAX_ASF_TRACKS ; i++ )
542 const asf_track_t *tk = p_sys->track[i];
544 if( tk && tk->p_es && tk->b_selected )
546 /* Skip discrete tracks */
547 if ( tk->i_cat != VIDEO_ES && tk->i_cat != AUDIO_ES )
550 /* We need to have all ES seen once, as they might have lower DTS */
551 if ( tk->i_time + (int64_t)p_sys->p_fp->i_preroll * 1000 < 0 )
556 else if ( tk->i_time > -1 && ( i_time == -1 || i_time > tk->i_time ) )
566 static void Packet_SetAR( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number,
567 uint8_t i_ratio_x, uint8_t i_ratio_y )
569 demux_t *p_demux = p_packetsys->p_demux;
570 asf_track_t *tk = p_demux->p_sys->track[i_stream_number];
571 if ( tk->p_fmt->video.i_sar_num == i_ratio_x && tk->p_fmt->video.i_sar_den == i_ratio_y )
574 /* Only apply if origin pixel size >= 1x1, due to broken yacast */
575 if ( tk->p_fmt->video.i_height * i_ratio_x > tk->p_fmt->video.i_width * i_ratio_y )
577 vout_thread_t *p_vout = input_GetVout( p_demux->p_input );
580 msg_Info( p_demux, "Changing aspect ratio to %i/%i", i_ratio_x, i_ratio_y );
581 vout_ChangeAspectRatio( p_vout, i_ratio_x, i_ratio_y );
582 vlc_object_release( p_vout );
585 tk->p_fmt->video.i_sar_num = i_ratio_x;
586 tk->p_fmt->video.i_sar_den = i_ratio_y;
589 static void Packet_UpdateTime( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number,
592 asf_track_t *tk = p_packetsys->p_demux->p_sys->track[i_stream_number];
597 static asf_track_info_t * Packet_GetTrackInfo( asf_packet_sys_t *p_packetsys,
598 uint8_t i_stream_number )
600 asf_track_t *tk = p_packetsys->p_demux->p_sys->track[i_stream_number];
607 static bool Packet_DoSkip( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number, bool b_packet_keyframe )
609 demux_t *p_demux = p_packetsys->p_demux;
610 demux_sys_t *p_sys = p_demux->p_sys;
611 const asf_track_t *tk = p_sys->track[i_stream_number];
615 msg_Warn( p_demux, "undeclared stream[Id 0x%x]", i_stream_number );
619 if( p_sys->i_wait_keyframe )
621 if ( i_stream_number == p_sys->i_seek_track )
623 if ( !b_packet_keyframe )
625 p_sys->i_wait_keyframe--;
629 p_sys->i_wait_keyframe = 0;
641 static void Packet_Send(asf_packet_sys_t *p_packetsys, uint8_t i_stream_number, block_t **pp_frame)
643 demux_t *p_demux = p_packetsys->p_demux;
644 demux_sys_t *p_sys = p_demux->p_sys;
645 const asf_track_t *tk = p_sys->track[i_stream_number];
649 block_t *p_gather = block_ChainGather( *pp_frame );
651 if( p_sys->i_time < VLC_TS_0 && tk->i_time > VLC_TS_INVALID )
653 p_sys->i_time = tk->i_time;
654 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_time );
656 msg_Dbg( p_demux, " setting PCR to %"PRId64, VLC_TS_0 + p_sys->i_time );
661 msg_Dbg( p_demux, " sending packet dts %"PRId64" pts %"PRId64" pcr %"PRId64, p_gather->i_dts, p_gather->i_pts, p_sys->i_time );
663 es_out_Send( p_demux->out, tk->p_es, p_gather );
667 /*****************************************************************************
669 *****************************************************************************/
670 typedef struct asf_es_priorities_t
672 uint16_t *pi_stream_numbers;
674 } asf_es_priorities_t;
676 /* Fills up our exclusion list */
677 static void ASF_fillup_es_priorities_ex( demux_sys_t *p_sys, void *p_hdr,
678 asf_es_priorities_t *p_prios )
680 /* Find stream exclusions */
681 asf_object_advanced_mutual_exclusion_t *p_mutex =
682 ASF_FindObject( p_hdr, &asf_object_advanced_mutual_exclusion, 0 );
683 if (! p_mutex ) return;
685 #if ( UINT_MAX > SIZE_MAX / 2 )
686 if ( p_sys->i_track > (size_t)SIZE_MAX / sizeof(uint16_t) )
689 p_prios->pi_stream_numbers = malloc( (size_t)p_sys->i_track * sizeof(uint16_t) );
690 if ( !p_prios->pi_stream_numbers ) return;
692 if ( p_mutex->i_stream_number_count )
694 /* Just set highest prio on highest in the group */
695 for ( uint16_t i = 1; i < p_mutex->i_stream_number_count; i++ )
697 if ( p_prios->i_count > p_sys->i_track || i > p_sys->i_track ) break;
698 p_prios->pi_stream_numbers[ p_prios->i_count++ ] = p_mutex->pi_stream_number[ i ];
703 /* Fills up our bitrate exclusion list */
704 static void ASF_fillup_es_bitrate_priorities_ex( demux_sys_t *p_sys, void *p_hdr,
705 asf_es_priorities_t *p_prios )
707 /* Find bitrate exclusions */
708 asf_object_bitrate_mutual_exclusion_t *p_bitrate_mutex =
709 ASF_FindObject( p_hdr, &asf_object_bitrate_mutual_exclusion_guid, 0 );
710 if (! p_bitrate_mutex ) return;
712 #if ( UINT_MAX > SIZE_MAX / 2 )
713 if ( p_sys->i_track > (size_t)SIZE_MAX / sizeof(uint16_t) )
716 p_prios->pi_stream_numbers = malloc( (size_t)p_sys->i_track * sizeof( uint16_t ) );
717 if ( !p_prios->pi_stream_numbers ) return;
719 if ( p_bitrate_mutex->i_stream_number_count )
721 /* Just remove < highest */
722 for ( uint16_t i = 1; i < p_bitrate_mutex->i_stream_number_count; i++ )
724 if ( p_prios->i_count > p_sys->i_track || i > p_sys->i_track ) break;
725 p_prios->pi_stream_numbers[ p_prios->i_count++ ] = p_bitrate_mutex->pi_stream_numbers[ i ];
731 #define GET_CHECKED( target, getter, maxtarget, temp ) \
733 temp i_temp = getter;\
734 if ( i_temp > maxtarget ) {\
735 msg_Warn( p_demux, "rejecting stream %u : " #target " overflow", i_stream );\
736 es_format_Clean( &fmt );\
743 static int DemuxInit( demux_t *p_demux )
745 demux_sys_t *p_sys = p_demux->p_sys;
750 p_sys->i_bitrate = 0;
751 p_sys->p_root = NULL;
755 p_sys->i_seek_track = 0;
756 p_sys->i_wait_keyframe = 0;
757 for( int i = 0; i < MAX_ASF_TRACKS; i++ )
759 p_sys->track[i] = NULL;
761 p_sys->i_data_begin = 0;
762 p_sys->i_data_end = 0;
763 p_sys->i_preroll_start = 0;
766 /* Now load all object ( except raw data ) */
767 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_canfastseek );
768 if( !(p_sys->p_root = ASF_ReadObjectRoot(p_demux->s, p_sys->b_canfastseek)) )
770 msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
773 p_sys->p_fp = p_sys->p_root->p_fp;
775 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
777 msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
781 if ( ASF_FindObject( p_sys->p_root->p_hdr,
782 &asf_object_content_encryption_guid, 0 ) != NULL
783 || ASF_FindObject( p_sys->p_root->p_hdr,
784 &asf_object_extended_content_encryption_guid, 0 ) != NULL
785 || ASF_FindObject( p_sys->p_root->p_hdr,
786 &asf_object_advanced_content_encryption_guid, 0 ) != NULL )
788 dialog_Fatal( p_demux, _("Could not demux ASF stream"), "%s",
789 _("DRM protected streams are not supported.") );
793 p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
794 &asf_object_stream_properties_guid );
795 if( p_sys->i_track == 0 )
797 msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
800 msg_Dbg( p_demux, "found %u streams", p_sys->i_track );
802 /* check if index is available */
803 asf_object_index_t *p_index = ASF_FindObject( p_sys->p_root,
804 &asf_object_simple_index_guid, 0 );
805 const bool b_index = p_index && p_index->i_index_entry_count;
807 /* Find the extended header if any */
808 asf_object_t *p_hdr_ext = ASF_FindObject( p_sys->p_root->p_hdr,
809 &asf_object_header_extension_guid, 0 );
811 asf_object_language_list_t *p_languages = NULL;
812 asf_es_priorities_t fmt_priorities_ex = { NULL, 0 };
813 asf_es_priorities_t fmt_priorities_bitrate_ex = { NULL, 0 };
817 p_languages = ASF_FindObject( p_hdr_ext, &asf_object_language_list, 0 );
819 ASF_fillup_es_priorities_ex( p_sys, p_hdr_ext, &fmt_priorities_ex );
820 ASF_fillup_es_bitrate_priorities_ex( p_sys, p_hdr_ext, &fmt_priorities_bitrate_ex );
823 for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
826 asf_object_stream_properties_t *p_sp;
827 asf_object_extended_stream_properties_t *p_esp;
828 bool b_access_selected;
830 p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
831 &asf_object_stream_properties_guid,
835 tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
836 memset( tk, 0, sizeof( asf_track_t ) );
839 tk->info.p_sp = p_sp;
841 tk->info.p_esp = NULL;
842 tk->info.p_frame = NULL;
844 if ( strncmp( p_demux->psz_access, "mms", 3 ) )
846 /* Check (not mms) if this track is selected (ie will receive data) */
847 if( !stream_Control( p_demux->s, STREAM_GET_PRIVATE_ID_STATE,
848 (int) p_sp->i_stream_number, &b_access_selected ) &&
851 tk->i_cat = UNKNOWN_ES;
852 msg_Dbg( p_demux, "ignoring not selected stream(ID:%u) (by access)",
853 p_sp->i_stream_number );
858 /* Find the associated extended_stream_properties if any */
861 int i_ext_stream = ASF_CountObject( p_hdr_ext,
862 &asf_object_extended_stream_properties_guid );
863 for( int i = 0; i < i_ext_stream; i++ )
865 asf_object_t *p_tmp =
866 ASF_FindObject( p_hdr_ext,
867 &asf_object_extended_stream_properties_guid, i );
868 if( p_tmp->ext_stream.i_stream_number == p_sp->i_stream_number )
870 p_esp = &p_tmp->ext_stream;
871 tk->info.p_esp = p_esp;
879 if( guidcmp( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
880 p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
882 uint8_t *p_data = p_sp->p_type_specific_data;
885 es_format_Init( &fmt, AUDIO_ES, 0 );
886 i_format = GetWLE( &p_data[0] );
887 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
889 GET_CHECKED( fmt.audio.i_channels, GetWLE( &p_data[2] ),
891 GET_CHECKED( fmt.audio.i_rate, GetDWLE( &p_data[4] ),
892 UINT_MAX, uint32_t );
893 GET_CHECKED( fmt.i_bitrate, GetDWLE( &p_data[8] ) * 8,
894 UINT_MAX, uint32_t );
895 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
896 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
898 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
899 i_format != WAVE_FORMAT_MPEGLAYER3 &&
900 i_format != WAVE_FORMAT_MPEG )
902 GET_CHECKED( fmt.i_extra, __MIN( GetWLE( &p_data[16] ),
903 p_sp->i_type_specific_data_length -
904 sizeof( WAVEFORMATEX ) ),
906 fmt.p_extra = malloc( fmt.i_extra );
907 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
911 msg_Dbg( p_demux, "added new audio stream(codec:0x%x,ID:%d)",
912 GetWLE( p_data ), p_sp->i_stream_number );
914 else if( guidcmp( &p_sp->i_stream_type,
915 &asf_object_stream_type_video ) &&
916 p_sp->i_type_specific_data_length >= 11 +
917 sizeof( VLC_BITMAPINFOHEADER ) )
919 uint8_t *p_data = &p_sp->p_type_specific_data[11];
921 es_format_Init( &fmt, VIDEO_ES,
922 VLC_FOURCC( p_data[16], p_data[17],
923 p_data[18], p_data[19] ) );
925 GET_CHECKED( fmt.video.i_width, GetDWLE( p_data + 4 ),
926 UINT_MAX, uint32_t );
927 GET_CHECKED( fmt.video.i_height, GetDWLE( p_data + 8 ),
928 UINT_MAX, uint32_t );
930 if( p_esp && p_esp->i_average_time_per_frame > 0 )
932 fmt.video.i_frame_rate = 10000000;
933 GET_CHECKED( fmt.video.i_frame_rate_base,
934 p_esp->i_average_time_per_frame,
935 UINT_MAX, uint64_t );
938 if( fmt.i_codec == VLC_FOURCC( 'D','V','R',' ') )
940 /* DVR-MS special ASF */
941 fmt.i_codec = VLC_FOURCC( 'm','p','g','2' ) ;
942 fmt.b_packetized = false;
945 if( p_sp->i_type_specific_data_length > 11 +
946 sizeof( VLC_BITMAPINFOHEADER ) )
948 GET_CHECKED( fmt.i_extra, __MIN( GetDWLE( p_data ),
949 p_sp->i_type_specific_data_length - 11 -
950 sizeof( VLC_BITMAPINFOHEADER ) ),
951 UINT_MAX, uint32_t );
952 fmt.p_extra = malloc( fmt.i_extra );
953 memcpy( fmt.p_extra, &p_data[sizeof( VLC_BITMAPINFOHEADER )],
957 /* Look for an aspect ratio */
958 if( p_sys->p_root->p_metadata )
960 asf_object_metadata_t *p_meta = p_sys->p_root->p_metadata;
961 unsigned int i_aspect_x = 0, i_aspect_y = 0;
963 for( i = 0; i < p_meta->i_record_entries_count; i++ )
965 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioX" ) )
967 if( (!i_aspect_x && !p_meta->record[i].i_stream) ||
968 p_meta->record[i].i_stream ==
969 p_sp->i_stream_number )
970 GET_CHECKED( i_aspect_x, p_meta->record[i].i_val,
971 UINT_MAX, uint64_t );
973 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioY" ) )
975 if( (!i_aspect_y && !p_meta->record[i].i_stream) ||
976 p_meta->record[i].i_stream ==
977 p_sp->i_stream_number )
978 GET_CHECKED( i_aspect_y, p_meta->record[i].i_val,
979 UINT_MAX, uint64_t );
983 if( i_aspect_x && i_aspect_y )
985 fmt.video.i_sar_num = i_aspect_x;
986 fmt.video.i_sar_den = i_aspect_y;
990 /* If there is a video track then use the index for seeking */
991 p_sys->b_index = b_index;
993 msg_Dbg( p_demux, "added new video stream(ID:%d)",
994 p_sp->i_stream_number );
996 else if( guidcmp( &p_sp->i_stream_type, &asf_object_extended_stream_header ) &&
997 p_sp->i_type_specific_data_length >= 64 )
999 /* Now follows a 64 byte header of which we don't know much */
1000 guid_t *p_ref = (guid_t *)p_sp->p_type_specific_data;
1001 uint8_t *p_data = p_sp->p_type_specific_data + 64;
1002 unsigned int i_data = p_sp->i_type_specific_data_length - 64;
1004 msg_Dbg( p_demux, "Ext stream header detected. datasize = %d", p_sp->i_type_specific_data_length );
1005 if( guidcmp( p_ref, &asf_object_extended_stream_type_audio ) &&
1006 i_data >= sizeof( WAVEFORMATEX ) - 2)
1009 es_format_Init( &fmt, AUDIO_ES, 0 );
1010 i_format = GetWLE( &p_data[0] );
1012 fmt.i_codec = VLC_CODEC_A52;
1014 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
1015 GET_CHECKED( fmt.audio.i_channels, GetWLE( &p_data[2] ),
1017 GET_CHECKED( fmt.audio.i_rate, GetDWLE( &p_data[4] ),
1018 UINT_MAX, uint32_t );
1019 GET_CHECKED( fmt.i_bitrate, GetDWLE( &p_data[8] ) * 8,
1020 UINT_MAX, uint32_t );
1021 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
1022 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
1023 fmt.b_packetized = true;
1025 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
1026 i_format != WAVE_FORMAT_MPEGLAYER3 &&
1027 i_format != WAVE_FORMAT_MPEG && i_data >= 19 )
1029 GET_CHECKED( fmt.i_extra, __MIN( GetWLE( &p_data[16] ),
1030 p_sp->i_type_specific_data_length -
1031 sizeof( WAVEFORMATEX ) ),
1032 INT_MAX, uint32_t );
1033 fmt.p_extra = malloc( fmt.i_extra );
1034 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
1038 msg_Dbg( p_demux, "added new audio stream (codec:0x%x,ID:%d)",
1039 i_format, p_sp->i_stream_number );
1043 es_format_Init( &fmt, UNKNOWN_ES, 0 );
1048 es_format_Init( &fmt, UNKNOWN_ES, 0 );
1051 tk->i_cat = fmt.i_cat;
1052 if( fmt.i_cat != UNKNOWN_ES )
1054 if( p_esp && p_languages &&
1055 p_esp->i_language_index < p_languages->i_language )
1057 fmt.psz_language = strdup( p_languages->ppsz_language[p_esp->i_language_index] );
1059 if( fmt.psz_language && (p = strchr( fmt.psz_language, '-' )) )
1063 /* Set our priority so we won't get multiple videos */
1064 int i_priority = ES_PRIORITY_SELECTABLE_MIN;
1065 for( uint16_t i = 0; i < fmt_priorities_ex.i_count; i++ )
1067 if ( fmt_priorities_ex.pi_stream_numbers[i] == p_sp->i_stream_number )
1069 i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
1073 for( uint16_t i = 0; i < fmt_priorities_bitrate_ex.i_count; i++ )
1075 if ( fmt_priorities_bitrate_ex.pi_stream_numbers[i] == p_sp->i_stream_number )
1077 i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
1081 fmt.i_priority = i_priority;
1083 if ( i_stream <= INT_MAX )
1084 fmt.i_id = i_stream;
1086 msg_Warn( p_demux, "Can't set fmt.i_id to match stream id %u", i_stream );
1088 if ( fmt.i_cat == VIDEO_ES )
1090 /* Backup our video format */
1091 tk->p_fmt = malloc( sizeof( es_format_t ) );
1093 es_format_Copy( tk->p_fmt, &fmt );
1096 tk->p_es = es_out_Add( p_demux->out, &fmt );
1098 if( !stream_Control( p_demux->s, STREAM_GET_PRIVATE_ID_STATE,
1099 (int) p_sp->i_stream_number, &b_access_selected ) &&
1102 p_sys->i_access_selected_track[fmt.i_cat] = p_sp->i_stream_number;
1108 msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
1109 p_sp->i_stream_number );
1112 es_format_Clean( &fmt );
1115 free( fmt_priorities_ex.pi_stream_numbers );
1116 free( fmt_priorities_bitrate_ex.pi_stream_numbers );
1118 p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
1119 if( p_sys->p_root->p_data->i_object_size != 0 )
1121 p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
1122 p_sys->p_root->p_data->i_object_size;
1123 p_sys->i_data_end = __MIN( (uint64_t)stream_Size( p_demux->s ), p_sys->i_data_end );
1126 { /* live/broacast */
1127 p_sys->i_data_end = 0;
1130 /* go to first packet */
1131 stream_Seek( p_demux->s, p_sys->i_data_begin );
1133 /* try to calculate movie time */
1134 if( p_sys->p_fp->i_data_packets_count > 0 )
1137 uint64_t i_size = stream_Size( p_demux->s );
1139 if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
1141 i_size = p_sys->i_data_end;
1144 /* real number of packets */
1145 i_count = ( i_size - p_sys->i_data_begin ) /
1146 p_sys->p_fp->i_min_data_packet_size;
1148 /* calculate the time duration in micro-s */
1149 p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
1151 (mtime_t)p_sys->p_fp->i_data_packets_count - p_sys->p_fp->i_preroll * 1000;
1152 if( p_sys->i_length < 0 )
1153 p_sys->i_length = 0;
1155 if( p_sys->i_length > 0 )
1157 p_sys->i_bitrate = 8 * i_size * 1000000 / p_sys->i_length;
1161 /* Create meta information */
1162 p_sys->meta = vlc_meta_New();
1164 asf_object_content_description_t *p_cd;
1165 if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
1166 &asf_object_content_description_guid, 0 ) ) )
1168 if( p_cd->psz_title && *p_cd->psz_title )
1170 vlc_meta_SetTitle( p_sys->meta, p_cd->psz_title );
1172 if( p_cd->psz_artist && *p_cd->psz_artist )
1174 vlc_meta_SetArtist( p_sys->meta, p_cd->psz_artist );
1176 if( p_cd->psz_copyright && *p_cd->psz_copyright )
1178 vlc_meta_SetCopyright( p_sys->meta, p_cd->psz_copyright );
1180 if( p_cd->psz_description && *p_cd->psz_description )
1182 vlc_meta_SetDescription( p_sys->meta, p_cd->psz_description );
1184 if( p_cd->psz_rating && *p_cd->psz_rating )
1186 vlc_meta_SetRating( p_sys->meta, p_cd->psz_rating );
1189 /// \tood Fix Child meta for ASF tracks
1191 for( i_stream = 0, i = 0; i < MAX_ASF_TRACKS; i++ )
1193 asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
1194 &asf_object_codec_list_guid, 0 );
1196 if( p_sys->track[i] )
1198 vlc_meta_t *tk = vlc_meta_New();
1199 TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
1201 if( p_cl && i_stream < p_cl->i_codec_entries_count )
1203 if( p_cl->codec[i_stream].psz_name &&
1204 *p_cl->codec[i_stream].psz_name )
1206 vlc_meta_Add( tk, VLC_META_CODEC_NAME,
1207 p_cl->codec[i_stream].psz_name );
1209 if( p_cl->codec[i_stream].psz_description &&
1210 *p_cl->codec[i_stream].psz_description )
1212 vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
1213 p_cl->codec[i_stream].psz_description );
1221 p_sys->packet_sys.pi_preroll = &p_sys->p_fp->i_preroll;
1222 p_sys->packet_sys.pi_preroll_start = &p_sys->i_preroll_start;
1227 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1228 return VLC_EGENERIC;
1231 /*****************************************************************************
1232 * FlushRemainingPackets: flushes tail packets
1233 *****************************************************************************/
1235 static void FlushRemainingPackets( demux_t *p_demux )
1237 demux_sys_t *p_sys = p_demux->p_sys;
1238 for ( unsigned int i = 0; i < MAX_ASF_TRACKS; i++ )
1240 asf_track_t *tk = p_sys->track[i];
1241 if( tk && tk->info.p_frame )
1242 Packet_Send( &p_sys->packet_sys, i, &tk->info.p_frame );
1246 /*****************************************************************************
1248 *****************************************************************************/
1249 static void DemuxEnd( demux_t *p_demux )
1251 demux_sys_t *p_sys = p_demux->p_sys;
1255 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1256 p_sys->p_root = NULL;
1260 vlc_meta_Delete( p_sys->meta );
1264 for( int i = 0; i < MAX_ASF_TRACKS; i++ )
1266 asf_track_t *tk = p_sys->track[i];
1270 if( tk->info.p_frame )
1271 block_ChainRelease( tk->info.p_frame );
1275 es_out_Del( p_demux->out, tk->p_es );
1279 es_format_Clean( tk->p_fmt );
1284 p_sys->track[i] = 0;