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