libasf.c 48.7 KB
Newer Older
1
/*****************************************************************************
2
 * libasf.c : asf stream demux module for vlc
3
 *****************************************************************************
hartman's avatar
hartman committed
4
 * Copyright (C) 2001-2003 VideoLAN
5
 * $Id$
6
 *
7
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8
 *          Gildas Bazin <gbazin@videolan.org>
9
 *
10
11
12
13
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
14
 *
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#include <stdlib.h>                                      /* malloc(), free() */

#include <vlc/vlc.h>
#include <vlc/input.h>

Laurent Aimar's avatar
Laurent Aimar committed
30
#include "codecs.h"                        /* BITMAPINFOHEADER, WAVEFORMATEX */
31
32
33
34
35
36
#include "libasf.h"

#define ASF_DEBUG 1

#define FREE( p ) \
    if( p ) {free( p ); p = NULL; }
37

38
39
40
41
42
43
44
45
#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"
#define GUID_PRINT( guid )  \
    (guid).v1,              \
    (guid).v2,              \
    (guid).v3,              \
    (guid).v4[0],(guid).v4[1],(guid).v4[2],(guid).v4[3],    \
    (guid).v4[4],(guid).v4[5],(guid).v4[6],(guid).v4[7]

46
47
48
/****************************************************************************
 *
 ****************************************************************************/
49
static int ASF_ReadObject( stream_t *, asf_object_t *,  asf_object_t * );
50
51
52
53

/****************************************************************************
 * GUID functions
 ****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
54
void ASF_GetGUID( guid_t *p_guid, uint8_t *p_data )
55
56
57
58
59
60
61
{
    p_guid->v1 = GetDWLE( p_data );
    p_guid->v2 = GetWLE( p_data + 4);
    p_guid->v3 = GetWLE( p_data + 6);
    memcpy( p_guid->v4, p_data + 8, 8 );
}

Laurent Aimar's avatar
Laurent Aimar committed
62
int ASF_CmpGUID( const guid_t *p_guid1, const guid_t *p_guid2 )
63
{
64
65
    if( (p_guid1->v1 != p_guid2->v1 )||
        (p_guid1->v2 != p_guid2->v2 )||
66
67
68
69
70
        (p_guid1->v3 != p_guid2->v3 )||
        ( memcmp( p_guid1->v4, p_guid2->v4,8 )) )
    {
        return( 0 );
    }
71
    return( 1 ); /* match */
72
73
}

74
75
76
77
/****************************************************************************
 *
 ****************************************************************************/
static int ASF_ReadObjectCommon( stream_t *s, asf_object_t *p_obj )
78
79
{
    asf_object_common_t *p_common = (asf_object_common_t*)p_obj;
80
    uint8_t             *p_peek;
81

82
    if( stream_Peek( s, &p_peek, 24 ) < 24 )
83
    {
84
        return( VLC_EGENERIC );
85
    }
Laurent Aimar's avatar
Laurent Aimar committed
86
    ASF_GetGUID( &p_common->i_object_id, p_peek );
87
    p_common->i_object_size = GetQWLE( p_peek + 16 );
88
    p_common->i_object_pos  = stream_Tell( s );
89
    p_common->p_next = NULL;
90

91
#ifdef ASF_DEBUG
92
    msg_Dbg( s,
hartman's avatar
hartman committed
93
             "found object guid: " GUID_FMT " size:"I64Fd,
94
95
             GUID_PRINT( p_common->i_object_id ),
             p_common->i_object_size );
96
#endif
97

98
    return( VLC_SUCCESS );
99
100
}

101
static int ASF_NextObject( stream_t *s, asf_object_t *p_obj )
102
103
{
    asf_object_t obj;
104
    if( p_obj == NULL )
105
    {
106
        if( ASF_ReadObjectCommon( s, &obj ) )
107
        {
108
            return( VLC_EGENERIC );
109
110
111
112
        }
        p_obj = &obj;
    }

113
    if( p_obj->common.i_object_size <= 0 )
114
    {
115
        return VLC_EGENERIC;
116
    }
117
118
    if( p_obj->common.p_father &&
        p_obj->common.p_father->common.i_object_size != 0 )
119
    {
120
121
        if( p_obj->common.p_father->common.i_object_pos +
            p_obj->common.p_father->common.i_object_size <
122
                p_obj->common.i_object_pos + p_obj->common.i_object_size + 24 )
123
124
                                /* 24 is min size of an object */
        {
125
            return VLC_EGENERIC;
126
127
128
129
        }

    }

130
131
    return stream_Seek( s, p_obj->common.i_object_pos +
                        p_obj->common.i_object_size );
132
133
}

134
static void ASF_FreeObject_Null( asf_object_t *pp_obj )
135
{
136
    return;
137
138
}

139
static int  ASF_ReadObject_Header( stream_t *s, asf_object_t *p_obj )
140
141
{
    asf_object_header_t *p_hdr = (asf_object_header_t*)p_obj;
142
143
144
    asf_object_t        *p_subobj;
    int                 i_peek;
    uint8_t             *p_peek;
145

146
    if( ( i_peek = stream_Peek( s, &p_peek, 30 ) ) < 30 )
147
    {
148
       return( VLC_EGENERIC );
149
    }
150

151
152
153
154
155
    p_hdr->i_sub_object_count = GetDWLE( p_peek + 24 );
    p_hdr->i_reserved1 = p_peek[28];
    p_hdr->i_reserved2 = p_peek[29];
    p_hdr->p_first = NULL;
    p_hdr->p_last  = NULL;
156

157
#ifdef ASF_DEBUG
158
    msg_Dbg( s,
hartman's avatar
hartman committed
159
             "read \"header object\" subobj:%d, reserved1:%d, reserved2:%d",
160
161
162
             p_hdr->i_sub_object_count,
             p_hdr->i_reserved1,
             p_hdr->i_reserved2 );
163
#endif
164
165

    /* Cannot fail as peek succeed */
166
167
    stream_Read( s, NULL, 30 );

168
169
170
    /* Now load sub object */
    for( ; ; )
    {
171
        p_subobj = malloc( sizeof( asf_object_t ) );
172

173
        if( ASF_ReadObject( s, p_subobj, (asf_object_t*)p_hdr ) )
174
        {
175
            free( p_subobj );
176
            break;
177
        }
178
        if( ASF_NextObject( s, p_subobj ) ) /* Go to the next object */
179
180
181
182
        {
            break;
        }
    }
183
    return VLC_SUCCESS;
184
185
}

186
static int ASF_ReadObject_Data( stream_t *s, asf_object_t *p_obj )
187
188
{
    asf_object_data_t *p_data = (asf_object_data_t*)p_obj;
189
190
    int               i_peek;
    uint8_t           *p_peek;
191

192
    if( ( i_peek = stream_Peek( s, &p_peek, 50 ) ) < 50 )
193
    {
194
       return VLC_EGENERIC;
195
    }
Laurent Aimar's avatar
Laurent Aimar committed
196
    ASF_GetGUID( &p_data->i_file_id, p_peek + 24 );
197
198
    p_data->i_total_data_packets = GetQWLE( p_peek + 40 );
    p_data->i_reserved = GetWLE( p_peek + 48 );
199

200
#ifdef ASF_DEBUG
201
    msg_Dbg( s,
hartman's avatar
hartman committed
202
             "read \"data object\" file_id:" GUID_FMT " total data packet:"
203
204
205
206
             I64Fd" reserved:%d",
             GUID_PRINT( p_data->i_file_id ),
             p_data->i_total_data_packets,
             p_data->i_reserved );
207
#endif
208

209
    return VLC_SUCCESS;
210
211
}

212
static int ASF_ReadObject_Index( stream_t *s, asf_object_t *p_obj )
213
214
{
    asf_object_index_t *p_index = (asf_object_index_t*)p_obj;
215
216
    int                i_peek;
    uint8_t            *p_peek;
217

218
    if( ( i_peek = stream_Peek( s, &p_peek, 56 ) ) < 56 )
219
    {
220
       return VLC_EGENERIC;
221
    }
Laurent Aimar's avatar
Laurent Aimar committed
222
    ASF_GetGUID( &p_index->i_file_id, p_peek + 24 );
223
224
225
226
227
228
    p_index->i_index_entry_time_interval = GetQWLE( p_peek + 40 );
    p_index->i_max_packet_count = GetDWLE( p_peek + 48 );
    p_index->i_index_entry_count = GetDWLE( p_peek + 52 );
    p_index->index_entry = NULL; /* FIXME */

#ifdef ASF_DEBUG
229
    msg_Dbg( s,
hartman's avatar
hartman committed
230
            "read \"index object\" file_id:" GUID_FMT
gbazin's avatar
   
gbazin committed
231
            " index_entry_time_interval:"I64Fd" max_packet_count:%d "
232
            "index_entry_count:%ld",
233
            GUID_PRINT( p_index->i_file_id ),
234
            p_index->i_index_entry_time_interval,
235
            p_index->i_max_packet_count,
236
            (long int)p_index->i_index_entry_count );
237
#endif
238

239
    return VLC_SUCCESS;
240
}
241

242
static void ASF_FreeObject_Index( asf_object_t *p_obj )
243
244
245
246
247
248
{
    asf_object_index_t *p_index = (asf_object_index_t*)p_obj;

    FREE( p_index->index_entry );
}

249
static int ASF_ReadObject_file_properties( stream_t *s, asf_object_t *p_obj )
250
251
{
    asf_object_file_properties_t *p_fp = (asf_object_file_properties_t*)p_obj;
252
253
    int      i_peek;
    uint8_t  *p_peek;
254

255
    if( ( i_peek = stream_Peek( s, &p_peek,  92) ) < 92 )
256
    {
257
       return VLC_EGENERIC;
258
    }
Laurent Aimar's avatar
Laurent Aimar committed
259
    ASF_GetGUID( &p_fp->i_file_id, p_peek + 24 );
260
261
262
263
264
265
266
267
268
269
    p_fp->i_file_size = GetQWLE( p_peek + 40 );
    p_fp->i_creation_date = GetQWLE( p_peek + 48 );
    p_fp->i_data_packets_count = GetQWLE( p_peek + 56 );
    p_fp->i_play_duration = GetQWLE( p_peek + 64 );
    p_fp->i_send_duration = GetQWLE( p_peek + 72 );
    p_fp->i_preroll = GetQWLE( p_peek + 80 );
    p_fp->i_flags = GetDWLE( p_peek + 88 );
    p_fp->i_min_data_packet_size = GetDWLE( p_peek + 92 );
    p_fp->i_max_data_packet_size = GetDWLE( p_peek + 96 );
    p_fp->i_max_bitrate = GetDWLE( p_peek + 100 );
270

271
#ifdef ASF_DEBUG
272
    msg_Dbg( s,
hartman's avatar
hartman committed
273
            "read \"file properties object\" file_id:" GUID_FMT
gbazin's avatar
   
gbazin committed
274
275
276
277
            " file_size:"I64Fd" creation_date:"I64Fd" data_packets_count:"
            I64Fd" play_duration:"I64Fd" send_duration:"I64Fd" preroll:"
            I64Fd" flags:%d min_data_packet_size:%d max_data_packet_size:%d "
            "max_bitrate:%d",
278
279
280
281
282
            GUID_PRINT( p_fp->i_file_id ), p_fp->i_file_size,
            p_fp->i_creation_date, p_fp->i_data_packets_count,
            p_fp->i_play_duration, p_fp->i_send_duration,
            p_fp->i_preroll, p_fp->i_flags,
            p_fp->i_min_data_packet_size, p_fp->i_max_data_packet_size,
283
284
            p_fp->i_max_bitrate );
#endif
285

286
    return VLC_SUCCESS;
287
288
}

289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
static void ASF_FreeObject_metadata( asf_object_t *p_obj )
{
    asf_object_metadata_t *p_meta =
        (asf_object_metadata_t *)p_obj;
    unsigned int i;

    for( i = 0; i < p_meta->i_record_entries_count; i++ )
    {
        if( p_meta->record[i].psz_name ) free( p_meta->record[i].psz_name );
        if( p_meta->record[i].p_data ) free( p_meta->record[i].p_data );
    }
    if( p_meta->record ) free( p_meta->record );
}

static int ASF_ReadObject_metadata( stream_t *s, asf_object_t *p_obj )
304
{
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
    asf_object_metadata_t *p_meta =
        (asf_object_metadata_t *)p_obj;

    int i_peek, i_entries, i;
    uint8_t *p_peek;

    p_meta->i_record_entries_count = 0;
    p_meta->record = 0;

    if( stream_Peek( s, &p_peek, p_meta->i_object_size ) <
        (int)p_meta->i_object_size )
    {
       return VLC_EGENERIC;
    }

    i_peek = 24;
    i_entries = GetWLE( p_peek + i_peek ); i_peek += 2;
    for( i = 0; i < i_entries && i_peek < (int)p_meta->i_object_size -12; i++ )
    {
        asf_metadata_record_t record;
        int i_name, i_data, j;

        if( GetWLE( p_peek + i_peek ) != 0 )
        {
            ASF_FreeObject_metadata( p_obj );
            return VLC_EGENERIC;
        }

        i_peek += 2;
        record.i_stream = GetWLE( p_peek + i_peek ); i_peek += 2;
        i_name = GetWLE( p_peek + i_peek ); i_peek += 2;
        record.i_type = GetWLE( p_peek + i_peek ); i_peek += 2;
        i_data = GetDWLE( p_peek + i_peek ); i_peek += 4;

        if( record.i_type > ASF_METADATA_TYPE_WORD ||
            i_peek + i_data + i_name > (int)p_meta->i_object_size )
        {
            ASF_FreeObject_metadata( p_obj );
            return VLC_EGENERIC;
        }

        record.i_val = 0;
        record.i_data = 0;
        record.p_data = 0;

        /* get name */
351
        record.psz_name = malloc( i_name/2 + 1 );
352
353
354
355
        for( j = 0; j < i_name/2; j++ )
        {
            record.psz_name[j] = GetWLE( p_peek + i_peek ); i_peek += 2;
        }
356
        record.psz_name[j] = 0; /* just to make sure */
357
358
359
360

        /* get data */
        if( record.i_type == ASF_METADATA_TYPE_STRING )
        {
361
            record.p_data = malloc( i_data/2 + 1 );
362
363
364
365
366
            record.i_data = i_data/2;
            for( j = 0; j < i_data/2; j++ )
            {
                record.p_data[j] = GetWLE( p_peek + i_peek ); i_peek += 2;
            }
367
            record.p_data[j] = 0; /* just to make sure */
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
        }
        else if( record.i_type == ASF_METADATA_TYPE_BYTE )
        {
            record.p_data = malloc( i_data );
            record.i_data = i_data;
            memcpy( record.p_data, p_peek + i_peek, i_data );
            p_peek += i_data;
        }
        else
        {
            if( record.i_type == ASF_METADATA_TYPE_QWORD )
            {
                record.i_val = GetQWLE( p_peek + i_peek ); i_peek += 8;
            }
            else if( record.i_type == ASF_METADATA_TYPE_DWORD )
            {
                record.i_val = GetDWLE( p_peek + i_peek ); i_peek += 4;
            }
            else
            {
                record.i_val = GetWLE( p_peek + i_peek ); i_peek += 2;
            }
        }

        p_meta->i_record_entries_count++;
        p_meta->record =
            realloc( p_meta->record, p_meta->i_record_entries_count *
                     sizeof(asf_metadata_record_t) );
        memcpy( &p_meta->record[p_meta->i_record_entries_count-1],
                &record, sizeof(asf_metadata_record_t) );
    }

400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#ifdef ASF_DEBUG
    msg_Dbg( s,
            "read \"metadata object\" %d entries",
            p_meta->i_record_entries_count );
    for( i = 0; i < p_meta->i_record_entries_count; i++ )
    {
        asf_metadata_record_t *p_rec = &p_meta->record[i];
        
        if( p_rec->i_type == ASF_METADATA_TYPE_STRING )
            msg_Dbg( s, "  - %s=%s",
                     p_rec->psz_name, p_rec->p_data );
        else if( p_rec->i_type == ASF_METADATA_TYPE_BYTE )
            msg_Dbg( s, "  - %s (%i bytes)",
                     p_rec->psz_name, p_rec->i_data );
        else
            msg_Dbg( s, "  - %s="I64Fd,
                     p_rec->psz_name, p_rec->i_val );
    }
#endif

420
421
422
423
424
425
426
    return VLC_SUCCESS;
}

static int ASF_ReadObject_header_extension( stream_t *s, asf_object_t *p_obj )
{
    asf_object_header_extension_t *p_he =
        (asf_object_header_extension_t *)p_obj;
427
428
    int     i_peek;
    uint8_t *p_peek;
429

430
    if( ( i_peek = stream_Peek( s, &p_peek, p_he->i_object_size ) ) <  46)
431
    {
432
       return VLC_EGENERIC;
433
    }
Laurent Aimar's avatar
Laurent Aimar committed
434
    ASF_GetGUID( &p_he->i_reserved1, p_peek + 24 );
435
    p_he->i_reserved2 = GetWLE( p_peek + 40 );
436
437
    p_he->i_header_extension_size = GetDWLE( p_peek + 42 );
    if( p_he->i_header_extension_size )
438
    {
439
440
441
442
        p_he->p_header_extension_data =
            malloc( p_he->i_header_extension_size );
        memcpy( p_he->p_header_extension_data, p_peek + 46,
                p_he->i_header_extension_size );
443
444
445
    }
    else
    {
446
        p_he->p_header_extension_data = NULL;
447
    }
448

449
#ifdef ASF_DEBUG
450
    msg_Dbg( s,
451
452
453
454
            "read \"header extension object\" reserved1:" GUID_FMT
            " reserved2:%d header_extension_size:%d",
            GUID_PRINT( p_he->i_reserved1 ), p_he->i_reserved2,
            p_he->i_header_extension_size );
455
#endif
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476

    if( !p_he->i_header_extension_size ) return VLC_SUCCESS;

    /* Read the extension objects */
    stream_Read( s, NULL, 46 );
    for( ; ; )
    {
        asf_object_t *p_obj = malloc( sizeof( asf_object_t ) );

        if( ASF_ReadObject( s, p_obj, (asf_object_t*)p_he ) )
        {
            free( p_obj );
            break;
        }

        if( ASF_NextObject( s, p_obj ) ) /* Go to the next object */
        {
            break;
        }
    }

477
    return VLC_SUCCESS;
478
}
479
480

static void ASF_FreeObject_header_extension( asf_object_t *p_obj )
481
{
482
483
    asf_object_header_extension_t *p_he =
        (asf_object_header_extension_t *)p_obj;
484

485
    FREE( p_he->p_header_extension_data );
486
487
}

488
static int ASF_ReadObject_stream_properties( stream_t *s, asf_object_t *p_obj )
489
{
490
    asf_object_stream_properties_t *p_sp =
491
                    (asf_object_stream_properties_t*)p_obj;
492
493
    int     i_peek;
    uint8_t *p_peek;
494

495
    if( ( i_peek = stream_Peek( s, &p_peek,  p_sp->i_object_size ) ) < 74 )
496
    {
497
       return VLC_EGENERIC;
498
    }
Laurent Aimar's avatar
Laurent Aimar committed
499
500
    ASF_GetGUID( &p_sp->i_stream_type, p_peek + 24 );
    ASF_GetGUID( &p_sp->i_error_correction_type, p_peek + 40 );
501
502
503
504
505
506
507
508
    p_sp->i_time_offset = GetQWLE( p_peek + 56 );
    p_sp->i_type_specific_data_length = GetDWLE( p_peek + 64 );
    p_sp->i_error_correction_data_length = GetDWLE( p_peek + 68 );
    p_sp->i_flags = GetWLE( p_peek + 72 );
        p_sp->i_stream_number = p_sp->i_flags&0x07f;
    p_sp->i_reserved = GetDWLE( p_peek + 74 );
    if( p_sp->i_type_specific_data_length )
    {
509
510
511
        p_sp->p_type_specific_data =
            malloc( p_sp->i_type_specific_data_length );
        memcpy( p_sp->p_type_specific_data, p_peek + 78,
512
513
514
515
516
517
518
519
                p_sp->i_type_specific_data_length );
    }
    else
    {
        p_sp->p_type_specific_data = NULL;
    }
    if( p_sp->i_error_correction_data_length )
    {
520
521
        p_sp->p_error_correction_data =
            malloc( p_sp->i_error_correction_data_length );
522
523
524
525
526
527
528
529
530
531
        memcpy( p_sp->p_error_correction_data,
                p_peek + 78 + p_sp->i_type_specific_data_length,
                p_sp->i_error_correction_data_length );
    }
    else
    {
        p_sp->p_error_correction_data = NULL;
    }

#ifdef ASF_DEBUG
532
    msg_Dbg( s,
hartman's avatar
hartman committed
533
            "read \"stream Properties object\" stream_type:" GUID_FMT
gbazin's avatar
   
gbazin committed
534
535
536
            " error_correction_type:" GUID_FMT " time_offset:"I64Fd
            " type_specific_data_length:%d error_correction_data_length:%d"
            " flags:0x%x stream_number:%d",
537
538
539
540
541
542
543
544
545
            GUID_PRINT( p_sp->i_stream_type ),
            GUID_PRINT( p_sp->i_error_correction_type ),
            p_sp->i_time_offset,
            p_sp->i_type_specific_data_length,
            p_sp->i_error_correction_data_length,
            p_sp->i_flags,
            p_sp->i_stream_number );

#endif
546
    return VLC_SUCCESS;
547
548
}

549
static void ASF_FreeObject_stream_properties( asf_object_t *p_obj )
550
{
551
    asf_object_stream_properties_t *p_sp =
552
553
554
555
556
557
558
                (asf_object_stream_properties_t*)p_obj;

    FREE( p_sp->p_type_specific_data );
    FREE( p_sp->p_error_correction_data );
}


559
static int ASF_ReadObject_codec_list( stream_t *s, asf_object_t *p_obj )
560
561
{
    asf_object_codec_list_t *p_cl = (asf_object_codec_list_t*)p_obj;
562
563
    int     i_peek;
    uint8_t *p_peek, *p_data;
564

565
566
    unsigned int i_codec;

567
    if( ( i_peek = stream_Peek( s, &p_peek, p_cl->i_object_size ) ) < 44 )
568
    {
569
       return VLC_EGENERIC;
570
571
    }

Laurent Aimar's avatar
Laurent Aimar committed
572
    ASF_GetGUID( &p_cl->i_reserved, p_peek + 24 );
573
574
575
    p_cl->i_codec_entries_count = GetWLE( p_peek + 40 );
    if( p_cl->i_codec_entries_count > 0 )
    {
576
577
578
579
        p_cl->codec = calloc( p_cl->i_codec_entries_count,
                              sizeof( asf_codec_entry_t ) );
        memset( p_cl->codec, 0,
                p_cl->i_codec_entries_count * sizeof( asf_codec_entry_t ) );
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606

        p_data = p_peek + 44;
        for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
        {
#define codec p_cl->codec[i_codec]
            int i_len, i;

            codec.i_type = GetWLE( p_data ); p_data += 2;
            /* codec name */
            i_len = GetWLE( p_data ); p_data += 2;
            codec.psz_name = calloc( sizeof( char ), i_len + 1);
            for( i = 0; i < i_len; i++ )
            {
                codec.psz_name[i] = GetWLE( p_data + 2*i );
            }
            codec.psz_name[i_len] = '\0';
            p_data += 2 * i_len;

            /* description */
            i_len = GetWLE( p_data ); p_data += 2;
            codec.psz_description = calloc( sizeof( char ), i_len + 1);
            for( i = 0; i < i_len; i++ )
            {
                codec.psz_description[i] = GetWLE( p_data + 2*i );
            }
            codec.psz_description[i_len] = '\0';
            p_data += 2 * i_len;
607

608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
            /* opaque information */
            codec.i_information_length = GetWLE( p_data ); p_data += 2;
            if( codec.i_information_length > 0 )
            {
                codec.p_information = malloc( codec.i_information_length );
                memcpy( codec.p_information, p_data, codec.i_information_length );
                p_data += codec.i_information_length;
            }
            else
            {
                codec.p_information = NULL;
            }
#undef  codec
        }
    }
    else
    {
        p_cl->codec = NULL;
    }

#ifdef ASF_DEBUG
629
630
631
    msg_Dbg( s, "read \"codec list object\" reserved_guid:" GUID_FMT
             " codec_entries_count:%d",
            GUID_PRINT( p_cl->i_reserved ), p_cl->i_codec_entries_count );
632

633
634
635
    for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
    {
#define codec p_cl->codec[i_codec]
636
        msg_Dbg( s, "  - codec[%d] %s name:\"%s\" "
637
638
639
640
641
                 "description:\"%s\" information_length:%d",
                 i_codec, ( codec.i_type == ASF_CODEC_TYPE_VIDEO ) ?
                 "video" : ( ( codec.i_type == ASF_CODEC_TYPE_AUDIO ) ?
                 "audio" : "unknown" ),
                 codec.psz_name, codec.psz_description,
642
                 codec.i_information_length );
643
#undef  codec
644
645
    }
#endif
646

647
    return VLC_SUCCESS;
648
}
649
650

static void ASF_FreeObject_codec_list( asf_object_t *p_obj )
651
652
{
    asf_object_codec_list_t *p_cl = (asf_object_codec_list_t*)p_obj;
653
    unsigned int i_codec;
654
655
656
657
658
659
660
661
662
663
664
665

    for( i_codec = 0; i_codec < p_cl->i_codec_entries_count; i_codec++ )
    {
#define codec p_cl->codec[i_codec]
        FREE( codec.psz_name );
        FREE( codec.psz_description );
        FREE( codec.p_information );
#undef  codec
    }
    FREE( p_cl->codec );
}

666
/* Microsoft should go to hell. This time the length give number of bytes
667
 * and for the some others object, length give char16 count ... */
668
static int ASF_ReadObject_content_description(stream_t *s, asf_object_t *p_obj)
669
{
670
    asf_object_content_description_t *p_cd =
671
        (asf_object_content_description_t *)p_obj;
672
    uint8_t *p_peek, *p_data;
673
674
    int i_peek;
    int i_len, i_title, i_author, i_copyright, i_description, i_rating;
675

676
677
678
679
680
681
682
683
#define GETSTRINGW( psz_str, i_size ) \
   psz_str = calloc( i_size/2 + 1, sizeof( char ) ); \
   for( i_len = 0; i_len < i_size/2; i_len++ ) \
   { \
       psz_str[i_len] = GetWLE( p_data + 2*i_len ); \
   } \
   psz_str[i_size/2] = '\0'; \
   p_data += i_size;
684

685
    if( ( i_peek = stream_Peek( s, &p_peek, p_cd->i_object_size ) ) < 34 )
686
    {
687
       return VLC_EGENERIC;
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
    }
    p_data = p_peek + 24;

    i_title = GetWLE( p_data ); p_data += 2;
    i_author= GetWLE( p_data ); p_data += 2;
    i_copyright     = GetWLE( p_data ); p_data += 2;
    i_description   = GetWLE( p_data ); p_data += 2;
    i_rating        = GetWLE( p_data ); p_data += 2;

    GETSTRINGW( p_cd->psz_title, i_title );
    GETSTRINGW( p_cd->psz_author, i_author );
    GETSTRINGW( p_cd->psz_copyright, i_copyright );
    GETSTRINGW( p_cd->psz_description, i_description );
    GETSTRINGW( p_cd->psz_rating, i_rating );

#undef  GETSTRINGW

#ifdef ASF_DEBUG
706
    msg_Dbg( s,
hartman's avatar
hartman committed
707
             "Read \"content description object\" title:\"%s\" author:\"%s\" copyright:\"%s\" description:\"%s\" rating:\"%s\"",
708
709
710
711
712
             p_cd->psz_title,
             p_cd->psz_author,
             p_cd->psz_copyright,
             p_cd->psz_description,
             p_cd->psz_rating );
713
#endif
714
    return VLC_SUCCESS;
715
716
}

717
static void ASF_FreeObject_content_description( asf_object_t *p_obj)
718
{
719
    asf_object_content_description_t *p_cd =
720
        (asf_object_content_description_t *)p_obj;
721

722
723
724
725
726
727
728
    FREE( p_cd->psz_title );
    FREE( p_cd->psz_author );
    FREE( p_cd->psz_copyright );
    FREE( p_cd->psz_description );
    FREE( p_cd->psz_rating );
}

729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
/* Language list: */
static int ASF_ReadObject_language_list(stream_t *s, asf_object_t *p_obj)
{
    asf_object_language_list_t *p_ll =
        (asf_object_language_list_t*)p_obj;
    uint8_t *p_peek, *p_data;
    int i_peek;
    int i;

    if( ( i_peek = stream_Peek( s, &p_peek, p_ll->i_object_size ) ) < 26 )
       return VLC_EGENERIC;

    p_data = &p_peek[24];

    p_ll->i_language = GetWLE( &p_data[0] ); p_data += 2;
    if( p_ll->i_language > 0 )
    {
        p_ll->ppsz_language = calloc( p_ll->i_language, sizeof( char *) );

        for( i = 0; i < p_ll->i_language; i++ )
        {
            char *psz;
            int i_size = *p_data++;
            int i_len;

            psz = calloc( i_size/2 + 1, sizeof( char ) );
            for( i_len = 0; i_len < i_size/2; i_len++ )
            {
                psz[i_len] = GetWLE( p_data + 2*i_len );
            }
            psz[i_size/2] = '\0'; \
            p_data += i_size;

            p_ll->ppsz_language[i] = psz;
        }
    }

#ifdef ASF_DEBUG
    msg_Dbg( s, "Read \"language list object\" %d entries", 
             p_ll->i_language );
    for( i = 0; i < p_ll->i_language; i++ )
        msg_Dbg( s, "  - '%s'", 
                 p_ll->ppsz_language[i] );
#endif
    return VLC_SUCCESS;
}

static void ASF_FreeObject_language_list( asf_object_t *p_obj)
{
    asf_object_language_list_t *p_ll =
        (asf_object_language_list_t *)p_obj;
    int i;

    for( i = 0; i < p_ll->i_language; i++ )
        FREE( p_ll->ppsz_language[i] );
    FREE( p_ll->ppsz_language );
}

/* Stream bitrate properties */
static int ASF_ReadObject_stream_bitrate_properties( stream_t *s,
                                                     asf_object_t *p_obj)
{
    asf_object_stream_bitrate_properties_t *p_sb =
        (asf_object_stream_bitrate_properties_t *)p_obj;
    uint8_t *p_peek, *p_data;
    int i_peek;
    int i;

    if( ( i_peek = stream_Peek( s, &p_peek, p_sb->i_object_size ) ) < 26 )
       return VLC_EGENERIC;

    p_data = &p_peek[24];

    p_sb->i_bitrate = GetWLE( &p_data[0] ); p_data += 2;
    if( p_sb->i_bitrate > 127 ) p_sb->i_bitrate = 127;  /* Buggy ? */
    for( i = 0; i < p_sb->i_bitrate; i++ )
    {
        p_sb->bitrate[i].i_stream_number = GetWLE( &p_data[0] )& 0x7f;
        p_sb->bitrate[i].i_avg_bitrate = GetDWLE( &p_data[2] );
       
        p_data += 2+4;
    }

#ifdef ASF_DEBUG
    msg_Dbg( s,"Read \"stream bitrate properties object\"" );
    for( i = 0; i < p_sb->i_bitrate; i++ )
    {
        msg_Dbg( s,"  - stream=%d bitrate=%d",
                 p_sb->bitrate[i].i_stream_number,
                 p_sb->bitrate[i].i_avg_bitrate ); 
    }
#endif
    return VLC_SUCCESS;
}
static void ASF_FreeObject_stream_bitrate_properties( asf_object_t *p_obj)
{
}

static int ASF_ReadObject_extended_stream_properties( stream_t *s,
                                                      asf_object_t *p_obj)
{
    asf_object_extended_stream_properties_t *p_esp =
        (asf_object_extended_stream_properties_t*)p_obj;
    uint8_t *p_peek, *p_data;
    int i_peek, i;

    if( ( i_peek = stream_Peek( s, &p_peek, p_esp->i_object_size ) ) < 88 )
       return VLC_EGENERIC;

    p_data = &p_peek[24];

    p_esp->i_start_time = GetQWLE( &p_data[0] );
    p_esp->i_end_time = GetQWLE( &p_data[8] );
    p_esp->i_data_bitrate = GetDWLE( &p_data[16] );
    p_esp->i_buffer_size = GetDWLE( &p_data[20] );
    p_esp->i_initial_buffer_fullness = GetDWLE( &p_data[24] );
    p_esp->i_alternate_data_bitrate = GetDWLE( &p_data[28] );
    p_esp->i_alternate_buffer_size = GetDWLE( &p_data[32] );
    p_esp->i_alternate_initial_buffer_fullness = GetDWLE( &p_data[36] );
    p_esp->i_maximum_object_size = GetDWLE( &p_data[40] );
    p_esp->i_flags = GetDWLE( &p_data[44] );
    p_esp->i_stream_number = GetWLE( &p_data[48] );
    p_esp->i_language_index = GetWLE( &p_data[50] );
    p_esp->i_average_time_per_frame= GetQWLE( &p_data[52] );
    p_esp->i_stream_name_count = GetWLE( &p_data[60] );
    p_esp->i_payload_extention_system_count = GetWLE( &p_data[62] );

    p_data += 64;

    p_esp->pi_stream_name_language = calloc( sizeof(int),
                                             p_esp->i_stream_name_count );
    p_esp->ppsz_stream_name = calloc( sizeof(char*),
                                      p_esp->i_stream_name_count );
    for( i = 0; i < p_esp->i_stream_name_count; i++ )
    {
        int i_size;
        char *psz;
        int i_len;

        p_esp->pi_stream_name_language[i] = GetWLE( &p_data[0] );
        i_size = GetWLE( &p_data[2] );
        p_data += 2;
        
        psz = calloc( i_size/2 + 1, sizeof( char ) );
        for( i_len = 0; i_len < i_size/2; i_len++ )
        {
            psz[i_len] = GetWLE( p_data + 2*i_len );
        }
        psz[i_size/2] = '\0'; \
        p_data += i_size;

        p_esp->ppsz_stream_name[i] = psz;
    }

    for( i = 0; i < p_esp->i_payload_extention_system_count; i++ )
    {
        /* Skip them */
        int i_size = GetDWLE( &p_data[16 + 2] );

        p_data += 16+2+4+i_size;
    }

891
    p_esp->p_sp = NULL;
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
    if( p_data < &p_peek[i_peek] )
    {
        asf_object_t *p_sp;
        /* Cannot fail as peek succeed */
        stream_Read( s, NULL, p_data - p_peek );
        
        p_sp = malloc( sizeof( asf_object_t ) );

        if( ASF_ReadObject( s, p_sp, NULL ) )
        {
            free( p_sp );
        }
        else
        {
            /* This p_sp will be inserted by ReadRoot later */
Laurent Aimar's avatar
Laurent Aimar committed
907
            p_esp->p_sp = (asf_object_stream_properties_t*)p_sp;
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
        }
    }

#ifdef ASF_DEBUG
    msg_Dbg( s, "Read \"extended stream properties object\":" );
    msg_Dbg( s, "  - start="I64Fd" end="I64Fd,
             p_esp->i_start_time, p_esp->i_end_time );
    msg_Dbg( s, "  - data bitrate=%d buffer=%d initial fullness=%d",
             p_esp->i_data_bitrate,
             p_esp->i_buffer_size,
             p_esp->i_initial_buffer_fullness );
    msg_Dbg( s, "  - alternate data bitrate=%d buffer=%d initial fullness=%d",
             p_esp->i_alternate_data_bitrate,
             p_esp->i_alternate_buffer_size,
             p_esp->i_alternate_initial_buffer_fullness );
    msg_Dbg( s, "  - maximum object size=%d", p_esp->i_maximum_object_size );
    msg_Dbg( s, "  - flags=0x%x", p_esp->i_flags );
    msg_Dbg( s, "  - stream number=%d language=%d",
             p_esp->i_stream_number, p_esp->i_language_index );
    msg_Dbg( s, "  - average time per frame="I64Fd,
             p_esp->i_average_time_per_frame );
    msg_Dbg( s, "  - stream name count=%d", p_esp->i_stream_name_count );
    for( i = 0; i < p_esp->i_stream_name_count; i++ )
        msg_Dbg( s, "     - lang id=%d name=%s",
                 p_esp->pi_stream_name_language[i],
                 p_esp->ppsz_stream_name[i] );
    msg_Dbg( s, "  - payload extention system count=%d",
             p_esp->i_payload_extention_system_count );
#endif
    return VLC_SUCCESS;
}
static void ASF_FreeObject_extended_stream_properties( asf_object_t *p_obj)
{
    asf_object_extended_stream_properties_t *p_esp =
        (asf_object_extended_stream_properties_t *)p_obj;
    int i;

    for( i = 0; i < p_esp->i_stream_name_count; i++ )
        FREE( p_esp->ppsz_stream_name[i] );
    FREE( p_esp->pi_stream_name_language );
    FREE( p_esp->ppsz_stream_name );
}


static int ASF_ReadObject_advanced_mutual_exclusion( stream_t *s,
                                                     asf_object_t *p_obj)
{
    asf_object_advanced_mutual_exclusion_t *p_ae =
        (asf_object_advanced_mutual_exclusion_t *)p_obj;
    uint8_t *p_peek, *p_data;
    int i_peek;
    int i;

    if( ( i_peek = stream_Peek( s, &p_peek, p_ae->i_object_size ) ) < 42 )
       return VLC_EGENERIC;

    p_data = &p_peek[24];

    ASF_GetGUID( &p_ae->type, &p_data[0] );
    p_ae->i_stream_number_count = GetWLE( &p_data[16] );

    p_data += 16 + 2;
    p_ae->pi_stream_number = calloc( sizeof(int),
                                     p_ae->i_stream_number_count );
    for( i = 0; i < p_ae->i_stream_number_count; i++ )
    {
        p_ae->pi_stream_number[i] = GetWLE( p_data );
        p_data += 2;
    }
        
#ifdef ASF_DEBUG
    msg_Dbg( s, "Read \"advanced mutual exclusion object\"" );
    for( i = 0; i < p_ae->i_stream_number_count; i++ )
        msg_Dbg( s, "  - stream=%d", p_ae->pi_stream_number[i] );
#endif
    return VLC_SUCCESS;
}
static void ASF_FreeObject_advanced_mutual_exclusion( asf_object_t *p_obj)
{
    asf_object_advanced_mutual_exclusion_t *p_ae =
        (asf_object_advanced_mutual_exclusion_t *)p_obj;

    FREE( p_ae->pi_stream_number );
}


static int ASF_ReadObject_stream_prioritization( stream_t *s,
                                                 asf_object_t *p_obj)
{
    asf_object_stream_prioritization_t *p_sp =
        (asf_object_stream_prioritization_t *)p_obj;
    uint8_t *p_peek, *p_data;
    int i_peek;
    int i;

    if( ( i_peek = stream_Peek( s, &p_peek, p_sp->i_object_size ) ) < 26 )
       return VLC_EGENERIC;

    p_data = &p_peek[24];

    p_sp->i_priority_count = GetWLE( &p_data[0] );
    p_data += 2;

    p_sp->pi_priority_flag = calloc( sizeof(int), p_sp->i_priority_count );
    p_sp->pi_priority_stream_number =
                             calloc( sizeof(int), p_sp->i_priority_count );

    for( i = 0; i < p_sp->i_priority_count; i++ )
    {
        p_sp->pi_priority_stream_number[i] = GetWLE( p_data ); p_data += 2;
        p_sp->pi_priority_flag[i] = GetWLE( p_data ); p_data += 2;
    }
#ifdef ASF_DEBUG
    msg_Dbg( s, "Read \"stream prioritization object\"" );
    for( i = 0; i < p_sp->i_priority_count; i++ )
        msg_Dbg( s, "  - Stream:%d flags=0x%x",
                 p_sp->pi_priority_stream_number[i],
                 p_sp->pi_priority_flag[i] );
#endif
    return VLC_SUCCESS;
}
static void ASF_FreeObject_stream_prioritization( asf_object_t *p_obj)
{
    asf_object_stream_prioritization_t *p_sp =
        (asf_object_stream_prioritization_t *)p_obj;

    FREE( p_sp->pi_priority_stream_number );
    FREE( p_sp->pi_priority_flag );
}


static int ASF_ReadObject_extended_content_description( stream_t *s,
                                                        asf_object_t *p_obj)
{
    asf_object_extended_content_description_t *p_ec =
        (asf_object_extended_content_description_t *)p_obj;
    uint8_t *p_peek, *p_data;
    int i_peek;
    int i;

    if( ( i_peek = stream_Peek( s, &p_peek, p_ec->i_object_size ) ) < 26 )
       return VLC_EGENERIC;

    p_data = &p_peek[24];

    p_ec->i_count = GetWLE( p_data ); p_data += 2;
    p_ec->ppsz_name = calloc( sizeof(char*), p_ec->i_count );
    p_ec->ppsz_value = calloc( sizeof(char*), p_ec->i_count );
    for( i = 0; i < p_ec->i_count; i++ )
    {
        int i_size;
        int i_type;
        int i_len;
#define GETSTRINGW( psz_str, i_size ) \
       psz_str = calloc( i_size/2 + 1, sizeof( char ) ); \
       for( i_len = 0; i_len < i_size/2; i_len++ ) \
       { \
           psz_str[i_len] = GetWLE( p_data + 2*i_len ); \
       } \
       psz_str[i_size/2] = '\0';

        i_size = GetWLE( p_data ); p_data += 2;
        GETSTRINGW( p_ec->ppsz_name[i], i_size );
        p_data += i_size;

        /* Grrr */
        i_type = GetWLE( p_data ); p_data += 2;
        i_size = GetWLE( p_data ); p_data += 2;
        if( i_type == 0 )
        {
            GETSTRINGW( p_ec->ppsz_value[i], i_size );
        }
        else if( i_type == 1 )
        {
            int j;
            /* Byte array */
            p_ec->ppsz_value[i] = malloc( 2*i_size + 1 );
            for( j = 0; j < i_size; j++ )
            {
                static const char hex[16] = "0123456789ABCDEF";
                p_ec->ppsz_value[i][2*j+0] = hex[p_data[0]>>4];
                p_ec->ppsz_value[i][2*j+1] = hex[p_data[0]&0xf];
            }
            p_ec->ppsz_value[i][2*i_size] = '\0';
        }
        else if( i_type == 2 )
        {
            /* Bool */
            p_ec->ppsz_value[i] = strdup( *p_data ? "true" : "false" );
        }
        else if( i_type == 3 )
        {
            /* DWord */
            asprintf( &p_ec->ppsz_value[i], "%d", GetDWLE(p_data));
        }
        else if( i_type == 4 )
        {
            /* QWord */
            asprintf( &p_ec->ppsz_value[i], I64Fd, GetQWLE(p_data));
        }
        else if( i_type == 5 )
        {
            /* Word */
            asprintf( &p_ec->ppsz_value[i], "%d", GetWLE(p_data));
        }
        else
            p_ec->ppsz_value[i] = NULL;

        p_data += i_size;
        


#undef GETSTRINGW

    }

#ifdef ASF_DEBUG
    msg_Dbg( s, "Read \"extended content description object\"" );
    for( i = 0; i < p_ec->i_count; i++ )
        msg_Dbg( s, "  - '%s' = '%s'",
                 p_ec->ppsz_name[i],
                 p_ec->ppsz_value[i] );
#endif
    return VLC_SUCCESS;
}
static void ASF_FreeObject_extended_content_description( asf_object_t *p_obj)
{
    asf_object_extended_content_description_t *p_ec =
        (asf_object_extended_content_description_t *)p_obj;
    int i;

    for( i = 0; i < p_ec->i_count; i++ )
    {
        FREE( p_ec->ppsz_name[i] );
        FREE( p_ec->ppsz_value[i] );
    }
}


#if 0
static int ASF_ReadObject_XXX(stream_t *s, asf_object_t *p_obj)
{
    asf_object_XXX_t *p_XX =
        (asf_object_XXX_t *)p_obj;
    uint8_t *p_peek, *p_data;
    int i_peek;

    if( ( i_peek = stream_Peek( s, &p_peek, p_XX->i_object_size ) ) < XXX )
       return VLC_EGENERIC;

    p_data = &p_peek[24];

#ifdef ASF_DEBUG
    msg_Dbg( s,
             "Read \"XXX object\"" );
#endif
    return VLC_SUCCESS;
}
static void ASF_FreeObject_XXX( asf_object_t *p_obj)
{
    asf_object_XXX_t *p_XX =
        (asf_object_XXX_t *)p_obj;
}
#endif


/* */
1175
1176
1177
1178
static struct
{
    const guid_t  *p_id;
    int     i_type;
1179
1180
    int     (*ASF_ReadObject_function)( stream_t *, asf_object_t *p_obj );
    void    (*ASF_FreeObject_function)( asf_object_t *p_obj );
1181

1182
1183
} ASF_Object_Function [] =
{
1184
    { &asf_object_header_guid, ASF_OBJECT_HEADER,
1185
      ASF_ReadObject_Header, ASF_FreeObject_Null },
1186
    { &asf_object_data_guid, ASF_OBJECT_DATA,
1187
      ASF_ReadObject_Data, ASF_FreeObject_Null },
1188
    { &asf_object_index_guid, ASF_OBJECT_INDEX,
1189
      ASF_ReadObject_Index, ASF_FreeObject_Index },
1190
    { &asf_object_file_properties_guid, ASF_OBJECT_FILE_PROPERTIES,
1191
      ASF_ReadObject_file_properties, ASF_FreeObject_Null },
1192
    { &asf_object_stream_properties_guid, ASF_OBJECT_STREAM_PROPERTIES,
1193
      ASF_ReadObject_stream_properties,ASF_FreeObject_stream_properties },
1194
    { &asf_object_header_extension_guid, ASF_OBJECT_HEADER_EXTENSION,
1195
      ASF_ReadObject_header_extension, ASF_FreeObject_header_extension},
1196
    { &asf_object_metadata_guid, ASF_OBJECT_METADATA,
1197
      ASF_ReadObject_metadata, ASF_FreeObject_metadata},
1198
    { &asf_object_codec_list_guid, ASF_OBJECT_CODEC_LIST,
1199
      ASF_ReadObject_codec_list, ASF_FreeObject_codec_list },
1200
1201
1202
    { &asf_object_marker_guid, ASF_OBJECT_MARKER, NULL, NULL },
    { &asf_object_padding, ASF_OBJECT_PADDING, NULL, NULL },
    { &asf_object_content_description_guid, ASF_OBJECT_CONTENT_DESCRIPTION,
1203
      ASF_ReadObject_content_description, ASF_FreeObject_content_description },
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
    { &asf_object_language_list, ASF_OBJECT_OTHER,
      ASF_ReadObject_language_list, ASF_FreeObject_language_list },
    { &asf_object_stream_bitrate_properties, ASF_OBJECT_OTHER,
      ASF_ReadObject_stream_bitrate_properties,
      ASF_FreeObject_stream_bitrate_properties },
    { &asf_object_extended_stream_properties, ASF_OBJECT_OTHER,
      ASF_ReadObject_extended_stream_properties,
      ASF_FreeObject_extended_stream_properties },
    { &asf_object_advanced_mutual_exclusion, ASF_OBJECT_OTHER,
      ASF_ReadObject_advanced_mutual_exclusion,
      ASF_FreeObject_advanced_mutual_exclusion },
    { &asf_object_stream_prioritization, ASF_OBJECT_OTHER,
      ASF_ReadObject_stream_prioritization,
      ASF_FreeObject_stream_prioritization },
    { &asf_object_extended_content_description, ASF_OBJECT_OTHER,
      ASF_ReadObject_extended_content_description,
      ASF_FreeObject_extended_content_description },
1221
1222

    { &asf_object_null_guid, 0, NULL, NULL }
1223
1224
};

1225
1226
static int ASF_ReadObject( stream_t *s, asf_object_t *p_obj,
                           asf_object_t *p_father )
1227
1228
1229
1230
{
    int i_result;
    int i_index;

1231
1232
1233
1234
    if( !p_obj )
        return( 0 );

    memset( p_obj, 0, sizeof( p_obj ) );
1235

1236
    if( ASF_ReadObjectCommon( s, p_obj ) )
1237
    {
1238
        msg_Warn( s, "cannot read one asf object" );
1239
        return VLC_EGENERIC;
1240
1241
1242
1243
1244
    }
    p_obj->common.p_father = p_father;
    p_obj->common.p_first = NULL;
    p_obj->common.p_next = NULL;
    p_obj->common.p_last = NULL;
1245

1246
1247
    if( p_obj->common.i_object_size < 24 )
    {
1248
        msg_Warn( s, "found a corrupted asf object (size<24)" );
1249
        return VLC_EGENERIC;
1250
    }
1251

1252
1253
1254
    /* find this object */
    for( i_index = 0; ; i_index++ )
    {
Laurent Aimar's avatar
Laurent Aimar committed
1255
        if( ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1256
                         &p_obj->common.i_object_id ) ||
Laurent Aimar's avatar
Laurent Aimar committed
1257
            ASF_CmpGUID( ASF_Object_Function[i_index].p_id,
1258
                         &asf_object_null_guid ) )
1259
1260
1261
1262
1263
1264
1265
1266
1267
        {
            break;
        }
    }
    p_obj->common.i_type = ASF_Object_Function[i_index].i_type;

    /* Now load this object */
    if( ASF_Object_Function[i_index].ASF_ReadObject_function == NULL )
    {
1268
        msg_Warn( s, "unknown asf object (not loaded)" );
1269
        i_result = VLC_SUCCESS;
1270
1271
1272
1273
    }
    else
    {
        /* XXX ASF_ReadObject_function realloc *pp_obj XXX */
1274
        i_result =
1275
          (ASF_Object_Function[i_index].ASF_ReadObject_function)( s, p_obj );
1276
    }
1277

1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
    /* link this object with father */
    if( p_father )
    {
        if( p_father->common.p_first )
        {
            p_father->common.p_last->common.p_next = p_obj;
        }
        else
        {
            p_father->common.p_first = p_obj;
        }
        p_father->common.p_last = p_obj;
    }

    return( i_result );
}

1295
static void ASF_FreeObject( stream_t *s, asf_object_t *p_obj )
1296
1297
1298
{
    int i_index;
    asf_object_t *p_child;
1299

1300
    if( !p_obj ) return;