]> git.sesse.net Git - vlc/blob - modules/demux/asf/libasf.c
59528d680217ab0c9a1863dd22d60ca0d2f46486
[vlc] / modules / demux / asf / libasf.c
1 /*****************************************************************************
2  * libasf.c :
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: libasf.c,v 1.13 2003/08/17 23:02:52 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 #include <string.h>                                              /* strdup() */
25 #include <errno.h>
26 #include <sys/types.h>
27
28 #include <vlc/vlc.h>
29 #include <vlc/input.h>
30
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 void GetGUID( guid_t *p_guid, uint8_t *p_data )
47 {
48     p_guid->v1 = GetDWLE( p_data );
49     p_guid->v2 = GetWLE( p_data + 4);
50     p_guid->v3 = GetWLE( p_data + 6);
51     memcpy( p_guid->v4, p_data + 8, 8 );
52 }
53
54 int CmpGUID( const guid_t *p_guid1, const guid_t *p_guid2 )
55 {
56     if( (p_guid1->v1 != p_guid2->v1 )||(p_guid1->v2 != p_guid2->v2 )||
57         (p_guid1->v3 != p_guid2->v3 )||
58         ( memcmp( p_guid1->v4, p_guid2->v4,8 )) )
59     {
60         return( 0 );
61     }
62     else
63     {
64         return( 1 ); /* match */
65     }
66 }
67 /*****************************************************************************
68  * Some basic functions to manipulate stream more easily in vlc
69  *
70  * ASF_TellAbsolute get file position
71  *
72  * ASF_SeekAbsolute seek in the file
73  *
74  * ASF_ReadData read data from the file in a buffer
75  *
76  *****************************************************************************/
77 off_t ASF_TellAbsolute( input_thread_t *p_input )
78 {
79     off_t i_pos;
80
81     vlc_mutex_lock( &p_input->stream.stream_lock );
82
83     i_pos= p_input->stream.p_selected_area->i_tell;
84 //           - ( p_input->p_last_data - p_input->p_current_data  );
85
86     vlc_mutex_unlock( &p_input->stream.stream_lock );
87
88     return( i_pos );
89 }
90
91 int ASF_SeekAbsolute( input_thread_t *p_input,
92                       off_t i_pos)
93 {
94     off_t i_filepos;
95
96     i_filepos = ASF_TellAbsolute( p_input );
97     if( i_pos == i_filepos )
98     {
99         return( 1 );
100     }
101
102     if( !p_input->stream.b_seekable && i_pos < i_filepos )
103     {
104         msg_Err( p_input, "cannot seek" );
105         return( 0 );
106     }
107
108     if( p_input->stream.b_seekable &&
109         ( p_input->stream.i_method == INPUT_METHOD_FILE ||
110           i_pos < i_filepos ||
111           i_pos - i_filepos > 10000 ) )
112     {
113         input_AccessReinit( p_input );
114         p_input->pf_seek( p_input, i_pos );
115         return( 1 );
116     }
117     else if( i_pos > i_filepos )
118     {
119         uint64_t i_size = i_pos - i_filepos;
120         do
121         {
122             data_packet_t *p_data;
123             int i_read;
124
125             i_read =
126                 input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
127             if( i_read <= 0 )
128             {
129                 return( 0 );
130             }
131             input_DeletePacket( p_input->p_method_data, p_data );
132             i_size -= i_read;
133
134         } while( i_size > 0 );
135     }
136     return( 1 );
137 }
138
139 /* return 1 if success, 0 if fail */
140 int ASF_ReadData( input_thread_t *p_input, uint8_t *p_buff, int i_size )
141 {
142     data_packet_t *p_data;
143
144     int i_read;
145
146
147     if( !i_size )
148     {
149         return( 1 );
150     }
151
152     do
153     {
154         i_read = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
155         if( i_read <= 0 )
156         {
157             return( 0 );
158         }
159         memcpy( p_buff, p_data->p_payload_start, i_read );
160         input_DeletePacket( p_input->p_method_data, p_data );
161
162         p_buff += i_read;
163         i_size -= i_read;
164
165     } while( i_size );
166
167     return( 1 );
168 }
169
170 int  ASF_SkipBytes( input_thread_t *p_input, int i_count )
171 {
172     return( ASF_SeekAbsolute( p_input,
173                               ASF_TellAbsolute( p_input ) + i_count ) );
174 }
175
176 /****************************************************************************/
177 int  ASF_ReadObjectCommon( input_thread_t *p_input,
178                            asf_object_t *p_obj )
179 {
180     asf_object_common_t *p_common = (asf_object_common_t*)p_obj;
181     uint8_t             *p_peek;
182
183     if( input_Peek( p_input, &p_peek, 24 ) < 24 )
184     {
185         return( 0 );
186     }
187     GetGUID( &p_common->i_object_id, p_peek );
188     p_common->i_object_size = GetQWLE( p_peek + 16 );
189     p_common->i_object_pos = ASF_TellAbsolute( p_input );
190     p_common->p_next = NULL;
191 #ifdef ASF_DEBUG
192     msg_Dbg(p_input,
193             "Found Object guid: " GUID_FMT " size:"I64Fd,
194             GUID_PRINT( p_common->i_object_id ),
195             p_common->i_object_size );
196 #endif
197
198     return( 1 );
199 }
200
201 int ASF_NextObject( input_thread_t *p_input,
202                     asf_object_t *p_obj )
203 {
204     asf_object_t obj;
205     if( !p_obj )
206     {
207         if( !ASF_ReadObjectCommon( p_input, &obj ) )
208         {
209             return( 0 );
210         }
211         p_obj = &obj;
212     }
213
214     if( !p_obj->common.i_object_size )
215     {
216         return( 0 ); /* failed */
217     }
218     if( p_obj->common.p_father && p_obj->common.p_father->common.i_object_size != 0 )
219     {
220         if( p_obj->common.p_father->common.i_object_pos + p_obj->common.p_father->common.i_object_size <
221                 p_obj->common.i_object_pos + p_obj->common.i_object_size + 24 )
222                                 /* 24 is min size of an object */
223         {
224             return( 0 );
225         }
226
227     }
228     return( ASF_SeekAbsolute( p_input,
229                               p_obj->common.i_object_pos + p_obj->common.i_object_size ) );
230 }
231
232 int  ASF_GotoObject( input_thread_t *p_input,
233                      asf_object_t *p_obj )
234 {
235     if( !p_obj )
236     {
237         return( 0 );
238     }
239     return( ASF_SeekAbsolute( p_input, p_obj->common.i_object_pos ) );
240 }
241
242
243 void ASF_FreeObject_Null( input_thread_t *p_input,
244                             asf_object_t *pp_obj )
245 {
246
247
248 }
249
250 int  ASF_ReadObject_Header( input_thread_t *p_input,
251                             asf_object_t *p_obj )
252 {
253     asf_object_header_t *p_hdr = (asf_object_header_t*)p_obj;
254     asf_object_t        *p_subobj;
255     int                 i_peek;
256     uint8_t             *p_peek;
257
258     if( ( i_peek = input_Peek( p_input, &p_peek, 30 ) ) < 30 )
259     {
260        return( 0 );
261     }
262     p_hdr->i_sub_object_count = GetDWLE( p_peek + 24 );
263     p_hdr->i_reserved1 = p_peek[28];
264     p_hdr->i_reserved2 = p_peek[29];
265     p_hdr->p_first = NULL;
266     p_hdr->p_last  = NULL;
267 #ifdef ASF_DEBUG
268     msg_Dbg(p_input,
269             "Read \"Header Object\" subobj:%d, reserved1:%d, reserved2:%d",
270             p_hdr->i_sub_object_count,
271             p_hdr->i_reserved1,
272             p_hdr->i_reserved2 );
273 #endif
274     ASF_SkipBytes( p_input, 30 );
275     /* Now load sub object */
276     for( ; ; )
277     {
278         p_subobj  = malloc( sizeof( asf_object_t ) );
279
280         if( !( ASF_ReadObject( p_input, p_subobj, (asf_object_t*)p_hdr ) ) )
281         {
282             break;
283         }
284         if( !ASF_NextObject( p_input, p_subobj ) ) /* Go to the next object */
285         {
286             break;
287         }
288     }
289     return( 1 );
290 }
291
292 int  ASF_ReadObject_Data( input_thread_t *p_input,
293                           asf_object_t *p_obj )
294 {
295     asf_object_data_t *p_data = (asf_object_data_t*)p_obj;
296     int               i_peek;
297     uint8_t           *p_peek;
298
299     if( ( i_peek = input_Peek( p_input, &p_peek, 50 ) ) < 50 )
300     {
301        return( 0 );
302     }
303     GetGUID( &p_data->i_file_id, p_peek + 24 );
304     p_data->i_total_data_packets = GetQWLE( p_peek + 40 );
305     p_data->i_reserved = GetWLE( p_peek + 48 );
306 #ifdef ASF_DEBUG
307     msg_Dbg( p_input,
308             "Read \"Data Object\" file_id:" GUID_FMT " total data packet:"
309             I64Fd" reserved:%d",
310             GUID_PRINT( p_data->i_file_id ),
311             p_data->i_total_data_packets,
312             p_data->i_reserved );
313 #endif
314     return( 1 );
315 }
316
317 int  ASF_ReadObject_Index( input_thread_t *p_input,
318                            asf_object_t *p_obj )
319 {
320     asf_object_index_t *p_index = (asf_object_index_t*)p_obj;
321     int                i_peek;
322     uint8_t            *p_peek;
323
324     if( ( i_peek = input_Peek( p_input, &p_peek, 56 ) ) < 56 )
325     {
326        return( 0 );
327     }
328     GetGUID( &p_index->i_file_id, p_peek + 24 );
329     p_index->i_index_entry_time_interval = GetQWLE( p_peek + 40 );
330     p_index->i_max_packet_count = GetDWLE( p_peek + 48 );
331     p_index->i_index_entry_count = GetDWLE( p_peek + 52 );
332     p_index->index_entry = NULL; /* FIXME */
333
334 #ifdef ASF_DEBUG
335     msg_Dbg( p_input,
336             "Read \"Index Object\" file_id:" GUID_FMT
337             " index_entry_time_interval:"I64Fd" max_packet_count:%d "
338             "index_entry_count:%ld",
339             GUID_PRINT( p_index->i_file_id ),
340             p_index->i_index_entry_time_interval,
341             p_index->i_max_packet_count,
342             (long int)p_index->i_index_entry_count );
343 #endif
344     return( 1 );
345 }
346 void ASF_FreeObject_Index( input_thread_t *p_input,
347                           asf_object_t *p_obj )
348 {
349     asf_object_index_t *p_index = (asf_object_index_t*)p_obj;
350
351     FREE( p_index->index_entry );
352 }
353
354 int  ASF_ReadObject_file_properties( input_thread_t *p_input,
355                                      asf_object_t *p_obj )
356 {
357     asf_object_file_properties_t *p_fp = (asf_object_file_properties_t*)p_obj;
358     int      i_peek;
359     uint8_t  *p_peek;
360
361     if( ( i_peek = input_Peek( p_input, &p_peek,  92) ) < 92 )
362     {
363        return( 0 );
364     }
365     GetGUID( &p_fp->i_file_id, p_peek + 24 );
366     p_fp->i_file_size = GetQWLE( p_peek + 40 );
367     p_fp->i_creation_date = GetQWLE( p_peek + 48 );
368     p_fp->i_data_packets_count = GetQWLE( p_peek + 56 );
369     p_fp->i_play_duration = GetQWLE( p_peek + 64 );
370     p_fp->i_send_duration = GetQWLE( p_peek + 72 );
371     p_fp->i_preroll = GetQWLE( p_peek + 80 );
372     p_fp->i_flags = GetDWLE( p_peek + 88 );
373     p_fp->i_min_data_packet_size = GetDWLE( p_peek + 92 );
374     p_fp->i_max_data_packet_size = GetDWLE( p_peek + 96 );
375     p_fp->i_max_bitrate = GetDWLE( p_peek + 100 );
376
377 #ifdef ASF_DEBUG
378     msg_Dbg( p_input,
379             "Read \"File Properties Object\" file_id:" GUID_FMT
380             " file_size:"I64Fd" creation_date:"I64Fd" data_packets_count:"
381             I64Fd" play_duration:"I64Fd" send_duration:"I64Fd" preroll:"
382             I64Fd" flags:%d min_data_packet_size:%d max_data_packet_size:%d "
383             "max_bitrate:%d",
384             GUID_PRINT( p_fp->i_file_id ),
385             p_fp->i_file_size,
386             p_fp->i_creation_date,
387             p_fp->i_data_packets_count,
388             p_fp->i_play_duration,
389             p_fp->i_send_duration,
390             p_fp->i_preroll,
391             p_fp->i_flags,
392             p_fp->i_min_data_packet_size,
393             p_fp->i_max_data_packet_size,
394             p_fp->i_max_bitrate );
395 #endif
396     return( 1 );
397 }
398
399 int  ASF_ReadObject_header_extention( input_thread_t *p_input,
400                                       asf_object_t *p_obj )
401 {
402     asf_object_header_extention_t *p_he = (asf_object_header_extention_t*)p_obj;
403     int     i_peek;
404     uint8_t *p_peek;
405
406     if( ( i_peek = input_Peek( p_input, &p_peek, p_he->i_object_size ) ) <  46)
407     {
408        return( 0 );
409     }
410     GetGUID( &p_he->i_reserved1, p_peek + 24 );
411     p_he->i_reserved2 = GetWLE( p_peek + 40 );
412     p_he->i_header_extention_size = GetDWLE( p_peek + 42 );
413     if( p_he->i_header_extention_size )
414     {
415         p_he->p_header_extention_data = malloc( p_he->i_header_extention_size );
416         memcpy( p_he->p_header_extention_data,
417                 p_peek + 46,
418                 p_he->i_header_extention_size );
419     }
420     else
421     {
422         p_he->p_header_extention_data = NULL;
423     }
424 #ifdef ASF_DEBUG
425     msg_Dbg( p_input,
426             "Read \"Header Extention Object\" reserved1:" GUID_FMT " reserved2:%d header_extention_size:%d",
427             GUID_PRINT( p_he->i_reserved1 ),
428             p_he->i_reserved2,
429             p_he->i_header_extention_size );
430 #endif
431     return( 1 );
432 }
433 void ASF_FreeObject_header_extention( input_thread_t *p_input,
434                                       asf_object_t *p_obj )
435 {
436     asf_object_header_extention_t *p_he = (asf_object_header_extention_t*)p_obj;
437
438     FREE( p_he->p_header_extention_data );
439 }
440
441 int  ASF_ReadObject_stream_properties( input_thread_t *p_input,
442                                        asf_object_t *p_obj )
443 {
444     asf_object_stream_properties_t *p_sp =
445                     (asf_object_stream_properties_t*)p_obj;
446     int     i_peek;
447     uint8_t *p_peek;
448
449     if( ( i_peek = input_Peek( p_input, &p_peek,  p_sp->i_object_size ) ) < 74 )
450     {
451        return( 0 );
452     }
453     GetGUID( &p_sp->i_stream_type, p_peek + 24 );
454     GetGUID( &p_sp->i_error_correction_type, p_peek + 40 );
455     p_sp->i_time_offset = GetQWLE( p_peek + 56 );
456     p_sp->i_type_specific_data_length = GetDWLE( p_peek + 64 );
457     p_sp->i_error_correction_data_length = GetDWLE( p_peek + 68 );
458     p_sp->i_flags = GetWLE( p_peek + 72 );
459         p_sp->i_stream_number = p_sp->i_flags&0x07f;
460     p_sp->i_reserved = GetDWLE( p_peek + 74 );
461     if( p_sp->i_type_specific_data_length )
462     {
463         p_sp->p_type_specific_data = malloc( p_sp->i_type_specific_data_length );
464         memcpy( p_sp->p_type_specific_data,
465                 p_peek + 78,
466                 p_sp->i_type_specific_data_length );
467     }
468     else
469     {
470         p_sp->p_type_specific_data = NULL;
471     }
472     if( p_sp->i_error_correction_data_length )
473     {
474         p_sp->p_error_correction_data = malloc( p_sp->i_error_correction_data_length );
475         memcpy( p_sp->p_error_correction_data,
476                 p_peek + 78 + p_sp->i_type_specific_data_length,
477                 p_sp->i_error_correction_data_length );
478     }
479     else
480     {
481         p_sp->p_error_correction_data = NULL;
482     }
483
484 #ifdef ASF_DEBUG
485     msg_Dbg( p_input,
486             "Read \"Stream Properties Object\" stream_type:" GUID_FMT
487             " error_correction_type:" GUID_FMT " time_offset:"I64Fd
488             " type_specific_data_length:%d error_correction_data_length:%d"
489             " flags:0x%x stream_number:%d",
490             GUID_PRINT( p_sp->i_stream_type ),
491             GUID_PRINT( p_sp->i_error_correction_type ),
492             p_sp->i_time_offset,
493             p_sp->i_type_specific_data_length,
494             p_sp->i_error_correction_data_length,
495             p_sp->i_flags,
496             p_sp->i_stream_number );
497
498 #endif
499     return( 1 );
500 }
501
502 void ASF_FreeObject_stream_properties( input_thread_t *p_input,
503                                       asf_object_t *p_obj )
504 {
505     asf_object_stream_properties_t *p_sp =
506                 (asf_object_stream_properties_t*)p_obj;
507
508     FREE( p_sp->p_type_specific_data );
509     FREE( p_sp->p_error_correction_data );
510 }
511
512
513 int  ASF_ReadObject_codec_list( input_thread_t *p_input,
514                                 asf_object_t *p_obj )
515 {
516     asf_object_codec_list_t *p_cl = (asf_object_codec_list_t*)p_obj;
517     int     i_peek;
518     uint8_t *p_peek, *p_data;
519
520     unsigned int i_codec;
521
522     if( ( i_peek = input_Peek( p_input, &p_peek, p_cl->i_object_size ) ) < 44 )
523     {
524        return( 0 );
525     }
526
527     GetGUID( &p_cl->i_reserved, p_peek + 24 );
528     p_cl->i_codec_entries_count = GetWLE( p_peek + 40 );
529     if( p_cl->i_codec_entries_count > 0 )
530     {
531
532         p_cl->codec = calloc( p_cl->i_codec_entries_count, sizeof( asf_codec_entry_t ) );
533         memset( p_cl->codec, 0, p_cl->i_codec_entries_count * sizeof( asf_codec_entry_t ) );
534
535         p_data = p_peek + 44;
536         for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
537         {
538 #define codec p_cl->codec[i_codec]
539             int i_len, i;
540
541             codec.i_type = GetWLE( p_data ); p_data += 2;
542             /* codec name */
543             i_len = GetWLE( p_data ); p_data += 2;
544             codec.psz_name = calloc( sizeof( char ), i_len + 1);
545             for( i = 0; i < i_len; i++ )
546             {
547                 codec.psz_name[i] = GetWLE( p_data + 2*i );
548             }
549             codec.psz_name[i_len] = '\0';
550             p_data += 2 * i_len;
551
552             /* description */
553             i_len = GetWLE( p_data ); p_data += 2;
554             codec.psz_description = calloc( sizeof( char ), i_len + 1);
555             for( i = 0; i < i_len; i++ )
556             {
557                 codec.psz_description[i] = GetWLE( p_data + 2*i );
558             }
559             codec.psz_description[i_len] = '\0';
560             p_data += 2 * i_len;
561
562             /* opaque information */
563             codec.i_information_length = GetWLE( p_data ); p_data += 2;
564             if( codec.i_information_length > 0 )
565             {
566                 codec.p_information = malloc( codec.i_information_length );
567                 memcpy( codec.p_information, p_data, codec.i_information_length );
568                 p_data += codec.i_information_length;
569             }
570             else
571             {
572                 codec.p_information = NULL;
573             }
574 #undef  codec
575         }
576
577     }
578     else
579     {
580         p_cl->codec = NULL;
581     }
582
583 #ifdef ASF_DEBUG
584     msg_Dbg( p_input,
585             "Read \"Codec List Object\" reserved_guid:" GUID_FMT " codec_entries_count:%d",
586             GUID_PRINT( p_cl->i_reserved ),
587             p_cl->i_codec_entries_count );
588     for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
589     {
590         char psz_cat[sizeof("Stream ")+10];
591         input_info_category_t *p_cat;
592         sprintf( psz_cat, "Stream %d", i_codec );
593         p_cat = input_InfoCategory( p_input, psz_cat);
594
595 #define codec p_cl->codec[i_codec]
596         input_AddInfo( p_cat, _("Codec name"), codec.psz_name );
597         input_AddInfo( p_cat, _("Codec description"), codec.psz_description );
598         msg_Dbg( p_input,
599                  "Read \"Codec List Object\" codec[%d] %s name:\"%s\" description:\"%s\" information_length:%d",
600                  i_codec,
601                  ( codec.i_type == ASF_CODEC_TYPE_VIDEO ) ? "video" : ( ( codec.i_type == ASF_CODEC_TYPE_AUDIO ) ? "audio" : "unknown" ),
602                  codec.psz_name,
603                  codec.psz_description,
604                  codec.i_information_length );
605
606 #undef  codec
607     }
608 #endif
609     return( 1 );
610 }
611 void ASF_FreeObject_codec_list( input_thread_t *p_input,
612                                 asf_object_t *p_obj )
613 {
614     asf_object_codec_list_t *p_cl = (asf_object_codec_list_t*)p_obj;
615     unsigned int i_codec;
616
617     for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
618     {
619 #define codec p_cl->codec[i_codec]
620         FREE( codec.psz_name );
621         FREE( codec.psz_description );
622         FREE( codec.p_information );
623
624 #undef  codec
625     }
626     FREE( p_cl->codec );
627 }
628
629 /* Microsoft should qo to hell. This time the length give number of bytes
630  * and for the some others object, length give char16 count ... */
631 int  ASF_ReadObject_content_description( input_thread_t *p_input,
632                                          asf_object_t *p_obj )
633 {
634     asf_object_content_description_t *p_cd =
635                                     (asf_object_content_description_t*)p_obj;
636     int     i_peek;
637     uint8_t *p_peek, *p_data;
638
639     int i_len;
640     int i_title;
641     int i_author;
642     int i_copyright;
643     int i_description;
644     int i_rating;
645
646 #define GETSTRINGW( psz_str, i_size ) \
647    psz_str = calloc( i_size/2 + 1, sizeof( char ) ); \
648    for( i_len = 0; i_len < i_size/2; i_len++ ) \
649    { \
650        psz_str[i_len] = GetWLE( p_data + 2*i_len ); \
651    } \
652    psz_str[i_size/2] = '\0'; \
653    p_data += i_size;
654
655     if( ( i_peek = input_Peek( p_input, &p_peek, p_cd->i_object_size ) ) < 34 )
656     {
657        return( 0 );
658     }
659     p_data = p_peek + 24;
660
661     i_title = GetWLE( p_data ); p_data += 2;
662     i_author= GetWLE( p_data ); p_data += 2;
663     i_copyright     = GetWLE( p_data ); p_data += 2;
664     i_description   = GetWLE( p_data ); p_data += 2;
665     i_rating        = GetWLE( p_data ); p_data += 2;
666
667     GETSTRINGW( p_cd->psz_title, i_title );
668     GETSTRINGW( p_cd->psz_author, i_author );
669     GETSTRINGW( p_cd->psz_copyright, i_copyright );
670     GETSTRINGW( p_cd->psz_description, i_description );
671     GETSTRINGW( p_cd->psz_rating, i_rating );
672
673 #undef  GETSTRINGW
674
675 #ifdef ASF_DEBUG
676     {
677         input_info_category_t *p_cat = input_InfoCategory( p_input, _("Asf") );
678         input_AddInfo( p_cat, _("Title"), p_cd->psz_title );
679         input_AddInfo( p_cat, _("Author"), p_cd->psz_author );
680         input_AddInfo( p_cat, _("Copyright"), p_cd->psz_copyright );
681         input_AddInfo( p_cat, _("Description"), p_cd->psz_description );
682         input_AddInfo( p_cat, _("Rating"), p_cd->psz_rating );
683     }
684     msg_Dbg( p_input,
685              "Read \"Content Description Object\" title:\"%s\" author:\"%s\" copyright:\"%s\" description:\"%s\" rating:\"%s\"",
686              p_cd->psz_title,
687              p_cd->psz_author,
688              p_cd->psz_copyright,
689              p_cd->psz_description,
690              p_cd->psz_rating );
691 #endif
692     return( 1 );
693 }
694
695 void ASF_FreeObject_content_description( input_thread_t *p_input,
696                                          asf_object_t *p_obj )
697 {
698     asf_object_content_description_t *p_cd = (asf_object_content_description_t*)p_obj;
699
700     FREE( p_cd->psz_title );
701     FREE( p_cd->psz_author );
702     FREE( p_cd->psz_copyright );
703     FREE( p_cd->psz_description );
704     FREE( p_cd->psz_rating );
705 }
706
707 static struct
708 {
709     const guid_t  *p_id;
710     int     i_type;
711     int     (*ASF_ReadObject_function)( input_thread_t *p_input,
712                                         asf_object_t *p_obj );
713     void    (*ASF_FreeObject_function)( input_thread_t *p_input,
714                                         asf_object_t *p_obj );
715 } ASF_Object_Function [] =
716 {
717     { &asf_object_header_guid,            ASF_OBJECT_TYPE_HEADER,             ASF_ReadObject_Header, ASF_FreeObject_Null },
718     { &asf_object_data_guid,              ASF_OBJECT_TYPE_DATA,               ASF_ReadObject_Data,   ASF_FreeObject_Null },
719     { &asf_object_index_guid,             ASF_OBJECT_TYPE_INDEX,              ASF_ReadObject_Index,  ASF_FreeObject_Index },
720     { &asf_object_file_properties_guid,   ASF_OBJECT_TYPE_FILE_PROPERTIES,    ASF_ReadObject_file_properties,  ASF_FreeObject_Null },
721     { &asf_object_stream_properties_guid, ASF_OBJECT_TYPE_STREAM_PROPERTIES,  ASF_ReadObject_stream_properties,ASF_FreeObject_stream_properties },
722     { &asf_object_header_extention_guid,  ASF_OBJECT_TYPE_EXTENTION_HEADER,   ASF_ReadObject_header_extention, ASF_FreeObject_header_extention},
723     { &asf_object_codec_list_guid,        ASF_OBJECT_TYPE_CODEC_LIST,         ASF_ReadObject_codec_list,       ASF_FreeObject_codec_list },
724     { &asf_object_marker_guid,            ASF_OBJECT_TYPE_MARKER,             NULL,                  NULL },
725     { &asf_object_content_description_guid, ASF_OBJECT_TYPE_CONTENT_DESCRIPTION, ASF_ReadObject_content_description, ASF_FreeObject_content_description },
726
727     { &asf_object_null_guid,   0,                      NULL,                  NULL }
728 };
729
730 int  ASF_ReadObject( input_thread_t *p_input,
731                      asf_object_t *p_obj,
732                      asf_object_t *p_father )
733 {
734     int i_result;
735     int i_index;
736
737     if( !p_obj )
738     {
739         return( 0 );
740     }
741     if( !ASF_ReadObjectCommon( p_input, p_obj ) )
742     {
743         msg_Warn( p_input, "Cannot read one asf object" );
744         return( 0 );
745     }
746     p_obj->common.p_father = p_father;
747     p_obj->common.p_first = NULL;
748     p_obj->common.p_next = NULL;
749     p_obj->common.p_last = NULL;
750
751
752     if( p_obj->common.i_object_size < 24 )
753     {
754         msg_Warn( p_input, "Found a corrupted asf object (size<24)" );
755         return( 0 );
756     }
757     /* find this object */
758     for( i_index = 0; ; i_index++ )
759     {
760         if( CmpGUID( ASF_Object_Function[i_index].p_id,
761                      &p_obj->common.i_object_id )||
762             CmpGUID( ASF_Object_Function[i_index].p_id,
763                      &asf_object_null_guid ) )
764         {
765             break;
766         }
767     }
768     p_obj->common.i_type = ASF_Object_Function[i_index].i_type;
769
770     /* Now load this object */
771     if( ASF_Object_Function[i_index].ASF_ReadObject_function == NULL )
772     {
773         msg_Warn( p_input, "Unknown asf object (not loaded)" );
774         i_result = 1;
775     }
776     else
777     {
778         /* XXX ASF_ReadObject_function realloc *pp_obj XXX */
779         i_result =
780             (ASF_Object_Function[i_index].ASF_ReadObject_function)( p_input,
781                                                                     p_obj );
782     }
783
784     /* link this object with father */
785     if( p_father )
786     {
787         if( p_father->common.p_first )
788         {
789             p_father->common.p_last->common.p_next = p_obj;
790         }
791         else
792         {
793             p_father->common.p_first = p_obj;
794         }
795         p_father->common.p_last = p_obj;
796     }
797
798     return( i_result );
799 }
800
801 void ASF_FreeObject( input_thread_t *p_input,
802                      asf_object_t *p_obj )
803 {
804     int i_index;
805     asf_object_t *p_child;
806
807     if( !p_obj )
808     {
809         return;
810     }
811
812     /* Free all child object */
813     p_child = p_obj->common.p_first;
814     while( p_child )
815     {
816         asf_object_t *p_next;
817         p_next = p_child->common.p_next;
818         ASF_FreeObject( p_input, p_child );
819         p_child = p_next;
820     }
821
822     /* find this object */
823     for( i_index = 0; ; i_index++ )
824     {
825         if( CmpGUID( ASF_Object_Function[i_index].p_id,
826                      &p_obj->common.i_object_id )||
827             CmpGUID( ASF_Object_Function[i_index].p_id,
828                      &asf_object_null_guid ) )
829         {
830             break;
831         }
832     }
833
834     /* Now free this object */
835     if( ASF_Object_Function[i_index].ASF_FreeObject_function == NULL )
836     {
837         msg_Warn( p_input,
838                   "Unknown asf object " GUID_FMT,
839                   GUID_PRINT( p_obj->common.i_object_id ) );
840     }
841     else
842     {
843 #ifdef ASF_DEBUG
844         msg_Dbg( p_input,
845                   "Free asf object " GUID_FMT,
846                   GUID_PRINT( p_obj->common.i_object_id ) );
847 #endif
848         (ASF_Object_Function[i_index].ASF_FreeObject_function)( p_input,
849                                                                 p_obj );
850     }
851     free( p_obj );
852     return;
853 }
854
855 /*****************************************************************************
856  * ASF_ReadObjetRoot : parse the entire stream/file
857  *****************************************************************************/
858 int ASF_ReadObjectRoot( input_thread_t *p_input,
859                         asf_object_root_t *p_root,
860                         int b_seekable )
861 {
862     asf_object_t *p_obj;
863
864     p_root->i_type = ASF_OBJECT_TYPE_ROOT;
865     memcpy( &p_root->i_object_id, &asf_object_null_guid, sizeof( guid_t ) );
866     p_root->i_object_pos = 0;
867     p_root->i_object_size = p_input->stream.p_selected_area->i_size;
868     p_root->p_first = NULL;
869     p_root->p_last = NULL;
870     p_root->p_next = NULL;
871     p_root->p_hdr = NULL;
872     p_root->p_data = NULL;
873     p_root->p_index = NULL;
874
875     for( ; ; )
876     {
877         p_obj  = malloc( sizeof( asf_object_t ) );
878
879         if( !( ASF_ReadObject( p_input, p_obj, (asf_object_t*)p_root ) ) )
880         {
881             return( 1 );
882         }
883         switch( p_obj->common.i_type )
884         {
885             case( ASF_OBJECT_TYPE_HEADER ):
886                 p_root->p_hdr = (asf_object_header_t*)p_obj;
887                 break;
888             case( ASF_OBJECT_TYPE_DATA ):
889                 p_root->p_data = (asf_object_data_t*)p_obj;
890                 break;
891             case( ASF_OBJECT_TYPE_INDEX ):
892                 p_root->p_index = (asf_object_index_t*)p_obj;
893                 break;
894             default:
895                 msg_Warn( p_input, "Unknow Object found" );
896                 break;
897         }
898         if( !b_seekable && ( p_root->p_hdr && p_root->p_data ) )
899         {
900             /* For unseekable stream it's enouth to play */
901             return( 1 );
902         }
903
904         if( !ASF_NextObject( p_input, p_obj ) ) /* Go to the next object */
905         {
906             return( 1 );
907         }
908     }
909 }
910
911 void ASF_FreeObjectRoot( input_thread_t *p_input,
912                          asf_object_root_t *p_root )
913 {
914     asf_object_t *p_obj;
915
916     p_obj = p_root->p_first;
917     while( p_obj )
918     {
919         asf_object_t *p_next;
920         p_next = p_obj->common.p_next;
921         ASF_FreeObject( p_input, p_obj );
922         p_obj = p_next;
923     }
924     p_root->p_first = NULL;
925     p_root->p_last = NULL;
926     p_root->p_next = NULL;
927
928     p_root->p_hdr = NULL;
929     p_root->p_data = NULL;
930     p_root->p_index = NULL;
931
932 }
933
934 int  __ASF_CountObject( asf_object_t *p_obj, const guid_t *p_guid )
935 {
936     int i_count;
937     asf_object_t *p_child;
938
939     if( !p_obj )
940     {
941         return( 0 );
942     }
943
944     i_count = 0;
945     p_child = p_obj->common.p_first;
946     while( p_child )
947     {
948         if( CmpGUID( &p_child->common.i_object_id, p_guid ) )
949         {
950             i_count++;
951         }
952         p_child = p_child->common.p_next;
953     }
954     return( i_count );
955 }
956
957 void *__ASF_FindObject( asf_object_t *p_obj, const guid_t *p_guid, int i_number )
958 {
959     asf_object_t *p_child;
960
961     p_child = p_obj->common.p_first;
962
963     while( p_child )
964     {
965         if( CmpGUID( &p_child->common.i_object_id, p_guid ) )
966         {
967             if( i_number == 0 )
968             {
969                 /* We found it */
970                 return( p_child );
971             }
972
973             i_number--;
974         }
975         p_child = p_child->common.p_next;
976     }
977     return( NULL );
978 }
979
980