]> git.sesse.net Git - vlc/blob - modules/access/mms/asf.c
Remove E_()
[vlc] / modules / access / mms / asf.c
1 /*****************************************************************************
2  * asf.c: MMS access plug-in
3  *****************************************************************************
4  * Copyright (C) 2001-2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc/vlc.h>
29
30 #include "asf.h"
31 #include "buffer.h"
32
33 static int CmpGuid( const guid_t *p_guid1, const guid_t *p_guid2 )
34 {
35     return( ( p_guid1->v1 == p_guid2->v1 &&
36               p_guid1->v2 == p_guid2->v2 &&
37               p_guid1->v3 == p_guid2->v3 &&
38               p_guid1->v4[0] == p_guid2->v4[0] &&
39               p_guid1->v4[1] == p_guid2->v4[1] &&
40               p_guid1->v4[2] == p_guid2->v4[2] &&
41               p_guid1->v4[3] == p_guid2->v4[3] &&
42               p_guid1->v4[4] == p_guid2->v4[4] &&
43               p_guid1->v4[5] == p_guid2->v4[5] &&
44               p_guid1->v4[6] == p_guid2->v4[6] &&
45               p_guid1->v4[7] == p_guid2->v4[7] ) ? 1 : 0 );
46 }
47
48 void  GenerateGuid ( guid_t *p_guid )
49 {
50     int i;
51
52     srand( mdate() & 0xffffffff );
53
54     /* FIXME should be generated using random data */
55     p_guid->v1 = 0xbabac001;
56     p_guid->v2 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
57     p_guid->v3 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
58     for( i = 0; i < 8; i++ )
59     {
60         p_guid->v4[i] = ( (uint64_t)rand() * 256 ) / RAND_MAX;
61     }
62 }
63
64 void  asf_HeaderParse ( asf_header_t *hdr,
65                             uint8_t *p_header, int i_header )
66 {
67     var_buffer_t buffer;
68     guid_t      guid;
69     uint64_t    i_size;
70     int         i;
71
72     hdr->i_file_size = 0;
73     hdr->i_data_packets_count = 0;
74     hdr->i_min_data_packet_size = 0;
75     for( i = 0; i < 128; i++ )
76     {
77         hdr->stream[i].i_cat = ASF_STREAM_UNKNOWN;
78         hdr->stream[i].i_selected = 0;
79         hdr->stream[i].i_bitrate = -1;
80     }
81
82     var_buffer_initread( &buffer, p_header, i_header );
83     var_buffer_getguid( &buffer, &guid );
84
85     if( !CmpGuid( &guid, &asf_object_header_guid ) )
86     {
87         /* ERROR: */
88     }
89     var_buffer_getmemory( &buffer, NULL, 30 - 16 );
90
91     for( ;; )
92     {
93         var_buffer_getguid( &buffer, &guid );
94         i_size = var_buffer_get64( &buffer );
95
96         if( CmpGuid( &guid, &asf_object_file_properties_guid ) )
97         {
98             var_buffer_getmemory( &buffer, NULL, 16 );
99             hdr->i_file_size            = var_buffer_get64( &buffer );
100             var_buffer_getmemory( &buffer, NULL, 8 );
101             hdr->i_data_packets_count   = var_buffer_get64( &buffer );
102             var_buffer_getmemory( &buffer, NULL, 8+8+8+4);
103             hdr->i_min_data_packet_size = var_buffer_get32( &buffer );
104
105             var_buffer_getmemory( &buffer, NULL, i_size - 24 - 16 - 8 - 8 - 8 - 8-8-8-4 - 4);
106         }
107         else if( CmpGuid( &guid, &asf_object_header_extension_guid ) )
108         {
109             /* Enter it */
110             var_buffer_getmemory( &buffer, NULL, 46 - 24 );
111         }
112         else if( CmpGuid( &guid, &asf_object_extended_stream_properties_guid ) )
113         {
114             /* Grrrrrr */
115             int16_t i_count1, i_count2;
116             int i_subsize;
117             int i;
118
119             var_buffer_getmemory( &buffer, NULL, 84 - 24 );
120
121             i_count1 = var_buffer_get16( &buffer );
122             i_count2 = var_buffer_get16( &buffer );
123
124             i_subsize = 88;
125             for( i = 0; i < i_count1; i++ )
126             {
127                 int i_len;
128
129                 var_buffer_get16( &buffer );
130                 i_len = var_buffer_get16( &buffer );
131                 var_buffer_getmemory( &buffer, NULL, i_len );
132
133                 i_subsize = 4 + i_len;
134             }
135
136             for( i = 0; i < i_count2; i++ )
137             {
138                 int i_len;
139                 var_buffer_getmemory( &buffer, NULL, 16 + 2 );
140                 i_len = var_buffer_get32( &buffer );
141                 var_buffer_getmemory( &buffer, NULL, i_len );
142
143                 i_subsize += 16 + 6 + i_len;
144             }
145
146             if( i_size - i_subsize <= 24 )
147             {
148                 var_buffer_getmemory( &buffer, NULL, i_size - i_subsize );
149             }
150             /* It's a hack we just skip the first part of the object until
151              * the embed stream properties if any (ugly, but whose fault ?) */
152         }
153         else if( CmpGuid( &guid, &asf_object_stream_properties_guid ) )
154         {
155             int     i_stream_id;
156             guid_t  stream_type;
157
158             var_buffer_getguid( &buffer, &stream_type );
159             var_buffer_getmemory( &buffer, NULL, 32 );
160
161             i_stream_id = var_buffer_get8( &buffer ) & 0x7f;
162             var_buffer_getmemory( &buffer, NULL, i_size - 24 - 32 - 16 - 1);
163
164             if( CmpGuid( &stream_type, &asf_object_stream_type_video ) )
165             {
166                 hdr->stream[i_stream_id].i_cat = ASF_STREAM_VIDEO;
167             }
168             else if( CmpGuid( &stream_type, &asf_object_stream_type_audio ) )
169             {
170                 hdr->stream[i_stream_id].i_cat = ASF_STREAM_AUDIO;
171             }
172             else
173             {
174                 hdr->stream[i_stream_id].i_cat = ASF_STREAM_UNKNOWN;
175             }
176         }
177         else if ( CmpGuid( &guid, &asf_object_bitrate_properties_guid ) )
178         {
179             int     i_count;
180             uint8_t i_stream_id;
181
182             i_count = var_buffer_get16( &buffer );
183             i_size -= 2;
184             while( i_count > 0 )
185             {
186                 i_stream_id = var_buffer_get16( &buffer )&0x7f;
187                 hdr->stream[i_stream_id].i_bitrate =  var_buffer_get32( &buffer );
188                 i_count--;
189                 i_size -= 6;
190             }
191             var_buffer_getmemory( &buffer, NULL, i_size - 24 );
192         }
193         else
194         {
195             // skip unknown guid
196             var_buffer_getmemory( &buffer, NULL, i_size - 24 );
197         }
198
199         if( var_buffer_readempty( &buffer ) )
200             return;
201     }
202 }
203
204 void  asf_StreamSelect  ( asf_header_t *hdr,
205                               int i_bitrate_max,
206                               bool b_all, bool b_audio, bool b_video )
207 {
208     /* XXX FIXME use mututal eclusion information */
209     int i;
210     int i_audio, i_video;
211     int i_bitrate_total;
212 #if 0
213     char *psz_stream;
214 #endif
215
216     i_audio = 0;
217     i_video = 0;
218     i_bitrate_total = 0;
219     if( b_all )
220     {
221         /* select all valid stream */
222         for( i = 1; i < 128; i++ )
223         {
224             if( hdr->stream[i].i_cat != ASF_STREAM_UNKNOWN )
225             {
226                 hdr->stream[i].i_selected = 1;
227             }
228         }
229         return;
230     }
231     else
232     {
233         for( i = 0; i < 128; i++ )
234         {
235             /* by default, not selected */
236             hdr->stream[i].i_selected = 0;
237         }
238     }
239
240     /* big test:
241      * select a stream if
242      *    - no audio nor video stream
243      *    - or:
244      *         - if i_bitrate_max not set keep the highest bitrate
245      *         - if i_bitrate_max is set, keep stream that make we used best
246      *           quality regarding i_bitrate_max
247      *
248      * XXX: little buggy:
249      *        - it doesn't use mutual exclusion info..
250      *        - when selecting a better stream we could select
251      *        something that make i_bitrate_total> i_bitrate_max
252      */
253     for( i = 1; i < 128; i++ )
254     {
255         if( hdr->stream[i].i_cat == ASF_STREAM_UNKNOWN )
256         {
257             continue;
258         }
259         else if( hdr->stream[i].i_cat == ASF_STREAM_AUDIO && b_audio &&
260                  ( i_audio <= 0 ||
261                     ( ( ( hdr->stream[i].i_bitrate > hdr->stream[i_audio].i_bitrate &&
262                           ( i_bitrate_total + hdr->stream[i].i_bitrate - hdr->stream[i_audio].i_bitrate
263                                             < i_bitrate_max || !i_bitrate_max) ) ||
264                         ( hdr->stream[i].i_bitrate < hdr->stream[i_audio].i_bitrate &&
265                               i_bitrate_max != 0 && i_bitrate_total > i_bitrate_max )
266                       ) )  ) )
267         {
268             /* unselect old stream */
269             if( i_audio > 0 )
270             {
271                 hdr->stream[i_audio].i_selected = 0;
272                 if( hdr->stream[i_audio].i_bitrate> 0 )
273                 {
274                     i_bitrate_total -= hdr->stream[i_audio].i_bitrate;
275                 }
276             }
277
278             hdr->stream[i].i_selected = 1;
279             if( hdr->stream[i].i_bitrate> 0 )
280             {
281                 i_bitrate_total += hdr->stream[i].i_bitrate;
282             }
283             i_audio = i;
284         }
285         else if( hdr->stream[i].i_cat == ASF_STREAM_VIDEO && b_video &&
286                  ( i_video <= 0 ||
287                     (
288                         ( ( hdr->stream[i].i_bitrate > hdr->stream[i_video].i_bitrate &&
289                             ( i_bitrate_total + hdr->stream[i].i_bitrate - hdr->stream[i_video].i_bitrate
290                                             < i_bitrate_max || !i_bitrate_max) ) ||
291                           ( hdr->stream[i].i_bitrate < hdr->stream[i_video].i_bitrate &&
292                             i_bitrate_max != 0 && i_bitrate_total > i_bitrate_max )
293                         ) ) )  )
294         {
295             /* unselect old stream */
296             if( i_video > 0 )
297             {
298                 hdr->stream[i_video].i_selected = 0;
299                 if( hdr->stream[i_video].i_bitrate> 0 )
300                 {
301                     i_bitrate_total -= hdr->stream[i_video].i_bitrate;
302                 }
303             }
304
305             hdr->stream[i].i_selected = 1;
306             if( hdr->stream[i].i_bitrate> 0 )
307             {
308                 i_bitrate_total += hdr->stream[i].i_bitrate;
309             }
310             i_video = i;
311         }
312
313     }
314 }