1 /*****************************************************************************
2 * asf.c : ASFv01 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: asf.c,v 1.48 2004/01/05 13:00:20 zorglub Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc/input.h>
32 #include "vlc_playlist.h"
34 #include "codecs.h" /* BITMAPINFOHEADER, WAVEFORMATEX */
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close ( vlc_object_t * );
44 set_description( _("ASF v1.0 demuxer") );
45 set_capability( "demux", 200 );
46 set_callbacks( Open, Close );
47 add_shortcut( "asf" );
51 /*****************************************************************************
53 *****************************************************************************/
54 static int Demux ( input_thread_t * );
56 typedef struct asf_stream_s
62 asf_object_stream_properties_t *p_sp;
66 block_t *p_frame; /* use to gather complete frame */
72 mtime_t i_time; /* µs */
73 mtime_t i_length; /* length of file file */
75 asf_object_root_t *p_root;
76 asf_object_file_properties_t *p_fp;
78 unsigned int i_streams;
79 asf_stream_t *stream[128];
85 static mtime_t GetMoviePTS( demux_sys_t * );
86 static int DemuxPacket( input_thread_t *, vlc_bool_t b_play_audio );
88 /*****************************************************************************
89 * Open: check file and initializes ASF structures
90 *****************************************************************************/
91 static int Open( vlc_object_t * p_this )
93 input_thread_t *p_input = (input_thread_t *)p_this;
99 unsigned int i_stream, i;
100 asf_object_content_description_t *p_cd;
102 vlc_bool_t b_seekable;
104 input_info_category_t *p_cat;
106 /* a little test to see if it could be a asf stream */
107 if( input_Peek( p_input, &p_peek, 16 ) < 16 )
109 msg_Warn( p_input, "ASF plugin discarded (cannot peek)" );
112 ASF_GetGUID( &guid, p_peek );
113 if( !ASF_CmpGUID( &guid, &asf_object_header_guid ) )
115 msg_Warn( p_input, "ASF plugin discarded (not a valid file)" );
119 /* Set p_input field */
120 p_input->pf_demux = Demux;
121 p_input->pf_demux_control = demux_vaControlDefault;
122 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
123 memset( p_sys, 0, sizeof( demux_sys_t ) );
127 /* Now load all object ( except raw data ) */
128 stream_Control( p_input->s, STREAM_CAN_FASTSEEK, &b_seekable );
129 if( (p_sys->p_root = ASF_ReadObjectRoot( p_input->s, b_seekable )) == NULL )
131 msg_Warn( p_input, "ASF plugin discarded (not a valid file)" );
135 p_sys->p_fp = p_sys->p_root->p_fp;
137 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
140 "ASF plugin discarded (invalid file_properties object)" );
144 p_sys->i_streams = ASF_CountObject( p_sys->p_root->p_hdr,
145 &asf_object_stream_properties_guid );
146 if( !p_sys->i_streams )
148 msg_Warn( p_input, "ASF plugin discarded (cannot find any stream!)" );
152 msg_Dbg( p_input, "found %d streams", p_sys->i_streams );
154 vlc_mutex_lock( &p_input->stream.stream_lock );
155 if( input_InitStream( p_input, 0 ) == -1)
157 vlc_mutex_unlock( &p_input->stream.stream_lock );
158 msg_Err( p_input, "cannot init stream" );
161 p_input->stream.i_mux_rate = 0 ; /* updated later */
162 vlc_mutex_unlock( &p_input->stream.stream_lock );
164 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream ++ )
166 asf_stream_t *p_stream;
167 asf_object_stream_properties_t *p_sp;
169 p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
170 &asf_object_stream_properties_guid,
174 p_sys->stream[p_sp->i_stream_number] =
175 malloc( sizeof( asf_stream_t ) );
176 memset( p_stream, 0, sizeof( asf_stream_t ) );
178 p_stream->i_time = -1;
179 p_stream->p_sp = p_sp;
180 p_stream->p_es = NULL;
181 p_stream->p_frame = NULL;
183 if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
184 p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
187 uint8_t *p_data = p_sp->p_type_specific_data;
189 es_format_Init( &fmt, AUDIO_ES, 0 );
190 wf_tag_to_fourcc( GetWLE( &p_data[0] ), &fmt.i_codec, NULL );
191 fmt.audio.i_channels = GetWLE( &p_data[2] );
192 fmt.audio.i_rate = GetDWLE( &p_data[4] );
193 fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
194 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
195 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
197 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) )
199 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
200 p_sp->i_type_specific_data_length - sizeof( WAVEFORMATEX ) );
201 fmt.p_extra = malloc( fmt.i_extra );
202 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )], fmt.i_extra );
205 p_stream->i_cat = AUDIO_ES;
206 p_stream->p_es = es_out_Add( p_input->p_es_out, &fmt );
209 "added new audio stream(codec:0x%x,ID:%d)",
210 GetWLE( p_data ), p_sp->i_stream_number );
212 else if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_video ) &&
213 p_sp->i_type_specific_data_length >= 11 + sizeof( BITMAPINFOHEADER ) )
216 uint8_t *p_data = &p_sp->p_type_specific_data[11];
218 es_format_Init( &fmt, VIDEO_ES,
219 VLC_FOURCC( p_data[16], p_data[17], p_data[18], p_data[19] ) );
220 fmt.video.i_width = GetDWLE( p_data + 4 );
221 fmt.video.i_height= GetDWLE( p_data + 8 );
223 if( p_sp->i_type_specific_data_length > 11 + sizeof( BITMAPINFOHEADER ) )
225 fmt.i_extra = __MIN( GetDWLE( p_data ),
226 p_sp->i_type_specific_data_length - 11 - sizeof( BITMAPINFOHEADER ) );
227 fmt.p_extra = malloc( fmt.i_extra );
228 memcpy( fmt.p_extra, &p_data[sizeof( BITMAPINFOHEADER )], fmt.i_extra );
231 p_stream->i_cat = VIDEO_ES;
232 p_stream->p_es = es_out_Add( p_input->p_es_out, &fmt );
234 msg_Dbg( p_input, "added new video stream(ID:%d)",
235 p_sp->i_stream_number );
239 p_stream->i_cat = UNKNOWN_ES;
240 msg_Dbg( p_input, "ignoring unknown stream(ID:%d)",
241 p_sp->i_stream_number );
245 p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
246 if( p_sys->p_root->p_data->i_object_size != 0 )
248 p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
249 p_sys->p_root->p_data->i_object_size;
252 { /* live/broacast */
253 p_sys->i_data_end = -1;
257 /* go to first packet */
258 stream_Seek( p_input->s, p_sys->i_data_begin );
260 /* try to calculate movie time */
261 if( p_sys->p_fp->i_data_packets_count > 0 )
264 int64_t i_size = stream_Size( p_input->s );
266 if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
268 i_size = p_sys->i_data_end;
271 /* real number of packets */
272 i_count = ( i_size - p_sys->i_data_begin ) /
273 p_sys->p_fp->i_min_data_packet_size;
275 /* calculate the time duration in micro-s */
276 p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
278 (mtime_t)p_sys->p_fp->i_data_packets_count;
280 if( p_sys->i_length > 0 )
282 p_input->stream.i_mux_rate =
283 i_size / 50 * (int64_t)1000000 / p_sys->i_length;
287 /* We add all info about this stream */
288 p_cat = input_InfoCategory( p_input, "Asf" );
289 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_input,
290 VLC_OBJECT_PLAYLIST, FIND_PARENT);
291 if( p_sys->i_length > 0 )
293 int64_t i_second = p_sys->i_length / (int64_t)1000000;
295 input_AddInfo( p_cat, _("Length"), "%d:%d:%d",
296 (int)(i_second / 36000),
297 (int)(( i_second / 60 ) % 60),
298 (int)(i_second % 60) );
301 playlist_AddInfo( p_playlist, -1, _("Asf"), _("Length"),
303 (int)(i_second / 36000),
304 (int)(( i_second / 60 ) % 60),
305 (int)(i_second % 60) );
308 input_AddInfo( p_cat, _("Number of streams"), "%d" , p_sys->i_streams );
311 playlist_AddInfo( p_playlist, -1, _("Asf"),_("Number of streams"),"%d",
315 if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
316 &asf_object_content_description_guid, 0 ) ) )
318 if( *p_cd->psz_title )
320 input_AddInfo( p_cat, _("Title"), p_cd->psz_title );
321 playlist_AddInfo( p_playlist, -1, _("Asf"),_("Title"),
323 playlist_SetName( p_playlist, -1, p_cd->psz_title );
325 if( p_cd->psz_author )
327 input_AddInfo( p_cat, _("Author"), p_cd->psz_author );
328 playlist_AddInfo( p_playlist, -1, _("Asf"),_("Author"),
330 playlist_AddInfo( p_playlist, -1, _("General"),_("Author"),
333 if( p_cd->psz_copyright )
335 input_AddInfo( p_cat, _("Copyright"), p_cd->psz_copyright );
336 playlist_AddInfo( p_playlist, -1, _("Asf"), _("Copyright"),
337 p_cd->psz_copyright );
339 if( *p_cd->psz_description )
341 input_AddInfo( p_cat, _("Description"), p_cd->psz_description );
342 playlist_AddInfo( p_playlist, -1, _("Asf"), _("Description"),
343 p_cd->psz_description );
345 if( *p_cd->psz_rating )
347 input_AddInfo( p_cat, _("Rating"), p_cd->psz_rating );
348 playlist_AddInfo( p_playlist, -1, _("Asf"), _("Rating"),
353 /* FIXME to port to new way */
354 for( i_stream = 0, i = 0; i < 128; i++ )
356 asf_object_codec_list_t *p_cl =
357 ASF_FindObject( p_sys->p_root->p_hdr,
358 &asf_object_codec_list_guid, 0 );
360 if( p_sys->stream[i] )
362 char *psz_cat = malloc( strlen(_("Stream")) + 10 );
363 sprintf( psz_cat, "%s %d", _("Stream"), i_stream );
364 p_cat = input_InfoCategory( p_input, psz_cat);
366 if( p_cl && i_stream < p_cl->i_codec_entries_count )
368 input_AddInfo( p_cat, _("Codec name"),
369 p_cl->codec[i_stream].psz_name );
370 playlist_AddInfo( p_playlist, -1, psz_cat, _("Codec name"),
371 p_cl->codec[i_stream].psz_name );
372 input_AddInfo( p_cat, _("Codec description"),
373 p_cl->codec[i_stream].psz_description );
374 playlist_AddInfo( p_playlist, -1, psz_cat,
375 _("Codec description"),
376 p_cl->codec[i_stream].psz_description );
383 if( p_playlist ) vlc_object_release( p_playlist );
387 ASF_FreeObjectRoot( p_input->s, p_sys->p_root );
393 /*****************************************************************************
394 * Demux: read packet and send them to decoders
395 *****************************************************************************/
396 static int Demux( input_thread_t *p_input )
398 demux_sys_t *p_sys = p_input->p_demux_data;
399 vlc_bool_t b_play_audio;
402 /* catch seek from user */
403 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
407 msleep( p_input->i_pts_delay );
409 i_offset = stream_Tell( p_input->s ) - p_sys->i_data_begin;
414 if( i_offset % p_sys->p_fp->i_min_data_packet_size > 0 )
416 i_offset -= i_offset % p_sys->p_fp->i_min_data_packet_size;
419 if( stream_Seek( p_input->s, i_offset + p_sys->i_data_begin ) )
421 msg_Warn( p_input, "cannot resynch after seek (EOF?)" );
426 for( i = 0; i < 128 ; i++ )
428 #define p_stream p_sys->stream[i]
431 p_stream->i_time = -1;
437 /* Check if we need to send the audio data to decoder */
438 b_play_audio = !p_input->stream.control.b_mute;
443 mtime_t i_time_begin = GetMoviePTS( p_sys );
451 if( ( i_result = DemuxPacket( p_input, b_play_audio ) ) <= 0 )
455 if( i_time_begin == -1 )
457 i_time_begin = GetMoviePTS( p_sys );
461 i_length = GetMoviePTS( p_sys ) - i_time_begin;
462 if( i_length < 0 || i_length >= 40 * 1000 )
469 p_sys->i_time = GetMoviePTS( p_sys );
470 if( p_sys->i_time >= 0 )
472 input_ClockManageRef( p_input,
473 p_input->stream.p_selected_program,
474 p_sys->i_time * 9 / 100 );
480 /*****************************************************************************
481 * Close: frees unused data
482 *****************************************************************************/
483 static void Close( vlc_object_t * p_this )
485 input_thread_t *p_input = (input_thread_t *)p_this;
486 demux_sys_t *p_sys = p_input->p_demux_data;
489 msg_Dbg( p_input, "Freeing all memory" );
491 ASF_FreeObjectRoot( p_input->s, p_sys->p_root );
492 for( i_stream = 0; i_stream < 128; i_stream++ )
494 asf_stream_t *p_stream = p_sys->stream[i_stream];
498 if( p_stream->p_frame )
500 block_ChainRelease( p_stream->p_frame );
509 /*****************************************************************************
511 *****************************************************************************/
512 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
518 for( i_stream = 0; i_stream < 128 ; i_stream++ )
520 asf_stream_t *p_stream = p_sys->stream[i_stream];
522 if( p_stream && p_stream->p_es && p_stream->i_time > 0)
526 i_time = p_stream->i_time;
530 i_time = __MIN( i_time, p_stream->i_time );
538 #define GETVALUE2b( bits, var, def ) \
539 switch( (bits)&0x03 ) \
541 case 1: var = p_peek[i_skip]; i_skip++; break; \
542 case 2: var = GetWLE( p_peek + i_skip ); i_skip+= 2; break; \
543 case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
545 default: var = def; break;\
548 static int DemuxPacket( input_thread_t *p_input, vlc_bool_t b_play_audio )
550 demux_sys_t *p_sys = p_input->p_demux_data;
551 int i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
555 int i_packet_size_left;
557 int i_packet_property;
559 int b_packet_multiple_payload;
561 int i_packet_sequence;
562 int i_packet_padding_length;
564 uint32_t i_packet_send_time;
565 uint16_t i_packet_duration;
568 int i_payload_length_type;
571 if( stream_Peek( p_input->s, &p_peek,i_data_packet_min)<i_data_packet_min )
574 msg_Warn( p_input, "cannot peek while getting new packet, EOF ?" );
579 /* *** parse error correction if present *** */
582 unsigned int i_error_correction_length_type;
583 unsigned int i_error_correction_data_length;
584 unsigned int i_opaque_data_present;
586 i_error_correction_data_length = p_peek[0] & 0x0f; // 4bits
587 i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01; // 1bit
588 i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
589 i_skip += 1; // skip error correction flags
591 if( i_error_correction_length_type != 0x00 ||
592 i_opaque_data_present != 0 ||
593 i_error_correction_data_length != 0x02 )
595 goto loop_error_recovery;
598 i_skip += i_error_correction_data_length;
602 msg_Warn( p_input, "p_peek[0]&0x80 != 0x80" );
606 if( i_skip + 2 >= i_data_packet_min )
608 goto loop_error_recovery;
611 i_packet_flags = p_peek[i_skip]; i_skip++;
612 i_packet_property = p_peek[i_skip]; i_skip++;
614 b_packet_multiple_payload = i_packet_flags&0x01;
616 /* read some value */
617 GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
618 GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
619 GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
621 i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
622 i_packet_duration = GetWLE( p_peek + i_skip ); i_skip += 2;
624 // i_packet_size_left = i_packet_length; // XXX données reellement lu
625 /* FIXME I have to do that for some file, I don't known why */
626 i_packet_size_left = i_data_packet_min;
628 if( b_packet_multiple_payload )
630 i_payload_count = p_peek[i_skip] & 0x3f;
631 i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
637 i_payload_length_type = 0x02; // unused
640 for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
642 asf_stream_t *p_stream;
645 int i_media_object_number;
646 int i_media_object_offset;
647 int i_replicated_data_length;
648 int i_payload_data_length;
649 int i_payload_data_pos;
650 int i_sub_payload_data_length;
656 if( i_skip >= i_packet_size_left )
658 /* prevent some segfault with invalid file */
662 i_stream_number = p_peek[i_skip] & 0x7f;
665 GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
666 GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
667 GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
669 if( i_replicated_data_length > 1 ) // should be at least 8 bytes
671 i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
672 i_skip += i_replicated_data_length;
675 i_media_object_offset = i_tmp;
677 if( i_skip >= i_packet_size_left )
682 else if( i_replicated_data_length == 1 )
685 msg_Dbg( p_input, "found compressed payload" );
687 i_pts = (mtime_t)i_tmp * 1000;
688 i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
690 i_media_object_offset = 0;
694 i_pts = (mtime_t)i_packet_send_time * 1000;
697 i_media_object_offset = i_tmp;
700 i_pts = __MAX( i_pts - p_sys->p_fp->i_preroll * 1000, 0 );
701 if( b_packet_multiple_payload )
703 GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
707 i_payload_data_length = i_packet_length -
708 i_packet_padding_length - i_skip;
711 if( i_payload_data_length < 0 || i_skip + i_payload_data_length > i_packet_size_left )
718 "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
722 i_media_object_number,
723 i_media_object_offset,
724 i_replicated_data_length,
725 i_payload_data_length );
728 if( !( p_stream = p_sys->stream[i_stream_number] ) )
731 "undeclared stream[Id 0x%x]", i_stream_number );
732 i_skip += i_payload_data_length;
733 continue; // over payload
736 if( !p_stream->p_es )
738 i_skip += i_payload_data_length;
743 for( i_payload_data_pos = 0;
744 i_payload_data_pos < i_payload_data_length &&
745 i_packet_size_left > 0;
746 i_payload_data_pos += i_sub_payload_data_length )
751 // read sub payload length
752 if( i_replicated_data_length == 1 )
754 i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
755 i_payload_data_pos++;
759 i_sub_payload_data_length = i_payload_data_length;
762 /* FIXME I don't use i_media_object_number, sould I ? */
763 if( p_stream->p_frame && i_media_object_offset == 0 )
765 /* send complete packet to decoder */
766 block_t *p_gather = block_ChainGather( p_stream->p_frame );
768 es_out_Send( p_input->p_es_out, p_stream->p_es, p_gather );
770 p_stream->p_frame = NULL;
773 i_read = i_sub_payload_data_length + i_skip;
774 if( ( p_frag = stream_Block( p_input->s, i_read ) ) == NULL )
776 msg_Warn( p_input, "cannot read data" );
779 i_packet_size_left -= i_read;
781 p_frag->p_buffer += i_skip;
782 p_frag->i_buffer -= i_skip;
784 if( p_stream->p_frame == NULL )
787 ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
790 input_ClockGetTS( p_input,
791 p_input->stream.p_selected_program,
792 p_stream->i_time * 9 /100 );
794 if( p_stream->i_cat != VIDEO_ES )
795 p_frag->i_dts = p_frag->i_pts;
798 p_frag->i_dts = p_frag->i_pts;
803 block_ChainAppend( &p_stream->p_frame, p_frag );
806 if( i_packet_size_left > 0 )
808 if( stream_Peek( p_input->s, &p_peek, i_packet_size_left )
809 < i_packet_size_left )
812 msg_Warn( p_input, "cannot peek, EOF ?" );
819 if( i_packet_size_left > 0 )
821 if( stream_Read( p_input->s, NULL, i_packet_size_left )
822 < i_packet_size_left )
824 msg_Warn( p_input, "cannot skip data, EOF ?" );
832 msg_Warn( p_input, "unsupported packet header" );
833 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
835 msg_Err( p_input, "unsupported packet header, fatal error" );
838 stream_Read( p_input->s, NULL, i_data_packet_min );