1 /*****************************************************************************
2 * asf.c : ASFv01 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: asf.c,v 1.43 2003/11/20 22:10:56 fenrir 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 "codecs.h" /* BITMAPINFOHEADER, WAVEFORMATEX */
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Open ( vlc_object_t * );
39 static void Close ( vlc_object_t * );
42 set_description( _("ASF v1.0 demuxer") );
43 set_capability( "demux", 200 );
44 set_callbacks( Open, Close );
45 add_shortcut( "asf" );
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Demux ( input_thread_t * );
54 typedef struct asf_stream_s
60 asf_object_stream_properties_t *p_sp;
64 pes_packet_t *p_pes; /* used to keep uncomplete frames */
70 mtime_t i_time; /* µs */
71 mtime_t i_length; /* length of file file */
73 asf_object_root_t *p_root;
74 asf_object_file_properties_t *p_fp;
76 unsigned int i_streams;
77 asf_stream_t *stream[128];
83 static mtime_t GetMoviePTS( demux_sys_t * );
84 static int DemuxPacket( input_thread_t *, vlc_bool_t b_play_audio );
86 /*****************************************************************************
87 * Open: check file and initializes ASF structures
88 *****************************************************************************/
89 static int Open( vlc_object_t * p_this )
91 input_thread_t *p_input = (input_thread_t *)p_this;
97 unsigned int i_stream, i;
98 asf_object_content_description_t *p_cd;
100 vlc_bool_t b_seekable;
102 input_info_category_t *p_cat;
104 /* a little test to see if it could be a asf stream */
105 if( input_Peek( p_input, &p_peek, 16 ) < 16 )
107 msg_Warn( p_input, "ASF plugin discarded (cannot peek)" );
110 ASF_GetGUID( &guid, p_peek );
111 if( !ASF_CmpGUID( &guid, &asf_object_header_guid ) )
113 msg_Warn( p_input, "ASF plugin discarded (not a valid file)" );
117 /* Set p_input field */
118 p_input->pf_demux = Demux;
119 p_input->pf_demux_control = demux_vaControlDefault;
120 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
121 memset( p_sys, 0, sizeof( demux_sys_t ) );
125 /* Now load all object ( except raw data ) */
126 stream_Control( p_input->s, STREAM_CAN_FASTSEEK, &b_seekable );
127 if( (p_sys->p_root = ASF_ReadObjectRoot( p_input->s, b_seekable )) == NULL )
129 msg_Warn( p_input, "ASF plugin discarded (not a valid file)" );
133 p_sys->p_fp = p_sys->p_root->p_fp;
135 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
138 "ASF plugin discarded (invalid file_properties object)" );
142 p_sys->i_streams = ASF_CountObject( p_sys->p_root->p_hdr,
143 &asf_object_stream_properties_guid );
144 if( !p_sys->i_streams )
146 msg_Warn( p_input, "ASF plugin discarded (cannot find any stream!)" );
150 msg_Dbg( p_input, "found %d streams", p_sys->i_streams );
152 vlc_mutex_lock( &p_input->stream.stream_lock );
153 if( input_InitStream( p_input, 0 ) == -1)
155 vlc_mutex_unlock( &p_input->stream.stream_lock );
156 msg_Err( p_input, "cannot init stream" );
159 p_input->stream.i_mux_rate = 0 ; /* updated later */
160 vlc_mutex_unlock( &p_input->stream.stream_lock );
162 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream ++ )
164 asf_stream_t *p_stream;
165 asf_object_stream_properties_t *p_sp;
167 p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
168 &asf_object_stream_properties_guid,
172 p_sys->stream[p_sp->i_stream_number] =
173 malloc( sizeof( asf_stream_t ) );
174 memset( p_stream, 0, sizeof( asf_stream_t ) );
176 p_stream->i_time = -1;
177 p_stream->p_sp = p_sp;
178 p_stream->p_es = NULL;
180 if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
181 p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
184 uint8_t *p_data = p_sp->p_type_specific_data;
186 es_format_Init( &fmt, AUDIO_ES, 0 );
187 wf_tag_to_fourcc( GetWLE( &p_data[0] ), &fmt.i_codec, NULL );
188 fmt.audio.i_channels = GetWLE( &p_data[2] );
189 fmt.audio.i_rate = GetDWLE( &p_data[4] );
190 fmt.i_bitrate = GetDWLE( &p_data[8] ) * 8;
191 fmt.audio.i_blockalign = GetWLE( &p_data[12] );
192 fmt.audio.i_bitspersample = GetWLE( &p_data[14] );
194 if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) )
196 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
197 p_sp->i_type_specific_data_length - sizeof( WAVEFORMATEX ) );
198 fmt.p_extra = malloc( fmt.i_extra );
199 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )], fmt.i_extra );
202 p_stream->i_cat = AUDIO_ES;
203 p_stream->p_es = es_out_Add( p_input->p_es_out, &fmt );
206 "added new audio stream(codec:0x%x,ID:%d)",
207 GetWLE( p_data ), p_sp->i_stream_number );
209 else if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_video ) &&
210 p_sp->i_type_specific_data_length >= 11 + sizeof( BITMAPINFOHEADER ) )
213 uint8_t *p_data = &p_sp->p_type_specific_data[11];
215 es_format_Init( &fmt, VIDEO_ES,
216 VLC_FOURCC( p_data[16], p_data[17], p_data[18], p_data[19] ) );
217 fmt.video.i_width = GetDWLE( p_data + 4 );
218 fmt.video.i_height= GetDWLE( p_data + 8 );
220 if( p_sp->i_type_specific_data_length > 11 + sizeof( BITMAPINFOHEADER ) )
222 fmt.i_extra = __MIN( GetDWLE( p_data ),
223 p_sp->i_type_specific_data_length - 11 - sizeof( BITMAPINFOHEADER ) );
224 fmt.p_extra = malloc( fmt.i_extra );
225 memcpy( fmt.p_extra, &p_data[sizeof( BITMAPINFOHEADER )], fmt.i_extra );
228 p_stream->i_cat = VIDEO_ES;
229 p_stream->p_es = es_out_Add( p_input->p_es_out, &fmt );
231 msg_Dbg( p_input, "added new video stream(ID:%d)",
232 p_sp->i_stream_number );
236 p_stream->i_cat = UNKNOWN_ES;
237 msg_Dbg( p_input, "ignoring unknown stream(ID:%d)",
238 p_sp->i_stream_number );
242 p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
243 if( p_sys->p_root->p_data->i_object_size != 0 )
245 p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
246 p_sys->p_root->p_data->i_object_size;
249 { /* live/broacast */
250 p_sys->i_data_end = -1;
254 /* go to first packet */
255 stream_Seek( p_input->s, p_sys->i_data_begin );
257 /* try to calculate movie time */
258 if( p_sys->p_fp->i_data_packets_count > 0 )
261 int64_t i_size = stream_Size( p_input->s );
263 if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
265 i_size = p_sys->i_data_end;
268 /* real number of packets */
269 i_count = ( i_size - p_sys->i_data_begin ) /
270 p_sys->p_fp->i_min_data_packet_size;
272 /* calculate the time duration in micro-s */
273 p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
275 (mtime_t)p_sys->p_fp->i_data_packets_count;
277 if( p_sys->i_length > 0 )
279 p_input->stream.i_mux_rate =
280 i_size / 50 * (int64_t)1000000 / p_sys->i_length;
284 /* We add all info about this stream */
285 p_cat = input_InfoCategory( p_input, "Asf" );
286 if( p_sys->i_length > 0 )
288 int64_t i_second = p_sys->i_length / (int64_t)1000000;
290 input_AddInfo( p_cat, _("Length"), "%d:%d:%d",
291 (int)(i_second / 36000),
292 (int)(( i_second / 60 ) % 60),
293 (int)(i_second % 60) );
295 input_AddInfo( p_cat, _("Number of streams"), "%d" , p_sys->i_streams );
297 if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
298 &asf_object_content_description_guid, 0 ) ) )
300 if( *p_cd->psz_title )
301 input_AddInfo( p_cat, _("Title"), p_cd->psz_title );
302 if( p_cd->psz_author )
303 input_AddInfo( p_cat, _("Author"), p_cd->psz_author );
304 if( p_cd->psz_copyright )
305 input_AddInfo( p_cat, _("Copyright"), p_cd->psz_copyright );
306 if( *p_cd->psz_description )
307 input_AddInfo( p_cat, _("Description"), p_cd->psz_description );
308 if( *p_cd->psz_rating )
309 input_AddInfo( p_cat, _("Rating"), p_cd->psz_rating );
312 /* FIXME to port to new way */
313 for( i_stream = 0, i = 0; i < 128; i++ )
315 asf_object_codec_list_t *p_cl =
316 ASF_FindObject( p_sys->p_root->p_hdr,
317 &asf_object_codec_list_guid, 0 );
319 if( p_sys->stream[i] )
321 char psz_cat[sizeof(_("Stream "))+10];
322 sprintf( psz_cat, _("Stream %d"), i_stream );
323 p_cat = input_InfoCategory( p_input, psz_cat);
325 if( p_cl && i_stream < p_cl->i_codec_entries_count )
327 input_AddInfo( p_cat, _("Codec name"),
328 p_cl->codec[i_stream].psz_name );
329 input_AddInfo( p_cat, _("Codec description"),
330 p_cl->codec[i_stream].psz_description );
339 ASF_FreeObjectRoot( p_input->s, p_sys->p_root );
345 /*****************************************************************************
346 * Demux: read packet and send them to decoders
347 *****************************************************************************/
348 static int Demux( input_thread_t *p_input )
350 demux_sys_t *p_sys = p_input->p_demux_data;
351 vlc_bool_t b_play_audio;
354 /* catch seek from user */
355 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
359 msleep( p_input->i_pts_delay );
361 i_offset = stream_Tell( p_input->s ) - p_sys->i_data_begin;
366 if( i_offset % p_sys->p_fp->i_min_data_packet_size > 0 )
368 i_offset -= i_offset % p_sys->p_fp->i_min_data_packet_size;
371 if( stream_Seek( p_input->s, i_offset + p_sys->i_data_begin ) )
373 msg_Warn( p_input, "cannot resynch after seek (EOF?)" );
378 for( i = 0; i < 128 ; i++ )
380 #define p_stream p_sys->stream[i]
383 p_stream->i_time = -1;
389 /* Check if we need to send the audio data to decoder */
390 b_play_audio = !p_input->stream.control.b_mute;
395 mtime_t i_time_begin = GetMoviePTS( p_sys );
403 if( ( i_result = DemuxPacket( p_input, b_play_audio ) ) <= 0 )
407 if( i_time_begin == -1 )
409 i_time_begin = GetMoviePTS( p_sys );
413 i_length = GetMoviePTS( p_sys ) - i_time_begin;
414 if( i_length < 0 || i_length >= 40 * 1000 )
421 p_sys->i_time = GetMoviePTS( p_sys );
422 if( p_sys->i_time >= 0 )
424 input_ClockManageRef( p_input,
425 p_input->stream.p_selected_program,
426 p_sys->i_time * 9 / 100 );
432 /*****************************************************************************
433 * Close: frees unused data
434 *****************************************************************************/
435 static void Close( vlc_object_t * p_this )
437 input_thread_t *p_input = (input_thread_t *)p_this;
438 demux_sys_t *p_sys = p_input->p_demux_data;
441 msg_Dbg( p_input, "Freeing all memory" );
443 ASF_FreeObjectRoot( p_input->s, p_sys->p_root );
444 for( i_stream = 0; i_stream < 128; i_stream++ )
446 #define p_stream p_sys->stream[i_stream]
449 if( p_stream->p_pes )
451 input_DeletePES( p_input->p_method_data, p_stream->p_pes );
461 /*****************************************************************************
463 *****************************************************************************/
464 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
470 for( i_stream = 0; i_stream < 128 ; i_stream++ )
472 #define p_stream p_sys->stream[i_stream]
473 if( p_stream && p_stream->p_es && p_stream->i_time > 0)
477 i_time = p_stream->i_time;
481 i_time = __MIN( i_time, p_stream->i_time );
490 #define GETVALUE2b( bits, var, def ) \
491 switch( (bits)&0x03 ) \
493 case 1: var = p_peek[i_skip]; i_skip++; break; \
494 case 2: var = GetWLE( p_peek + i_skip ); i_skip+= 2; break; \
495 case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
497 default: var = def; break;\
500 static int DemuxPacket( input_thread_t *p_input, vlc_bool_t b_play_audio )
502 demux_sys_t *p_sys = p_input->p_demux_data;
503 int i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
507 int i_packet_size_left;
509 int i_packet_property;
511 int b_packet_multiple_payload;
513 int i_packet_sequence;
514 int i_packet_padding_length;
516 uint32_t i_packet_send_time;
517 uint16_t i_packet_duration;
520 int i_payload_length_type;
523 if( stream_Peek( p_input->s, &p_peek,i_data_packet_min)<i_data_packet_min )
526 msg_Warn( p_input, "cannot peek while getting new packet, EOF ?" );
531 /* *** parse error correction if present *** */
534 unsigned int i_error_correction_length_type;
535 unsigned int i_error_correction_data_length;
536 unsigned int i_opaque_data_present;
538 i_error_correction_data_length = p_peek[0] & 0x0f; // 4bits
539 i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01; // 1bit
540 i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
541 i_skip += 1; // skip error correction flags
543 if( i_error_correction_length_type != 0x00 ||
544 i_opaque_data_present != 0 ||
545 i_error_correction_data_length != 0x02 )
547 goto loop_error_recovery;
550 i_skip += i_error_correction_data_length;
554 msg_Warn( p_input, "p_peek[0]&0x80 != 0x80" );
558 if( i_skip + 2 >= i_data_packet_min )
560 goto loop_error_recovery;
563 i_packet_flags = p_peek[i_skip]; i_skip++;
564 i_packet_property = p_peek[i_skip]; i_skip++;
566 b_packet_multiple_payload = i_packet_flags&0x01;
568 /* read some value */
569 GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
570 GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
571 GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
573 i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
574 i_packet_duration = GetWLE( p_peek + i_skip ); i_skip += 2;
576 // i_packet_size_left = i_packet_length; // XXX données reellement lu
577 /* FIXME I have to do that for some file, I don't known why */
578 i_packet_size_left = i_data_packet_min;
580 if( b_packet_multiple_payload )
582 i_payload_count = p_peek[i_skip] & 0x3f;
583 i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
589 i_payload_length_type = 0x02; // unused
592 for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
594 asf_stream_t *p_stream;
597 int i_media_object_number;
598 int i_media_object_offset;
599 int i_replicated_data_length;
600 int i_payload_data_length;
601 int i_payload_data_pos;
602 int i_sub_payload_data_length;
608 if( i_skip >= i_packet_size_left )
610 /* prevent some segfault with invalid file */
614 i_stream_number = p_peek[i_skip] & 0x7f;
617 GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
618 GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
619 GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
621 if( i_replicated_data_length > 1 ) // should be at least 8 bytes
623 i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
624 i_skip += i_replicated_data_length;
627 i_media_object_offset = i_tmp;
629 if( i_skip >= i_packet_size_left )
634 else if( i_replicated_data_length == 1 )
637 msg_Dbg( p_input, "found compressed payload" );
639 i_pts = (mtime_t)i_tmp * 1000;
640 i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
642 i_media_object_offset = 0;
646 i_pts = (mtime_t)i_packet_send_time * 1000;
649 i_media_object_offset = i_tmp;
652 i_pts = __MAX( i_pts - p_sys->p_fp->i_preroll * 1000, 0 );
653 if( b_packet_multiple_payload )
655 GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
659 i_payload_data_length = i_packet_length -
660 i_packet_padding_length - i_skip;
663 if( i_payload_data_length < 0 || i_skip + i_payload_data_length > i_packet_size_left )
670 "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
674 i_media_object_number,
675 i_media_object_offset,
676 i_replicated_data_length,
677 i_payload_data_length );
680 if( !( p_stream = p_sys->stream[i_stream_number] ) )
683 "undeclared stream[Id 0x%x]", i_stream_number );
684 i_skip += i_payload_data_length;
685 continue; // over payload
688 if( !p_stream->p_es )
690 i_skip += i_payload_data_length;
695 for( i_payload_data_pos = 0;
696 i_payload_data_pos < i_payload_data_length &&
697 i_packet_size_left > 0;
698 i_payload_data_pos += i_sub_payload_data_length )
700 data_packet_t *p_data;
702 // read sub payload length
703 if( i_replicated_data_length == 1 )
705 i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
706 i_payload_data_pos++;
710 i_sub_payload_data_length = i_payload_data_length;
713 /* FIXME I don't use i_media_object_number, sould I ? */
714 if( p_stream->p_pes && i_media_object_offset == 0 )
716 /* send complete packet to decoder */
717 if( p_stream->p_pes->i_pes_size > 0 )
719 es_out_Send( p_input->p_es_out, p_stream->p_es, p_stream->p_pes );
720 p_stream->p_pes = NULL;
724 if( !p_stream->p_pes ) // add a new PES
727 ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
729 p_stream->p_pes = input_NewPES( p_input->p_method_data );
730 p_stream->p_pes->i_dts =
731 p_stream->p_pes->i_pts =
732 input_ClockGetTS( p_input,
733 p_input->stream.p_selected_program,
734 p_stream->i_time * 9 /100 );
736 //msg_Err( p_input, "stream[0x%2x] pts=%lld", i_stream_number, p_stream->p_pes->i_pts );
737 p_stream->p_pes->p_next = NULL;
738 p_stream->p_pes->i_nb_data = 0;
739 p_stream->p_pes->i_pes_size = 0;
742 i_read = i_sub_payload_data_length + i_skip;
743 if((p_data = stream_DataPacket( p_input->s,i_read,VLC_TRUE))==NULL)
745 msg_Warn( p_input, "cannot read data" );
748 p_data->p_payload_start += i_skip;
749 i_packet_size_left -= i_read;
752 if( !p_stream->p_pes->p_first )
754 p_stream->p_pes->p_first = p_stream->p_pes->p_last = p_data;
758 p_stream->p_pes->p_last->p_next = p_data;
759 p_stream->p_pes->p_last = p_data;
761 p_stream->p_pes->i_pes_size += i_sub_payload_data_length;
762 p_stream->p_pes->i_nb_data++;
765 if( i_packet_size_left > 0 )
767 if( stream_Peek( p_input->s, &p_peek, i_packet_size_left )
768 < i_packet_size_left )
771 msg_Warn( p_input, "cannot peek, EOF ?" );
778 if( i_packet_size_left > 0 )
780 if( stream_Read( p_input->s, NULL, i_packet_size_left )
781 < i_packet_size_left )
783 msg_Warn( p_input, "cannot skip data, EOF ?" );
791 msg_Warn( p_input, "unsupported packet header" );
792 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
794 msg_Err( p_input, "unsupported packet header, fatal error" );
797 stream_Read( p_input->s, NULL, i_data_packet_min );