* mms/* : converted to access2. It should be reworked to use pf_block, but
[vlc.git] / modules / access / mms / asf.c
1 /*****************************************************************************
2  * asf.c: MMS access plug-in
3  *****************************************************************************
4  * Copyright (C) 2001-2004 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
121             var_buffer_getguid( &buffer, &stream_type );
122             var_buffer_getmemory( &buffer, NULL, 32 );
123             i_stream_id = var_buffer_get8( &buffer ) & 0x7f;
124
125             //fprintf( stderr, " 1---------------------skip:%lld\n", i_size - 24 - 32 - 16 - 1 );
126             var_buffer_getmemory( &buffer, NULL, i_size - 24 - 32 - 16 - 1);
127
128             if( CmpGuid( &stream_type, &asf_object_stream_type_video ) )
129             {
130                 //fprintf( stderr, "\nvideo stream[%d] found\n", i_stream_id );
131                 hdr->stream[i_stream_id].i_cat = ASF_STREAM_VIDEO;
132             }
133             else if( CmpGuid( &stream_type, &asf_object_stream_type_audio ) )
134             {
135                 //fprintf( stderr, "\naudio stream[%d] found\n", i_stream_id );
136                 hdr->stream[i_stream_id].i_cat = ASF_STREAM_AUDIO;
137             }
138             else
139             {
140                 hdr->stream[i_stream_id].i_cat = ASF_STREAM_UNKNOWN;
141             }
142         }
143         else if ( CmpGuid( &guid, &asf_object_bitrate_properties_guid ) )
144         {
145             int     i_count;
146             uint8_t i_stream_id;
147
148             i_count = var_buffer_get16( &buffer );
149             i_size -= 2;
150             while( i_count > 0 )
151             {
152                 i_stream_id = var_buffer_get16( &buffer )&0x7f;
153                 hdr->stream[i_stream_id].i_bitrate =  var_buffer_get32( &buffer );
154                 i_count--;
155                 i_size -= 6;
156             }
157             //fprintf( stderr, " 2---------------------skip:%lld\n", i_size - 24);
158             var_buffer_getmemory( &buffer, NULL, i_size - 24 );
159         }
160         else
161         {
162             //fprintf( stderr, " 3---------------------skip:%lld\n", i_size - 24);
163             // skip unknown guid
164             var_buffer_getmemory( &buffer, NULL, i_size - 24 );
165         }
166
167         if( var_buffer_readempty( &buffer ) )
168             return;
169     }
170 }
171
172 void E_( asf_StreamSelect ) ( asf_header_t *hdr,
173                               int i_bitrate_max,
174                               vlc_bool_t b_all, vlc_bool_t b_audio, vlc_bool_t b_video )
175 {
176     /* XXX FIXME use mututal eclusion information */
177     int i;
178     int i_audio, i_video;
179     int i_bitrate_total;
180 #if 0
181     char *psz_stream;
182 #endif
183
184     i_audio = 0;
185     i_video = 0;
186     i_bitrate_total = 0;
187     if( b_all )
188     {
189         /* select all valid stream */
190         for( i = 1; i < 128; i++ )
191         {
192             if( hdr->stream[i].i_cat != ASF_STREAM_UNKNOWN )
193             {
194                 hdr->stream[i].i_selected = 1;
195             }
196         }
197         return;
198     }
199     else
200     {
201         for( i = 0; i < 128; i++ )
202         {
203             hdr->stream[i].i_selected = 0; /* by default, not selected */
204         }
205     }
206
207     /* big test:
208      * select a stream if
209      *    - no audio nor video stream
210      *    - or:
211      *         - if i_bitrate_max not set keep the highest bitrate
212      *         - if i_bitrate_max is set, keep stream that make we used best
213      *           quality regarding i_bitrate_max
214      *
215      * XXX: little buggy:
216      *        - it doesn't use mutual exclusion info..
217      *        - when selecting a better stream we could select
218      *        something that make i_bitrate_total> i_bitrate_max
219      */
220     for( i = 1; i < 128; i++ )
221     {
222         if( hdr->stream[i].i_cat == ASF_STREAM_UNKNOWN )
223         {
224             continue;
225         }
226         else if( hdr->stream[i].i_cat == ASF_STREAM_AUDIO && b_audio &&
227                  ( i_audio <= 0 ||
228                     ( ( ( hdr->stream[i].i_bitrate > hdr->stream[i_audio].i_bitrate &&
229                           ( i_bitrate_total + hdr->stream[i].i_bitrate - hdr->stream[i_audio].i_bitrate
230                                             < i_bitrate_max || !i_bitrate_max) ) ||
231                         ( hdr->stream[i].i_bitrate < hdr->stream[i_audio].i_bitrate &&
232                               i_bitrate_max != 0 && i_bitrate_total > i_bitrate_max )
233                       ) )  ) )
234         {
235             /* unselect old stream */
236             if( i_audio > 0 )
237             {
238                 hdr->stream[i_audio].i_selected = 0;
239                 if( hdr->stream[i_audio].i_bitrate> 0 )
240                 {
241                     i_bitrate_total -= hdr->stream[i_audio].i_bitrate;
242                 }
243             }
244
245             hdr->stream[i].i_selected = 1;
246             if( hdr->stream[i].i_bitrate> 0 )
247             {
248                 i_bitrate_total += hdr->stream[i].i_bitrate;
249             }
250             i_audio = i;
251         }
252         else if( hdr->stream[i].i_cat == ASF_STREAM_VIDEO && b_video &&
253                  ( i_video <= 0 ||
254                     (
255                         ( ( hdr->stream[i].i_bitrate > hdr->stream[i_video].i_bitrate &&
256                             ( i_bitrate_total + hdr->stream[i].i_bitrate - hdr->stream[i_video].i_bitrate
257                                             < i_bitrate_max || !i_bitrate_max) ) ||
258                           ( hdr->stream[i].i_bitrate < hdr->stream[i_video].i_bitrate &&
259                             i_bitrate_max != 0 && i_bitrate_total > i_bitrate_max )
260                         ) ) )  )
261         {
262             /* unselect old stream */
263
264             if( i_video > 0 )
265             {
266                 hdr->stream[i_video].i_selected = 0;
267                 if( hdr->stream[i_video].i_bitrate> 0 )
268                 {
269                     i_bitrate_total -= hdr->stream[i_video].i_bitrate;
270                 }
271             }
272
273             hdr->stream[i].i_selected = 1;
274             if( hdr->stream[i].i_bitrate> 0 )
275             {
276                 i_bitrate_total += hdr->stream[i].i_bitrate;
277             }
278             i_video = i;
279         }
280
281     }
282 }
283