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