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