1 /*****************************************************************************
2 * asf.c : ASFv01 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: asf.c,v 1.42 2003/11/16 22:54:12 gbazin 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_type = ES_EXTRA_TYPE_WAVEFORMATEX;
197 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
198 p_sp->i_type_specific_data_length - sizeof( WAVEFORMATEX ) );
199 fmt.p_extra = malloc( fmt.i_extra );
200 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )], fmt.i_extra );
203 p_stream->i_cat = AUDIO_ES;
204 p_stream->p_es = es_out_Add( p_input->p_es_out, &fmt );
207 "added new audio stream(codec:0x%x,ID:%d)",
208 GetWLE( p_data ), p_sp->i_stream_number );
210 else if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_video ) &&
211 p_sp->i_type_specific_data_length >= 11 + sizeof( BITMAPINFOHEADER ) )
214 uint8_t *p_data = &p_sp->p_type_specific_data[11];
216 es_format_Init( &fmt, VIDEO_ES,
217 VLC_FOURCC( p_data[16], p_data[17], p_data[18], p_data[19] ) );
218 fmt.video.i_width = GetDWLE( p_data + 4 );
219 fmt.video.i_height= GetDWLE( p_data + 8 );
221 if( p_sp->i_type_specific_data_length > 11 + sizeof( BITMAPINFOHEADER ) )
223 fmt.i_extra_type = ES_EXTRA_TYPE_BITMAPINFOHEADER;
224 fmt.i_extra = __MIN( GetDWLE( p_data ),
225 p_sp->i_type_specific_data_length - 11 - sizeof( BITMAPINFOHEADER ) );
226 fmt.p_extra = malloc( fmt.i_extra );
227 memcpy( fmt.p_extra, &p_data[sizeof( BITMAPINFOHEADER )], fmt.i_extra );
230 p_stream->i_cat = VIDEO_ES;
231 p_stream->p_es = es_out_Add( p_input->p_es_out, &fmt );
233 msg_Dbg( p_input, "added new video stream(ID:%d)",
234 p_sp->i_stream_number );
238 p_stream->i_cat = UNKNOWN_ES;
239 msg_Dbg( p_input, "ignoring unknown stream(ID:%d)",
240 p_sp->i_stream_number );
244 p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
245 if( p_sys->p_root->p_data->i_object_size != 0 )
247 p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
248 p_sys->p_root->p_data->i_object_size;
251 { /* live/broacast */
252 p_sys->i_data_end = -1;
256 /* go to first packet */
257 stream_Seek( p_input->s, p_sys->i_data_begin );
259 /* try to calculate movie time */
260 if( p_sys->p_fp->i_data_packets_count > 0 )
263 int64_t i_size = stream_Size( p_input->s );
265 if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
267 i_size = p_sys->i_data_end;
270 /* real number of packets */
271 i_count = ( i_size - p_sys->i_data_begin ) /
272 p_sys->p_fp->i_min_data_packet_size;
274 /* calculate the time duration in micro-s */
275 p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
277 (mtime_t)p_sys->p_fp->i_data_packets_count;
279 if( p_sys->i_length > 0 )
281 p_input->stream.i_mux_rate =
282 i_size / 50 * (int64_t)1000000 / p_sys->i_length;
286 /* We add all info about this stream */
287 p_cat = input_InfoCategory( p_input, "Asf" );
288 if( p_sys->i_length > 0 )
290 int64_t i_second = p_sys->i_length / (int64_t)1000000;
292 input_AddInfo( p_cat, _("Length"), "%d:%d:%d",
293 (int)(i_second / 36000),
294 (int)(( i_second / 60 ) % 60),
295 (int)(i_second % 60) );
297 input_AddInfo( p_cat, _("Number of streams"), "%d" , p_sys->i_streams );
299 if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
300 &asf_object_content_description_guid, 0 ) ) )
302 if( *p_cd->psz_title )
303 input_AddInfo( p_cat, _("Title"), p_cd->psz_title );
304 if( p_cd->psz_author )
305 input_AddInfo( p_cat, _("Author"), p_cd->psz_author );
306 if( p_cd->psz_copyright )
307 input_AddInfo( p_cat, _("Copyright"), p_cd->psz_copyright );
308 if( *p_cd->psz_description )
309 input_AddInfo( p_cat, _("Description"), p_cd->psz_description );
310 if( *p_cd->psz_rating )
311 input_AddInfo( p_cat, _("Rating"), p_cd->psz_rating );
314 /* FIXME to port to new way */
315 for( i_stream = 0, i = 0; i < 128; i++ )
317 asf_object_codec_list_t *p_cl =
318 ASF_FindObject( p_sys->p_root->p_hdr,
319 &asf_object_codec_list_guid, 0 );
321 if( p_sys->stream[i] )
323 char psz_cat[sizeof(_("Stream "))+10];
324 sprintf( psz_cat, _("Stream %d"), i_stream );
325 p_cat = input_InfoCategory( p_input, psz_cat);
327 if( p_cl && i_stream < p_cl->i_codec_entries_count )
329 input_AddInfo( p_cat, _("Codec name"),
330 p_cl->codec[i_stream].psz_name );
331 input_AddInfo( p_cat, _("Codec description"),
332 p_cl->codec[i_stream].psz_description );
341 ASF_FreeObjectRoot( p_input->s, p_sys->p_root );
347 /*****************************************************************************
348 * Demux: read packet and send them to decoders
349 *****************************************************************************/
350 static int Demux( input_thread_t *p_input )
352 demux_sys_t *p_sys = p_input->p_demux_data;
353 vlc_bool_t b_play_audio;
356 /* catch seek from user */
357 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
361 msleep( p_input->i_pts_delay );
363 i_offset = stream_Tell( p_input->s ) - p_sys->i_data_begin;
368 if( i_offset % p_sys->p_fp->i_min_data_packet_size > 0 )
370 i_offset -= i_offset % p_sys->p_fp->i_min_data_packet_size;
373 if( stream_Seek( p_input->s, i_offset + p_sys->i_data_begin ) )
375 msg_Warn( p_input, "cannot resynch after seek (EOF?)" );
380 for( i = 0; i < 128 ; i++ )
382 #define p_stream p_sys->stream[i]
385 p_stream->i_time = -1;
391 /* Check if we need to send the audio data to decoder */
392 b_play_audio = !p_input->stream.control.b_mute;
397 mtime_t i_time_begin = GetMoviePTS( p_sys );
405 if( ( i_result = DemuxPacket( p_input, b_play_audio ) ) <= 0 )
409 if( i_time_begin == -1 )
411 i_time_begin = GetMoviePTS( p_sys );
415 i_length = GetMoviePTS( p_sys ) - i_time_begin;
416 if( i_length < 0 || i_length >= 40 * 1000 )
423 p_sys->i_time = GetMoviePTS( p_sys );
424 if( p_sys->i_time >= 0 )
426 input_ClockManageRef( p_input,
427 p_input->stream.p_selected_program,
428 p_sys->i_time * 9 / 100 );
434 /*****************************************************************************
435 * Close: frees unused data
436 *****************************************************************************/
437 static void Close( vlc_object_t * p_this )
439 input_thread_t *p_input = (input_thread_t *)p_this;
440 demux_sys_t *p_sys = p_input->p_demux_data;
443 msg_Dbg( p_input, "Freeing all memory" );
445 ASF_FreeObjectRoot( p_input->s, p_sys->p_root );
446 for( i_stream = 0; i_stream < 128; i_stream++ )
448 #define p_stream p_sys->stream[i_stream]
451 if( p_stream->p_pes )
453 input_DeletePES( p_input->p_method_data, p_stream->p_pes );
463 /*****************************************************************************
465 *****************************************************************************/
466 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
472 for( i_stream = 0; i_stream < 128 ; i_stream++ )
474 #define p_stream p_sys->stream[i_stream]
475 if( p_stream && p_stream->p_es && p_stream->i_time > 0)
479 i_time = p_stream->i_time;
483 i_time = __MIN( i_time, p_stream->i_time );
492 #define GETVALUE2b( bits, var, def ) \
493 switch( (bits)&0x03 ) \
495 case 1: var = p_peek[i_skip]; i_skip++; break; \
496 case 2: var = GetWLE( p_peek + i_skip ); i_skip+= 2; break; \
497 case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
499 default: var = def; break;\
502 static int DemuxPacket( input_thread_t *p_input, vlc_bool_t b_play_audio )
504 demux_sys_t *p_sys = p_input->p_demux_data;
505 int i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
509 int i_packet_size_left;
511 int i_packet_property;
513 int b_packet_multiple_payload;
515 int i_packet_sequence;
516 int i_packet_padding_length;
518 uint32_t i_packet_send_time;
519 uint16_t i_packet_duration;
522 int i_payload_length_type;
525 if( stream_Peek( p_input->s, &p_peek,i_data_packet_min)<i_data_packet_min )
528 msg_Warn( p_input, "cannot peek while getting new packet, EOF ?" );
533 /* *** parse error correction if present *** */
536 unsigned int i_error_correction_length_type;
537 unsigned int i_error_correction_data_length;
538 unsigned int i_opaque_data_present;
540 i_error_correction_data_length = p_peek[0] & 0x0f; // 4bits
541 i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01; // 1bit
542 i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
543 i_skip += 1; // skip error correction flags
545 if( i_error_correction_length_type != 0x00 ||
546 i_opaque_data_present != 0 ||
547 i_error_correction_data_length != 0x02 )
549 goto loop_error_recovery;
552 i_skip += i_error_correction_data_length;
556 msg_Warn( p_input, "p_peek[0]&0x80 != 0x80" );
560 if( i_skip + 2 >= i_data_packet_min )
562 goto loop_error_recovery;
565 i_packet_flags = p_peek[i_skip]; i_skip++;
566 i_packet_property = p_peek[i_skip]; i_skip++;
568 b_packet_multiple_payload = i_packet_flags&0x01;
570 /* read some value */
571 GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
572 GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
573 GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
575 i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
576 i_packet_duration = GetWLE( p_peek + i_skip ); i_skip += 2;
578 // i_packet_size_left = i_packet_length; // XXX données reellement lu
579 /* FIXME I have to do that for some file, I don't known why */
580 i_packet_size_left = i_data_packet_min;
582 if( b_packet_multiple_payload )
584 i_payload_count = p_peek[i_skip] & 0x3f;
585 i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
591 i_payload_length_type = 0x02; // unused
594 for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
596 asf_stream_t *p_stream;
599 int i_media_object_number;
600 int i_media_object_offset;
601 int i_replicated_data_length;
602 int i_payload_data_length;
603 int i_payload_data_pos;
604 int i_sub_payload_data_length;
610 if( i_skip >= i_packet_size_left )
612 /* prevent some segfault with invalid file */
616 i_stream_number = p_peek[i_skip] & 0x7f;
619 GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
620 GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
621 GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
623 if( i_replicated_data_length > 1 ) // should be at least 8 bytes
625 i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
626 i_skip += i_replicated_data_length;
629 i_media_object_offset = i_tmp;
631 if( i_skip >= i_packet_size_left )
636 else if( i_replicated_data_length == 1 )
639 msg_Dbg( p_input, "found compressed payload" );
641 i_pts = (mtime_t)i_tmp * 1000;
642 i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
644 i_media_object_offset = 0;
648 i_pts = (mtime_t)i_packet_send_time * 1000;
651 i_media_object_offset = i_tmp;
654 i_pts = __MAX( i_pts - p_sys->p_fp->i_preroll * 1000, 0 );
655 if( b_packet_multiple_payload )
657 GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
661 i_payload_data_length = i_packet_length -
662 i_packet_padding_length - i_skip;
665 if( i_payload_data_length < 0 || i_skip + i_payload_data_length > i_packet_size_left )
672 "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
676 i_media_object_number,
677 i_media_object_offset,
678 i_replicated_data_length,
679 i_payload_data_length );
682 if( !( p_stream = p_sys->stream[i_stream_number] ) )
685 "undeclared stream[Id 0x%x]", i_stream_number );
686 i_skip += i_payload_data_length;
687 continue; // over payload
690 if( !p_stream->p_es )
692 i_skip += i_payload_data_length;
697 for( i_payload_data_pos = 0;
698 i_payload_data_pos < i_payload_data_length &&
699 i_packet_size_left > 0;
700 i_payload_data_pos += i_sub_payload_data_length )
702 data_packet_t *p_data;
704 // read sub payload length
705 if( i_replicated_data_length == 1 )
707 i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
708 i_payload_data_pos++;
712 i_sub_payload_data_length = i_payload_data_length;
715 /* FIXME I don't use i_media_object_number, sould I ? */
716 if( p_stream->p_pes && i_media_object_offset == 0 )
718 /* send complete packet to decoder */
719 if( p_stream->p_pes->i_pes_size > 0 )
721 es_out_Send( p_input->p_es_out, p_stream->p_es, p_stream->p_pes );
722 p_stream->p_pes = NULL;
726 if( !p_stream->p_pes ) // add a new PES
729 ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
731 p_stream->p_pes = input_NewPES( p_input->p_method_data );
732 p_stream->p_pes->i_dts =
733 p_stream->p_pes->i_pts =
734 input_ClockGetTS( p_input,
735 p_input->stream.p_selected_program,
736 p_stream->i_time * 9 /100 );
738 //msg_Err( p_input, "stream[0x%2x] pts=%lld", i_stream_number, p_stream->p_pes->i_pts );
739 p_stream->p_pes->p_next = NULL;
740 p_stream->p_pes->i_nb_data = 0;
741 p_stream->p_pes->i_pes_size = 0;
744 i_read = i_sub_payload_data_length + i_skip;
745 if((p_data = stream_DataPacket( p_input->s,i_read,VLC_TRUE))==NULL)
747 msg_Warn( p_input, "cannot read data" );
750 p_data->p_payload_start += i_skip;
751 i_packet_size_left -= i_read;
754 if( !p_stream->p_pes->p_first )
756 p_stream->p_pes->p_first = p_stream->p_pes->p_last = p_data;
760 p_stream->p_pes->p_last->p_next = p_data;
761 p_stream->p_pes->p_last = p_data;
763 p_stream->p_pes->i_pes_size += i_sub_payload_data_length;
764 p_stream->p_pes->i_nb_data++;
767 if( i_packet_size_left > 0 )
769 if( stream_Peek( p_input->s, &p_peek, i_packet_size_left )
770 < i_packet_size_left )
773 msg_Warn( p_input, "cannot peek, EOF ?" );
780 if( i_packet_size_left > 0 )
782 if( stream_Read( p_input->s, NULL, i_packet_size_left )
783 < i_packet_size_left )
785 msg_Warn( p_input, "cannot skip data, EOF ?" );
793 msg_Warn( p_input, "unsupported packet header" );
794 if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
796 msg_Err( p_input, "unsupported packet header, fatal error" );
799 stream_Read( p_input->s, NULL, i_data_packet_min );