]> git.sesse.net Git - vlc/blob - modules/demux/asf/libasf.c
ASF: do not display GUID when not in debug mode
[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 static inline char *get_wstring( const uint8_t *p_data, size_t i_size )
704 {
705     char *psz_str = FromCharset( "UTF-16LE", p_data, i_size );
706     if( psz_str )
707         p_data += i_size;
708     return psz_str;
709 }
710
711 /* Microsoft should go to hell. This time the length give number of bytes
712  * and for the some others object, length give char16 count ... */
713 static int ASF_ReadObject_content_description(stream_t *s, asf_object_t *p_obj)
714 {
715     asf_object_content_description_t *p_cd = &p_obj->content_description;
716     const uint8_t *p_peek, *p_data;
717     int i_peek, i_title, i_artist, i_copyright, i_description, i_rating;
718
719     if( ( i_peek = stream_Peek( s, &p_peek, p_cd->i_object_size ) ) < 34 )
720        return VLC_EGENERIC;
721
722     p_data = p_peek + 24;
723
724     i_title         = ASF_READ2();
725     i_artist        = ASF_READ2();
726     i_copyright     = ASF_READ2();
727     i_description   = ASF_READ2();
728     i_rating        = ASF_READ2();
729
730     if( !ASF_HAVE( i_title+i_artist+i_copyright+i_description+i_rating ) )
731         return VLC_EGENERIC;
732
733     p_cd->psz_title = get_wstring( p_data, i_title );
734     p_cd->psz_artist = get_wstring( p_data, i_artist );
735     p_cd->psz_copyright = get_wstring( p_data, i_copyright );
736     p_cd->psz_description = get_wstring( p_data, i_description );
737     p_cd->psz_rating = get_wstring( p_data, i_rating );
738
739 #ifdef ASF_DEBUG
740     msg_Dbg( s,
741              "read \"content description object\" title:\"%s\" artist:\"%s\" copyright:\"%s\" description:\"%s\" rating:\"%s\"",
742              p_cd->psz_title,
743              p_cd->psz_artist,
744              p_cd->psz_copyright,
745              p_cd->psz_description,
746              p_cd->psz_rating );
747 #endif
748
749     return VLC_SUCCESS;
750 }
751
752 static void ASF_FreeObject_content_description( asf_object_t *p_obj)
753 {
754     asf_object_content_description_t *p_cd = &p_obj->content_description;
755
756     FREENULL( p_cd->psz_title );
757     FREENULL( p_cd->psz_artist );
758     FREENULL( p_cd->psz_copyright );
759     FREENULL( p_cd->psz_description );
760     FREENULL( p_cd->psz_rating );
761 }
762
763 /* Language list: */
764 static int ASF_ReadObject_language_list(stream_t *s, asf_object_t *p_obj)
765 {
766     asf_object_language_list_t *p_ll = &p_obj->language_list;
767     const uint8_t *p_peek, *p_data;
768     int i_peek;
769     int i;
770
771     if( ( i_peek = stream_Peek( s, &p_peek, p_ll->i_object_size ) ) < 26 )
772        return VLC_EGENERIC;
773
774     p_data = &p_peek[24];
775
776     p_ll->i_language = ASF_READ2();
777     if( p_ll->i_language > 0 )
778     {
779         p_ll->ppsz_language = calloc( p_ll->i_language, sizeof( char *) );
780         if( !p_ll->ppsz_language )
781             return VLC_ENOMEM;
782
783         for( i = 0; i < p_ll->i_language; i++ )
784         {
785             if( !ASF_HAVE(1) )
786                 break;
787             p_ll->ppsz_language[i] = ASF_READS( ASF_READ1() );
788         }
789         p_ll->i_language = i;
790     }
791
792 #ifdef ASF_DEBUG
793     msg_Dbg( s, "read \"language list object\" %d entries",
794              p_ll->i_language );
795     for( i = 0; i < p_ll->i_language; i++ )
796         msg_Dbg( s, "  - '%s'",
797                  p_ll->ppsz_language[i] );
798 #endif
799     return VLC_SUCCESS;
800 }
801
802 static void ASF_FreeObject_language_list( asf_object_t *p_obj)
803 {
804     asf_object_language_list_t *p_ll = &p_obj->language_list;
805     int i;
806
807     for( i = 0; i < p_ll->i_language; i++ )
808         FREENULL( p_ll->ppsz_language[i] );
809     FREENULL( p_ll->ppsz_language );
810 }
811
812 /* Stream bitrate properties */
813 static int ASF_ReadObject_stream_bitrate_properties( stream_t *s,
814                                                      asf_object_t *p_obj)
815 {
816     asf_object_stream_bitrate_properties_t *p_sb = &p_obj->stream_bitrate;
817     const uint8_t *p_peek, *p_data;
818     int i_peek;
819     int i;
820
821     if( ( i_peek = stream_Peek( s, &p_peek, p_sb->i_object_size ) ) < 26 )
822        return VLC_EGENERIC;
823
824     p_data = &p_peek[24];
825
826     p_sb->i_bitrate = ASF_READ2();
827     if( p_sb->i_bitrate > 127 )
828         p_sb->i_bitrate = 127;  /* Buggy ? */
829     for( i = 0; i < p_sb->i_bitrate; i++ )
830     {
831         if( !ASF_HAVE(2 + 4) )
832             break;
833         p_sb->bitrate[i].i_stream_number = ASF_READ2()& 0x7f;
834         p_sb->bitrate[i].i_avg_bitrate = ASF_READ4();
835     }
836     p_sb->i_bitrate = i;
837
838 #ifdef ASF_DEBUG
839     msg_Dbg( s,"read \"stream bitrate properties object\"" );
840     for( i = 0; i < p_sb->i_bitrate; i++ )
841     {
842         msg_Dbg( s,"  - stream=%d bitrate=%d",
843                  p_sb->bitrate[i].i_stream_number,
844                  p_sb->bitrate[i].i_avg_bitrate );
845     }
846 #endif
847     return VLC_SUCCESS;
848 }
849 static void ASF_FreeObject_stream_bitrate_properties( asf_object_t *p_obj)
850 {
851     VLC_UNUSED(p_obj);
852 }
853
854 static int ASF_ReadObject_extended_stream_properties( stream_t *s,
855                                                       asf_object_t *p_obj)
856 {
857     asf_object_extended_stream_properties_t *p_esp = &p_obj->ext_stream;
858     const uint8_t *p_peek, *p_data;
859     int i_peek, i;
860
861     if( ( i_peek = stream_Peek( s, &p_peek, p_esp->i_object_size ) ) < 88 )
862        return VLC_EGENERIC;
863
864     p_data = &p_peek[24];
865
866     p_esp->i_start_time = GetQWLE( &p_data[0] );
867     p_esp->i_end_time = GetQWLE( &p_data[8] );
868     p_esp->i_data_bitrate = GetDWLE( &p_data[16] );
869     p_esp->i_buffer_size = GetDWLE( &p_data[20] );
870     p_esp->i_initial_buffer_fullness = GetDWLE( &p_data[24] );
871     p_esp->i_alternate_data_bitrate = GetDWLE( &p_data[28] );
872     p_esp->i_alternate_buffer_size = GetDWLE( &p_data[32] );
873     p_esp->i_alternate_initial_buffer_fullness = GetDWLE( &p_data[36] );
874     p_esp->i_maximum_object_size = GetDWLE( &p_data[40] );
875     p_esp->i_flags = GetDWLE( &p_data[44] );
876     p_esp->i_stream_number = GetWLE( &p_data[48] );
877     p_esp->i_language_index = GetWLE( &p_data[50] );
878     p_esp->i_average_time_per_frame= GetQWLE( &p_data[52] );
879     p_esp->i_stream_name_count = GetWLE( &p_data[60] );
880     p_esp->i_payload_extension_system_count = GetWLE( &p_data[62] );
881
882     p_data += 64;
883
884     p_esp->pi_stream_name_language = calloc( p_esp->i_stream_name_count,
885                                              sizeof(int) );
886     p_esp->ppsz_stream_name = calloc( p_esp->i_stream_name_count,
887                                       sizeof(char*) );
888     if( !p_esp->pi_stream_name_language ||
889         !p_esp->ppsz_stream_name )
890     {
891         free( p_esp->pi_stream_name_language );
892         free( p_esp->ppsz_stream_name );
893         return VLC_ENOMEM;
894     }
895     for( i = 0; i < p_esp->i_stream_name_count; i++ )
896     {
897         if( !ASF_HAVE( 2+2 ) )
898             break;
899         p_esp->pi_stream_name_language[i] = ASF_READ2();
900         p_esp->ppsz_stream_name[i] = ASF_READS( ASF_READ2() );
901     }
902     p_esp->i_stream_name_count = i;
903
904     for( i = 0; i < p_esp->i_payload_extension_system_count; i++ )
905     {
906         ASF_SKIP( 16 );   // GUID
907         ASF_SKIP( 2 );
908         ASF_SKIP( ASF_READ4() );
909     }
910
911     p_esp->p_sp = NULL;
912     if( p_data < &p_peek[i_peek] )
913     {
914         asf_object_t *p_sp;
915         /* Cannot fail as peek succeed */
916         stream_Read( s, NULL, p_data - p_peek );
917
918         p_sp = malloc( sizeof( asf_object_t ) );
919
920         if( !p_sp || ASF_ReadObject( s, p_sp, NULL ) )
921         {
922             free( p_sp );
923         }
924         else
925         {
926             /* This p_sp will be inserted by ReadRoot later */
927             p_esp->p_sp = (asf_object_stream_properties_t*)p_sp;
928         }
929     }
930
931 #ifdef ASF_DEBUG
932     msg_Dbg( s, "read \"extended stream properties object\":" );
933     msg_Dbg( s, "  - start=%"PRId64" end=%"PRId64,
934              p_esp->i_start_time, p_esp->i_end_time );
935     msg_Dbg( s, "  - data bitrate=%d buffer=%d initial fullness=%d",
936              p_esp->i_data_bitrate,
937              p_esp->i_buffer_size,
938              p_esp->i_initial_buffer_fullness );
939     msg_Dbg( s, "  - alternate data bitrate=%d buffer=%d initial fullness=%d",
940              p_esp->i_alternate_data_bitrate,
941              p_esp->i_alternate_buffer_size,
942              p_esp->i_alternate_initial_buffer_fullness );
943     msg_Dbg( s, "  - maximum object size=%d", p_esp->i_maximum_object_size );
944     msg_Dbg( s, "  - flags=0x%x", p_esp->i_flags );
945     msg_Dbg( s, "  - stream number=%d language=%d",
946              p_esp->i_stream_number, p_esp->i_language_index );
947     msg_Dbg( s, "  - average time per frame=%"PRId64,
948              p_esp->i_average_time_per_frame );
949     msg_Dbg( s, "  - stream name count=%d", p_esp->i_stream_name_count );
950     for( i = 0; i < p_esp->i_stream_name_count; i++ )
951         msg_Dbg( s, "     - lang id=%d name=%s",
952                  p_esp->pi_stream_name_language[i],
953                  p_esp->ppsz_stream_name[i] );
954     msg_Dbg( s, "  - payload extension system count=%d",
955              p_esp->i_payload_extension_system_count );
956 #endif
957     return VLC_SUCCESS;
958 }
959 static void ASF_FreeObject_extended_stream_properties( asf_object_t *p_obj)
960 {
961     asf_object_extended_stream_properties_t *p_esp = &p_obj->ext_stream;
962     int i;
963
964     for( i = 0; i < p_esp->i_stream_name_count; i++ )
965         FREENULL( p_esp->ppsz_stream_name[i] );
966     FREENULL( p_esp->pi_stream_name_language );
967     FREENULL( p_esp->ppsz_stream_name );
968 }
969
970
971 static int ASF_ReadObject_advanced_mutual_exclusion( stream_t *s,
972                                                      asf_object_t *p_obj)
973 {
974     asf_object_advanced_mutual_exclusion_t *p_ae = &p_obj->advanced_mutual_exclusion;
975     const uint8_t *p_peek, *p_data;
976     int i_peek;
977     int i;
978
979     if( ( i_peek = stream_Peek( s, &p_peek, p_ae->i_object_size ) ) < 42 )
980        return VLC_EGENERIC;
981
982     p_data = &p_peek[24];
983
984     ASF_GetGUID( &p_ae->type, &p_data[0] );
985     ASF_SKIP( 16 );
986 #ifdef ASF_DEBUG
987     if( guidcmp( &p_ae->type, &asf_guid_mutex_language ) )
988         msg_Dbg( s, "Language exclusion" );
989     else if( guidcmp( &p_ae->type, &asf_guid_mutex_bitrate ) )
990         msg_Dbg( s, "Bitrate exclusion" );
991     else
992         msg_Warn(s, "Unknown exclusion type" );
993 #endif
994
995     p_ae->i_stream_number_count = ASF_READ2();
996     p_ae->pi_stream_number = calloc( p_ae->i_stream_number_count, sizeof(int) );
997
998     for( i = 0; i < p_ae->i_stream_number_count; i++ )
999     {
1000         if( !ASF_HAVE(2) )
1001             break;
1002         p_ae->pi_stream_number[i] = ASF_READ2();
1003     }
1004     p_ae->i_stream_number_count = i;
1005
1006 #ifdef ASF_DEBUG
1007     msg_Dbg( s, "read \"advanced mutual exclusion object\"" );
1008     for( i = 0; i < p_ae->i_stream_number_count; i++ )
1009         msg_Dbg( s, "  - stream=%d", p_ae->pi_stream_number[i] );
1010 #endif
1011     return VLC_SUCCESS;
1012 }
1013 static void ASF_FreeObject_advanced_mutual_exclusion( asf_object_t *p_obj)
1014 {
1015     asf_object_advanced_mutual_exclusion_t *p_ae = &p_obj->advanced_mutual_exclusion;
1016
1017     FREENULL( p_ae->pi_stream_number );
1018 }
1019
1020
1021 static int ASF_ReadObject_stream_prioritization( stream_t *s,
1022                                                  asf_object_t *p_obj)
1023 {
1024     asf_object_stream_prioritization_t *p_sp = &p_obj->stream_prioritization;
1025     const uint8_t *p_peek, *p_data;
1026     int i_peek;
1027     int i;
1028
1029     if( ( i_peek = stream_Peek( s, &p_peek, p_sp->i_object_size ) ) < 26 )
1030        return VLC_EGENERIC;
1031
1032     p_data = &p_peek[24];
1033
1034     p_sp->i_priority_count = ASF_READ2();
1035
1036     p_sp->pi_priority_flag = calloc( p_sp->i_priority_count, sizeof(int) );
1037     p_sp->pi_priority_stream_number =
1038                              calloc( p_sp->i_priority_count, sizeof(int) );
1039
1040     if( !p_sp->pi_priority_flag || !p_sp->pi_priority_stream_number )
1041     {
1042         free( p_sp->pi_priority_flag );
1043         free( p_sp->pi_priority_stream_number );
1044         return VLC_ENOMEM;
1045     }
1046
1047     for( i = 0; i < p_sp->i_priority_count; i++ )
1048     {
1049         if( !ASF_HAVE(2+2) )
1050             break;
1051         p_sp->pi_priority_stream_number[i] = ASF_READ2();
1052         p_sp->pi_priority_flag[i] = ASF_READ2();
1053     }
1054     p_sp->i_priority_count = i;
1055
1056 #ifdef ASF_DEBUG
1057     msg_Dbg( s, "read \"stream prioritization object\"" );
1058     for( i = 0; i < p_sp->i_priority_count; i++ )
1059         msg_Dbg( s, "  - Stream:%d flags=0x%x",
1060                  p_sp->pi_priority_stream_number[i],
1061                  p_sp->pi_priority_flag[i] );
1062 #endif
1063     return VLC_SUCCESS;
1064 }
1065 static void ASF_FreeObject_stream_prioritization( asf_object_t *p_obj)
1066 {
1067     asf_object_stream_prioritization_t *p_sp = &p_obj->stream_prioritization;
1068
1069     FREENULL( p_sp->pi_priority_stream_number );
1070     FREENULL( p_sp->pi_priority_flag );
1071 }
1072
1073
1074 static int ASF_ReadObject_extended_content_description( stream_t *s,
1075                                                         asf_object_t *p_obj)
1076 {
1077     asf_object_extended_content_description_t *p_ec =
1078                                         &p_obj->extended_content_description;
1079     const uint8_t *p_peek, *p_data;
1080     int i_peek;
1081     int i;
1082
1083     if( ( i_peek = stream_Peek( s, &p_peek, p_ec->i_object_size ) ) < 26 )
1084        return VLC_EGENERIC;
1085
1086     p_data = &p_peek[24];
1087
1088     p_ec->i_count = ASF_READ2();
1089     p_ec->ppsz_name  = calloc( p_ec->i_count, sizeof(char*) );
1090     p_ec->ppsz_value = calloc( p_ec->i_count, sizeof(char*) );
1091     if( !p_ec->ppsz_name || !p_ec->ppsz_value )
1092     {
1093         free( p_ec->ppsz_name );
1094         free( p_ec->ppsz_value );
1095         return VLC_ENOMEM;
1096     }
1097     for( i = 0; i < p_ec->i_count; i++ )
1098     {
1099         int i_size;
1100         int i_type;
1101
1102         if( !ASF_HAVE(2 + 2+2) )
1103             break;
1104
1105         p_ec->ppsz_name[i] = ASF_READS( ASF_READ2() );
1106
1107         /* Grrr */
1108         i_type = ASF_READ2();
1109         i_size = ASF_READ2();
1110
1111         if( i_type == 0 )
1112         {
1113             p_ec->ppsz_value[i] = ASF_READS( i_size );
1114         }
1115         else if( i_type == 1 )
1116         {
1117             /* Byte array */
1118             static const char hex[16] = "0123456789ABCDEF";
1119             int j;
1120
1121             p_ec->ppsz_value[i] = malloc( 2*i_size + 1 );
1122             if( p_ec->ppsz_value[i] )
1123             {
1124                 char *psz_value = p_ec->ppsz_value[i];
1125                 for( j = 0; j < i_size; j++ )
1126                 {
1127                     const uint8_t v = ASF_READ1();
1128                     psz_value[2*j+0] = hex[v>>4];
1129                     psz_value[2*j+1] = hex[v&0xf];
1130                 }
1131                 psz_value[2*i_size] = '\0';
1132             }
1133         }
1134         else if( i_type == 2 )
1135         {
1136             /* Bool */
1137             p_ec->ppsz_value[i] = strdup( ASF_READ1() ? "true" : "false" );
1138             ASF_SKIP(i_size-1);
1139         }
1140         else if( i_type == 3 )
1141         {
1142             /* DWord */
1143             if( asprintf( &p_ec->ppsz_value[i], "%d", ASF_READ4() ) == -1 )
1144                 p_ec->ppsz_value[i] = NULL;
1145         }
1146         else if( i_type == 4 )
1147         {
1148             /* QWord */
1149             if( asprintf( &p_ec->ppsz_value[i], "%"PRId64, ASF_READ8() ) == -1 )
1150                 p_ec->ppsz_value[i] = NULL;
1151         }
1152         else if( i_type == 5 )
1153         {
1154             /* Word */
1155             if( asprintf( &p_ec->ppsz_value[i], "%d", ASF_READ2() ) == -1 )
1156                 p_ec->ppsz_value[i] = NULL;
1157         }
1158         else
1159         {
1160             p_ec->ppsz_value[i] = NULL;
1161             ASF_SKIP(i_size);
1162         }
1163     }
1164     p_ec->i_count = i;
1165
1166 #ifdef ASF_DEBUG
1167     msg_Dbg( s, "read \"extended content description object\"" );
1168     for( i = 0; i < p_ec->i_count; i++ )
1169         msg_Dbg( s, "  - '%s' = '%s'",
1170                  p_ec->ppsz_name[i],
1171                  p_ec->ppsz_value[i] );
1172 #endif
1173     return VLC_SUCCESS;
1174 }
1175 static void ASF_FreeObject_extended_content_description( asf_object_t *p_obj)
1176 {
1177     asf_object_extended_content_description_t *p_ec =
1178                                         &p_obj->extended_content_description;
1179     int i;
1180
1181     for( i = 0; i < p_ec->i_count; i++ )
1182     {
1183         FREENULL( p_ec->ppsz_name[i] );
1184         FREENULL( p_ec->ppsz_value[i] );
1185     }
1186     FREENULL( p_ec->ppsz_name );
1187     FREENULL( p_ec->ppsz_value );
1188 }
1189
1190
1191 #if 0
1192 static int ASF_ReadObject_XXX(stream_t *s, asf_object_t *p_obj)
1193 {
1194     asf_object_XXX_t *p_XX =
1195         (asf_object_XXX_t *)p_obj;
1196     const uint8_t *p_peek;
1197     uint8_t *p_data;
1198     int i_peek;
1199
1200     if( ( i_peek = stream_Peek( s, &p_peek, p_XX->i_object_size ) ) < XXX )
1201        return VLC_EGENERIC;
1202
1203     p_data = &p_peek[24];
1204
1205 #ifdef ASF_DEBUG
1206     msg_Dbg( s,
1207              "Read \"XXX object\"" );
1208 #endif
1209     return VLC_SUCCESS;
1210 }
1211 static void ASF_FreeObject_XXX( asf_object_t *p_obj)
1212 {
1213     asf_object_XXX_t *p_XX =
1214         (asf_object_XXX_t *)p_obj;
1215 }
1216 #endif
1217
1218
1219 /* */
1220 static const struct
1221 {
1222     const guid_t  *p_id;
1223     int     i_type;
1224     int     (*ASF_ReadObject_function)( stream_t *, asf_object_t *p_obj );
1225     void    (*ASF_FreeObject_function)( asf_object_t *p_obj );
1226
1227 } ASF_Object_Function [] =
1228 {
1229     { &asf_object_header_guid, ASF_OBJECT_HEADER,
1230       ASF_ReadObject_Header, ASF_FreeObject_Null },
1231     { &asf_object_data_guid, ASF_OBJECT_DATA,
1232       ASF_ReadObject_Data, ASF_FreeObject_Null },
1233     { &asf_object_simple_index_guid, ASF_OBJECT_INDEX,
1234       ASF_ReadObject_Index, ASF_FreeObject_Index },
1235     { &asf_object_file_properties_guid, ASF_OBJECT_FILE_PROPERTIES,
1236       ASF_ReadObject_file_properties, ASF_FreeObject_Null },
1237     { &asf_object_stream_properties_guid, ASF_OBJECT_STREAM_PROPERTIES,
1238       ASF_ReadObject_stream_properties,ASF_FreeObject_stream_properties },
1239     { &asf_object_header_extension_guid, ASF_OBJECT_HEADER_EXTENSION,
1240       ASF_ReadObject_header_extension, ASF_FreeObject_header_extension},
1241     { &asf_object_metadata_guid, ASF_OBJECT_METADATA,
1242       ASF_ReadObject_metadata, ASF_FreeObject_metadata},
1243     { &asf_object_codec_list_guid, ASF_OBJECT_CODEC_LIST,
1244       ASF_ReadObject_codec_list, ASF_FreeObject_codec_list },
1245     { &asf_object_marker_guid, ASF_OBJECT_MARKER, NULL, NULL },
1246     { &asf_object_padding, ASF_OBJECT_PADDING, NULL, NULL },
1247     { &asf_object_compatibility_guid, ASF_OBJECT_OTHER, NULL, NULL },
1248     { &asf_object_content_description_guid, ASF_OBJECT_CONTENT_DESCRIPTION,
1249       ASF_ReadObject_content_description, ASF_FreeObject_content_description },
1250     { &asf_object_language_list, ASF_OBJECT_OTHER,
1251       ASF_ReadObject_language_list, ASF_FreeObject_language_list },
1252     { &asf_object_stream_bitrate_properties, ASF_OBJECT_OTHER,
1253       ASF_ReadObject_stream_bitrate_properties,
1254       ASF_FreeObject_stream_bitrate_properties },
1255     { &asf_object_extended_stream_properties_guid, ASF_OBJECT_OTHER,
1256       ASF_ReadObject_extended_stream_properties,
1257       ASF_FreeObject_extended_stream_properties },
1258     { &asf_object_advanced_mutual_exclusion, ASF_OBJECT_OTHER,
1259       ASF_ReadObject_advanced_mutual_exclusion,
1260       ASF_FreeObject_advanced_mutual_exclusion },
1261     { &asf_object_stream_prioritization, ASF_OBJECT_OTHER,
1262       ASF_ReadObject_stream_prioritization,
1263       ASF_FreeObject_stream_prioritization },
1264     { &asf_object_extended_content_description, ASF_OBJECT_OTHER,
1265       ASF_ReadObject_extended_content_description,
1266       ASF_FreeObject_extended_content_description },
1267
1268     { &asf_object_null_guid, 0, NULL, NULL }
1269 };
1270
1271 static int ASF_ReadObject( stream_t *s, asf_object_t *p_obj,
1272                            asf_object_t *p_father )
1273 {
1274     int i_result;
1275     int i_index;
1276
1277     if( !p_obj )
1278         return 0;
1279
1280     memset( p_obj, 0, sizeof( *p_obj ) );
1281
1282     if( ASF_ReadObjectCommon( s, p_obj ) )
1283     {
1284         msg_Warn( s, "cannot read one asf object" );
1285         return VLC_EGENERIC;
1286     }
1287     p_obj->common.p_father = p_father;
1288     p_obj->common.p_first = NULL;
1289     p_obj->common.p_next = NULL;
1290     p_obj->common.p_last = NULL;
1291
1292     if( p_obj->common.i_object_size < 24 )
1293     {
1294         msg_Warn( s, "found a corrupted asf object (size<24)" );
1295         return VLC_EGENERIC;
1296     }
1297
1298     /* find this object */
1299     for( i_index = 0; ; i_index++ )
1300     {
1301         if( guidcmp( ASF_Object_Function[i_index].p_id,
1302                          &p_obj->common.i_object_id ) ||
1303             guidcmp( ASF_Object_Function[i_index].p_id,
1304                          &asf_object_null_guid ) )
1305         {
1306             break;
1307         }
1308     }
1309     p_obj->common.i_type = ASF_Object_Function[i_index].i_type;
1310
1311     if( i_index == sizeof(ASF_Object_Function)/sizeof(ASF_Object_Function[0]) - 1 )
1312         msg_Warn( s, "unknown asf object (not loaded): " GUID_FMT,
1313                 GUID_PRINT( p_obj->common.i_object_id ) );
1314
1315     /* Now load this object */
1316     if( ASF_Object_Function[i_index].ASF_ReadObject_function == NULL )
1317     {
1318         i_result = VLC_SUCCESS;
1319     }
1320     else
1321     {
1322         /* XXX ASF_ReadObject_function realloc *pp_obj XXX */
1323         i_result =
1324           (ASF_Object_Function[i_index].ASF_ReadObject_function)( s, p_obj );
1325     }
1326
1327     /* link this object with father */
1328     if( p_father && ! i_result )
1329     {
1330         if( p_father->common.p_first )
1331         {
1332             p_father->common.p_last->common.p_next = p_obj;
1333         }
1334         else
1335         {
1336             p_father->common.p_first = p_obj;
1337         }
1338         p_father->common.p_last = p_obj;
1339     }
1340
1341     return i_result;
1342 }
1343
1344 static void ASF_FreeObject( stream_t *s, asf_object_t *p_obj )
1345 {
1346     int i_index;
1347     asf_object_t *p_child;
1348
1349     if( !p_obj )
1350         return;
1351
1352     /* Free all child object */
1353     p_child = p_obj->common.p_first;
1354     while( p_child )
1355     {
1356         asf_object_t *p_next;
1357         p_next = p_child->common.p_next;
1358         ASF_FreeObject( s, p_child );
1359         p_child = p_next;
1360     }
1361
1362     /* find this object */
1363     for( i_index = 0; ; i_index++ )
1364     {
1365         if( guidcmp( ASF_Object_Function[i_index].p_id,
1366                      &p_obj->common.i_object_id )||
1367             guidcmp( ASF_Object_Function[i_index].p_id,
1368                      &asf_object_null_guid ) )
1369         {
1370             break;
1371         }
1372     }
1373
1374     /* Now free this object */
1375     if( ASF_Object_Function[i_index].ASF_FreeObject_function != NULL )
1376     {
1377 #ifdef ASF_DEBUG
1378         msg_Dbg( s,
1379                   "free asf object " GUID_FMT,
1380                   GUID_PRINT( p_obj->common.i_object_id ) );
1381 #endif
1382         (ASF_Object_Function[i_index].ASF_FreeObject_function)( p_obj );
1383     }
1384     free( p_obj );
1385 }
1386
1387 /*****************************************************************************
1388  * ASF_ObjectDumpDebug:
1389  *****************************************************************************/
1390 static const struct
1391 {
1392     const guid_t *p_id;
1393     const char *psz_name;
1394 } ASF_ObjectDumpDebugInfo[] =
1395 {
1396     { &asf_object_header_guid, "Header" },
1397     { &asf_object_data_guid, "Data" },
1398     { &asf_object_index_guid, "Index" },
1399     { &asf_object_simple_index_guid, "Simple Index" },
1400     { &asf_object_file_properties_guid, "File Properties" },
1401     { &asf_object_stream_properties_guid, "Stream Properties" },
1402     { &asf_object_content_description_guid, "Content Description" },
1403     { &asf_object_header_extension_guid, "Header Extension" },
1404     { &asf_object_metadata_guid, "Metadata" },
1405     { &asf_object_codec_list_guid, "Codec List" },
1406     { &asf_object_marker_guid, "Marker" },
1407     { &asf_object_stream_type_audio, "Stream Type Audio" },
1408     { &asf_object_stream_type_video, "Stream Type Video" },
1409     { &asf_object_stream_type_command, "Stream Type Command" },
1410     { &asf_object_language_list, "Language List" },
1411     { &asf_object_stream_bitrate_properties, "Stream Bitrate Properties" },
1412     { &asf_object_padding, "Padding" },
1413     { &asf_object_extended_stream_properties_guid, "Extended Stream Properties" },
1414     { &asf_object_advanced_mutual_exclusion, "Advanced Mutual Exclusion" },
1415     { &asf_object_stream_prioritization, "Stream Prioritization" },
1416     { &asf_object_extended_content_description, "Extended content description"},
1417
1418     { NULL, "Unknown" },
1419 };
1420
1421
1422 static void ASF_ObjectDumpDebug( vlc_object_t *p_obj,
1423                                  asf_object_common_t *p_node, unsigned i_level )
1424 {
1425     unsigned i;
1426     union asf_object_u *p_child;
1427     const char *psz_name;
1428
1429     /* Find the name */
1430     for( i = 0; ASF_ObjectDumpDebugInfo[i].p_id != NULL; i++ )
1431     {
1432         if( guidcmp( ASF_ObjectDumpDebugInfo[i].p_id,
1433                           &p_node->i_object_id ) )
1434             break;
1435     }
1436     psz_name = ASF_ObjectDumpDebugInfo[i].psz_name;
1437
1438     char str[512];
1439     if( i_level >= (sizeof(str) - 1)/5 )
1440         return;
1441
1442     memset( str, ' ', sizeof( str ) );
1443     for( i = 0; i < i_level; i++ )
1444     {
1445         str[i * 4] = '|';
1446     }
1447     snprintf( &str[4*i_level], sizeof(str) - 5*i_level,
1448              "+ '%s'"
1449 #ifdef ASF_DEBUG
1450              "GUID "GUID_FMT" size:%"PRIu64" pos:%"PRIu64
1451 #endif
1452              , psz_name
1453
1454 #ifdef ASF_DEBUG
1455              , GUID_PRINT( p_node->i_object_id ),
1456              p_node->i_object_size, p_node->i_object_pos
1457 #endif
1458              );
1459
1460
1461     msg_Dbg( p_obj, "%s", str );
1462
1463     for( p_child = p_node->p_first; p_child != NULL;
1464                                              p_child = p_child->common.p_next )
1465     {
1466         ASF_ObjectDumpDebug( p_obj, &p_child->common, i_level + 1 );
1467     }
1468 }
1469
1470 /*****************************************************************************
1471  * ASF_ReadObjetRoot : parse the entire stream/file
1472  *****************************************************************************/
1473 asf_object_root_t *ASF_ReadObjectRoot( stream_t *s, int b_seekable )
1474 {
1475     asf_object_root_t *p_root = malloc( sizeof( asf_object_root_t ) );
1476     asf_object_t *p_obj;
1477
1478     if( !p_root )
1479         return NULL;
1480
1481     p_root->i_type = ASF_OBJECT_ROOT;
1482     memcpy( &p_root->i_object_id, &asf_object_null_guid, sizeof( guid_t ) );
1483     p_root->i_object_pos = stream_Tell( s );
1484     p_root->i_object_size = 0;
1485     p_root->p_first = NULL;
1486     p_root->p_last  = NULL;
1487     p_root->p_next  = NULL;
1488     p_root->p_hdr   = NULL;
1489     p_root->p_data  = NULL;
1490     p_root->p_fp    = NULL;
1491     p_root->p_index = NULL;
1492     p_root->p_metadata = NULL;
1493
1494     for( ; ; )
1495     {
1496         p_obj = malloc( sizeof( asf_object_t ) );
1497
1498         if( !p_obj || ASF_ReadObject( s, p_obj, (asf_object_t*)p_root ) )
1499         {
1500             free( p_obj );
1501             break;
1502         }
1503         switch( p_obj->common.i_type )
1504         {
1505             case( ASF_OBJECT_HEADER ):
1506                 p_root->p_hdr = (asf_object_header_t*)p_obj;
1507                 break;
1508             case( ASF_OBJECT_DATA ):
1509                 p_root->p_data = (asf_object_data_t*)p_obj;
1510                 break;
1511             case( ASF_OBJECT_INDEX ):
1512                 p_root->p_index = (asf_object_index_t*)p_obj;
1513                 break;
1514             default:
1515                 msg_Warn( s, "unknown top-level object found: " GUID_FMT,
1516                       GUID_PRINT( p_obj->common.i_object_id ) );
1517                 break;
1518         }
1519         if( p_obj->common.i_type == ASF_OBJECT_DATA &&
1520             p_obj->common.i_object_size <= 50 )
1521         {
1522             /* probably a dump of broadcasted asf */
1523             break;
1524         }
1525         if( !b_seekable && p_root->p_hdr && p_root->p_data )
1526         {
1527             /* For unseekable stream it's enough to play */
1528             break;
1529         }
1530
1531         if( ASF_NextObject( s, p_obj ) ) /* Go to the next object */
1532             break;
1533     }
1534
1535     if( p_root->p_hdr != NULL && p_root->p_data != NULL )
1536     {
1537         p_root->p_fp = ASF_FindObject( p_root->p_hdr,
1538                                        &asf_object_file_properties_guid, 0 );
1539
1540         if( p_root->p_fp )
1541         {
1542             asf_object_t *p_hdr_ext =
1543                 ASF_FindObject( p_root->p_hdr,
1544                                 &asf_object_header_extension_guid, 0 );
1545             if( p_hdr_ext )
1546             {
1547                 int i_ext_stream;
1548                 int i;
1549
1550                 p_root->p_metadata =
1551                     ASF_FindObject( p_hdr_ext,
1552                                     &asf_object_metadata_guid, 0 );
1553                 /* Special case for broken designed file format :( */
1554                 i_ext_stream = ASF_CountObject( p_hdr_ext,
1555                                     &asf_object_extended_stream_properties_guid );
1556                 for( i = 0; i < i_ext_stream; i++ )
1557                 {
1558                     asf_object_t *p_esp =
1559                         ASF_FindObject( p_hdr_ext,
1560                                    &asf_object_extended_stream_properties_guid, i );
1561                     if( p_esp->ext_stream.p_sp )
1562                     {
1563                         asf_object_t *p_sp =
1564                                          (asf_object_t*)p_esp->ext_stream.p_sp;
1565
1566                         /* Insert this p_sp */
1567                         p_root->p_hdr->p_last->common.p_next = p_sp;
1568                         p_root->p_hdr->p_last = p_sp;
1569
1570                         p_sp->common.p_father = (asf_object_t*)p_root->p_hdr;
1571                     }
1572                 }
1573             }
1574
1575             ASF_ObjectDumpDebug( VLC_OBJECT(s),
1576                                  (asf_object_common_t*)p_root, 0 );
1577             return p_root;
1578         }
1579         msg_Warn( s, "cannot find file properties object" );
1580     }
1581
1582     /* Invalid file */
1583     ASF_FreeObjectRoot( s, p_root );
1584     return NULL;
1585 }
1586
1587 void ASF_FreeObjectRoot( stream_t *s, asf_object_root_t *p_root )
1588 {
1589     asf_object_t *p_obj;
1590
1591     p_obj = p_root->p_first;
1592     while( p_obj )
1593     {
1594         asf_object_t *p_next;
1595         p_next = p_obj->common.p_next;
1596         ASF_FreeObject( s, p_obj );
1597         p_obj = p_next;
1598     }
1599     free( p_root );
1600 }
1601
1602 int ASF_CountObject( void *_p_obj, const guid_t *p_guid )
1603 {
1604     int i_count;
1605     asf_object_t *p_child, *p_obj;
1606
1607     p_obj = (asf_object_t *)_p_obj;
1608     if( !p_obj )
1609         return 0;
1610
1611     i_count = 0;
1612     p_child = p_obj->common.p_first;
1613     while( p_child )
1614     {
1615         if( guidcmp( &p_child->common.i_object_id, p_guid ) )
1616             i_count++;
1617
1618         p_child = p_child->common.p_next;
1619     }
1620     return i_count;
1621 }
1622
1623 void *ASF_FindObject( void *_p_obj, const guid_t *p_guid,
1624                         int i_number )
1625 {
1626     asf_object_t *p_child, *p_obj;
1627
1628     p_obj = (asf_object_t *)_p_obj;
1629     p_child = p_obj->common.p_first;
1630
1631     while( p_child )
1632     {
1633         if( guidcmp( &p_child->common.i_object_id, p_guid ) )
1634         {
1635             if( i_number == 0 )
1636                 return p_child;
1637
1638             i_number--;
1639         }
1640         p_child = p_child->common.p_next;
1641     }
1642     return NULL;
1643 }