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 */
45 * - add support for the newly added object: language, bitrate,
46 * extended stream properties.
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Open ( vlc_object_t * );
53 static void Close ( vlc_object_t * );
56 set_category( CAT_INPUT )
57 set_subcategory( SUBCAT_INPUT_DEMUX )
58 set_description( N_("ASF/WMV demuxer") )
59 set_capability( "demux", 200 )
60 set_callbacks( Open, Close )
61 add_shortcut( "asf", "wmv" )
65 /*****************************************************************************
67 *****************************************************************************/
68 static int Demux ( demux_t * );
69 static int Control( demux_t *, int i_query, va_list args );
77 asf_object_stream_properties_t *p_sp;
81 block_t *p_frame; /* use to gather complete frame */
87 mtime_t i_time; /* s */
88 mtime_t i_length; /* length of file file */
89 int64_t i_bitrate; /* global file bitrate */
91 asf_object_root_t *p_root;
92 asf_object_file_properties_t *p_fp;
95 asf_track_t *track[128]; /* track number is stored on 7 bits */
101 uint8_t i_seek_track;
102 unsigned int i_wait_keyframe;
107 static mtime_t GetMoviePTS( demux_sys_t * );
108 static int DemuxInit( demux_t * );
109 static void DemuxEnd( demux_t * );
110 static int DemuxPacket( demux_t * );
112 /*****************************************************************************
113 * Open: check file and initializes ASF structures
114 *****************************************************************************/
115 static int Open( vlc_object_t * p_this )
117 demux_t *p_demux = (demux_t *)p_this;
120 const uint8_t *p_peek;
122 /* A little test to see if it could be a asf stream */
123 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 ) return VLC_EGENERIC;
125 ASF_GetGUID( &guid, p_peek );
126 if( !guidcmp( &guid, &asf_object_header_guid ) ) return VLC_EGENERIC;
128 /* Set p_demux fields */
129 p_demux->pf_demux = Demux;
130 p_demux->pf_control = Control;
131 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
133 /* Load the headers */
134 if( DemuxInit( p_demux ) )
143 /*****************************************************************************
144 * Demux: read packet and send them to decoders
145 *****************************************************************************/
146 static int Demux( demux_t *p_demux )
148 demux_sys_t *p_sys = p_demux->p_sys;
152 const uint8_t *p_peek;
154 mtime_t i_time_begin = GetMoviePTS( p_sys );
157 if( !vlc_object_alive (p_demux) )
160 /* FIXME: returns EOF too early for some mms streams */
161 if( p_sys->i_data_end >= 0 &&
162 stream_Tell( p_demux->s ) >= p_sys->i_data_end )
166 /* Check if we have concatenated files */
167 if( stream_Peek( p_demux->s, &p_peek, 16 ) == 16 )
171 ASF_GetGUID( &guid, p_peek );
172 if( guidcmp( &guid, &asf_object_header_guid ) )
174 msg_Warn( p_demux, "found a new ASF header" );
175 /* We end this stream */
178 /* And we prepare to read the next one */
179 if( DemuxInit( p_demux ) )
181 msg_Err( p_demux, "failed to load the new header" );
182 dialog_Fatal( p_demux, _("Could not demux ASF stream"), "%s",
183 _("VLC failed to load the ASF header.") );
186 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
191 /* Read and demux a packet */
192 if( ( i_result = DemuxPacket( p_demux ) ) <= 0 )
196 if( i_time_begin == -1 )
198 i_time_begin = GetMoviePTS( p_sys );
202 i_length = GetMoviePTS( p_sys ) - i_time_begin;
203 if( i_length < 0 || i_length >= 40 * 1000 ) break;
208 p_sys->i_time = GetMoviePTS( p_sys );
209 if( p_sys->i_time >= 0 )
211 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time+1 );
217 /*****************************************************************************
218 * Close: frees unused data
219 *****************************************************************************/
220 static void Close( vlc_object_t * p_this )
222 demux_t *p_demux = (demux_t *)p_this;
226 free( p_demux->p_sys );
229 /*****************************************************************************
230 * SeekIndex: goto to i_date or i_percent
231 *****************************************************************************/
232 static int SeekPercent( demux_t *p_demux, int i_query, va_list args )
234 demux_sys_t *p_sys = p_demux->p_sys;
235 p_sys->i_wait_keyframe = p_sys->i_seek_track ? 50 : 0;
236 return demux_vaControlHelper( p_demux->s, p_sys->i_data_begin,
237 p_sys->i_data_end, p_sys->i_bitrate,
238 p_sys->p_fp->i_min_data_packet_size,
242 static int SeekIndex( demux_t *p_demux, mtime_t i_date, float f_pos )
244 demux_sys_t *p_sys = p_demux->p_sys;
245 asf_object_index_t *p_index;
247 msg_Dbg( p_demux, "seek with index: %i seconds, position %f",
248 i_date >= 0 ? (int)(i_date/1000000) : -1, f_pos );
251 i_date = p_sys->i_length * f_pos;
253 p_index = ASF_FindObject( p_sys->p_root, &asf_object_simple_index_guid, 0 );
255 uint64_t i_entry = i_date * 10 / p_index->i_index_entry_time_interval;
256 if( i_entry >= p_index->i_index_entry_count )
258 msg_Warn( p_demux, "Incomplete index" );
262 p_sys->i_wait_keyframe = p_sys->i_seek_track ? 50 : 0;
264 uint64_t i_offset = (uint64_t)p_index->index_entry[i_entry].i_packet_number *
265 p_sys->p_fp->i_min_data_packet_size;
267 if ( stream_Seek( p_demux->s, i_offset + p_sys->i_data_begin ) == VLC_SUCCESS )
269 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, VLC_TS_0 + i_date );
272 else return VLC_EGENERIC;
275 static void SeekPrepare( demux_t *p_demux )
277 demux_sys_t *p_sys = p_demux->p_sys;
280 for( int i = 0; i < 128 ; i++ )
282 asf_track_t *tk = p_sys->track[i];
288 block_ChainRelease( tk->p_frame );
293 /*****************************************************************************
295 *****************************************************************************/
296 static int Control( demux_t *p_demux, int i_query, va_list args )
298 demux_sys_t *p_sys = p_demux->p_sys;
305 case DEMUX_GET_LENGTH:
306 pi64 = (int64_t*)va_arg( args, int64_t * );
307 *pi64 = p_sys->i_length;
311 pi64 = (int64_t*)va_arg( args, int64_t * );
312 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
313 *pi64 = p_sys->i_time;
318 ! ( p_sys->p_fp->i_flags & ASF_FILE_PROPERTIES_SEEKABLE ) )
321 SeekPrepare( p_demux );
323 if( p_sys->b_index && p_sys->i_length > 0 )
326 va_copy( acpy, args );
327 i64 = (int64_t)va_arg( acpy, int64_t );
330 if( !SeekIndex( p_demux, i64, -1 ) )
333 return SeekPercent( p_demux, i_query, args );
335 case DEMUX_GET_POSITION:
336 if( p_sys->i_time < 0 ) return VLC_EGENERIC;
337 if( p_sys->i_length > 0 )
339 pf = (double*)va_arg( args, double * );
340 *pf = p_sys->i_time / (double)p_sys->i_length;
343 return demux_vaControlHelper( p_demux->s, p_sys->i_data_begin,
344 p_sys->i_data_end, p_sys->i_bitrate,
345 p_sys->p_fp->i_min_data_packet_size,
348 case DEMUX_SET_POSITION:
350 ! ( p_sys->p_fp->i_flags & ASF_FILE_PROPERTIES_SEEKABLE ) )
353 SeekPrepare( p_demux );
355 if( p_sys->b_index && p_sys->i_length > 0 )
358 va_copy( acpy, args );
359 f = (double)va_arg( acpy, double );
362 if( !SeekIndex( p_demux, -1, f ) )
365 return SeekPercent( p_demux, i_query, args );
368 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
369 vlc_meta_Merge( p_meta, p_sys->meta );
374 ! ( p_sys->p_fp->i_flags & ASF_FILE_PROPERTIES_SEEKABLE ) )
376 bool *pb_bool = (bool*)va_arg( args, bool * );
383 return demux_vaControlHelper( p_demux->s, p_sys->i_data_begin,
384 p_sys->i_data_end, p_sys->i_bitrate,
385 ( p_sys->p_fp ) ? p_sys->p_fp->i_min_data_packet_size : 1,
390 /*****************************************************************************
392 *****************************************************************************/
393 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
398 for( i = 0; i < 128 ; i++ )
400 asf_track_t *tk = p_sys->track[i];
402 if( tk && tk->p_es && tk->i_time > 0)
404 if( i_time < 0 ) i_time = tk->i_time;
405 else i_time = __MIN( i_time, tk->i_time );
412 static inline int GetValue2b(uint32_t *var, const uint8_t *p, unsigned int *skip, int left, int bits)
419 *var = p[*skip]; *skip += 1;
424 *var = GetWLE(&p[*skip]); *skip += 2;
429 *var = GetDWLE(&p[*skip]); *skip += 4;
441 uint32_t padding_length;
446 /* buffer handling for this ASF packet */
448 const uint8_t *p_peek;
452 static void SendPacket(demux_t *p_demux, asf_track_t *tk)
454 demux_sys_t *p_sys = p_demux->p_sys;
456 block_t *p_gather = block_ChainGather( tk->p_frame );
458 if( p_gather->i_dts > VLC_TS_INVALID )
459 tk->i_time = p_gather->i_dts - VLC_TS_0;
461 if( p_sys->i_time < 0 )
462 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_gather->i_dts );
464 es_out_Send( p_demux->out, tk->p_es, p_gather );
469 static int DemuxSubPayload(demux_t *p_demux, asf_track_t *tk,
470 uint32_t i_sub_payload_data_length, mtime_t i_pts, uint32_t i_media_object_offset)
472 /* FIXME I don't use i_media_object_number, sould I ? */
473 if( tk->p_frame && i_media_object_offset == 0 )
474 SendPacket(p_demux, tk);
476 block_t *p_frag = stream_Block( p_demux->s, i_sub_payload_data_length );
477 if( p_frag == NULL ) {
478 msg_Warn( p_demux, "cannot read data" );
482 if( tk->p_frame == NULL ) {
483 p_frag->i_pts = VLC_TS_0 + i_pts;
484 p_frag->i_dts = VLC_TS_0 + p_frag->i_pts; //FIXME: VLC_TS_0 * 2 ?
485 if( tk->i_cat == VIDEO_ES )
486 p_frag->i_pts = VLC_TS_INVALID;
489 block_ChainAppend( &tk->p_frame, p_frag );
494 static uint32_t SkipBytes( stream_t *s, uint32_t i_bytes )
497 int i_to_read = __MIN(i_bytes, INT_MAX);
498 uint32_t i_bytes_read = 0;
502 i_read = stream_Read( s, NULL, i_to_read );
504 i_bytes_read += i_read;
505 if ( i_read < i_to_read || i_bytes == 0 )
510 i_to_read = __MIN(i_bytes, INT_MAX);
516 static int DemuxPayload(demux_t *p_demux, struct asf_packet_t *pkt, int i_payload)
519 VLC_UNUSED( i_payload );
521 demux_sys_t *p_sys = p_demux->p_sys;
523 if( ! pkt->left || pkt->i_skip >= pkt->left )
526 bool b_packet_keyframe = pkt->p_peek[pkt->i_skip] >> 7;
527 uint8_t i_stream_number = pkt->p_peek[pkt->i_skip++] & 0x7f;
529 uint32_t i_media_object_number = 0;
530 if (GetValue2b(&i_media_object_number, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->property >> 4) < 0)
532 uint32_t i_media_object_offset = 0;
533 if (GetValue2b(&i_media_object_offset, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->property >> 2) < 0)
535 uint32_t i_replicated_data_length = 0;
536 if (GetValue2b(&i_replicated_data_length, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->property) < 0)
541 if( i_replicated_data_length > 1 ) // should be at least 8 bytes
543 i_pts = (mtime_t)GetDWLE( pkt->p_peek + pkt->i_skip + 4 );
544 pkt->i_skip += i_replicated_data_length;
546 if( ! pkt->left || pkt->i_skip >= pkt->left )
549 /* Compressed sub payload */
550 else if( i_replicated_data_length == 1 )
552 /* i_media_object_offset is presentation time */
553 /* Next byte is Presentation Time Delta */
554 i_pts = (mtime_t)i_media_object_offset + (mtime_t)pkt->p_peek[pkt->i_skip] * i_payload;
556 i_media_object_offset = 0;
560 i_pts = (mtime_t)pkt->send_time * 1000;
563 i_pts -= p_sys->p_fp->i_preroll;
564 if (i_pts < 0) i_pts = 0; // FIXME?
565 i_pts *= 1000; // FIXME ?
567 uint32_t i_payload_data_length = 0;
568 uint32_t i_temp_payload_length = 0;
569 if( pkt->multiple ) {
570 if (GetValue2b(&i_temp_payload_length, pkt->p_peek, &pkt->i_skip, pkt->left - pkt->i_skip, pkt->length_type) < 0)
573 i_temp_payload_length = pkt->length - pkt->padding_length - pkt->i_skip;
575 if( ! i_temp_payload_length || i_temp_payload_length > pkt->left )
578 i_payload_data_length = i_temp_payload_length;
582 "payload(%d) stream_number:%"PRIu8" media_object_number:%d media_object_offset:%"PRIu32" replicated_data_length:%"PRIu32" payload_data_length %"PRIu32,
583 i_payload + 1, i_stream_number, i_media_object_number,
584 i_media_object_offset, i_replicated_data_length, i_payload_data_length );
587 asf_track_t *tk = p_sys->track[i_stream_number];
590 msg_Warn( p_demux, "undeclared stream[Id 0x%x]", i_stream_number );
594 if( p_sys->i_wait_keyframe && !i_media_object_offset &&
595 (i_stream_number != p_sys->i_seek_track || !b_packet_keyframe) )
597 p_sys->i_wait_keyframe--;
600 p_sys->i_wait_keyframe = 0;
605 while (i_payload_data_length)
607 uint32_t i_sub_payload_data_length = i_payload_data_length;
608 if( i_replicated_data_length == 1 )
610 i_sub_payload_data_length = pkt->p_peek[pkt->i_skip++];
611 i_payload_data_length--;
614 SkipBytes( p_demux->s, pkt->i_skip );
616 if ( i_sub_payload_data_length &&
617 DemuxSubPayload(p_demux, tk, i_sub_payload_data_length, i_pts,
618 i_media_object_offset) < 0)
621 if ( pkt->left > pkt->i_skip + i_sub_payload_data_length )
622 pkt->left -= pkt->i_skip + i_sub_payload_data_length;
628 int i_return = stream_Peek( p_demux->s, &pkt->p_peek, __MIN(pkt->left, INT_MAX) );
629 if ( i_return <= 0 || (unsigned int) i_return < __MIN(pkt->left, INT_MAX) )
631 msg_Warn( p_demux, "cannot peek, EOF ?" );
636 if ( i_sub_payload_data_length <= i_payload_data_length )
637 i_payload_data_length -= i_sub_payload_data_length;
639 i_payload_data_length = 0;
645 pkt->i_skip += i_payload_data_length;
649 static int DemuxPacket( demux_t *p_demux )
651 demux_sys_t *p_sys = p_demux->p_sys;
653 uint32_t i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
655 const uint8_t *p_peek;
656 int i_return = stream_Peek( p_demux->s, &p_peek,i_data_packet_min );
657 if( i_return <= 0 || ((unsigned int) i_return) < i_data_packet_min )
659 msg_Warn( p_demux, "cannot peek while getting new packet, EOF ?" );
662 unsigned int i_skip = 0;
664 /* *** parse error correction if present *** */
667 unsigned int i_error_correction_data_length = p_peek[0] & 0x0f;
668 unsigned int i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01;
669 unsigned int i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03;
670 i_skip += 1; // skip error correction flags
672 if( i_error_correction_length_type != 0x00 ||
673 i_opaque_data_present != 0 ||
674 i_error_correction_data_length != 0x02 )
676 goto loop_error_recovery;
679 i_skip += i_error_correction_data_length;
682 msg_Warn( p_demux, "no error correction" );
685 if( i_skip + 2 >= i_data_packet_min )
686 goto loop_error_recovery;
688 struct asf_packet_t pkt;
689 int i_packet_flags = p_peek[i_skip]; i_skip++;
690 pkt.property = p_peek[i_skip]; i_skip++;
691 pkt.multiple = !!(i_packet_flags&0x01);
693 pkt.length = i_data_packet_min;
694 pkt.padding_length = 0;
696 if (GetValue2b(&pkt.length, p_peek, &i_skip, i_data_packet_min - i_skip, i_packet_flags >> 5) < 0)
697 goto loop_error_recovery;
698 uint32_t i_packet_sequence;
699 if (GetValue2b(&i_packet_sequence, p_peek, &i_skip, i_data_packet_min - i_skip, i_packet_flags >> 1) < 0)
700 goto loop_error_recovery;
701 if (GetValue2b(&pkt.padding_length, p_peek, &i_skip, i_data_packet_min - i_skip, i_packet_flags >> 3) < 0)
702 goto loop_error_recovery;
704 if( pkt.padding_length > pkt.length )
706 msg_Warn( p_demux, "Too large padding: %d", pkt.padding_length );
707 goto loop_error_recovery;
710 if( pkt.length < i_data_packet_min )
712 /* if packet length too short, there is extra padding */
713 pkt.padding_length += i_data_packet_min - pkt.length;
714 pkt.length = i_data_packet_min;
717 pkt.send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
718 /* uint16_t i_packet_duration = GetWLE( p_peek + i_skip ); */ i_skip += 2;
720 i_return = stream_Peek( p_demux->s, &p_peek, pkt.length );
721 if( i_return <= 0 || pkt.length == 0 || (unsigned int)i_return < pkt.length )
723 msg_Warn( p_demux, "cannot peek, EOF ?" );
727 int i_payload_count = 1;
728 pkt.length_type = 0x02; //unused
731 i_payload_count = p_peek[i_skip] & 0x3f;
732 pkt.length_type = ( p_peek[i_skip] >> 6 )&0x03;
737 msg_Dbg(p_demux, "%d payloads", i_payload_count);
742 pkt.left = pkt.length;
744 for( int i_payload = 0; i_payload < i_payload_count ; i_payload++ )
745 if (DemuxPayload(p_demux, &pkt, i_payload) < 0)
751 if( pkt.left > pkt.padding_length )
752 msg_Warn( p_demux, "Didn't read %"PRIu32" bytes in the packet",
753 pkt.left - pkt.padding_length );
754 else if( pkt.left < pkt.padding_length )
755 msg_Warn( p_demux, "Read %"PRIu32" too much bytes in the packet",
756 pkt.padding_length - pkt.left );
758 int i_return = stream_Read( p_demux->s, NULL, pkt.left );
759 if( i_return < 0 || (unsigned int) i_return < pkt.left )
761 msg_Err( p_demux, "cannot skip data, EOF ?" );
769 msg_Warn( p_demux, "unsupported packet header" );
770 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
772 msg_Err( p_demux, "unsupported packet header, fatal error" );
775 i_return = stream_Read( p_demux->s, NULL, i_data_packet_min );
776 if( i_return <= 0 || (unsigned int) i_return != i_data_packet_min )
778 msg_Warn( p_demux, "cannot skip data, EOF ?" );
785 /*****************************************************************************
787 *****************************************************************************/
788 typedef struct asf_es_priorities_t
790 int16_t *pi_stream_numbers;
792 } asf_es_priorities_t;
794 /* Fills up our exclusion list */
795 static void ASF_fillup_es_priorities_ex( demux_sys_t *p_sys, void *p_hdr,
796 asf_es_priorities_t *p_prios )
798 /* Find stream exclusions */
799 asf_object_advanced_mutual_exclusion_t *p_mutex =
800 ASF_FindObject( p_hdr, &asf_object_advanced_mutual_exclusion, 0 );
801 if (! p_mutex ) return;
803 p_prios->pi_stream_numbers = malloc( p_sys->i_track * sizeof( int16_t ) );
804 if ( !p_prios->pi_stream_numbers ) return;
806 if ( p_mutex->i_stream_number_count )
808 /* Just set highest prio on highest in the group */
809 for ( int16_t i = 1; i < p_mutex->i_stream_number_count; i++ )
811 if ( p_prios->i_count + 1 == INT_MAX ) break; /* FIXME: fix all types */
812 if ( (unsigned int) p_prios->i_count > p_sys->i_track ) break;
813 p_prios->pi_stream_numbers[ p_prios->i_count++ ] = p_mutex->pi_stream_number[ i ];
818 /* Fills up our bitrate exclusion list */
819 static void ASF_fillup_es_bitrate_priorities_ex( demux_sys_t *p_sys, void *p_hdr,
820 asf_es_priorities_t *p_prios )
822 /* Find bitrate exclusions */
823 asf_object_bitrate_mutual_exclusion_t *p_bitrate_mutex =
824 ASF_FindObject( p_hdr, &asf_object_bitrate_mutual_exclusion_guid, 0 );
825 if (! p_bitrate_mutex ) return;
827 p_prios->pi_stream_numbers = malloc( p_sys->i_track * sizeof( int16_t ) );
828 if ( !p_prios->pi_stream_numbers ) return;
830 if ( p_bitrate_mutex->i_stream_number_count )
832 /* Just remove < highest */
833 for ( int16_t i = 1; i < p_bitrate_mutex->i_stream_number_count; i++ )
835 if ( p_prios->i_count + 1 == INT_MAX ) break; /* FIXME: fix all types */
836 if ( (unsigned int) p_prios->i_count > p_sys->i_track ) break;
837 p_prios->pi_stream_numbers[ p_prios->i_count++ ] = p_bitrate_mutex->pi_stream_numbers[ i ];
843 static int DemuxInit( demux_t *p_demux )
845 demux_sys_t *p_sys = p_demux->p_sys;
850 p_sys->i_bitrate = 0;
851 p_sys->p_root = NULL;
855 p_sys->i_seek_track = 0;
856 p_sys->i_wait_keyframe = 0;
857 for( int i = 0; i < 128; i++ )
859 p_sys->track[i] = NULL;
861 p_sys->i_data_begin = -1;
862 p_sys->i_data_end = -1;
865 /* Now load all object ( except raw data ) */
867 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
868 if( !(p_sys->p_root = ASF_ReadObjectRoot(p_demux->s, b_seekable)) )
870 msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
873 p_sys->p_fp = p_sys->p_root->p_fp;
875 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
877 msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
881 if ( ASF_FindObject( p_sys->p_root->p_hdr,
882 &asf_object_content_encryption_guid, 0 ) != NULL
883 || ASF_FindObject( p_sys->p_root->p_hdr,
884 &asf_object_extended_content_encryption_guid, 0 ) != NULL
885 || ASF_FindObject( p_sys->p_root->p_hdr,
886 &asf_object_advanced_content_encryption_guid, 0 ) != NULL )
888 msg_Warn( p_demux, "ASF plugin discarded (DRM encumbered content)" );
892 p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
893 &asf_object_stream_properties_guid );
894 if( p_sys->i_track <= 0 )
896 msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
899 msg_Dbg( p_demux, "found %d streams", p_sys->i_track );
901 /* check if index is available */
902 asf_object_index_t *p_index = ASF_FindObject( p_sys->p_root,
903 &asf_object_simple_index_guid, 0 );
904 const bool b_index = p_index && p_index->i_index_entry_count;
906 /* Find the extended header if any */
907 asf_object_t *p_hdr_ext = ASF_FindObject( p_sys->p_root->p_hdr,
908 &asf_object_header_extension_guid, 0 );
910 asf_object_language_list_t *p_languages = NULL;
911 asf_es_priorities_t fmt_priorities_ex = { NULL, 0 };
912 asf_es_priorities_t fmt_priorities_bitrate_ex = { NULL, 0 };
916 p_languages = ASF_FindObject( p_hdr_ext, &asf_object_language_list, 0 );
918 ASF_fillup_es_priorities_ex( p_sys, p_hdr_ext, &fmt_priorities_ex );
919 ASF_fillup_es_bitrate_priorities_ex( p_sys, p_hdr_ext, &fmt_priorities_bitrate_ex );
922 for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
925 asf_object_stream_properties_t *p_sp;
926 asf_object_extended_stream_properties_t *p_esp;
927 bool b_access_selected;
929 p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
930 &asf_object_stream_properties_guid,
934 tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
935 memset( tk, 0, sizeof( asf_track_t ) );
942 /* Check (in case of mms) if this track is selected (ie will receive data) */
943 if( !stream_Control( p_demux->s, STREAM_GET_PRIVATE_ID_STATE,
944 (int) p_sp->i_stream_number, &b_access_selected ) &&
947 tk->i_cat = UNKNOWN_ES;
948 msg_Dbg( p_demux, "ignoring not selected stream(ID:%u) (by access)",
949 p_sp->i_stream_number );
953 /* Find the associated extended_stream_properties if any */
956 int i_ext_stream = ASF_CountObject( p_hdr_ext,
957 &asf_object_extended_stream_properties_guid );
958 for( int i = 0; i < i_ext_stream; i++ )
960 asf_object_t *p_tmp =
961 ASF_FindObject( p_hdr_ext,
962 &asf_object_extended_stream_properties_guid, i );
963 if( p_tmp->ext_stream.i_stream_number == p_sp->i_stream_number )
965 p_esp = &p_tmp->ext_stream;
973 if( guidcmp( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
974 p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
976 uint8_t *p_data = p_sp->p_type_specific_data;
979 es_format_Init( &fmt, AUDIO_ES, 0 );
980 i_format = GetWLE( &p_data[0] );
981 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
982 fmt.audio.i_channels = GetWLE( &p_data[2] );
983 fmt.audio.i_rate = GetDWLE( &p_data[4] );
984 fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
985 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
986 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
988 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
989 i_format != WAVE_FORMAT_MPEGLAYER3 &&
990 i_format != WAVE_FORMAT_MPEG )
992 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
993 p_sp->i_type_specific_data_length -
994 sizeof( WAVEFORMATEX ) );
995 fmt.p_extra = malloc( fmt.i_extra );
996 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
1000 msg_Dbg( p_demux, "added new audio stream(codec:0x%x,ID:%d)",
1001 GetWLE( p_data ), p_sp->i_stream_number );
1003 else if( guidcmp( &p_sp->i_stream_type,
1004 &asf_object_stream_type_video ) &&
1005 p_sp->i_type_specific_data_length >= 11 +
1006 sizeof( VLC_BITMAPINFOHEADER ) )
1008 uint8_t *p_data = &p_sp->p_type_specific_data[11];
1010 es_format_Init( &fmt, VIDEO_ES,
1011 VLC_FOURCC( p_data[16], p_data[17],
1012 p_data[18], p_data[19] ) );
1013 fmt.video.i_width = GetDWLE( p_data + 4 );
1014 fmt.video.i_height= GetDWLE( p_data + 8 );
1016 if( p_esp && p_esp->i_average_time_per_frame > 0 )
1018 fmt.video.i_frame_rate = 10000000;
1019 fmt.video.i_frame_rate_base = p_esp->i_average_time_per_frame;
1022 if( fmt.i_codec == VLC_FOURCC( 'D','V','R',' ') )
1024 /* DVR-MS special ASF */
1025 fmt.i_codec = VLC_FOURCC( 'm','p','g','2' ) ;
1026 fmt.b_packetized = false;
1029 if( p_sp->i_type_specific_data_length > 11 +
1030 sizeof( VLC_BITMAPINFOHEADER ) )
1032 fmt.i_extra = __MIN( GetDWLE( p_data ),
1033 p_sp->i_type_specific_data_length - 11 -
1034 sizeof( VLC_BITMAPINFOHEADER ) );
1035 fmt.p_extra = malloc( fmt.i_extra );
1036 memcpy( fmt.p_extra, &p_data[sizeof( VLC_BITMAPINFOHEADER )],
1040 /* Look for an aspect ratio */
1041 if( p_sys->p_root->p_metadata )
1043 asf_object_metadata_t *p_meta = p_sys->p_root->p_metadata;
1044 int i_aspect_x = 0, i_aspect_y = 0;
1047 for( i = 0; i < p_meta->i_record_entries_count; i++ )
1049 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioX" ) )
1051 if( (!i_aspect_x && !p_meta->record[i].i_stream) ||
1052 p_meta->record[i].i_stream ==
1053 p_sp->i_stream_number )
1054 i_aspect_x = p_meta->record[i].i_val;
1056 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioY" ) )
1058 if( (!i_aspect_y && !p_meta->record[i].i_stream) ||
1059 p_meta->record[i].i_stream ==
1060 p_sp->i_stream_number )
1061 i_aspect_y = p_meta->record[i].i_val;
1065 if( i_aspect_x && i_aspect_y )
1067 fmt.video.i_sar_num = i_aspect_x;
1068 fmt.video.i_sar_den = i_aspect_y;
1072 /* If there is a video track then use the index for seeking */
1073 p_sys->b_index = b_index;
1075 msg_Dbg( p_demux, "added new video stream(ID:%d)",
1076 p_sp->i_stream_number );
1078 else if( guidcmp( &p_sp->i_stream_type, &asf_object_extended_stream_header ) &&
1079 p_sp->i_type_specific_data_length >= 64 )
1081 /* Now follows a 64 byte header of which we don't know much */
1082 guid_t *p_ref = (guid_t *)p_sp->p_type_specific_data;
1083 uint8_t *p_data = p_sp->p_type_specific_data + 64;
1084 unsigned int i_data = p_sp->i_type_specific_data_length - 64;
1086 msg_Dbg( p_demux, "Ext stream header detected. datasize = %d", p_sp->i_type_specific_data_length );
1087 if( guidcmp( p_ref, &asf_object_extended_stream_type_audio ) &&
1088 i_data >= sizeof( WAVEFORMATEX ) - 2)
1091 es_format_Init( &fmt, AUDIO_ES, 0 );
1092 i_format = GetWLE( &p_data[0] );
1094 fmt.i_codec = VLC_CODEC_A52;
1096 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
1097 fmt.audio.i_channels = GetWLE( &p_data[2] );
1098 fmt.audio.i_rate = GetDWLE( &p_data[4] );
1099 fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
1100 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
1101 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
1102 fmt.b_packetized = true;
1104 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
1105 i_format != WAVE_FORMAT_MPEGLAYER3 &&
1106 i_format != WAVE_FORMAT_MPEG )
1108 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
1109 p_sp->i_type_specific_data_length -
1110 sizeof( WAVEFORMATEX ) );
1111 fmt.p_extra = malloc( fmt.i_extra );
1112 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
1116 msg_Dbg( p_demux, "added new audio stream (codec:0x%x,ID:%d)",
1117 i_format, p_sp->i_stream_number );
1121 es_format_Init( &fmt, UNKNOWN_ES, 0 );
1126 es_format_Init( &fmt, UNKNOWN_ES, 0 );
1129 tk->i_cat = fmt.i_cat;
1130 if( fmt.i_cat != UNKNOWN_ES )
1132 if( p_esp && p_languages &&
1133 p_esp->i_language_index >= 0 &&
1134 p_esp->i_language_index < p_languages->i_language )
1136 fmt.psz_language = strdup( p_languages->ppsz_language[p_esp->i_language_index] );
1138 if( fmt.psz_language && (p = strchr( fmt.psz_language, '-' )) )
1142 /* Set the track on which we'll do our seeking to the first video track */
1143 if(!p_sys->i_seek_track && fmt.i_cat == VIDEO_ES)
1144 p_sys->i_seek_track = p_sp->i_stream_number;
1146 /* Set our priority so we won't get multiple videos */
1147 int i_priority = ES_PRIORITY_SELECTABLE_MIN;
1148 for( int16_t i = 0; i < fmt_priorities_ex.i_count; i++ )
1150 if ( fmt_priorities_ex.pi_stream_numbers[i] == p_sp->i_stream_number )
1152 i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
1156 for( int16_t i = 0; i < fmt_priorities_bitrate_ex.i_count; i++ )
1158 if ( fmt_priorities_bitrate_ex.pi_stream_numbers[i] == p_sp->i_stream_number )
1160 i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
1164 fmt.i_priority = i_priority;
1166 tk->p_es = es_out_Add( p_demux->out, &fmt );
1170 msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
1171 p_sp->i_stream_number );
1173 es_format_Clean( &fmt );
1176 free( fmt_priorities_ex.pi_stream_numbers );
1177 free( fmt_priorities_bitrate_ex.pi_stream_numbers );
1179 p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
1180 if( p_sys->p_root->p_data->i_object_size != 0 )
1182 p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
1183 p_sys->p_root->p_data->i_object_size;
1184 p_sys->i_data_end = __MIN( stream_Size( p_demux->s ), p_sys->i_data_end );
1187 { /* live/broacast */
1188 p_sys->i_data_end = -1;
1191 /* go to first packet */
1192 stream_Seek( p_demux->s, p_sys->i_data_begin );
1194 /* try to calculate movie time */
1195 if( p_sys->p_fp->i_data_packets_count > 0 )
1198 int64_t i_size = stream_Size( p_demux->s );
1200 if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
1202 i_size = p_sys->i_data_end;
1205 /* real number of packets */
1206 i_count = ( i_size - p_sys->i_data_begin ) /
1207 p_sys->p_fp->i_min_data_packet_size;
1209 /* calculate the time duration in micro-s */
1210 p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
1212 (mtime_t)p_sys->p_fp->i_data_packets_count - p_sys->p_fp->i_preroll * 1000;
1213 if( p_sys->i_length < 0 )
1214 p_sys->i_length = 0;
1216 if( p_sys->i_length > 0 )
1218 p_sys->i_bitrate = 8 * i_size * (int64_t)1000000 / p_sys->i_length;
1222 /* Create meta information */
1223 p_sys->meta = vlc_meta_New();
1225 asf_object_content_description_t *p_cd;
1226 if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
1227 &asf_object_content_description_guid, 0 ) ) )
1229 if( p_cd->psz_title && *p_cd->psz_title )
1231 vlc_meta_SetTitle( p_sys->meta, p_cd->psz_title );
1233 if( p_cd->psz_artist && *p_cd->psz_artist )
1235 vlc_meta_SetArtist( p_sys->meta, p_cd->psz_artist );
1237 if( p_cd->psz_copyright && *p_cd->psz_copyright )
1239 vlc_meta_SetCopyright( p_sys->meta, p_cd->psz_copyright );
1241 if( p_cd->psz_description && *p_cd->psz_description )
1243 vlc_meta_SetDescription( p_sys->meta, p_cd->psz_description );
1245 if( p_cd->psz_rating && *p_cd->psz_rating )
1247 vlc_meta_SetRating( p_sys->meta, p_cd->psz_rating );
1250 /// \tood Fix Child meta for ASF tracks
1252 for( i_stream = 0, i = 0; i < 128; i++ )
1254 asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
1255 &asf_object_codec_list_guid, 0 );
1257 if( p_sys->track[i] )
1259 vlc_meta_t *tk = vlc_meta_New();
1260 TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
1262 if( p_cl && i_stream < p_cl->i_codec_entries_count )
1264 if( p_cl->codec[i_stream].psz_name &&
1265 *p_cl->codec[i_stream].psz_name )
1267 vlc_meta_Add( tk, VLC_META_CODEC_NAME,
1268 p_cl->codec[i_stream].psz_name );
1270 if( p_cl->codec[i_stream].psz_description &&
1271 *p_cl->codec[i_stream].psz_description )
1273 vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
1274 p_cl->codec[i_stream].psz_description );
1284 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1285 return VLC_EGENERIC;
1287 /*****************************************************************************
1289 *****************************************************************************/
1290 static void DemuxEnd( demux_t *p_demux )
1292 demux_sys_t *p_sys = p_demux->p_sys;
1296 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
1297 p_sys->p_root = NULL;
1301 vlc_meta_Delete( p_sys->meta );
1305 for( int i = 0; i < 128; i++ )
1307 asf_track_t *tk = p_sys->track[i];
1313 block_ChainRelease( tk->p_frame );
1317 es_out_Del( p_demux->out, tk->p_es );
1321 p_sys->track[i] = 0;