1 /*****************************************************************************
2 * asf.c : ASFv01 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: asf.c,v 1.1 2002/10/20 17:22:33 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 ) < 8 )
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 ) );
106 p_input->p_demux_data = p_demux;
109 /* Now load all object ( except raw data ) */
110 if( !ASF_ReadObjectRoot( p_input, &p_demux->root, p_input->stream.b_seekable ) )
112 msg_Warn( p_input, "ASF v1.0 plugin discarded (not a valid file)" );
116 /* Check if we have found all mandatory asf object */
117 if( !p_demux->root.p_hdr || !p_demux->root.p_data )
119 ASF_FreeObjectRoot( p_input, &p_demux->root );
121 msg_Warn( p_input, "ASF v1.0 plugin discarded (not a valid file)" );
125 if( !( p_demux->p_fp = ASF_FindObject( (asf_object_t*)p_demux->root.p_hdr,
126 &asf_object_file_properties_guid, 0 ) ) )
128 ASF_FreeObjectRoot( p_input, &p_demux->root );
130 msg_Warn( p_input, "ASF v1.0 plugin discarded (missing file_properties object)" );
134 if( p_demux->p_fp->i_min_data_packet_size != p_demux->p_fp->i_max_data_packet_size )
136 ASF_FreeObjectRoot( p_input, &p_demux->root );
138 msg_Warn( p_input, "ASF v1.0 plugin discarded (invalid file_properties object)" );
142 p_demux->i_streams = ASF_CountObject( (asf_object_t*)p_demux->root.p_hdr,
143 &asf_object_stream_properties_guid );
144 if( !p_demux->i_streams )
146 ASF_FreeObjectRoot( p_input, &p_demux->root );
148 msg_Warn( p_input, "ASF plugin discarded (cannot find any stream!)" );
153 msg_Dbg( p_input, "found %d streams", p_demux->i_streams );
156 /* create one program */
157 vlc_mutex_lock( &p_input->stream.stream_lock );
158 if( input_InitStream( p_input, 0 ) == -1)
160 vlc_mutex_unlock( &p_input->stream.stream_lock );
161 msg_Err( p_input, "cannot init stream" );
164 if( input_AddProgram( p_input, 0, 0) == NULL )
166 vlc_mutex_unlock( &p_input->stream.stream_lock );
167 msg_Err( p_input, "cannot add program" );
170 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
171 p_input->stream.i_mux_rate = 0 ; /* FIXME */
172 vlc_mutex_unlock( &p_input->stream.stream_lock );
174 for( i_stream = 0; i_stream < p_demux->i_streams; i_stream ++ )
176 asf_stream_t *p_stream;
177 asf_object_stream_properties_t *p_sp;
179 p_sp = (asf_object_stream_properties_t*)
180 ASF_FindObject( (asf_object_t*)p_demux->root.p_hdr,
181 &asf_object_stream_properties_guid,
184 p_stream = p_demux->stream[p_sp->i_stream_number] = malloc( sizeof( asf_stream_t ) );
185 memset( p_stream, 0, sizeof( asf_stream_t ) );
186 p_stream->p_sp = p_sp;
188 vlc_mutex_lock( &p_input->stream.stream_lock );
190 input_AddES( p_input,
191 p_input->stream.p_selected_program,
192 p_sp->i_stream_number,
193 p_sp->i_type_specific_data_length );
194 if( p_sp->i_type_specific_data_length > 11 )
196 memcpy( p_stream->p_es->p_demux_data,
197 p_sp->p_type_specific_data + 11,
198 p_sp->i_type_specific_data_length - 11 );
201 vlc_mutex_unlock( &p_input->stream.stream_lock );
202 if( CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) )
205 if( p_sp->p_type_specific_data )
207 i_codec = GetWLE( p_sp->p_type_specific_data );
214 p_stream->i_cat = AUDIO_ES;
216 "adding new audio stream(codec:0x%x,ID:%d)",
218 p_sp->i_stream_number );
223 p_stream->p_es->i_fourcc =
224 VLC_FOURCC( 'm','p','g','a' );
227 p_stream->p_es->i_fourcc =
228 VLC_FOURCC( 'a','5','2',' ' );
232 p_stream->p_es->i_fourcc =
233 VLC_FOURCC( 'w','m','a',' ' );
236 p_stream->p_es->i_fourcc =
237 VLC_FOURCC( 'u','n','d','f' );
242 if( CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_video ) )
244 p_stream->i_cat = VIDEO_ES;
246 "adding new video stream(ID:%d)",
247 p_sp->i_stream_number );
248 if( p_sp->p_type_specific_data )
250 p_stream->p_es->i_fourcc =
251 GetDWLE( p_sp->p_type_specific_data + 27 );
255 p_stream->p_es->i_fourcc =
256 VLC_FOURCC( 'u','n','d','f' );
262 p_stream->i_cat = UNKNOWN_ES;
264 "ignoring unknown stream(ID:%d)",
265 p_sp->i_stream_number );
266 p_stream->p_es->i_fourcc = VLC_FOURCC( 'u','n','d','f' );
268 p_stream->p_es->i_cat = p_stream->i_cat;
270 vlc_mutex_lock( &p_input->stream.stream_lock );
271 input_SelectES( p_input, p_stream->p_es );
272 vlc_mutex_unlock( &p_input->stream.stream_lock );
275 vlc_mutex_lock( &p_input->stream.stream_lock );
276 p_input->stream.p_selected_program->b_is_ok = 1;
277 vlc_mutex_unlock( &p_input->stream.stream_lock );
279 // go to first packet
280 p_demux->i_data_begin = p_demux->root.p_data->i_object_pos + 50;
281 if( p_demux->root.p_data->i_object_size != 0 )
283 p_demux->i_data_end = p_demux->root.p_data->i_object_pos +
284 p_demux->root.p_data->i_object_size;
288 p_demux->i_data_end = -1;
290 ASF_SeekAbsolute( p_input, p_demux->i_data_begin );
294 /*****************************************************************************
295 * Demux: read packet and send them to decoders
296 *****************************************************************************/
297 #define GETVALUE2b( bits, var, def ) \
300 case 1: var = p_peek[i_skip]; i_skip++; break; \
301 case 2: var = GetWLE( p_peek + i_skip ); i_skip+= 2; break; \
302 case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
304 default: var = def; break;\
307 static int Demux( input_thread_t *p_input )
309 demux_sys_t *p_demux = p_input->p_demux_data;
312 /* catch seek from user */
313 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
316 i_offset = ASF_TellAbsolute( p_input ) - p_demux->i_data_begin;
322 /* XXX work only when i_min_data_packet_size == i_max_data_packet_size */
323 i_offset -= i_offset % p_demux->p_fp->i_min_data_packet_size;
324 ASF_SeekAbsolute( p_input, p_demux->i_data_begin + i_offset );
327 for( i = 0; i < 127 ; i++ )
329 #define p_stream p_demux->stream[i]
332 p_stream->i_time = 0;
338 /* first wait for the good time to read a packet */
340 input_ClockManageRef( p_input,
341 p_input->stream.p_selected_program,
344 /* update pcr XXX in mpeg scale so in 90000 unit/s */
345 p_demux->i_pcr = p_demux->i_time * 9 / 100;
347 // /* we will read 100ms for each stream so */
348 // p_demux->i_time += 100 * 1000;
350 for( i = 0; i < 10; i++ ) // parse 10 packets
352 int i_data_packet_min = p_demux->p_fp->i_min_data_packet_size;
356 int i_packet_size_left;
358 int i_packet_property;
360 int b_packet_multiple_payload;
362 int i_packet_sequence;
363 int i_packet_padding_length;
365 u32 i_packet_send_time;
366 u16 i_packet_duration;
369 int i_payload_length_type;
372 if( input_Peek( p_input, &p_peek, i_data_packet_min ) < i_data_packet_min )
375 msg_Err( p_input, "cannot peek while getting new packet, EOF ?" );
380 /* *** parse error correction if present *** */
383 int i_error_correction_length_type;
384 int i_error_correction_data_length;
385 int i_opaque_data_present;
387 i_error_correction_data_length = p_peek[0] & 0x0f; // 4bits
388 i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01; // 1bit
389 i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
390 i_skip += 1; // skip error correction flags
392 if( i_error_correction_length_type != 0x00 ||
393 i_opaque_data_present != 0 ||
394 i_error_correction_data_length != 0x02 )
396 goto loop_error_recovery;
399 i_skip += i_error_correction_data_length;
403 msg_Warn( p_input, "p_peek[0]&0x80 != 0x80" );
406 i_packet_flags = p_peek[i_skip]; i_skip++;
407 i_packet_property = p_peek[i_skip]; i_skip++;
409 b_packet_multiple_payload = i_packet_flags&0x01;
411 /* read some value */
412 GETVALUE2b( ( i_packet_flags >> 5 )&0x3, i_packet_length, i_data_packet_min );
413 GETVALUE2b( ( i_packet_flags >> 1 )&0x3, i_packet_sequence, 0 );
414 GETVALUE2b( ( i_packet_flags >> 3 )&0x3, i_packet_padding_length, 0 );
416 i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
417 i_packet_duration = GetWLE( p_peek + i_skip ); i_skip += 2;
419 i_packet_size_left = i_packet_length; // XXX données reellement lu
421 if( b_packet_multiple_payload )
423 i_payload_count = p_peek[i_skip] & 0x3f;
424 i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
430 i_payload_length_type = 0x02; // unused
433 for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
435 asf_stream_t *p_stream;
438 int i_media_object_number;
439 int i_media_object_offset;
440 int i_replicated_data_length;
441 int i_payload_data_length;
442 int i_payload_data_pos;
443 int i_sub_payload_data_length;
449 i_stream_number = p_peek[i_skip] & 0x7f;
452 GETVALUE2b( ( i_packet_property >> 4 )&0x03, i_media_object_number, 0 );
453 GETVALUE2b( ( i_packet_property >> 2 )&0x03, i_tmp, 0 );
454 GETVALUE2b( i_packet_property&0x03, i_replicated_data_length, 0 );
456 if( i_replicated_data_length > 1 ) // should be at least 8 bytes
458 i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
459 i_skip += i_replicated_data_length;
462 i_media_object_offset = i_tmp;
464 else if( i_replicated_data_length == 1 )
467 msg_Warn( p_input, "found compressed payload" );
469 i_pts = (mtime_t)i_tmp * 1000;
470 i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
472 i_media_object_offset = 0;
476 i_pts = (mtime_t)i_packet_send_time * 1000;
479 i_media_object_offset = i_tmp;
483 if( b_packet_multiple_payload )
485 GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
489 msg_Warn( p_input, "single payload" );
490 i_payload_data_length = i_packet_length - i_packet_padding_length - i_skip;
494 "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
498 i_media_object_number,
499 i_media_object_offset,
500 i_replicated_data_length,
501 i_payload_data_length );
504 if( !( p_stream = p_demux->stream[i_stream_number] ) )
506 msg_Warn( p_input, "undeclared stream[Id 0x%x]", i_stream_number );
507 i_skip += i_payload_data_length;
508 continue; // over payload
511 if( !p_stream->p_es || !p_stream->p_es->p_decoder_fifo )
513 i_skip += i_payload_data_length;
518 for( i_payload_data_pos = 0; i_payload_data_pos < i_payload_data_length; i_payload_data_pos += i_sub_payload_data_length )
520 data_packet_t *p_data;
522 // read sub payload length
523 if( i_replicated_data_length == 1 )
525 i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
526 i_payload_data_pos++;
530 i_sub_payload_data_length = i_payload_data_length;
534 if( p_stream->p_pes && i_media_object_offset == 0 ) // I don't use i_media_object_number, sould I ?
536 // send complete packet to decoder
537 if( p_stream->p_pes->i_pes_size > 0 )
539 input_DecodePES( p_stream->p_es->p_decoder_fifo, p_stream->p_pes );
540 p_stream->p_pes = NULL;
544 if( !p_stream->p_pes ) // add a new PES
547 p_stream->i_time = ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
549 p_stream->p_pes = input_NewPES( p_input->p_method_data );
550 p_stream->p_pes->i_dts =
551 p_stream->p_pes->i_pts =
552 input_ClockGetTS( p_input,
553 p_input->stream.p_selected_program,
554 p_stream->i_time * 9 / 100 );
556 p_stream->p_pes->p_next = NULL;
557 p_stream->p_pes->i_nb_data = 0;
558 p_stream->p_pes->i_pes_size = 0;
561 i_read = i_sub_payload_data_length + i_skip;
562 if( input_SplitBuffer( p_input, &p_data, i_read ) < i_read )
564 msg_Err( p_input, "cannot read data" );
567 p_data->p_payload_start += i_skip;
568 i_packet_size_left -= i_read;
571 if( !p_stream->p_pes->p_first )
573 p_stream->p_pes->p_first = p_stream->p_pes->p_last = p_data;
577 p_stream->p_pes->p_last->p_next = p_data;
578 p_stream->p_pes->p_last = p_data;
580 p_stream->p_pes->i_pes_size += i_sub_payload_data_length;
581 p_stream->p_pes->i_nb_data++;
584 if( i_packet_size_left <= 0 )
588 if( input_Peek( p_input, &p_peek, i_packet_size_left ) < i_packet_size_left )
591 msg_Err( p_input, "cannot peek, EOF ?" );
597 if( i_packet_size_left <= 0 )
604 if( i_packet_size_left > 0 )
606 if( !ASF_SkipBytes( p_input, i_packet_size_left ) )
608 msg_Err( p_input, "cannot skip data, EOF ?" );
617 msg_Warn( p_input, "unsupported packet header" );
618 if( p_demux->p_fp->i_min_data_packet_size != p_demux->p_fp->i_max_data_packet_size )
620 msg_Err( p_input, "unsupported packet header, fatal error" );
623 ASF_SkipBytes( p_input, i_data_packet_min );
626 } // loop over packet
629 for( i = 0; i < 127 ; i++ )
631 #define p_stream p_demux->stream[i]
632 if( p_stream && p_stream->p_es && p_stream->p_es->p_decoder_fifo )
634 p_demux->i_time = __MAX( p_demux->i_time, p_stream->i_time );
642 /*****************************************************************************
643 * MP4End: frees unused data
644 *****************************************************************************/
645 static void Deactivate( vlc_object_t * p_this )
648 if( p ) { free( p ); }
650 input_thread_t * p_input = (input_thread_t *)p_this;
651 demux_sys_t *p_demux = p_input->p_demux_data;
653 msg_Dbg( p_input, "Freeing all memory" );
654 ASF_FreeObjectRoot( p_input, &p_demux->root );