]> git.sesse.net Git - vlc/blob - modules/demux/asf/libasf.c
* all : demuxers *have to* set pf_demux_control. (demux_vaControlDefault
[vlc] / modules / demux / asf / libasf.c
1 /*****************************************************************************
2  * libasf.c :
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: libasf.c,v 1.18 2003/09/07 22:48:29 fenrir Exp $
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 #include <stdlib.h>                                      /* malloc(), free() */
24
25 #include <vlc/vlc.h>
26 #include <vlc/input.h>
27
28 #include "codecs.h"                        /* BITMAPINFOHEADER, WAVEFORMATEX */
29 #include "libasf.h"
30
31 #define ASF_DEBUG 1
32
33 #define FREE( p ) \
34     if( p ) {free( p ); p = NULL; }
35
36 #define GUID_FMT "0x%x-0x%x-0x%x-0x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x"
37 #define GUID_PRINT( guid )  \
38     (guid).v1,              \
39     (guid).v2,              \
40     (guid).v3,              \
41     (guid).v4[0],(guid).v4[1],(guid).v4[2],(guid).v4[3],    \
42     (guid).v4[4],(guid).v4[5],(guid).v4[6],(guid).v4[7]
43
44 /****************************************************************************
45  *
46  ****************************************************************************/
47 static int ASF_ReadObject( stream_t *,
48                            asf_object_t *p_obj,  asf_object_t *p_father );
49
50
51 /****************************************************************************
52  * GUID functions
53  ****************************************************************************/
54 void ASF_GetGUID( guid_t *p_guid, uint8_t *p_data )
55 {
56     p_guid->v1 = GetDWLE( p_data );
57     p_guid->v2 = GetWLE( p_data + 4);
58     p_guid->v3 = GetWLE( p_data + 6);
59     memcpy( p_guid->v4, p_data + 8, 8 );
60 }
61
62 int ASF_CmpGUID( const guid_t *p_guid1, const guid_t *p_guid2 )
63 {
64     if( (p_guid1->v1 != p_guid2->v1 )||
65         (p_guid1->v2 != p_guid2->v2 )||
66         (p_guid1->v3 != p_guid2->v3 )||
67         ( memcmp( p_guid1->v4, p_guid2->v4,8 )) )
68     {
69         return( 0 );
70     }
71     return( 1 ); /* match */
72 }
73
74 /****************************************************************************
75  *
76  ****************************************************************************/
77 static int ASF_ReadObjectCommon( stream_t *s, asf_object_t *p_obj )
78 {
79     asf_object_common_t *p_common = (asf_object_common_t*)p_obj;
80     uint8_t             *p_peek;
81
82     if( stream_Peek( s, &p_peek, 24 ) < 24 )
83     {
84         return( VLC_EGENERIC );
85     }
86     ASF_GetGUID( &p_common->i_object_id, p_peek );
87     p_common->i_object_size = GetQWLE( p_peek + 16 );
88     p_common->i_object_pos  = stream_Tell( s );
89     p_common->p_next = NULL;
90 #ifdef ASF_DEBUG
91     msg_Dbg( (vlc_object_t*)s,
92              "Found Object guid: " GUID_FMT " size:"I64Fd,
93              GUID_PRINT( p_common->i_object_id ),
94              p_common->i_object_size );
95 #endif
96
97     return( VLC_SUCCESS );
98 }
99
100 static int ASF_NextObject( stream_t *s, asf_object_t *p_obj )
101 {
102     asf_object_t obj;
103     if( p_obj == NULL )
104     {
105         if( ASF_ReadObjectCommon( s, &obj ) )
106         {
107             return( VLC_EGENERIC );
108         }
109         p_obj = &obj;
110     }
111
112     if( p_obj->common.i_object_size <= 0 )
113     {
114         return( VLC_EGENERIC );
115     }
116     if( p_obj->common.p_father && p_obj->common.p_father->common.i_object_size != 0 )
117     {
118         if( p_obj->common.p_father->common.i_object_pos + p_obj->common.p_father->common.i_object_size <
119                 p_obj->common.i_object_pos + p_obj->common.i_object_size + 24 )
120                                 /* 24 is min size of an object */
121         {
122             return( VLC_EGENERIC );
123         }
124
125     }
126
127     return stream_Seek( s,
128                         p_obj->common.i_object_pos +
129                            p_obj->common.i_object_size );
130 }
131
132 static void ASF_FreeObject_Null( asf_object_t *pp_obj )
133 {
134     return;
135 }
136
137 static int  ASF_ReadObject_Header( stream_t *s, asf_object_t *p_obj )
138 {
139     asf_object_header_t *p_hdr = (asf_object_header_t*)p_obj;
140     asf_object_t        *p_subobj;
141     int                 i_peek;
142     uint8_t             *p_peek;
143
144     if( ( i_peek = stream_Peek( s, &p_peek, 30 ) ) < 30 )
145     {
146        return( VLC_EGENERIC );
147     }
148
149     p_hdr->i_sub_object_count = GetDWLE( p_peek + 24 );
150     p_hdr->i_reserved1 = p_peek[28];
151     p_hdr->i_reserved2 = p_peek[29];
152     p_hdr->p_first = NULL;
153     p_hdr->p_last  = NULL;
154 #ifdef ASF_DEBUG
155     msg_Dbg( (vlc_object_t*)s,
156              "Read \"Header Object\" subobj:%d, reserved1:%d, reserved2:%d",
157              p_hdr->i_sub_object_count,
158              p_hdr->i_reserved1,
159              p_hdr->i_reserved2 );
160 #endif
161     /* Cannot failed as peek succeed */
162     stream_Read( s, NULL, 30 );
163
164     /* Now load sub object */
165     for( ; ; )
166     {
167         p_subobj  = malloc( sizeof( asf_object_t ) );
168
169         if( ASF_ReadObject( s, p_subobj, (asf_object_t*)p_hdr ) )
170         {
171             break;
172         }
173         if( ASF_NextObject( s, p_subobj ) ) /* Go to the next object */
174         {
175             break;
176         }
177     }
178     return VLC_SUCCESS;
179 }
180
181 static int ASF_ReadObject_Data( stream_t *s, asf_object_t *p_obj )
182 {
183     asf_object_data_t *p_data = (asf_object_data_t*)p_obj;
184     int               i_peek;
185     uint8_t           *p_peek;
186
187     if( ( i_peek = stream_Peek( s, &p_peek, 50 ) ) < 50 )
188     {
189        return VLC_EGENERIC;
190     }
191     ASF_GetGUID( &p_data->i_file_id, p_peek + 24 );
192     p_data->i_total_data_packets = GetQWLE( p_peek + 40 );
193     p_data->i_reserved = GetWLE( p_peek + 48 );
194 #ifdef ASF_DEBUG
195     msg_Dbg( (vlc_object_t*)s,
196              "Read \"Data Object\" file_id:" GUID_FMT " total data packet:"
197              I64Fd" reserved:%d",
198              GUID_PRINT( p_data->i_file_id ),
199              p_data->i_total_data_packets,
200              p_data->i_reserved );
201 #endif
202     return VLC_SUCCESS;
203 }
204
205 static int ASF_ReadObject_Index( stream_t *s, asf_object_t *p_obj )
206 {
207     asf_object_index_t *p_index = (asf_object_index_t*)p_obj;
208     int                i_peek;
209     uint8_t            *p_peek;
210
211     if( ( i_peek = stream_Peek( s, &p_peek, 56 ) ) < 56 )
212     {
213        return VLC_EGENERIC;
214     }
215     ASF_GetGUID( &p_index->i_file_id, p_peek + 24 );
216     p_index->i_index_entry_time_interval = GetQWLE( p_peek + 40 );
217     p_index->i_max_packet_count = GetDWLE( p_peek + 48 );
218     p_index->i_index_entry_count = GetDWLE( p_peek + 52 );
219     p_index->index_entry = NULL; /* FIXME */
220
221 #ifdef ASF_DEBUG
222     msg_Dbg( (vlc_object_t*)s,
223             "Read \"Index Object\" file_id:" GUID_FMT
224             " index_entry_time_interval:"I64Fd" max_packet_count:%d "
225             "index_entry_count:%ld",
226             GUID_PRINT( p_index->i_file_id ),
227             p_index->i_index_entry_time_interval,
228             p_index->i_max_packet_count,
229             (long int)p_index->i_index_entry_count );
230 #endif
231     return VLC_SUCCESS;
232 }
233 static void ASF_FreeObject_Index( asf_object_t *p_obj )
234 {
235     asf_object_index_t *p_index = (asf_object_index_t*)p_obj;
236
237     FREE( p_index->index_entry );
238 }
239
240 static int ASF_ReadObject_file_properties( stream_t *s, asf_object_t *p_obj )
241 {
242     asf_object_file_properties_t *p_fp = (asf_object_file_properties_t*)p_obj;
243     int      i_peek;
244     uint8_t  *p_peek;
245
246     if( ( i_peek = stream_Peek( s, &p_peek,  92) ) < 92 )
247     {
248        return VLC_EGENERIC;
249     }
250     ASF_GetGUID( &p_fp->i_file_id, p_peek + 24 );
251     p_fp->i_file_size = GetQWLE( p_peek + 40 );
252     p_fp->i_creation_date = GetQWLE( p_peek + 48 );
253     p_fp->i_data_packets_count = GetQWLE( p_peek + 56 );
254     p_fp->i_play_duration = GetQWLE( p_peek + 64 );
255     p_fp->i_send_duration = GetQWLE( p_peek + 72 );
256     p_fp->i_preroll = GetQWLE( p_peek + 80 );
257     p_fp->i_flags = GetDWLE( p_peek + 88 );
258     p_fp->i_min_data_packet_size = GetDWLE( p_peek + 92 );
259     p_fp->i_max_data_packet_size = GetDWLE( p_peek + 96 );
260     p_fp->i_max_bitrate = GetDWLE( p_peek + 100 );
261
262 #ifdef ASF_DEBUG
263     msg_Dbg( (vlc_object_t*)s,
264             "Read \"File Properties Object\" file_id:" GUID_FMT
265             " file_size:"I64Fd" creation_date:"I64Fd" data_packets_count:"
266             I64Fd" play_duration:"I64Fd" send_duration:"I64Fd" preroll:"
267             I64Fd" flags:%d min_data_packet_size:%d max_data_packet_size:%d "
268             "max_bitrate:%d",
269             GUID_PRINT( p_fp->i_file_id ),
270             p_fp->i_file_size,
271             p_fp->i_creation_date,
272             p_fp->i_data_packets_count,
273             p_fp->i_play_duration,
274             p_fp->i_send_duration,
275             p_fp->i_preroll,
276             p_fp->i_flags,
277             p_fp->i_min_data_packet_size,
278             p_fp->i_max_data_packet_size,
279             p_fp->i_max_bitrate );
280 #endif
281     return VLC_SUCCESS;
282 }
283
284 static int ASF_ReadObject_header_extention( stream_t *s, asf_object_t *p_obj )
285 {
286     asf_object_header_extention_t *p_he = (asf_object_header_extention_t*)p_obj;
287     int     i_peek;
288     uint8_t *p_peek;
289
290     if( ( i_peek = stream_Peek( s, &p_peek, p_he->i_object_size ) ) <  46)
291     {
292        return VLC_EGENERIC;
293     }
294     ASF_GetGUID( &p_he->i_reserved1, p_peek + 24 );
295     p_he->i_reserved2 = GetWLE( p_peek + 40 );
296     p_he->i_header_extention_size = GetDWLE( p_peek + 42 );
297     if( p_he->i_header_extention_size )
298     {
299         p_he->p_header_extention_data = malloc( p_he->i_header_extention_size );
300         memcpy( p_he->p_header_extention_data,
301                 p_peek + 46,
302                 p_he->i_header_extention_size );
303     }
304     else
305     {
306         p_he->p_header_extention_data = NULL;
307     }
308 #ifdef ASF_DEBUG
309     msg_Dbg( (vlc_object_t*)s,
310             "Read \"Header Extention Object\" reserved1:" GUID_FMT " reserved2:%d header_extention_size:%d",
311             GUID_PRINT( p_he->i_reserved1 ),
312             p_he->i_reserved2,
313             p_he->i_header_extention_size );
314 #endif
315     return VLC_SUCCESS;
316 }
317 static void ASF_FreeObject_header_extention( asf_object_t *p_obj )
318 {
319     asf_object_header_extention_t *p_he = (asf_object_header_extention_t*)p_obj;
320
321     FREE( p_he->p_header_extention_data );
322 }
323
324 static int ASF_ReadObject_stream_properties( stream_t *s, asf_object_t *p_obj )
325 {
326     asf_object_stream_properties_t *p_sp =
327                     (asf_object_stream_properties_t*)p_obj;
328     int     i_peek;
329     uint8_t *p_peek;
330
331     if( ( i_peek = stream_Peek( s, &p_peek,  p_sp->i_object_size ) ) < 74 )
332     {
333        return VLC_EGENERIC;
334     }
335     ASF_GetGUID( &p_sp->i_stream_type, p_peek + 24 );
336     ASF_GetGUID( &p_sp->i_error_correction_type, p_peek + 40 );
337     p_sp->i_time_offset = GetQWLE( p_peek + 56 );
338     p_sp->i_type_specific_data_length = GetDWLE( p_peek + 64 );
339     p_sp->i_error_correction_data_length = GetDWLE( p_peek + 68 );
340     p_sp->i_flags = GetWLE( p_peek + 72 );
341         p_sp->i_stream_number = p_sp->i_flags&0x07f;
342     p_sp->i_reserved = GetDWLE( p_peek + 74 );
343     if( p_sp->i_type_specific_data_length )
344     {
345         p_sp->p_type_specific_data = malloc( p_sp->i_type_specific_data_length );
346         memcpy( p_sp->p_type_specific_data,
347                 p_peek + 78,
348                 p_sp->i_type_specific_data_length );
349     }
350     else
351     {
352         p_sp->p_type_specific_data = NULL;
353     }
354     if( p_sp->i_error_correction_data_length )
355     {
356         p_sp->p_error_correction_data = malloc( p_sp->i_error_correction_data_length );
357         memcpy( p_sp->p_error_correction_data,
358                 p_peek + 78 + p_sp->i_type_specific_data_length,
359                 p_sp->i_error_correction_data_length );
360     }
361     else
362     {
363         p_sp->p_error_correction_data = NULL;
364     }
365
366 #ifdef ASF_DEBUG
367     msg_Dbg( (vlc_object_t*)s,
368             "Read \"Stream Properties Object\" stream_type:" GUID_FMT
369             " error_correction_type:" GUID_FMT " time_offset:"I64Fd
370             " type_specific_data_length:%d error_correction_data_length:%d"
371             " flags:0x%x stream_number:%d",
372             GUID_PRINT( p_sp->i_stream_type ),
373             GUID_PRINT( p_sp->i_error_correction_type ),
374             p_sp->i_time_offset,
375             p_sp->i_type_specific_data_length,
376             p_sp->i_error_correction_data_length,
377             p_sp->i_flags,
378             p_sp->i_stream_number );
379
380 #endif
381     return VLC_SUCCESS;
382 }
383
384 static void ASF_FreeObject_stream_properties( asf_object_t *p_obj )
385 {
386     asf_object_stream_properties_t *p_sp =
387                 (asf_object_stream_properties_t*)p_obj;
388
389     FREE( p_sp->p_type_specific_data );
390     FREE( p_sp->p_error_correction_data );
391 }
392
393
394 static int ASF_ReadObject_codec_list( stream_t *s, asf_object_t *p_obj )
395 {
396     asf_object_codec_list_t *p_cl = (asf_object_codec_list_t*)p_obj;
397     int     i_peek;
398     uint8_t *p_peek, *p_data;
399
400     unsigned int i_codec;
401
402     if( ( i_peek = stream_Peek( s, &p_peek, p_cl->i_object_size ) ) < 44 )
403     {
404        return VLC_EGENERIC;
405     }
406
407     ASF_GetGUID( &p_cl->i_reserved, p_peek + 24 );
408     p_cl->i_codec_entries_count = GetWLE( p_peek + 40 );
409     if( p_cl->i_codec_entries_count > 0 )
410     {
411
412         p_cl->codec = calloc( p_cl->i_codec_entries_count,
413                               sizeof( asf_codec_entry_t ) );
414         memset( p_cl->codec, 0,
415                 p_cl->i_codec_entries_count * sizeof( asf_codec_entry_t ) );
416
417         p_data = p_peek + 44;
418         for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
419         {
420 #define codec p_cl->codec[i_codec]
421             int i_len, i;
422
423             codec.i_type = GetWLE( p_data ); p_data += 2;
424             /* codec name */
425             i_len = GetWLE( p_data ); p_data += 2;
426             codec.psz_name = calloc( sizeof( char ), i_len + 1);
427             for( i = 0; i < i_len; i++ )
428             {
429                 codec.psz_name[i] = GetWLE( p_data + 2*i );
430             }
431             codec.psz_name[i_len] = '\0';
432             p_data += 2 * i_len;
433
434             /* description */
435             i_len = GetWLE( p_data ); p_data += 2;
436             codec.psz_description = calloc( sizeof( char ), i_len + 1);
437             for( i = 0; i < i_len; i++ )
438             {
439                 codec.psz_description[i] = GetWLE( p_data + 2*i );
440             }
441             codec.psz_description[i_len] = '\0';
442             p_data += 2 * i_len;
443
444             /* opaque information */
445             codec.i_information_length = GetWLE( p_data ); p_data += 2;
446             if( codec.i_information_length > 0 )
447             {
448                 codec.p_information = malloc( codec.i_information_length );
449                 memcpy( codec.p_information, p_data, codec.i_information_length );
450                 p_data += codec.i_information_length;
451             }
452             else
453             {
454                 codec.p_information = NULL;
455             }
456 #undef  codec
457         }
458     }
459     else
460     {
461         p_cl->codec = NULL;
462     }
463
464 #ifdef ASF_DEBUG
465     msg_Dbg( (vlc_object_t*)s,
466             "Read \"Codec List Object\" reserved_guid:" GUID_FMT " codec_entries_count:%d",
467             GUID_PRINT( p_cl->i_reserved ),
468             p_cl->i_codec_entries_count );
469
470     for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
471     {
472 #define codec p_cl->codec[i_codec]
473         msg_Dbg( (vlc_object_t*)s,
474                  "Read \"Codec List Object\" codec[%d] %s name:\"%s\" description:\"%s\" information_length:%d",
475                  i_codec,
476                  ( codec.i_type == ASF_CODEC_TYPE_VIDEO ) ? "video" : ( ( codec.i_type == ASF_CODEC_TYPE_AUDIO ) ? "audio" : "unknown" ),
477                  codec.psz_name,
478                  codec.psz_description,
479                  codec.i_information_length );
480     }
481 #endif
482     return VLC_SUCCESS;
483 }
484
485 static void ASF_FreeObject_codec_list( asf_object_t *p_obj )
486 {
487     asf_object_codec_list_t *p_cl = (asf_object_codec_list_t*)p_obj;
488     unsigned int i_codec;
489
490     for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
491     {
492 #define codec p_cl->codec[i_codec]
493         FREE( codec.psz_name );
494         FREE( codec.psz_description );
495         FREE( codec.p_information );
496
497 #undef  codec
498     }
499     FREE( p_cl->codec );
500 }
501
502 /* Microsoft should go to hell. This time the length give number of bytes
503  * and for the some others object, length give char16 count ... */
504 static int ASF_ReadObject_content_description(stream_t *s, asf_object_t *p_obj)
505 {
506     asf_object_content_description_t *p_cd =
507                                     (asf_object_content_description_t*)p_obj;
508     int     i_peek;
509     uint8_t *p_peek, *p_data;
510
511     int i_len;
512     int i_title;
513     int i_author;
514     int i_copyright;
515     int i_description;
516     int i_rating;
517
518 #define GETSTRINGW( psz_str, i_size ) \
519    psz_str = calloc( i_size/2 + 1, sizeof( char ) ); \
520    for( i_len = 0; i_len < i_size/2; i_len++ ) \
521    { \
522        psz_str[i_len] = GetWLE( p_data + 2*i_len ); \
523    } \
524    psz_str[i_size/2] = '\0'; \
525    p_data += i_size;
526
527     if( ( i_peek = stream_Peek( s, &p_peek, p_cd->i_object_size ) ) < 34 )
528     {
529        return VLC_EGENERIC;
530     }
531     p_data = p_peek + 24;
532
533     i_title = GetWLE( p_data ); p_data += 2;
534     i_author= GetWLE( p_data ); p_data += 2;
535     i_copyright     = GetWLE( p_data ); p_data += 2;
536     i_description   = GetWLE( p_data ); p_data += 2;
537     i_rating        = GetWLE( p_data ); p_data += 2;
538
539     GETSTRINGW( p_cd->psz_title, i_title );
540     GETSTRINGW( p_cd->psz_author, i_author );
541     GETSTRINGW( p_cd->psz_copyright, i_copyright );
542     GETSTRINGW( p_cd->psz_description, i_description );
543     GETSTRINGW( p_cd->psz_rating, i_rating );
544
545 #undef  GETSTRINGW
546
547 #ifdef ASF_DEBUG
548     msg_Dbg( (vlc_object_t*)s,
549              "Read \"Content Description Object\" title:\"%s\" author:\"%s\" copyright:\"%s\" description:\"%s\" rating:\"%s\"",
550              p_cd->psz_title,
551              p_cd->psz_author,
552              p_cd->psz_copyright,
553              p_cd->psz_description,
554              p_cd->psz_rating );
555 #endif
556     return VLC_SUCCESS;
557 }
558
559 static void ASF_FreeObject_content_description( asf_object_t *p_obj)
560 {
561     asf_object_content_description_t *p_cd =
562                                     (asf_object_content_description_t*)p_obj;
563
564     FREE( p_cd->psz_title );
565     FREE( p_cd->psz_author );
566     FREE( p_cd->psz_copyright );
567     FREE( p_cd->psz_description );
568     FREE( p_cd->psz_rating );
569 }
570
571 static struct
572 {
573     const guid_t  *p_id;
574     int     i_type;
575     int     (*ASF_ReadObject_function)( stream_t *, asf_object_t *p_obj );
576     void    (*ASF_FreeObject_function)( asf_object_t *p_obj );
577 } ASF_Object_Function [] =
578 {
579     { &asf_object_header_guid,            ASF_OBJECT_TYPE_HEADER,             ASF_ReadObject_Header, ASF_FreeObject_Null },
580     { &asf_object_data_guid,              ASF_OBJECT_TYPE_DATA,               ASF_ReadObject_Data,   ASF_FreeObject_Null },
581     { &asf_object_index_guid,             ASF_OBJECT_TYPE_INDEX,              ASF_ReadObject_Index,  ASF_FreeObject_Index },
582     { &asf_object_file_properties_guid,   ASF_OBJECT_TYPE_FILE_PROPERTIES,    ASF_ReadObject_file_properties,  ASF_FreeObject_Null },
583     { &asf_object_stream_properties_guid, ASF_OBJECT_TYPE_STREAM_PROPERTIES,  ASF_ReadObject_stream_properties,ASF_FreeObject_stream_properties },
584     { &asf_object_header_extention_guid,  ASF_OBJECT_TYPE_EXTENTION_HEADER,   ASF_ReadObject_header_extention, ASF_FreeObject_header_extention},
585     { &asf_object_codec_list_guid,        ASF_OBJECT_TYPE_CODEC_LIST,         ASF_ReadObject_codec_list,       ASF_FreeObject_codec_list },
586     { &asf_object_marker_guid,            ASF_OBJECT_TYPE_MARKER,             NULL,                  NULL },
587     { &asf_object_content_description_guid, ASF_OBJECT_TYPE_CONTENT_DESCRIPTION, ASF_ReadObject_content_description, ASF_FreeObject_content_description },
588
589     { &asf_object_null_guid,   0,                      NULL,                  NULL }
590 };
591
592 static int ASF_ReadObject( stream_t *s,
593                            asf_object_t *p_obj, asf_object_t *p_father )
594 {
595     int i_result;
596     int i_index;
597
598     if( !p_obj )
599     {
600         return( 0 );
601     }
602     if( ASF_ReadObjectCommon( s, p_obj ) )
603     {
604         msg_Warn( (vlc_object_t*)s, "Cannot read one asf object" );
605         return VLC_EGENERIC;
606     }
607     p_obj->common.p_father = p_father;
608     p_obj->common.p_first = NULL;
609     p_obj->common.p_next = NULL;
610     p_obj->common.p_last = NULL;
611
612
613     if( p_obj->common.i_object_size < 24 )
614     {
615         msg_Warn( (vlc_object_t*)s, "Found a corrupted asf object (size<24)" );
616         return VLC_EGENERIC;
617     }
618     /* find this object */
619     for( i_index = 0; ; i_index++ )
620     {
621         if( ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
622                      &p_obj->common.i_object_id )||
623             ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
624                      &asf_object_null_guid ) )
625         {
626             break;
627         }
628     }
629     p_obj->common.i_type = ASF_Object_Function[i_index].i_type;
630
631     /* Now load this object */
632     if( ASF_Object_Function[i_index].ASF_ReadObject_function == NULL )
633     {
634         msg_Warn( (vlc_object_t*)s, "Unknown asf object (not loaded)" );
635         i_result = VLC_SUCCESS;
636     }
637     else
638     {
639         /* XXX ASF_ReadObject_function realloc *pp_obj XXX */
640         i_result =
641           (ASF_Object_Function[i_index].ASF_ReadObject_function)( s, p_obj );
642     }
643
644     /* link this object with father */
645     if( p_father )
646     {
647         if( p_father->common.p_first )
648         {
649             p_father->common.p_last->common.p_next = p_obj;
650         }
651         else
652         {
653             p_father->common.p_first = p_obj;
654         }
655         p_father->common.p_last = p_obj;
656     }
657
658     return( i_result );
659 }
660
661 static void ASF_FreeObject( stream_t *s, asf_object_t *p_obj )
662 {
663     int i_index;
664     asf_object_t *p_child;
665
666     if( !p_obj )
667     {
668         return;
669     }
670
671     /* Free all child object */
672     p_child = p_obj->common.p_first;
673     while( p_child )
674     {
675         asf_object_t *p_next;
676         p_next = p_child->common.p_next;
677         ASF_FreeObject( s, p_child );
678         p_child = p_next;
679     }
680
681     /* find this object */
682     for( i_index = 0; ; i_index++ )
683     {
684         if( ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
685                      &p_obj->common.i_object_id )||
686             ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
687                      &asf_object_null_guid ) )
688         {
689             break;
690         }
691     }
692
693     /* Now free this object */
694     if( ASF_Object_Function[i_index].ASF_FreeObject_function == NULL )
695     {
696         msg_Warn( (vlc_object_t*)s,
697                   "Unknown asf object " GUID_FMT,
698                   GUID_PRINT( p_obj->common.i_object_id ) );
699     }
700     else
701     {
702 #ifdef ASF_DEBUG
703         msg_Dbg( (vlc_object_t*)s,
704                   "Free asf object " GUID_FMT,
705                   GUID_PRINT( p_obj->common.i_object_id ) );
706 #endif
707         (ASF_Object_Function[i_index].ASF_FreeObject_function)( p_obj );
708     }
709     free( p_obj );
710     return;
711 }
712
713 /*****************************************************************************
714  * ASF_ReadObjetRoot : parse the entire stream/file
715  *****************************************************************************/
716 asf_object_root_t *ASF_ReadObjectRoot( stream_t *s, int b_seekable )
717 {
718     asf_object_root_t *p_root = malloc( sizeof( asf_object_root_t ) );
719     asf_object_t *p_obj;
720
721     p_root->i_type = ASF_OBJECT_TYPE_ROOT;
722     memcpy( &p_root->i_object_id, &asf_object_null_guid, sizeof( guid_t ) );
723     p_root->i_object_pos = 0;
724     p_root->i_object_size = stream_Tell( s );
725     p_root->p_first = NULL;
726     p_root->p_last  = NULL;
727     p_root->p_next  = NULL;
728     p_root->p_hdr   = NULL;
729     p_root->p_data  = NULL;
730     p_root->p_fp    = NULL;
731     p_root->p_index = NULL;
732
733     for( ; ; )
734     {
735         p_obj  = malloc( sizeof( asf_object_t ) );
736
737         if( ASF_ReadObject( s, p_obj, (asf_object_t*)p_root ) )
738         {
739             break;
740         }
741         switch( p_obj->common.i_type )
742         {
743             case( ASF_OBJECT_TYPE_HEADER ):
744                 p_root->p_hdr = (asf_object_header_t*)p_obj;
745                 break;
746             case( ASF_OBJECT_TYPE_DATA ):
747                 p_root->p_data = (asf_object_data_t*)p_obj;
748                 break;
749             case( ASF_OBJECT_TYPE_INDEX ):
750                 p_root->p_index = (asf_object_index_t*)p_obj;
751                 break;
752             default:
753                 msg_Warn( (vlc_object_t*)s, "Unknow Object found" );
754                 break;
755         }
756         if( p_obj->common.i_type == ASF_OBJECT_TYPE_DATA &&
757             p_obj->common.i_object_size <= 50 )
758         {
759             /* probably a dump of broadcasted asf */
760             break;
761         }
762         if( !b_seekable && p_root->p_hdr && p_root->p_data )
763         {
764             /* For unseekable stream it's enouth to play */
765             break;
766         }
767
768         if( ASF_NextObject( s, p_obj ) ) /* Go to the next object */
769         {
770             break;
771         }
772     }
773
774     if( p_root->p_hdr != NULL && p_root->p_data != NULL )
775     {
776         p_root->p_fp = ASF_FindObject( p_root->p_hdr,
777                                        &asf_object_file_properties_guid, 0 );
778
779         if( p_root->p_fp )
780         {
781             return p_root;
782         }
783         msg_Warn( (vlc_object_t*)s, "cannot fine file properties object" );
784     }
785
786     /* Invalid file */
787     ASF_FreeObjectRoot( s, p_root );
788     return NULL;
789 }
790
791 void ASF_FreeObjectRoot( stream_t *s, asf_object_root_t *p_root )
792 {
793     asf_object_t *p_obj;
794
795     p_obj = p_root->p_first;
796     while( p_obj )
797     {
798         asf_object_t *p_next;
799         p_next = p_obj->common.p_next;
800         ASF_FreeObject( s, p_obj );
801         p_obj = p_next;
802     }
803     free( p_root );
804 }
805
806 int  __ASF_CountObject( asf_object_t *p_obj, const guid_t *p_guid )
807 {
808     int i_count;
809     asf_object_t *p_child;
810
811     if( !p_obj )
812     {
813         return( 0 );
814     }
815
816     i_count = 0;
817     p_child = p_obj->common.p_first;
818     while( p_child )
819     {
820         if( ASF_CmpGUID( &p_child->common.i_object_id, p_guid ) )
821         {
822             i_count++;
823         }
824         p_child = p_child->common.p_next;
825     }
826     return( i_count );
827 }
828
829 void *__ASF_FindObject( asf_object_t *p_obj, const guid_t *p_guid, int i_number )
830 {
831     asf_object_t *p_child;
832
833     p_child = p_obj->common.p_first;
834
835     while( p_child )
836     {
837         if( ASF_CmpGUID( &p_child->common.i_object_id, p_guid ) )
838         {
839             if( i_number == 0 )
840             {
841                 /* We found it */
842                 return( p_child );
843             }
844
845             i_number--;
846         }
847         p_child = p_child->common.p_next;
848     }
849     return( NULL );
850 }
851