1 /*****************************************************************************
2 * libasf.c : asf stream demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 #include <stdlib.h> /* malloc(), free() */
28 #include <vlc/input.h>
30 #include "codecs.h" /* BITMAPINFOHEADER, WAVEFORMATEX */
36 if( p ) {free( p ); p = NULL; }
38 #define GUID_FMT "0x%x-0x%x-0x%x-0x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x"
39 #define GUID_PRINT( guid ) \
43 (guid).v4[0],(guid).v4[1],(guid).v4[2],(guid).v4[3], \
44 (guid).v4[4],(guid).v4[5],(guid).v4[6],(guid).v4[7]
46 /****************************************************************************
48 ****************************************************************************/
49 static int ASF_ReadObject( stream_t *, asf_object_t *, asf_object_t * );
51 /****************************************************************************
53 ****************************************************************************/
54 void ASF_GetGUID( guid_t *p_guid, uint8_t *p_data )
56 p_guid->v1 = GetDWLE( p_data );
57 p_guid->v2 = GetWLE( p_data + 4);
58 p_guid->v3 = GetWLE( p_data + 6);
59 memcpy( p_guid->v4, p_data + 8, 8 );
62 int ASF_CmpGUID( const guid_t *p_guid1, const guid_t *p_guid2 )
64 if( (p_guid1->v1 != p_guid2->v1 )||
65 (p_guid1->v2 != p_guid2->v2 )||
66 (p_guid1->v3 != p_guid2->v3 )||
67 ( memcmp( p_guid1->v4, p_guid2->v4,8 )) )
71 return( 1 ); /* match */
74 /****************************************************************************
76 ****************************************************************************/
77 static int ASF_ReadObjectCommon( stream_t *s, asf_object_t *p_obj )
79 asf_object_common_t *p_common = (asf_object_common_t*)p_obj;
82 if( stream_Peek( s, &p_peek, 24 ) < 24 )
84 return( VLC_EGENERIC );
86 ASF_GetGUID( &p_common->i_object_id, p_peek );
87 p_common->i_object_size = GetQWLE( p_peek + 16 );
88 p_common->i_object_pos = stream_Tell( s );
89 p_common->p_next = NULL;
92 msg_Dbg( (vlc_object_t*)s,
93 "found object guid: " GUID_FMT " size:"I64Fd,
94 GUID_PRINT( p_common->i_object_id ),
95 p_common->i_object_size );
98 return( VLC_SUCCESS );
101 static int ASF_NextObject( stream_t *s, asf_object_t *p_obj )
106 if( ASF_ReadObjectCommon( s, &obj ) )
108 return( VLC_EGENERIC );
113 if( p_obj->common.i_object_size <= 0 )
117 if( p_obj->common.p_father &&
118 p_obj->common.p_father->common.i_object_size != 0 )
120 if( p_obj->common.p_father->common.i_object_pos +
121 p_obj->common.p_father->common.i_object_size <
122 p_obj->common.i_object_pos + p_obj->common.i_object_size + 24 )
123 /* 24 is min size of an object */
130 return stream_Seek( s, p_obj->common.i_object_pos +
131 p_obj->common.i_object_size );
134 static void ASF_FreeObject_Null( asf_object_t *pp_obj )
139 static int ASF_ReadObject_Header( stream_t *s, asf_object_t *p_obj )
141 asf_object_header_t *p_hdr = (asf_object_header_t*)p_obj;
142 asf_object_t *p_subobj;
146 if( ( i_peek = stream_Peek( s, &p_peek, 30 ) ) < 30 )
148 return( VLC_EGENERIC );
151 p_hdr->i_sub_object_count = GetDWLE( p_peek + 24 );
152 p_hdr->i_reserved1 = p_peek[28];
153 p_hdr->i_reserved2 = p_peek[29];
154 p_hdr->p_first = NULL;
155 p_hdr->p_last = NULL;
158 msg_Dbg( (vlc_object_t*)s,
159 "read \"header object\" subobj:%d, reserved1:%d, reserved2:%d",
160 p_hdr->i_sub_object_count,
162 p_hdr->i_reserved2 );
165 /* Cannot fail as peek succeed */
166 stream_Read( s, NULL, 30 );
168 /* Now load sub object */
171 p_subobj = malloc( sizeof( asf_object_t ) );
173 if( ASF_ReadObject( s, p_subobj, (asf_object_t*)p_hdr ) )
178 if( ASF_NextObject( s, p_subobj ) ) /* Go to the next object */
186 static int ASF_ReadObject_Data( stream_t *s, asf_object_t *p_obj )
188 asf_object_data_t *p_data = (asf_object_data_t*)p_obj;
192 if( ( i_peek = stream_Peek( s, &p_peek, 50 ) ) < 50 )
196 ASF_GetGUID( &p_data->i_file_id, p_peek + 24 );
197 p_data->i_total_data_packets = GetQWLE( p_peek + 40 );
198 p_data->i_reserved = GetWLE( p_peek + 48 );
201 msg_Dbg( (vlc_object_t*)s,
202 "read \"data object\" file_id:" GUID_FMT " total data packet:"
204 GUID_PRINT( p_data->i_file_id ),
205 p_data->i_total_data_packets,
206 p_data->i_reserved );
212 static int ASF_ReadObject_Index( stream_t *s, asf_object_t *p_obj )
214 asf_object_index_t *p_index = (asf_object_index_t*)p_obj;
218 if( ( i_peek = stream_Peek( s, &p_peek, 56 ) ) < 56 )
222 ASF_GetGUID( &p_index->i_file_id, p_peek + 24 );
223 p_index->i_index_entry_time_interval = GetQWLE( p_peek + 40 );
224 p_index->i_max_packet_count = GetDWLE( p_peek + 48 );
225 p_index->i_index_entry_count = GetDWLE( p_peek + 52 );
226 p_index->index_entry = NULL; /* FIXME */
229 msg_Dbg( (vlc_object_t*)s,
230 "read \"index object\" file_id:" GUID_FMT
231 " index_entry_time_interval:"I64Fd" max_packet_count:%d "
232 "index_entry_count:%ld",
233 GUID_PRINT( p_index->i_file_id ),
234 p_index->i_index_entry_time_interval,
235 p_index->i_max_packet_count,
236 (long int)p_index->i_index_entry_count );
242 static void ASF_FreeObject_Index( asf_object_t *p_obj )
244 asf_object_index_t *p_index = (asf_object_index_t*)p_obj;
246 FREE( p_index->index_entry );
249 static int ASF_ReadObject_file_properties( stream_t *s, asf_object_t *p_obj )
251 asf_object_file_properties_t *p_fp = (asf_object_file_properties_t*)p_obj;
255 if( ( i_peek = stream_Peek( s, &p_peek, 92) ) < 92 )
259 ASF_GetGUID( &p_fp->i_file_id, p_peek + 24 );
260 p_fp->i_file_size = GetQWLE( p_peek + 40 );
261 p_fp->i_creation_date = GetQWLE( p_peek + 48 );
262 p_fp->i_data_packets_count = GetQWLE( p_peek + 56 );
263 p_fp->i_play_duration = GetQWLE( p_peek + 64 );
264 p_fp->i_send_duration = GetQWLE( p_peek + 72 );
265 p_fp->i_preroll = GetQWLE( p_peek + 80 );
266 p_fp->i_flags = GetDWLE( p_peek + 88 );
267 p_fp->i_min_data_packet_size = GetDWLE( p_peek + 92 );
268 p_fp->i_max_data_packet_size = GetDWLE( p_peek + 96 );
269 p_fp->i_max_bitrate = GetDWLE( p_peek + 100 );
272 msg_Dbg( (vlc_object_t*)s,
273 "read \"file properties object\" file_id:" GUID_FMT
274 " file_size:"I64Fd" creation_date:"I64Fd" data_packets_count:"
275 I64Fd" play_duration:"I64Fd" send_duration:"I64Fd" preroll:"
276 I64Fd" flags:%d min_data_packet_size:%d max_data_packet_size:%d "
278 GUID_PRINT( p_fp->i_file_id ), p_fp->i_file_size,
279 p_fp->i_creation_date, p_fp->i_data_packets_count,
280 p_fp->i_play_duration, p_fp->i_send_duration,
281 p_fp->i_preroll, p_fp->i_flags,
282 p_fp->i_min_data_packet_size, p_fp->i_max_data_packet_size,
283 p_fp->i_max_bitrate );
289 static void ASF_FreeObject_metadata( asf_object_t *p_obj )
291 asf_object_metadata_t *p_meta =
292 (asf_object_metadata_t *)p_obj;
295 for( i = 0; i < p_meta->i_record_entries_count; i++ )
297 if( p_meta->record[i].psz_name ) free( p_meta->record[i].psz_name );
298 if( p_meta->record[i].p_data ) free( p_meta->record[i].p_data );
300 if( p_meta->record ) free( p_meta->record );
303 static int ASF_ReadObject_metadata( stream_t *s, asf_object_t *p_obj )
305 asf_object_metadata_t *p_meta =
306 (asf_object_metadata_t *)p_obj;
308 int i_peek, i_entries, i;
311 p_meta->i_record_entries_count = 0;
314 if( stream_Peek( s, &p_peek, p_meta->i_object_size ) <
315 (int)p_meta->i_object_size )
321 i_entries = GetWLE( p_peek + i_peek ); i_peek += 2;
322 for( i = 0; i < i_entries && i_peek < (int)p_meta->i_object_size -12; i++ )
324 asf_metadata_record_t record;
325 int i_name, i_data, j;
327 if( GetWLE( p_peek + i_peek ) != 0 )
329 ASF_FreeObject_metadata( p_obj );
334 record.i_stream = GetWLE( p_peek + i_peek ); i_peek += 2;
335 i_name = GetWLE( p_peek + i_peek ); i_peek += 2;
336 record.i_type = GetWLE( p_peek + i_peek ); i_peek += 2;
337 i_data = GetDWLE( p_peek + i_peek ); i_peek += 4;
339 if( record.i_type > ASF_METADATA_TYPE_WORD ||
340 i_peek + i_data + i_name > (int)p_meta->i_object_size )
342 ASF_FreeObject_metadata( p_obj );
351 record.psz_name = malloc( i_name/2 );
352 for( j = 0; j < i_name/2; j++ )
354 record.psz_name[j] = GetWLE( p_peek + i_peek ); i_peek += 2;
358 if( record.i_type == ASF_METADATA_TYPE_STRING )
360 record.p_data = malloc( i_data/2 );
361 record.i_data = i_data/2;
362 for( j = 0; j < i_data/2; j++ )
364 record.p_data[j] = GetWLE( p_peek + i_peek ); i_peek += 2;
367 msg_Dbg( s, "metadata: %s=%s", record.psz_name, record.p_data );
369 else if( record.i_type == ASF_METADATA_TYPE_BYTE )
371 record.p_data = malloc( i_data );
372 record.i_data = i_data;
373 memcpy( record.p_data, p_peek + i_peek, i_data );
376 msg_Dbg( s, "metadata: %s (%i bytes)", record.psz_name,
381 if( record.i_type == ASF_METADATA_TYPE_QWORD )
383 record.i_val = GetQWLE( p_peek + i_peek ); i_peek += 8;
385 else if( record.i_type == ASF_METADATA_TYPE_DWORD )
387 record.i_val = GetDWLE( p_peek + i_peek ); i_peek += 4;
391 record.i_val = GetWLE( p_peek + i_peek ); i_peek += 2;
394 msg_Dbg( s, "metadata: %s=%i", record.psz_name, record.i_val );
397 p_meta->i_record_entries_count++;
399 realloc( p_meta->record, p_meta->i_record_entries_count *
400 sizeof(asf_metadata_record_t) );
401 memcpy( &p_meta->record[p_meta->i_record_entries_count-1],
402 &record, sizeof(asf_metadata_record_t) );
408 static int ASF_ReadObject_header_extension( stream_t *s, asf_object_t *p_obj )
410 asf_object_header_extension_t *p_he =
411 (asf_object_header_extension_t *)p_obj;
415 if( ( i_peek = stream_Peek( s, &p_peek, p_he->i_object_size ) ) < 46)
419 ASF_GetGUID( &p_he->i_reserved1, p_peek + 24 );
420 p_he->i_reserved2 = GetWLE( p_peek + 40 );
421 p_he->i_header_extension_size = GetDWLE( p_peek + 42 );
422 if( p_he->i_header_extension_size )
424 p_he->p_header_extension_data =
425 malloc( p_he->i_header_extension_size );
426 memcpy( p_he->p_header_extension_data, p_peek + 46,
427 p_he->i_header_extension_size );
431 p_he->p_header_extension_data = NULL;
435 msg_Dbg( (vlc_object_t*)s,
436 "read \"header extension object\" reserved1:" GUID_FMT
437 " reserved2:%d header_extension_size:%d",
438 GUID_PRINT( p_he->i_reserved1 ), p_he->i_reserved2,
439 p_he->i_header_extension_size );
442 if( !p_he->i_header_extension_size ) return VLC_SUCCESS;
444 /* Read the extension objects */
445 stream_Read( s, NULL, 46 );
448 asf_object_t *p_obj = malloc( sizeof( asf_object_t ) );
450 if( ASF_ReadObject( s, p_obj, (asf_object_t*)p_he ) )
456 if( ASF_NextObject( s, p_obj ) ) /* Go to the next object */
465 static void ASF_FreeObject_header_extension( asf_object_t *p_obj )
467 asf_object_header_extension_t *p_he =
468 (asf_object_header_extension_t *)p_obj;
470 FREE( p_he->p_header_extension_data );
473 static int ASF_ReadObject_stream_properties( stream_t *s, asf_object_t *p_obj )
475 asf_object_stream_properties_t *p_sp =
476 (asf_object_stream_properties_t*)p_obj;
480 if( ( i_peek = stream_Peek( s, &p_peek, p_sp->i_object_size ) ) < 74 )
484 ASF_GetGUID( &p_sp->i_stream_type, p_peek + 24 );
485 ASF_GetGUID( &p_sp->i_error_correction_type, p_peek + 40 );
486 p_sp->i_time_offset = GetQWLE( p_peek + 56 );
487 p_sp->i_type_specific_data_length = GetDWLE( p_peek + 64 );
488 p_sp->i_error_correction_data_length = GetDWLE( p_peek + 68 );
489 p_sp->i_flags = GetWLE( p_peek + 72 );
490 p_sp->i_stream_number = p_sp->i_flags&0x07f;
491 p_sp->i_reserved = GetDWLE( p_peek + 74 );
492 if( p_sp->i_type_specific_data_length )
494 p_sp->p_type_specific_data =
495 malloc( p_sp->i_type_specific_data_length );
496 memcpy( p_sp->p_type_specific_data, p_peek + 78,
497 p_sp->i_type_specific_data_length );
501 p_sp->p_type_specific_data = NULL;
503 if( p_sp->i_error_correction_data_length )
505 p_sp->p_error_correction_data =
506 malloc( p_sp->i_error_correction_data_length );
507 memcpy( p_sp->p_error_correction_data,
508 p_peek + 78 + p_sp->i_type_specific_data_length,
509 p_sp->i_error_correction_data_length );
513 p_sp->p_error_correction_data = NULL;
517 msg_Dbg( (vlc_object_t*)s,
518 "read \"stream Properties object\" stream_type:" GUID_FMT
519 " error_correction_type:" GUID_FMT " time_offset:"I64Fd
520 " type_specific_data_length:%d error_correction_data_length:%d"
521 " flags:0x%x stream_number:%d",
522 GUID_PRINT( p_sp->i_stream_type ),
523 GUID_PRINT( p_sp->i_error_correction_type ),
525 p_sp->i_type_specific_data_length,
526 p_sp->i_error_correction_data_length,
528 p_sp->i_stream_number );
534 static void ASF_FreeObject_stream_properties( asf_object_t *p_obj )
536 asf_object_stream_properties_t *p_sp =
537 (asf_object_stream_properties_t*)p_obj;
539 FREE( p_sp->p_type_specific_data );
540 FREE( p_sp->p_error_correction_data );
544 static int ASF_ReadObject_codec_list( stream_t *s, asf_object_t *p_obj )
546 asf_object_codec_list_t *p_cl = (asf_object_codec_list_t*)p_obj;
548 uint8_t *p_peek, *p_data;
550 unsigned int i_codec;
552 if( ( i_peek = stream_Peek( s, &p_peek, p_cl->i_object_size ) ) < 44 )
557 ASF_GetGUID( &p_cl->i_reserved, p_peek + 24 );
558 p_cl->i_codec_entries_count = GetWLE( p_peek + 40 );
559 if( p_cl->i_codec_entries_count > 0 )
562 p_cl->codec = calloc( p_cl->i_codec_entries_count,
563 sizeof( asf_codec_entry_t ) );
564 memset( p_cl->codec, 0,
565 p_cl->i_codec_entries_count * sizeof( asf_codec_entry_t ) );
567 p_data = p_peek + 44;
568 for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
570 #define codec p_cl->codec[i_codec]
573 codec.i_type = GetWLE( p_data ); p_data += 2;
575 i_len = GetWLE( p_data ); p_data += 2;
576 codec.psz_name = calloc( sizeof( char ), i_len + 1);
577 for( i = 0; i < i_len; i++ )
579 codec.psz_name[i] = GetWLE( p_data + 2*i );
581 codec.psz_name[i_len] = '\0';
585 i_len = GetWLE( p_data ); p_data += 2;
586 codec.psz_description = calloc( sizeof( char ), i_len + 1);
587 for( i = 0; i < i_len; i++ )
589 codec.psz_description[i] = GetWLE( p_data + 2*i );
591 codec.psz_description[i_len] = '\0';
594 /* opaque information */
595 codec.i_information_length = GetWLE( p_data ); p_data += 2;
596 if( codec.i_information_length > 0 )
598 codec.p_information = malloc( codec.i_information_length );
599 memcpy( codec.p_information, p_data, codec.i_information_length );
600 p_data += codec.i_information_length;
604 codec.p_information = NULL;
615 msg_Dbg( s, "read \"codec list object\" reserved_guid:" GUID_FMT
616 " codec_entries_count:%d",
617 GUID_PRINT( p_cl->i_reserved ), p_cl->i_codec_entries_count );
619 for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
621 #define codec p_cl->codec[i_codec]
622 msg_Dbg( s, "read \"codec list object\" codec[%d] %s name:\"%s\" "
623 "description:\"%s\" information_length:%d",
624 i_codec, ( codec.i_type == ASF_CODEC_TYPE_VIDEO ) ?
625 "video" : ( ( codec.i_type == ASF_CODEC_TYPE_AUDIO ) ?
626 "audio" : "unknown" ),
627 codec.psz_name, codec.psz_description,
628 codec.i_information_length );
636 static void ASF_FreeObject_codec_list( asf_object_t *p_obj )
638 asf_object_codec_list_t *p_cl = (asf_object_codec_list_t*)p_obj;
639 unsigned int i_codec;
641 for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
643 #define codec p_cl->codec[i_codec]
644 FREE( codec.psz_name );
645 FREE( codec.psz_description );
646 FREE( codec.p_information );
652 /* Microsoft should go to hell. This time the length give number of bytes
653 * and for the some others object, length give char16 count ... */
654 static int ASF_ReadObject_content_description(stream_t *s, asf_object_t *p_obj)
656 asf_object_content_description_t *p_cd =
657 (asf_object_content_description_t *)p_obj;
658 uint8_t *p_peek, *p_data;
660 int i_len, i_title, i_author, i_copyright, i_description, i_rating;
662 #define GETSTRINGW( psz_str, i_size ) \
663 psz_str = calloc( i_size/2 + 1, sizeof( char ) ); \
664 for( i_len = 0; i_len < i_size/2; i_len++ ) \
666 psz_str[i_len] = GetWLE( p_data + 2*i_len ); \
668 psz_str[i_size/2] = '\0'; \
671 if( ( i_peek = stream_Peek( s, &p_peek, p_cd->i_object_size ) ) < 34 )
675 p_data = p_peek + 24;
677 i_title = GetWLE( p_data ); p_data += 2;
678 i_author= GetWLE( p_data ); p_data += 2;
679 i_copyright = GetWLE( p_data ); p_data += 2;
680 i_description = GetWLE( p_data ); p_data += 2;
681 i_rating = GetWLE( p_data ); p_data += 2;
683 GETSTRINGW( p_cd->psz_title, i_title );
684 GETSTRINGW( p_cd->psz_author, i_author );
685 GETSTRINGW( p_cd->psz_copyright, i_copyright );
686 GETSTRINGW( p_cd->psz_description, i_description );
687 GETSTRINGW( p_cd->psz_rating, i_rating );
692 msg_Dbg( (vlc_object_t*)s,
693 "Read \"content description object\" title:\"%s\" author:\"%s\" copyright:\"%s\" description:\"%s\" rating:\"%s\"",
697 p_cd->psz_description,
703 static void ASF_FreeObject_content_description( asf_object_t *p_obj)
705 asf_object_content_description_t *p_cd =
706 (asf_object_content_description_t *)p_obj;
708 FREE( p_cd->psz_title );
709 FREE( p_cd->psz_author );
710 FREE( p_cd->psz_copyright );
711 FREE( p_cd->psz_description );
712 FREE( p_cd->psz_rating );
719 int (*ASF_ReadObject_function)( stream_t *, asf_object_t *p_obj );
720 void (*ASF_FreeObject_function)( asf_object_t *p_obj );
722 } ASF_Object_Function [] =
724 { &asf_object_header_guid, ASF_OBJECT_TYPE_HEADER,
725 ASF_ReadObject_Header, ASF_FreeObject_Null },
726 { &asf_object_data_guid, ASF_OBJECT_TYPE_DATA,
727 ASF_ReadObject_Data, ASF_FreeObject_Null },
728 { &asf_object_index_guid, ASF_OBJECT_TYPE_INDEX,
729 ASF_ReadObject_Index, ASF_FreeObject_Index },
730 { &asf_object_file_properties_guid, ASF_OBJECT_TYPE_FILE_PROPERTIES,
731 ASF_ReadObject_file_properties, ASF_FreeObject_Null },
732 { &asf_object_stream_properties_guid, ASF_OBJECT_TYPE_STREAM_PROPERTIES,
733 ASF_ReadObject_stream_properties,ASF_FreeObject_stream_properties },
734 { &asf_object_header_extension_guid, ASF_OBJECT_TYPE_HEADER_EXTENSION,
735 ASF_ReadObject_header_extension, ASF_FreeObject_header_extension},
736 { &asf_object_metadata_guid, ASF_OBJECT_TYPE_METADATA,
737 ASF_ReadObject_metadata, ASF_FreeObject_metadata},
738 { &asf_object_codec_list_guid, ASF_OBJECT_TYPE_CODEC_LIST,
739 ASF_ReadObject_codec_list, ASF_FreeObject_codec_list },
740 { &asf_object_marker_guid, ASF_OBJECT_TYPE_MARKER,
742 { &asf_object_content_description_guid, ASF_OBJECT_TYPE_CONTENT_DESCRIPTION,
743 ASF_ReadObject_content_description, ASF_FreeObject_content_description },
745 { &asf_object_null_guid, 0, NULL, NULL }
748 static int ASF_ReadObject( stream_t *s, asf_object_t *p_obj,
749 asf_object_t *p_father )
754 if( !p_obj ) return( 0 );
756 if( ASF_ReadObjectCommon( s, p_obj ) )
758 msg_Warn( (vlc_object_t*)s, "cannot read one asf object" );
761 p_obj->common.p_father = p_father;
762 p_obj->common.p_first = NULL;
763 p_obj->common.p_next = NULL;
764 p_obj->common.p_last = NULL;
766 if( p_obj->common.i_object_size < 24 )
768 msg_Warn( (vlc_object_t*)s, "found a corrupted asf object (size<24)" );
772 /* find this object */
773 for( i_index = 0; ; i_index++ )
775 if( ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
776 &p_obj->common.i_object_id ) ||
777 ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
778 &asf_object_null_guid ) )
783 p_obj->common.i_type = ASF_Object_Function[i_index].i_type;
785 /* Now load this object */
786 if( ASF_Object_Function[i_index].ASF_ReadObject_function == NULL )
788 msg_Warn( (vlc_object_t*)s, "unknown asf object (not loaded)" );
789 i_result = VLC_SUCCESS;
793 /* XXX ASF_ReadObject_function realloc *pp_obj XXX */
795 (ASF_Object_Function[i_index].ASF_ReadObject_function)( s, p_obj );
798 /* link this object with father */
801 if( p_father->common.p_first )
803 p_father->common.p_last->common.p_next = p_obj;
807 p_father->common.p_first = p_obj;
809 p_father->common.p_last = p_obj;
815 static void ASF_FreeObject( stream_t *s, asf_object_t *p_obj )
818 asf_object_t *p_child;
822 /* Free all child object */
823 p_child = p_obj->common.p_first;
826 asf_object_t *p_next;
827 p_next = p_child->common.p_next;
828 ASF_FreeObject( s, p_child );
832 /* find this object */
833 for( i_index = 0; ; i_index++ )
835 if( ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
836 &p_obj->common.i_object_id )||
837 ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
838 &asf_object_null_guid ) )
844 /* Now free this object */
845 if( ASF_Object_Function[i_index].ASF_FreeObject_function == NULL )
847 msg_Warn( (vlc_object_t*)s,
848 "unknown asf object " GUID_FMT,
849 GUID_PRINT( p_obj->common.i_object_id ) );
854 msg_Dbg( (vlc_object_t*)s,
855 "free asf object " GUID_FMT,
856 GUID_PRINT( p_obj->common.i_object_id ) );
858 (ASF_Object_Function[i_index].ASF_FreeObject_function)( p_obj );
864 /*****************************************************************************
865 * ASF_ReadObjetRoot : parse the entire stream/file
866 *****************************************************************************/
867 asf_object_root_t *ASF_ReadObjectRoot( stream_t *s, int b_seekable )
869 asf_object_root_t *p_root = malloc( sizeof( asf_object_root_t ) );
872 p_root->i_type = ASF_OBJECT_TYPE_ROOT;
873 memcpy( &p_root->i_object_id, &asf_object_null_guid, sizeof( guid_t ) );
874 p_root->i_object_pos = stream_Tell( s );
875 p_root->i_object_size = 0;
876 p_root->p_first = NULL;
877 p_root->p_last = NULL;
878 p_root->p_next = NULL;
879 p_root->p_hdr = NULL;
880 p_root->p_data = NULL;
882 p_root->p_index = NULL;
883 p_root->p_hdr_ext = NULL;
884 p_root->p_metadata = NULL;
888 p_obj = malloc( sizeof( asf_object_t ) );
890 if( ASF_ReadObject( s, p_obj, (asf_object_t*)p_root ) )
895 switch( p_obj->common.i_type )
897 case( ASF_OBJECT_TYPE_HEADER ):
898 p_root->p_hdr = (asf_object_header_t*)p_obj;
900 case( ASF_OBJECT_TYPE_DATA ):
901 p_root->p_data = (asf_object_data_t*)p_obj;
903 case( ASF_OBJECT_TYPE_INDEX ):
904 p_root->p_index = (asf_object_index_t*)p_obj;
906 case( ASF_OBJECT_TYPE_HEADER_EXTENSION ):
907 p_root->p_hdr_ext = (asf_object_header_extension_t*)p_obj;
910 msg_Warn( (vlc_object_t*)s, "unknow object found" );
913 if( p_obj->common.i_type == ASF_OBJECT_TYPE_DATA &&
914 p_obj->common.i_object_size <= 50 )
916 /* probably a dump of broadcasted asf */
919 if( !b_seekable && p_root->p_hdr && p_root->p_data )
921 /* For unseekable stream it's enough to play */
925 if( ASF_NextObject( s, p_obj ) ) /* Go to the next object */
931 if( p_root->p_hdr != NULL && p_root->p_data != NULL )
933 p_root->p_fp = ASF_FindObject( p_root->p_hdr,
934 &asf_object_file_properties_guid, 0 );
939 if( p_root->p_hdr_ext != NULL )
942 ASF_FindObject( p_root->p_hdr_ext,
943 &asf_object_metadata_guid, 0 );
948 msg_Warn( (vlc_object_t*)s, "cannot find file properties object" );
952 ASF_FreeObjectRoot( s, p_root );
956 void ASF_FreeObjectRoot( stream_t *s, asf_object_root_t *p_root )
960 p_obj = p_root->p_first;
963 asf_object_t *p_next;
964 p_next = p_obj->common.p_next;
965 ASF_FreeObject( s, p_obj );
971 int __ASF_CountObject( asf_object_t *p_obj, const guid_t *p_guid )
974 asf_object_t *p_child;
976 if( !p_obj ) return( 0 );
979 p_child = p_obj->common.p_first;
982 if( ASF_CmpGUID( &p_child->common.i_object_id, p_guid ) )
986 p_child = p_child->common.p_next;
991 void *__ASF_FindObject( asf_object_t *p_obj, const guid_t *p_guid,
994 asf_object_t *p_child;
996 p_child = p_obj->common.p_first;
1000 if( ASF_CmpGUID( &p_child->common.i_object_id, p_guid ) )
1010 p_child = p_child->common.p_next;