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