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 = (asf_object_common_t*)p_obj;
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 = (asf_object_header_t*)p_obj;
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( 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 = (asf_object_data_t*)p_obj;
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 = (asf_object_index_t*)p_obj;
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( (int)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) );
317 for( i = 0, p_peek += 56; i < (int)p_index->i_index_entry_count;
320 p_index->index_entry[i].i_packet_number = GetDWLE( p_peek );
321 p_index->index_entry[i].i_packet_count = GetDWLE( p_peek + 4 );
327 static void ASF_FreeObject_Index( asf_object_t *p_obj )
329 asf_object_index_t *p_index = (asf_object_index_t*)p_obj;
331 FREENULL( p_index->index_entry );
334 static int ASF_ReadObject_file_properties( stream_t *s, asf_object_t *p_obj )
336 asf_object_file_properties_t *p_fp = (asf_object_file_properties_t*)p_obj;
338 const uint8_t *p_peek;
340 if( ( i_peek = stream_Peek( s, &p_peek, 104 ) ) < 104 )
343 ASF_GetGUID( &p_fp->i_file_id, p_peek + 24 );
344 p_fp->i_file_size = GetQWLE( p_peek + 40 );
345 p_fp->i_creation_date = GetQWLE( p_peek + 48 );
346 p_fp->i_data_packets_count = GetQWLE( p_peek + 56 );
347 p_fp->i_play_duration = GetQWLE( p_peek + 64 );
348 p_fp->i_send_duration = GetQWLE( p_peek + 72 );
349 p_fp->i_preroll = GetQWLE( p_peek + 80 );
350 p_fp->i_flags = GetDWLE( p_peek + 88 );
351 p_fp->i_min_data_packet_size = GetDWLE( p_peek + 92 );
352 p_fp->i_max_data_packet_size = GetDWLE( p_peek + 96 );
353 p_fp->i_max_bitrate = GetDWLE( p_peek + 100 );
357 "read \"file properties object\" file_id:" GUID_FMT
358 " file_size:%"PRId64" creation_date:%"PRId64" data_packets_count:"
359 "%"PRId64" play_duration:%"PRId64" send_duration:%"PRId64" preroll:%"PRId64
360 " flags:%d min_data_packet_size:%d "
361 " max_data_packet_size:%d max_bitrate:%d",
362 GUID_PRINT( p_fp->i_file_id ), p_fp->i_file_size,
363 p_fp->i_creation_date, p_fp->i_data_packets_count,
364 p_fp->i_play_duration, p_fp->i_send_duration,
365 p_fp->i_preroll, p_fp->i_flags,
366 p_fp->i_min_data_packet_size, p_fp->i_max_data_packet_size,
367 p_fp->i_max_bitrate );
373 static void ASF_FreeObject_metadata( asf_object_t *p_obj )
375 asf_object_metadata_t *p_meta =
376 (asf_object_metadata_t *)p_obj;
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 =
390 (asf_object_metadata_t *)p_obj;
394 const uint8_t *p_peek, *p_data;
399 if( ( i_peek = stream_Peek( s, &p_peek, p_meta->i_object_size ) ) <
400 __MAX( (int)p_meta->i_object_size, 26 ) )
403 p_meta->i_record_entries_count = GetWLE( p_peek + 24 );
405 p_data = p_peek + 26;
407 p_meta->record = calloc( p_meta->i_record_entries_count,
408 sizeof(asf_metadata_record_t) );
410 for( i = 0; i < p_meta->i_record_entries_count; i++ )
412 asf_metadata_record_t *p_record = &p_meta->record[i];
416 if( !ASF_HAVE( 2+2+2+2+4 ) )
419 if( ASF_READ2() != 0 )
422 p_record->i_stream = ASF_READ2();
423 i_name = ASF_READ2();
424 p_record->i_type = ASF_READ2();
425 i_data = ASF_READ4();
427 if( !ASF_HAVE( i_name + i_data ) )
431 p_record->psz_name = ASF_READS( i_name );
434 if( p_record->i_type == ASF_METADATA_TYPE_STRING )
436 p_record->p_data = ASF_READS( i_data );
437 p_record->i_data = i_data/2; /* FIXME Is that needed ? */
439 else if( p_record->i_type == ASF_METADATA_TYPE_BYTE )
441 p_record->p_data = malloc( i_data );
442 p_record->i_data = i_data;
444 memcpy( p_record->p_data, p_data, i_data );
448 else if( p_record->i_type == ASF_METADATA_TYPE_QWORD )
450 p_record->i_val = ASF_READ8();
452 else if( p_record->i_type == ASF_METADATA_TYPE_DWORD )
454 p_record->i_val = ASF_READ4();
456 else if( p_record->i_type == ASF_METADATA_TYPE_WORD )
458 p_record->i_val = ASF_READ2();
460 else if( p_record->i_type == ASF_METADATA_TYPE_BOOL )
462 p_record->i_val = ASF_READ2();
470 p_meta->i_record_entries_count = i;
474 "read \"metadata object\" %d entries",
475 p_meta->i_record_entries_count );
476 for( j = 0; j < p_meta->i_record_entries_count; j++ )
478 asf_metadata_record_t *p_rec = &p_meta->record[j];
480 if( p_rec->i_type == ASF_METADATA_TYPE_STRING )
481 msg_Dbg( s, " - %s=%s",
482 p_rec->psz_name, p_rec->p_data );
483 else if( p_rec->i_type == ASF_METADATA_TYPE_BYTE )
484 msg_Dbg( s, " - %s (%i bytes)",
485 p_rec->psz_name, p_rec->i_data );
487 msg_Dbg( s, " - %s=%"PRId64,
488 p_rec->psz_name, p_rec->i_val );
495 static int ASF_ReadObject_header_extension( stream_t *s, asf_object_t *p_obj )
497 asf_object_header_extension_t *p_he =
498 (asf_object_header_extension_t *)p_obj;
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( i_peek-46 < (int)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( 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 =
561 (asf_object_header_extension_t *)p_obj;
563 FREENULL( p_he->p_header_extension_data );
566 static int ASF_ReadObject_stream_properties( stream_t *s, asf_object_t *p_obj )
568 asf_object_stream_properties_t *p_sp =
569 (asf_object_stream_properties_t*)p_obj;
571 const uint8_t *p_peek;
573 if( ( i_peek = stream_Peek( s, &p_peek, p_sp->i_object_size ) ) < 78 )
576 ASF_GetGUID( &p_sp->i_stream_type, p_peek + 24 );
577 ASF_GetGUID( &p_sp->i_error_correction_type, p_peek + 40 );
578 p_sp->i_time_offset = GetQWLE( p_peek + 56 );
579 p_sp->i_type_specific_data_length = GetDWLE( p_peek + 64 );
580 p_sp->i_error_correction_data_length = GetDWLE( p_peek + 68 );
581 p_sp->i_flags = GetWLE( p_peek + 72 );
582 p_sp->i_stream_number = p_sp->i_flags&0x07f;
583 p_sp->i_reserved = GetDWLE( p_peek + 74 );
586 if( p_sp->i_type_specific_data_length )
588 if( i_peek < p_sp->i_type_specific_data_length )
591 p_sp->p_type_specific_data =
592 malloc( p_sp->i_type_specific_data_length );
593 if( p_sp->p_type_specific_data == NULL )
596 memcpy( p_sp->p_type_specific_data, p_peek + 78,
597 p_sp->i_type_specific_data_length );
598 i_peek -= p_sp->i_type_specific_data_length;
602 p_sp->p_type_specific_data = NULL;
605 if( p_sp->i_error_correction_data_length )
607 if( i_peek < p_sp->i_error_correction_data_length )
609 free( p_sp->p_type_specific_data );
613 p_sp->p_error_correction_data =
614 malloc( p_sp->i_error_correction_data_length );
615 if( p_sp->p_error_correction_data == NULL )
617 free( p_sp->p_type_specific_data );
620 memcpy( p_sp->p_error_correction_data,
621 p_peek + 78 + p_sp->i_type_specific_data_length,
622 p_sp->i_error_correction_data_length );
626 p_sp->p_error_correction_data = NULL;
631 "read \"stream Properties object\" stream_type:" GUID_FMT
632 " error_correction_type:" GUID_FMT " time_offset:%"PRId64
633 " type_specific_data_length:%d error_correction_data_length:%d"
634 " flags:0x%x stream_number:%d",
635 GUID_PRINT( p_sp->i_stream_type ),
636 GUID_PRINT( p_sp->i_error_correction_type ),
638 p_sp->i_type_specific_data_length,
639 p_sp->i_error_correction_data_length,
641 p_sp->i_stream_number );
647 static void ASF_FreeObject_stream_properties( asf_object_t *p_obj )
649 asf_object_stream_properties_t *p_sp =
650 (asf_object_stream_properties_t*)p_obj;
652 FREENULL( p_sp->p_type_specific_data );
653 FREENULL( p_sp->p_error_correction_data );
657 static int ASF_ReadObject_codec_list( stream_t *s, asf_object_t *p_obj )
659 asf_object_codec_list_t *p_cl = (asf_object_codec_list_t*)p_obj;
661 const uint8_t *p_peek, *p_data;
663 unsigned int i_codec;
665 if( ( i_peek = stream_Peek( s, &p_peek, p_cl->i_object_size ) ) < 44 )
668 ASF_GetGUID( &p_cl->i_reserved, p_peek + 24 );
669 p_cl->i_codec_entries_count = GetWLE( p_peek + 40 );
671 p_data = p_peek + 44;
673 if( p_cl->i_codec_entries_count > 0 )
675 p_cl->codec = calloc( p_cl->i_codec_entries_count,
676 sizeof( asf_codec_entry_t ) );
680 for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
682 asf_codec_entry_t *p_codec = &p_cl->codec[i_codec];
684 if( !ASF_HAVE( 2+2+2 ) )
688 p_codec->i_type = ASF_READ2();
690 /* XXX the length here are the number of *unicode* characters and
691 * not of bytes like nearly every elsewhere */
694 p_codec->psz_name = ASF_READS( 2*ASF_READ2() );
697 p_codec->psz_description = ASF_READS( 2*ASF_READ2() );
699 /* opaque information */
700 p_codec->i_information_length = ASF_READ2();
701 if( p_codec->i_information_length > 0 && ASF_HAVE( p_codec->i_information_length ) )
703 p_codec->p_information = malloc( p_codec->i_information_length );
704 memcpy( p_codec->p_information, p_data, p_codec->i_information_length );
705 p_data += p_codec->i_information_length;
709 p_codec->p_information = NULL;
712 p_cl->i_codec_entries_count = i_codec;
716 msg_Dbg( s, "read \"codec list object\" reserved_guid:" GUID_FMT
717 " codec_entries_count:%d",
718 GUID_PRINT( p_cl->i_reserved ), p_cl->i_codec_entries_count );
720 for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
722 const asf_codec_entry_t *p_codec = &p_cl->codec[i_codec];
724 msg_Dbg( s, " - codec[%d] %s name:\"%s\" "
725 "description:\"%s\" information_length:%d",
726 i_codec, ( p_codec->i_type == ASF_CODEC_TYPE_VIDEO ) ?
727 "video" : ( ( p_codec->i_type == ASF_CODEC_TYPE_AUDIO ) ?
728 "audio" : "unknown" ),
729 p_codec->psz_name, p_codec->psz_description,
730 p_codec->i_information_length );
737 static void ASF_FreeObject_codec_list( asf_object_t *p_obj )
739 asf_object_codec_list_t *p_cl = (asf_object_codec_list_t*)p_obj;
740 unsigned int i_codec;
742 for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
744 asf_codec_entry_t *p_codec = &p_cl->codec[i_codec];
746 FREENULL( p_codec->psz_name );
747 FREENULL( p_codec->psz_description );
748 FREENULL( p_codec->p_information );
750 FREENULL( p_cl->codec );
753 /* Microsoft should go to hell. This time the length give number of bytes
754 * and for the some others object, length give char16 count ... */
755 static int ASF_ReadObject_content_description(stream_t *s, asf_object_t *p_obj)
757 asf_object_content_description_t *p_cd =
758 (asf_object_content_description_t *)p_obj;
759 const uint8_t *p_peek, *p_data;
760 int i_peek, i_title, i_artist, i_copyright, i_description, i_rating;
761 vlc_iconv_t cd = (vlc_iconv_t)-1;
762 const char *ib = NULL;
764 size_t i_ibl, i_obl, i_len;
766 if( ( i_peek = stream_Peek( s, &p_peek, p_cd->i_object_size ) ) < 34 )
769 cd = vlc_iconv_open("UTF-8", "UTF-16LE");
770 if( cd == (vlc_iconv_t)-1 )
772 msg_Err( s, "vlc_iconv_open failed" );
776 /* FIXME i_size*3 is the worst case. */
777 #define GETSTRINGW( psz_str, i_size ) \
778 psz_str = (char *)calloc( i_size*3+1, sizeof( char ) ); \
779 ib = (const char *)p_data; \
783 i_len = vlc_iconv(cd, &ib, &i_ibl, &ob, &i_obl); \
786 p_data = p_peek + 24;
788 i_title = ASF_READ2();
789 i_artist = ASF_READ2();
790 i_copyright = ASF_READ2();
791 i_description = ASF_READ2();
792 i_rating = ASF_READ2();
794 if( !ASF_HAVE( i_title+i_artist+i_copyright+i_description+i_rating ) )
796 vlc_iconv_close( cd );
800 GETSTRINGW( p_cd->psz_title, i_title );
801 GETSTRINGW( p_cd->psz_artist, i_artist );
802 GETSTRINGW( p_cd->psz_copyright, i_copyright );
803 GETSTRINGW( p_cd->psz_description, i_description );
804 GETSTRINGW( p_cd->psz_rating, i_rating );
810 "read \"content description object\" title:\"%s\" artist:\"%s\" copyright:\"%s\" description:\"%s\" rating:\"%s\"",
814 p_cd->psz_description,
822 static void ASF_FreeObject_content_description( asf_object_t *p_obj)
824 asf_object_content_description_t *p_cd =
825 (asf_object_content_description_t *)p_obj;
827 FREENULL( p_cd->psz_title );
828 FREENULL( p_cd->psz_artist );
829 FREENULL( p_cd->psz_copyright );
830 FREENULL( p_cd->psz_description );
831 FREENULL( p_cd->psz_rating );
835 static int ASF_ReadObject_language_list(stream_t *s, asf_object_t *p_obj)
837 asf_object_language_list_t *p_ll =
838 (asf_object_language_list_t*)p_obj;
839 const uint8_t *p_peek, *p_data;
843 if( ( i_peek = stream_Peek( s, &p_peek, p_ll->i_object_size ) ) < 26 )
846 p_data = &p_peek[24];
848 p_ll->i_language = ASF_READ2();
849 if( p_ll->i_language > 0 )
851 p_ll->ppsz_language = calloc( p_ll->i_language, sizeof( char *) );
853 for( i = 0; i < p_ll->i_language; i++ )
857 p_ll->ppsz_language[i] = ASF_READS( ASF_READ1() );
859 p_ll->i_language = i;
863 msg_Dbg( s, "read \"language list object\" %d entries",
865 for( i = 0; i < p_ll->i_language; i++ )
866 msg_Dbg( s, " - '%s'",
867 p_ll->ppsz_language[i] );
872 static void ASF_FreeObject_language_list( asf_object_t *p_obj)
874 asf_object_language_list_t *p_ll =
875 (asf_object_language_list_t *)p_obj;
878 for( i = 0; i < p_ll->i_language; i++ )
879 FREENULL( p_ll->ppsz_language[i] );
880 FREENULL( p_ll->ppsz_language );
883 /* Stream bitrate properties */
884 static int ASF_ReadObject_stream_bitrate_properties( stream_t *s,
887 asf_object_stream_bitrate_properties_t *p_sb =
888 (asf_object_stream_bitrate_properties_t *)p_obj;
889 const uint8_t *p_peek, *p_data;
893 if( ( i_peek = stream_Peek( s, &p_peek, p_sb->i_object_size ) ) < 26 )
896 p_data = &p_peek[24];
898 p_sb->i_bitrate = ASF_READ2();
899 if( p_sb->i_bitrate > 127 )
900 p_sb->i_bitrate = 127; /* Buggy ? */
901 for( i = 0; i < p_sb->i_bitrate; i++ )
903 if( !ASF_HAVE(2 + 4) )
905 p_sb->bitrate[i].i_stream_number = ASF_READ2()& 0x7f;
906 p_sb->bitrate[i].i_avg_bitrate = ASF_READ4();
911 msg_Dbg( s,"read \"stream bitrate properties object\"" );
912 for( i = 0; i < p_sb->i_bitrate; i++ )
914 msg_Dbg( s," - stream=%d bitrate=%d",
915 p_sb->bitrate[i].i_stream_number,
916 p_sb->bitrate[i].i_avg_bitrate );
921 static void ASF_FreeObject_stream_bitrate_properties( asf_object_t *p_obj)
926 static int ASF_ReadObject_extended_stream_properties( stream_t *s,
929 asf_object_extended_stream_properties_t *p_esp =
930 (asf_object_extended_stream_properties_t*)p_obj;
931 const uint8_t *p_peek, *p_data;
934 if( ( i_peek = stream_Peek( s, &p_peek, p_esp->i_object_size ) ) < 88 )
937 p_data = &p_peek[24];
939 p_esp->i_start_time = GetQWLE( &p_data[0] );
940 p_esp->i_end_time = GetQWLE( &p_data[8] );
941 p_esp->i_data_bitrate = GetDWLE( &p_data[16] );
942 p_esp->i_buffer_size = GetDWLE( &p_data[20] );
943 p_esp->i_initial_buffer_fullness = GetDWLE( &p_data[24] );
944 p_esp->i_alternate_data_bitrate = GetDWLE( &p_data[28] );
945 p_esp->i_alternate_buffer_size = GetDWLE( &p_data[32] );
946 p_esp->i_alternate_initial_buffer_fullness = GetDWLE( &p_data[36] );
947 p_esp->i_maximum_object_size = GetDWLE( &p_data[40] );
948 p_esp->i_flags = GetDWLE( &p_data[44] );
949 p_esp->i_stream_number = GetWLE( &p_data[48] );
950 p_esp->i_language_index = GetWLE( &p_data[50] );
951 p_esp->i_average_time_per_frame= GetQWLE( &p_data[52] );
952 p_esp->i_stream_name_count = GetWLE( &p_data[60] );
953 p_esp->i_payload_extension_system_count = GetWLE( &p_data[62] );
957 p_esp->pi_stream_name_language = calloc( p_esp->i_stream_name_count,
959 p_esp->ppsz_stream_name = calloc( p_esp->i_stream_name_count,
961 for( i = 0; i < p_esp->i_stream_name_count; i++ )
963 if( !ASF_HAVE( 2+2 ) )
965 p_esp->pi_stream_name_language[i] = ASF_READ2();
966 p_esp->ppsz_stream_name[i] = ASF_READS( ASF_READ2() );
968 p_esp->i_stream_name_count = i;
970 for( i = 0; i < p_esp->i_payload_extension_system_count; i++ )
972 ASF_SKIP( 16 ); // GUID
974 ASF_SKIP( ASF_READ4() );
978 if( p_data < &p_peek[i_peek] )
981 /* Cannot fail as peek succeed */
982 stream_Read( s, NULL, p_data - p_peek );
984 p_sp = malloc( sizeof( asf_object_t ) );
986 if( ASF_ReadObject( s, p_sp, NULL ) )
992 /* This p_sp will be inserted by ReadRoot later */
993 p_esp->p_sp = (asf_object_stream_properties_t*)p_sp;
998 msg_Dbg( s, "read \"extended stream properties object\":" );
999 msg_Dbg( s, " - start=%"PRId64" end=%"PRId64,
1000 p_esp->i_start_time, p_esp->i_end_time );
1001 msg_Dbg( s, " - data bitrate=%d buffer=%d initial fullness=%d",
1002 p_esp->i_data_bitrate,
1003 p_esp->i_buffer_size,
1004 p_esp->i_initial_buffer_fullness );
1005 msg_Dbg( s, " - alternate data bitrate=%d buffer=%d initial fullness=%d",
1006 p_esp->i_alternate_data_bitrate,
1007 p_esp->i_alternate_buffer_size,
1008 p_esp->i_alternate_initial_buffer_fullness );
1009 msg_Dbg( s, " - maximum object size=%d", p_esp->i_maximum_object_size );
1010 msg_Dbg( s, " - flags=0x%x", p_esp->i_flags );
1011 msg_Dbg( s, " - stream number=%d language=%d",
1012 p_esp->i_stream_number, p_esp->i_language_index );
1013 msg_Dbg( s, " - average time per frame=%"PRId64,
1014 p_esp->i_average_time_per_frame );
1015 msg_Dbg( s, " - stream name count=%d", p_esp->i_stream_name_count );
1016 for( i = 0; i < p_esp->i_stream_name_count; i++ )
1017 msg_Dbg( s, " - lang id=%d name=%s",
1018 p_esp->pi_stream_name_language[i],
1019 p_esp->ppsz_stream_name[i] );
1020 msg_Dbg( s, " - payload extension system count=%d",
1021 p_esp->i_payload_extension_system_count );
1025 static void ASF_FreeObject_extended_stream_properties( asf_object_t *p_obj)
1027 asf_object_extended_stream_properties_t *p_esp =
1028 (asf_object_extended_stream_properties_t *)p_obj;
1031 for( i = 0; i < p_esp->i_stream_name_count; i++ )
1032 FREENULL( p_esp->ppsz_stream_name[i] );
1033 FREENULL( p_esp->pi_stream_name_language );
1034 FREENULL( p_esp->ppsz_stream_name );
1038 static int ASF_ReadObject_advanced_mutual_exclusion( stream_t *s,
1039 asf_object_t *p_obj)
1041 asf_object_advanced_mutual_exclusion_t *p_ae =
1042 (asf_object_advanced_mutual_exclusion_t *)p_obj;
1043 const uint8_t *p_peek, *p_data;
1047 if( ( i_peek = stream_Peek( s, &p_peek, p_ae->i_object_size ) ) < 42 )
1048 return VLC_EGENERIC;
1050 p_data = &p_peek[24];
1052 ASF_GetGUID( &p_ae->type, &p_data[0] );
1054 p_ae->i_stream_number_count = ASF_READ2();
1055 p_ae->pi_stream_number = calloc( p_ae->i_stream_number_count, sizeof(int) );
1057 for( i = 0; i < p_ae->i_stream_number_count; i++ )
1061 p_ae->pi_stream_number[i] = ASF_READ2();
1063 p_ae->i_stream_number_count = i;
1066 msg_Dbg( s, "read \"advanced mutual exclusion object\"" );
1067 for( i = 0; i < p_ae->i_stream_number_count; i++ )
1068 msg_Dbg( s, " - stream=%d", p_ae->pi_stream_number[i] );
1072 static void ASF_FreeObject_advanced_mutual_exclusion( asf_object_t *p_obj)
1074 asf_object_advanced_mutual_exclusion_t *p_ae =
1075 (asf_object_advanced_mutual_exclusion_t *)p_obj;
1077 FREENULL( p_ae->pi_stream_number );
1081 static int ASF_ReadObject_stream_prioritization( stream_t *s,
1082 asf_object_t *p_obj)
1084 asf_object_stream_prioritization_t *p_sp =
1085 (asf_object_stream_prioritization_t *)p_obj;
1086 const uint8_t *p_peek, *p_data;
1090 if( ( i_peek = stream_Peek( s, &p_peek, p_sp->i_object_size ) ) < 26 )
1091 return VLC_EGENERIC;
1093 p_data = &p_peek[24];
1095 p_sp->i_priority_count = ASF_READ2();
1097 p_sp->pi_priority_flag = calloc( p_sp->i_priority_count, sizeof(int) );
1098 p_sp->pi_priority_stream_number =
1099 calloc( p_sp->i_priority_count, sizeof(int) );
1101 for( i = 0; i < p_sp->i_priority_count; i++ )
1103 if( !ASF_HAVE(2+2) )
1105 p_sp->pi_priority_stream_number[i] = ASF_READ2();
1106 p_sp->pi_priority_flag[i] = ASF_READ2();
1108 p_sp->i_priority_count = i;
1111 msg_Dbg( s, "read \"stream prioritization object\"" );
1112 for( i = 0; i < p_sp->i_priority_count; i++ )
1113 msg_Dbg( s, " - Stream:%d flags=0x%x",
1114 p_sp->pi_priority_stream_number[i],
1115 p_sp->pi_priority_flag[i] );
1119 static void ASF_FreeObject_stream_prioritization( asf_object_t *p_obj)
1121 asf_object_stream_prioritization_t *p_sp =
1122 (asf_object_stream_prioritization_t *)p_obj;
1124 FREENULL( p_sp->pi_priority_stream_number );
1125 FREENULL( p_sp->pi_priority_flag );
1129 static int ASF_ReadObject_extended_content_description( stream_t *s,
1130 asf_object_t *p_obj)
1132 asf_object_extended_content_description_t *p_ec =
1133 (asf_object_extended_content_description_t *)p_obj;
1134 const uint8_t *p_peek, *p_data;
1138 if( ( i_peek = stream_Peek( s, &p_peek, p_ec->i_object_size ) ) < 26 )
1139 return VLC_EGENERIC;
1141 p_data = &p_peek[24];
1143 p_ec->i_count = ASF_READ2();
1144 p_ec->ppsz_name = calloc( p_ec->i_count, sizeof(char*) );
1145 p_ec->ppsz_value = calloc( p_ec->i_count, sizeof(char*) );
1146 if( !p_ec->ppsz_name || !p_ec->ppsz_value )
1148 free( p_ec->ppsz_name );
1149 free( p_ec->ppsz_value );
1152 for( i = 0; i < p_ec->i_count; i++ )
1157 if( !ASF_HAVE(2 + 2+2) )
1160 p_ec->ppsz_name[i] = ASF_READS( ASF_READ2() );
1163 i_type = ASF_READ2();
1164 i_size = ASF_READ2();
1168 p_ec->ppsz_value[i] = ASF_READS( i_size );
1170 else if( i_type == 1 )
1173 static const char hex[16] = "0123456789ABCDEF";
1176 p_ec->ppsz_value[i] = malloc( 2*i_size + 1 );
1177 for( j = 0; j < i_size; j++ )
1179 const uint8_t v = ASF_READ1();
1180 p_ec->ppsz_value[i][2*j+0] = hex[v>>4];
1181 p_ec->ppsz_value[i][2*j+1] = hex[v&0xf];
1183 p_ec->ppsz_value[i][2*i_size] = '\0';
1185 else if( i_type == 2 )
1188 p_ec->ppsz_value[i] = strdup( ASF_READ1() ? "true" : "false" );
1191 else if( i_type == 3 )
1194 asprintf( &p_ec->ppsz_value[i], "%d", ASF_READ4() );
1196 else if( i_type == 4 )
1199 asprintf( &p_ec->ppsz_value[i], "%"PRId64, ASF_READ8() );
1201 else if( i_type == 5 )
1204 asprintf( &p_ec->ppsz_value[i], "%d", ASF_READ2() );
1208 p_ec->ppsz_value[i] = NULL;
1215 msg_Dbg( s, "read \"extended content description object\"" );
1216 for( i = 0; i < p_ec->i_count; i++ )
1217 msg_Dbg( s, " - '%s' = '%s'",
1219 p_ec->ppsz_value[i] );
1223 static void ASF_FreeObject_extended_content_description( asf_object_t *p_obj)
1225 asf_object_extended_content_description_t *p_ec =
1226 (asf_object_extended_content_description_t *)p_obj;
1229 for( i = 0; i < p_ec->i_count; i++ )
1231 FREENULL( p_ec->ppsz_name[i] );
1232 FREENULL( p_ec->ppsz_value[i] );
1234 FREENULL( p_ec->ppsz_name );
1235 FREENULL( p_ec->ppsz_value );
1240 static int ASF_ReadObject_XXX(stream_t *s, asf_object_t *p_obj)
1242 asf_object_XXX_t *p_XX =
1243 (asf_object_XXX_t *)p_obj;
1244 const uint8_t *p_peek;
1248 if( ( i_peek = stream_Peek( s, &p_peek, p_XX->i_object_size ) ) < XXX )
1249 return VLC_EGENERIC;
1251 p_data = &p_peek[24];
1255 "Read \"XXX object\"" );
1259 static void ASF_FreeObject_XXX( asf_object_t *p_obj)
1261 asf_object_XXX_t *p_XX =
1262 (asf_object_XXX_t *)p_obj;
1272 int (*ASF_ReadObject_function)( stream_t *, asf_object_t *p_obj );
1273 void (*ASF_FreeObject_function)( asf_object_t *p_obj );
1275 } ASF_Object_Function [] =
1277 { &asf_object_header_guid, ASF_OBJECT_HEADER,
1278 ASF_ReadObject_Header, ASF_FreeObject_Null },
1279 { &asf_object_data_guid, ASF_OBJECT_DATA,
1280 ASF_ReadObject_Data, ASF_FreeObject_Null },
1281 { &asf_object_index_guid, ASF_OBJECT_INDEX,
1282 ASF_ReadObject_Index, ASF_FreeObject_Index },
1283 { &asf_object_file_properties_guid, ASF_OBJECT_FILE_PROPERTIES,
1284 ASF_ReadObject_file_properties, ASF_FreeObject_Null },
1285 { &asf_object_stream_properties_guid, ASF_OBJECT_STREAM_PROPERTIES,
1286 ASF_ReadObject_stream_properties,ASF_FreeObject_stream_properties },
1287 { &asf_object_header_extension_guid, ASF_OBJECT_HEADER_EXTENSION,
1288 ASF_ReadObject_header_extension, ASF_FreeObject_header_extension},
1289 { &asf_object_metadata_guid, ASF_OBJECT_METADATA,
1290 ASF_ReadObject_metadata, ASF_FreeObject_metadata},
1291 { &asf_object_codec_list_guid, ASF_OBJECT_CODEC_LIST,
1292 ASF_ReadObject_codec_list, ASF_FreeObject_codec_list },
1293 { &asf_object_marker_guid, ASF_OBJECT_MARKER, NULL, NULL },
1294 { &asf_object_padding, ASF_OBJECT_PADDING, NULL, NULL },
1295 { &asf_object_content_description_guid, ASF_OBJECT_CONTENT_DESCRIPTION,
1296 ASF_ReadObject_content_description, ASF_FreeObject_content_description },
1297 { &asf_object_language_list, ASF_OBJECT_OTHER,
1298 ASF_ReadObject_language_list, ASF_FreeObject_language_list },
1299 { &asf_object_stream_bitrate_properties, ASF_OBJECT_OTHER,
1300 ASF_ReadObject_stream_bitrate_properties,
1301 ASF_FreeObject_stream_bitrate_properties },
1302 { &asf_object_extended_stream_properties, ASF_OBJECT_OTHER,
1303 ASF_ReadObject_extended_stream_properties,
1304 ASF_FreeObject_extended_stream_properties },
1305 { &asf_object_advanced_mutual_exclusion, ASF_OBJECT_OTHER,
1306 ASF_ReadObject_advanced_mutual_exclusion,
1307 ASF_FreeObject_advanced_mutual_exclusion },
1308 { &asf_object_stream_prioritization, ASF_OBJECT_OTHER,
1309 ASF_ReadObject_stream_prioritization,
1310 ASF_FreeObject_stream_prioritization },
1311 { &asf_object_extended_content_description, ASF_OBJECT_OTHER,
1312 ASF_ReadObject_extended_content_description,
1313 ASF_FreeObject_extended_content_description },
1315 { &asf_object_null_guid, 0, NULL, NULL }
1318 static int ASF_ReadObject( stream_t *s, asf_object_t *p_obj,
1319 asf_object_t *p_father )
1327 memset( p_obj, 0, sizeof( *p_obj ) );
1329 if( ASF_ReadObjectCommon( s, p_obj ) )
1331 msg_Warn( s, "cannot read one asf object" );
1332 return VLC_EGENERIC;
1334 p_obj->common.p_father = p_father;
1335 p_obj->common.p_first = NULL;
1336 p_obj->common.p_next = NULL;
1337 p_obj->common.p_last = NULL;
1339 if( p_obj->common.i_object_size < 24 )
1341 msg_Warn( s, "found a corrupted asf object (size<24)" );
1342 return VLC_EGENERIC;
1345 /* find this object */
1346 for( i_index = 0; ; i_index++ )
1348 if( ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1349 &p_obj->common.i_object_id ) ||
1350 ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1351 &asf_object_null_guid ) )
1356 p_obj->common.i_type = ASF_Object_Function[i_index].i_type;
1358 /* Now load this object */
1359 if( ASF_Object_Function[i_index].ASF_ReadObject_function == NULL )
1361 msg_Warn( s, "unknown asf object (not loaded)" );
1362 i_result = VLC_SUCCESS;
1366 /* XXX ASF_ReadObject_function realloc *pp_obj XXX */
1368 (ASF_Object_Function[i_index].ASF_ReadObject_function)( s, p_obj );
1371 /* link this object with father */
1372 if( p_father && ! i_result )
1374 if( p_father->common.p_first )
1376 p_father->common.p_last->common.p_next = p_obj;
1380 p_father->common.p_first = p_obj;
1382 p_father->common.p_last = p_obj;
1388 static void ASF_FreeObject( stream_t *s, asf_object_t *p_obj )
1391 asf_object_t *p_child;
1396 /* Free all child object */
1397 p_child = p_obj->common.p_first;
1400 asf_object_t *p_next;
1401 p_next = p_child->common.p_next;
1402 ASF_FreeObject( s, p_child );
1406 /* find this object */
1407 for( i_index = 0; ; i_index++ )
1409 if( ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1410 &p_obj->common.i_object_id )||
1411 ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1412 &asf_object_null_guid ) )
1418 /* Now free this object */
1419 if( ASF_Object_Function[i_index].ASF_FreeObject_function == NULL )
1422 "unknown asf object " GUID_FMT,
1423 GUID_PRINT( p_obj->common.i_object_id ) );
1429 "free asf object " GUID_FMT,
1430 GUID_PRINT( p_obj->common.i_object_id ) );
1432 (ASF_Object_Function[i_index].ASF_FreeObject_function)( p_obj );
1437 /*****************************************************************************
1438 * ASF_ObjectDumpDebug:
1439 *****************************************************************************/
1443 const char *psz_name;
1444 } ASF_ObjectDumpDebugInfo[] =
1446 { &asf_object_header_guid, "Header" },
1447 { &asf_object_data_guid, "Data" },
1448 { &asf_object_index_guid, "Index" },
1449 { &asf_object_file_properties_guid, "File Properties" },
1450 { &asf_object_stream_properties_guid, "Stream Properties" },
1451 { &asf_object_content_description_guid, "Content Description" },
1452 { &asf_object_header_extension_guid, "Header Extension" },
1453 { &asf_object_metadata_guid, "Metadata" },
1454 { &asf_object_codec_list_guid, "Codec List" },
1455 { &asf_object_marker_guid, "Marker" },
1456 { &asf_object_stream_type_audio, "Stream Type Audio" },
1457 { &asf_object_stream_type_video, "Stream Type Video" },
1458 { &asf_object_stream_type_command, "Stream Type Command" },
1459 { &asf_object_language_list, "Language List" },
1460 { &asf_object_stream_bitrate_properties, "Stream Bitrate Properties" },
1461 { &asf_object_padding, "Padding" },
1462 { &asf_object_extended_stream_properties, "Extended Stream Properties" },
1463 { &asf_object_advanced_mutual_exclusion, "Advanced Mutual Exclusion" },
1464 { &asf_object_stream_prioritization, "Stream Prioritization" },
1465 { &asf_object_extended_content_description, "Extended content description"},
1467 { NULL, "Unknown" },
1471 static void ASF_ObjectDumpDebug( vlc_object_t *p_obj,
1472 asf_object_common_t *p_node, int i_level )
1476 union asf_object_u *p_child;
1477 const char *psz_name;
1480 for( i = 0; ASF_ObjectDumpDebugInfo[i].p_id != NULL; i++ )
1482 if( ASF_CmpGUID( ASF_ObjectDumpDebugInfo[i].p_id,
1483 &p_node->i_object_id ) )
1486 psz_name = ASF_ObjectDumpDebugInfo[i].psz_name;
1488 memset( str, ' ', sizeof( str ) );
1489 for( i = 1; i < i_level; i++ )
1493 snprintf( str + 5*i_level, 1024,
1494 "+ '%s' GUID "GUID_FMT" size:%"PRIu64"pos:%"PRIu64,
1496 GUID_PRINT( p_node->i_object_id ),
1497 p_node->i_object_size, p_node->i_object_pos );
1499 msg_Dbg( p_obj, "%s", str );
1501 for( p_child = p_node->p_first; p_child != NULL;
1502 p_child = p_child->common.p_next )
1504 ASF_ObjectDumpDebug( p_obj, &p_child->common, i_level + 1 );
1508 /*****************************************************************************
1509 * ASF_ReadObjetRoot : parse the entire stream/file
1510 *****************************************************************************/
1511 asf_object_root_t *ASF_ReadObjectRoot( stream_t *s, int b_seekable )
1513 asf_object_root_t *p_root = malloc( sizeof( asf_object_root_t ) );
1514 asf_object_t *p_obj;
1516 p_root->i_type = ASF_OBJECT_ROOT;
1517 memcpy( &p_root->i_object_id, &asf_object_null_guid, sizeof( guid_t ) );
1518 p_root->i_object_pos = stream_Tell( s );
1519 p_root->i_object_size = 0;
1520 p_root->p_first = NULL;
1521 p_root->p_last = NULL;
1522 p_root->p_next = NULL;
1523 p_root->p_hdr = NULL;
1524 p_root->p_data = NULL;
1525 p_root->p_fp = NULL;
1526 p_root->p_index = NULL;
1527 p_root->p_metadata = NULL;
1531 p_obj = malloc( sizeof( asf_object_t ) );
1533 if( ASF_ReadObject( s, p_obj, (asf_object_t*)p_root ) )
1538 switch( p_obj->common.i_type )
1540 case( ASF_OBJECT_HEADER ):
1541 p_root->p_hdr = (asf_object_header_t*)p_obj;
1543 case( ASF_OBJECT_DATA ):
1544 p_root->p_data = (asf_object_data_t*)p_obj;
1546 case( ASF_OBJECT_INDEX ):
1547 p_root->p_index = (asf_object_index_t*)p_obj;
1550 msg_Warn( s, "unknow object found" );
1553 if( p_obj->common.i_type == ASF_OBJECT_DATA &&
1554 p_obj->common.i_object_size <= 50 )
1556 /* probably a dump of broadcasted asf */
1559 if( !b_seekable && p_root->p_hdr && p_root->p_data )
1561 /* For unseekable stream it's enough to play */
1565 if( ASF_NextObject( s, p_obj ) ) /* Go to the next object */
1569 if( p_root->p_hdr != NULL && p_root->p_data != NULL )
1571 p_root->p_fp = ASF_FindObject( p_root->p_hdr,
1572 &asf_object_file_properties_guid, 0 );
1576 asf_object_t *p_hdr_ext =
1577 ASF_FindObject( p_root->p_hdr,
1578 &asf_object_header_extension_guid, 0 );
1584 p_root->p_metadata =
1585 ASF_FindObject( p_hdr_ext,
1586 &asf_object_metadata_guid, 0 );
1587 /* Special case for broken designed file format :( */
1588 i_ext_stream = ASF_CountObject( p_hdr_ext,
1589 &asf_object_extended_stream_properties );
1590 for( i = 0; i < i_ext_stream; i++ )
1592 asf_object_t *p_esp =
1593 ASF_FindObject( p_hdr_ext,
1594 &asf_object_extended_stream_properties, i );
1595 if( p_esp->ext_stream.p_sp )
1597 asf_object_t *p_sp =
1598 (asf_object_t*)p_esp->ext_stream.p_sp;
1600 /* Insert this p_sp */
1601 p_root->p_hdr->p_last->common.p_next = p_sp;
1602 p_root->p_hdr->p_last = p_sp;
1604 p_sp->common.p_father = (asf_object_t*)p_root->p_hdr;
1609 ASF_ObjectDumpDebug( VLC_OBJECT(s),
1610 (asf_object_common_t*)p_root, 0 );
1613 msg_Warn( s, "cannot find file properties object" );
1617 ASF_FreeObjectRoot( s, p_root );
1621 void ASF_FreeObjectRoot( stream_t *s, asf_object_root_t *p_root )
1623 asf_object_t *p_obj;
1625 p_obj = p_root->p_first;
1628 asf_object_t *p_next;
1629 p_next = p_obj->common.p_next;
1630 ASF_FreeObject( s, p_obj );
1636 int __ASF_CountObject( asf_object_t *p_obj, const guid_t *p_guid )
1639 asf_object_t *p_child;
1645 p_child = p_obj->common.p_first;
1648 if( ASF_CmpGUID( &p_child->common.i_object_id, p_guid ) )
1651 p_child = p_child->common.p_next;
1656 void *__ASF_FindObject( asf_object_t *p_obj, const guid_t *p_guid,
1659 asf_object_t *p_child;
1661 p_child = p_obj->common.p_first;
1665 if( ASF_CmpGUID( &p_child->common.i_object_id, p_guid ) )
1672 p_child = p_child->common.p_next;