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