asf.c 11 KB
Newer Older
1
2
3
4
/*****************************************************************************
 * asf.c: MMS access plug-in
 *****************************************************************************
 * Copyright (C) 2001, 2002 VideoLAN
Jean-Paul Saman's avatar
Jean-Paul Saman committed
5
 * $Id: asf.c,v 1.2 2003/12/07 18:55:59 jpsaman Exp $
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *
 * 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.
 *
 * 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>

#include <vlc/vlc.h>

#include "asf.h"
#include "buffer.h"


static int CmpGuid( const guid_t *p_guid1, const guid_t *p_guid2 )
{
    return( ( p_guid1->v1 == p_guid2->v1 &&
              p_guid1->v2 == p_guid2->v2 &&
              p_guid1->v3 == p_guid2->v3 &&
              p_guid1->v4[0] == p_guid2->v4[0] &&
              p_guid1->v4[1] == p_guid2->v4[1] &&
              p_guid1->v4[2] == p_guid2->v4[2] &&
              p_guid1->v4[3] == p_guid2->v4[3] &&
              p_guid1->v4[4] == p_guid2->v4[4] &&
              p_guid1->v4[5] == p_guid2->v4[5] &&
              p_guid1->v4[6] == p_guid2->v4[6] &&
              p_guid1->v4[7] == p_guid2->v4[7] ) ? 1 : 0 );
}

void E_( GenerateGuid )( guid_t *p_guid )
{
    int i;

    srand( mdate() & 0xffffffff );

    /* FIXME should be generated using random data */
    p_guid->v1 = 0xbabac001;
    p_guid->v2 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
    p_guid->v3 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
    for( i = 0; i < 8; i++ )
    {
        p_guid->v4[i] = ( (uint64_t)rand() * 256 ) / RAND_MAX;
    }
}

void E_( asf_HeaderParse )  ( asf_header_t *hdr,
                              uint8_t *p_header, int i_header )
{
    var_buffer_t buffer;
    guid_t      guid;
    uint64_t    i_size;
    int         i;

    for( i = 0; i < 128; i++ )
    {
        hdr->stream[i].i_cat = ASF_STREAM_UNKNOWN;
    }

    //fprintf( stderr, " ---------------------header:%d\n", i_header );
    var_buffer_initread( &buffer, p_header, i_header );

    var_buffer_getguid( &buffer, &guid );

    if( !CmpGuid( &guid, &asf_object_header_guid ) )
    {
//        XXX Error
//        fprintf( stderr, " ---------------------ERROR------\n" );
    }
    var_buffer_getmemory( &buffer, NULL, 30 - 16 );

    for( ;; )
    {
        //fprintf( stderr, " ---------------------data:%d\n", buffer.i_data );

        var_buffer_getguid( &buffer, &guid );
        i_size = var_buffer_get64( &buffer );

        //fprintf( stderr, "  guid=0x%8.8x-0x%4.4x-0x%4.4x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x size=%lld\n",
        //                  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],
        //                  i_size );

        if( CmpGuid( &guid, &asf_object_file_properties_guid ) )
        {
            var_buffer_getmemory( &buffer, NULL, 16 );
            hdr->i_file_size            = var_buffer_get64( &buffer );
            var_buffer_getmemory( &buffer, NULL, 8 );
            hdr->i_data_packets_count   = var_buffer_get64( &buffer );
            var_buffer_getmemory( &buffer, NULL, 8+8+8+4);
            hdr->i_min_data_packet_size = var_buffer_get32( &buffer );

            var_buffer_getmemory( &buffer, NULL, i_size - 24 - 16 - 8 - 8 - 8 - 8-8-8-4 - 4);
        }
        else if( CmpGuid( &guid, &asf_object_stream_properties_guid ) )
        {
            int     i_stream_id;
            guid_t  stream_type;
            //msg_Dbg( p_input, "found stream_properties" );

            var_buffer_getguid( &buffer, &stream_type );
            var_buffer_getmemory( &buffer, NULL, 32 );
            i_stream_id = var_buffer_get8( &buffer ) & 0x7f;

            //fprintf( stderr, " 1---------------------skip:%lld\n", i_size - 24 - 32 - 16 - 1 );
            var_buffer_getmemory( &buffer, NULL, i_size - 24 - 32 - 16 - 1);

            if( CmpGuid( &stream_type, &asf_object_stream_type_video ) )
            {
                //fprintf( stderr, "\nvideo stream[%d] found\n", i_stream_id );
                //msg_Dbg( p_input, "video stream[%d] found", i_stream_id );
                hdr->stream[i_stream_id].i_cat = ASF_STREAM_VIDEO;
            }
            else if( CmpGuid( &stream_type, &asf_object_stream_type_audio ) )
            {
                //fprintf( stderr, "\naudio stream[%d] found\n", i_stream_id );
                //msg_Dbg( p_input, "audio stream[%d] found", i_stream_id );
                hdr->stream[i_stream_id].i_cat = ASF_STREAM_AUDIO;
            }
            else
            {
//                msg_Dbg( p_input, "unknown stream[%d] found", i_stream_id );
                hdr->stream[i_stream_id].i_cat = ASF_STREAM_UNKNOWN;
            }
        }
        else if ( CmpGuid( &guid, &asf_object_bitrate_properties_guid ) )
        {
            int     i_count;
            uint8_t i_stream_id;

            i_count = var_buffer_get16( &buffer );
            i_size -= 2;
            while( i_count > 0 )
            {
                i_stream_id = var_buffer_get16( &buffer )&0x7f;
                hdr->stream[i_stream_id].i_bitrate =  var_buffer_get32( &buffer );
                i_count--;
                i_size -= 6;
            }
            //fprintf( stderr, " 2---------------------skip:%lld\n", i_size - 24);
            var_buffer_getmemory( &buffer, NULL, i_size - 24 );
        }
        else
        {
            //fprintf( stderr, " 3---------------------skip:%lld\n", i_size - 24);
            // skip unknown guid
            var_buffer_getmemory( &buffer, NULL, i_size - 24 );
        }

        if( var_buffer_readempty( &buffer ) )
        {
            return;
        }
    }
}

void E_( asf_StreamSelect ) ( asf_header_t *hdr,
                              int i_bitrate_max,
                              vlc_bool_t b_all, vlc_bool_t b_audio, vlc_bool_t b_video )
{
    /* XXX FIXME use mututal eclusion information */
    int i;
    int i_audio, i_video;
    int i_bitrate_total;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
181
#if 0
182
    char *psz_stream;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
183
#endif
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
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

    i_audio = 0;
    i_video = 0;
    i_bitrate_total = 0;
    if( b_all )
    {
        /* select all valid stream */
        for( i = 1; i < 128; i++ )
        {
            if( hdr->stream[i].i_cat != ASF_STREAM_UNKNOWN )
            {
                hdr->stream[i].i_selected = 1;
            }
        }
        return;
    }
    else
    {
        for( i = 0; i < 128; i++ )
        {
            hdr->stream[i].i_selected = 0; /* by default, not selected */
        }
    }

#if 0
    psz_stream = config_GetPsz( p_input, "mms-stream" );

    if( psz_stream && *psz_stream )
    {
        char *psz_tmp = psz_stream;
        while( *psz_tmp )
        {
            if( *psz_tmp == ',' )
            {
                psz_tmp++;
            }
            else
            {
                int i_stream;
                i_stream = atoi( psz_tmp );
                while( *psz_tmp != '\0' && *psz_tmp != ',' )
                {
                    psz_tmp++;
                }

                if( i_stream > 0 && i_stream < 128 &&
                    stream[i_stream].i_cat != MMS_STREAM_UNKNOWN )
                {
                    stream[i_stream].i_selected = 1;
                }
            }
        }
        FREE( psz_stream );
        return;
    }
    FREE( psz_stream );
#endif

    /* big test:
     * select a stream if
     *    - no audio nor video stream
     *    - or:
     *         - if i_bitrate_max not set keep the highest bitrate
     *         - if i_bitrate_max is set, keep stream that make we used best
     *           quality regarding i_bitrate_max
     *
     * XXX: little buggy:
     *        - it doesn't use mutual exclusion info..
     *        - when selecting a better stream we could select
     *        something that make i_bitrate_total> i_bitrate_max
     */
    for( i = 1; i < 128; i++ )
    {
        if( hdr->stream[i].i_cat == ASF_STREAM_UNKNOWN )
        {
            continue;
        }
        else if( hdr->stream[i].i_cat == ASF_STREAM_AUDIO && b_audio &&
                 ( i_audio <= 0 ||
                    ( ( ( hdr->stream[i].i_bitrate > hdr->stream[i_audio].i_bitrate &&
                          ( i_bitrate_total + hdr->stream[i].i_bitrate - hdr->stream[i_audio].i_bitrate
                                            < i_bitrate_max || !i_bitrate_max) ) ||
                        ( hdr->stream[i].i_bitrate < hdr->stream[i_audio].i_bitrate &&
                              i_bitrate_max != 0 && i_bitrate_total > i_bitrate_max )
                      ) )  ) )
        {
            /* unselect old stream */
            if( i_audio > 0 )
            {
                hdr->stream[i_audio].i_selected = 0;
                if( hdr->stream[i_audio].i_bitrate> 0 )
                {
                    i_bitrate_total -= hdr->stream[i_audio].i_bitrate;
                }
            }

            hdr->stream[i].i_selected = 1;
            if( hdr->stream[i].i_bitrate> 0 )
            {
                i_bitrate_total += hdr->stream[i].i_bitrate;
            }
            i_audio = i;
        }
        else if( hdr->stream[i].i_cat == ASF_STREAM_VIDEO && b_video &&
                 ( i_video <= 0 ||
                    (
                        ( ( hdr->stream[i].i_bitrate > hdr->stream[i_video].i_bitrate &&
                            ( i_bitrate_total + hdr->stream[i].i_bitrate - hdr->stream[i_video].i_bitrate
                                            < i_bitrate_max || !i_bitrate_max) ) ||
                          ( hdr->stream[i].i_bitrate < hdr->stream[i_video].i_bitrate &&
                            i_bitrate_max != 0 && i_bitrate_total > i_bitrate_max )
                        ) ) )  )
        {
            /* unselect old stream */

            if( i_video > 0 )
            {
                hdr->stream[i_video].i_selected = 0;
                if( hdr->stream[i_video].i_bitrate> 0 )
                {
                    i_bitrate_total -= hdr->stream[i_video].i_bitrate;
                }
            }

            hdr->stream[i].i_selected = 1;
            if( hdr->stream[i].i_bitrate> 0 )
            {
                i_bitrate_total += hdr->stream[i].i_bitrate;
            }
            i_video = i;
        }

    }
#if 0
    if( i_bitrate_max > 0 )
    {
        msg_Dbg( p_input,
                 "requested bitrate:%d real bitrate:%d",
                 i_bitrate_max, i_bitrate_total );
    }
    else
    {
        msg_Dbg( p_input,
                 "total bitrate:%d",
                 i_bitrate_total );
    }
#endif
}