1 /*****************************************************************************
2 * libasf.c : asf stream demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2003 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
29 #include <vlc_common.h>
32 #include <vlc_demux.h>
34 #include <vlc_codecs.h> /* BITMAPINFOHEADER, WAVEFORMATEX */
39 #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"
40 #define GUID_PRINT( guid ) \
44 (guid).v4[0],(guid).v4[1],(guid).v4[2],(guid).v4[3], \
45 (guid).v4[4],(guid).v4[5],(guid).v4[6],(guid).v4[7]
48 * They ensure that invalid reads will not create problems.
49 * They are expansion safe
50 * They make the following assumptions:
51 * const uint8_t *p_peek exists and points to the start of a buffer
52 * int i_peek gives the size of the buffer pointed by p_peek
53 * const uint8_t *p_data exits and points to the data inside p_peek to be read.
56 * Check that n bytes can be read */
57 static inline bool AsfObjectHelperHave( const uint8_t *p_peek, int i_peek, const uint8_t *p_current, int i_wanted )
59 if( i_wanted < 0 || i_wanted > i_peek )
61 return &p_current[i_wanted] <= &p_peek[i_peek];
63 #define ASF_HAVE(n) AsfObjectHelperHave( p_peek, i_peek, p_data, n )
66 * Skip n bytes if possible */
67 static inline void AsfObjectHelperSkip( const uint8_t *p_peek, int i_peek, uint8_t **pp_data, int i_wanted )
69 if( AsfObjectHelperHave( p_peek, i_peek, *pp_data, i_wanted ) )
72 *pp_data = (uint8_t*)&p_peek[i_peek];
74 #define ASF_SKIP(n) AsfObjectHelperSkip( p_peek, i_peek, (uint8_t**)&p_data, n )
77 * Read X byte if possible, else return 0 */
78 #define ASF_FUNCTION_READ_X(type, x, cmd ) \
79 static inline type AsfObjectHelperRead##x( const uint8_t *p_peek, int i_peek, uint8_t **pp_data ) { \
80 uint8_t *p_data = *pp_data; \
87 ASF_FUNCTION_READ_X( uint8_t, 1, *p_data )
88 ASF_FUNCTION_READ_X( uint16_t, 2, GetWLE(p_data) )
89 ASF_FUNCTION_READ_X( uint32_t, 4, GetDWLE(p_data) )
90 ASF_FUNCTION_READ_X( uint64_t, 8, GetQWLE(p_data) )
91 #define ASF_READ1() AsfObjectHelperRead1( p_peek, i_peek, (uint8_t**)&p_data )
92 #define ASF_READ2() AsfObjectHelperRead2( p_peek, i_peek, (uint8_t**)&p_data )
93 #define ASF_READ4() AsfObjectHelperRead4( p_peek, i_peek, (uint8_t**)&p_data )
94 #define ASF_READ8() AsfObjectHelperRead8( p_peek, i_peek, (uint8_t**)&p_data )
97 * Read a string of n/2 wchar long ie n bytes. Do a stupid conversion (suppose latin1)
98 * Return allocated "" if not possible */
99 static char *AsfObjectHelperReadString( const uint8_t *p_peek, int i_peek, uint8_t **pp_data, int i_size )
101 uint8_t *p_data = *pp_data;
103 if( ASF_HAVE(i_size) )
105 psz_string = calloc( i_size/2 + 1, sizeof( char ) );
109 for( i = 0; i < i_size/2; i++ )
110 psz_string[i] = GetWLE( &p_data[2*i] );
111 psz_string[i_size/2] = '\0'; \
116 psz_string = strdup("");
122 #define ASF_READS(n) AsfObjectHelperReadString( p_peek, i_peek, (uint8_t**)&p_data, n )
124 /****************************************************************************
126 ****************************************************************************/
127 static int ASF_ReadObject( stream_t *, asf_object_t *, asf_object_t * );
129 /****************************************************************************
131 ****************************************************************************/
132 void ASF_GetGUID( guid_t *p_guid, const uint8_t *p_data )
134 p_guid->v1 = GetDWLE( p_data );
135 p_guid->v2 = GetWLE( p_data + 4);
136 p_guid->v3 = GetWLE( p_data + 6);
137 memcpy( p_guid->v4, p_data + 8, 8 );
140 bool ASF_CmpGUID( const guid_t *p_guid1, const guid_t *p_guid2 )
142 if( (p_guid1->v1 != p_guid2->v1 )||
143 (p_guid1->v2 != p_guid2->v2 )||
144 (p_guid1->v3 != p_guid2->v3 )||
145 ( memcmp( p_guid1->v4, p_guid2->v4,8 )) )
152 /****************************************************************************
154 ****************************************************************************/
155 static int ASF_ReadObjectCommon( stream_t *s, asf_object_t *p_obj )
157 asf_object_common_t *p_common = &p_obj->common;
158 const uint8_t *p_peek;
160 if( stream_Peek( s, &p_peek, 24 ) < 24 )
163 ASF_GetGUID( &p_common->i_object_id, p_peek );
164 p_common->i_object_size = GetQWLE( p_peek + 16 );
165 p_common->i_object_pos = stream_Tell( s );
166 p_common->p_next = NULL;
170 "found object guid: " GUID_FMT " size:%"PRId64,
171 GUID_PRINT( p_common->i_object_id ),
172 p_common->i_object_size );
178 static int ASF_NextObject( stream_t *s, asf_object_t *p_obj )
183 if( ASF_ReadObjectCommon( s, &obj ) )
189 if( p_obj->common.i_object_size <= 0 )
192 if( p_obj->common.p_father &&
193 p_obj->common.p_father->common.i_object_size != 0 )
195 if( p_obj->common.p_father->common.i_object_pos +
196 p_obj->common.p_father->common.i_object_size <
197 p_obj->common.i_object_pos + p_obj->common.i_object_size + 24 )
198 /* 24 is min size of an object */
205 return stream_Seek( s, p_obj->common.i_object_pos +
206 p_obj->common.i_object_size );
209 static void ASF_FreeObject_Null( asf_object_t *pp_obj )
214 static int ASF_ReadObject_Header( stream_t *s, asf_object_t *p_obj )
216 asf_object_header_t *p_hdr = &p_obj->header;
217 asf_object_t *p_subobj;
219 const uint8_t *p_peek;
221 if( ( i_peek = stream_Peek( s, &p_peek, 30 ) ) < 30 )
224 p_hdr->i_sub_object_count = GetDWLE( p_peek + 24 );
225 p_hdr->i_reserved1 = p_peek[28];
226 p_hdr->i_reserved2 = p_peek[29];
227 p_hdr->p_first = NULL;
228 p_hdr->p_last = NULL;
232 "read \"header object\" subobj:%d, reserved1:%d, reserved2:%d",
233 p_hdr->i_sub_object_count,
235 p_hdr->i_reserved2 );
238 /* Cannot fail as peek succeed */
239 stream_Read( s, NULL, 30 );
241 /* Now load sub object */
244 p_subobj = malloc( sizeof( asf_object_t ) );
246 if( !p_subobj || ASF_ReadObject( s, p_subobj, (asf_object_t*)p_hdr ) )
251 if( ASF_NextObject( s, p_subobj ) ) /* Go to the next object */
257 static int ASF_ReadObject_Data( stream_t *s, asf_object_t *p_obj )
259 asf_object_data_t *p_data = &p_obj->data;
261 const uint8_t *p_peek;
263 if( ( i_peek = stream_Peek( s, &p_peek, 50 ) ) < 50 )
266 ASF_GetGUID( &p_data->i_file_id, p_peek + 24 );
267 p_data->i_total_data_packets = GetQWLE( p_peek + 40 );
268 p_data->i_reserved = GetWLE( p_peek + 48 );
272 "read \"data object\" file_id:" GUID_FMT " total data packet:"
273 "%"PRId64" reserved:%d",
274 GUID_PRINT( p_data->i_file_id ),
275 p_data->i_total_data_packets,
276 p_data->i_reserved );
282 static int ASF_ReadObject_Index( stream_t *s, asf_object_t *p_obj )
284 asf_object_index_t *p_index = &p_obj->index;
285 const uint8_t *p_peek;
288 /* We just ignore error on the index */
289 if( stream_Peek( s, &p_peek, p_index->i_object_size ) <
290 __MAX( (int64_t)p_index->i_object_size, 56 ) )
293 ASF_GetGUID( &p_index->i_file_id, p_peek + 24 );
294 p_index->i_index_entry_time_interval = GetQWLE( p_peek + 40 );
295 p_index->i_max_packet_count = GetDWLE( p_peek + 48 );
296 p_index->i_index_entry_count = GetDWLE( p_peek + 52 );
297 p_index->index_entry = NULL;
301 "read \"index object\" file_id:" GUID_FMT
302 " index_entry_time_interval:%"PRId64" max_packet_count:%d "
303 "index_entry_count:%ld",
304 GUID_PRINT( p_index->i_file_id ),
305 p_index->i_index_entry_time_interval,
306 p_index->i_max_packet_count,
307 (long int)p_index->i_index_entry_count );
310 /* Sanity checking */
311 if( p_index->i_index_entry_count > (p_index->i_object_size - 56) / 6 )
312 p_index->i_index_entry_count = (p_index->i_object_size - 56) / 6;
314 p_index->index_entry = calloc( p_index->i_index_entry_count,
315 sizeof(asf_index_entry_t) );
316 if( !p_index->index_entry )
319 for( i = 0, p_peek += 56; i < p_index->i_index_entry_count; i++, p_peek += 6 )
321 p_index->index_entry[i].i_packet_number = GetDWLE( p_peek );
322 p_index->index_entry[i].i_packet_count = GetDWLE( p_peek + 4 );
328 static void ASF_FreeObject_Index( asf_object_t *p_obj )
330 asf_object_index_t *p_index = &p_obj->index;
332 FREENULL( p_index->index_entry );
335 static int ASF_ReadObject_file_properties( stream_t *s, asf_object_t *p_obj )
337 asf_object_file_properties_t *p_fp = &p_obj->file_properties;
339 const uint8_t *p_peek;
341 if( ( i_peek = stream_Peek( s, &p_peek, 104 ) ) < 104 )
344 ASF_GetGUID( &p_fp->i_file_id, p_peek + 24 );
345 p_fp->i_file_size = GetQWLE( p_peek + 40 );
346 p_fp->i_creation_date = GetQWLE( p_peek + 48 );
347 p_fp->i_data_packets_count = GetQWLE( p_peek + 56 );
348 p_fp->i_play_duration = GetQWLE( p_peek + 64 );
349 p_fp->i_send_duration = GetQWLE( p_peek + 72 );
350 p_fp->i_preroll = GetQWLE( p_peek + 80 );
351 p_fp->i_flags = GetDWLE( p_peek + 88 );
352 p_fp->i_min_data_packet_size = GetDWLE( p_peek + 92 );
353 p_fp->i_max_data_packet_size = GetDWLE( p_peek + 96 );
354 p_fp->i_max_bitrate = GetDWLE( p_peek + 100 );
358 "read \"file properties object\" file_id:" GUID_FMT
359 " file_size:%"PRId64" creation_date:%"PRId64" data_packets_count:"
360 "%"PRId64" play_duration:%"PRId64" send_duration:%"PRId64" preroll:%"PRId64
361 " flags:%d min_data_packet_size:%d "
362 " max_data_packet_size:%d max_bitrate:%d",
363 GUID_PRINT( p_fp->i_file_id ), p_fp->i_file_size,
364 p_fp->i_creation_date, p_fp->i_data_packets_count,
365 p_fp->i_play_duration, p_fp->i_send_duration,
366 p_fp->i_preroll, p_fp->i_flags,
367 p_fp->i_min_data_packet_size, p_fp->i_max_data_packet_size,
368 p_fp->i_max_bitrate );
374 static void ASF_FreeObject_metadata( asf_object_t *p_obj )
376 asf_object_metadata_t *p_meta = &p_obj->metadata;
379 for( i = 0; i < p_meta->i_record_entries_count; i++ )
381 free( p_meta->record[i].psz_name );
382 free( p_meta->record[i].p_data );
384 free( p_meta->record );
387 static int ASF_ReadObject_metadata( stream_t *s, asf_object_t *p_obj )
389 asf_object_metadata_t *p_meta = &p_obj->metadata;
393 const uint8_t *p_peek, *p_data;
398 if( ( i_peek = stream_Peek( s, &p_peek, p_meta->i_object_size ) ) <
399 __MAX( (int64_t)p_meta->i_object_size, 26 ) )
402 p_meta->i_record_entries_count = GetWLE( p_peek + 24 );
404 p_data = p_peek + 26;
406 p_meta->record = calloc( p_meta->i_record_entries_count,
407 sizeof(asf_metadata_record_t) );
408 if( !p_meta->record )
411 for( i = 0; i < p_meta->i_record_entries_count; i++ )
413 asf_metadata_record_t *p_record = &p_meta->record[i];
417 if( !ASF_HAVE( 2+2+2+2+4 ) )
420 if( ASF_READ2() != 0 )
423 p_record->i_stream = ASF_READ2();
424 i_name = ASF_READ2();
425 p_record->i_type = ASF_READ2();
426 i_data = ASF_READ4();
428 if( !ASF_HAVE( i_name + i_data ) )
432 p_record->psz_name = ASF_READS( i_name );
435 if( p_record->i_type == ASF_METADATA_TYPE_STRING )
437 p_record->p_data = ASF_READS( i_data );
438 p_record->i_data = i_data/2; /* FIXME Is that needed ? */
440 else if( p_record->i_type == ASF_METADATA_TYPE_BYTE )
442 p_record->p_data = malloc( i_data );
443 p_record->i_data = i_data;
444 if( p_record->p_data && i_data > 0 )
445 memcpy( p_record->p_data, p_data, i_data );
449 else if( p_record->i_type == ASF_METADATA_TYPE_QWORD )
451 p_record->i_val = ASF_READ8();
453 else if( p_record->i_type == ASF_METADATA_TYPE_DWORD )
455 p_record->i_val = ASF_READ4();
457 else if( p_record->i_type == ASF_METADATA_TYPE_WORD )
459 p_record->i_val = ASF_READ2();
461 else if( p_record->i_type == ASF_METADATA_TYPE_BOOL )
463 p_record->i_val = ASF_READ2();
471 p_meta->i_record_entries_count = i;
475 "read \"metadata object\" %d entries",
476 p_meta->i_record_entries_count );
477 for( j = 0; j < p_meta->i_record_entries_count; j++ )
479 asf_metadata_record_t *p_rec = &p_meta->record[j];
481 if( p_rec->i_type == ASF_METADATA_TYPE_STRING )
482 msg_Dbg( s, " - %s=%s",
483 p_rec->psz_name, p_rec->p_data );
484 else if( p_rec->i_type == ASF_METADATA_TYPE_BYTE )
485 msg_Dbg( s, " - %s (%i bytes)",
486 p_rec->psz_name, p_rec->i_data );
488 msg_Dbg( s, " - %s=%"PRId64,
489 p_rec->psz_name, p_rec->i_val );
496 static int ASF_ReadObject_header_extension( stream_t *s, asf_object_t *p_obj )
498 asf_object_header_extension_t *p_he = &p_obj->header_extension;
500 const uint8_t *p_peek;
502 if( ( i_peek = stream_Peek( s, &p_peek, p_he->i_object_size ) ) < 46)
506 ASF_GetGUID( &p_he->i_reserved1, p_peek + 24 );
507 p_he->i_reserved2 = GetWLE( p_peek + 40 );
508 p_he->i_header_extension_size = GetDWLE( p_peek + 42 );
509 if( p_he->i_header_extension_size )
511 if( (unsigned int)(i_peek-46) < p_he->i_header_extension_size )
514 p_he->p_header_extension_data =
515 malloc( p_he->i_header_extension_size );
516 if( !p_he->p_header_extension_data )
519 memcpy( p_he->p_header_extension_data, p_peek + 46,
520 p_he->i_header_extension_size );
524 p_he->p_header_extension_data = NULL;
529 "read \"header extension object\" reserved1:" GUID_FMT
530 " reserved2:%d header_extension_size:%d",
531 GUID_PRINT( p_he->i_reserved1 ), p_he->i_reserved2,
532 p_he->i_header_extension_size );
535 if( !p_he->i_header_extension_size ) return VLC_SUCCESS;
537 /* Read the extension objects */
538 stream_Read( s, NULL, 46 );
541 asf_object_t *p_obj = malloc( sizeof( asf_object_t ) );
543 if( !p_obj || ASF_ReadObject( s, p_obj, (asf_object_t*)p_he ) )
549 if( ASF_NextObject( s, p_obj ) ) /* Go to the next object */
558 static void ASF_FreeObject_header_extension( asf_object_t *p_obj )
560 asf_object_header_extension_t *p_he = &p_obj->header_extension;
562 FREENULL( p_he->p_header_extension_data );
565 static int ASF_ReadObject_stream_properties( stream_t *s, asf_object_t *p_obj )
567 asf_object_stream_properties_t *p_sp = &p_obj->stream_properties;
569 const uint8_t *p_peek;
571 if( ( i_peek = stream_Peek( s, &p_peek, p_sp->i_object_size ) ) < 78 )
574 ASF_GetGUID( &p_sp->i_stream_type, p_peek + 24 );
575 ASF_GetGUID( &p_sp->i_error_correction_type, p_peek + 40 );
576 p_sp->i_time_offset = GetQWLE( p_peek + 56 );
577 p_sp->i_type_specific_data_length = GetDWLE( p_peek + 64 );
578 p_sp->i_error_correction_data_length = GetDWLE( p_peek + 68 );
579 p_sp->i_flags = GetWLE( p_peek + 72 );
580 p_sp->i_stream_number = p_sp->i_flags&0x07f;
581 p_sp->i_reserved = GetDWLE( p_peek + 74 );
584 if( p_sp->i_type_specific_data_length )
586 if( i_peek < p_sp->i_type_specific_data_length )
589 p_sp->p_type_specific_data =
590 malloc( p_sp->i_type_specific_data_length );
591 if( !p_sp->p_type_specific_data )
594 memcpy( p_sp->p_type_specific_data, p_peek + 78,
595 p_sp->i_type_specific_data_length );
596 i_peek -= p_sp->i_type_specific_data_length;
599 if( p_sp->i_error_correction_data_length )
601 if( i_peek < p_sp->i_error_correction_data_length )
603 free( p_sp->p_type_specific_data );
607 p_sp->p_error_correction_data =
608 malloc( p_sp->i_error_correction_data_length );
609 if( !p_sp->p_error_correction_data )
611 free( p_sp->p_type_specific_data );
614 memcpy( p_sp->p_error_correction_data,
615 p_peek + 78 + p_sp->i_type_specific_data_length,
616 p_sp->i_error_correction_data_length );
621 "read \"stream Properties object\" stream_type:" GUID_FMT
622 " error_correction_type:" GUID_FMT " time_offset:%"PRId64
623 " type_specific_data_length:%d error_correction_data_length:%d"
624 " flags:0x%x stream_number:%d",
625 GUID_PRINT( p_sp->i_stream_type ),
626 GUID_PRINT( p_sp->i_error_correction_type ),
628 p_sp->i_type_specific_data_length,
629 p_sp->i_error_correction_data_length,
631 p_sp->i_stream_number );
637 static void ASF_FreeObject_stream_properties( asf_object_t *p_obj )
639 asf_object_stream_properties_t *p_sp = &p_obj->stream_properties;
641 FREENULL( p_sp->p_type_specific_data );
642 FREENULL( p_sp->p_error_correction_data );
646 static int ASF_ReadObject_codec_list( stream_t *s, asf_object_t *p_obj )
648 asf_object_codec_list_t *p_cl = &p_obj->codec_list;
650 const uint8_t *p_peek, *p_data;
652 unsigned int i_codec;
654 if( ( i_peek = stream_Peek( s, &p_peek, p_cl->i_object_size ) ) < 44 )
657 ASF_GetGUID( &p_cl->i_reserved, p_peek + 24 );
658 p_cl->i_codec_entries_count = GetWLE( p_peek + 40 );
660 p_data = p_peek + 44;
662 if( p_cl->i_codec_entries_count > 0 )
664 p_cl->codec = calloc( p_cl->i_codec_entries_count,
665 sizeof( asf_codec_entry_t ) );
669 for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
671 asf_codec_entry_t *p_codec = &p_cl->codec[i_codec];
673 if( !ASF_HAVE( 2+2+2 ) )
677 p_codec->i_type = ASF_READ2();
679 /* XXX the length here are the number of *unicode* characters and
680 * not of bytes like nearly every elsewhere */
683 p_codec->psz_name = ASF_READS( 2*ASF_READ2() );
686 p_codec->psz_description = ASF_READS( 2*ASF_READ2() );
688 /* opaque information */
689 p_codec->i_information_length = ASF_READ2();
690 if( p_codec->i_information_length > 0 && ASF_HAVE( p_codec->i_information_length ) )
692 p_codec->p_information = malloc( p_codec->i_information_length );
693 if( p_codec->p_information )
694 memcpy( p_codec->p_information, p_data, p_codec->i_information_length );
696 p_codec->i_information_length = 0;
697 p_data += p_codec->i_information_length;
700 p_cl->i_codec_entries_count = i_codec;
704 msg_Dbg( s, "read \"codec list object\" reserved_guid:" GUID_FMT
705 " codec_entries_count:%d",
706 GUID_PRINT( p_cl->i_reserved ), p_cl->i_codec_entries_count );
708 for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
710 const asf_codec_entry_t *p_codec = &p_cl->codec[i_codec];
712 msg_Dbg( s, " - codec[%d] %s name:\"%s\" "
713 "description:\"%s\" information_length:%d",
714 i_codec, ( p_codec->i_type == ASF_CODEC_TYPE_VIDEO ) ?
715 "video" : ( ( p_codec->i_type == ASF_CODEC_TYPE_AUDIO ) ?
716 "audio" : "unknown" ),
717 p_codec->psz_name, p_codec->psz_description,
718 p_codec->i_information_length );
725 static void ASF_FreeObject_codec_list( asf_object_t *p_obj )
727 asf_object_codec_list_t *p_cl = &p_obj->codec_list;
728 unsigned int i_codec;
730 for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
732 asf_codec_entry_t *p_codec = &p_cl->codec[i_codec];
734 FREENULL( p_codec->psz_name );
735 FREENULL( p_codec->psz_description );
736 FREENULL( p_codec->p_information );
738 FREENULL( p_cl->codec );
741 /* Microsoft should go to hell. This time the length give number of bytes
742 * and for the some others object, length give char16 count ... */
743 static int ASF_ReadObject_content_description(stream_t *s, asf_object_t *p_obj)
745 asf_object_content_description_t *p_cd = &p_obj->content_description;
746 const uint8_t *p_peek, *p_data;
747 int i_peek, i_title, i_artist, i_copyright, i_description, i_rating;
748 vlc_iconv_t cd = (vlc_iconv_t)-1;
749 const char *ib = NULL;
751 size_t i_ibl, i_obl, i_len;
753 if( ( i_peek = stream_Peek( s, &p_peek, p_cd->i_object_size ) ) < 34 )
756 cd = vlc_iconv_open("UTF-8", "UTF-16LE");
757 if( cd == (vlc_iconv_t)-1 )
759 msg_Err( s, "vlc_iconv_open failed" );
763 /* FIXME i_size*3 is the worst case. */
764 #define GETSTRINGW( psz_str, i_size ) do { \
765 psz_str = calloc( i_size*3+1, sizeof(char) ); \
767 ib = (const char *)p_data; \
771 i_len = vlc_iconv(cd, &ib, &i_ibl, &ob, &i_obl); \
775 p_data = p_peek + 24;
777 i_title = ASF_READ2();
778 i_artist = ASF_READ2();
779 i_copyright = ASF_READ2();
780 i_description = ASF_READ2();
781 i_rating = ASF_READ2();
783 if( !ASF_HAVE( i_title+i_artist+i_copyright+i_description+i_rating ) )
785 vlc_iconv_close( cd );
789 GETSTRINGW( p_cd->psz_title, i_title );
790 GETSTRINGW( p_cd->psz_artist, i_artist );
791 GETSTRINGW( p_cd->psz_copyright, i_copyright );
792 GETSTRINGW( p_cd->psz_description, i_description );
793 GETSTRINGW( p_cd->psz_rating, i_rating );
799 "read \"content description object\" title:\"%s\" artist:\"%s\" copyright:\"%s\" description:\"%s\" rating:\"%s\"",
803 p_cd->psz_description,
811 static void ASF_FreeObject_content_description( asf_object_t *p_obj)
813 asf_object_content_description_t *p_cd = &p_obj->content_description;
815 FREENULL( p_cd->psz_title );
816 FREENULL( p_cd->psz_artist );
817 FREENULL( p_cd->psz_copyright );
818 FREENULL( p_cd->psz_description );
819 FREENULL( p_cd->psz_rating );
823 static int ASF_ReadObject_language_list(stream_t *s, asf_object_t *p_obj)
825 asf_object_language_list_t *p_ll = &p_obj->language_list;
826 const uint8_t *p_peek, *p_data;
830 if( ( i_peek = stream_Peek( s, &p_peek, p_ll->i_object_size ) ) < 26 )
833 p_data = &p_peek[24];
835 p_ll->i_language = ASF_READ2();
836 if( p_ll->i_language > 0 )
838 p_ll->ppsz_language = calloc( p_ll->i_language, sizeof( char *) );
839 if( !p_ll->ppsz_language )
842 for( i = 0; i < p_ll->i_language; i++ )
846 p_ll->ppsz_language[i] = ASF_READS( ASF_READ1() );
848 p_ll->i_language = i;
852 msg_Dbg( s, "read \"language list object\" %d entries",
854 for( i = 0; i < p_ll->i_language; i++ )
855 msg_Dbg( s, " - '%s'",
856 p_ll->ppsz_language[i] );
861 static void ASF_FreeObject_language_list( asf_object_t *p_obj)
863 asf_object_language_list_t *p_ll = &p_obj->language_list;
866 for( i = 0; i < p_ll->i_language; i++ )
867 FREENULL( p_ll->ppsz_language[i] );
868 FREENULL( p_ll->ppsz_language );
871 /* Stream bitrate properties */
872 static int ASF_ReadObject_stream_bitrate_properties( stream_t *s,
875 asf_object_stream_bitrate_properties_t *p_sb = &p_obj->stream_bitrate;
876 const uint8_t *p_peek, *p_data;
880 if( ( i_peek = stream_Peek( s, &p_peek, p_sb->i_object_size ) ) < 26 )
883 p_data = &p_peek[24];
885 p_sb->i_bitrate = ASF_READ2();
886 if( p_sb->i_bitrate > 127 )
887 p_sb->i_bitrate = 127; /* Buggy ? */
888 for( i = 0; i < p_sb->i_bitrate; i++ )
890 if( !ASF_HAVE(2 + 4) )
892 p_sb->bitrate[i].i_stream_number = ASF_READ2()& 0x7f;
893 p_sb->bitrate[i].i_avg_bitrate = ASF_READ4();
898 msg_Dbg( s,"read \"stream bitrate properties object\"" );
899 for( i = 0; i < p_sb->i_bitrate; i++ )
901 msg_Dbg( s," - stream=%d bitrate=%d",
902 p_sb->bitrate[i].i_stream_number,
903 p_sb->bitrate[i].i_avg_bitrate );
908 static void ASF_FreeObject_stream_bitrate_properties( asf_object_t *p_obj)
913 static int ASF_ReadObject_extended_stream_properties( stream_t *s,
916 asf_object_extended_stream_properties_t *p_esp = &p_obj->ext_stream;
917 const uint8_t *p_peek, *p_data;
920 if( ( i_peek = stream_Peek( s, &p_peek, p_esp->i_object_size ) ) < 88 )
923 p_data = &p_peek[24];
925 p_esp->i_start_time = GetQWLE( &p_data[0] );
926 p_esp->i_end_time = GetQWLE( &p_data[8] );
927 p_esp->i_data_bitrate = GetDWLE( &p_data[16] );
928 p_esp->i_buffer_size = GetDWLE( &p_data[20] );
929 p_esp->i_initial_buffer_fullness = GetDWLE( &p_data[24] );
930 p_esp->i_alternate_data_bitrate = GetDWLE( &p_data[28] );
931 p_esp->i_alternate_buffer_size = GetDWLE( &p_data[32] );
932 p_esp->i_alternate_initial_buffer_fullness = GetDWLE( &p_data[36] );
933 p_esp->i_maximum_object_size = GetDWLE( &p_data[40] );
934 p_esp->i_flags = GetDWLE( &p_data[44] );
935 p_esp->i_stream_number = GetWLE( &p_data[48] );
936 p_esp->i_language_index = GetWLE( &p_data[50] );
937 p_esp->i_average_time_per_frame= GetQWLE( &p_data[52] );
938 p_esp->i_stream_name_count = GetWLE( &p_data[60] );
939 p_esp->i_payload_extension_system_count = GetWLE( &p_data[62] );
943 p_esp->pi_stream_name_language = calloc( p_esp->i_stream_name_count,
945 p_esp->ppsz_stream_name = calloc( p_esp->i_stream_name_count,
947 if( !p_esp->pi_stream_name_language ||
948 !p_esp->ppsz_stream_name )
950 free( p_esp->pi_stream_name_language );
951 free( p_esp->ppsz_stream_name );
954 for( i = 0; i < p_esp->i_stream_name_count; i++ )
956 if( !ASF_HAVE( 2+2 ) )
958 p_esp->pi_stream_name_language[i] = ASF_READ2();
959 p_esp->ppsz_stream_name[i] = ASF_READS( ASF_READ2() );
961 p_esp->i_stream_name_count = i;
963 for( i = 0; i < p_esp->i_payload_extension_system_count; i++ )
965 ASF_SKIP( 16 ); // GUID
967 ASF_SKIP( ASF_READ4() );
971 if( p_data < &p_peek[i_peek] )
974 /* Cannot fail as peek succeed */
975 stream_Read( s, NULL, p_data - p_peek );
977 p_sp = malloc( sizeof( asf_object_t ) );
979 if( !p_sp || ASF_ReadObject( s, p_sp, NULL ) )
985 /* This p_sp will be inserted by ReadRoot later */
986 p_esp->p_sp = (asf_object_stream_properties_t*)p_sp;
991 msg_Dbg( s, "read \"extended stream properties object\":" );
992 msg_Dbg( s, " - start=%"PRId64" end=%"PRId64,
993 p_esp->i_start_time, p_esp->i_end_time );
994 msg_Dbg( s, " - data bitrate=%d buffer=%d initial fullness=%d",
995 p_esp->i_data_bitrate,
996 p_esp->i_buffer_size,
997 p_esp->i_initial_buffer_fullness );
998 msg_Dbg( s, " - alternate data bitrate=%d buffer=%d initial fullness=%d",
999 p_esp->i_alternate_data_bitrate,
1000 p_esp->i_alternate_buffer_size,
1001 p_esp->i_alternate_initial_buffer_fullness );
1002 msg_Dbg( s, " - maximum object size=%d", p_esp->i_maximum_object_size );
1003 msg_Dbg( s, " - flags=0x%x", p_esp->i_flags );
1004 msg_Dbg( s, " - stream number=%d language=%d",
1005 p_esp->i_stream_number, p_esp->i_language_index );
1006 msg_Dbg( s, " - average time per frame=%"PRId64,
1007 p_esp->i_average_time_per_frame );
1008 msg_Dbg( s, " - stream name count=%d", p_esp->i_stream_name_count );
1009 for( i = 0; i < p_esp->i_stream_name_count; i++ )
1010 msg_Dbg( s, " - lang id=%d name=%s",
1011 p_esp->pi_stream_name_language[i],
1012 p_esp->ppsz_stream_name[i] );
1013 msg_Dbg( s, " - payload extension system count=%d",
1014 p_esp->i_payload_extension_system_count );
1018 static void ASF_FreeObject_extended_stream_properties( asf_object_t *p_obj)
1020 asf_object_extended_stream_properties_t *p_esp = &p_obj->ext_stream;
1023 for( i = 0; i < p_esp->i_stream_name_count; i++ )
1024 FREENULL( p_esp->ppsz_stream_name[i] );
1025 FREENULL( p_esp->pi_stream_name_language );
1026 FREENULL( p_esp->ppsz_stream_name );
1030 static int ASF_ReadObject_advanced_mutual_exclusion( stream_t *s,
1031 asf_object_t *p_obj)
1033 asf_object_advanced_mutual_exclusion_t *p_ae = &p_obj->advanced_mutual_exclusion;
1034 const uint8_t *p_peek, *p_data;
1038 if( ( i_peek = stream_Peek( s, &p_peek, p_ae->i_object_size ) ) < 42 )
1039 return VLC_EGENERIC;
1041 p_data = &p_peek[24];
1043 ASF_GetGUID( &p_ae->type, &p_data[0] );
1045 p_ae->i_stream_number_count = ASF_READ2();
1046 p_ae->pi_stream_number = calloc( p_ae->i_stream_number_count, sizeof(int) );
1048 for( i = 0; i < p_ae->i_stream_number_count; i++ )
1052 p_ae->pi_stream_number[i] = ASF_READ2();
1054 p_ae->i_stream_number_count = i;
1057 msg_Dbg( s, "read \"advanced mutual exclusion object\"" );
1058 for( i = 0; i < p_ae->i_stream_number_count; i++ )
1059 msg_Dbg( s, " - stream=%d", p_ae->pi_stream_number[i] );
1063 static void ASF_FreeObject_advanced_mutual_exclusion( asf_object_t *p_obj)
1065 asf_object_advanced_mutual_exclusion_t *p_ae = &p_obj->advanced_mutual_exclusion;
1067 FREENULL( p_ae->pi_stream_number );
1071 static int ASF_ReadObject_stream_prioritization( stream_t *s,
1072 asf_object_t *p_obj)
1074 asf_object_stream_prioritization_t *p_sp = &p_obj->stream_prioritization;
1075 const uint8_t *p_peek, *p_data;
1079 if( ( i_peek = stream_Peek( s, &p_peek, p_sp->i_object_size ) ) < 26 )
1080 return VLC_EGENERIC;
1082 p_data = &p_peek[24];
1084 p_sp->i_priority_count = ASF_READ2();
1086 p_sp->pi_priority_flag = calloc( p_sp->i_priority_count, sizeof(int) );
1087 p_sp->pi_priority_stream_number =
1088 calloc( p_sp->i_priority_count, sizeof(int) );
1090 if( !p_sp->pi_priority_flag || !p_sp->pi_priority_stream_number )
1092 free( p_sp->pi_priority_flag );
1093 free( p_sp->pi_priority_stream_number );
1097 for( i = 0; i < p_sp->i_priority_count; i++ )
1099 if( !ASF_HAVE(2+2) )
1101 p_sp->pi_priority_stream_number[i] = ASF_READ2();
1102 p_sp->pi_priority_flag[i] = ASF_READ2();
1104 p_sp->i_priority_count = i;
1107 msg_Dbg( s, "read \"stream prioritization object\"" );
1108 for( i = 0; i < p_sp->i_priority_count; i++ )
1109 msg_Dbg( s, " - Stream:%d flags=0x%x",
1110 p_sp->pi_priority_stream_number[i],
1111 p_sp->pi_priority_flag[i] );
1115 static void ASF_FreeObject_stream_prioritization( asf_object_t *p_obj)
1117 asf_object_stream_prioritization_t *p_sp = &p_obj->stream_prioritization;
1119 FREENULL( p_sp->pi_priority_stream_number );
1120 FREENULL( p_sp->pi_priority_flag );
1124 static int ASF_ReadObject_extended_content_description( stream_t *s,
1125 asf_object_t *p_obj)
1127 asf_object_extended_content_description_t *p_ec =
1128 &p_obj->extended_content_description;
1129 const uint8_t *p_peek, *p_data;
1133 if( ( i_peek = stream_Peek( s, &p_peek, p_ec->i_object_size ) ) < 26 )
1134 return VLC_EGENERIC;
1136 p_data = &p_peek[24];
1138 p_ec->i_count = ASF_READ2();
1139 p_ec->ppsz_name = calloc( p_ec->i_count, sizeof(char*) );
1140 p_ec->ppsz_value = calloc( p_ec->i_count, sizeof(char*) );
1141 if( !p_ec->ppsz_name || !p_ec->ppsz_value )
1143 free( p_ec->ppsz_name );
1144 free( p_ec->ppsz_value );
1147 for( i = 0; i < p_ec->i_count; i++ )
1152 if( !ASF_HAVE(2 + 2+2) )
1155 p_ec->ppsz_name[i] = ASF_READS( ASF_READ2() );
1158 i_type = ASF_READ2();
1159 i_size = ASF_READ2();
1163 p_ec->ppsz_value[i] = ASF_READS( i_size );
1165 else if( i_type == 1 )
1168 static const char hex[16] = "0123456789ABCDEF";
1171 p_ec->ppsz_value[i] = malloc( 2*i_size + 1 );
1172 if( p_ec->ppsz_value[i] )
1174 char *psz_value = p_ec->ppsz_value[i];
1175 for( j = 0; j < i_size; j++ )
1177 const uint8_t v = ASF_READ1();
1178 psz_value[2*j+0] = hex[v>>4];
1179 psz_value[2*j+1] = hex[v&0xf];
1181 psz_value[2*i_size] = '\0';
1184 else if( i_type == 2 )
1187 p_ec->ppsz_value[i] = strdup( ASF_READ1() ? "true" : "false" );
1190 else if( i_type == 3 )
1193 asprintf( &p_ec->ppsz_value[i], "%d", ASF_READ4() );
1195 else if( i_type == 4 )
1198 asprintf( &p_ec->ppsz_value[i], "%"PRId64, ASF_READ8() );
1200 else if( i_type == 5 )
1203 asprintf( &p_ec->ppsz_value[i], "%d", ASF_READ2() );
1207 p_ec->ppsz_value[i] = NULL;
1214 msg_Dbg( s, "read \"extended content description object\"" );
1215 for( i = 0; i < p_ec->i_count; i++ )
1216 msg_Dbg( s, " - '%s' = '%s'",
1218 p_ec->ppsz_value[i] );
1222 static void ASF_FreeObject_extended_content_description( asf_object_t *p_obj)
1224 asf_object_extended_content_description_t *p_ec =
1225 &p_obj->extended_content_description;
1228 for( i = 0; i < p_ec->i_count; i++ )
1230 FREENULL( p_ec->ppsz_name[i] );
1231 FREENULL( p_ec->ppsz_value[i] );
1233 FREENULL( p_ec->ppsz_name );
1234 FREENULL( p_ec->ppsz_value );
1239 static int ASF_ReadObject_XXX(stream_t *s, asf_object_t *p_obj)
1241 asf_object_XXX_t *p_XX =
1242 (asf_object_XXX_t *)p_obj;
1243 const uint8_t *p_peek;
1247 if( ( i_peek = stream_Peek( s, &p_peek, p_XX->i_object_size ) ) < XXX )
1248 return VLC_EGENERIC;
1250 p_data = &p_peek[24];
1254 "Read \"XXX object\"" );
1258 static void ASF_FreeObject_XXX( asf_object_t *p_obj)
1260 asf_object_XXX_t *p_XX =
1261 (asf_object_XXX_t *)p_obj;
1271 int (*ASF_ReadObject_function)( stream_t *, asf_object_t *p_obj );
1272 void (*ASF_FreeObject_function)( asf_object_t *p_obj );
1274 } ASF_Object_Function [] =
1276 { &asf_object_header_guid, ASF_OBJECT_HEADER,
1277 ASF_ReadObject_Header, ASF_FreeObject_Null },
1278 { &asf_object_data_guid, ASF_OBJECT_DATA,
1279 ASF_ReadObject_Data, ASF_FreeObject_Null },
1280 { &asf_object_index_guid, ASF_OBJECT_INDEX,
1281 ASF_ReadObject_Index, ASF_FreeObject_Index },
1282 { &asf_object_file_properties_guid, ASF_OBJECT_FILE_PROPERTIES,
1283 ASF_ReadObject_file_properties, ASF_FreeObject_Null },
1284 { &asf_object_stream_properties_guid, ASF_OBJECT_STREAM_PROPERTIES,
1285 ASF_ReadObject_stream_properties,ASF_FreeObject_stream_properties },
1286 { &asf_object_header_extension_guid, ASF_OBJECT_HEADER_EXTENSION,
1287 ASF_ReadObject_header_extension, ASF_FreeObject_header_extension},
1288 { &asf_object_metadata_guid, ASF_OBJECT_METADATA,
1289 ASF_ReadObject_metadata, ASF_FreeObject_metadata},
1290 { &asf_object_codec_list_guid, ASF_OBJECT_CODEC_LIST,
1291 ASF_ReadObject_codec_list, ASF_FreeObject_codec_list },
1292 { &asf_object_marker_guid, ASF_OBJECT_MARKER, NULL, NULL },
1293 { &asf_object_padding, ASF_OBJECT_PADDING, NULL, NULL },
1294 { &asf_object_content_description_guid, ASF_OBJECT_CONTENT_DESCRIPTION,
1295 ASF_ReadObject_content_description, ASF_FreeObject_content_description },
1296 { &asf_object_language_list, ASF_OBJECT_OTHER,
1297 ASF_ReadObject_language_list, ASF_FreeObject_language_list },
1298 { &asf_object_stream_bitrate_properties, ASF_OBJECT_OTHER,
1299 ASF_ReadObject_stream_bitrate_properties,
1300 ASF_FreeObject_stream_bitrate_properties },
1301 { &asf_object_extended_stream_properties, ASF_OBJECT_OTHER,
1302 ASF_ReadObject_extended_stream_properties,
1303 ASF_FreeObject_extended_stream_properties },
1304 { &asf_object_advanced_mutual_exclusion, ASF_OBJECT_OTHER,
1305 ASF_ReadObject_advanced_mutual_exclusion,
1306 ASF_FreeObject_advanced_mutual_exclusion },
1307 { &asf_object_stream_prioritization, ASF_OBJECT_OTHER,
1308 ASF_ReadObject_stream_prioritization,
1309 ASF_FreeObject_stream_prioritization },
1310 { &asf_object_extended_content_description, ASF_OBJECT_OTHER,
1311 ASF_ReadObject_extended_content_description,
1312 ASF_FreeObject_extended_content_description },
1314 { &asf_object_null_guid, 0, NULL, NULL }
1317 static int ASF_ReadObject( stream_t *s, asf_object_t *p_obj,
1318 asf_object_t *p_father )
1326 memset( p_obj, 0, sizeof( *p_obj ) );
1328 if( ASF_ReadObjectCommon( s, p_obj ) )
1330 msg_Warn( s, "cannot read one asf object" );
1331 return VLC_EGENERIC;
1333 p_obj->common.p_father = p_father;
1334 p_obj->common.p_first = NULL;
1335 p_obj->common.p_next = NULL;
1336 p_obj->common.p_last = NULL;
1338 if( p_obj->common.i_object_size < 24 )
1340 msg_Warn( s, "found a corrupted asf object (size<24)" );
1341 return VLC_EGENERIC;
1344 /* find this object */
1345 for( i_index = 0; ; i_index++ )
1347 if( ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1348 &p_obj->common.i_object_id ) ||
1349 ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1350 &asf_object_null_guid ) )
1355 p_obj->common.i_type = ASF_Object_Function[i_index].i_type;
1357 /* Now load this object */
1358 if( ASF_Object_Function[i_index].ASF_ReadObject_function == NULL )
1360 msg_Warn( s, "unknown asf object (not loaded)" );
1361 i_result = VLC_SUCCESS;
1365 /* XXX ASF_ReadObject_function realloc *pp_obj XXX */
1367 (ASF_Object_Function[i_index].ASF_ReadObject_function)( s, p_obj );
1370 /* link this object with father */
1371 if( p_father && ! i_result )
1373 if( p_father->common.p_first )
1375 p_father->common.p_last->common.p_next = p_obj;
1379 p_father->common.p_first = p_obj;
1381 p_father->common.p_last = p_obj;
1387 static void ASF_FreeObject( stream_t *s, asf_object_t *p_obj )
1390 asf_object_t *p_child;
1395 /* Free all child object */
1396 p_child = p_obj->common.p_first;
1399 asf_object_t *p_next;
1400 p_next = p_child->common.p_next;
1401 ASF_FreeObject( s, p_child );
1405 /* find this object */
1406 for( i_index = 0; ; i_index++ )
1408 if( ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1409 &p_obj->common.i_object_id )||
1410 ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1411 &asf_object_null_guid ) )
1417 /* Now free this object */
1418 if( ASF_Object_Function[i_index].ASF_FreeObject_function == NULL )
1421 "unknown asf object " GUID_FMT,
1422 GUID_PRINT( p_obj->common.i_object_id ) );
1428 "free asf object " GUID_FMT,
1429 GUID_PRINT( p_obj->common.i_object_id ) );
1431 (ASF_Object_Function[i_index].ASF_FreeObject_function)( p_obj );
1436 /*****************************************************************************
1437 * ASF_ObjectDumpDebug:
1438 *****************************************************************************/
1442 const char *psz_name;
1443 } ASF_ObjectDumpDebugInfo[] =
1445 { &asf_object_header_guid, "Header" },
1446 { &asf_object_data_guid, "Data" },
1447 { &asf_object_index_guid, "Index" },
1448 { &asf_object_file_properties_guid, "File Properties" },
1449 { &asf_object_stream_properties_guid, "Stream Properties" },
1450 { &asf_object_content_description_guid, "Content Description" },
1451 { &asf_object_header_extension_guid, "Header Extension" },
1452 { &asf_object_metadata_guid, "Metadata" },
1453 { &asf_object_codec_list_guid, "Codec List" },
1454 { &asf_object_marker_guid, "Marker" },
1455 { &asf_object_stream_type_audio, "Stream Type Audio" },
1456 { &asf_object_stream_type_video, "Stream Type Video" },
1457 { &asf_object_stream_type_command, "Stream Type Command" },
1458 { &asf_object_language_list, "Language List" },
1459 { &asf_object_stream_bitrate_properties, "Stream Bitrate Properties" },
1460 { &asf_object_padding, "Padding" },
1461 { &asf_object_extended_stream_properties, "Extended Stream Properties" },
1462 { &asf_object_advanced_mutual_exclusion, "Advanced Mutual Exclusion" },
1463 { &asf_object_stream_prioritization, "Stream Prioritization" },
1464 { &asf_object_extended_content_description, "Extended content description"},
1466 { NULL, "Unknown" },
1470 static void ASF_ObjectDumpDebug( vlc_object_t *p_obj,
1471 asf_object_common_t *p_node, int i_level )
1475 union asf_object_u *p_child;
1476 const char *psz_name;
1479 for( i = 0; ASF_ObjectDumpDebugInfo[i].p_id != NULL; i++ )
1481 if( ASF_CmpGUID( ASF_ObjectDumpDebugInfo[i].p_id,
1482 &p_node->i_object_id ) )
1485 psz_name = ASF_ObjectDumpDebugInfo[i].psz_name;
1487 memset( str, ' ', sizeof( str ) );
1488 for( i = 1; i < i_level; i++ )
1492 snprintf( str + 5*i_level, 1024,
1493 "+ '%s' GUID "GUID_FMT" size:%"PRIu64"pos:%"PRIu64,
1495 GUID_PRINT( p_node->i_object_id ),
1496 p_node->i_object_size, p_node->i_object_pos );
1498 msg_Dbg( p_obj, "%s", str );
1500 for( p_child = p_node->p_first; p_child != NULL;
1501 p_child = p_child->common.p_next )
1503 ASF_ObjectDumpDebug( p_obj, &p_child->common, i_level + 1 );
1507 /*****************************************************************************
1508 * ASF_ReadObjetRoot : parse the entire stream/file
1509 *****************************************************************************/
1510 asf_object_root_t *ASF_ReadObjectRoot( stream_t *s, int b_seekable )
1512 asf_object_root_t *p_root = malloc( sizeof( asf_object_root_t ) );
1513 asf_object_t *p_obj;
1518 p_root->i_type = ASF_OBJECT_ROOT;
1519 memcpy( &p_root->i_object_id, &asf_object_null_guid, sizeof( guid_t ) );
1520 p_root->i_object_pos = stream_Tell( s );
1521 p_root->i_object_size = 0;
1522 p_root->p_first = NULL;
1523 p_root->p_last = NULL;
1524 p_root->p_next = NULL;
1525 p_root->p_hdr = NULL;
1526 p_root->p_data = NULL;
1527 p_root->p_fp = NULL;
1528 p_root->p_index = NULL;
1529 p_root->p_metadata = NULL;
1533 p_obj = malloc( sizeof( asf_object_t ) );
1535 if( !p_obj || ASF_ReadObject( s, p_obj, (asf_object_t*)p_root ) )
1540 switch( p_obj->common.i_type )
1542 case( ASF_OBJECT_HEADER ):
1543 p_root->p_hdr = (asf_object_header_t*)p_obj;
1545 case( ASF_OBJECT_DATA ):
1546 p_root->p_data = (asf_object_data_t*)p_obj;
1548 case( ASF_OBJECT_INDEX ):
1549 p_root->p_index = (asf_object_index_t*)p_obj;
1552 msg_Warn( s, "unknow object found" );
1555 if( p_obj->common.i_type == ASF_OBJECT_DATA &&
1556 p_obj->common.i_object_size <= 50 )
1558 /* probably a dump of broadcasted asf */
1561 if( !b_seekable && p_root->p_hdr && p_root->p_data )
1563 /* For unseekable stream it's enough to play */
1567 if( ASF_NextObject( s, p_obj ) ) /* Go to the next object */
1571 if( p_root->p_hdr != NULL && p_root->p_data != NULL )
1573 p_root->p_fp = ASF_FindObject( p_root->p_hdr,
1574 &asf_object_file_properties_guid, 0 );
1578 asf_object_t *p_hdr_ext =
1579 ASF_FindObject( p_root->p_hdr,
1580 &asf_object_header_extension_guid, 0 );
1586 p_root->p_metadata =
1587 ASF_FindObject( p_hdr_ext,
1588 &asf_object_metadata_guid, 0 );
1589 /* Special case for broken designed file format :( */
1590 i_ext_stream = ASF_CountObject( p_hdr_ext,
1591 &asf_object_extended_stream_properties );
1592 for( i = 0; i < i_ext_stream; i++ )
1594 asf_object_t *p_esp =
1595 ASF_FindObject( p_hdr_ext,
1596 &asf_object_extended_stream_properties, i );
1597 if( p_esp->ext_stream.p_sp )
1599 asf_object_t *p_sp =
1600 (asf_object_t*)p_esp->ext_stream.p_sp;
1602 /* Insert this p_sp */
1603 p_root->p_hdr->p_last->common.p_next = p_sp;
1604 p_root->p_hdr->p_last = p_sp;
1606 p_sp->common.p_father = (asf_object_t*)p_root->p_hdr;
1611 ASF_ObjectDumpDebug( VLC_OBJECT(s),
1612 (asf_object_common_t*)p_root, 0 );
1615 msg_Warn( s, "cannot find file properties object" );
1619 ASF_FreeObjectRoot( s, p_root );
1623 void ASF_FreeObjectRoot( stream_t *s, asf_object_root_t *p_root )
1625 asf_object_t *p_obj;
1627 p_obj = p_root->p_first;
1630 asf_object_t *p_next;
1631 p_next = p_obj->common.p_next;
1632 ASF_FreeObject( s, p_obj );
1638 int __ASF_CountObject( asf_object_t *p_obj, const guid_t *p_guid )
1641 asf_object_t *p_child;
1647 p_child = p_obj->common.p_first;
1650 if( ASF_CmpGUID( &p_child->common.i_object_id, p_guid ) )
1653 p_child = p_child->common.p_next;
1658 void *__ASF_FindObject( asf_object_t *p_obj, const guid_t *p_guid,
1661 asf_object_t *p_child;
1663 p_child = p_obj->common.p_first;
1667 if( ASF_CmpGUID( &p_child->common.i_object_id, p_guid ) )
1674 p_child = p_child->common.p_next;