1 /*****************************************************************************
2 * asf.c: MMS access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001-2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
28 #include <vlc_common.h>
34 static int CmpGuid( const guid_t *p_guid1, const guid_t *p_guid2 )
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 );
49 void GenerateGuid ( guid_t *p_guid )
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));
57 void asf_HeaderParse ( asf_header_t *hdr,
58 uint8_t *p_header, int i_header )
65 hdr->i_data_packets_count = 0;
66 hdr->i_min_data_packet_size = 0;
67 for( unsigned i = 0; i < 128; i++ )
69 hdr->stream[i].i_cat = ASF_STREAM_UNKNOWN;
70 hdr->stream[i].i_selected = 0;
71 hdr->stream[i].i_bitrate = -1;
74 var_buffer_initread( &buffer, p_header, i_header );
75 var_buffer_getguid( &buffer, &guid );
77 if( !CmpGuid( &guid, &asf_object_header_guid ) )
81 var_buffer_getmemory( &buffer, NULL, 30 - 16 );
85 var_buffer_getguid( &buffer, &guid );
86 i_size = var_buffer_get64( &buffer );
88 if( CmpGuid( &guid, &asf_object_file_properties_guid ) )
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 );
97 var_buffer_getmemory( &buffer, NULL, i_size - 24 - 16 - 8 - 8 - 8 - 8-8-8-4 - 4);
99 else if( CmpGuid( &guid, &asf_object_header_extension_guid ) )
102 var_buffer_getmemory( &buffer, NULL, 46 - 24 );
104 else if( CmpGuid( &guid, &asf_object_extended_stream_properties_guid ) )
107 int16_t i_count1, i_count2;
110 var_buffer_getmemory( &buffer, NULL, 84 - 24 );
112 i_count1 = var_buffer_get16( &buffer );
113 i_count2 = var_buffer_get16( &buffer );
116 for( int i = 0; i < i_count1; i++ )
120 var_buffer_get16( &buffer );
121 i_len = var_buffer_get16( &buffer );
122 var_buffer_getmemory( &buffer, NULL, i_len );
124 i_subsize += 4 + i_len;
127 for( int i = 0; i < i_count2; i++ )
130 var_buffer_getmemory( &buffer, NULL, 16 + 2 );
131 i_len = var_buffer_get32( &buffer );
132 var_buffer_getmemory( &buffer, NULL, i_len );
134 i_subsize += 16 + 6 + i_len;
137 if( i_size - i_subsize <= 24 )
139 var_buffer_getmemory( &buffer, NULL, i_size - i_subsize );
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 ?) */
144 else if( CmpGuid( &guid, &asf_object_stream_properties_guid ) )
149 var_buffer_getguid( &buffer, &stream_type );
150 var_buffer_getmemory( &buffer, NULL, 32 );
152 i_stream_id = var_buffer_get8( &buffer ) & 0x7f;
153 var_buffer_getmemory( &buffer, NULL, i_size - 24 - 32 - 16 - 1);
155 if( CmpGuid( &stream_type, &asf_object_stream_type_video ) )
157 hdr->stream[i_stream_id].i_cat = ASF_STREAM_VIDEO;
159 else if( CmpGuid( &stream_type, &asf_object_stream_type_audio ) )
161 hdr->stream[i_stream_id].i_cat = ASF_STREAM_AUDIO;
165 hdr->stream[i_stream_id].i_cat = ASF_STREAM_UNKNOWN;
168 else if ( CmpGuid( &guid, &asf_object_bitrate_properties_guid ) )
173 i_count = var_buffer_get16( &buffer );
177 i_stream_id = var_buffer_get16( &buffer )&0x7f;
178 hdr->stream[i_stream_id].i_bitrate = var_buffer_get32( &buffer );
182 var_buffer_getmemory( &buffer, NULL, i_size - 24 );
187 var_buffer_getmemory( &buffer, NULL, i_size - 24 );
190 if( var_buffer_readempty( &buffer ) )
195 void asf_StreamSelect ( asf_header_t *hdr,
197 bool b_all, bool b_audio, bool b_video )
199 /* XXX FIXME use mututal eclusion information */
201 int i_audio, i_video;
212 /* select all valid stream */
213 for( i = 1; i < 128; i++ )
215 if( hdr->stream[i].i_cat != ASF_STREAM_UNKNOWN )
217 hdr->stream[i].i_selected = 1;
224 for( i = 0; i < 128; i++ )
226 /* by default, not selected */
227 hdr->stream[i].i_selected = 0;
233 * - no audio nor video stream
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
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
244 for( i = 1; i < 128; i++ )
246 if( hdr->stream[i].i_cat == ASF_STREAM_UNKNOWN )
250 else if( hdr->stream[i].i_cat == ASF_STREAM_AUDIO && b_audio &&
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 )
259 /* unselect old stream */
262 hdr->stream[i_audio].i_selected = 0;
263 if( hdr->stream[i_audio].i_bitrate> 0 )
265 i_bitrate_total -= hdr->stream[i_audio].i_bitrate;
269 hdr->stream[i].i_selected = 1;
270 if( hdr->stream[i].i_bitrate> 0 )
272 i_bitrate_total += hdr->stream[i].i_bitrate;
276 else if( hdr->stream[i].i_cat == ASF_STREAM_VIDEO && b_video &&
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 )
286 /* unselect old stream */
289 hdr->stream[i_video].i_selected = 0;
290 if( hdr->stream[i_video].i_bitrate> 0 )
292 i_bitrate_total -= hdr->stream[i_video].i_bitrate;
296 hdr->stream[i].i_selected = 1;
297 if( hdr->stream[i].i_bitrate> 0 )
299 i_bitrate_total += hdr->stream[i].i_bitrate;