📄 libasf.c
字号:
/***************************************************************************** * libasf.c : asf stream demux module for vlc ***************************************************************************** * Copyright (C) 2001-2003 VideoLAN * $Id: libasf.c 10813 2005-04-26 07:21:31Z fenrir $ * * Authors: Laurent Aimar <fenrir@via.ecp.fr> * Gildas Bazin <gbazin@videolan.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. *****************************************************************************/#include <stdlib.h> /* malloc(), free() */#include <vlc/vlc.h>#include <vlc/input.h>#include "codecs.h" /* BITMAPINFOHEADER, WAVEFORMATEX */#include "libasf.h"#define ASF_DEBUG 1#define FREE( p ) \ if( p ) {free( p ); p = NULL; }#define GUID_FMT "0x%x-0x%x-0x%x-0x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x"#define GUID_PRINT( guid ) \ (guid).v1, \ (guid).v2, \ (guid).v3, \ (guid).v4[0],(guid).v4[1],(guid).v4[2],(guid).v4[3], \ (guid).v4[4],(guid).v4[5],(guid).v4[6],(guid).v4[7]/**************************************************************************** * ****************************************************************************/static int ASF_ReadObject( stream_t *, asf_object_t *, asf_object_t * );/**************************************************************************** * GUID functions ****************************************************************************/void ASF_GetGUID( guid_t *p_guid, uint8_t *p_data ){ p_guid->v1 = GetDWLE( p_data ); p_guid->v2 = GetWLE( p_data + 4); p_guid->v3 = GetWLE( p_data + 6); memcpy( p_guid->v4, p_data + 8, 8 );}int ASF_CmpGUID( const guid_t *p_guid1, const guid_t *p_guid2 ){ if( (p_guid1->v1 != p_guid2->v1 )|| (p_guid1->v2 != p_guid2->v2 )|| (p_guid1->v3 != p_guid2->v3 )|| ( memcmp( p_guid1->v4, p_guid2->v4,8 )) ) { return( 0 ); } return( 1 ); /* match */}/**************************************************************************** * ****************************************************************************/static int ASF_ReadObjectCommon( stream_t *s, asf_object_t *p_obj ){ asf_object_common_t *p_common = (asf_object_common_t*)p_obj; uint8_t *p_peek; if( stream_Peek( s, &p_peek, 24 ) < 24 ) { return( VLC_EGENERIC ); } ASF_GetGUID( &p_common->i_object_id, p_peek ); p_common->i_object_size = GetQWLE( p_peek + 16 ); p_common->i_object_pos = stream_Tell( s ); p_common->p_next = NULL;#ifdef ASF_DEBUG msg_Dbg( s, "found object guid: " GUID_FMT " size:"I64Fd, GUID_PRINT( p_common->i_object_id ), p_common->i_object_size );#endif return( VLC_SUCCESS );}static int ASF_NextObject( stream_t *s, asf_object_t *p_obj ){ asf_object_t obj; if( p_obj == NULL ) { if( ASF_ReadObjectCommon( s, &obj ) ) { return( VLC_EGENERIC ); } p_obj = &obj; } if( p_obj->common.i_object_size <= 0 ) { return VLC_EGENERIC; } if( p_obj->common.p_father && p_obj->common.p_father->common.i_object_size != 0 ) { if( p_obj->common.p_father->common.i_object_pos + p_obj->common.p_father->common.i_object_size < p_obj->common.i_object_pos + p_obj->common.i_object_size + 24 ) /* 24 is min size of an object */ { return VLC_EGENERIC; } } return stream_Seek( s, p_obj->common.i_object_pos + p_obj->common.i_object_size );}static void ASF_FreeObject_Null( asf_object_t *pp_obj ){ return;}static int ASF_ReadObject_Header( stream_t *s, asf_object_t *p_obj ){ asf_object_header_t *p_hdr = (asf_object_header_t*)p_obj; asf_object_t *p_subobj; int i_peek; uint8_t *p_peek; if( ( i_peek = stream_Peek( s, &p_peek, 30 ) ) < 30 ) { return( VLC_EGENERIC ); } p_hdr->i_sub_object_count = GetDWLE( p_peek + 24 ); p_hdr->i_reserved1 = p_peek[28]; p_hdr->i_reserved2 = p_peek[29]; p_hdr->p_first = NULL; p_hdr->p_last = NULL;#ifdef ASF_DEBUG msg_Dbg( s, "read \"header object\" subobj:%d, reserved1:%d, reserved2:%d", p_hdr->i_sub_object_count, p_hdr->i_reserved1, p_hdr->i_reserved2 );#endif /* Cannot fail as peek succeed */ stream_Read( s, NULL, 30 ); /* Now load sub object */ for( ; ; ) { p_subobj = malloc( sizeof( asf_object_t ) ); if( ASF_ReadObject( s, p_subobj, (asf_object_t*)p_hdr ) ) { free( p_subobj ); break; } if( ASF_NextObject( s, p_subobj ) ) /* Go to the next object */ { break; } } return VLC_SUCCESS;}static int ASF_ReadObject_Data( stream_t *s, asf_object_t *p_obj ){ asf_object_data_t *p_data = (asf_object_data_t*)p_obj; int i_peek; uint8_t *p_peek; if( ( i_peek = stream_Peek( s, &p_peek, 50 ) ) < 50 ) { return VLC_EGENERIC; } ASF_GetGUID( &p_data->i_file_id, p_peek + 24 ); p_data->i_total_data_packets = GetQWLE( p_peek + 40 ); p_data->i_reserved = GetWLE( p_peek + 48 );#ifdef ASF_DEBUG msg_Dbg( s, "read \"data object\" file_id:" GUID_FMT " total data packet:" I64Fd" reserved:%d", GUID_PRINT( p_data->i_file_id ), p_data->i_total_data_packets, p_data->i_reserved );#endif return VLC_SUCCESS;}static int ASF_ReadObject_Index( stream_t *s, asf_object_t *p_obj ){ asf_object_index_t *p_index = (asf_object_index_t*)p_obj; int i_peek; uint8_t *p_peek; if( ( i_peek = stream_Peek( s, &p_peek, 56 ) ) < 56 ) { return VLC_EGENERIC; } ASF_GetGUID( &p_index->i_file_id, p_peek + 24 ); p_index->i_index_entry_time_interval = GetQWLE( p_peek + 40 ); p_index->i_max_packet_count = GetDWLE( p_peek + 48 ); p_index->i_index_entry_count = GetDWLE( p_peek + 52 ); p_index->index_entry = NULL; /* FIXME */#ifdef ASF_DEBUG msg_Dbg( s, "read \"index object\" file_id:" GUID_FMT " index_entry_time_interval:"I64Fd" max_packet_count:%d " "index_entry_count:%ld", GUID_PRINT( p_index->i_file_id ), p_index->i_index_entry_time_interval, p_index->i_max_packet_count, (long int)p_index->i_index_entry_count );#endif return VLC_SUCCESS;}static void ASF_FreeObject_Index( asf_object_t *p_obj ){ asf_object_index_t *p_index = (asf_object_index_t*)p_obj; FREE( p_index->index_entry );}static int ASF_ReadObject_file_properties( stream_t *s, asf_object_t *p_obj ){ asf_object_file_properties_t *p_fp = (asf_object_file_properties_t*)p_obj; int i_peek; uint8_t *p_peek; if( ( i_peek = stream_Peek( s, &p_peek, 92) ) < 92 ) { return VLC_EGENERIC; } ASF_GetGUID( &p_fp->i_file_id, p_peek + 24 ); p_fp->i_file_size = GetQWLE( p_peek + 40 ); p_fp->i_creation_date = GetQWLE( p_peek + 48 ); p_fp->i_data_packets_count = GetQWLE( p_peek + 56 ); p_fp->i_play_duration = GetQWLE( p_peek + 64 ); p_fp->i_send_duration = GetQWLE( p_peek + 72 ); p_fp->i_preroll = GetQWLE( p_peek + 80 ); p_fp->i_flags = GetDWLE( p_peek + 88 ); p_fp->i_min_data_packet_size = GetDWLE( p_peek + 92 ); p_fp->i_max_data_packet_size = GetDWLE( p_peek + 96 ); p_fp->i_max_bitrate = GetDWLE( p_peek + 100 );#ifdef ASF_DEBUG msg_Dbg( s, "read \"file properties object\" file_id:" GUID_FMT " file_size:"I64Fd" creation_date:"I64Fd" data_packets_count:" I64Fd" play_duration:"I64Fd" send_duration:"I64Fd" preroll:" I64Fd" flags:%d min_data_packet_size:%d max_data_packet_size:%d " "max_bitrate:%d", GUID_PRINT( p_fp->i_file_id ), p_fp->i_file_size, p_fp->i_creation_date, p_fp->i_data_packets_count, p_fp->i_play_duration, p_fp->i_send_duration, p_fp->i_preroll, p_fp->i_flags, p_fp->i_min_data_packet_size, p_fp->i_max_data_packet_size, p_fp->i_max_bitrate );#endif return VLC_SUCCESS;}static void ASF_FreeObject_metadata( asf_object_t *p_obj ){ asf_object_metadata_t *p_meta = (asf_object_metadata_t *)p_obj; unsigned int i; for( i = 0; i < p_meta->i_record_entries_count; i++ ) { if( p_meta->record[i].psz_name ) free( p_meta->record[i].psz_name ); if( p_meta->record[i].p_data ) free( p_meta->record[i].p_data ); } if( p_meta->record ) free( p_meta->record );}static int ASF_ReadObject_metadata( stream_t *s, asf_object_t *p_obj ){ asf_object_metadata_t *p_meta = (asf_object_metadata_t *)p_obj; int i_peek, i_entries, i; uint8_t *p_peek; p_meta->i_record_entries_count = 0; p_meta->record = 0; if( stream_Peek( s, &p_peek, p_meta->i_object_size ) < (int)p_meta->i_object_size ) { return VLC_EGENERIC; } i_peek = 24; i_entries = GetWLE( p_peek + i_peek ); i_peek += 2; for( i = 0; i < i_entries && i_peek < (int)p_meta->i_object_size -12; i++ ) { asf_metadata_record_t record; int i_name, i_data, j; if( GetWLE( p_peek + i_peek ) != 0 ) { ASF_FreeObject_metadata( p_obj ); return VLC_EGENERIC; } i_peek += 2; record.i_stream = GetWLE( p_peek + i_peek ); i_peek += 2; i_name = GetWLE( p_peek + i_peek ); i_peek += 2; record.i_type = GetWLE( p_peek + i_peek ); i_peek += 2; i_data = GetDWLE( p_peek + i_peek ); i_peek += 4; if( record.i_type > ASF_METADATA_TYPE_WORD || i_peek + i_data + i_name > (int)p_meta->i_object_size ) { ASF_FreeObject_metadata( p_obj ); return VLC_EGENERIC; } record.i_val = 0; record.i_data = 0; record.p_data = 0; /* get name */ record.psz_name = malloc( i_name/2 + 1 ); for( j = 0; j < i_name/2; j++ ) { record.psz_name[j] = GetWLE( p_peek + i_peek ); i_peek += 2; } record.psz_name[j] = 0; /* just to make sure */ /* get data */ if( record.i_type == ASF_METADATA_TYPE_STRING ) { record.p_data = malloc( i_data/2 + 1 ); record.i_data = i_data/2; for( j = 0; j < i_data/2; j++ ) { record.p_data[j] = GetWLE( p_peek + i_peek ); i_peek += 2; } record.p_data[j] = 0; /* just to make sure */ } else if( record.i_type == ASF_METADATA_TYPE_BYTE ) { record.p_data = malloc( i_data ); record.i_data = i_data; memcpy( record.p_data, p_peek + i_peek, i_data ); p_peek += i_data; } else { if( record.i_type == ASF_METADATA_TYPE_QWORD ) { record.i_val = GetQWLE( p_peek + i_peek ); i_peek += 8; } else if( record.i_type == ASF_METADATA_TYPE_DWORD ) { record.i_val = GetDWLE( p_peek + i_peek ); i_peek += 4; } else { record.i_val = GetWLE( p_peek + i_peek ); i_peek += 2; } } p_meta->i_record_entries_count++; p_meta->record = realloc( p_meta->record, p_meta->i_record_entries_count * sizeof(asf_metadata_record_t) ); memcpy( &p_meta->record[p_meta->i_record_entries_count-1], &record, sizeof(asf_metadata_record_t) );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -