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