📄 libasf.c
字号:
/***************************************************************************** * libasf.c : asf stream demux module for vlc ***************************************************************************** * Copyright (C) 2001-2003 the VideoLAN team * $Id$ * * 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/#ifdef HAVE_CONFIG_H# include "config.h"#endif#include <vlc_common.h>#include <vlc_demux.h>#include <vlc_codecs.h> /* BITMAPINFOHEADER, WAVEFORMATEX */#include "libasf.h"#define ASF_DEBUG 1#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]/* Helpers: * They ensure that invalid reads will not create problems. * They are expansion safe * They make the following assumptions: * const uint8_t *p_peek exists and points to the start of a buffer * int i_peek gives the size of the buffer pointed by p_peek * const uint8_t *p_data exits and points to the data inside p_peek to be read. *//* ASF_HAVE(n): * Check that n bytes can be read */static inline bool AsfObjectHelperHave( const uint8_t *p_peek, int i_peek, const uint8_t *p_current, int i_wanted ){ if( i_wanted < 0 || i_wanted > i_peek ) return false; return &p_current[i_wanted] <= &p_peek[i_peek];}#define ASF_HAVE(n) AsfObjectHelperHave( p_peek, i_peek, p_data, n )/* ASF_SKIP(n) * Skip n bytes if possible */static inline void AsfObjectHelperSkip( const uint8_t *p_peek, int i_peek, uint8_t **pp_data, int i_wanted ){ if( AsfObjectHelperHave( p_peek, i_peek, *pp_data, i_wanted ) ) *pp_data += i_wanted; else *pp_data = (uint8_t*)&p_peek[i_peek];}#define ASF_SKIP(n) AsfObjectHelperSkip( p_peek, i_peek, (uint8_t**)&p_data, n )/* ASF_READX() * Read X byte if possible, else return 0 */#define ASF_FUNCTION_READ_X(type, x, cmd ) \static inline type AsfObjectHelperRead##x( const uint8_t *p_peek, int i_peek, uint8_t **pp_data ) { \ uint8_t *p_data = *pp_data; \ type i_ret = 0; \ if( ASF_HAVE(x) ) \ i_ret = cmd; \ ASF_SKIP(x); \ *pp_data = p_data; \ return i_ret; }ASF_FUNCTION_READ_X( uint8_t, 1, *p_data )ASF_FUNCTION_READ_X( uint16_t, 2, GetWLE(p_data) )ASF_FUNCTION_READ_X( uint32_t, 4, GetDWLE(p_data) )ASF_FUNCTION_READ_X( uint64_t, 8, GetQWLE(p_data) )#define ASF_READ1() AsfObjectHelperRead1( p_peek, i_peek, (uint8_t**)&p_data )#define ASF_READ2() AsfObjectHelperRead2( p_peek, i_peek, (uint8_t**)&p_data )#define ASF_READ4() AsfObjectHelperRead4( p_peek, i_peek, (uint8_t**)&p_data )#define ASF_READ8() AsfObjectHelperRead8( p_peek, i_peek, (uint8_t**)&p_data )/* ASF_READS(n) * Read a string of n/2 wchar long ie n bytes. Do a stupid conversion (suppose latin1) * Return allocated "" if not possible */static char *AsfObjectHelperReadString( const uint8_t *p_peek, int i_peek, uint8_t **pp_data, int i_size ){ uint8_t *p_data = *pp_data; char *psz_string; if( ASF_HAVE(i_size) ) { psz_string = calloc( i_size/2 + 1, sizeof( char ) ); if( psz_string ) { int i; for( i = 0; i < i_size/2; i++ ) psz_string[i] = GetWLE( &p_data[2*i] ); psz_string[i_size/2] = '\0'; \ } } else { psz_string = strdup(""); } ASF_SKIP(i_size); *pp_data = p_data; return psz_string;}#define ASF_READS(n) AsfObjectHelperReadString( p_peek, i_peek, (uint8_t**)&p_data, n )/**************************************************************************** * ****************************************************************************/static int ASF_ReadObject( stream_t *, asf_object_t *, asf_object_t * );/**************************************************************************** * GUID functions ****************************************************************************/void ASF_GetGUID( guid_t *p_guid, const 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 );}bool 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 false; } return true;}/**************************************************************************** * ****************************************************************************/static int ASF_ReadObjectCommon( stream_t *s, asf_object_t *p_obj ){ asf_object_common_t *p_common = &p_obj->common; const 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:%"PRId64, 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 ){ VLC_UNUSED(pp_obj);}static int ASF_ReadObject_Header( stream_t *s, asf_object_t *p_obj ){ asf_object_header_t *p_hdr = &p_obj->header; asf_object_t *p_subobj; int i_peek; const 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( !p_subobj || 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 = &p_obj->data; int i_peek; const 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:" "%"PRId64" 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 = &p_obj->index; const uint8_t *p_peek; unsigned int i; /* We just ignore error on the index */ if( stream_Peek( s, &p_peek, p_index->i_object_size ) < __MAX( (int64_t)p_index->i_object_size, 56 ) ) return VLC_SUCCESS; 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;#ifdef ASF_DEBUG msg_Dbg( s, "read \"index object\" file_id:" GUID_FMT " index_entry_time_interval:%"PRId64" 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 /* Sanity checking */ if( p_index->i_index_entry_count > (p_index->i_object_size - 56) / 6 ) p_index->i_index_entry_count = (p_index->i_object_size - 56) / 6; p_index->index_entry = calloc( p_index->i_index_entry_count, sizeof(asf_index_entry_t) ); if( !p_index->index_entry ) return VLC_ENOMEM; for( i = 0, p_peek += 56; i < p_index->i_index_entry_count; i++, p_peek += 6 ) { p_index->index_entry[i].i_packet_number = GetDWLE( p_peek ); p_index->index_entry[i].i_packet_count = GetDWLE( p_peek + 4 ); } return VLC_SUCCESS;}static void ASF_FreeObject_Index( asf_object_t *p_obj ){ asf_object_index_t *p_index = &p_obj->index; FREENULL( 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 = &p_obj->file_properties; int i_peek; const uint8_t *p_peek; if( ( i_peek = stream_Peek( s, &p_peek, 104 ) ) < 104 ) 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:%"PRId64" creation_date:%"PRId64" data_packets_count:" "%"PRId64" play_duration:%"PRId64" send_duration:%"PRId64" preroll:%"PRId64 " 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 = &p_obj->metadata; unsigned int i; for( i = 0; i < p_meta->i_record_entries_count; i++ ) { free( p_meta->record[i].psz_name ); free( p_meta->record[i].p_data ); } free( p_meta->record );}static int ASF_ReadObject_metadata( stream_t *s, asf_object_t *p_obj ){ asf_object_metadata_t *p_meta = &p_obj->metadata; int i_peek; unsigned int i; const uint8_t *p_peek, *p_data;#ifdef ASF_DEBUG unsigned int j;#endif if( ( i_peek = stream_Peek( s, &p_peek, p_meta->i_object_size ) ) < __MAX( (int64_t)p_meta->i_object_size, 26 ) ) return VLC_EGENERIC; p_meta->i_record_entries_count = GetWLE( p_peek + 24 ); p_data = p_peek + 26; p_meta->record = calloc( p_meta->i_record_entries_count, sizeof(asf_metadata_record_t) ); if( !p_meta->record ) return VLC_ENOMEM; for( i = 0; i < p_meta->i_record_entries_count; i++ ) { asf_metadata_record_t *p_record = &p_meta->record[i]; int i_name; int i_data; if( !ASF_HAVE( 2+2+2+2+4 ) ) break; if( ASF_READ2() != 0 ) break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -