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>
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]
47 * They ensure that invalid reads will not create problems.
48 * They are expansion safe
49 * They make the following assumptions:
50 * const uint8_t *p_peek exists and points to the start of a buffer
51 * int i_peek gives the size of the buffer pointed by p_peek
52 * const uint8_t *p_data exits and points to the data inside p_peek to be read.
55 * Check that n bytes can be read */
56 static inline bool AsfObjectHelperHave( const uint8_t *p_peek, int i_peek, const uint8_t *p_current, int i_wanted )
58 if( i_wanted < 0 || i_wanted > i_peek )
60 return &p_current[i_wanted] <= &p_peek[i_peek];
62 #define ASF_HAVE(n) AsfObjectHelperHave( p_peek, i_peek, p_data, n )
65 * Skip n bytes if possible */
66 static inline void AsfObjectHelperSkip( const uint8_t *p_peek, int i_peek, uint8_t **pp_data, int i_wanted )
68 if( AsfObjectHelperHave( p_peek, i_peek, *pp_data, i_wanted ) )
71 *pp_data = (uint8_t*)&p_peek[i_peek];
73 #define ASF_SKIP(n) AsfObjectHelperSkip( p_peek, i_peek, (uint8_t**)&p_data, n )
76 * Read X byte if possible, else return 0 */
77 #define ASF_FUNCTION_READ_X(type, x, cmd ) \
78 static inline type AsfObjectHelperRead##x( const uint8_t *p_peek, int i_peek, uint8_t **pp_data ) { \
79 uint8_t *p_data = *pp_data; \
86 ASF_FUNCTION_READ_X( uint8_t, 1, *p_data )
87 ASF_FUNCTION_READ_X( uint16_t, 2, GetWLE(p_data) )
88 ASF_FUNCTION_READ_X( uint32_t, 4, GetDWLE(p_data) )
89 ASF_FUNCTION_READ_X( uint64_t, 8, GetQWLE(p_data) )
90 #define ASF_READ1() AsfObjectHelperRead1( p_peek, i_peek, (uint8_t**)&p_data )
91 #define ASF_READ2() AsfObjectHelperRead2( p_peek, i_peek, (uint8_t**)&p_data )
92 #define ASF_READ4() AsfObjectHelperRead4( p_peek, i_peek, (uint8_t**)&p_data )
93 #define ASF_READ8() AsfObjectHelperRead8( p_peek, i_peek, (uint8_t**)&p_data )
96 * Read a string of n/2 wchar long ie n bytes. Do a stupid conversion (suppose latin1)
97 * Return allocated "" if not possible */
98 static char *AsfObjectHelperReadString( const uint8_t *p_peek, int i_peek, uint8_t **pp_data, int i_size )
100 uint8_t *p_data = *pp_data;
102 if( ASF_HAVE(i_size) )
104 psz_string = calloc( i_size/2 + 1, sizeof( char ) );
108 for( i = 0; i < i_size/2; i++ )
109 psz_string[i] = GetWLE( &p_data[2*i] );
110 psz_string[i_size/2] = '\0';
115 psz_string = strdup("");
121 #define ASF_READS(n) AsfObjectHelperReadString( p_peek, i_peek, (uint8_t**)&p_data, n )
123 /****************************************************************************
125 ****************************************************************************/
126 static int ASF_ReadObject( stream_t *, asf_object_t *, asf_object_t * );
128 /****************************************************************************
130 ****************************************************************************/
131 void ASF_GetGUID( guid_t *p_guid, const uint8_t *p_data )
133 p_guid->v1 = GetDWLE( p_data );
134 p_guid->v2 = GetWLE( p_data + 4);
135 p_guid->v3 = GetWLE( p_data + 6);
136 memcpy( p_guid->v4, p_data + 8, 8 );
139 bool ASF_CmpGUID( const guid_t *p_guid1, const guid_t *p_guid2 )
141 if( (p_guid1->v1 != p_guid2->v1 )||
142 (p_guid1->v2 != p_guid2->v2 )||
143 (p_guid1->v3 != p_guid2->v3 )||
144 ( memcmp( p_guid1->v4, p_guid2->v4,8 )) )
151 /****************************************************************************
153 ****************************************************************************/
154 static int ASF_ReadObjectCommon( stream_t *s, asf_object_t *p_obj )
156 asf_object_common_t *p_common = &p_obj->common;
157 const uint8_t *p_peek;
159 if( stream_Peek( s, &p_peek, 24 ) < 24 )
162 ASF_GetGUID( &p_common->i_object_id, p_peek );
163 p_common->i_object_size = GetQWLE( p_peek + 16 );
164 p_common->i_object_pos = stream_Tell( s );
165 p_common->p_next = NULL;
169 "found object guid: " GUID_FMT " size:%"PRId64,
170 GUID_PRINT( p_common->i_object_id ),
171 p_common->i_object_size );
177 static int ASF_NextObject( stream_t *s, asf_object_t *p_obj )
182 if( ASF_ReadObjectCommon( s, &obj ) )
188 if( p_obj->common.i_object_size <= 0 )
191 if( p_obj->common.p_father &&
192 p_obj->common.p_father->common.i_object_size != 0 )
194 if( p_obj->common.p_father->common.i_object_pos +
195 p_obj->common.p_father->common.i_object_size <
196 p_obj->common.i_object_pos + p_obj->common.i_object_size + 24 )
197 /* 24 is min size of an object */
204 return stream_Seek( s, p_obj->common.i_object_pos +
205 p_obj->common.i_object_size );
208 static void ASF_FreeObject_Null( asf_object_t *pp_obj )
213 static int ASF_ReadObject_Header( stream_t *s, asf_object_t *p_obj )
215 asf_object_header_t *p_hdr = &p_obj->header;
216 asf_object_t *p_subobj;
218 const uint8_t *p_peek;
220 if( ( i_peek = stream_Peek( s, &p_peek, 30 ) ) < 30 )
223 p_hdr->i_sub_object_count = GetDWLE( p_peek + 24 );
224 p_hdr->i_reserved1 = p_peek[28];
225 p_hdr->i_reserved2 = p_peek[29];
226 p_hdr->p_first = NULL;
227 p_hdr->p_last = NULL;
231 "read \"header object\" subobj:%d, reserved1:%d, reserved2:%d",
232 p_hdr->i_sub_object_count,
234 p_hdr->i_reserved2 );
237 /* Cannot fail as peek succeed */
238 stream_Read( s, NULL, 30 );
240 /* Now load sub object */
243 p_subobj = malloc( sizeof( asf_object_t ) );
245 if( !p_subobj || ASF_ReadObject( s, p_subobj, (asf_object_t*)p_hdr ) )
250 if( ASF_NextObject( s, p_subobj ) ) /* Go to the next object */
256 static int ASF_ReadObject_Data( stream_t *s, asf_object_t *p_obj )
258 asf_object_data_t *p_data = &p_obj->data;
260 const uint8_t *p_peek;
262 if( ( i_peek = stream_Peek( s, &p_peek, 50 ) ) < 50 )
265 ASF_GetGUID( &p_data->i_file_id, p_peek + 24 );
266 p_data->i_total_data_packets = GetQWLE( p_peek + 40 );
267 p_data->i_reserved = GetWLE( p_peek + 48 );
271 "read \"data object\" file_id:" GUID_FMT " total data packet:"
272 "%"PRId64" reserved:%d",
273 GUID_PRINT( p_data->i_file_id ),
274 p_data->i_total_data_packets,
275 p_data->i_reserved );
281 static int ASF_ReadObject_Index( stream_t *s, asf_object_t *p_obj )
283 asf_object_index_t *p_index = &p_obj->index;
284 const uint8_t *p_peek;
287 /* We just ignore error on the index */
288 if( stream_Peek( s, &p_peek, p_index->i_object_size ) <
289 __MAX( (int64_t)p_index->i_object_size, 56 ) )
292 ASF_GetGUID( &p_index->i_file_id, p_peek + 24 );
293 p_index->i_index_entry_time_interval = GetQWLE( p_peek + 40 );
294 p_index->i_max_packet_count = GetDWLE( p_peek + 48 );
295 p_index->i_index_entry_count = GetDWLE( p_peek + 52 );
296 p_index->index_entry = NULL;
300 "read \"index object\" file_id:" GUID_FMT
301 " index_entry_time_interval:%"PRId64" max_packet_count:%d "
302 "index_entry_count:%ld",
303 GUID_PRINT( p_index->i_file_id ),
304 p_index->i_index_entry_time_interval,
305 p_index->i_max_packet_count,
306 (long int)p_index->i_index_entry_count );
309 /* Sanity checking */
310 if( p_index->i_index_entry_count > (p_index->i_object_size - 56) / 6 )
311 p_index->i_index_entry_count = (p_index->i_object_size - 56) / 6;
313 p_index->index_entry = calloc( p_index->i_index_entry_count,
314 sizeof(asf_index_entry_t) );
315 if( !p_index->index_entry )
318 for( i = 0, p_peek += 56; i < p_index->i_index_entry_count; i++, p_peek += 6 )
320 p_index->index_entry[i].i_packet_number = GetDWLE( p_peek );
321 p_index->index_entry[i].i_packet_count = GetWLE( p_peek + 4 );
327 static void ASF_FreeObject_Index( asf_object_t *p_obj )
329 asf_object_index_t *p_index = &p_obj->index;
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 = &p_obj->file_properties;
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 = &p_obj->metadata;
378 for( i = 0; i < p_meta->i_record_entries_count; i++ )
380 free( p_meta->record[i].psz_name );
381 free( p_meta->record[i].p_data );
383 free( p_meta->record );
386 static int ASF_ReadObject_metadata( stream_t *s, asf_object_t *p_obj )
388 asf_object_metadata_t *p_meta = &p_obj->metadata;
392 const uint8_t *p_peek, *p_data;
397 if( ( i_peek = stream_Peek( s, &p_peek, p_meta->i_object_size ) ) <
398 __MAX( (int64_t)p_meta->i_object_size, 26 ) )
401 p_meta->i_record_entries_count = GetWLE( p_peek + 24 );
403 p_data = p_peek + 26;
405 p_meta->record = calloc( p_meta->i_record_entries_count,
406 sizeof(asf_metadata_record_t) );
407 if( !p_meta->record )
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 = (uint8_t *)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;
443 if( p_record->p_data && i_data > 0 )
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 = &p_obj->header_extension;
499 const uint8_t *p_peek;
501 if( ( i_peek = stream_Peek( s, &p_peek, p_he->i_object_size ) ) < 46)
505 ASF_GetGUID( &p_he->i_reserved1, p_peek + 24 );
506 p_he->i_reserved2 = GetWLE( p_peek + 40 );
507 p_he->i_header_extension_size = GetDWLE( p_peek + 42 );
508 if( p_he->i_header_extension_size )
510 if( (unsigned int)(i_peek-46) < p_he->i_header_extension_size )
513 p_he->p_header_extension_data =
514 malloc( p_he->i_header_extension_size );
515 if( !p_he->p_header_extension_data )
518 memcpy( p_he->p_header_extension_data, p_peek + 46,
519 p_he->i_header_extension_size );
523 p_he->p_header_extension_data = NULL;
528 "read \"header extension object\" reserved1:" GUID_FMT
529 " reserved2:%d header_extension_size:%d",
530 GUID_PRINT( p_he->i_reserved1 ), p_he->i_reserved2,
531 p_he->i_header_extension_size );
534 if( !p_he->i_header_extension_size ) return VLC_SUCCESS;
536 /* Read the extension objects */
537 stream_Read( s, NULL, 46 );
540 asf_object_t *p_obj = malloc( sizeof( asf_object_t ) );
542 if( !p_obj || ASF_ReadObject( s, p_obj, (asf_object_t*)p_he ) )
548 if( ASF_NextObject( s, p_obj ) ) /* Go to the next object */
557 static void ASF_FreeObject_header_extension( asf_object_t *p_obj )
559 asf_object_header_extension_t *p_he = &p_obj->header_extension;
561 FREENULL( p_he->p_header_extension_data );
564 static int ASF_ReadObject_stream_properties( stream_t *s, asf_object_t *p_obj )
566 asf_object_stream_properties_t *p_sp = &p_obj->stream_properties;
568 const uint8_t *p_peek;
570 if( ( i_peek = stream_Peek( s, &p_peek, p_sp->i_object_size ) ) < 78 )
573 ASF_GetGUID( &p_sp->i_stream_type, p_peek + 24 );
574 ASF_GetGUID( &p_sp->i_error_correction_type, p_peek + 40 );
575 p_sp->i_time_offset = GetQWLE( p_peek + 56 );
576 p_sp->i_type_specific_data_length = GetDWLE( p_peek + 64 );
577 p_sp->i_error_correction_data_length = GetDWLE( p_peek + 68 );
578 p_sp->i_flags = GetWLE( p_peek + 72 );
579 p_sp->i_stream_number = p_sp->i_flags&0x07f;
580 p_sp->i_reserved = GetDWLE( p_peek + 74 );
583 if( p_sp->i_type_specific_data_length )
585 if( i_peek < p_sp->i_type_specific_data_length )
588 p_sp->p_type_specific_data =
589 malloc( p_sp->i_type_specific_data_length );
590 if( !p_sp->p_type_specific_data )
593 memcpy( p_sp->p_type_specific_data, p_peek + 78,
594 p_sp->i_type_specific_data_length );
595 i_peek -= p_sp->i_type_specific_data_length;
598 if( p_sp->i_error_correction_data_length )
600 if( i_peek < p_sp->i_error_correction_data_length )
602 free( p_sp->p_type_specific_data );
606 p_sp->p_error_correction_data =
607 malloc( p_sp->i_error_correction_data_length );
608 if( !p_sp->p_error_correction_data )
610 free( p_sp->p_type_specific_data );
613 memcpy( p_sp->p_error_correction_data,
614 p_peek + 78 + p_sp->i_type_specific_data_length,
615 p_sp->i_error_correction_data_length );
620 "read \"stream Properties object\" stream_type:" GUID_FMT
621 " error_correction_type:" GUID_FMT " time_offset:%"PRId64
622 " type_specific_data_length:%d error_correction_data_length:%d"
623 " flags:0x%x stream_number:%d",
624 GUID_PRINT( p_sp->i_stream_type ),
625 GUID_PRINT( p_sp->i_error_correction_type ),
627 p_sp->i_type_specific_data_length,
628 p_sp->i_error_correction_data_length,
630 p_sp->i_stream_number );
636 static void ASF_FreeObject_stream_properties( asf_object_t *p_obj )
638 asf_object_stream_properties_t *p_sp = &p_obj->stream_properties;
640 FREENULL( p_sp->p_type_specific_data );
641 FREENULL( p_sp->p_error_correction_data );
645 static int ASF_ReadObject_codec_list( stream_t *s, asf_object_t *p_obj )
647 asf_object_codec_list_t *p_cl = &p_obj->codec_list;
649 const uint8_t *p_peek, *p_data;
651 unsigned int i_codec;
653 if( ( i_peek = stream_Peek( s, &p_peek, p_cl->i_object_size ) ) < 44 )
656 ASF_GetGUID( &p_cl->i_reserved, p_peek + 24 );
657 p_cl->i_codec_entries_count = GetWLE( p_peek + 40 );
659 p_data = p_peek + 44;
661 if( p_cl->i_codec_entries_count > 0 )
663 p_cl->codec = calloc( p_cl->i_codec_entries_count,
664 sizeof( asf_codec_entry_t ) );
668 for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
670 asf_codec_entry_t *p_codec = &p_cl->codec[i_codec];
672 if( !ASF_HAVE( 2+2+2 ) )
676 p_codec->i_type = ASF_READ2();
678 /* XXX the length here are the number of *unicode* characters and
679 * not of bytes like nearly every elsewhere */
682 p_codec->psz_name = ASF_READS( 2*ASF_READ2() );
685 p_codec->psz_description = ASF_READS( 2*ASF_READ2() );
687 /* opaque information */
688 p_codec->i_information_length = ASF_READ2();
689 if( p_codec->i_information_length > 0 && ASF_HAVE( p_codec->i_information_length ) )
691 p_codec->p_information = malloc( p_codec->i_information_length );
692 if( p_codec->p_information )
693 memcpy( p_codec->p_information, p_data, p_codec->i_information_length );
695 p_codec->i_information_length = 0;
696 p_data += p_codec->i_information_length;
699 p_cl->i_codec_entries_count = i_codec;
703 msg_Dbg( s, "read \"codec list object\" reserved_guid:" GUID_FMT
704 " codec_entries_count:%d",
705 GUID_PRINT( p_cl->i_reserved ), p_cl->i_codec_entries_count );
707 for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
709 const asf_codec_entry_t *p_codec = &p_cl->codec[i_codec];
711 msg_Dbg( s, " - codec[%d] %s name:\"%s\" "
712 "description:\"%s\" information_length:%d",
713 i_codec, ( p_codec->i_type == ASF_CODEC_TYPE_VIDEO ) ?
714 "video" : ( ( p_codec->i_type == ASF_CODEC_TYPE_AUDIO ) ?
715 "audio" : "unknown" ),
716 p_codec->psz_name, p_codec->psz_description,
717 p_codec->i_information_length );
724 static void ASF_FreeObject_codec_list( asf_object_t *p_obj )
726 asf_object_codec_list_t *p_cl = &p_obj->codec_list;
727 unsigned int i_codec;
729 for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
731 asf_codec_entry_t *p_codec = &p_cl->codec[i_codec];
733 FREENULL( p_codec->psz_name );
734 FREENULL( p_codec->psz_description );
735 FREENULL( p_codec->p_information );
737 FREENULL( p_cl->codec );
740 /* Microsoft should go to hell. This time the length give number of bytes
741 * and for the some others object, length give char16 count ... */
742 static int ASF_ReadObject_content_description(stream_t *s, asf_object_t *p_obj)
744 asf_object_content_description_t *p_cd = &p_obj->content_description;
745 const uint8_t *p_peek, *p_data;
746 int i_peek, i_title, i_artist, i_copyright, i_description, i_rating;
747 vlc_iconv_t cd = (vlc_iconv_t)-1;
748 const char *ib = NULL;
750 size_t i_ibl, i_obl, i_len;
752 if( ( i_peek = stream_Peek( s, &p_peek, p_cd->i_object_size ) ) < 34 )
755 cd = vlc_iconv_open("UTF-8", "UTF-16LE");
756 if( cd == (vlc_iconv_t)-1 )
758 msg_Err( s, "vlc_iconv_open failed" );
762 /* FIXME i_size*3 is the worst case. */
763 #define GETSTRINGW( psz_str, i_size ) do { \
764 psz_str = calloc( i_size*3+1, sizeof(char) ); \
766 ib = (const char *)p_data; \
770 i_len = vlc_iconv(cd, &ib, &i_ibl, &ob, &i_obl); \
774 p_data = p_peek + 24;
776 i_title = ASF_READ2();
777 i_artist = ASF_READ2();
778 i_copyright = ASF_READ2();
779 i_description = ASF_READ2();
780 i_rating = ASF_READ2();
782 if( !ASF_HAVE( i_title+i_artist+i_copyright+i_description+i_rating ) )
784 vlc_iconv_close( cd );
788 GETSTRINGW( p_cd->psz_title, i_title );
789 GETSTRINGW( p_cd->psz_artist, i_artist );
790 GETSTRINGW( p_cd->psz_copyright, i_copyright );
791 GETSTRINGW( p_cd->psz_description, i_description );
792 GETSTRINGW( p_cd->psz_rating, i_rating );
798 "read \"content description object\" title:\"%s\" artist:\"%s\" copyright:\"%s\" description:\"%s\" rating:\"%s\"",
802 p_cd->psz_description,
810 static void ASF_FreeObject_content_description( asf_object_t *p_obj)
812 asf_object_content_description_t *p_cd = &p_obj->content_description;
814 FREENULL( p_cd->psz_title );
815 FREENULL( p_cd->psz_artist );
816 FREENULL( p_cd->psz_copyright );
817 FREENULL( p_cd->psz_description );
818 FREENULL( p_cd->psz_rating );
822 static int ASF_ReadObject_language_list(stream_t *s, asf_object_t *p_obj)
824 asf_object_language_list_t *p_ll = &p_obj->language_list;
825 const uint8_t *p_peek, *p_data;
829 if( ( i_peek = stream_Peek( s, &p_peek, p_ll->i_object_size ) ) < 26 )
832 p_data = &p_peek[24];
834 p_ll->i_language = ASF_READ2();
835 if( p_ll->i_language > 0 )
837 p_ll->ppsz_language = calloc( p_ll->i_language, sizeof( char *) );
838 if( !p_ll->ppsz_language )
841 for( i = 0; i < p_ll->i_language; i++ )
845 p_ll->ppsz_language[i] = ASF_READS( ASF_READ1() );
847 p_ll->i_language = i;
851 msg_Dbg( s, "read \"language list object\" %d entries",
853 for( i = 0; i < p_ll->i_language; i++ )
854 msg_Dbg( s, " - '%s'",
855 p_ll->ppsz_language[i] );
860 static void ASF_FreeObject_language_list( asf_object_t *p_obj)
862 asf_object_language_list_t *p_ll = &p_obj->language_list;
865 for( i = 0; i < p_ll->i_language; i++ )
866 FREENULL( p_ll->ppsz_language[i] );
867 FREENULL( p_ll->ppsz_language );
870 /* Stream bitrate properties */
871 static int ASF_ReadObject_stream_bitrate_properties( stream_t *s,
874 asf_object_stream_bitrate_properties_t *p_sb = &p_obj->stream_bitrate;
875 const uint8_t *p_peek, *p_data;
879 if( ( i_peek = stream_Peek( s, &p_peek, p_sb->i_object_size ) ) < 26 )
882 p_data = &p_peek[24];
884 p_sb->i_bitrate = ASF_READ2();
885 if( p_sb->i_bitrate > 127 )
886 p_sb->i_bitrate = 127; /* Buggy ? */
887 for( i = 0; i < p_sb->i_bitrate; i++ )
889 if( !ASF_HAVE(2 + 4) )
891 p_sb->bitrate[i].i_stream_number = ASF_READ2()& 0x7f;
892 p_sb->bitrate[i].i_avg_bitrate = ASF_READ4();
897 msg_Dbg( s,"read \"stream bitrate properties object\"" );
898 for( i = 0; i < p_sb->i_bitrate; i++ )
900 msg_Dbg( s," - stream=%d bitrate=%d",
901 p_sb->bitrate[i].i_stream_number,
902 p_sb->bitrate[i].i_avg_bitrate );
907 static void ASF_FreeObject_stream_bitrate_properties( asf_object_t *p_obj)
912 static int ASF_ReadObject_extended_stream_properties( stream_t *s,
915 asf_object_extended_stream_properties_t *p_esp = &p_obj->ext_stream;
916 const uint8_t *p_peek, *p_data;
919 if( ( i_peek = stream_Peek( s, &p_peek, p_esp->i_object_size ) ) < 88 )
922 p_data = &p_peek[24];
924 p_esp->i_start_time = GetQWLE( &p_data[0] );
925 p_esp->i_end_time = GetQWLE( &p_data[8] );
926 p_esp->i_data_bitrate = GetDWLE( &p_data[16] );
927 p_esp->i_buffer_size = GetDWLE( &p_data[20] );
928 p_esp->i_initial_buffer_fullness = GetDWLE( &p_data[24] );
929 p_esp->i_alternate_data_bitrate = GetDWLE( &p_data[28] );
930 p_esp->i_alternate_buffer_size = GetDWLE( &p_data[32] );
931 p_esp->i_alternate_initial_buffer_fullness = GetDWLE( &p_data[36] );
932 p_esp->i_maximum_object_size = GetDWLE( &p_data[40] );
933 p_esp->i_flags = GetDWLE( &p_data[44] );
934 p_esp->i_stream_number = GetWLE( &p_data[48] );
935 p_esp->i_language_index = GetWLE( &p_data[50] );
936 p_esp->i_average_time_per_frame= GetQWLE( &p_data[52] );
937 p_esp->i_stream_name_count = GetWLE( &p_data[60] );
938 p_esp->i_payload_extension_system_count = GetWLE( &p_data[62] );
942 p_esp->pi_stream_name_language = calloc( p_esp->i_stream_name_count,
944 p_esp->ppsz_stream_name = calloc( p_esp->i_stream_name_count,
946 if( !p_esp->pi_stream_name_language ||
947 !p_esp->ppsz_stream_name )
949 free( p_esp->pi_stream_name_language );
950 free( p_esp->ppsz_stream_name );
953 for( i = 0; i < p_esp->i_stream_name_count; i++ )
955 if( !ASF_HAVE( 2+2 ) )
957 p_esp->pi_stream_name_language[i] = ASF_READ2();
958 p_esp->ppsz_stream_name[i] = ASF_READS( ASF_READ2() );
960 p_esp->i_stream_name_count = i;
962 for( i = 0; i < p_esp->i_payload_extension_system_count; i++ )
964 ASF_SKIP( 16 ); // GUID
966 ASF_SKIP( ASF_READ4() );
970 if( p_data < &p_peek[i_peek] )
973 /* Cannot fail as peek succeed */
974 stream_Read( s, NULL, p_data - p_peek );
976 p_sp = malloc( sizeof( asf_object_t ) );
978 if( !p_sp || ASF_ReadObject( s, p_sp, NULL ) )
984 /* This p_sp will be inserted by ReadRoot later */
985 p_esp->p_sp = (asf_object_stream_properties_t*)p_sp;
990 msg_Dbg( s, "read \"extended stream properties object\":" );
991 msg_Dbg( s, " - start=%"PRId64" end=%"PRId64,
992 p_esp->i_start_time, p_esp->i_end_time );
993 msg_Dbg( s, " - data bitrate=%d buffer=%d initial fullness=%d",
994 p_esp->i_data_bitrate,
995 p_esp->i_buffer_size,
996 p_esp->i_initial_buffer_fullness );
997 msg_Dbg( s, " - alternate data bitrate=%d buffer=%d initial fullness=%d",
998 p_esp->i_alternate_data_bitrate,
999 p_esp->i_alternate_buffer_size,
1000 p_esp->i_alternate_initial_buffer_fullness );
1001 msg_Dbg( s, " - maximum object size=%d", p_esp->i_maximum_object_size );
1002 msg_Dbg( s, " - flags=0x%x", p_esp->i_flags );
1003 msg_Dbg( s, " - stream number=%d language=%d",
1004 p_esp->i_stream_number, p_esp->i_language_index );
1005 msg_Dbg( s, " - average time per frame=%"PRId64,
1006 p_esp->i_average_time_per_frame );
1007 msg_Dbg( s, " - stream name count=%d", p_esp->i_stream_name_count );
1008 for( i = 0; i < p_esp->i_stream_name_count; i++ )
1009 msg_Dbg( s, " - lang id=%d name=%s",
1010 p_esp->pi_stream_name_language[i],
1011 p_esp->ppsz_stream_name[i] );
1012 msg_Dbg( s, " - payload extension system count=%d",
1013 p_esp->i_payload_extension_system_count );
1017 static void ASF_FreeObject_extended_stream_properties( asf_object_t *p_obj)
1019 asf_object_extended_stream_properties_t *p_esp = &p_obj->ext_stream;
1022 for( i = 0; i < p_esp->i_stream_name_count; i++ )
1023 FREENULL( p_esp->ppsz_stream_name[i] );
1024 FREENULL( p_esp->pi_stream_name_language );
1025 FREENULL( p_esp->ppsz_stream_name );
1029 static int ASF_ReadObject_advanced_mutual_exclusion( stream_t *s,
1030 asf_object_t *p_obj)
1032 asf_object_advanced_mutual_exclusion_t *p_ae = &p_obj->advanced_mutual_exclusion;
1033 const uint8_t *p_peek, *p_data;
1037 if( ( i_peek = stream_Peek( s, &p_peek, p_ae->i_object_size ) ) < 42 )
1038 return VLC_EGENERIC;
1040 p_data = &p_peek[24];
1042 ASF_GetGUID( &p_ae->type, &p_data[0] );
1044 p_ae->i_stream_number_count = ASF_READ2();
1045 p_ae->pi_stream_number = calloc( p_ae->i_stream_number_count, sizeof(int) );
1047 for( i = 0; i < p_ae->i_stream_number_count; i++ )
1051 p_ae->pi_stream_number[i] = ASF_READ2();
1053 p_ae->i_stream_number_count = i;
1056 msg_Dbg( s, "read \"advanced mutual exclusion object\"" );
1057 for( i = 0; i < p_ae->i_stream_number_count; i++ )
1058 msg_Dbg( s, " - stream=%d", p_ae->pi_stream_number[i] );
1062 static void ASF_FreeObject_advanced_mutual_exclusion( asf_object_t *p_obj)
1064 asf_object_advanced_mutual_exclusion_t *p_ae = &p_obj->advanced_mutual_exclusion;
1066 FREENULL( p_ae->pi_stream_number );
1070 static int ASF_ReadObject_stream_prioritization( stream_t *s,
1071 asf_object_t *p_obj)
1073 asf_object_stream_prioritization_t *p_sp = &p_obj->stream_prioritization;
1074 const uint8_t *p_peek, *p_data;
1078 if( ( i_peek = stream_Peek( s, &p_peek, p_sp->i_object_size ) ) < 26 )
1079 return VLC_EGENERIC;
1081 p_data = &p_peek[24];
1083 p_sp->i_priority_count = ASF_READ2();
1085 p_sp->pi_priority_flag = calloc( p_sp->i_priority_count, sizeof(int) );
1086 p_sp->pi_priority_stream_number =
1087 calloc( p_sp->i_priority_count, sizeof(int) );
1089 if( !p_sp->pi_priority_flag || !p_sp->pi_priority_stream_number )
1091 free( p_sp->pi_priority_flag );
1092 free( p_sp->pi_priority_stream_number );
1096 for( i = 0; i < p_sp->i_priority_count; i++ )
1098 if( !ASF_HAVE(2+2) )
1100 p_sp->pi_priority_stream_number[i] = ASF_READ2();
1101 p_sp->pi_priority_flag[i] = ASF_READ2();
1103 p_sp->i_priority_count = i;
1106 msg_Dbg( s, "read \"stream prioritization object\"" );
1107 for( i = 0; i < p_sp->i_priority_count; i++ )
1108 msg_Dbg( s, " - Stream:%d flags=0x%x",
1109 p_sp->pi_priority_stream_number[i],
1110 p_sp->pi_priority_flag[i] );
1114 static void ASF_FreeObject_stream_prioritization( asf_object_t *p_obj)
1116 asf_object_stream_prioritization_t *p_sp = &p_obj->stream_prioritization;
1118 FREENULL( p_sp->pi_priority_stream_number );
1119 FREENULL( p_sp->pi_priority_flag );
1123 static int ASF_ReadObject_extended_content_description( stream_t *s,
1124 asf_object_t *p_obj)
1126 asf_object_extended_content_description_t *p_ec =
1127 &p_obj->extended_content_description;
1128 const uint8_t *p_peek, *p_data;
1132 if( ( i_peek = stream_Peek( s, &p_peek, p_ec->i_object_size ) ) < 26 )
1133 return VLC_EGENERIC;
1135 p_data = &p_peek[24];
1137 p_ec->i_count = ASF_READ2();
1138 p_ec->ppsz_name = calloc( p_ec->i_count, sizeof(char*) );
1139 p_ec->ppsz_value = calloc( p_ec->i_count, sizeof(char*) );
1140 if( !p_ec->ppsz_name || !p_ec->ppsz_value )
1142 free( p_ec->ppsz_name );
1143 free( p_ec->ppsz_value );
1146 for( i = 0; i < p_ec->i_count; i++ )
1151 if( !ASF_HAVE(2 + 2+2) )
1154 p_ec->ppsz_name[i] = ASF_READS( ASF_READ2() );
1157 i_type = ASF_READ2();
1158 i_size = ASF_READ2();
1162 p_ec->ppsz_value[i] = ASF_READS( i_size );
1164 else if( i_type == 1 )
1167 static const char hex[16] = "0123456789ABCDEF";
1170 p_ec->ppsz_value[i] = malloc( 2*i_size + 1 );
1171 if( p_ec->ppsz_value[i] )
1173 char *psz_value = p_ec->ppsz_value[i];
1174 for( j = 0; j < i_size; j++ )
1176 const uint8_t v = ASF_READ1();
1177 psz_value[2*j+0] = hex[v>>4];
1178 psz_value[2*j+1] = hex[v&0xf];
1180 psz_value[2*i_size] = '\0';
1183 else if( i_type == 2 )
1186 p_ec->ppsz_value[i] = strdup( ASF_READ1() ? "true" : "false" );
1189 else if( i_type == 3 )
1192 if( asprintf( &p_ec->ppsz_value[i], "%d", ASF_READ4() ) == -1 )
1193 p_ec->ppsz_value[i] = NULL;
1195 else if( i_type == 4 )
1198 if( asprintf( &p_ec->ppsz_value[i], "%"PRId64, ASF_READ8() ) == -1 )
1199 p_ec->ppsz_value[i] = NULL;
1201 else if( i_type == 5 )
1204 if( asprintf( &p_ec->ppsz_value[i], "%d", ASF_READ2() ) == -1 )
1205 p_ec->ppsz_value[i] = NULL;
1209 p_ec->ppsz_value[i] = NULL;
1216 msg_Dbg( s, "read \"extended content description object\"" );
1217 for( i = 0; i < p_ec->i_count; i++ )
1218 msg_Dbg( s, " - '%s' = '%s'",
1220 p_ec->ppsz_value[i] );
1224 static void ASF_FreeObject_extended_content_description( asf_object_t *p_obj)
1226 asf_object_extended_content_description_t *p_ec =
1227 &p_obj->extended_content_description;
1230 for( i = 0; i < p_ec->i_count; i++ )
1232 FREENULL( p_ec->ppsz_name[i] );
1233 FREENULL( p_ec->ppsz_value[i] );
1235 FREENULL( p_ec->ppsz_name );
1236 FREENULL( p_ec->ppsz_value );
1241 static int ASF_ReadObject_XXX(stream_t *s, asf_object_t *p_obj)
1243 asf_object_XXX_t *p_XX =
1244 (asf_object_XXX_t *)p_obj;
1245 const uint8_t *p_peek;
1249 if( ( i_peek = stream_Peek( s, &p_peek, p_XX->i_object_size ) ) < XXX )
1250 return VLC_EGENERIC;
1252 p_data = &p_peek[24];
1256 "Read \"XXX object\"" );
1260 static void ASF_FreeObject_XXX( asf_object_t *p_obj)
1262 asf_object_XXX_t *p_XX =
1263 (asf_object_XXX_t *)p_obj;
1273 int (*ASF_ReadObject_function)( stream_t *, asf_object_t *p_obj );
1274 void (*ASF_FreeObject_function)( asf_object_t *p_obj );
1276 } ASF_Object_Function [] =
1278 { &asf_object_header_guid, ASF_OBJECT_HEADER,
1279 ASF_ReadObject_Header, ASF_FreeObject_Null },
1280 { &asf_object_data_guid, ASF_OBJECT_DATA,
1281 ASF_ReadObject_Data, ASF_FreeObject_Null },
1282 { &asf_object_index_guid, ASF_OBJECT_INDEX,
1283 ASF_ReadObject_Index, ASF_FreeObject_Index },
1284 { &asf_object_file_properties_guid, ASF_OBJECT_FILE_PROPERTIES,
1285 ASF_ReadObject_file_properties, ASF_FreeObject_Null },
1286 { &asf_object_stream_properties_guid, ASF_OBJECT_STREAM_PROPERTIES,
1287 ASF_ReadObject_stream_properties,ASF_FreeObject_stream_properties },
1288 { &asf_object_header_extension_guid, ASF_OBJECT_HEADER_EXTENSION,
1289 ASF_ReadObject_header_extension, ASF_FreeObject_header_extension},
1290 { &asf_object_metadata_guid, ASF_OBJECT_METADATA,
1291 ASF_ReadObject_metadata, ASF_FreeObject_metadata},
1292 { &asf_object_codec_list_guid, ASF_OBJECT_CODEC_LIST,
1293 ASF_ReadObject_codec_list, ASF_FreeObject_codec_list },
1294 { &asf_object_marker_guid, ASF_OBJECT_MARKER, NULL, NULL },
1295 { &asf_object_padding, ASF_OBJECT_PADDING, NULL, NULL },
1296 { &asf_object_content_description_guid, ASF_OBJECT_CONTENT_DESCRIPTION,
1297 ASF_ReadObject_content_description, ASF_FreeObject_content_description },
1298 { &asf_object_language_list, ASF_OBJECT_OTHER,
1299 ASF_ReadObject_language_list, ASF_FreeObject_language_list },
1300 { &asf_object_stream_bitrate_properties, ASF_OBJECT_OTHER,
1301 ASF_ReadObject_stream_bitrate_properties,
1302 ASF_FreeObject_stream_bitrate_properties },
1303 { &asf_object_extended_stream_properties, ASF_OBJECT_OTHER,
1304 ASF_ReadObject_extended_stream_properties,
1305 ASF_FreeObject_extended_stream_properties },
1306 { &asf_object_advanced_mutual_exclusion, ASF_OBJECT_OTHER,
1307 ASF_ReadObject_advanced_mutual_exclusion,
1308 ASF_FreeObject_advanced_mutual_exclusion },
1309 { &asf_object_stream_prioritization, ASF_OBJECT_OTHER,
1310 ASF_ReadObject_stream_prioritization,
1311 ASF_FreeObject_stream_prioritization },
1312 { &asf_object_extended_content_description, ASF_OBJECT_OTHER,
1313 ASF_ReadObject_extended_content_description,
1314 ASF_FreeObject_extended_content_description },
1316 { &asf_object_null_guid, 0, NULL, NULL }
1319 static int ASF_ReadObject( stream_t *s, asf_object_t *p_obj,
1320 asf_object_t *p_father )
1328 memset( p_obj, 0, sizeof( *p_obj ) );
1330 if( ASF_ReadObjectCommon( s, p_obj ) )
1332 msg_Warn( s, "cannot read one asf object" );
1333 return VLC_EGENERIC;
1335 p_obj->common.p_father = p_father;
1336 p_obj->common.p_first = NULL;
1337 p_obj->common.p_next = NULL;
1338 p_obj->common.p_last = NULL;
1340 if( p_obj->common.i_object_size < 24 )
1342 msg_Warn( s, "found a corrupted asf object (size<24)" );
1343 return VLC_EGENERIC;
1346 /* find this object */
1347 for( i_index = 0; ; i_index++ )
1349 if( ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1350 &p_obj->common.i_object_id ) ||
1351 ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1352 &asf_object_null_guid ) )
1357 p_obj->common.i_type = ASF_Object_Function[i_index].i_type;
1359 /* Now load this object */
1360 if( ASF_Object_Function[i_index].ASF_ReadObject_function == NULL )
1362 msg_Warn( s, "unknown asf object (not loaded)" );
1363 i_result = VLC_SUCCESS;
1367 /* XXX ASF_ReadObject_function realloc *pp_obj XXX */
1369 (ASF_Object_Function[i_index].ASF_ReadObject_function)( s, p_obj );
1372 /* link this object with father */
1373 if( p_father && ! i_result )
1375 if( p_father->common.p_first )
1377 p_father->common.p_last->common.p_next = p_obj;
1381 p_father->common.p_first = p_obj;
1383 p_father->common.p_last = p_obj;
1389 static void ASF_FreeObject( stream_t *s, asf_object_t *p_obj )
1392 asf_object_t *p_child;
1397 /* Free all child object */
1398 p_child = p_obj->common.p_first;
1401 asf_object_t *p_next;
1402 p_next = p_child->common.p_next;
1403 ASF_FreeObject( s, p_child );
1407 /* find this object */
1408 for( i_index = 0; ; i_index++ )
1410 if( ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1411 &p_obj->common.i_object_id )||
1412 ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1413 &asf_object_null_guid ) )
1419 /* Now free this object */
1420 if( ASF_Object_Function[i_index].ASF_FreeObject_function == NULL )
1423 "unknown asf object " GUID_FMT,
1424 GUID_PRINT( p_obj->common.i_object_id ) );
1430 "free asf object " GUID_FMT,
1431 GUID_PRINT( p_obj->common.i_object_id ) );
1433 (ASF_Object_Function[i_index].ASF_FreeObject_function)( p_obj );
1438 /*****************************************************************************
1439 * ASF_ObjectDumpDebug:
1440 *****************************************************************************/
1444 const char *psz_name;
1445 } ASF_ObjectDumpDebugInfo[] =
1447 { &asf_object_header_guid, "Header" },
1448 { &asf_object_data_guid, "Data" },
1449 { &asf_object_index_guid, "Index" },
1450 { &asf_object_file_properties_guid, "File Properties" },
1451 { &asf_object_stream_properties_guid, "Stream Properties" },
1452 { &asf_object_content_description_guid, "Content Description" },
1453 { &asf_object_header_extension_guid, "Header Extension" },
1454 { &asf_object_metadata_guid, "Metadata" },
1455 { &asf_object_codec_list_guid, "Codec List" },
1456 { &asf_object_marker_guid, "Marker" },
1457 { &asf_object_stream_type_audio, "Stream Type Audio" },
1458 { &asf_object_stream_type_video, "Stream Type Video" },
1459 { &asf_object_stream_type_command, "Stream Type Command" },
1460 { &asf_object_language_list, "Language List" },
1461 { &asf_object_stream_bitrate_properties, "Stream Bitrate Properties" },
1462 { &asf_object_padding, "Padding" },
1463 { &asf_object_extended_stream_properties, "Extended Stream Properties" },
1464 { &asf_object_advanced_mutual_exclusion, "Advanced Mutual Exclusion" },
1465 { &asf_object_stream_prioritization, "Stream Prioritization" },
1466 { &asf_object_extended_content_description, "Extended content description"},
1468 { NULL, "Unknown" },
1472 static void ASF_ObjectDumpDebug( vlc_object_t *p_obj,
1473 asf_object_common_t *p_node, unsigned 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;
1489 if( i_level >= (sizeof(str) - 1)/5 )
1492 memset( str, ' ', sizeof( str ) );
1493 for( i = 1; i < i_level; i++ )
1497 snprintf( &str[5*i_level], sizeof(str) - 5*i_level,
1498 "+ '%s' GUID "GUID_FMT" size:%"PRIu64"pos:%"PRIu64,
1500 GUID_PRINT( p_node->i_object_id ),
1501 p_node->i_object_size, p_node->i_object_pos );
1503 msg_Dbg( p_obj, "%s", str );
1505 for( p_child = p_node->p_first; p_child != NULL;
1506 p_child = p_child->common.p_next )
1508 ASF_ObjectDumpDebug( p_obj, &p_child->common, i_level + 1 );
1512 /*****************************************************************************
1513 * ASF_ReadObjetRoot : parse the entire stream/file
1514 *****************************************************************************/
1515 asf_object_root_t *ASF_ReadObjectRoot( stream_t *s, int b_seekable )
1517 asf_object_root_t *p_root = malloc( sizeof( asf_object_root_t ) );
1518 asf_object_t *p_obj;
1523 p_root->i_type = ASF_OBJECT_ROOT;
1524 memcpy( &p_root->i_object_id, &asf_object_null_guid, sizeof( guid_t ) );
1525 p_root->i_object_pos = stream_Tell( s );
1526 p_root->i_object_size = 0;
1527 p_root->p_first = NULL;
1528 p_root->p_last = NULL;
1529 p_root->p_next = NULL;
1530 p_root->p_hdr = NULL;
1531 p_root->p_data = NULL;
1532 p_root->p_fp = NULL;
1533 p_root->p_index = NULL;
1534 p_root->p_metadata = NULL;
1538 p_obj = malloc( sizeof( asf_object_t ) );
1540 if( !p_obj || ASF_ReadObject( s, p_obj, (asf_object_t*)p_root ) )
1545 switch( p_obj->common.i_type )
1547 case( ASF_OBJECT_HEADER ):
1548 p_root->p_hdr = (asf_object_header_t*)p_obj;
1550 case( ASF_OBJECT_DATA ):
1551 p_root->p_data = (asf_object_data_t*)p_obj;
1553 case( ASF_OBJECT_INDEX ):
1554 p_root->p_index = (asf_object_index_t*)p_obj;
1557 msg_Warn( s, "unknow object found" );
1560 if( p_obj->common.i_type == ASF_OBJECT_DATA &&
1561 p_obj->common.i_object_size <= 50 )
1563 /* probably a dump of broadcasted asf */
1566 if( !b_seekable && p_root->p_hdr && p_root->p_data )
1568 /* For unseekable stream it's enough to play */
1572 if( ASF_NextObject( s, p_obj ) ) /* Go to the next object */
1576 if( p_root->p_hdr != NULL && p_root->p_data != NULL )
1578 p_root->p_fp = ASF_FindObject( p_root->p_hdr,
1579 &asf_object_file_properties_guid, 0 );
1583 asf_object_t *p_hdr_ext =
1584 ASF_FindObject( p_root->p_hdr,
1585 &asf_object_header_extension_guid, 0 );
1591 p_root->p_metadata =
1592 ASF_FindObject( p_hdr_ext,
1593 &asf_object_metadata_guid, 0 );
1594 /* Special case for broken designed file format :( */
1595 i_ext_stream = ASF_CountObject( p_hdr_ext,
1596 &asf_object_extended_stream_properties );
1597 for( i = 0; i < i_ext_stream; i++ )
1599 asf_object_t *p_esp =
1600 ASF_FindObject( p_hdr_ext,
1601 &asf_object_extended_stream_properties, i );
1602 if( p_esp->ext_stream.p_sp )
1604 asf_object_t *p_sp =
1605 (asf_object_t*)p_esp->ext_stream.p_sp;
1607 /* Insert this p_sp */
1608 p_root->p_hdr->p_last->common.p_next = p_sp;
1609 p_root->p_hdr->p_last = p_sp;
1611 p_sp->common.p_father = (asf_object_t*)p_root->p_hdr;
1616 ASF_ObjectDumpDebug( VLC_OBJECT(s),
1617 (asf_object_common_t*)p_root, 0 );
1620 msg_Warn( s, "cannot find file properties object" );
1624 ASF_FreeObjectRoot( s, p_root );
1628 void ASF_FreeObjectRoot( stream_t *s, asf_object_root_t *p_root )
1630 asf_object_t *p_obj;
1632 p_obj = p_root->p_first;
1635 asf_object_t *p_next;
1636 p_next = p_obj->common.p_next;
1637 ASF_FreeObject( s, p_obj );
1643 int __ASF_CountObject( asf_object_t *p_obj, const guid_t *p_guid )
1646 asf_object_t *p_child;
1652 p_child = p_obj->common.p_first;
1655 if( ASF_CmpGUID( &p_child->common.i_object_id, p_guid ) )
1658 p_child = p_child->common.p_next;
1663 void *__ASF_FindObject( asf_object_t *p_obj, const guid_t *p_guid,
1666 asf_object_t *p_child;
1668 p_child = p_obj->common.p_first;
1672 if( ASF_CmpGUID( &p_child->common.i_object_id, p_guid ) )
1679 p_child = p_child->common.p_next;