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