1 /*****************************************************************************
2 * asf.c : ASFv01 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: asf.c,v 1.2 2002/10/21 09:18:37 fenrir Exp $
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 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 General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
26 #include <stdlib.h> /* malloc(), free() */
27 #include <string.h> /* strdup() */
29 #include <sys/types.h>
32 #include <vlc/input.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Activate ( vlc_object_t * );
41 static void Deactivate ( vlc_object_t * );
42 static int Demux ( input_thread_t * );
44 /*****************************************************************************
46 *****************************************************************************/
48 set_description( "ASF v1.0 demuxer (file only)" );
49 set_capability( "demux", 200 );
50 set_callbacks( Activate, Deactivate );
51 add_shortcut( "asf" );
54 static u16 GetWLE( u8 *p_buff )
56 return( (p_buff[0]) + ( p_buff[1] <<8 ) );
58 static u32 GetDWLE( u8 *p_buff )
60 return( p_buff[0] + ( p_buff[1] <<8 ) +
61 ( p_buff[2] <<16 ) + ( p_buff[3] <<24 ) );
64 /*****************************************************************************
65 * Activate: check file and initializes ASF structures
66 *****************************************************************************/
67 static int Activate( vlc_object_t * p_this )
69 input_thread_t *p_input = (input_thread_t *)p_this;
76 /* Initialize access plug-in structures. */
77 if( p_input->i_mtu == 0 )
80 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
83 p_input->pf_demux = Demux;
85 /* a little test to see if it could be a asf stream */
86 if( input_Peek( p_input, &p_peek, 16 ) < 16 )
88 msg_Warn( p_input, "ASF v1.0 plugin discarded (cannot peek)" );
91 GetGUID( &guid, p_peek );
92 if( !CmpGUID( &guid, &asf_object_header_guid ) )
94 msg_Warn( p_input, "ASF v1.0 plugin discarded (not a valid file)" );
98 /* create our structure that will contains all data */
99 if( !( p_input->p_demux_data =
100 p_demux = malloc( sizeof( demux_sys_t ) ) ) )
102 msg_Err( p_input, "out of memory" );
105 memset( p_demux, 0, sizeof( demux_sys_t ) );
107 /* Now load all object ( except raw data ) */
108 if( !ASF_ReadObjectRoot( p_input, &p_demux->root, p_input->stream.b_seekable ) )
110 msg_Warn( p_input, "ASF v1.0 plugin discarded (not a valid file)" );
114 /* Check if we have found all mandatory asf object */
115 if( !p_demux->root.p_hdr || !p_demux->root.p_data )
117 ASF_FreeObjectRoot( p_input, &p_demux->root );
119 msg_Warn( p_input, "ASF v1.0 plugin discarded (not a valid file)" );
123 if( !( p_demux->p_fp = ASF_FindObject( p_demux->root.p_hdr,
124 &asf_object_file_properties_guid, 0 ) ) )
126 ASF_FreeObjectRoot( p_input, &p_demux->root );
128 msg_Warn( p_input, "ASF v1.0 plugin discarded (missing file_properties object)" );
132 if( p_demux->p_fp->i_min_data_packet_size != p_demux->p_fp->i_max_data_packet_size )
134 ASF_FreeObjectRoot( p_input, &p_demux->root );
136 msg_Warn( p_input, "ASF v1.0 plugin discarded (invalid file_properties object)" );
140 p_demux->i_streams = ASF_CountObject( p_demux->root.p_hdr,
141 &asf_object_stream_properties_guid );
142 if( !p_demux->i_streams )
144 ASF_FreeObjectRoot( p_input, &p_demux->root );
146 msg_Warn( p_input, "ASF plugin discarded (cannot find any stream!)" );
151 msg_Dbg( p_input, "found %d streams", p_demux->i_streams );
154 /* create one program */
155 vlc_mutex_lock( &p_input->stream.stream_lock );
156 if( input_InitStream( p_input, 0 ) == -1)
158 vlc_mutex_unlock( &p_input->stream.stream_lock );
159 msg_Err( p_input, "cannot init stream" );
162 if( input_AddProgram( p_input, 0, 0) == NULL )
164 vlc_mutex_unlock( &p_input->stream.stream_lock );
165 msg_Err( p_input, "cannot add program" );
168 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
169 p_input->stream.i_mux_rate = 0 ; /* FIXME */
170 vlc_mutex_unlock( &p_input->stream.stream_lock );
172 for( i_stream = 0; i_stream < p_demux->i_streams; i_stream ++ )
174 asf_stream_t *p_stream;
175 asf_object_stream_properties_t *p_sp;
177 p_sp = ASF_FindObject( p_demux->root.p_hdr,
178 &asf_object_stream_properties_guid,
182 p_demux->stream[p_sp->i_stream_number] =
183 malloc( sizeof( asf_stream_t ) );
184 memset( p_stream, 0, sizeof( asf_stream_t ) );
185 p_stream->p_sp = p_sp;
187 vlc_mutex_lock( &p_input->stream.stream_lock );
189 input_AddES( p_input,
190 p_input->stream.p_selected_program,
191 p_sp->i_stream_number,
192 p_sp->i_type_specific_data_length );
193 if( p_sp->i_type_specific_data_length > 11 )
195 memcpy( p_stream->p_es->p_demux_data,
196 p_sp->p_type_specific_data + 11,
197 p_sp->i_type_specific_data_length - 11 );
200 vlc_mutex_unlock( &p_input->stream.stream_lock );
201 if( CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) )
204 if( p_sp->p_type_specific_data )
206 i_codec = GetWLE( p_sp->p_type_specific_data );
213 p_stream->i_cat = AUDIO_ES;
215 "adding new audio stream(codec:0x%x,ID:%d)",
217 p_sp->i_stream_number );
221 p_stream->p_es->i_fourcc =
222 VLC_FOURCC( 'a', 'r', 'a', 'w' );
226 p_stream->p_es->i_fourcc =
227 VLC_FOURCC( 'm','p','g','a' );
230 p_stream->p_es->i_fourcc =
231 VLC_FOURCC( 'a','5','2',' ' );
235 p_stream->p_es->i_fourcc =
236 VLC_FOURCC( 'w','m','a',' ' );
239 p_stream->p_es->i_fourcc =
240 VLC_FOURCC( 'u','n','d','f' );
245 if( CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_video ) )
247 p_stream->i_cat = VIDEO_ES;
249 "adding new video stream(ID:%d)",
250 p_sp->i_stream_number );
251 if( p_sp->p_type_specific_data )
253 p_stream->p_es->i_fourcc =
254 GetDWLE( p_sp->p_type_specific_data + 27 );
258 p_stream->p_es->i_fourcc =
259 VLC_FOURCC( 'u','n','d','f' );
265 p_stream->i_cat = UNKNOWN_ES;
267 "ignoring unknown stream(ID:%d)",
268 p_sp->i_stream_number );
269 p_stream->p_es->i_fourcc = VLC_FOURCC( 'u','n','d','f' );
271 p_stream->p_es->i_cat = p_stream->i_cat;
273 vlc_mutex_lock( &p_input->stream.stream_lock );
274 input_SelectES( p_input, p_stream->p_es );
275 vlc_mutex_unlock( &p_input->stream.stream_lock );
278 vlc_mutex_lock( &p_input->stream.stream_lock );
279 p_input->stream.p_selected_program->b_is_ok = 1;
280 vlc_mutex_unlock( &p_input->stream.stream_lock );
282 p_demux->i_data_begin = p_demux->root.p_data->i_object_pos + 50;
283 if( p_demux->root.p_data->i_object_size != 0 )
285 p_demux->i_data_end = p_demux->root.p_data->i_object_pos +
286 p_demux->root.p_data->i_object_size;
290 p_demux->i_data_end = -1;
294 // go to first packet
295 ASF_SeekAbsolute( p_input, p_demux->i_data_begin );
299 /*****************************************************************************
300 * Demux: read packet and send them to decoders
301 *****************************************************************************/
302 #define GETVALUE2b( bits, var, def ) \
303 switch( (bits)&0x03 ) \
305 case 1: var = p_peek[i_skip]; i_skip++; break; \
306 case 2: var = GetWLE( p_peek + i_skip ); i_skip+= 2; break; \
307 case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
309 default: var = def; break;\
312 static int Demux( input_thread_t *p_input )
314 demux_sys_t *p_demux = p_input->p_demux_data;
317 /* catch seek from user */
318 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
322 i_offset = ASF_TellAbsolute( p_input ) - p_demux->i_data_begin;
328 /* XXX work only when i_min_data_packet_size == i_max_data_packet_size */
329 i_offset -= i_offset % p_demux->p_fp->i_min_data_packet_size;
330 ASF_SeekAbsolute( p_input, p_demux->i_data_begin + i_offset );
333 for( i = 0; i < 128 ; i++ )
335 #define p_stream p_demux->stream[i]
338 p_stream->i_time = 0;
344 /* first wait for the good time to read a packet */
346 input_ClockManageRef( p_input,
347 p_input->stream.p_selected_program,
350 /* update pcr XXX in mpeg scale so in 90000 unit/s */
351 p_demux->i_pcr = p_demux->i_time * 9 / 100;
353 for( i = 0; i < 10; i++ ) // parse 10 packets
355 int i_data_packet_min = p_demux->p_fp->i_min_data_packet_size;
359 int i_packet_size_left;
361 int i_packet_property;
363 int b_packet_multiple_payload;
365 int i_packet_sequence;
366 int i_packet_padding_length;
368 u32 i_packet_send_time;
369 u16 i_packet_duration;
372 int i_payload_length_type;
375 if( input_Peek( p_input, &p_peek, i_data_packet_min ) < i_data_packet_min )
378 msg_Err( p_input, "cannot peek while getting new packet, EOF ?" );
383 /* *** parse error correction if present *** */
386 int i_error_correction_length_type;
387 int i_error_correction_data_length;
388 int i_opaque_data_present;
390 i_error_correction_data_length = p_peek[0] & 0x0f; // 4bits
391 i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01; // 1bit
392 i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
393 i_skip += 1; // skip error correction flags
395 if( i_error_correction_length_type != 0x00 ||
396 i_opaque_data_present != 0 ||
397 i_error_correction_data_length != 0x02 )
399 goto loop_error_recovery;
402 i_skip += i_error_correction_data_length;
406 msg_Warn( p_input, "p_peek[0]&0x80 != 0x80" );
409 i_packet_flags = p_peek[i_skip]; i_skip++;
410 i_packet_property = p_peek[i_skip]; i_skip++;
412 b_packet_multiple_payload = i_packet_flags&0x01;
414 /* read some value */
415 GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
416 GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
417 GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
419 i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
420 i_packet_duration = GetWLE( p_peek + i_skip ); i_skip += 2;
422 i_packet_size_left = i_packet_length; // XXX données reellement lu
424 if( b_packet_multiple_payload )
426 i_payload_count = p_peek[i_skip] & 0x3f;
427 i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
433 i_payload_length_type = 0x02; // unused
436 for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
438 asf_stream_t *p_stream;
441 int i_media_object_number;
442 int i_media_object_offset;
443 int i_replicated_data_length;
444 int i_payload_data_length;
445 int i_payload_data_pos;
446 int i_sub_payload_data_length;
452 i_stream_number = p_peek[i_skip] & 0x7f;
455 GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
456 GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
457 GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
459 if( i_replicated_data_length > 1 ) // should be at least 8 bytes
461 i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
462 i_skip += i_replicated_data_length;
465 i_media_object_offset = i_tmp;
467 else if( i_replicated_data_length == 1 )
470 msg_Warn( p_input, "found compressed payload" );
472 i_pts = (mtime_t)i_tmp * 1000;
473 i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
475 i_media_object_offset = 0;
479 i_pts = (mtime_t)i_packet_send_time * 1000;
482 i_media_object_offset = i_tmp;
486 if( b_packet_multiple_payload )
488 GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
492 msg_Warn( p_input, "single payload" );
493 i_payload_data_length = i_packet_length - i_packet_padding_length - i_skip;
498 "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
502 i_media_object_number,
503 i_media_object_offset,
504 i_replicated_data_length,
505 i_payload_data_length );
508 if( !( p_stream = p_demux->stream[i_stream_number] ) )
510 msg_Warn( p_input, "undeclared stream[Id 0x%x]", i_stream_number );
511 i_skip += i_payload_data_length;
512 continue; // over payload
515 if( !p_stream->p_es || !p_stream->p_es->p_decoder_fifo )
517 i_skip += i_payload_data_length;
522 for( i_payload_data_pos = 0;
523 i_payload_data_pos < i_payload_data_length &&
524 i_packet_size_left > 0;
525 i_payload_data_pos += i_sub_payload_data_length )
527 data_packet_t *p_data;
529 // read sub payload length
530 if( i_replicated_data_length == 1 )
532 i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
533 i_payload_data_pos++;
537 i_sub_payload_data_length = i_payload_data_length;
540 /* FIXME I don't use i_media_object_number, sould I ? */
541 if( p_stream->p_pes && i_media_object_offset == 0 ) {
542 /* send complete packet to decoder */
543 if( p_stream->p_pes->i_pes_size > 0 )
545 input_DecodePES( p_stream->p_es->p_decoder_fifo, p_stream->p_pes );
546 p_stream->p_pes = NULL;
550 if( !p_stream->p_pes ) // add a new PES
552 p_stream->i_time = ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
554 p_stream->p_pes = input_NewPES( p_input->p_method_data );
555 p_stream->p_pes->i_dts =
556 p_stream->p_pes->i_pts =
557 input_ClockGetTS( p_input,
558 p_input->stream.p_selected_program,
559 p_stream->i_time * 9 / 100 );
561 p_stream->p_pes->p_next = NULL;
562 p_stream->p_pes->i_nb_data = 0;
563 p_stream->p_pes->i_pes_size = 0;
566 i_read = i_sub_payload_data_length + i_skip;
567 if( input_SplitBuffer( p_input, &p_data, i_read ) < i_read )
569 msg_Err( p_input, "cannot read data" );
572 p_data->p_payload_start += i_skip;
573 i_packet_size_left -= i_read;
576 if( !p_stream->p_pes->p_first )
578 p_stream->p_pes->p_first = p_stream->p_pes->p_last = p_data;
582 p_stream->p_pes->p_last->p_next = p_data;
583 p_stream->p_pes->p_last = p_data;
585 p_stream->p_pes->i_pes_size += i_sub_payload_data_length;
586 p_stream->p_pes->i_nb_data++;
589 if( i_packet_size_left > 0 )
591 if( input_Peek( p_input, &p_peek, i_packet_size_left ) < i_packet_size_left )
594 msg_Warn( p_input, "cannot peek, EOF ?" );
601 if( i_packet_size_left > 0 )
603 if( !ASF_SkipBytes( p_input, i_packet_size_left ) )
605 msg_Warn( p_input, "cannot skip data, EOF ?" );
613 msg_Warn( p_input, "unsupported packet header" );
614 if( p_demux->p_fp->i_min_data_packet_size != p_demux->p_fp->i_max_data_packet_size )
616 msg_Err( p_input, "unsupported packet header, fatal error" );
619 ASF_SkipBytes( p_input, i_data_packet_min );
621 } // loop over packet
624 for( i = 0; i < 128 ; i++ )
626 #define p_stream p_demux->stream[i]
627 if( p_stream && p_stream->p_es && p_stream->p_es->p_decoder_fifo )
629 p_demux->i_time = __MAX( p_demux->i_time, p_stream->i_time );
637 /*****************************************************************************
638 * MP4End: frees unused data
639 *****************************************************************************/
640 static void Deactivate( vlc_object_t * p_this )
643 if( p ) { free( p ); }
645 input_thread_t * p_input = (input_thread_t *)p_this;
646 demux_sys_t *p_demux = p_input->p_demux_data;
649 msg_Dbg( p_input, "Freeing all memory" );
650 ASF_FreeObjectRoot( p_input, &p_demux->root );
651 for( i_stream = 0; i_stream < 128; i_stream++ )
653 #define p_stream p_demux->stream[i_stream]
656 if( p_stream->p_pes )
658 input_DeletePES( p_input->p_method_data, p_stream->p_pes );