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