1 /*****************************************************************************
2 * asf.c : ASF demux module
3 *****************************************************************************
4 * Copyright (C) 2002-2003 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc/input.h>
34 #include "codecs.h" /* BITMAPINFOHEADER, WAVEFORMATEX */
38 * - add support for the newly added object: language, bitrate,
39 * extended stream properties.
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close ( vlc_object_t * );
49 set_category( CAT_INPUT );
50 set_subcategory( SUBCAT_INPUT_DEMUX );
51 set_description( _("ASF v1.0 demuxer") );
52 set_capability( "demux2", 200 );
53 set_callbacks( Open, Close );
54 add_shortcut( "asf" );
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Demux ( demux_t * );
62 static int Control( demux_t *, int i_query, va_list args );
70 asf_object_stream_properties_t *p_sp;
74 block_t *p_frame; /* use to gather complete frame */
80 mtime_t i_time; /* s */
81 mtime_t i_length; /* length of file file */
82 int64_t i_bitrate; /* global file bitrate */
84 asf_object_root_t *p_root;
85 asf_object_file_properties_t *p_fp;
88 asf_track_t *track[128];
96 static mtime_t GetMoviePTS( demux_sys_t * );
97 static int DemuxInit( demux_t * );
98 static void DemuxEnd( demux_t * );
99 static int DemuxPacket( demux_t * );
101 /*****************************************************************************
102 * Open: check file and initializes ASF structures
103 *****************************************************************************/
104 static int Open( vlc_object_t * p_this )
106 demux_t *p_demux = (demux_t *)p_this;
111 /* A little test to see if it could be a asf stream */
112 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 ) return VLC_EGENERIC;
114 ASF_GetGUID( &guid, p_peek );
115 if( !ASF_CmpGUID( &guid, &asf_object_header_guid ) ) return VLC_EGENERIC;
117 /* Set p_demux fields */
118 p_demux->pf_demux = Demux;
119 p_demux->pf_control = Control;
120 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
121 memset( p_sys, 0, sizeof( demux_sys_t ) );
123 /* Load the headers */
124 if( DemuxInit( p_demux ) )
132 /*****************************************************************************
133 * Demux: read packet and send them to decoders
134 *****************************************************************************/
135 static int Demux( demux_t *p_demux )
137 demux_sys_t *p_sys = p_demux->p_sys;
143 mtime_t i_time_begin = GetMoviePTS( p_sys );
151 /* Check if we have concatenated files */
152 if( stream_Peek( p_demux->s, &p_peek, 16 ) == 16 )
156 ASF_GetGUID( &guid, p_peek );
157 if( ASF_CmpGUID( &guid, &asf_object_header_guid ) )
159 msg_Warn( p_demux, "found a new ASF header" );
160 /* We end this stream */
163 /* And we prepare to read the next one */
164 if( DemuxInit( p_demux ) )
166 msg_Err( p_demux, "failed to load the new header" );
173 /* Read and demux a packet */
174 if( ( i_result = DemuxPacket( p_demux ) ) <= 0 )
178 if( i_time_begin == -1 )
180 i_time_begin = GetMoviePTS( p_sys );
184 i_length = GetMoviePTS( p_sys ) - i_time_begin;
185 if( i_length < 0 || i_length >= 40 * 1000 )
193 p_sys->i_time = GetMoviePTS( p_sys );
194 if( p_sys->i_time >= 0 )
196 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
202 /*****************************************************************************
203 * Close: frees unused data
204 *****************************************************************************/
205 static void Close( vlc_object_t * p_this )
207 demux_t *p_demux = (demux_t *)p_this;
211 free( p_demux->p_sys );
214 /*****************************************************************************
216 *****************************************************************************/
217 static int Control( demux_t *p_demux, int i_query, va_list args )
219 demux_sys_t *p_sys = p_demux->p_sys;
222 vlc_meta_t **pp_meta;
229 case DEMUX_GET_LENGTH:
230 pi64 = (int64_t*)va_arg( args, int64_t * );
231 *pi64 = p_sys->i_length;
235 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
236 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
239 case DEMUX_SET_POSITION:
241 for( i = 0; i < 128 ; i++ )
243 asf_track_t *tk = p_sys->track[i];
244 if( tk ) tk->i_time = -1;
248 return demux2_vaControlHelper( p_demux->s,
249 p_sys->i_data_begin, p_sys->i_data_end,
250 p_sys->i_bitrate, p_sys->p_fp->i_min_data_packet_size,
255 /*****************************************************************************
257 *****************************************************************************/
258 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
264 for( i = 0; i < 128 ; i++ )
266 asf_track_t *tk = p_sys->track[i];
268 if( tk && tk->p_es && tk->i_time > 0)
276 i_time = __MIN( i_time, tk->i_time );
284 #define GETVALUE2b( bits, var, def ) \
285 switch( (bits)&0x03 ) \
287 case 1: var = p_peek[i_skip]; i_skip++; break; \
288 case 2: var = GetWLE( p_peek + i_skip ); i_skip+= 2; break; \
289 case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
291 default: var = def; break;\
294 static int DemuxPacket( demux_t *p_demux )
296 demux_sys_t *p_sys = p_demux->p_sys;
297 int i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
301 int i_packet_size_left;
303 int i_packet_property;
305 int b_packet_multiple_payload;
307 int i_packet_sequence;
308 int i_packet_padding_length;
310 uint32_t i_packet_send_time;
311 uint16_t i_packet_duration;
314 int i_payload_length_type;
317 if( stream_Peek( p_demux->s, &p_peek,i_data_packet_min)<i_data_packet_min )
319 msg_Warn( p_demux, "cannot peek while getting new packet, EOF ?" );
324 /* *** parse error correction if present *** */
327 unsigned int i_error_correction_length_type;
328 unsigned int i_error_correction_data_length;
329 unsigned int i_opaque_data_present;
331 i_error_correction_data_length = p_peek[0] & 0x0f; // 4bits
332 i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01; // 1bit
333 i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
334 i_skip += 1; // skip error correction flags
336 if( i_error_correction_length_type != 0x00 ||
337 i_opaque_data_present != 0 ||
338 i_error_correction_data_length != 0x02 )
340 goto loop_error_recovery;
343 i_skip += i_error_correction_data_length;
347 msg_Warn( p_demux, "p_peek[0]&0x80 != 0x80" );
351 if( i_skip + 2 >= i_data_packet_min )
353 goto loop_error_recovery;
356 i_packet_flags = p_peek[i_skip]; i_skip++;
357 i_packet_property = p_peek[i_skip]; i_skip++;
359 b_packet_multiple_payload = i_packet_flags&0x01;
361 /* read some value */
362 GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
363 GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
364 GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
366 i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
367 i_packet_duration = GetWLE( p_peek + i_skip ); i_skip += 2;
369 // i_packet_size_left = i_packet_length; // XXX data really read
370 /* FIXME I have to do that for some file, I don't known why */
371 i_packet_size_left = i_data_packet_min;
373 if( b_packet_multiple_payload )
375 i_payload_count = p_peek[i_skip] & 0x3f;
376 i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
382 i_payload_length_type = 0x02; // unused
385 for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
390 int i_media_object_number;
391 int i_media_object_offset;
392 int i_replicated_data_length;
393 int i_payload_data_length;
394 int i_payload_data_pos;
395 int i_sub_payload_data_length;
401 if( i_skip >= i_packet_size_left )
403 /* prevent some segfault with invalid file */
407 i_stream_number = p_peek[i_skip] & 0x7f;
410 GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
411 GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
412 GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
414 if( i_replicated_data_length > 1 ) // should be at least 8 bytes
416 i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
417 i_skip += i_replicated_data_length;
420 i_media_object_offset = i_tmp;
422 if( i_skip >= i_packet_size_left )
427 else if( i_replicated_data_length == 1 )
429 /* msg_Dbg( p_demux, "found compressed payload" ); */
431 i_pts = (mtime_t)i_tmp * 1000;
432 i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
434 i_media_object_offset = 0;
438 i_pts = (mtime_t)i_packet_send_time * 1000;
441 i_media_object_offset = i_tmp;
444 i_pts = __MAX( i_pts - p_sys->p_fp->i_preroll * 1000, 0 );
445 if( b_packet_multiple_payload )
447 GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
451 i_payload_data_length = i_packet_length -
452 i_packet_padding_length - i_skip;
455 if( i_payload_data_length < 0 || i_skip + i_payload_data_length > i_packet_size_left )
461 "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
462 i_payload + 1, i_payload_count, i_stream_number, i_media_object_number,
463 i_media_object_offset, i_replicated_data_length, i_payload_data_length );
466 if( ( tk = p_sys->track[i_stream_number] ) == NULL )
469 "undeclared stream[Id 0x%x]", i_stream_number );
470 i_skip += i_payload_data_length;
471 continue; // over payload
476 i_skip += i_payload_data_length;
481 for( i_payload_data_pos = 0;
482 i_payload_data_pos < i_payload_data_length &&
483 i_packet_size_left > 0;
484 i_payload_data_pos += i_sub_payload_data_length )
489 // read sub payload length
490 if( i_replicated_data_length == 1 )
492 i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
493 i_payload_data_pos++;
497 i_sub_payload_data_length = i_payload_data_length;
500 /* FIXME I don't use i_media_object_number, sould I ? */
501 if( tk->p_frame && i_media_object_offset == 0 )
503 /* send complete packet to decoder */
504 block_t *p_gather = block_ChainGather( tk->p_frame );
506 es_out_Send( p_demux->out, tk->p_es, p_gather );
511 i_read = i_sub_payload_data_length + i_skip;
512 if( ( p_frag = stream_Block( p_demux->s, i_read ) ) == NULL )
514 msg_Warn( p_demux, "cannot read data" );
517 i_packet_size_left -= i_read;
519 p_frag->p_buffer += i_skip;
520 p_frag->i_buffer -= i_skip;
522 if( tk->p_frame == NULL )
525 ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
527 p_frag->i_pts = tk->i_time;
529 if( tk->i_cat != VIDEO_ES )
530 p_frag->i_dts = p_frag->i_pts;
533 p_frag->i_dts = p_frag->i_pts;
538 block_ChainAppend( &tk->p_frame, p_frag );
541 if( i_packet_size_left > 0 )
543 if( stream_Peek( p_demux->s, &p_peek, i_packet_size_left )
544 < i_packet_size_left )
546 msg_Warn( p_demux, "cannot peek, EOF ?" );
553 if( i_packet_size_left > 0 )
555 if( stream_Read( p_demux->s, NULL, i_packet_size_left )
556 < i_packet_size_left )
558 msg_Warn( p_demux, "cannot skip data, EOF ?" );
566 msg_Warn( p_demux, "unsupported packet header" );
567 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
569 msg_Err( p_demux, "unsupported packet header, fatal error" );
572 stream_Read( p_demux->s, NULL, i_data_packet_min );
577 /*****************************************************************************
579 *****************************************************************************/
580 static int DemuxInit( demux_t *p_demux )
582 demux_sys_t *p_sys = p_demux->p_sys;
583 vlc_bool_t b_seekable;
586 unsigned int i_stream;
587 asf_object_content_description_t *p_cd;
592 p_sys->i_bitrate = 0;
593 p_sys->p_root = NULL;
596 for( i = 0; i < 128; i++ )
598 p_sys->track[i] = NULL;
600 p_sys->i_data_begin = -1;
601 p_sys->i_data_end = -1;
604 /* Now load all object ( except raw data ) */
605 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
606 if( (p_sys->p_root = ASF_ReadObjectRoot( p_demux->s, b_seekable )) == NULL )
608 msg_Warn( p_demux, "ASF plugin discarded (not a valid file)" );
611 p_sys->p_fp = p_sys->p_root->p_fp;
613 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
615 msg_Warn( p_demux, "ASF plugin discarded (invalid file_properties object)" );
619 p_sys->i_track = ASF_CountObject( p_sys->p_root->p_hdr,
620 &asf_object_stream_properties_guid );
621 if( p_sys->i_track <= 0 )
623 msg_Warn( p_demux, "ASF plugin discarded (cannot find any stream!)" );
627 msg_Dbg( p_demux, "found %d streams", p_sys->i_track );
629 for( i_stream = 0; i_stream < p_sys->i_track; i_stream ++ )
632 asf_object_stream_properties_t *p_sp;
633 vlc_bool_t b_access_selected;
635 p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
636 &asf_object_stream_properties_guid,
639 tk = p_sys->track[p_sp->i_stream_number] = malloc( sizeof( asf_track_t ) );
640 memset( tk, 0, sizeof( asf_track_t ) );
647 /* Check (in case of mms) if this track is selected (ie will receive data) */
648 if( !stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_GET_PRIVATE_ID_STATE,
649 p_sp->i_stream_number, &b_access_selected ) &&
652 tk->i_cat = UNKNOWN_ES;
653 msg_Dbg( p_demux, "ignoring not selected stream(ID:%d) (by access)",
654 p_sp->i_stream_number );
658 if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
659 p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
662 uint8_t *p_data = p_sp->p_type_specific_data;
665 es_format_Init( &fmt, AUDIO_ES, 0 );
666 i_format = GetWLE( &p_data[0] );
667 wf_tag_to_fourcc( i_format, &fmt.i_codec, NULL );
668 fmt.audio.i_channels = GetWLE( &p_data[2] );
669 fmt.audio.i_rate = GetDWLE( &p_data[4] );
670 fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
671 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
672 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
674 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) &&
675 i_format != WAVE_FORMAT_MPEGLAYER3 &&
676 i_format != WAVE_FORMAT_MPEG )
678 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
679 p_sp->i_type_specific_data_length -
680 sizeof( WAVEFORMATEX ) );
681 fmt.p_extra = malloc( fmt.i_extra );
682 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )],
686 tk->i_cat = AUDIO_ES;
687 tk->p_es = es_out_Add( p_demux->out, &fmt );
688 es_format_Clean( &fmt );
690 msg_Dbg( p_demux, "added new audio stream(codec:0x%x,ID:%d)",
691 GetWLE( p_data ), p_sp->i_stream_number );
693 else if( ASF_CmpGUID( &p_sp->i_stream_type,
694 &asf_object_stream_type_video ) &&
695 p_sp->i_type_specific_data_length >= 11 +
696 sizeof( BITMAPINFOHEADER ) )
699 uint8_t *p_data = &p_sp->p_type_specific_data[11];
701 es_format_Init( &fmt, VIDEO_ES,
702 VLC_FOURCC( p_data[16], p_data[17],
703 p_data[18], p_data[19] ) );
704 fmt.video.i_width = GetDWLE( p_data + 4 );
705 fmt.video.i_height= GetDWLE( p_data + 8 );
707 if( p_sp->i_type_specific_data_length > 11 +
708 sizeof( BITMAPINFOHEADER ) )
710 fmt.i_extra = __MIN( GetDWLE( p_data ),
711 p_sp->i_type_specific_data_length - 11 -
712 sizeof( BITMAPINFOHEADER ) );
713 fmt.p_extra = malloc( fmt.i_extra );
714 memcpy( fmt.p_extra, &p_data[sizeof( BITMAPINFOHEADER )],
718 /* Look for an aspect ratio */
719 if( p_sys->p_root->p_metadata )
721 asf_object_metadata_t *p_meta = p_sys->p_root->p_metadata;
722 int i_aspect_x = 0, i_aspect_y = 0;
725 for( i = 0; i < p_meta->i_record_entries_count; i++ )
727 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioX" ) )
729 if( (!i_aspect_x && !p_meta->record[i].i_stream) ||
730 p_meta->record[i].i_stream ==
731 p_sp->i_stream_number )
732 i_aspect_x = p_meta->record[i].i_val;
734 if( !strcmp( p_meta->record[i].psz_name, "AspectRatioY" ) )
736 if( (!i_aspect_y && !p_meta->record[i].i_stream) ||
737 p_meta->record[i].i_stream ==
738 p_sp->i_stream_number )
739 i_aspect_y = p_meta->record[i].i_val;
743 if( i_aspect_x && i_aspect_y )
745 fmt.video.i_aspect = i_aspect_x *
746 (int64_t)fmt.video.i_width * VOUT_ASPECT_FACTOR /
747 fmt.video.i_height / i_aspect_y;
751 tk->i_cat = VIDEO_ES;
752 tk->p_es = es_out_Add( p_demux->out, &fmt );
753 es_format_Clean( &fmt );
755 msg_Dbg( p_demux, "added new video stream(ID:%d)",
756 p_sp->i_stream_number );
760 tk->i_cat = UNKNOWN_ES;
761 msg_Dbg( p_demux, "ignoring unknown stream(ID:%d)",
762 p_sp->i_stream_number );
766 p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
767 if( p_sys->p_root->p_data->i_object_size != 0 )
769 p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
770 p_sys->p_root->p_data->i_object_size;
773 { /* live/broacast */
774 p_sys->i_data_end = -1;
778 /* go to first packet */
779 stream_Seek( p_demux->s, p_sys->i_data_begin );
781 /* try to calculate movie time */
782 if( p_sys->p_fp->i_data_packets_count > 0 )
785 int64_t i_size = stream_Size( p_demux->s );
787 if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
789 i_size = p_sys->i_data_end;
792 /* real number of packets */
793 i_count = ( i_size - p_sys->i_data_begin ) /
794 p_sys->p_fp->i_min_data_packet_size;
796 /* calculate the time duration in micro-s */
797 p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
799 (mtime_t)p_sys->p_fp->i_data_packets_count;
801 if( p_sys->i_length > 0 )
803 p_sys->i_bitrate = 8 * i_size * (int64_t)1000000 / p_sys->i_length;
807 /* Create meta information */
808 p_sys->meta = vlc_meta_New();
810 if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
811 &asf_object_content_description_guid, 0 ) ) )
813 if( p_cd->psz_title && *p_cd->psz_title )
815 vlc_meta_Add( p_sys->meta, VLC_META_TITLE, p_cd->psz_title );
817 if( p_cd->psz_author && *p_cd->psz_author )
819 vlc_meta_Add( p_sys->meta, VLC_META_AUTHOR, p_cd->psz_author );
821 if( p_cd->psz_copyright && *p_cd->psz_copyright )
823 vlc_meta_Add( p_sys->meta, VLC_META_COPYRIGHT, p_cd->psz_copyright );
825 if( p_cd->psz_description && *p_cd->psz_description )
827 vlc_meta_Add( p_sys->meta, VLC_META_DESCRIPTION, p_cd->psz_description );
829 if( p_cd->psz_rating && *p_cd->psz_rating )
831 vlc_meta_Add( p_sys->meta, VLC_META_RATING, p_cd->psz_rating );
834 for( i_stream = 0, i = 0; i < 128; i++ )
836 asf_object_codec_list_t *p_cl = ASF_FindObject( p_sys->p_root->p_hdr,
837 &asf_object_codec_list_guid, 0 );
839 if( p_sys->track[i] )
841 vlc_meta_t *tk = vlc_meta_New();
842 TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, tk );
844 if( p_cl && i_stream < p_cl->i_codec_entries_count )
846 if( p_cl->codec[i_stream].psz_name &&
847 *p_cl->codec[i_stream].psz_name )
849 vlc_meta_Add( tk, VLC_META_CODEC_NAME,
850 p_cl->codec[i_stream].psz_name );
852 if( p_cl->codec[i_stream].psz_description &&
853 *p_cl->codec[i_stream].psz_description )
855 vlc_meta_Add( tk, VLC_META_CODEC_DESCRIPTION,
856 p_cl->codec[i_stream].psz_description );
863 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
867 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
870 /*****************************************************************************
872 *****************************************************************************/
873 static void DemuxEnd( demux_t *p_demux )
875 demux_sys_t *p_sys = p_demux->p_sys;
880 ASF_FreeObjectRoot( p_demux->s, p_sys->p_root );
881 p_sys->p_root = NULL;
885 vlc_meta_Delete( p_sys->meta );
889 for( i = 0; i < 128; i++ )
891 asf_track_t *tk = p_sys->track[i];
897 block_ChainRelease( tk->p_frame );
901 es_out_Del( p_demux->out, tk->p_es );