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