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