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