1 /*****************************************************************************
2 * libasf.c : asf stream demux module for vlc
3 *****************************************************************************
4 * Copyright © 2001-2004, 2006-2008 the VideoLAN team
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <vlc_demux.h>
29 #include <vlc_charset.h> /* FromCharset */
36 * They ensure that invalid reads will not create problems.
37 * They are expansion safe
38 * They make the following assumptions:
39 * const uint8_t *p_peek exists and points to the start of a buffer
40 * int i_peek gives the size of the buffer pointed by p_peek
41 * const uint8_t *p_data exits and points to the data inside p_peek to be read.
44 * Check that n bytes can be read */
45 static inline bool AsfObjectHelperHave( const uint8_t *p_peek, int i_peek, const uint8_t *p_current, int i_wanted )
47 if( i_wanted < 0 || i_wanted > i_peek )
49 return &p_current[i_wanted] <= &p_peek[i_peek];
51 #define ASF_HAVE(n) AsfObjectHelperHave( p_peek, i_peek, p_data, n )
54 * Skip n bytes if possible */
55 static inline void AsfObjectHelperSkip( const uint8_t *p_peek, int i_peek, uint8_t **pp_data, int i_wanted )
57 if( AsfObjectHelperHave( p_peek, i_peek, *pp_data, i_wanted ) )
60 *pp_data = (uint8_t*)&p_peek[i_peek];
62 #define ASF_SKIP(n) AsfObjectHelperSkip( p_peek, i_peek, (uint8_t**)&p_data, n )
65 * Read X byte if possible, else return 0 */
66 #define ASF_FUNCTION_READ_X(type, x, cmd ) \
67 static inline type AsfObjectHelperRead##x( const uint8_t *p_peek, int i_peek, uint8_t **pp_data ) { \
68 uint8_t *p_data = *pp_data; \
75 ASF_FUNCTION_READ_X( uint8_t, 1, *p_data )
76 ASF_FUNCTION_READ_X( uint16_t, 2, GetWLE(p_data) )
77 ASF_FUNCTION_READ_X( uint32_t, 4, GetDWLE(p_data) )
78 ASF_FUNCTION_READ_X( uint64_t, 8, GetQWLE(p_data) )
79 #define ASF_READ1() AsfObjectHelperRead1( p_peek, i_peek, (uint8_t**)&p_data )
80 #define ASF_READ2() AsfObjectHelperRead2( p_peek, i_peek, (uint8_t**)&p_data )
81 #define ASF_READ4() AsfObjectHelperRead4( p_peek, i_peek, (uint8_t**)&p_data )
82 #define ASF_READ8() AsfObjectHelperRead8( p_peek, i_peek, (uint8_t**)&p_data )
85 * Read a string of n/2 wchar long ie n bytes. Do a stupid conversion (suppose latin1)
86 * Return allocated "" if not possible */
87 static char *AsfObjectHelperReadString( const uint8_t *p_peek, int i_peek, uint8_t **pp_data, int i_size )
89 uint8_t *p_data = *pp_data;
91 if( ASF_HAVE(i_size) )
93 psz_string = calloc( i_size/2 + 1, sizeof( char ) );
96 for( int i = 0; i < i_size/2; i++ )
97 psz_string[i] = GetWLE( &p_data[2*i] );
98 psz_string[i_size/2] = '\0';
103 psz_string = strdup("");
109 #define ASF_READS(n) AsfObjectHelperReadString( p_peek, i_peek, (uint8_t**)&p_data, n )
111 /****************************************************************************
113 ****************************************************************************/
114 static int ASF_ReadObject( stream_t *, asf_object_t *, asf_object_t * );
116 /****************************************************************************
118 ****************************************************************************/
119 static int ASF_ReadObjectCommon( stream_t *s, asf_object_t *p_obj )
121 asf_object_common_t *p_common = &p_obj->common;
122 const uint8_t *p_peek;
124 if( stream_Peek( s, &p_peek, 24 ) < 24 )
127 ASF_GetGUID( &p_common->i_object_id, p_peek );
128 p_common->i_object_size = GetQWLE( p_peek + 16 );
129 p_common->i_object_pos = stream_Tell( s );
130 p_common->p_next = NULL;
134 "found object guid: " GUID_FMT " size:%"PRId64,
135 GUID_PRINT( p_common->i_object_id ),
136 p_common->i_object_size );
142 static int ASF_NextObject( stream_t *s, asf_object_t *p_obj )
147 if( ASF_ReadObjectCommon( s, &obj ) )
153 if( p_obj->common.i_object_size <= 0 )
156 if( p_obj->common.p_father &&
157 p_obj->common.p_father->common.i_object_size != 0 )
159 if( p_obj->common.p_father->common.i_object_pos +
160 p_obj->common.p_father->common.i_object_size <
161 p_obj->common.i_object_pos + p_obj->common.i_object_size + 24 )
162 /* 24 is min size of an object */
169 return stream_Seek( s, p_obj->common.i_object_pos +
170 p_obj->common.i_object_size );
173 static void ASF_FreeObject_Null( asf_object_t *pp_obj )
178 static int ASF_ReadObject_Header( stream_t *s, asf_object_t *p_obj )
180 asf_object_header_t *p_hdr = &p_obj->header;
181 asf_object_t *p_subobj;
182 const uint8_t *p_peek;
184 if( stream_Peek( s, &p_peek, 30 ) < 30 )
187 p_hdr->i_sub_object_count = GetDWLE( p_peek + 24 );
188 p_hdr->i_reserved1 = p_peek[28];
189 p_hdr->i_reserved2 = p_peek[29];
190 p_hdr->p_first = NULL;
191 p_hdr->p_last = NULL;
195 "read \"header object\" subobj:%d, reserved1:%d, reserved2:%d",
196 p_hdr->i_sub_object_count,
198 p_hdr->i_reserved2 );
201 /* Cannot fail as peek succeed */
202 stream_Read( s, NULL, 30 );
204 /* Now load sub object */
207 p_subobj = malloc( sizeof( asf_object_t ) );
209 if( !p_subobj || ASF_ReadObject( s, p_subobj, (asf_object_t*)p_hdr ) )
214 if( ASF_NextObject( s, p_subobj ) ) /* Go to the next object */
220 static int ASF_ReadObject_Data( stream_t *s, asf_object_t *p_obj )
222 asf_object_data_t *p_data = &p_obj->data;
223 const uint8_t *p_peek;
225 if( stream_Peek( s, &p_peek, 50 ) < 50 )
228 ASF_GetGUID( &p_data->i_file_id, p_peek + 24 );
229 p_data->i_total_data_packets = GetQWLE( p_peek + 40 );
230 p_data->i_reserved = GetWLE( p_peek + 48 );
234 "read \"data object\" file_id:" GUID_FMT " total data packet:"
235 "%"PRId64" reserved:%d",
236 GUID_PRINT( p_data->i_file_id ),
237 p_data->i_total_data_packets,
238 p_data->i_reserved );
244 static int ASF_ReadObject_Index( stream_t *s, asf_object_t *p_obj )
246 asf_object_index_t *p_index = &p_obj->index;
247 const uint8_t *p_peek;
250 /* We just ignore error on the index */
251 if( stream_Peek( s, &p_peek, p_index->i_object_size ) <
252 __MAX( (int64_t)p_index->i_object_size, 56 ) )
255 ASF_GetGUID( &p_index->i_file_id, p_peek + 24 );
256 p_index->i_index_entry_time_interval = GetQWLE( p_peek + 40 );
257 p_index->i_max_packet_count = GetDWLE( p_peek + 48 );
258 p_index->i_index_entry_count = GetDWLE( p_peek + 52 );
259 p_index->index_entry = NULL;
263 "read \"index object\" file_id:" GUID_FMT
264 " index_entry_time_interval:%"PRId64" max_packet_count:%d "
265 "index_entry_count:%ld",
266 GUID_PRINT( p_index->i_file_id ),
267 p_index->i_index_entry_time_interval,
268 p_index->i_max_packet_count,
269 (long int)p_index->i_index_entry_count );
272 /* Sanity checking */
273 if( p_index->i_index_entry_count > (p_index->i_object_size - 56) / 6 )
274 p_index->i_index_entry_count = (p_index->i_object_size - 56) / 6;
276 p_index->index_entry = calloc( p_index->i_index_entry_count,
277 sizeof(asf_index_entry_t) );
278 if( !p_index->index_entry )
281 for( i = 0, p_peek += 56; i < p_index->i_index_entry_count; i++, p_peek += 6 )
283 p_index->index_entry[i].i_packet_number = GetDWLE( p_peek );
284 p_index->index_entry[i].i_packet_count = GetWLE( p_peek + 4 );
290 static void ASF_FreeObject_Index( asf_object_t *p_obj )
292 asf_object_index_t *p_index = &p_obj->index;
294 FREENULL( p_index->index_entry );
297 static int ASF_ReadObject_file_properties( stream_t *s, asf_object_t *p_obj )
299 asf_object_file_properties_t *p_fp = &p_obj->file_properties;
300 const uint8_t *p_peek;
302 if( stream_Peek( s, &p_peek, 104 ) < 104 )
305 ASF_GetGUID( &p_fp->i_file_id, p_peek + 24 );
306 p_fp->i_file_size = GetQWLE( p_peek + 40 );
307 p_fp->i_creation_date = GetQWLE( p_peek + 48 );
308 p_fp->i_data_packets_count = GetQWLE( p_peek + 56 );
309 p_fp->i_play_duration = GetQWLE( p_peek + 64 );
310 p_fp->i_send_duration = GetQWLE( p_peek + 72 );
311 p_fp->i_preroll = GetQWLE( p_peek + 80 );
312 p_fp->i_flags = GetDWLE( p_peek + 88 );
313 p_fp->i_min_data_packet_size = GetDWLE( p_peek + 92 );
314 p_fp->i_max_data_packet_size = GetDWLE( p_peek + 96 );
315 p_fp->i_max_bitrate = GetDWLE( p_peek + 100 );
319 "read \"file properties object\" file_id:" GUID_FMT
320 " file_size:%"PRId64" creation_date:%"PRId64" data_packets_count:"
321 "%"PRId64" play_duration:%"PRId64" send_duration:%"PRId64" preroll:%"PRId64
322 " flags:%d min_data_packet_size:%d "
323 " max_data_packet_size:%d max_bitrate:%d",
324 GUID_PRINT( p_fp->i_file_id ), p_fp->i_file_size,
325 p_fp->i_creation_date, p_fp->i_data_packets_count,
326 p_fp->i_play_duration, p_fp->i_send_duration,
327 p_fp->i_preroll, p_fp->i_flags,
328 p_fp->i_min_data_packet_size, p_fp->i_max_data_packet_size,
329 p_fp->i_max_bitrate );
335 static void ASF_FreeObject_metadata( asf_object_t *p_obj )
337 asf_object_metadata_t *p_meta = &p_obj->metadata;
339 for( unsigned int i = 0; i < p_meta->i_record_entries_count; i++ )
341 free( p_meta->record[i].psz_name );
342 free( p_meta->record[i].p_data );
344 free( p_meta->record );
347 static int ASF_ReadObject_metadata( stream_t *s, asf_object_t *p_obj )
349 asf_object_metadata_t *p_meta = &p_obj->metadata;
353 const uint8_t *p_peek, *p_data;
355 if( ( i_peek = stream_Peek( s, &p_peek, p_meta->i_object_size ) ) <
356 __MAX( (int64_t)p_meta->i_object_size, 26 ) )
359 p_meta->i_record_entries_count = GetWLE( p_peek + 24 );
361 p_data = p_peek + 26;
363 p_meta->record = calloc( p_meta->i_record_entries_count,
364 sizeof(asf_metadata_record_t) );
365 if( !p_meta->record )
368 for( i = 0; i < p_meta->i_record_entries_count; i++ )
370 asf_metadata_record_t *p_record = &p_meta->record[i];
374 if( !ASF_HAVE( 2+2+2+2+4 ) )
377 if( ASF_READ2() != 0 )
380 p_record->i_stream = ASF_READ2();
381 i_name = ASF_READ2();
382 p_record->i_type = ASF_READ2();
383 i_data = ASF_READ4();
385 if( !ASF_HAVE( i_name + i_data ) )
389 p_record->psz_name = ASF_READS( i_name );
392 if( p_record->i_type == ASF_METADATA_TYPE_STRING )
394 p_record->p_data = (uint8_t *)ASF_READS( i_data );
395 p_record->i_data = i_data/2; /* FIXME Is that needed ? */
397 else if( p_record->i_type == ASF_METADATA_TYPE_BYTE )
399 p_record->p_data = malloc( i_data );
400 p_record->i_data = i_data;
401 if( p_record->p_data && i_data > 0 )
402 memcpy( p_record->p_data, p_data, i_data );
406 else if( p_record->i_type == ASF_METADATA_TYPE_QWORD )
408 p_record->i_val = ASF_READ8();
410 else if( p_record->i_type == ASF_METADATA_TYPE_DWORD )
412 p_record->i_val = ASF_READ4();
414 else if( p_record->i_type == ASF_METADATA_TYPE_WORD )
416 p_record->i_val = ASF_READ2();
418 else if( p_record->i_type == ASF_METADATA_TYPE_BOOL )
420 p_record->i_val = ASF_READ2();
428 p_meta->i_record_entries_count = i;
432 "read \"metadata object\" %d entries",
433 p_meta->i_record_entries_count );
434 for( unsigned int j = 0; j < p_meta->i_record_entries_count; j++ )
436 asf_metadata_record_t *p_rec = &p_meta->record[j];
438 if( p_rec->i_type == ASF_METADATA_TYPE_STRING )
439 msg_Dbg( s, " - %s=%s",
440 p_rec->psz_name, p_rec->p_data );
441 else if( p_rec->i_type == ASF_METADATA_TYPE_BYTE )
442 msg_Dbg( s, " - %s (%i bytes)",
443 p_rec->psz_name, p_rec->i_data );
445 msg_Dbg( s, " - %s=%"PRId64,
446 p_rec->psz_name, p_rec->i_val );
453 static int ASF_ReadObject_header_extension( stream_t *s, asf_object_t *p_obj )
455 asf_object_header_extension_t *p_he = &p_obj->header_extension;
457 const uint8_t *p_peek;
459 if( ( i_peek = stream_Peek( s, &p_peek, p_he->i_object_size ) ) < 46)
463 ASF_GetGUID( &p_he->i_reserved1, p_peek + 24 );
464 p_he->i_reserved2 = GetWLE( p_peek + 40 );
465 p_he->i_header_extension_size = GetDWLE( p_peek + 42 );
466 if( p_he->i_header_extension_size )
468 if( (unsigned int)(i_peek-46) < p_he->i_header_extension_size )
471 p_he->p_header_extension_data =
472 malloc( p_he->i_header_extension_size );
473 if( !p_he->p_header_extension_data )
476 memcpy( p_he->p_header_extension_data, p_peek + 46,
477 p_he->i_header_extension_size );
481 p_he->p_header_extension_data = NULL;
486 "read \"header extension object\" reserved1:" GUID_FMT
487 " reserved2:%d header_extension_size:%d",
488 GUID_PRINT( p_he->i_reserved1 ), p_he->i_reserved2,
489 p_he->i_header_extension_size );
492 if( !p_he->i_header_extension_size ) return VLC_SUCCESS;
494 /* Read the extension objects */
495 stream_Read( s, NULL, 46 );
498 asf_object_t *p_obj = malloc( sizeof( asf_object_t ) );
500 if( !p_obj || ASF_ReadObject( s, p_obj, (asf_object_t*)p_he ) )
506 if( ASF_NextObject( s, p_obj ) ) /* Go to the next object */
515 static void ASF_FreeObject_header_extension( asf_object_t *p_obj )
517 asf_object_header_extension_t *p_he = &p_obj->header_extension;
519 FREENULL( p_he->p_header_extension_data );
522 static int ASF_ReadObject_stream_properties( stream_t *s, asf_object_t *p_obj )
524 asf_object_stream_properties_t *p_sp = &p_obj->stream_properties;
526 const uint8_t *p_peek;
528 if( ( i_peek = stream_Peek( s, &p_peek, p_sp->i_object_size ) ) < 78 )
531 ASF_GetGUID( &p_sp->i_stream_type, p_peek + 24 );
532 ASF_GetGUID( &p_sp->i_error_correction_type, p_peek + 40 );
533 p_sp->i_time_offset = GetQWLE( p_peek + 56 );
534 p_sp->i_type_specific_data_length = GetDWLE( p_peek + 64 );
535 p_sp->i_error_correction_data_length = GetDWLE( p_peek + 68 );
536 p_sp->i_flags = GetWLE( p_peek + 72 );
537 p_sp->i_stream_number = p_sp->i_flags&0x07f;
538 p_sp->i_reserved = GetDWLE( p_peek + 74 );
541 if( p_sp->i_type_specific_data_length )
543 if( i_peek < p_sp->i_type_specific_data_length )
546 p_sp->p_type_specific_data =
547 malloc( p_sp->i_type_specific_data_length );
548 if( !p_sp->p_type_specific_data )
551 memcpy( p_sp->p_type_specific_data, p_peek + 78,
552 p_sp->i_type_specific_data_length );
553 i_peek -= p_sp->i_type_specific_data_length;
556 if( p_sp->i_error_correction_data_length )
558 if( i_peek < p_sp->i_error_correction_data_length )
560 free( p_sp->p_type_specific_data );
564 p_sp->p_error_correction_data =
565 malloc( p_sp->i_error_correction_data_length );
566 if( !p_sp->p_error_correction_data )
568 free( p_sp->p_type_specific_data );
571 memcpy( p_sp->p_error_correction_data,
572 p_peek + 78 + p_sp->i_type_specific_data_length,
573 p_sp->i_error_correction_data_length );
578 "read \"stream Properties object\" stream_type:" GUID_FMT
579 " error_correction_type:" GUID_FMT " time_offset:%"PRId64
580 " type_specific_data_length:%d error_correction_data_length:%d"
581 " flags:0x%x stream_number:%d",
582 GUID_PRINT( p_sp->i_stream_type ),
583 GUID_PRINT( p_sp->i_error_correction_type ),
585 p_sp->i_type_specific_data_length,
586 p_sp->i_error_correction_data_length,
588 p_sp->i_stream_number );
594 static void ASF_FreeObject_stream_properties( asf_object_t *p_obj )
596 asf_object_stream_properties_t *p_sp = &p_obj->stream_properties;
598 FREENULL( p_sp->p_type_specific_data );
599 FREENULL( p_sp->p_error_correction_data );
603 static int ASF_ReadObject_codec_list( stream_t *s, asf_object_t *p_obj )
605 asf_object_codec_list_t *p_cl = &p_obj->codec_list;
607 const uint8_t *p_peek, *p_data;
609 unsigned int i_codec;
611 if( ( i_peek = stream_Peek( s, &p_peek, p_cl->i_object_size ) ) < 44 )
614 ASF_GetGUID( &p_cl->i_reserved, p_peek + 24 );
615 p_cl->i_codec_entries_count = GetDWLE( p_peek + 40 );
617 p_data = p_peek + 44;
619 if( p_cl->i_codec_entries_count > 0 )
621 p_cl->codec = calloc( p_cl->i_codec_entries_count,
622 sizeof( asf_codec_entry_t ) );
626 for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
628 asf_codec_entry_t *p_codec = &p_cl->codec[i_codec];
630 if( !ASF_HAVE( 2+2+2 ) )
634 p_codec->i_type = ASF_READ2();
636 /* XXX the length here are the number of *unicode* characters and
637 * not of bytes like nearly every elsewhere */
640 p_codec->psz_name = ASF_READS( 2*ASF_READ2() );
643 p_codec->psz_description = ASF_READS( 2*ASF_READ2() );
645 /* opaque information */
646 p_codec->i_information_length = ASF_READ2();
647 if( p_codec->i_information_length > 0 && ASF_HAVE( p_codec->i_information_length ) )
649 p_codec->p_information = malloc( p_codec->i_information_length );
650 if( p_codec->p_information )
651 memcpy( p_codec->p_information, p_data, p_codec->i_information_length );
653 p_codec->i_information_length = 0;
654 p_data += p_codec->i_information_length;
657 p_cl->i_codec_entries_count = i_codec;
661 msg_Dbg( s, "read \"codec list object\" reserved_guid:" GUID_FMT
662 " codec_entries_count:%d",
663 GUID_PRINT( p_cl->i_reserved ), p_cl->i_codec_entries_count );
665 for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
667 const asf_codec_entry_t *p_codec = &p_cl->codec[i_codec];
669 msg_Dbg( s, " - codec[%d] %s name:\"%s\" "
670 "description:\"%s\" information_length:%d",
671 i_codec, ( p_codec->i_type == ASF_CODEC_TYPE_VIDEO ) ?
672 "video" : ( ( p_codec->i_type == ASF_CODEC_TYPE_AUDIO ) ?
673 "audio" : "unknown" ),
674 p_codec->psz_name, p_codec->psz_description,
675 p_codec->i_information_length );
682 static void ASF_FreeObject_codec_list( asf_object_t *p_obj )
684 asf_object_codec_list_t *p_cl = &p_obj->codec_list;
686 for( unsigned int i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
688 asf_codec_entry_t *p_codec = &p_cl->codec[i_codec];
690 FREENULL( p_codec->psz_name );
691 FREENULL( p_codec->psz_description );
692 FREENULL( p_codec->p_information );
694 FREENULL( p_cl->codec );
697 static inline char *get_wstring( const uint8_t *p_data, size_t i_size )
699 char *psz_str = FromCharset( "UTF-16LE", p_data, i_size );
705 /* Microsoft should go to hell. This time the length give number of bytes
706 * and for the some others object, length give char16 count ... */
707 static int ASF_ReadObject_content_description(stream_t *s, asf_object_t *p_obj)
709 asf_object_content_description_t *p_cd = &p_obj->content_description;
710 const uint8_t *p_peek, *p_data;
711 int i_peek, i_title, i_artist, i_copyright, i_description, i_rating;
713 if( ( i_peek = stream_Peek( s, &p_peek, p_cd->i_object_size ) ) < 34 )
716 p_data = p_peek + 24;
718 i_title = ASF_READ2();
719 i_artist = ASF_READ2();
720 i_copyright = ASF_READ2();
721 i_description = ASF_READ2();
722 i_rating = ASF_READ2();
724 if( !ASF_HAVE( i_title+i_artist+i_copyright+i_description+i_rating ) )
727 p_cd->psz_title = get_wstring( p_data, i_title );
728 p_cd->psz_artist = get_wstring( p_data, i_artist );
729 p_cd->psz_copyright = get_wstring( p_data, i_copyright );
730 p_cd->psz_description = get_wstring( p_data, i_description );
731 p_cd->psz_rating = get_wstring( p_data, i_rating );
735 "read \"content description object\" title:\"%s\" artist:\"%s\" copyright:\"%s\" description:\"%s\" rating:\"%s\"",
739 p_cd->psz_description,
746 static void ASF_FreeObject_content_description( asf_object_t *p_obj)
748 asf_object_content_description_t *p_cd = &p_obj->content_description;
750 FREENULL( p_cd->psz_title );
751 FREENULL( p_cd->psz_artist );
752 FREENULL( p_cd->psz_copyright );
753 FREENULL( p_cd->psz_description );
754 FREENULL( p_cd->psz_rating );
758 static int ASF_ReadObject_language_list(stream_t *s, asf_object_t *p_obj)
760 asf_object_language_list_t *p_ll = &p_obj->language_list;
761 const uint8_t *p_peek, *p_data;
765 if( ( i_peek = stream_Peek( s, &p_peek, p_ll->i_object_size ) ) < 26 )
768 p_data = &p_peek[24];
770 p_ll->i_language = ASF_READ2();
771 if( p_ll->i_language > 0 )
773 p_ll->ppsz_language = calloc( p_ll->i_language, sizeof( char *) );
774 if( !p_ll->ppsz_language )
777 for( i = 0; i < p_ll->i_language; i++ )
781 p_ll->ppsz_language[i] = ASF_READS( ASF_READ1() );
783 p_ll->i_language = i;
787 msg_Dbg( s, "read \"language list object\" %d entries",
789 for( i = 0; i < p_ll->i_language; i++ )
790 msg_Dbg( s, " - '%s'",
791 p_ll->ppsz_language[i] );
796 static void ASF_FreeObject_language_list( asf_object_t *p_obj)
798 asf_object_language_list_t *p_ll = &p_obj->language_list;
801 for( i = 0; i < p_ll->i_language; i++ )
802 FREENULL( p_ll->ppsz_language[i] );
803 FREENULL( p_ll->ppsz_language );
806 /* Stream bitrate properties */
807 static int ASF_ReadObject_stream_bitrate_properties( stream_t *s,
810 asf_object_stream_bitrate_properties_t *p_sb = &p_obj->stream_bitrate;
811 const uint8_t *p_peek, *p_data;
815 if( ( i_peek = stream_Peek( s, &p_peek, p_sb->i_object_size ) ) < 26 )
818 p_data = &p_peek[24];
820 p_sb->i_bitrate = ASF_READ2();
821 if( p_sb->i_bitrate > 127 )
822 p_sb->i_bitrate = 127; /* Buggy ? */
823 for( i = 0; i < p_sb->i_bitrate; i++ )
825 if( !ASF_HAVE(2 + 4) )
827 p_sb->bitrate[i].i_stream_number = ASF_READ2()& 0x7f;
828 p_sb->bitrate[i].i_avg_bitrate = ASF_READ4();
833 msg_Dbg( s,"read \"stream bitrate properties object\"" );
834 for( i = 0; i < p_sb->i_bitrate; i++ )
836 msg_Dbg( s," - stream=%d bitrate=%d",
837 p_sb->bitrate[i].i_stream_number,
838 p_sb->bitrate[i].i_avg_bitrate );
843 static void ASF_FreeObject_stream_bitrate_properties( asf_object_t *p_obj)
848 static int ASF_ReadObject_extended_stream_properties( stream_t *s,
851 asf_object_extended_stream_properties_t *p_esp = &p_obj->ext_stream;
852 const uint8_t *p_peek, *p_data;
855 if( ( i_peek = stream_Peek( s, &p_peek, p_esp->i_object_size ) ) < 88 )
858 p_data = &p_peek[24];
860 p_esp->i_start_time = GetQWLE( &p_data[0] );
861 p_esp->i_end_time = GetQWLE( &p_data[8] );
862 p_esp->i_data_bitrate = GetDWLE( &p_data[16] );
863 p_esp->i_buffer_size = GetDWLE( &p_data[20] );
864 p_esp->i_initial_buffer_fullness = GetDWLE( &p_data[24] );
865 p_esp->i_alternate_data_bitrate = GetDWLE( &p_data[28] );
866 p_esp->i_alternate_buffer_size = GetDWLE( &p_data[32] );
867 p_esp->i_alternate_initial_buffer_fullness = GetDWLE( &p_data[36] );
868 p_esp->i_maximum_object_size = GetDWLE( &p_data[40] );
869 p_esp->i_flags = GetDWLE( &p_data[44] );
870 p_esp->i_stream_number = GetWLE( &p_data[48] );
871 p_esp->i_language_index = GetWLE( &p_data[50] );
872 p_esp->i_average_time_per_frame= GetQWLE( &p_data[52] );
873 p_esp->i_stream_name_count = GetWLE( &p_data[60] );
874 p_esp->i_payload_extension_system_count = GetWLE( &p_data[62] );
878 p_esp->pi_stream_name_language = calloc( p_esp->i_stream_name_count,
880 p_esp->ppsz_stream_name = calloc( p_esp->i_stream_name_count,
882 if( !p_esp->pi_stream_name_language ||
883 !p_esp->ppsz_stream_name )
885 free( p_esp->pi_stream_name_language );
886 free( p_esp->ppsz_stream_name );
889 for( i = 0; i < p_esp->i_stream_name_count; i++ )
891 if( !ASF_HAVE( 2+2 ) )
893 p_esp->pi_stream_name_language[i] = ASF_READ2();
894 p_esp->ppsz_stream_name[i] = ASF_READS( ASF_READ2() );
896 p_esp->i_stream_name_count = i;
898 for( i = 0; i < p_esp->i_payload_extension_system_count; i++ )
900 ASF_SKIP( 16 ); // GUID
902 ASF_SKIP( ASF_READ4() );
906 if( p_data < &p_peek[i_peek] )
909 /* Cannot fail as peek succeed */
910 stream_Read( s, NULL, p_data - p_peek );
912 p_sp = malloc( sizeof( asf_object_t ) );
914 if( !p_sp || ASF_ReadObject( s, p_sp, NULL ) )
920 /* This p_sp will be inserted by ReadRoot later */
921 p_esp->p_sp = (asf_object_stream_properties_t*)p_sp;
926 msg_Dbg( s, "read \"extended stream properties object\":" );
927 msg_Dbg( s, " - start=%"PRId64" end=%"PRId64,
928 p_esp->i_start_time, p_esp->i_end_time );
929 msg_Dbg( s, " - data bitrate=%d buffer=%d initial fullness=%d",
930 p_esp->i_data_bitrate,
931 p_esp->i_buffer_size,
932 p_esp->i_initial_buffer_fullness );
933 msg_Dbg( s, " - alternate data bitrate=%d buffer=%d initial fullness=%d",
934 p_esp->i_alternate_data_bitrate,
935 p_esp->i_alternate_buffer_size,
936 p_esp->i_alternate_initial_buffer_fullness );
937 msg_Dbg( s, " - maximum object size=%d", p_esp->i_maximum_object_size );
938 msg_Dbg( s, " - flags=0x%x", p_esp->i_flags );
939 msg_Dbg( s, " - stream number=%d language=%d",
940 p_esp->i_stream_number, p_esp->i_language_index );
941 msg_Dbg( s, " - average time per frame=%"PRId64,
942 p_esp->i_average_time_per_frame );
943 msg_Dbg( s, " - stream name count=%d", p_esp->i_stream_name_count );
944 for( i = 0; i < p_esp->i_stream_name_count; i++ )
945 msg_Dbg( s, " - lang id=%d name=%s",
946 p_esp->pi_stream_name_language[i],
947 p_esp->ppsz_stream_name[i] );
948 msg_Dbg( s, " - payload extension system count=%d",
949 p_esp->i_payload_extension_system_count );
953 static void ASF_FreeObject_extended_stream_properties( asf_object_t *p_obj)
955 asf_object_extended_stream_properties_t *p_esp = &p_obj->ext_stream;
957 for( int i = 0; i < p_esp->i_stream_name_count; i++ )
958 FREENULL( p_esp->ppsz_stream_name[i] );
959 FREENULL( p_esp->pi_stream_name_language );
960 FREENULL( p_esp->ppsz_stream_name );
964 static int ASF_ReadObject_advanced_mutual_exclusion( stream_t *s,
967 asf_object_advanced_mutual_exclusion_t *p_ae = &p_obj->advanced_mutual_exclusion;
968 const uint8_t *p_peek, *p_data;
972 if( ( i_peek = stream_Peek( s, &p_peek, p_ae->i_object_size ) ) < 42 )
975 p_data = &p_peek[24];
977 ASF_GetGUID( &p_ae->type, &p_data[0] );
980 if( guidcmp( &p_ae->type, &asf_guid_mutex_language ) )
981 msg_Dbg( s, "Language exclusion" );
982 else if( guidcmp( &p_ae->type, &asf_guid_mutex_bitrate ) )
983 msg_Dbg( s, "Bitrate exclusion" );
985 msg_Warn(s, "Unknown exclusion type" );
988 p_ae->i_stream_number_count = ASF_READ2();
989 p_ae->pi_stream_number = calloc( p_ae->i_stream_number_count, sizeof(int) );
991 for( i = 0; i < p_ae->i_stream_number_count; i++ )
995 p_ae->pi_stream_number[i] = ASF_READ2();
997 p_ae->i_stream_number_count = i;
1000 msg_Dbg( s, "read \"advanced mutual exclusion object\"" );
1001 for( i = 0; i < p_ae->i_stream_number_count; i++ )
1002 msg_Dbg( s, " - stream=%d", p_ae->pi_stream_number[i] );
1006 static void ASF_FreeObject_advanced_mutual_exclusion( asf_object_t *p_obj)
1008 asf_object_advanced_mutual_exclusion_t *p_ae = &p_obj->advanced_mutual_exclusion;
1010 FREENULL( p_ae->pi_stream_number );
1014 static int ASF_ReadObject_stream_prioritization( stream_t *s,
1015 asf_object_t *p_obj)
1017 asf_object_stream_prioritization_t *p_sp = &p_obj->stream_prioritization;
1018 const uint8_t *p_peek, *p_data;
1022 if( ( i_peek = stream_Peek( s, &p_peek, p_sp->i_object_size ) ) < 26 )
1023 return VLC_EGENERIC;
1025 p_data = &p_peek[24];
1027 p_sp->i_priority_count = ASF_READ2();
1029 p_sp->pi_priority_flag = calloc( p_sp->i_priority_count, sizeof(int) );
1030 p_sp->pi_priority_stream_number =
1031 calloc( p_sp->i_priority_count, sizeof(int) );
1033 if( !p_sp->pi_priority_flag || !p_sp->pi_priority_stream_number )
1035 free( p_sp->pi_priority_flag );
1036 free( p_sp->pi_priority_stream_number );
1040 for( i = 0; i < p_sp->i_priority_count; i++ )
1042 if( !ASF_HAVE(2+2) )
1044 p_sp->pi_priority_stream_number[i] = ASF_READ2();
1045 p_sp->pi_priority_flag[i] = ASF_READ2();
1047 p_sp->i_priority_count = i;
1050 msg_Dbg( s, "read \"stream prioritization object\"" );
1051 for( i = 0; i < p_sp->i_priority_count; i++ )
1052 msg_Dbg( s, " - Stream:%d flags=0x%x",
1053 p_sp->pi_priority_stream_number[i],
1054 p_sp->pi_priority_flag[i] );
1058 static void ASF_FreeObject_stream_prioritization( asf_object_t *p_obj)
1060 asf_object_stream_prioritization_t *p_sp = &p_obj->stream_prioritization;
1062 FREENULL( p_sp->pi_priority_stream_number );
1063 FREENULL( p_sp->pi_priority_flag );
1067 static int ASF_ReadObject_extended_content_description( stream_t *s,
1068 asf_object_t *p_obj)
1070 asf_object_extended_content_description_t *p_ec =
1071 &p_obj->extended_content_description;
1072 const uint8_t *p_peek, *p_data;
1076 if( ( i_peek = stream_Peek( s, &p_peek, p_ec->i_object_size ) ) < 26 )
1077 return VLC_EGENERIC;
1079 p_data = &p_peek[24];
1081 p_ec->i_count = ASF_READ2();
1082 p_ec->ppsz_name = calloc( p_ec->i_count, sizeof(char*) );
1083 p_ec->ppsz_value = calloc( p_ec->i_count, sizeof(char*) );
1084 if( !p_ec->ppsz_name || !p_ec->ppsz_value )
1086 free( p_ec->ppsz_name );
1087 free( p_ec->ppsz_value );
1090 for( i = 0; i < p_ec->i_count; i++ )
1095 if( !ASF_HAVE(2 + 2+2) )
1098 p_ec->ppsz_name[i] = ASF_READS( ASF_READ2() );
1101 i_type = ASF_READ2();
1102 i_size = ASF_READ2();
1106 /* Unicode string */
1107 p_ec->ppsz_value[i] = ASF_READS( i_size );
1109 else if( i_type == 1 )
1112 static const char hex[16] = "0123456789ABCDEF";
1114 p_ec->ppsz_value[i] = malloc( 2*i_size + 1 );
1115 if( p_ec->ppsz_value[i] )
1117 char *psz_value = p_ec->ppsz_value[i];
1118 for( int j = 0; j < i_size; j++ )
1120 const uint8_t v = ASF_READ1();
1121 psz_value[2*j+0] = hex[v>>4];
1122 psz_value[2*j+1] = hex[v&0xf];
1124 psz_value[2*i_size] = '\0';
1127 else if( i_type == 2 )
1130 p_ec->ppsz_value[i] = strdup( ASF_READ1() ? "true" : "false" );
1133 else if( i_type == 3 )
1136 if( asprintf( &p_ec->ppsz_value[i], "%d", ASF_READ4() ) == -1 )
1137 p_ec->ppsz_value[i] = NULL;
1139 else if( i_type == 4 )
1142 if( asprintf( &p_ec->ppsz_value[i], "%"PRId64, ASF_READ8() ) == -1 )
1143 p_ec->ppsz_value[i] = NULL;
1145 else if( i_type == 5 )
1148 if( asprintf( &p_ec->ppsz_value[i], "%d", ASF_READ2() ) == -1 )
1149 p_ec->ppsz_value[i] = NULL;
1153 p_ec->ppsz_value[i] = NULL;
1160 msg_Dbg( s, "read \"extended content description object\"" );
1161 for( i = 0; i < p_ec->i_count; i++ )
1162 msg_Dbg( s, " - '%s' = '%s'",
1164 p_ec->ppsz_value[i] );
1168 static void ASF_FreeObject_extended_content_description( asf_object_t *p_obj)
1170 asf_object_extended_content_description_t *p_ec =
1171 &p_obj->extended_content_description;
1173 for( int i = 0; i < p_ec->i_count; i++ )
1175 FREENULL( p_ec->ppsz_name[i] );
1176 FREENULL( p_ec->ppsz_value[i] );
1178 FREENULL( p_ec->ppsz_name );
1179 FREENULL( p_ec->ppsz_value );
1182 static int ASF_ReadObject_marker(stream_t *s, asf_object_t *p_obj)
1184 asf_object_marker_t *p_mk = (asf_object_marker_t *)p_obj;
1185 const uint8_t *p_peek, *p_data;
1188 if( ( i_peek = stream_Peek( s, &p_peek, p_mk->i_object_size ) ) < 24 )
1189 return VLC_EGENERIC;
1191 p_data = &p_peek[24];
1193 ASF_GetGUID( &p_mk->i_reserved1, p_data );
1195 p_mk->i_count = ASF_READ4();
1196 p_mk->i_reserved2 = ASF_READ2();
1197 p_mk->name = ASF_READS( ASF_READ2() );
1199 if( p_mk->i_count > 0 )
1201 p_mk->marker = calloc( p_mk->i_count,
1202 sizeof( asf_marker_t ) );
1206 for( unsigned i = 0; i < p_mk->i_count; i++ )
1208 asf_marker_t *p_marker = &p_mk->marker[i];
1210 if( !ASF_HAVE(8+8+2+4+4+4) )
1213 p_marker->i_offset = ASF_READ8();
1214 p_marker->i_presentation_time = ASF_READ8();
1215 p_marker->i_entry_length = ASF_READ2();
1216 p_marker->i_send_time = ASF_READ4();
1217 p_marker->i_flags = ASF_READ4();
1218 p_marker->i_marker_description_length = ASF_READ4();
1219 p_marker->p_marker_description = ASF_READS( p_marker->i_marker_description_length * 2 );
1224 msg_Dbg( s, "Read \"marker object\": %i chapters: %s", p_mk->i_count, p_mk->name );
1226 for( unsigned i = 0; i < p_mk->i_count; i++ )
1227 msg_Dbg( s, "New chapter named: %s", p_mk->marker[i].p_marker_description );
1231 static void ASF_FreeObject_marker( asf_object_t *p_obj)
1233 asf_object_marker_t *p_mk = (asf_object_marker_t *)p_obj;
1235 for( unsigned i = 0; i < p_mk->i_count; i++ )
1237 FREENULL( p_mk->marker[i].p_marker_description );
1239 FREENULL( p_mk->name );
1243 static int ASF_ReadObject_XXX(stream_t *s, asf_object_t *p_obj)
1245 asf_object_XXX_t *p_XX =
1246 (asf_object_XXX_t *)p_obj;
1247 const uint8_t *p_peek;
1251 if( ( i_peek = stream_Peek( s, &p_peek, p_XX->i_object_size ) ) < XXX )
1252 return VLC_EGENERIC;
1254 p_data = &p_peek[24];
1258 "Read \"XXX object\"" );
1262 static void ASF_FreeObject_XXX( asf_object_t *p_obj)
1264 asf_object_XXX_t *p_XX =
1265 (asf_object_XXX_t *)p_obj;
1275 int (*ASF_ReadObject_function)( stream_t *, asf_object_t *p_obj );
1276 void (*ASF_FreeObject_function)( asf_object_t *p_obj );
1278 } ASF_Object_Function [] =
1280 { &asf_object_header_guid, ASF_OBJECT_HEADER,
1281 ASF_ReadObject_Header, ASF_FreeObject_Null },
1282 { &asf_object_data_guid, ASF_OBJECT_DATA,
1283 ASF_ReadObject_Data, ASF_FreeObject_Null },
1284 { &asf_object_simple_index_guid, ASF_OBJECT_INDEX,
1285 ASF_ReadObject_Index, ASF_FreeObject_Index },
1286 { &asf_object_file_properties_guid, ASF_OBJECT_FILE_PROPERTIES,
1287 ASF_ReadObject_file_properties, ASF_FreeObject_Null },
1288 { &asf_object_stream_properties_guid, ASF_OBJECT_STREAM_PROPERTIES,
1289 ASF_ReadObject_stream_properties,ASF_FreeObject_stream_properties },
1290 { &asf_object_header_extension_guid, ASF_OBJECT_HEADER_EXTENSION,
1291 ASF_ReadObject_header_extension, ASF_FreeObject_header_extension},
1292 { &asf_object_metadata_guid, ASF_OBJECT_METADATA,
1293 ASF_ReadObject_metadata, ASF_FreeObject_metadata},
1294 { &asf_object_codec_list_guid, ASF_OBJECT_CODEC_LIST,
1295 ASF_ReadObject_codec_list, ASF_FreeObject_codec_list },
1296 { &asf_object_marker_guid, ASF_OBJECT_MARKER,
1297 ASF_ReadObject_marker, ASF_FreeObject_marker },
1298 { &asf_object_padding, ASF_OBJECT_PADDING, NULL, NULL },
1299 { &asf_object_compatibility_guid, ASF_OBJECT_OTHER, NULL, NULL },
1300 { &asf_object_content_description_guid, ASF_OBJECT_CONTENT_DESCRIPTION,
1301 ASF_ReadObject_content_description, ASF_FreeObject_content_description },
1302 { &asf_object_language_list, ASF_OBJECT_OTHER,
1303 ASF_ReadObject_language_list, ASF_FreeObject_language_list },
1304 { &asf_object_stream_bitrate_properties, ASF_OBJECT_OTHER,
1305 ASF_ReadObject_stream_bitrate_properties,
1306 ASF_FreeObject_stream_bitrate_properties },
1307 { &asf_object_extended_stream_properties_guid, ASF_OBJECT_OTHER,
1308 ASF_ReadObject_extended_stream_properties,
1309 ASF_FreeObject_extended_stream_properties },
1310 { &asf_object_advanced_mutual_exclusion, ASF_OBJECT_OTHER,
1311 ASF_ReadObject_advanced_mutual_exclusion,
1312 ASF_FreeObject_advanced_mutual_exclusion },
1313 { &asf_object_stream_prioritization, ASF_OBJECT_OTHER,
1314 ASF_ReadObject_stream_prioritization,
1315 ASF_FreeObject_stream_prioritization },
1316 { &asf_object_extended_content_description, ASF_OBJECT_OTHER,
1317 ASF_ReadObject_extended_content_description,
1318 ASF_FreeObject_extended_content_description },
1320 { &asf_object_null_guid, 0, NULL, NULL }
1323 static int ASF_ReadObject( stream_t *s, asf_object_t *p_obj,
1324 asf_object_t *p_father )
1332 memset( p_obj, 0, sizeof( *p_obj ) );
1334 if( ASF_ReadObjectCommon( s, p_obj ) )
1336 msg_Warn( s, "cannot read one asf object" );
1337 return VLC_EGENERIC;
1339 p_obj->common.p_father = p_father;
1340 p_obj->common.p_first = NULL;
1341 p_obj->common.p_next = NULL;
1342 p_obj->common.p_last = NULL;
1344 if( p_obj->common.i_object_size < 24 )
1346 msg_Warn( s, "found a corrupted asf object (size<24)" );
1347 return VLC_EGENERIC;
1350 /* find this object */
1351 for( i_index = 0; ; i_index++ )
1353 if( guidcmp( ASF_Object_Function[i_index].p_id,
1354 &p_obj->common.i_object_id ) ||
1355 guidcmp( ASF_Object_Function[i_index].p_id,
1356 &asf_object_null_guid ) )
1361 p_obj->common.i_type = ASF_Object_Function[i_index].i_type;
1363 if( i_index == sizeof(ASF_Object_Function)/sizeof(ASF_Object_Function[0]) - 1 )
1364 msg_Warn( s, "unknown asf object (not loaded): " GUID_FMT,
1365 GUID_PRINT( p_obj->common.i_object_id ) );
1367 /* Now load this object */
1368 if( ASF_Object_Function[i_index].ASF_ReadObject_function == NULL )
1370 i_result = VLC_SUCCESS;
1374 /* XXX ASF_ReadObject_function realloc *pp_obj XXX */
1376 (ASF_Object_Function[i_index].ASF_ReadObject_function)( s, p_obj );
1379 /* link this object with father */
1380 if( p_father && ! i_result )
1382 if( p_father->common.p_first )
1384 p_father->common.p_last->common.p_next = p_obj;
1388 p_father->common.p_first = p_obj;
1390 p_father->common.p_last = p_obj;
1396 static void ASF_FreeObject( stream_t *s, asf_object_t *p_obj )
1399 asf_object_t *p_child;
1404 /* Free all child object */
1405 p_child = p_obj->common.p_first;
1408 asf_object_t *p_next;
1409 p_next = p_child->common.p_next;
1410 ASF_FreeObject( s, p_child );
1414 /* find this object */
1415 for( i_index = 0; ; i_index++ )
1417 if( guidcmp( ASF_Object_Function[i_index].p_id,
1418 &p_obj->common.i_object_id )||
1419 guidcmp( ASF_Object_Function[i_index].p_id,
1420 &asf_object_null_guid ) )
1426 /* Now free this object */
1427 if( ASF_Object_Function[i_index].ASF_FreeObject_function != NULL )
1431 "free asf object " GUID_FMT,
1432 GUID_PRINT( p_obj->common.i_object_id ) );
1434 (ASF_Object_Function[i_index].ASF_FreeObject_function)( p_obj );
1439 /*****************************************************************************
1440 * ASF_ObjectDumpDebug:
1441 *****************************************************************************/
1445 const char *psz_name;
1446 } ASF_ObjectDumpDebugInfo[] =
1448 { &asf_object_header_guid, "Header" },
1449 { &asf_object_data_guid, "Data" },
1450 { &asf_object_index_guid, "Index" },
1451 { &asf_object_simple_index_guid, "Simple Index" },
1452 { &asf_object_file_properties_guid, "File Properties" },
1453 { &asf_object_stream_properties_guid, "Stream Properties" },
1454 { &asf_object_content_description_guid, "Content Description" },
1455 { &asf_object_header_extension_guid, "Header Extension" },
1456 { &asf_object_metadata_guid, "Metadata" },
1457 { &asf_object_codec_list_guid, "Codec List" },
1458 { &asf_object_marker_guid, "Marker" },
1459 { &asf_object_stream_type_audio, "Stream Type Audio" },
1460 { &asf_object_stream_type_video, "Stream Type Video" },
1461 { &asf_object_stream_type_command, "Stream Type Command" },
1462 { &asf_object_language_list, "Language List" },
1463 { &asf_object_stream_bitrate_properties, "Stream Bitrate Properties" },
1464 { &asf_object_padding, "Padding" },
1465 { &asf_object_extended_stream_properties_guid, "Extended Stream Properties" },
1466 { &asf_object_advanced_mutual_exclusion, "Advanced Mutual Exclusion" },
1467 { &asf_object_stream_prioritization, "Stream Prioritization" },
1468 { &asf_object_extended_content_description, "Extended content description"},
1470 { NULL, "Unknown" },
1474 static void ASF_ObjectDumpDebug( vlc_object_t *p_obj,
1475 asf_object_common_t *p_node, unsigned i_level )
1478 union asf_object_u *p_child;
1479 const char *psz_name;
1482 for( i = 0; ASF_ObjectDumpDebugInfo[i].p_id != NULL; i++ )
1484 if( guidcmp( ASF_ObjectDumpDebugInfo[i].p_id,
1485 &p_node->i_object_id ) )
1488 psz_name = ASF_ObjectDumpDebugInfo[i].psz_name;
1491 if( i_level >= (sizeof(str) - 1)/5 )
1494 memset( str, ' ', sizeof( str ) );
1495 for( i = 0; i < i_level; i++ )
1499 snprintf( &str[4*i_level], sizeof(str) - 5*i_level,
1502 "GUID "GUID_FMT" size:%"PRIu64" pos:%"PRIu64
1507 , GUID_PRINT( p_node->i_object_id ),
1508 p_node->i_object_size, p_node->i_object_pos
1513 msg_Dbg( p_obj, "%s", str );
1515 for( p_child = p_node->p_first; p_child != NULL;
1516 p_child = p_child->common.p_next )
1518 ASF_ObjectDumpDebug( p_obj, &p_child->common, i_level + 1 );
1522 /*****************************************************************************
1523 * ASF_ReadObjetRoot : parse the entire stream/file
1524 *****************************************************************************/
1525 asf_object_root_t *ASF_ReadObjectRoot( stream_t *s, int b_seekable )
1527 asf_object_root_t *p_root = malloc( sizeof( asf_object_root_t ) );
1528 asf_object_t *p_obj;
1533 p_root->i_type = ASF_OBJECT_ROOT;
1534 memcpy( &p_root->i_object_id, &asf_object_null_guid, sizeof( guid_t ) );
1535 p_root->i_object_pos = stream_Tell( s );
1536 p_root->i_object_size = 0;
1537 p_root->p_first = NULL;
1538 p_root->p_last = NULL;
1539 p_root->p_next = NULL;
1540 p_root->p_hdr = NULL;
1541 p_root->p_data = NULL;
1542 p_root->p_fp = NULL;
1543 p_root->p_index = NULL;
1544 p_root->p_metadata = NULL;
1548 p_obj = malloc( sizeof( asf_object_t ) );
1550 if( !p_obj || ASF_ReadObject( s, p_obj, (asf_object_t*)p_root ) )
1555 switch( p_obj->common.i_type )
1557 case( ASF_OBJECT_HEADER ):
1558 p_root->p_hdr = (asf_object_header_t*)p_obj;
1560 case( ASF_OBJECT_DATA ):
1561 p_root->p_data = (asf_object_data_t*)p_obj;
1563 case( ASF_OBJECT_INDEX ):
1564 p_root->p_index = (asf_object_index_t*)p_obj;
1567 msg_Warn( s, "unknown top-level object found: " GUID_FMT,
1568 GUID_PRINT( p_obj->common.i_object_id ) );
1571 if( p_obj->common.i_type == ASF_OBJECT_DATA &&
1572 p_obj->common.i_object_size <= 50 )
1574 /* probably a dump of broadcasted asf */
1577 if( !b_seekable && p_root->p_hdr && p_root->p_data )
1579 /* For unseekable stream it's enough to play */
1583 if( ASF_NextObject( s, p_obj ) ) /* Go to the next object */
1587 if( p_root->p_hdr != NULL && p_root->p_data != NULL )
1589 p_root->p_fp = ASF_FindObject( p_root->p_hdr,
1590 &asf_object_file_properties_guid, 0 );
1594 asf_object_t *p_hdr_ext =
1595 ASF_FindObject( p_root->p_hdr,
1596 &asf_object_header_extension_guid, 0 );
1601 p_root->p_metadata =
1602 ASF_FindObject( p_hdr_ext,
1603 &asf_object_metadata_guid, 0 );
1604 /* Special case for broken designed file format :( */
1605 i_ext_stream = ASF_CountObject( p_hdr_ext,
1606 &asf_object_extended_stream_properties_guid );
1607 for( int i = 0; i < i_ext_stream; i++ )
1609 asf_object_t *p_esp =
1610 ASF_FindObject( p_hdr_ext,
1611 &asf_object_extended_stream_properties_guid, i );
1612 if( p_esp->ext_stream.p_sp )
1614 asf_object_t *p_sp =
1615 (asf_object_t*)p_esp->ext_stream.p_sp;
1617 /* Insert this p_sp */
1618 p_root->p_hdr->p_last->common.p_next = p_sp;
1619 p_root->p_hdr->p_last = p_sp;
1621 p_sp->common.p_father = (asf_object_t*)p_root->p_hdr;
1626 ASF_ObjectDumpDebug( VLC_OBJECT(s),
1627 (asf_object_common_t*)p_root, 0 );
1630 msg_Warn( s, "cannot find file properties object" );
1634 ASF_FreeObjectRoot( s, p_root );
1638 void ASF_FreeObjectRoot( stream_t *s, asf_object_root_t *p_root )
1640 asf_object_t *p_obj;
1642 p_obj = p_root->p_first;
1645 asf_object_t *p_next;
1646 p_next = p_obj->common.p_next;
1647 ASF_FreeObject( s, p_obj );
1653 int ASF_CountObject( void *_p_obj, const guid_t *p_guid )
1656 asf_object_t *p_child, *p_obj;
1658 p_obj = (asf_object_t *)_p_obj;
1663 p_child = p_obj->common.p_first;
1666 if( guidcmp( &p_child->common.i_object_id, p_guid ) )
1669 p_child = p_child->common.p_next;
1674 void *ASF_FindObject( void *_p_obj, const guid_t *p_guid,
1677 asf_object_t *p_child, *p_obj;
1679 p_obj = (asf_object_t *)_p_obj;
1680 p_child = p_obj->common.p_first;
1684 if( guidcmp( &p_child->common.i_object_id, p_guid ) )
1691 p_child = p_child->common.p_next;