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