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