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