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