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