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