1 /*****************************************************************************
2 * asf.c: MMS access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
31 static int CmpGuid( const guid_t *p_guid1, const guid_t *p_guid2 )
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 );
46 void E_( GenerateGuid )( guid_t *p_guid )
50 srand( mdate() & 0xffffffff );
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++ )
58 p_guid->v4[i] = ( (uint64_t)rand() * 256 ) / RAND_MAX;
62 void E_( asf_HeaderParse )( asf_header_t *hdr,
63 uint8_t *p_header, int i_header )
71 hdr->i_data_packets_count = 0;
72 hdr->i_min_data_packet_size = 0;
73 for( i = 0; i < 128; i++ )
75 hdr->stream[i].i_cat = ASF_STREAM_UNKNOWN;
76 hdr->stream[i].i_selected = 0;
77 hdr->stream[i].i_bitrate = -1;
80 //fprintf( stderr, " ---------------------header:%d\n", i_header );
81 var_buffer_initread( &buffer, p_header, i_header );
83 var_buffer_getguid( &buffer, &guid );
85 if( !CmpGuid( &guid, &asf_object_header_guid ) )
88 // fprintf( stderr, " ---------------------ERROR------\n" );
90 var_buffer_getmemory( &buffer, NULL, 30 - 16 );
94 //fprintf( stderr, " ---------------------data:%d\n", buffer.i_data );
96 var_buffer_getguid( &buffer, &guid );
97 i_size = var_buffer_get64( &buffer );
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],
105 if( CmpGuid( &guid, &asf_object_file_properties_guid ) )
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 );
114 var_buffer_getmemory( &buffer, NULL, i_size - 24 - 16 - 8 - 8 - 8 - 8-8-8-4 - 4);
116 else if( CmpGuid( &guid, &asf_object_stream_properties_guid ) )
121 var_buffer_getguid( &buffer, &stream_type );
122 var_buffer_getmemory( &buffer, NULL, 32 );
123 i_stream_id = var_buffer_get8( &buffer ) & 0x7f;
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);
128 if( CmpGuid( &stream_type, &asf_object_stream_type_video ) )
130 //fprintf( stderr, "\nvideo stream[%d] found\n", i_stream_id );
131 hdr->stream[i_stream_id].i_cat = ASF_STREAM_VIDEO;
133 else if( CmpGuid( &stream_type, &asf_object_stream_type_audio ) )
135 //fprintf( stderr, "\naudio stream[%d] found\n", i_stream_id );
136 hdr->stream[i_stream_id].i_cat = ASF_STREAM_AUDIO;
140 hdr->stream[i_stream_id].i_cat = ASF_STREAM_UNKNOWN;
143 else if ( CmpGuid( &guid, &asf_object_bitrate_properties_guid ) )
148 i_count = var_buffer_get16( &buffer );
152 i_stream_id = var_buffer_get16( &buffer )&0x7f;
153 hdr->stream[i_stream_id].i_bitrate = var_buffer_get32( &buffer );
157 //fprintf( stderr, " 2---------------------skip:%lld\n", i_size - 24);
158 var_buffer_getmemory( &buffer, NULL, i_size - 24 );
162 //fprintf( stderr, " 3---------------------skip:%lld\n", i_size - 24);
164 var_buffer_getmemory( &buffer, NULL, i_size - 24 );
167 if( var_buffer_readempty( &buffer ) )
172 void E_( asf_StreamSelect ) ( asf_header_t *hdr,
174 vlc_bool_t b_all, vlc_bool_t b_audio, vlc_bool_t b_video )
176 /* XXX FIXME use mututal eclusion information */
178 int i_audio, i_video;
189 /* select all valid stream */
190 for( i = 1; i < 128; i++ )
192 if( hdr->stream[i].i_cat != ASF_STREAM_UNKNOWN )
194 hdr->stream[i].i_selected = 1;
201 for( i = 0; i < 128; i++ )
203 hdr->stream[i].i_selected = 0; /* by default, not selected */
209 * - no audio nor video stream
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
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
220 for( i = 1; i < 128; i++ )
222 if( hdr->stream[i].i_cat == ASF_STREAM_UNKNOWN )
226 else if( hdr->stream[i].i_cat == ASF_STREAM_AUDIO && b_audio &&
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 )
235 /* unselect old stream */
238 hdr->stream[i_audio].i_selected = 0;
239 if( hdr->stream[i_audio].i_bitrate> 0 )
241 i_bitrate_total -= hdr->stream[i_audio].i_bitrate;
245 hdr->stream[i].i_selected = 1;
246 if( hdr->stream[i].i_bitrate> 0 )
248 i_bitrate_total += hdr->stream[i].i_bitrate;
252 else if( hdr->stream[i].i_cat == ASF_STREAM_VIDEO && b_video &&
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 )
262 /* unselect old stream */
266 hdr->stream[i_video].i_selected = 0;
267 if( hdr->stream[i_video].i_bitrate> 0 )
269 i_bitrate_total -= hdr->stream[i_video].i_bitrate;
273 hdr->stream[i].i_selected = 1;
274 if( hdr->stream[i].i_bitrate> 0 )
276 i_bitrate_total += hdr->stream[i].i_bitrate;