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