04fc2cfde826431654af83ac9c6b6c0cf33de4f2
[vlc.git] / modules / access / mms / asf.c
1 /*****************************************************************************
2  * asf.c: MMS access plug-in
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23 #include <stdlib.h>
24
25 #include <vlc/vlc.h>
26
27 #include "asf.h"
28 #include "buffer.h"
29
30
31 static int CmpGuid( const guid_t *p_guid1, const guid_t *p_guid2 )
32 {
33     return( ( p_guid1->v1 == p_guid2->v1 &&
34               p_guid1->v2 == p_guid2->v2 &&
35               p_guid1->v3 == p_guid2->v3 &&
36               p_guid1->v4[0] == p_guid2->v4[0] &&
37               p_guid1->v4[1] == p_guid2->v4[1] &&
38               p_guid1->v4[2] == p_guid2->v4[2] &&
39               p_guid1->v4[3] == p_guid2->v4[3] &&
40               p_guid1->v4[4] == p_guid2->v4[4] &&
41               p_guid1->v4[5] == p_guid2->v4[5] &&
42               p_guid1->v4[6] == p_guid2->v4[6] &&
43               p_guid1->v4[7] == p_guid2->v4[7] ) ? 1 : 0 );
44 }
45
46 void E_( GenerateGuid )( guid_t *p_guid )
47 {
48     int i;
49
50     srand( mdate() & 0xffffffff );
51
52     /* FIXME should be generated using random data */
53     p_guid->v1 = 0xbabac001;
54     p_guid->v2 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
55     p_guid->v3 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
56     for( i = 0; i < 8; i++ )
57     {
58         p_guid->v4[i] = ( (uint64_t)rand() * 256 ) / RAND_MAX;
59     }
60 }
61
62 void E_( asf_HeaderParse )  ( asf_header_t *hdr,
63                               uint8_t *p_header, int i_header )
64 {
65     var_buffer_t buffer;
66     guid_t      guid;
67     uint64_t    i_size;
68     int         i;
69
70     hdr->i_file_size = 0;
71     hdr->i_data_packets_count = 0;
72     hdr->i_min_data_packet_size = 0;
73     for( i = 0; i < 128; i++ )
74     {
75         hdr->stream[i].i_cat = ASF_STREAM_UNKNOWN;
76         hdr->stream[i].i_selected = 0;
77         hdr->stream[i].i_bitrate = -1;
78     }
79
80     //fprintf( stderr, " ---------------------header:%d\n", i_header );
81     var_buffer_initread( &buffer, p_header, i_header );
82
83     var_buffer_getguid( &buffer, &guid );
84
85     if( !CmpGuid( &guid, &asf_object_header_guid ) )
86     {
87 //        XXX Error
88 //        fprintf( stderr, " ---------------------ERROR------\n" );
89     }
90     var_buffer_getmemory( &buffer, NULL, 30 - 16 );
91
92     for( ;; )
93     {
94         //fprintf( stderr, " ---------------------data:%d\n", buffer.i_data );
95
96         var_buffer_getguid( &buffer, &guid );
97         i_size = var_buffer_get64( &buffer );
98
99         //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",
100         //                  guid.v1,guid.v2, guid.v3,
101         //                  guid.v4[0],guid.v4[1],guid.v4[2],guid.v4[3],
102         //                  guid.v4[4],guid.v4[5],guid.v4[6],guid.v4[7],
103         //                  i_size );
104
105         if( CmpGuid( &guid, &asf_object_file_properties_guid ) )
106         {
107             var_buffer_getmemory( &buffer, NULL, 16 );
108             hdr->i_file_size            = var_buffer_get64( &buffer );
109             var_buffer_getmemory( &buffer, NULL, 8 );
110             hdr->i_data_packets_count   = var_buffer_get64( &buffer );
111             var_buffer_getmemory( &buffer, NULL, 8+8+8+4);
112             hdr->i_min_data_packet_size = var_buffer_get32( &buffer );
113
114             var_buffer_getmemory( &buffer, NULL, i_size - 24 - 16 - 8 - 8 - 8 - 8-8-8-4 - 4);
115         }
116         else if( CmpGuid( &guid, &asf_object_stream_properties_guid ) )
117         {
118             int     i_stream_id;
119             guid_t  stream_type;
120             //msg_Dbg( p_input, "found stream_properties" );
121
122             var_buffer_getguid( &buffer, &stream_type );
123             var_buffer_getmemory( &buffer, NULL, 32 );
124             i_stream_id = var_buffer_get8( &buffer ) & 0x7f;
125
126             //fprintf( stderr, " 1---------------------skip:%lld\n", i_size - 24 - 32 - 16 - 1 );
127             var_buffer_getmemory( &buffer, NULL, i_size - 24 - 32 - 16 - 1);
128
129             if( CmpGuid( &stream_type, &asf_object_stream_type_video ) )
130             {
131                 //fprintf( stderr, "\nvideo stream[%d] found\n", i_stream_id );
132                 //msg_Dbg( p_input, "video stream[%d] found", i_stream_id );
133                 hdr->stream[i_stream_id].i_cat = ASF_STREAM_VIDEO;
134             }
135             else if( CmpGuid( &stream_type, &asf_object_stream_type_audio ) )
136             {
137                 //fprintf( stderr, "\naudio stream[%d] found\n", i_stream_id );
138                 //msg_Dbg( p_input, "audio stream[%d] found", i_stream_id );
139                 hdr->stream[i_stream_id].i_cat = ASF_STREAM_AUDIO;
140             }
141             else
142             {
143 //                msg_Dbg( p_input, "unknown stream[%d] found", i_stream_id );
144                 hdr->stream[i_stream_id].i_cat = ASF_STREAM_UNKNOWN;
145             }
146         }
147         else if ( CmpGuid( &guid, &asf_object_bitrate_properties_guid ) )
148         {
149             int     i_count;
150             uint8_t i_stream_id;
151
152             i_count = var_buffer_get16( &buffer );
153             i_size -= 2;
154             while( i_count > 0 )
155             {
156                 i_stream_id = var_buffer_get16( &buffer )&0x7f;
157                 hdr->stream[i_stream_id].i_bitrate =  var_buffer_get32( &buffer );
158                 i_count--;
159                 i_size -= 6;
160             }
161             //fprintf( stderr, " 2---------------------skip:%lld\n", i_size - 24);
162             var_buffer_getmemory( &buffer, NULL, i_size - 24 );
163         }
164         else
165         {
166             //fprintf( stderr, " 3---------------------skip:%lld\n", i_size - 24);
167             // skip unknown guid
168             var_buffer_getmemory( &buffer, NULL, i_size - 24 );
169         }
170
171         if( var_buffer_readempty( &buffer ) )
172         {
173             return;
174         }
175     }
176 }
177
178 void E_( asf_StreamSelect ) ( asf_header_t *hdr,
179                               int i_bitrate_max,
180                               vlc_bool_t b_all, vlc_bool_t b_audio, vlc_bool_t b_video )
181 {
182     /* XXX FIXME use mututal eclusion information */
183     int i;
184     int i_audio, i_video;
185     int i_bitrate_total;
186 #if 0
187     char *psz_stream;
188 #endif
189
190     i_audio = 0;
191     i_video = 0;
192     i_bitrate_total = 0;
193     if( b_all )
194     {
195         /* select all valid stream */
196         for( i = 1; i < 128; i++ )
197         {
198             if( hdr->stream[i].i_cat != ASF_STREAM_UNKNOWN )
199             {
200                 hdr->stream[i].i_selected = 1;
201             }
202         }
203         return;
204     }
205     else
206     {
207         for( i = 0; i < 128; i++ )
208         {
209             hdr->stream[i].i_selected = 0; /* by default, not selected */
210         }
211     }
212
213 #if 0
214     psz_stream = config_GetPsz( p_input, "mms-stream" );
215
216     if( psz_stream && *psz_stream )
217     {
218         char *psz_tmp = psz_stream;
219         while( *psz_tmp )
220         {
221             if( *psz_tmp == ',' )
222             {
223                 psz_tmp++;
224             }
225             else
226             {
227                 int i_stream;
228                 i_stream = atoi( psz_tmp );
229                 while( *psz_tmp != '\0' && *psz_tmp != ',' )
230                 {
231                     psz_tmp++;
232                 }
233
234                 if( i_stream > 0 && i_stream < 128 &&
235                     stream[i_stream].i_cat != MMS_STREAM_UNKNOWN )
236                 {
237                     stream[i_stream].i_selected = 1;
238                 }
239             }
240         }
241         FREE( psz_stream );
242         return;
243     }
244     FREE( psz_stream );
245 #endif
246
247     /* big test:
248      * select a stream if
249      *    - no audio nor video stream
250      *    - or:
251      *         - if i_bitrate_max not set keep the highest bitrate
252      *         - if i_bitrate_max is set, keep stream that make we used best
253      *           quality regarding i_bitrate_max
254      *
255      * XXX: little buggy:
256      *        - it doesn't use mutual exclusion info..
257      *        - when selecting a better stream we could select
258      *        something that make i_bitrate_total> i_bitrate_max
259      */
260     for( i = 1; i < 128; i++ )
261     {
262         if( hdr->stream[i].i_cat == ASF_STREAM_UNKNOWN )
263         {
264             continue;
265         }
266         else if( hdr->stream[i].i_cat == ASF_STREAM_AUDIO && b_audio &&
267                  ( i_audio <= 0 ||
268                     ( ( ( hdr->stream[i].i_bitrate > hdr->stream[i_audio].i_bitrate &&
269                           ( i_bitrate_total + hdr->stream[i].i_bitrate - hdr->stream[i_audio].i_bitrate
270                                             < i_bitrate_max || !i_bitrate_max) ) ||
271                         ( hdr->stream[i].i_bitrate < hdr->stream[i_audio].i_bitrate &&
272                               i_bitrate_max != 0 && i_bitrate_total > i_bitrate_max )
273                       ) )  ) )
274         {
275             /* unselect old stream */
276             if( i_audio > 0 )
277             {
278                 hdr->stream[i_audio].i_selected = 0;
279                 if( hdr->stream[i_audio].i_bitrate> 0 )
280                 {
281                     i_bitrate_total -= hdr->stream[i_audio].i_bitrate;
282                 }
283             }
284
285             hdr->stream[i].i_selected = 1;
286             if( hdr->stream[i].i_bitrate> 0 )
287             {
288                 i_bitrate_total += hdr->stream[i].i_bitrate;
289             }
290             i_audio = i;
291         }
292         else if( hdr->stream[i].i_cat == ASF_STREAM_VIDEO && b_video &&
293                  ( i_video <= 0 ||
294                     (
295                         ( ( hdr->stream[i].i_bitrate > hdr->stream[i_video].i_bitrate &&
296                             ( i_bitrate_total + hdr->stream[i].i_bitrate - hdr->stream[i_video].i_bitrate
297                                             < i_bitrate_max || !i_bitrate_max) ) ||
298                           ( hdr->stream[i].i_bitrate < hdr->stream[i_video].i_bitrate &&
299                             i_bitrate_max != 0 && i_bitrate_total > i_bitrate_max )
300                         ) ) )  )
301         {
302             /* unselect old stream */
303
304             if( i_video > 0 )
305             {
306                 hdr->stream[i_video].i_selected = 0;
307                 if( hdr->stream[i_video].i_bitrate> 0 )
308                 {
309                     i_bitrate_total -= hdr->stream[i_video].i_bitrate;
310                 }
311             }
312
313             hdr->stream[i].i_selected = 1;
314             if( hdr->stream[i].i_bitrate> 0 )
315             {
316                 i_bitrate_total += hdr->stream[i].i_bitrate;
317             }
318             i_video = i;
319         }
320
321     }
322 #if 0
323     if( i_bitrate_max > 0 )
324     {
325         msg_Dbg( p_input,
326                  "requested bitrate:%d real bitrate:%d",
327                  i_bitrate_max, i_bitrate_total );
328     }
329     else
330     {
331         msg_Dbg( p_input,
332                  "total bitrate:%d",
333                  i_bitrate_total );
334     }
335 #endif
336 }
337