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