📄 asf.c
字号:
/***************************************************************************** * asf.c : ASF demux module ***************************************************************************** * Copyright (C) 2002-2003 VideoLAN * $Id: asf.c 10150 2005-03-05 17:54:19Z gbazin $ * * Authors: Laurent Aimar <fenrir@via.ecp.fr> * * 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. *****************************************************************************//***************************************************************************** * Preamble *****************************************************************************/#include <stdlib.h> /* malloc(), free() */#include <vlc/vlc.h>#include <vlc/input.h>#include "vlc_meta.h"#include "codecs.h" /* BITMAPINFOHEADER, WAVEFORMATEX */#include "libasf.h"/* TODO * - add support for the newly added object: language, bitrate, * extended stream properties. *//***************************************************************************** * Module descriptor *****************************************************************************/static int Open ( vlc_object_t * );static void Close ( vlc_object_t * );vlc_module_begin(); set_category( CAT_INPUT ); set_subcategory( SUBCAT_INPUT_DEMUX ); set_description( _("ASF v1.0 demuxer") ); set_capability( "demux2", 200 ); set_callbacks( Open, Close ); add_shortcut( "asf" );vlc_module_end();/***************************************************************************** * Local prototypes *****************************************************************************/static int Demux ( demux_t * );static int Control( demux_t *, int i_query, va_list args );typedef struct{ int i_cat; es_out_id_t *p_es; asf_object_stream_properties_t *p_sp; mtime_t i_time; block_t *p_frame; /* use to gather complete frame */} asf_track_t;struct demux_sys_t{ mtime_t i_time; /* 祍 */ mtime_t i_length; /* length of file file */ int64_t i_bitrate; /* global file bitrate */ asf_object_root_t *p_root; asf_object_file_properties_t *p_fp; unsigned int i_track; asf_track_t *track[128]; int64_t i_data_begin; int64_t i_data_end; vlc_meta_t *meta;};static mtime_t GetMoviePTS( demux_sys_t * );static int DemuxInit( demux_t * );static void DemuxEnd( demux_t * );static int DemuxPacket( demux_t * );/***************************************************************************** * Open: check file and initializes ASF structures *****************************************************************************/static int Open( vlc_object_t * p_this ){ demux_t *p_demux = (demux_t *)p_this; demux_sys_t *p_sys; guid_t guid; uint8_t *p_peek; /* A little test to see if it could be a asf stream */ if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 ) return VLC_EGENERIC; ASF_GetGUID( &guid, p_peek ); if( !ASF_CmpGUID( &guid, &asf_object_header_guid ) ) return VLC_EGENERIC; /* Set p_demux fields */ p_demux->pf_demux = Demux; p_demux->pf_control = Control; p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) ); memset( p_sys, 0, sizeof( demux_sys_t ) ); /* Load the headers */ if( DemuxInit( p_demux ) ) { return VLC_EGENERIC; } return VLC_SUCCESS;}/***************************************************************************** * Demux: read packet and send them to decoders *****************************************************************************/static int Demux( demux_t *p_demux ){ demux_sys_t *p_sys = p_demux->p_sys; for( ;; ) { uint8_t *p_peek; mtime_t i_length; mtime_t i_time_begin = GetMoviePTS( p_sys ); int i_result; if( p_demux->b_die ) { break; } /* Check if we have concatenated files */ if( stream_Peek( p_demux->s, &p_peek, 16 ) == 16 ) { guid_t guid; ASF_GetGUID( &guid, p_peek ); if( ASF_CmpGUID( &guid, &asf_object_header_guid ) ) { msg_Warn( p_demux, "Found a new ASF header" ); /* We end this stream */ DemuxEnd( p_demux ); /* And we prepare to read the next one */ if( DemuxInit( p_demux ) ) { msg_Err( p_demux, "failed to load the new header" ); return 0; } continue; } } /* Read and demux a packet */ if( ( i_result = DemuxPacket( p_demux ) ) <= 0 ) { return i_result; } if( i_time_begin == -1 ) { i_time_begin = GetMoviePTS( p_sys ); } else { i_length = GetMoviePTS( p_sys ) - i_time_begin; if( i_length < 0 || i_length >= 40 * 1000 ) { break; } } } /* Set the PCR */ p_sys->i_time = GetMoviePTS( p_sys ); if( p_sys->i_time >= 0 ) { es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time ); } return 1;}/***************************************************************************** * Close: frees unused data *****************************************************************************/static void Close( vlc_object_t * p_this ){ demux_t *p_demux = (demux_t *)p_this; DemuxEnd( p_demux ); free( p_demux->p_sys );}/***************************************************************************** * Control: *****************************************************************************/static int Control( demux_t *p_demux, int i_query, va_list args ){ demux_sys_t *p_sys = p_demux->p_sys; int64_t *pi64; int i; vlc_meta_t **pp_meta; switch( i_query ) { case DEMUX_SET_TIME: return VLC_EGENERIC; case DEMUX_GET_LENGTH: pi64 = (int64_t*)va_arg( args, int64_t * ); *pi64 = p_sys->i_length; return VLC_SUCCESS; case DEMUX_GET_META: pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** ); *pp_meta = vlc_meta_Duplicate( p_sys->meta ); return VLC_SUCCESS; case DEMUX_SET_POSITION: p_sys->i_time = -1; for( i = 0; i < 128 ; i++ ) { asf_track_t *tk = p_sys->track[i]; if( tk ) tk->i_time = -1; } default: return demux2_vaControlHelper( p_demux->s, p_sys->i_data_begin, p_sys->i_data_end, p_sys->i_bitrate, p_sys->p_fp->i_min_data_packet_size, i_query, args ); }}/***************************************************************************** * *****************************************************************************/static mtime_t GetMoviePTS( demux_sys_t *p_sys ){ mtime_t i_time; int i; i_time = -1; for( i = 0; i < 128 ; i++ ) { asf_track_t *tk = p_sys->track[i]; if( tk && tk->p_es && tk->i_time > 0) { if( i_time < 0 ) { i_time = tk->i_time; } else { i_time = __MIN( i_time, tk->i_time ); } } } return i_time;}#define GETVALUE2b( bits, var, def ) \ switch( (bits)&0x03 ) \ { \ case 1: var = p_peek[i_skip]; i_skip++; break; \ case 2: var = GetWLE( p_peek + i_skip ); i_skip+= 2; break; \ case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \ case 0: \ default: var = def; break;\ }static int DemuxPacket( demux_t *p_demux ){ demux_sys_t *p_sys = p_demux->p_sys; int i_data_packet_min = p_sys->p_fp->i_min_data_packet_size; uint8_t *p_peek; int i_skip; int i_packet_size_left; int i_packet_flags; int i_packet_property; int b_packet_multiple_payload; int i_packet_length; int i_packet_sequence; int i_packet_padding_length; uint32_t i_packet_send_time; uint16_t i_packet_duration; int i_payload; int i_payload_count; int i_payload_length_type; if( stream_Peek( p_demux->s, &p_peek,i_data_packet_min)<i_data_packet_min ) { msg_Warn( p_demux, "cannot peek while getting new packet, EOF ?" ); return 0; } i_skip = 0; /* *** parse error correction if present *** */ if( p_peek[0]&0x80 ) { unsigned int i_error_correction_length_type; unsigned int i_error_correction_data_length; unsigned int i_opaque_data_present; i_error_correction_data_length = p_peek[0] & 0x0f; // 4bits i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01; // 1bit i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits i_skip += 1; // skip error correction flags if( i_error_correction_length_type != 0x00 || i_opaque_data_present != 0 || i_error_correction_data_length != 0x02 ) { goto loop_error_recovery; } i_skip += i_error_correction_data_length; } else { msg_Warn( p_demux, "p_peek[0]&0x80 != 0x80" ); } /* sanity check */ if( i_skip + 2 >= i_data_packet_min ) { goto loop_error_recovery; } i_packet_flags = p_peek[i_skip]; i_skip++; i_packet_property = p_peek[i_skip]; i_skip++; b_packet_multiple_payload = i_packet_flags&0x01; /* read some value */ GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min ); GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 ); GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 ); i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4; i_packet_duration = GetWLE( p_peek + i_skip ); i_skip += 2;// i_packet_size_left = i_packet_length; // XXX donn閑s reellement lu /* FIXME I have to do that for some file, I don't known why */ i_packet_size_left = i_data_packet_min; if( b_packet_multiple_payload ) { i_payload_count = p_peek[i_skip] & 0x3f; i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03; i_skip++; } else { i_payload_count = 1; i_payload_length_type = 0x02; // unused } for( i_payload = 0; i_payload < i_payload_count ; i_payload++ ) { asf_track_t *tk; int i_stream_number; int i_media_object_number; int i_media_object_offset; int i_replicated_data_length; int i_payload_data_length; int i_payload_data_pos; int i_sub_payload_data_length; int i_tmp; mtime_t i_pts; mtime_t i_pts_delta; if( i_skip >= i_packet_size_left ) { /* prevent some segfault with invalid file */ break; } i_stream_number = p_peek[i_skip] & 0x7f; i_skip++; GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 ); GETVALUE2b( i_packet_property >> 2, i_tmp, 0 ); GETVALUE2b( i_packet_property, i_replicated_data_length, 0 ); if( i_replicated_data_length > 1 ) // should be at least 8 bytes { i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000; i_skip += i_replicated_data_length; i_pts_delta = 0; i_media_object_offset = i_tmp; if( i_skip >= i_packet_size_left ) { break; } } else if( i_replicated_data_length == 1 ) { /* msg_Dbg( p_demux, "found compressed payload" ); */ i_pts = (mtime_t)i_tmp * 1000; i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++; i_media_object_offset = 0; } else { i_pts = (mtime_t)i_packet_send_time * 1000; i_pts_delta = 0; i_media_object_offset = i_tmp; } i_pts = __MAX( i_pts - p_sys->p_fp->i_preroll * 1000, 0 ); if( b_packet_multiple_payload ) { GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 ); } else { i_payload_data_length = i_packet_length - i_packet_padding_length - i_skip; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -