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