]> git.sesse.net Git - vlc/blob - modules/demux/asf/libasf.c
All: string review (refs: #438)
[vlc] / modules / demux / asf / libasf.c
1 /*****************************************************************************
2  * libasf.c : asf stream demux module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #include <stdlib.h>                                      /* malloc(), free() */
26
27 #include <vlc/vlc.h>
28 #include <vlc/input.h>
29
30 #include "codecs.h"                        /* BITMAPINFOHEADER, WAVEFORMATEX */
31 #include "libasf.h"
32
33 #define ASF_DEBUG 1
34
35 #define FREE( p ) \
36     if( p ) {free( p ); p = NULL; }
37
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 )  \
40     (guid).v1,              \
41     (guid).v2,              \
42     (guid).v3,              \
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]
45
46 /****************************************************************************
47  *
48  ****************************************************************************/
49 static int ASF_ReadObject( stream_t *, asf_object_t *,  asf_object_t * );
50
51 /****************************************************************************
52  * GUID functions
53  ****************************************************************************/
54 void ASF_GetGUID( guid_t *p_guid, uint8_t *p_data )
55 {
56     p_guid->v1 = GetDWLE( p_data );
57     p_guid->v2 = GetWLE( p_data + 4);
58     p_guid->v3 = GetWLE( p_data + 6);
59     memcpy( p_guid->v4, p_data + 8, 8 );
60 }
61
62 int ASF_CmpGUID( const guid_t *p_guid1, const guid_t *p_guid2 )
63 {
64     if( (p_guid1->v1 != p_guid2->v1 )||
65         (p_guid1->v2 != p_guid2->v2 )||
66         (p_guid1->v3 != p_guid2->v3 )||
67         ( memcmp( p_guid1->v4, p_guid2->v4,8 )) )
68     {
69         return( 0 );
70     }
71     return( 1 ); /* match */
72 }
73
74 /****************************************************************************
75  *
76  ****************************************************************************/
77 static int ASF_ReadObjectCommon( stream_t *s, asf_object_t *p_obj )
78 {
79     asf_object_common_t *p_common = (asf_object_common_t*)p_obj;
80     uint8_t             *p_peek;
81
82     if( stream_Peek( s, &p_peek, 24 ) < 24 )
83     {
84         return( VLC_EGENERIC );
85     }
86     ASF_GetGUID( &p_common->i_object_id, p_peek );
87     p_common->i_object_size = GetQWLE( p_peek + 16 );
88     p_common->i_object_pos  = stream_Tell( s );
89     p_common->p_next = NULL;
90
91 #ifdef ASF_DEBUG
92     msg_Dbg( s,
93              "found object guid: " GUID_FMT " size:"I64Fd,
94              GUID_PRINT( p_common->i_object_id ),
95              p_common->i_object_size );
96 #endif
97
98     return( VLC_SUCCESS );
99 }
100
101 static int ASF_NextObject( stream_t *s, asf_object_t *p_obj )
102 {
103     asf_object_t obj;
104     if( p_obj == NULL )
105     {
106         if( ASF_ReadObjectCommon( s, &obj ) )
107         {
108             return( VLC_EGENERIC );
109         }
110         p_obj = &obj;
111     }
112
113     if( p_obj->common.i_object_size <= 0 )
114     {
115         return VLC_EGENERIC;
116     }
117     if( p_obj->common.p_father &&
118         p_obj->common.p_father->common.i_object_size != 0 )
119     {
120         if( p_obj->common.p_father->common.i_object_pos +
121             p_obj->common.p_father->common.i_object_size <
122                 p_obj->common.i_object_pos + p_obj->common.i_object_size + 24 )
123                                 /* 24 is min size of an object */
124         {
125             return VLC_EGENERIC;
126         }
127
128     }
129
130     return stream_Seek( s, p_obj->common.i_object_pos +
131                         p_obj->common.i_object_size );
132 }
133
134 static void ASF_FreeObject_Null( asf_object_t *pp_obj )
135 {
136     return;
137 }
138
139 static int  ASF_ReadObject_Header( stream_t *s, asf_object_t *p_obj )
140 {
141     asf_object_header_t *p_hdr = (asf_object_header_t*)p_obj;
142     asf_object_t        *p_subobj;
143     int                 i_peek;
144     uint8_t             *p_peek;
145
146     if( ( i_peek = stream_Peek( s, &p_peek, 30 ) ) < 30 )
147     {
148        return( VLC_EGENERIC );
149     }
150
151     p_hdr->i_sub_object_count = GetDWLE( p_peek + 24 );
152     p_hdr->i_reserved1 = p_peek[28];
153     p_hdr->i_reserved2 = p_peek[29];
154     p_hdr->p_first = NULL;
155     p_hdr->p_last  = NULL;
156
157 #ifdef ASF_DEBUG
158     msg_Dbg( s,
159              "read \"header object\" subobj:%d, reserved1:%d, reserved2:%d",
160              p_hdr->i_sub_object_count,
161              p_hdr->i_reserved1,
162              p_hdr->i_reserved2 );
163 #endif
164
165     /* Cannot fail as peek succeed */
166     stream_Read( s, NULL, 30 );
167
168     /* Now load sub object */
169     for( ; ; )
170     {
171         p_subobj = malloc( sizeof( asf_object_t ) );
172
173         if( ASF_ReadObject( s, p_subobj, (asf_object_t*)p_hdr ) )
174         {
175             free( p_subobj );
176             break;
177         }
178         if( ASF_NextObject( s, p_subobj ) ) /* Go to the next object */
179         {
180             break;
181         }
182     }
183     return VLC_SUCCESS;
184 }
185
186 static int ASF_ReadObject_Data( stream_t *s, asf_object_t *p_obj )
187 {
188     asf_object_data_t *p_data = (asf_object_data_t*)p_obj;
189     int               i_peek;
190     uint8_t           *p_peek;
191
192     if( ( i_peek = stream_Peek( s, &p_peek, 50 ) ) < 50 )
193     {
194        return VLC_EGENERIC;
195     }
196     ASF_GetGUID( &p_data->i_file_id, p_peek + 24 );
197     p_data->i_total_data_packets = GetQWLE( p_peek + 40 );
198     p_data->i_reserved = GetWLE( p_peek + 48 );
199
200 #ifdef ASF_DEBUG
201     msg_Dbg( s,
202              "read \"data object\" file_id:" GUID_FMT " total data packet:"
203              I64Fd" reserved:%d",
204              GUID_PRINT( p_data->i_file_id ),
205              p_data->i_total_data_packets,
206              p_data->i_reserved );
207 #endif
208
209     return VLC_SUCCESS;
210 }
211
212 static int ASF_ReadObject_Index( stream_t *s, asf_object_t *p_obj )
213 {
214     asf_object_index_t *p_index = (asf_object_index_t*)p_obj;
215     int                i_peek;
216     uint8_t            *p_peek;
217
218     if( ( i_peek = stream_Peek( s, &p_peek, 56 ) ) < 56 )
219     {
220        return VLC_EGENERIC;
221     }
222     ASF_GetGUID( &p_index->i_file_id, p_peek + 24 );
223     p_index->i_index_entry_time_interval = GetQWLE( p_peek + 40 );
224     p_index->i_max_packet_count = GetDWLE( p_peek + 48 );
225     p_index->i_index_entry_count = GetDWLE( p_peek + 52 );
226     p_index->index_entry = NULL; /* FIXME */
227
228 #ifdef ASF_DEBUG
229     msg_Dbg( s,
230             "read \"index object\" file_id:" GUID_FMT
231             " index_entry_time_interval:"I64Fd" max_packet_count:%d "
232             "index_entry_count:%ld",
233             GUID_PRINT( p_index->i_file_id ),
234             p_index->i_index_entry_time_interval,
235             p_index->i_max_packet_count,
236             (long int)p_index->i_index_entry_count );
237 #endif
238
239     return VLC_SUCCESS;
240 }
241
242 static void ASF_FreeObject_Index( asf_object_t *p_obj )
243 {
244     asf_object_index_t *p_index = (asf_object_index_t*)p_obj;
245
246     FREE( p_index->index_entry );
247 }
248
249 static int ASF_ReadObject_file_properties( stream_t *s, asf_object_t *p_obj )
250 {
251     asf_object_file_properties_t *p_fp = (asf_object_file_properties_t*)p_obj;
252     int      i_peek;
253     uint8_t  *p_peek;
254
255     if( ( i_peek = stream_Peek( s, &p_peek,  92) ) < 92 )
256     {
257        return VLC_EGENERIC;
258     }
259     ASF_GetGUID( &p_fp->i_file_id, p_peek + 24 );
260     p_fp->i_file_size = GetQWLE( p_peek + 40 );
261     p_fp->i_creation_date = GetQWLE( p_peek + 48 );
262     p_fp->i_data_packets_count = GetQWLE( p_peek + 56 );
263     p_fp->i_play_duration = GetQWLE( p_peek + 64 );
264     p_fp->i_send_duration = GetQWLE( p_peek + 72 );
265     p_fp->i_preroll = GetQWLE( p_peek + 80 );
266     p_fp->i_flags = GetDWLE( p_peek + 88 );
267     p_fp->i_min_data_packet_size = GetDWLE( p_peek + 92 );
268     p_fp->i_max_data_packet_size = GetDWLE( p_peek + 96 );
269     p_fp->i_max_bitrate = GetDWLE( p_peek + 100 );
270
271 #ifdef ASF_DEBUG
272     msg_Dbg( s,
273             "read \"file properties object\" file_id:" GUID_FMT
274             " file_size:"I64Fd" creation_date:"I64Fd" data_packets_count:"
275             I64Fd" play_duration:"I64Fd" send_duration:"I64Fd" preroll:"
276             I64Fd" flags:%d min_data_packet_size:%d max_data_packet_size:%d "
277             "max_bitrate:%d",
278             GUID_PRINT( p_fp->i_file_id ), p_fp->i_file_size,
279             p_fp->i_creation_date, p_fp->i_data_packets_count,
280             p_fp->i_play_duration, p_fp->i_send_duration,
281             p_fp->i_preroll, p_fp->i_flags,
282             p_fp->i_min_data_packet_size, p_fp->i_max_data_packet_size,
283             p_fp->i_max_bitrate );
284 #endif
285
286     return VLC_SUCCESS;
287 }
288
289 static void ASF_FreeObject_metadata( asf_object_t *p_obj )
290 {
291     asf_object_metadata_t *p_meta =
292         (asf_object_metadata_t *)p_obj;
293     unsigned int i;
294
295     for( i = 0; i < p_meta->i_record_entries_count; i++ )
296     {
297         if( p_meta->record[i].psz_name ) free( p_meta->record[i].psz_name );
298         if( p_meta->record[i].p_data ) free( p_meta->record[i].p_data );
299     }
300     if( p_meta->record ) free( p_meta->record );
301 }
302
303 static int ASF_ReadObject_metadata( stream_t *s, asf_object_t *p_obj )
304 {
305     asf_object_metadata_t *p_meta =
306         (asf_object_metadata_t *)p_obj;
307
308     int i_peek, i_entries, i;
309     uint8_t *p_peek;
310 #ifdef ASF_DEBUG
311     unsigned int j;
312 #endif
313
314     p_meta->i_record_entries_count = 0;
315     p_meta->record = 0;
316
317     if( stream_Peek( s, &p_peek, p_meta->i_object_size ) <
318         (int)p_meta->i_object_size )
319     {
320        return VLC_EGENERIC;
321     }
322
323     i_peek = 24;
324     i_entries = GetWLE( p_peek + i_peek ); i_peek += 2;
325     for( i = 0; i < i_entries && i_peek < (int)p_meta->i_object_size -12; i++ )
326     {
327         asf_metadata_record_t record;
328         int i_name, i_data, j;
329
330         if( GetWLE( p_peek + i_peek ) != 0 )
331         {
332             ASF_FreeObject_metadata( p_obj );
333             return VLC_EGENERIC;
334         }
335
336         i_peek += 2;
337         record.i_stream = GetWLE( p_peek + i_peek ); i_peek += 2;
338         i_name = GetWLE( p_peek + i_peek ); i_peek += 2;
339         record.i_type = GetWLE( p_peek + i_peek ); i_peek += 2;
340         i_data = GetDWLE( p_peek + i_peek ); i_peek += 4;
341
342         if( record.i_type > ASF_METADATA_TYPE_WORD ||
343             i_peek + i_data + i_name > (int)p_meta->i_object_size )
344         {
345             ASF_FreeObject_metadata( p_obj );
346             return VLC_EGENERIC;
347         }
348
349         record.i_val = 0;
350         record.i_data = 0;
351         record.p_data = 0;
352
353         /* get name */
354         record.psz_name = malloc( i_name/2 + 1 );
355         for( j = 0; j < i_name/2; j++ )
356         {
357             record.psz_name[j] = GetWLE( p_peek + i_peek ); i_peek += 2;
358         }
359         record.psz_name[j] = 0; /* just to make sure */
360
361         /* get data */
362         if( record.i_type == ASF_METADATA_TYPE_STRING )
363         {
364             record.p_data = malloc( i_data/2 + 1 );
365             record.i_data = i_data/2;
366             for( j = 0; j < i_data/2; j++ )
367             {
368                 record.p_data[j] = GetWLE( p_peek + i_peek ); i_peek += 2;
369             }
370             record.p_data[j] = 0; /* just to make sure */
371         }
372         else if( record.i_type == ASF_METADATA_TYPE_BYTE )
373         {
374             record.p_data = malloc( i_data );
375             record.i_data = i_data;
376             memcpy( record.p_data, p_peek + i_peek, i_data );
377             p_peek += i_data;
378         }
379         else
380         {
381             if( record.i_type == ASF_METADATA_TYPE_QWORD )
382             {
383                 record.i_val = GetQWLE( p_peek + i_peek ); i_peek += 8;
384             }
385             else if( record.i_type == ASF_METADATA_TYPE_DWORD )
386             {
387                 record.i_val = GetDWLE( p_peek + i_peek ); i_peek += 4;
388             }
389             else
390             {
391                 record.i_val = GetWLE( p_peek + i_peek ); i_peek += 2;
392             }
393         }
394
395         p_meta->i_record_entries_count++;
396         p_meta->record =
397             realloc( p_meta->record, p_meta->i_record_entries_count *
398                      sizeof(asf_metadata_record_t) );
399         memcpy( &p_meta->record[p_meta->i_record_entries_count-1],
400                 &record, sizeof(asf_metadata_record_t) );
401     }
402
403 #ifdef ASF_DEBUG
404     msg_Dbg( s,
405             "read \"metadata object\" %d entries",
406             p_meta->i_record_entries_count );
407     for( j = 0; j < p_meta->i_record_entries_count; j++ )
408     {
409         asf_metadata_record_t *p_rec = &p_meta->record[j];
410
411         if( p_rec->i_type == ASF_METADATA_TYPE_STRING )
412             msg_Dbg( s, "  - %s=%s",
413                      p_rec->psz_name, p_rec->p_data );
414         else if( p_rec->i_type == ASF_METADATA_TYPE_BYTE )
415             msg_Dbg( s, "  - %s (%i bytes)",
416                      p_rec->psz_name, p_rec->i_data );
417         else
418             msg_Dbg( s, "  - %s="I64Fd,
419                      p_rec->psz_name, p_rec->i_val );
420     }
421 #endif
422
423     return VLC_SUCCESS;
424 }
425
426 static int ASF_ReadObject_header_extension( stream_t *s, asf_object_t *p_obj )
427 {
428     asf_object_header_extension_t *p_he =
429         (asf_object_header_extension_t *)p_obj;
430     int     i_peek;
431     uint8_t *p_peek;
432
433     if( ( i_peek = stream_Peek( s, &p_peek, p_he->i_object_size ) ) <  46)
434     {
435        return VLC_EGENERIC;
436     }
437     ASF_GetGUID( &p_he->i_reserved1, p_peek + 24 );
438     p_he->i_reserved2 = GetWLE( p_peek + 40 );
439     p_he->i_header_extension_size = GetDWLE( p_peek + 42 );
440     if( p_he->i_header_extension_size )
441     {
442         p_he->p_header_extension_data =
443             malloc( p_he->i_header_extension_size );
444         memcpy( p_he->p_header_extension_data, p_peek + 46,
445                 p_he->i_header_extension_size );
446     }
447     else
448     {
449         p_he->p_header_extension_data = NULL;
450     }
451
452 #ifdef ASF_DEBUG
453     msg_Dbg( s,
454             "read \"header extension object\" reserved1:" GUID_FMT
455             " reserved2:%d header_extension_size:%d",
456             GUID_PRINT( p_he->i_reserved1 ), p_he->i_reserved2,
457             p_he->i_header_extension_size );
458 #endif
459
460     if( !p_he->i_header_extension_size ) return VLC_SUCCESS;
461
462     /* Read the extension objects */
463     stream_Read( s, NULL, 46 );
464     for( ; ; )
465     {
466         asf_object_t *p_obj = malloc( sizeof( asf_object_t ) );
467
468         if( ASF_ReadObject( s, p_obj, (asf_object_t*)p_he ) )
469         {
470             free( p_obj );
471             break;
472         }
473
474         if( ASF_NextObject( s, p_obj ) ) /* Go to the next object */
475         {
476             break;
477         }
478     }
479
480     return VLC_SUCCESS;
481 }
482
483 static void ASF_FreeObject_header_extension( asf_object_t *p_obj )
484 {
485     asf_object_header_extension_t *p_he =
486         (asf_object_header_extension_t *)p_obj;
487
488     FREE( p_he->p_header_extension_data );
489 }
490
491 static int ASF_ReadObject_stream_properties( stream_t *s, asf_object_t *p_obj )
492 {
493     asf_object_stream_properties_t *p_sp =
494                     (asf_object_stream_properties_t*)p_obj;
495     int     i_peek;
496     uint8_t *p_peek;
497
498     if( ( i_peek = stream_Peek( s, &p_peek,  p_sp->i_object_size ) ) < 74 )
499     {
500        return VLC_EGENERIC;
501     }
502     ASF_GetGUID( &p_sp->i_stream_type, p_peek + 24 );
503     ASF_GetGUID( &p_sp->i_error_correction_type, p_peek + 40 );
504     p_sp->i_time_offset = GetQWLE( p_peek + 56 );
505     p_sp->i_type_specific_data_length = GetDWLE( p_peek + 64 );
506     p_sp->i_error_correction_data_length = GetDWLE( p_peek + 68 );
507     p_sp->i_flags = GetWLE( p_peek + 72 );
508         p_sp->i_stream_number = p_sp->i_flags&0x07f;
509     p_sp->i_reserved = GetDWLE( p_peek + 74 );
510     if( p_sp->i_type_specific_data_length )
511     {
512         p_sp->p_type_specific_data =
513             malloc( p_sp->i_type_specific_data_length );
514         memcpy( p_sp->p_type_specific_data, p_peek + 78,
515                 p_sp->i_type_specific_data_length );
516     }
517     else
518     {
519         p_sp->p_type_specific_data = NULL;
520     }
521     if( p_sp->i_error_correction_data_length )
522     {
523         p_sp->p_error_correction_data =
524             malloc( p_sp->i_error_correction_data_length );
525         memcpy( p_sp->p_error_correction_data,
526                 p_peek + 78 + p_sp->i_type_specific_data_length,
527                 p_sp->i_error_correction_data_length );
528     }
529     else
530     {
531         p_sp->p_error_correction_data = NULL;
532     }
533
534 #ifdef ASF_DEBUG
535     msg_Dbg( s,
536             "read \"stream Properties object\" stream_type:" GUID_FMT
537             " error_correction_type:" GUID_FMT " time_offset:"I64Fd
538             " type_specific_data_length:%d error_correction_data_length:%d"
539             " flags:0x%x stream_number:%d",
540             GUID_PRINT( p_sp->i_stream_type ),
541             GUID_PRINT( p_sp->i_error_correction_type ),
542             p_sp->i_time_offset,
543             p_sp->i_type_specific_data_length,
544             p_sp->i_error_correction_data_length,
545             p_sp->i_flags,
546             p_sp->i_stream_number );
547
548 #endif
549     return VLC_SUCCESS;
550 }
551
552 static void ASF_FreeObject_stream_properties( asf_object_t *p_obj )
553 {
554     asf_object_stream_properties_t *p_sp =
555                 (asf_object_stream_properties_t*)p_obj;
556
557     FREE( p_sp->p_type_specific_data );
558     FREE( p_sp->p_error_correction_data );
559 }
560
561
562 static int ASF_ReadObject_codec_list( stream_t *s, asf_object_t *p_obj )
563 {
564     asf_object_codec_list_t *p_cl = (asf_object_codec_list_t*)p_obj;
565     int     i_peek;
566     uint8_t *p_peek, *p_data;
567
568     unsigned int i_codec;
569
570     if( ( i_peek = stream_Peek( s, &p_peek, p_cl->i_object_size ) ) < 44 )
571     {
572        return VLC_EGENERIC;
573     }
574
575     ASF_GetGUID( &p_cl->i_reserved, p_peek + 24 );
576     p_cl->i_codec_entries_count = GetWLE( p_peek + 40 );
577     if( p_cl->i_codec_entries_count > 0 )
578     {
579         p_cl->codec = calloc( p_cl->i_codec_entries_count,
580                               sizeof( asf_codec_entry_t ) );
581         memset( p_cl->codec, 0,
582                 p_cl->i_codec_entries_count * sizeof( asf_codec_entry_t ) );
583
584         p_data = p_peek + 44;
585         for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
586         {
587 #define codec p_cl->codec[i_codec]
588             int i_len, i;
589
590             codec.i_type = GetWLE( p_data ); p_data += 2;
591             /* codec name */
592             i_len = GetWLE( p_data ); p_data += 2;
593             codec.psz_name = calloc( sizeof( char ), i_len + 1);
594             for( i = 0; i < i_len; i++ )
595             {
596                 codec.psz_name[i] = GetWLE( p_data + 2*i );
597             }
598             codec.psz_name[i_len] = '\0';
599             p_data += 2 * i_len;
600
601             /* description */
602             i_len = GetWLE( p_data ); p_data += 2;
603             codec.psz_description = calloc( sizeof( char ), i_len + 1);
604             for( i = 0; i < i_len; i++ )
605             {
606                 codec.psz_description[i] = GetWLE( p_data + 2*i );
607             }
608             codec.psz_description[i_len] = '\0';
609             p_data += 2 * i_len;
610
611             /* opaque information */
612             codec.i_information_length = GetWLE( p_data ); p_data += 2;
613             if( codec.i_information_length > 0 )
614             {
615                 codec.p_information = malloc( codec.i_information_length );
616                 memcpy( codec.p_information, p_data, codec.i_information_length );
617                 p_data += codec.i_information_length;
618             }
619             else
620             {
621                 codec.p_information = NULL;
622             }
623 #undef  codec
624         }
625     }
626     else
627     {
628         p_cl->codec = NULL;
629     }
630
631 #ifdef ASF_DEBUG
632     msg_Dbg( s, "read \"codec list object\" reserved_guid:" GUID_FMT
633              " codec_entries_count:%d",
634             GUID_PRINT( p_cl->i_reserved ), p_cl->i_codec_entries_count );
635
636     for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
637     {
638 #define codec p_cl->codec[i_codec]
639         msg_Dbg( s, "  - codec[%d] %s name:\"%s\" "
640                  "description:\"%s\" information_length:%d",
641                  i_codec, ( codec.i_type == ASF_CODEC_TYPE_VIDEO ) ?
642                  "video" : ( ( codec.i_type == ASF_CODEC_TYPE_AUDIO ) ?
643                  "audio" : "unknown" ),
644                  codec.psz_name, codec.psz_description,
645                  codec.i_information_length );
646 #undef  codec
647     }
648 #endif
649
650     return VLC_SUCCESS;
651 }
652
653 static void ASF_FreeObject_codec_list( asf_object_t *p_obj )
654 {
655     asf_object_codec_list_t *p_cl = (asf_object_codec_list_t*)p_obj;
656     unsigned int i_codec;
657
658     for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
659     {
660 #define codec p_cl->codec[i_codec]
661         FREE( codec.psz_name );
662         FREE( codec.psz_description );
663         FREE( codec.p_information );
664 #undef  codec
665     }
666     FREE( p_cl->codec );
667 }
668
669 /* Microsoft should go to hell. This time the length give number of bytes
670  * and for the some others object, length give char16 count ... */
671 static int ASF_ReadObject_content_description(stream_t *s, asf_object_t *p_obj)
672 {
673     asf_object_content_description_t *p_cd =
674         (asf_object_content_description_t *)p_obj;
675     uint8_t *p_peek, *p_data;
676     int i_peek, i_title, i_author, i_copyright, i_description, i_rating;
677     vlc_iconv_t cd = (vlc_iconv_t)-1;
678     char *ib = NULL;
679     char *ob = NULL;
680     size_t i_ibl, i_obl, i_len;
681
682     cd = vlc_iconv_open("UTF-8", "UTF-16LE");
683     if ( cd == (vlc_iconv_t)-1 ) {
684         msg_Err( s, "vlc_iconv_open failed" );
685         return VLC_EGENERIC;
686     }
687
688 /* FIXME i_size*3 is the worst case. */
689 #define GETSTRINGW( psz_str, i_size ) \
690     psz_str = (char *)calloc( i_size*3+1, sizeof( char ) ); \
691     ib = (char *)p_data; \
692     ob = psz_str; \
693     i_ibl = i_size; \
694     i_obl = i_size*3; \
695     i_len = vlc_iconv(cd, &ib, &i_ibl, &ob, &i_obl); \
696     p_data += i_size;
697
698     if( ( i_peek = stream_Peek( s, &p_peek, p_cd->i_object_size ) ) < 34 )
699     {
700        return VLC_EGENERIC;
701     }
702     p_data = p_peek + 24;
703
704     i_title = GetWLE( p_data ); p_data += 2;
705     i_author= GetWLE( p_data ); p_data += 2;
706     i_copyright     = GetWLE( p_data ); p_data += 2;
707     i_description   = GetWLE( p_data ); p_data += 2;
708     i_rating        = GetWLE( p_data ); p_data += 2;
709
710     GETSTRINGW( p_cd->psz_title, i_title );
711     GETSTRINGW( p_cd->psz_author, i_author );
712     GETSTRINGW( p_cd->psz_copyright, i_copyright );
713     GETSTRINGW( p_cd->psz_description, i_description );
714     GETSTRINGW( p_cd->psz_rating, i_rating );
715
716 #undef  GETSTRINGW
717
718 #ifdef ASF_DEBUG
719     msg_Dbg( s,
720              "read \"content description object\" title:\"%s\" author:\"%s\" copyright:\"%s\" description:\"%s\" rating:\"%s\"",
721              p_cd->psz_title,
722              p_cd->psz_author,
723              p_cd->psz_copyright,
724              p_cd->psz_description,
725              p_cd->psz_rating );
726 #endif
727
728     vlc_iconv_close(cd);
729     return VLC_SUCCESS;
730 }
731
732 static void ASF_FreeObject_content_description( asf_object_t *p_obj)
733 {
734     asf_object_content_description_t *p_cd =
735         (asf_object_content_description_t *)p_obj;
736
737     FREE( p_cd->psz_title );
738     FREE( p_cd->psz_author );
739     FREE( p_cd->psz_copyright );
740     FREE( p_cd->psz_description );
741     FREE( p_cd->psz_rating );
742 }
743
744 /* Language list: */
745 static int ASF_ReadObject_language_list(stream_t *s, asf_object_t *p_obj)
746 {
747     asf_object_language_list_t *p_ll =
748         (asf_object_language_list_t*)p_obj;
749     uint8_t *p_peek, *p_data;
750     int i_peek;
751     int i;
752
753     if( ( i_peek = stream_Peek( s, &p_peek, p_ll->i_object_size ) ) < 26 )
754        return VLC_EGENERIC;
755
756     p_data = &p_peek[24];
757
758     p_ll->i_language = GetWLE( &p_data[0] ); p_data += 2;
759     if( p_ll->i_language > 0 )
760     {
761         p_ll->ppsz_language = calloc( p_ll->i_language, sizeof( char *) );
762
763         for( i = 0; i < p_ll->i_language; i++ )
764         {
765             char *psz;
766             int i_size = *p_data++;
767             int i_len;
768
769             psz = calloc( i_size/2 + 1, sizeof( char ) );
770             for( i_len = 0; i_len < i_size/2; i_len++ )
771             {
772                 psz[i_len] = GetWLE( p_data + 2*i_len );
773             }
774             psz[i_size/2] = '\0'; \
775             p_data += i_size;
776
777             p_ll->ppsz_language[i] = psz;
778         }
779     }
780
781 #ifdef ASF_DEBUG
782     msg_Dbg( s, "read \"language list object\" %d entries", 
783              p_ll->i_language );
784     for( i = 0; i < p_ll->i_language; i++ )
785         msg_Dbg( s, "  - '%s'", 
786                  p_ll->ppsz_language[i] );
787 #endif
788     return VLC_SUCCESS;
789 }
790
791 static void ASF_FreeObject_language_list( asf_object_t *p_obj)
792 {
793     asf_object_language_list_t *p_ll =
794         (asf_object_language_list_t *)p_obj;
795     int i;
796
797     for( i = 0; i < p_ll->i_language; i++ )
798         FREE( p_ll->ppsz_language[i] );
799     FREE( p_ll->ppsz_language );
800 }
801
802 /* Stream bitrate properties */
803 static int ASF_ReadObject_stream_bitrate_properties( stream_t *s,
804                                                      asf_object_t *p_obj)
805 {
806     asf_object_stream_bitrate_properties_t *p_sb =
807         (asf_object_stream_bitrate_properties_t *)p_obj;
808     uint8_t *p_peek, *p_data;
809     int i_peek;
810     int i;
811
812     if( ( i_peek = stream_Peek( s, &p_peek, p_sb->i_object_size ) ) < 26 )
813        return VLC_EGENERIC;
814
815     p_data = &p_peek[24];
816
817     p_sb->i_bitrate = GetWLE( &p_data[0] ); p_data += 2;
818     if( p_sb->i_bitrate > 127 ) p_sb->i_bitrate = 127;  /* Buggy ? */
819     for( i = 0; i < p_sb->i_bitrate; i++ )
820     {
821         p_sb->bitrate[i].i_stream_number = GetWLE( &p_data[0] )& 0x7f;
822         p_sb->bitrate[i].i_avg_bitrate = GetDWLE( &p_data[2] );
823
824         p_data += 2+4;
825     }
826
827 #ifdef ASF_DEBUG
828     msg_Dbg( s,"read \"stream bitrate properties object\"" );
829     for( i = 0; i < p_sb->i_bitrate; i++ )
830     {
831         msg_Dbg( s,"  - stream=%d bitrate=%d",
832                  p_sb->bitrate[i].i_stream_number,
833                  p_sb->bitrate[i].i_avg_bitrate ); 
834     }
835 #endif
836     return VLC_SUCCESS;
837 }
838 static void ASF_FreeObject_stream_bitrate_properties( asf_object_t *p_obj)
839 {
840 }
841
842 static int ASF_ReadObject_extended_stream_properties( stream_t *s,
843                                                       asf_object_t *p_obj)
844 {
845     asf_object_extended_stream_properties_t *p_esp =
846         (asf_object_extended_stream_properties_t*)p_obj;
847     uint8_t *p_peek, *p_data;
848     int i_peek, i;
849
850     if( ( i_peek = stream_Peek( s, &p_peek, p_esp->i_object_size ) ) < 88 )
851        return VLC_EGENERIC;
852
853     p_data = &p_peek[24];
854
855     p_esp->i_start_time = GetQWLE( &p_data[0] );
856     p_esp->i_end_time = GetQWLE( &p_data[8] );
857     p_esp->i_data_bitrate = GetDWLE( &p_data[16] );
858     p_esp->i_buffer_size = GetDWLE( &p_data[20] );
859     p_esp->i_initial_buffer_fullness = GetDWLE( &p_data[24] );
860     p_esp->i_alternate_data_bitrate = GetDWLE( &p_data[28] );
861     p_esp->i_alternate_buffer_size = GetDWLE( &p_data[32] );
862     p_esp->i_alternate_initial_buffer_fullness = GetDWLE( &p_data[36] );
863     p_esp->i_maximum_object_size = GetDWLE( &p_data[40] );
864     p_esp->i_flags = GetDWLE( &p_data[44] );
865     p_esp->i_stream_number = GetWLE( &p_data[48] );
866     p_esp->i_language_index = GetWLE( &p_data[50] );
867     p_esp->i_average_time_per_frame= GetQWLE( &p_data[52] );
868     p_esp->i_stream_name_count = GetWLE( &p_data[60] );
869     p_esp->i_payload_extention_system_count = GetWLE( &p_data[62] );
870
871     p_data += 64;
872
873     p_esp->pi_stream_name_language = calloc( sizeof(int),
874                                              p_esp->i_stream_name_count );
875     p_esp->ppsz_stream_name = calloc( sizeof(char*),
876                                       p_esp->i_stream_name_count );
877     for( i = 0; i < p_esp->i_stream_name_count; i++ )
878     {
879         int i_size;
880         char *psz;
881         int i_len;
882
883         p_esp->pi_stream_name_language[i] = GetWLE( &p_data[0] );
884         i_size = GetWLE( &p_data[2] );
885         p_data += 2;
886  
887         psz = calloc( i_size/2 + 1, sizeof( char ) );
888         for( i_len = 0; i_len < i_size/2; i_len++ )
889         {
890             psz[i_len] = GetWLE( p_data + 2*i_len );
891         }
892         psz[i_size/2] = '\0'; \
893         p_data += i_size;
894
895         p_esp->ppsz_stream_name[i] = psz;
896     }
897
898     for( i = 0; i < p_esp->i_payload_extention_system_count; i++ )
899     {
900         /* Skip them */
901         int i_size = GetDWLE( &p_data[16 + 2] );
902
903         p_data += 16+2+4+i_size;
904     }
905
906     p_esp->p_sp = NULL;
907     if( p_data < &p_peek[i_peek] )
908     {
909         asf_object_t *p_sp;
910         /* Cannot fail as peek succeed */
911         stream_Read( s, NULL, p_data - p_peek );
912         
913         p_sp = malloc( sizeof( asf_object_t ) );
914
915         if( ASF_ReadObject( s, p_sp, NULL ) )
916         {
917             free( p_sp );
918         }
919         else
920         {
921             /* This p_sp will be inserted by ReadRoot later */
922             p_esp->p_sp = (asf_object_stream_properties_t*)p_sp;
923         }
924     }
925
926 #ifdef ASF_DEBUG
927     msg_Dbg( s, "read \"extended stream properties object\":" );
928     msg_Dbg( s, "  - start="I64Fd" end="I64Fd,
929              p_esp->i_start_time, p_esp->i_end_time );
930     msg_Dbg( s, "  - data bitrate=%d buffer=%d initial fullness=%d",
931              p_esp->i_data_bitrate,
932              p_esp->i_buffer_size,
933              p_esp->i_initial_buffer_fullness );
934     msg_Dbg( s, "  - alternate data bitrate=%d buffer=%d initial fullness=%d",
935              p_esp->i_alternate_data_bitrate,
936              p_esp->i_alternate_buffer_size,
937              p_esp->i_alternate_initial_buffer_fullness );
938     msg_Dbg( s, "  - maximum object size=%d", p_esp->i_maximum_object_size );
939     msg_Dbg( s, "  - flags=0x%x", p_esp->i_flags );
940     msg_Dbg( s, "  - stream number=%d language=%d",
941              p_esp->i_stream_number, p_esp->i_language_index );
942     msg_Dbg( s, "  - average time per frame="I64Fd,
943              p_esp->i_average_time_per_frame );
944     msg_Dbg( s, "  - stream name count=%d", p_esp->i_stream_name_count );
945     for( i = 0; i < p_esp->i_stream_name_count; i++ )
946         msg_Dbg( s, "     - lang id=%d name=%s",
947                  p_esp->pi_stream_name_language[i],
948                  p_esp->ppsz_stream_name[i] );
949     msg_Dbg( s, "  - payload extention system count=%d",
950              p_esp->i_payload_extention_system_count );
951 #endif
952     return VLC_SUCCESS;
953 }
954 static void ASF_FreeObject_extended_stream_properties( asf_object_t *p_obj)
955 {
956     asf_object_extended_stream_properties_t *p_esp =
957         (asf_object_extended_stream_properties_t *)p_obj;
958     int i;
959
960     for( i = 0; i < p_esp->i_stream_name_count; i++ )
961         FREE( p_esp->ppsz_stream_name[i] );
962     FREE( p_esp->pi_stream_name_language );
963     FREE( p_esp->ppsz_stream_name );
964 }
965
966
967 static int ASF_ReadObject_advanced_mutual_exclusion( stream_t *s,
968                                                      asf_object_t *p_obj)
969 {
970     asf_object_advanced_mutual_exclusion_t *p_ae =
971         (asf_object_advanced_mutual_exclusion_t *)p_obj;
972     uint8_t *p_peek, *p_data;
973     int i_peek;
974     int i;
975
976     if( ( i_peek = stream_Peek( s, &p_peek, p_ae->i_object_size ) ) < 42 )
977        return VLC_EGENERIC;
978
979     p_data = &p_peek[24];
980
981     ASF_GetGUID( &p_ae->type, &p_data[0] );
982     p_ae->i_stream_number_count = GetWLE( &p_data[16] );
983
984     p_data += 16 + 2;
985     p_ae->pi_stream_number = calloc( sizeof(int),
986                                      p_ae->i_stream_number_count );
987     for( i = 0; i < p_ae->i_stream_number_count; i++ )
988     {
989         p_ae->pi_stream_number[i] = GetWLE( p_data );
990         p_data += 2;
991     }
992         
993 #ifdef ASF_DEBUG
994     msg_Dbg( s, "read \"advanced mutual exclusion object\"" );
995     for( i = 0; i < p_ae->i_stream_number_count; i++ )
996         msg_Dbg( s, "  - stream=%d", p_ae->pi_stream_number[i] );
997 #endif
998     return VLC_SUCCESS;
999 }
1000 static void ASF_FreeObject_advanced_mutual_exclusion( asf_object_t *p_obj)
1001 {
1002     asf_object_advanced_mutual_exclusion_t *p_ae =
1003         (asf_object_advanced_mutual_exclusion_t *)p_obj;
1004
1005     FREE( p_ae->pi_stream_number );
1006 }
1007
1008
1009 static int ASF_ReadObject_stream_prioritization( stream_t *s,
1010                                                  asf_object_t *p_obj)
1011 {
1012     asf_object_stream_prioritization_t *p_sp =
1013         (asf_object_stream_prioritization_t *)p_obj;
1014     uint8_t *p_peek, *p_data;
1015     int i_peek;
1016     int i;
1017
1018     if( ( i_peek = stream_Peek( s, &p_peek, p_sp->i_object_size ) ) < 26 )
1019        return VLC_EGENERIC;
1020
1021     p_data = &p_peek[24];
1022
1023     p_sp->i_priority_count = GetWLE( &p_data[0] );
1024     p_data += 2;
1025
1026     p_sp->pi_priority_flag = calloc( sizeof(int), p_sp->i_priority_count );
1027     p_sp->pi_priority_stream_number =
1028                              calloc( sizeof(int), p_sp->i_priority_count );
1029
1030     for( i = 0; i < p_sp->i_priority_count; i++ )
1031     {
1032         p_sp->pi_priority_stream_number[i] = GetWLE( p_data ); p_data += 2;
1033         p_sp->pi_priority_flag[i] = GetWLE( p_data ); p_data += 2;
1034     }
1035 #ifdef ASF_DEBUG
1036     msg_Dbg( s, "read \"stream prioritization object\"" );
1037     for( i = 0; i < p_sp->i_priority_count; i++ )
1038         msg_Dbg( s, "  - Stream:%d flags=0x%x",
1039                  p_sp->pi_priority_stream_number[i],
1040                  p_sp->pi_priority_flag[i] );
1041 #endif
1042     return VLC_SUCCESS;
1043 }
1044 static void ASF_FreeObject_stream_prioritization( asf_object_t *p_obj)
1045 {
1046     asf_object_stream_prioritization_t *p_sp =
1047         (asf_object_stream_prioritization_t *)p_obj;
1048
1049     FREE( p_sp->pi_priority_stream_number );
1050     FREE( p_sp->pi_priority_flag );
1051 }
1052
1053
1054 static int ASF_ReadObject_extended_content_description( stream_t *s,
1055                                                         asf_object_t *p_obj)
1056 {
1057     asf_object_extended_content_description_t *p_ec =
1058         (asf_object_extended_content_description_t *)p_obj;
1059     uint8_t *p_peek, *p_data;
1060     int i_peek;
1061     int i;
1062
1063     if( ( i_peek = stream_Peek( s, &p_peek, p_ec->i_object_size ) ) < 26 )
1064        return VLC_EGENERIC;
1065
1066     p_data = &p_peek[24];
1067
1068     p_ec->i_count = GetWLE( p_data ); p_data += 2;
1069     p_ec->ppsz_name = calloc( sizeof(char*), p_ec->i_count );
1070     p_ec->ppsz_value = calloc( sizeof(char*), p_ec->i_count );
1071     for( i = 0; i < p_ec->i_count; i++ )
1072     {
1073         int i_size;
1074         int i_type;
1075         int i_len;
1076 #define GETSTRINGW( psz_str, i_size ) \
1077        psz_str = calloc( i_size/2 + 1, sizeof( char ) ); \
1078        for( i_len = 0; i_len < i_size/2; i_len++ ) \
1079        { \
1080            psz_str[i_len] = GetWLE( p_data + 2*i_len ); \
1081        } \
1082        psz_str[i_size/2] = '\0';
1083
1084         i_size = GetWLE( p_data ); p_data += 2;
1085         GETSTRINGW( p_ec->ppsz_name[i], i_size );
1086         p_data += i_size;
1087
1088         /* Grrr */
1089         i_type = GetWLE( p_data ); p_data += 2;
1090         i_size = GetWLE( p_data ); p_data += 2;
1091         if( i_type == 0 )
1092         {
1093             GETSTRINGW( p_ec->ppsz_value[i], i_size );
1094         }
1095         else if( i_type == 1 )
1096         {
1097             int j;
1098             /* Byte array */
1099             p_ec->ppsz_value[i] = malloc( 2*i_size + 1 );
1100             for( j = 0; j < i_size; j++ )
1101             {
1102                 static const char hex[16] = "0123456789ABCDEF";
1103                 p_ec->ppsz_value[i][2*j+0] = hex[p_data[0]>>4];
1104                 p_ec->ppsz_value[i][2*j+1] = hex[p_data[0]&0xf];
1105             }
1106             p_ec->ppsz_value[i][2*i_size] = '\0';
1107         }
1108         else if( i_type == 2 )
1109         {
1110             /* Bool */
1111             p_ec->ppsz_value[i] = strdup( *p_data ? "true" : "false" );
1112         }
1113         else if( i_type == 3 )
1114         {
1115             /* DWord */
1116             asprintf( &p_ec->ppsz_value[i], "%d", GetDWLE(p_data));
1117         }
1118         else if( i_type == 4 )
1119         {
1120             /* QWord */
1121             asprintf( &p_ec->ppsz_value[i], I64Fd, GetQWLE(p_data));
1122         }
1123         else if( i_type == 5 )
1124         {
1125             /* Word */
1126             asprintf( &p_ec->ppsz_value[i], "%d", GetWLE(p_data));
1127         }
1128         else
1129             p_ec->ppsz_value[i] = NULL;
1130
1131         p_data += i_size;
1132         
1133
1134
1135 #undef GETSTRINGW
1136
1137     }
1138
1139 #ifdef ASF_DEBUG
1140     msg_Dbg( s, "read \"extended content description object\"" );
1141     for( i = 0; i < p_ec->i_count; i++ )
1142         msg_Dbg( s, "  - '%s' = '%s'",
1143                  p_ec->ppsz_name[i],
1144                  p_ec->ppsz_value[i] );
1145 #endif
1146     return VLC_SUCCESS;
1147 }
1148 static void ASF_FreeObject_extended_content_description( asf_object_t *p_obj)
1149 {
1150     asf_object_extended_content_description_t *p_ec =
1151         (asf_object_extended_content_description_t *)p_obj;
1152     int i;
1153
1154     for( i = 0; i < p_ec->i_count; i++ )
1155     {
1156         FREE( p_ec->ppsz_name[i] );
1157         FREE( p_ec->ppsz_value[i] );
1158     }
1159 }
1160
1161
1162 #if 0
1163 static int ASF_ReadObject_XXX(stream_t *s, asf_object_t *p_obj)
1164 {
1165     asf_object_XXX_t *p_XX =
1166         (asf_object_XXX_t *)p_obj;
1167     uint8_t *p_peek, *p_data;
1168     int i_peek;
1169
1170     if( ( i_peek = stream_Peek( s, &p_peek, p_XX->i_object_size ) ) < XXX )
1171        return VLC_EGENERIC;
1172
1173     p_data = &p_peek[24];
1174
1175 #ifdef ASF_DEBUG
1176     msg_Dbg( s,
1177              "Read \"XXX object\"" );
1178 #endif
1179     return VLC_SUCCESS;
1180 }
1181 static void ASF_FreeObject_XXX( asf_object_t *p_obj)
1182 {
1183     asf_object_XXX_t *p_XX =
1184         (asf_object_XXX_t *)p_obj;
1185 }
1186 #endif
1187
1188
1189 /* */
1190 static struct
1191 {
1192     const guid_t  *p_id;
1193     int     i_type;
1194     int     (*ASF_ReadObject_function)( stream_t *, asf_object_t *p_obj );
1195     void    (*ASF_FreeObject_function)( asf_object_t *p_obj );
1196
1197 } ASF_Object_Function [] =
1198 {
1199     { &asf_object_header_guid, ASF_OBJECT_HEADER,
1200       ASF_ReadObject_Header, ASF_FreeObject_Null },
1201     { &asf_object_data_guid, ASF_OBJECT_DATA,
1202       ASF_ReadObject_Data, ASF_FreeObject_Null },
1203     { &asf_object_index_guid, ASF_OBJECT_INDEX,
1204       ASF_ReadObject_Index, ASF_FreeObject_Index },
1205     { &asf_object_file_properties_guid, ASF_OBJECT_FILE_PROPERTIES,
1206       ASF_ReadObject_file_properties, ASF_FreeObject_Null },
1207     { &asf_object_stream_properties_guid, ASF_OBJECT_STREAM_PROPERTIES,
1208       ASF_ReadObject_stream_properties,ASF_FreeObject_stream_properties },
1209     { &asf_object_header_extension_guid, ASF_OBJECT_HEADER_EXTENSION,
1210       ASF_ReadObject_header_extension, ASF_FreeObject_header_extension},
1211     { &asf_object_metadata_guid, ASF_OBJECT_METADATA,
1212       ASF_ReadObject_metadata, ASF_FreeObject_metadata},
1213     { &asf_object_codec_list_guid, ASF_OBJECT_CODEC_LIST,
1214       ASF_ReadObject_codec_list, ASF_FreeObject_codec_list },
1215     { &asf_object_marker_guid, ASF_OBJECT_MARKER, NULL, NULL },
1216     { &asf_object_padding, ASF_OBJECT_PADDING, NULL, NULL },
1217     { &asf_object_content_description_guid, ASF_OBJECT_CONTENT_DESCRIPTION,
1218       ASF_ReadObject_content_description, ASF_FreeObject_content_description },
1219     { &asf_object_language_list, ASF_OBJECT_OTHER,
1220       ASF_ReadObject_language_list, ASF_FreeObject_language_list },
1221     { &asf_object_stream_bitrate_properties, ASF_OBJECT_OTHER,
1222       ASF_ReadObject_stream_bitrate_properties,
1223       ASF_FreeObject_stream_bitrate_properties },
1224     { &asf_object_extended_stream_properties, ASF_OBJECT_OTHER,
1225       ASF_ReadObject_extended_stream_properties,
1226       ASF_FreeObject_extended_stream_properties },
1227     { &asf_object_advanced_mutual_exclusion, ASF_OBJECT_OTHER,
1228       ASF_ReadObject_advanced_mutual_exclusion,
1229       ASF_FreeObject_advanced_mutual_exclusion },
1230     { &asf_object_stream_prioritization, ASF_OBJECT_OTHER,
1231       ASF_ReadObject_stream_prioritization,
1232       ASF_FreeObject_stream_prioritization },
1233     { &asf_object_extended_content_description, ASF_OBJECT_OTHER,
1234       ASF_ReadObject_extended_content_description,
1235       ASF_FreeObject_extended_content_description },
1236
1237     { &asf_object_null_guid, 0, NULL, NULL }
1238 };
1239
1240 static int ASF_ReadObject( stream_t *s, asf_object_t *p_obj,
1241                            asf_object_t *p_father )
1242 {
1243     int i_result;
1244     int i_index;
1245
1246     if( !p_obj )
1247         return( 0 );
1248
1249     memset( p_obj, 0, sizeof( p_obj ) );
1250
1251     if( ASF_ReadObjectCommon( s, p_obj ) )
1252     {
1253         msg_Warn( s, "cannot read one asf object" );
1254         return VLC_EGENERIC;
1255     }
1256     p_obj->common.p_father = p_father;
1257     p_obj->common.p_first = NULL;
1258     p_obj->common.p_next = NULL;
1259     p_obj->common.p_last = NULL;
1260
1261     if( p_obj->common.i_object_size < 24 )
1262     {
1263         msg_Warn( s, "found a corrupted asf object (size<24)" );
1264         return VLC_EGENERIC;
1265     }
1266
1267     /* find this object */
1268     for( i_index = 0; ; i_index++ )
1269     {
1270         if( ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1271                          &p_obj->common.i_object_id ) ||
1272             ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1273                          &asf_object_null_guid ) )
1274         {
1275             break;
1276         }
1277     }
1278     p_obj->common.i_type = ASF_Object_Function[i_index].i_type;
1279
1280     /* Now load this object */
1281     if( ASF_Object_Function[i_index].ASF_ReadObject_function == NULL )
1282     {
1283         msg_Warn( s, "unknown asf object (not loaded)" );
1284         i_result = VLC_SUCCESS;
1285     }
1286     else
1287     {
1288         /* XXX ASF_ReadObject_function realloc *pp_obj XXX */
1289         i_result =
1290           (ASF_Object_Function[i_index].ASF_ReadObject_function)( s, p_obj );
1291     }
1292
1293     /* link this object with father */
1294     if( p_father )
1295     {
1296         if( p_father->common.p_first )
1297         {
1298             p_father->common.p_last->common.p_next = p_obj;
1299         }
1300         else
1301         {
1302             p_father->common.p_first = p_obj;
1303         }
1304         p_father->common.p_last = p_obj;
1305     }
1306
1307     return( i_result );
1308 }
1309
1310 static void ASF_FreeObject( stream_t *s, asf_object_t *p_obj )
1311 {
1312     int i_index;
1313     asf_object_t *p_child;
1314
1315     if( !p_obj ) return;
1316
1317     /* Free all child object */
1318     p_child = p_obj->common.p_first;
1319     while( p_child )
1320     {
1321         asf_object_t *p_next;
1322         p_next = p_child->common.p_next;
1323         ASF_FreeObject( s, p_child );
1324         p_child = p_next;
1325     }
1326
1327     /* find this object */
1328     for( i_index = 0; ; i_index++ )
1329     {
1330         if( ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1331                      &p_obj->common.i_object_id )||
1332             ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1333                      &asf_object_null_guid ) )
1334         {
1335             break;
1336         }
1337     }
1338
1339     /* Now free this object */
1340     if( ASF_Object_Function[i_index].ASF_FreeObject_function == NULL )
1341     {
1342         msg_Warn( s,
1343                   "unknown asf object " GUID_FMT,
1344                   GUID_PRINT( p_obj->common.i_object_id ) );
1345     }
1346     else
1347     {
1348 #ifdef ASF_DEBUG
1349         msg_Dbg( s,
1350                   "free asf object " GUID_FMT,
1351                   GUID_PRINT( p_obj->common.i_object_id ) );
1352 #endif
1353         (ASF_Object_Function[i_index].ASF_FreeObject_function)( p_obj );
1354     }
1355     free( p_obj );
1356     return;
1357 }
1358
1359 /*****************************************************************************
1360  * ASF_ObjectDumpDebug:
1361  *****************************************************************************/
1362 static const struct
1363 {
1364     const guid_t *p_id;
1365     char *psz_name;
1366 } ASF_ObjectDumpDebugInfo[] =
1367 {
1368     { &asf_object_header_guid, "Header" },
1369     { &asf_object_data_guid, "Data" },
1370     { &asf_object_index_guid, "Index" },
1371     { &asf_object_file_properties_guid, "File Properties" },
1372     { &asf_object_stream_properties_guid, "Stream Properties" },
1373     { &asf_object_content_description_guid, "Content Description" },
1374     { &asf_object_header_extension_guid, "Header Extention" },
1375     { &asf_object_metadata_guid, "Metadata" },
1376     { &asf_object_codec_list_guid, "Codec List" },
1377     { &asf_object_marker_guid, "Marker" },
1378     { &asf_object_stream_type_audio, "Stream Type Audio" },
1379     { &asf_object_stream_type_video, "Stream Type Video" },
1380     { &asf_object_stream_type_command, "Stream Type Command" },
1381     { &asf_object_language_list, "Language List" },
1382     { &asf_object_stream_bitrate_properties, "Stream Bitrate Propoerties" },
1383     { &asf_object_padding, "Padding" },
1384     { &asf_object_extended_stream_properties, "Extended Stream Properties" },
1385     { &asf_object_advanced_mutual_exclusion, "Advanced Mutual Exclusion" },
1386     { &asf_object_stream_prioritization, "Stream Prioritization" },
1387     { &asf_object_extended_content_description, "Extended content description"},
1388
1389     { NULL, "Unknown" },
1390 };
1391
1392
1393 static void ASF_ObjectDumpDebug( vlc_object_t *p_obj,
1394                                  asf_object_common_t *p_node, int i_level )
1395 {
1396     char str[1024];
1397     int i;
1398     union asf_object_u *p_child;
1399     char *psz_name;
1400
1401     /* Find the name */
1402     for( i = 0; ASF_ObjectDumpDebugInfo[i].p_id != NULL; i++ )
1403     {
1404         if( ASF_CmpGUID( ASF_ObjectDumpDebugInfo[i].p_id,
1405                           &p_node->i_object_id ) )
1406             break;
1407     }
1408     psz_name = ASF_ObjectDumpDebugInfo[i].psz_name;
1409
1410     memset( str, ' ', sizeof( str ) );
1411     for( i = 1; i < i_level; i++ )
1412     {
1413         str[i * 5] = '|';
1414     }
1415     snprintf( str + 5*i_level, 1024,
1416              "+ '%s' GUID "GUID_FMT" size:"I64Fu"pos:"I64Fu,
1417              psz_name,
1418              GUID_PRINT( p_node->i_object_id ),
1419              p_node->i_object_size, p_node->i_object_pos );
1420
1421     msg_Dbg( p_obj, "%s", str );
1422
1423     for( p_child = p_node->p_first; p_child != NULL;
1424                                              p_child = p_child->common.p_next )
1425     {
1426         ASF_ObjectDumpDebug( p_obj, &p_child->common, i_level + 1 );
1427     }
1428 }
1429
1430 /*****************************************************************************
1431  * ASF_ReadObjetRoot : parse the entire stream/file
1432  *****************************************************************************/
1433 asf_object_root_t *ASF_ReadObjectRoot( stream_t *s, int b_seekable )
1434 {
1435     asf_object_root_t *p_root = malloc( sizeof( asf_object_root_t ) );
1436     asf_object_t *p_obj;
1437
1438     p_root->i_type = ASF_OBJECT_ROOT;
1439     memcpy( &p_root->i_object_id, &asf_object_null_guid, sizeof( guid_t ) );
1440     p_root->i_object_pos = stream_Tell( s );
1441     p_root->i_object_size = 0;
1442     p_root->p_first = NULL;
1443     p_root->p_last  = NULL;
1444     p_root->p_next  = NULL;
1445     p_root->p_hdr   = NULL;
1446     p_root->p_data  = NULL;
1447     p_root->p_fp    = NULL;
1448     p_root->p_index = NULL;
1449     p_root->p_metadata = NULL;
1450
1451     for( ; ; )
1452     {
1453         p_obj = malloc( sizeof( asf_object_t ) );
1454
1455         if( ASF_ReadObject( s, p_obj, (asf_object_t*)p_root ) )
1456         {
1457             free( p_obj );
1458             break;
1459         }
1460         switch( p_obj->common.i_type )
1461         {
1462             case( ASF_OBJECT_HEADER ):
1463                 p_root->p_hdr = (asf_object_header_t*)p_obj;
1464                 break;
1465             case( ASF_OBJECT_DATA ):
1466                 p_root->p_data = (asf_object_data_t*)p_obj;
1467                 break;
1468             case( ASF_OBJECT_INDEX ):
1469                 p_root->p_index = (asf_object_index_t*)p_obj;
1470                 break;
1471             default:
1472                 msg_Warn( s, "unknow object found" );
1473                 break;
1474         }
1475         if( p_obj->common.i_type == ASF_OBJECT_DATA &&
1476             p_obj->common.i_object_size <= 50 )
1477         {
1478             /* probably a dump of broadcasted asf */
1479             break;
1480         }
1481         if( !b_seekable && p_root->p_hdr && p_root->p_data )
1482         {
1483             /* For unseekable stream it's enough to play */
1484             break;
1485         }
1486
1487         if( ASF_NextObject( s, p_obj ) ) /* Go to the next object */
1488         {
1489             break;
1490         }
1491     }
1492
1493     if( p_root->p_hdr != NULL && p_root->p_data != NULL )
1494     {
1495         p_root->p_fp = ASF_FindObject( p_root->p_hdr,
1496                                        &asf_object_file_properties_guid, 0 );
1497
1498         if( p_root->p_fp )
1499         {
1500             asf_object_t *p_hdr_ext =
1501                 ASF_FindObject( p_root->p_hdr,
1502                                 &asf_object_header_extension_guid, 0 );
1503             if( p_hdr_ext )
1504             {
1505                 int i_ext_stream;
1506                 int i;
1507
1508                 p_root->p_metadata =
1509                     ASF_FindObject( p_hdr_ext,
1510                                     &asf_object_metadata_guid, 0 );
1511                 /* Special case for broken designed file format :( */
1512                 i_ext_stream = ASF_CountObject( p_hdr_ext,
1513                                     &asf_object_extended_stream_properties );
1514                 for( i = 0; i < i_ext_stream; i++ )
1515                 {
1516                     asf_object_t *p_esp =
1517                         ASF_FindObject( p_hdr_ext,
1518                                    &asf_object_extended_stream_properties, i );
1519                     if( p_esp->ext_stream.p_sp )
1520                     {
1521                         asf_object_t *p_sp =
1522                                          (asf_object_t*)p_esp->ext_stream.p_sp;
1523
1524                         /* Insert this p_sp */
1525                         p_root->p_hdr->p_last->common.p_next = p_sp;
1526                         p_root->p_hdr->p_last = p_sp;
1527
1528                         p_sp->common.p_father = (asf_object_t*)p_root->p_hdr;
1529                     }
1530                 }
1531             }
1532
1533             ASF_ObjectDumpDebug( VLC_OBJECT(s),
1534                                  (asf_object_common_t*)p_root, 0 );
1535             return p_root;
1536         }
1537         msg_Warn( s, "cannot find file properties object" );
1538     }
1539
1540     /* Invalid file */
1541     ASF_FreeObjectRoot( s, p_root );
1542     return NULL;
1543 }
1544
1545 void ASF_FreeObjectRoot( stream_t *s, asf_object_root_t *p_root )
1546 {
1547     asf_object_t *p_obj;
1548
1549     p_obj = p_root->p_first;
1550     while( p_obj )
1551     {
1552         asf_object_t *p_next;
1553         p_next = p_obj->common.p_next;
1554         ASF_FreeObject( s, p_obj );
1555         p_obj = p_next;
1556     }
1557     free( p_root );
1558 }
1559
1560 int  __ASF_CountObject( asf_object_t *p_obj, const guid_t *p_guid )
1561 {
1562     int i_count;
1563     asf_object_t *p_child;
1564
1565     if( !p_obj ) return( 0 );
1566
1567     i_count = 0;
1568     p_child = p_obj->common.p_first;
1569     while( p_child )
1570     {
1571         if( ASF_CmpGUID( &p_child->common.i_object_id, p_guid ) )
1572         {
1573             i_count++;
1574         }
1575         p_child = p_child->common.p_next;
1576     }
1577     return( i_count );
1578 }
1579
1580 void *__ASF_FindObject( asf_object_t *p_obj, const guid_t *p_guid,
1581                         int i_number )
1582 {
1583     asf_object_t *p_child;
1584
1585     p_child = p_obj->common.p_first;
1586
1587     while( p_child )
1588     {
1589         if( ASF_CmpGUID( &p_child->common.i_object_id, p_guid ) )
1590         {
1591             if( i_number == 0 )
1592             {
1593                 /* We found it */
1594                 return( p_child );
1595             }
1596
1597             i_number--;
1598         }
1599         p_child = p_child->common.p_next;
1600     }
1601     return( NULL );
1602 }